1 /* SPDX-License-Identifier: GPL-2.0 */
3 * nvmem framework provider.
5 * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
6 * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
9 #ifndef _LINUX_NVMEM_PROVIDER_H
10 #define _LINUX_NVMEM_PROVIDER_H
12 #include <linux/device/driver.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gpio/consumer.h>
18 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
19 void *val, size_t bytes);
20 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
21 void *val, size_t bytes);
22 /* used for vendor specific post processing of cell data */
23 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
24 unsigned int offset, void *buf,
28 NVMEM_TYPE_UNKNOWN = 0,
31 NVMEM_TYPE_BATTERY_BACKED,
35 #define NVMEM_DEVID_NONE (-1)
36 #define NVMEM_DEVID_AUTO (-2)
39 * struct nvmem_keepout - NVMEM register keepout range.
41 * @start: The first byte offset to avoid.
42 * @end: One beyond the last byte offset to avoid.
43 * @value: The byte to fill reads with for this region.
45 struct nvmem_keepout {
52 * struct nvmem_cell_info - NVMEM cell description
54 * @offset: Offset within the NVMEM device.
55 * @raw_len: Length of raw data (without post processing).
56 * @bytes: Length of the cell.
57 * @bit_offset: Bit offset if cell is smaller than a byte.
58 * @nbits: Number of bits.
59 * @np: Optional device_node pointer.
60 * @read_post_process: Callback for optional post processing of cell data
62 * @priv: Opaque data passed to the read_post_process hook.
64 struct nvmem_cell_info {
69 unsigned int bit_offset;
71 struct device_node *np;
72 nvmem_cell_post_process_t read_post_process;
77 * struct nvmem_config - NVMEM device configuration
79 * @dev: Parent device.
80 * @name: Optional name.
81 * @id: Optional device ID used in full name. Ignored if name is NULL.
82 * @owner: Pointer to exporter module. Used for refcounting.
83 * @cells: Optional array of pre-defined NVMEM cells.
84 * @ncells: Number of elements in cells.
85 * @add_legacy_fixed_of_cells: Read fixed NVMEM cells from old OF syntax.
86 * @keepout: Optional array of keepout ranges (sorted ascending by start).
87 * @nkeepout: Number of elements in the keepout array.
88 * @type: Type of the nvmem storage
89 * @read_only: Device is read-only.
90 * @root_only: Device is accessibly to root only.
91 * @of_node: If given, this will be used instead of the parent's of_node.
92 * @reg_read: Callback to read data.
93 * @reg_write: Callback to write data.
95 * @word_size: Minimum read/write access granularity.
96 * @stride: Minimum read/write access stride.
97 * @priv: User context passed to read/write callbacks.
98 * @ignore_wp: Write Protect pin is managed by the provider.
99 * @layout: Fixed layout associated with this nvmem device.
101 * Note: A default "nvmem<id>" name will be assigned to the device if
102 * no name is specified in its configuration. In such case "<id>" is
103 * generated with ida_simple_get() and provided id field is ignored.
105 * Note: Specifying name and setting id to -1 implies a unique device
106 * whose name is provided as-is (kept unaltered).
108 struct nvmem_config {
112 struct module *owner;
113 const struct nvmem_cell_info *cells;
115 bool add_legacy_fixed_of_cells;
116 const struct nvmem_keepout *keepout;
117 unsigned int nkeepout;
118 enum nvmem_type type;
122 struct nvmem_layout *layout;
123 struct device_node *of_node;
124 nvmem_reg_read_t reg_read;
125 nvmem_reg_write_t reg_write;
130 /* To be only used by old driver/misc/eeprom drivers */
132 struct device *base_dev;
136 * struct nvmem_cell_table - NVMEM cell definitions for given provider
138 * @nvmem_name: Provider name.
139 * @cells: Array of cell definitions.
140 * @ncells: Number of cell definitions in the array.
143 * This structure together with related helper functions is provided for users
144 * that don't can't access the nvmem provided structure but wish to register
145 * cell definitions for it e.g. board files registering an EEPROM device.
147 struct nvmem_cell_table {
148 const char *nvmem_name;
149 const struct nvmem_cell_info *cells;
151 struct list_head node;
155 * struct nvmem_layout - NVMEM layout definitions
157 * @name: Layout name.
158 * @of_match_table: Open firmware match table.
159 * @add_cells: Will be called if a nvmem device is found which
160 * has this layout. The function will add layout
161 * specific cells with nvmem_add_one_cell().
162 * @fixup_cell_info: Will be called before a cell is added. Can be
163 * used to modify the nvmem_cell_info.
164 * @owner: Pointer to struct module.
167 * A nvmem device can hold a well defined structure which can just be
168 * evaluated during runtime. For example a TLV list, or a list of "name=val"
169 * pairs. A nvmem layout can parse the nvmem device and add appropriate
172 struct nvmem_layout {
174 const struct of_device_id *of_match_table;
175 int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
176 struct nvmem_layout *layout);
177 void (*fixup_cell_info)(struct nvmem_device *nvmem,
178 struct nvmem_layout *layout,
179 struct nvmem_cell_info *cell);
182 struct module *owner;
183 struct list_head node;
186 #if IS_ENABLED(CONFIG_NVMEM)
188 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
189 void nvmem_unregister(struct nvmem_device *nvmem);
191 struct nvmem_device *devm_nvmem_register(struct device *dev,
192 const struct nvmem_config *cfg);
194 void nvmem_add_cell_table(struct nvmem_cell_table *table);
195 void nvmem_del_cell_table(struct nvmem_cell_table *table);
197 int nvmem_add_one_cell(struct nvmem_device *nvmem,
198 const struct nvmem_cell_info *info);
200 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
201 #define nvmem_layout_register(layout) \
202 __nvmem_layout_register(layout, THIS_MODULE)
203 void nvmem_layout_unregister(struct nvmem_layout *layout);
205 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
206 struct nvmem_layout *layout);
210 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
212 return ERR_PTR(-EOPNOTSUPP);
215 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
217 static inline struct nvmem_device *
218 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
220 return nvmem_register(c);
223 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
224 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
225 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
226 const struct nvmem_cell_info *info)
231 static inline int nvmem_layout_register(struct nvmem_layout *layout)
236 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
238 static inline const void *
239 nvmem_layout_get_match_data(struct nvmem_device *nvmem,
240 struct nvmem_layout *layout)
245 #endif /* CONFIG_NVMEM */
247 #define module_nvmem_layout_driver(__layout_driver) \
248 module_driver(__layout_driver, nvmem_layout_register, \
249 nvmem_layout_unregister)
251 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */