nvmem-provider.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * nvmem framework provider.
  4. *
  5. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  6. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  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. enum nvmem_type {
  20. NVMEM_TYPE_UNKNOWN = 0,
  21. NVMEM_TYPE_EEPROM,
  22. NVMEM_TYPE_OTP,
  23. NVMEM_TYPE_BATTERY_BACKED,
  24. };
  25. #define NVMEM_DEVID_NONE (-1)
  26. #define NVMEM_DEVID_AUTO (-2)
  27. /**
  28. * struct nvmem_config - NVMEM device configuration
  29. *
  30. * @dev: Parent device.
  31. * @name: Optional name.
  32. * @id: Optional device ID used in full name. Ignored if name is NULL.
  33. * @owner: Pointer to exporter module. Used for refcounting.
  34. * @cells: Optional array of pre-defined NVMEM cells.
  35. * @ncells: Number of elements in cells.
  36. * @type: Type of the nvmem storage
  37. * @read_only: Device is read-only.
  38. * @root_only: Device is accessibly to root only.
  39. * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
  40. * @reg_read: Callback to read data.
  41. * @reg_write: Callback to write data.
  42. * @size: Device size.
  43. * @word_size: Minimum read/write access granularity.
  44. * @stride: Minimum read/write access stride.
  45. * @priv: User context passed to read/write callbacks.
  46. * @wp-gpio: Write protect pin
  47. *
  48. * Note: A default "nvmem<id>" name will be assigned to the device if
  49. * no name is specified in its configuration. In such case "<id>" is
  50. * generated with ida_simple_get() and provided id field is ignored.
  51. *
  52. * Note: Specifying name and setting id to -1 implies a unique device
  53. * whose name is provided as-is (kept unaltered).
  54. */
  55. struct nvmem_config {
  56. struct device *dev;
  57. const char *name;
  58. int id;
  59. struct module *owner;
  60. struct gpio_desc *wp_gpio;
  61. const struct nvmem_cell_info *cells;
  62. int ncells;
  63. enum nvmem_type type;
  64. bool read_only;
  65. bool root_only;
  66. bool no_of_node;
  67. nvmem_reg_read_t reg_read;
  68. nvmem_reg_write_t reg_write;
  69. int size;
  70. int word_size;
  71. int stride;
  72. void *priv;
  73. /* To be only used by old driver/misc/eeprom drivers */
  74. bool compat;
  75. struct device *base_dev;
  76. };
  77. /**
  78. * struct nvmem_cell_table - NVMEM cell definitions for given provider
  79. *
  80. * @nvmem_name: Provider name.
  81. * @cells: Array of cell definitions.
  82. * @ncells: Number of cell definitions in the array.
  83. * @node: List node.
  84. *
  85. * This structure together with related helper functions is provided for users
  86. * that don't can't access the nvmem provided structure but wish to register
  87. * cell definitions for it e.g. board files registering an EEPROM device.
  88. */
  89. struct nvmem_cell_table {
  90. const char *nvmem_name;
  91. const struct nvmem_cell_info *cells;
  92. size_t ncells;
  93. struct list_head node;
  94. };
  95. #if IS_ENABLED(CONFIG_NVMEM)
  96. struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
  97. void nvmem_unregister(struct nvmem_device *nvmem);
  98. struct nvmem_device *devm_nvmem_register(struct device *dev,
  99. const struct nvmem_config *cfg);
  100. int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
  101. void nvmem_add_cell_table(struct nvmem_cell_table *table);
  102. void nvmem_del_cell_table(struct nvmem_cell_table *table);
  103. #else
  104. static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
  105. {
  106. return ERR_PTR(-EOPNOTSUPP);
  107. }
  108. static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
  109. static inline struct nvmem_device *
  110. devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
  111. {
  112. return nvmem_register(c);
  113. }
  114. static inline int
  115. devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
  116. {
  117. return -EOPNOTSUPP;
  118. }
  119. static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
  120. static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
  121. #endif /* CONFIG_NVMEM */
  122. #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */