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/err.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
17 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
18 void *val, size_t bytes);
19 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
20 void *val, size_t bytes);
21 /* used for vendor specific post processing of cell data */
22 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
23 unsigned int offset, void *buf, size_t bytes);
26 NVMEM_TYPE_UNKNOWN = 0,
29 NVMEM_TYPE_BATTERY_BACKED,
33 #define NVMEM_DEVID_NONE (-1)
34 #define NVMEM_DEVID_AUTO (-2)
37 * struct nvmem_keepout - NVMEM register keepout range.
39 * @start: The first byte offset to avoid.
40 * @end: One beyond the last byte offset to avoid.
41 * @value: The byte to fill reads with for this region.
43 struct nvmem_keepout {
50 * struct nvmem_cell_info - NVMEM cell description
52 * @offset: Offset within the NVMEM device.
53 * @bytes: Length of the cell.
54 * @bit_offset: Bit offset if cell is smaller than a byte.
55 * @nbits: Number of bits.
56 * @np: Optional device_node pointer.
57 * @read_post_process: Callback for optional post processing of cell data
60 struct nvmem_cell_info {
64 unsigned int bit_offset;
66 struct device_node *np;
67 nvmem_cell_post_process_t read_post_process;
71 * struct nvmem_config - NVMEM device configuration
73 * @dev: Parent device.
74 * @name: Optional name.
75 * @id: Optional device ID used in full name. Ignored if name is NULL.
76 * @owner: Pointer to exporter module. Used for refcounting.
77 * @cells: Optional array of pre-defined NVMEM cells.
78 * @ncells: Number of elements in cells.
79 * @keepout: Optional array of keepout ranges (sorted ascending by start).
80 * @nkeepout: Number of elements in the keepout array.
81 * @type: Type of the nvmem storage
82 * @read_only: Device is read-only.
83 * @root_only: Device is accessibly to root only.
84 * @of_node: If given, this will be used instead of the parent's of_node.
85 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
86 * @reg_read: Callback to read data.
87 * @reg_write: Callback to write data.
88 * @cell_post_process: Callback for vendor specific post processing of cell data
90 * @word_size: Minimum read/write access granularity.
91 * @stride: Minimum read/write access stride.
92 * @priv: User context passed to read/write callbacks.
93 * @ignore_wp: Write Protect pin is managed by the provider.
94 * @layout: Fixed layout associated with this nvmem device.
96 * Note: A default "nvmem<id>" name will be assigned to the device if
97 * no name is specified in its configuration. In such case "<id>" is
98 * generated with ida_simple_get() and provided id field is ignored.
100 * Note: Specifying name and setting id to -1 implies a unique device
101 * whose name is provided as-is (kept unaltered).
103 struct nvmem_config {
107 struct module *owner;
108 const struct nvmem_cell_info *cells;
110 const struct nvmem_keepout *keepout;
111 unsigned int nkeepout;
112 enum nvmem_type type;
116 struct nvmem_layout *layout;
117 struct device_node *of_node;
119 nvmem_reg_read_t reg_read;
120 nvmem_reg_write_t reg_write;
121 nvmem_cell_post_process_t cell_post_process;
126 /* To be only used by old driver/misc/eeprom drivers */
128 struct device *base_dev;
132 * struct nvmem_cell_table - NVMEM cell definitions for given provider
134 * @nvmem_name: Provider name.
135 * @cells: Array of cell definitions.
136 * @ncells: Number of cell definitions in the array.
139 * This structure together with related helper functions is provided for users
140 * that don't can't access the nvmem provided structure but wish to register
141 * cell definitions for it e.g. board files registering an EEPROM device.
143 struct nvmem_cell_table {
144 const char *nvmem_name;
145 const struct nvmem_cell_info *cells;
147 struct list_head node;
151 * struct nvmem_layout - NVMEM layout definitions
153 * @name: Layout name.
154 * @of_match_table: Open firmware match table.
155 * @add_cells: Will be called if a nvmem device is found which
156 * has this layout. The function will add layout
157 * specific cells with nvmem_add_one_cell().
158 * @fixup_cell_info: Will be called before a cell is added. Can be
159 * used to modify the nvmem_cell_info.
160 * @owner: Pointer to struct module.
163 * A nvmem device can hold a well defined structure which can just be
164 * evaluated during runtime. For example a TLV list, or a list of "name=val"
165 * pairs. A nvmem layout can parse the nvmem device and add appropriate
168 struct nvmem_layout {
170 const struct of_device_id *of_match_table;
171 int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
172 struct nvmem_layout *layout);
173 void (*fixup_cell_info)(struct nvmem_device *nvmem,
174 struct nvmem_layout *layout,
175 struct nvmem_cell_info *cell);
178 struct module *owner;
179 struct list_head node;
182 #if IS_ENABLED(CONFIG_NVMEM)
184 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
185 void nvmem_unregister(struct nvmem_device *nvmem);
187 struct nvmem_device *devm_nvmem_register(struct device *dev,
188 const struct nvmem_config *cfg);
190 void nvmem_add_cell_table(struct nvmem_cell_table *table);
191 void nvmem_del_cell_table(struct nvmem_cell_table *table);
193 int nvmem_add_one_cell(struct nvmem_device *nvmem,
194 const struct nvmem_cell_info *info);
196 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
197 #define nvmem_layout_register(layout) \
198 __nvmem_layout_register(layout, THIS_MODULE)
199 void nvmem_layout_unregister(struct nvmem_layout *layout);
201 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
202 struct nvmem_layout *layout);
206 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
208 return ERR_PTR(-EOPNOTSUPP);
211 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
213 static inline struct nvmem_device *
214 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
216 return nvmem_register(c);
219 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
220 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
221 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
222 const struct nvmem_cell_info *info)
227 static inline int nvmem_layout_register(struct nvmem_layout *layout)
232 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
234 static inline const void *
235 nvmem_layout_get_match_data(struct nvmem_device *nvmem,
236 struct nvmem_layout *layout)
241 #endif /* CONFIG_NVMEM */
242 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */