nvmem-provider.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * nvmem framework provider.
  4. *
  5. * Copyright (C) 2015 Srinivas Kandagatla <[email protected]>
  6. * Copyright (C) 2013 Maxime Ripard <[email protected]>
  7. */
  8. #ifndef _LINUX_NVMEM_PROVIDER_H
  9. #define _LINUX_NVMEM_PROVIDER_H
  10. #include <linux/err.h>
  11. #include <linux/errno.h>
  12. #include <linux/gpio/consumer.h>
  13. struct nvmem_device;
  14. struct nvmem_cell_info;
  15. typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
  16. void *val, size_t bytes);
  17. typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
  18. void *val, size_t bytes);
  19. /* used for vendor specific post processing of cell data */
  20. typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, unsigned int offset,
  21. void *buf, size_t bytes);
  22. enum nvmem_type {
  23. NVMEM_TYPE_UNKNOWN = 0,
  24. NVMEM_TYPE_EEPROM,
  25. NVMEM_TYPE_OTP,
  26. NVMEM_TYPE_BATTERY_BACKED,
  27. NVMEM_TYPE_FRAM,
  28. };
  29. #define NVMEM_DEVID_NONE (-1)
  30. #define NVMEM_DEVID_AUTO (-2)
  31. /**
  32. * struct nvmem_keepout - NVMEM register keepout range.
  33. *
  34. * @start: The first byte offset to avoid.
  35. * @end: One beyond the last byte offset to avoid.
  36. * @value: The byte to fill reads with for this region.
  37. */
  38. struct nvmem_keepout {
  39. unsigned int start;
  40. unsigned int end;
  41. unsigned char value;
  42. };
  43. /**
  44. * struct nvmem_config - NVMEM device configuration
  45. *
  46. * @dev: Parent device.
  47. * @name: Optional name.
  48. * @id: Optional device ID used in full name. Ignored if name is NULL.
  49. * @owner: Pointer to exporter module. Used for refcounting.
  50. * @cells: Optional array of pre-defined NVMEM cells.
  51. * @ncells: Number of elements in cells.
  52. * @keepout: Optional array of keepout ranges (sorted ascending by start).
  53. * @nkeepout: Number of elements in the keepout array.
  54. * @type: Type of the nvmem storage
  55. * @read_only: Device is read-only.
  56. * @root_only: Device is accessibly to root only.
  57. * @of_node: If given, this will be used instead of the parent's of_node.
  58. * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
  59. * @reg_read: Callback to read data.
  60. * @reg_write: Callback to write data.
  61. * @cell_post_process: Callback for vendor specific post processing of cell data
  62. * @size: Device size.
  63. * @word_size: Minimum read/write access granularity.
  64. * @stride: Minimum read/write access stride.
  65. * @priv: User context passed to read/write callbacks.
  66. * @ignore_wp: Write Protect pin is managed by the provider.
  67. *
  68. * Note: A default "nvmem<id>" name will be assigned to the device if
  69. * no name is specified in its configuration. In such case "<id>" is
  70. * generated with ida_simple_get() and provided id field is ignored.
  71. *
  72. * Note: Specifying name and setting id to -1 implies a unique device
  73. * whose name is provided as-is (kept unaltered).
  74. */
  75. struct nvmem_config {
  76. struct device *dev;
  77. const char *name;
  78. int id;
  79. struct module *owner;
  80. const struct nvmem_cell_info *cells;
  81. int ncells;
  82. const struct nvmem_keepout *keepout;
  83. unsigned int nkeepout;
  84. enum nvmem_type type;
  85. bool read_only;
  86. bool root_only;
  87. bool ignore_wp;
  88. struct device_node *of_node;
  89. bool no_of_node;
  90. nvmem_reg_read_t reg_read;
  91. nvmem_reg_write_t reg_write;
  92. nvmem_cell_post_process_t cell_post_process;
  93. int size;
  94. int word_size;
  95. int stride;
  96. void *priv;
  97. /* To be only used by old driver/misc/eeprom drivers */
  98. bool compat;
  99. struct device *base_dev;
  100. };
  101. /**
  102. * struct nvmem_cell_table - NVMEM cell definitions for given provider
  103. *
  104. * @nvmem_name: Provider name.
  105. * @cells: Array of cell definitions.
  106. * @ncells: Number of cell definitions in the array.
  107. * @node: List node.
  108. *
  109. * This structure together with related helper functions is provided for users
  110. * that don't can't access the nvmem provided structure but wish to register
  111. * cell definitions for it e.g. board files registering an EEPROM device.
  112. */
  113. struct nvmem_cell_table {
  114. const char *nvmem_name;
  115. const struct nvmem_cell_info *cells;
  116. size_t ncells;
  117. struct list_head node;
  118. };
  119. #if IS_ENABLED(CONFIG_NVMEM)
  120. struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
  121. void nvmem_unregister(struct nvmem_device *nvmem);
  122. struct nvmem_device *devm_nvmem_register(struct device *dev,
  123. const struct nvmem_config *cfg);
  124. void nvmem_add_cell_table(struct nvmem_cell_table *table);
  125. void nvmem_del_cell_table(struct nvmem_cell_table *table);
  126. #else
  127. static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
  128. {
  129. return ERR_PTR(-EOPNOTSUPP);
  130. }
  131. static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
  132. static inline struct nvmem_device *
  133. devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
  134. {
  135. return nvmem_register(c);
  136. }
  137. static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
  138. static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
  139. #endif /* CONFIG_NVMEM */
  140. #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */