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
9 #ifndef _LINUX_NVMEM_PROVIDER_H
10 #define _LINUX_NVMEM_PROVIDER_H
11
12 #include <linux/device/driver.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gpio/consumer.h>
16
17 struct nvmem_device;
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,
25 size_t bytes);
26
27 enum nvmem_type {
28 NVMEM_TYPE_UNKNOWN = 0,
29 NVMEM_TYPE_EEPROM,
30 NVMEM_TYPE_OTP,
31 NVMEM_TYPE_BATTERY_BACKED,
32 NVMEM_TYPE_FRAM,
33 };
34
35 #define NVMEM_DEVID_NONE (-1)
36 #define NVMEM_DEVID_AUTO (-2)
37
38 /**
39 * struct nvmem_keepout - NVMEM register keepout range.
40 *
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.
44 */
45 struct nvmem_keepout {
46 unsigned int start;
47 unsigned int end;
48 unsigned char value;
49 };
50
51 /**
52 * struct nvmem_cell_info - NVMEM cell description
53 * @name: Name.
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
61 * on reads.
62 * @priv: Opaque data passed to the read_post_process hook.
63 */
64 struct nvmem_cell_info {
65 const char *name;
66 unsigned int offset;
67 size_t raw_len;
68 unsigned int bytes;
69 unsigned int bit_offset;
70 unsigned int nbits;
71 struct device_node *np;
72 nvmem_cell_post_process_t read_post_process;
73 void *priv;
74 };
75
76 /**
77 * struct nvmem_config - NVMEM device configuration
78 *
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 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
93 * @reg_read: Callback to read data.
94 * @reg_write: Callback to write data.
95 * @size: Device size.
96 * @word_size: Minimum read/write access granularity.
97 * @stride: Minimum read/write access stride.
98 * @priv: User context passed to read/write callbacks.
99 * @ignore_wp: Write Protect pin is managed by the provider.
100 * @layout: Fixed layout associated with this nvmem device.
101 *
102 * Note: A default "nvmem<id>" name will be assigned to the device if
103 * no name is specified in its configuration. In such case "<id>" is
104 * generated with ida_simple_get() and provided id field is ignored.
105 *
106 * Note: Specifying name and setting id to -1 implies a unique device
107 * whose name is provided as-is (kept unaltered).
108 */
109 struct nvmem_config {
110 struct device *dev;
111 const char *name;
112 int id;
113 struct module *owner;
114 const struct nvmem_cell_info *cells;
115 int ncells;
116 bool add_legacy_fixed_of_cells;
117 const struct nvmem_keepout *keepout;
118 unsigned int nkeepout;
119 enum nvmem_type type;
120 bool read_only;
121 bool root_only;
122 bool ignore_wp;
123 struct nvmem_layout *layout;
124 struct device_node *of_node;
125 bool no_of_node;
126 nvmem_reg_read_t reg_read;
127 nvmem_reg_write_t reg_write;
128 int size;
129 int word_size;
130 int stride;
131 void *priv;
132 /* To be only used by old driver/misc/eeprom drivers */
133 bool compat;
134 struct device *base_dev;
135 };
136
137 /**
138 * struct nvmem_cell_table - NVMEM cell definitions for given provider
139 *
140 * @nvmem_name: Provider name.
141 * @cells: Array of cell definitions.
142 * @ncells: Number of cell definitions in the array.
143 * @node: List node.
144 *
145 * This structure together with related helper functions is provided for users
146 * that don't can't access the nvmem provided structure but wish to register
147 * cell definitions for it e.g. board files registering an EEPROM device.
148 */
149 struct nvmem_cell_table {
150 const char *nvmem_name;
151 const struct nvmem_cell_info *cells;
152 size_t ncells;
153 struct list_head node;
154 };
155
156 /**
157 * struct nvmem_layout - NVMEM layout definitions
158 *
159 * @name: Layout name.
160 * @of_match_table: Open firmware match table.
161 * @add_cells: Will be called if a nvmem device is found which
162 * has this layout. The function will add layout
163 * specific cells with nvmem_add_one_cell().
164 * @fixup_cell_info: Will be called before a cell is added. Can be
165 * used to modify the nvmem_cell_info.
166 * @owner: Pointer to struct module.
167 * @node: List node.
168 *
169 * A nvmem device can hold a well defined structure which can just be
170 * evaluated during runtime. For example a TLV list, or a list of "name=val"
171 * pairs. A nvmem layout can parse the nvmem device and add appropriate
172 * cells.
173 */
174 struct nvmem_layout {
175 const char *name;
176 const struct of_device_id *of_match_table;
177 int (*add_cells)(struct device *dev, struct nvmem_device *nvmem,
178 struct nvmem_layout *layout);
179 void (*fixup_cell_info)(struct nvmem_device *nvmem,
180 struct nvmem_layout *layout,
181 struct nvmem_cell_info *cell);
182
183 /* private */
184 struct module *owner;
185 struct list_head node;
186 };
187
188 #if IS_ENABLED(CONFIG_NVMEM)
189
190 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
191 void nvmem_unregister(struct nvmem_device *nvmem);
192
193 struct nvmem_device *devm_nvmem_register(struct device *dev,
194 const struct nvmem_config *cfg);
195
196 void nvmem_add_cell_table(struct nvmem_cell_table *table);
197 void nvmem_del_cell_table(struct nvmem_cell_table *table);
198
199 int nvmem_add_one_cell(struct nvmem_device *nvmem,
200 const struct nvmem_cell_info *info);
201
202 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
203 #define nvmem_layout_register(layout) \
204 __nvmem_layout_register(layout, THIS_MODULE)
205 void nvmem_layout_unregister(struct nvmem_layout *layout);
206
207 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
208 struct nvmem_layout *layout);
209
210 #else
211
nvmem_register(const struct nvmem_config * c)212 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
213 {
214 return ERR_PTR(-EOPNOTSUPP);
215 }
216
nvmem_unregister(struct nvmem_device * nvmem)217 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
218
219 static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)220 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
221 {
222 return nvmem_register(c);
223 }
224
nvmem_add_cell_table(struct nvmem_cell_table * table)225 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
nvmem_del_cell_table(struct nvmem_cell_table * table)226 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
nvmem_add_one_cell(struct nvmem_device * nvmem,const struct nvmem_cell_info * info)227 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
228 const struct nvmem_cell_info *info)
229 {
230 return -EOPNOTSUPP;
231 }
232
nvmem_layout_register(struct nvmem_layout * layout)233 static inline int nvmem_layout_register(struct nvmem_layout *layout)
234 {
235 return -EOPNOTSUPP;
236 }
237
nvmem_layout_unregister(struct nvmem_layout * layout)238 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
239
240 static inline const void *
nvmem_layout_get_match_data(struct nvmem_device * nvmem,struct nvmem_layout * layout)241 nvmem_layout_get_match_data(struct nvmem_device *nvmem,
242 struct nvmem_layout *layout)
243 {
244 return NULL;
245 }
246
247 #endif /* CONFIG_NVMEM */
248
249 #define module_nvmem_layout_driver(__layout_driver) \
250 module_driver(__layout_driver, nvmem_layout_register, \
251 nvmem_layout_unregister)
252
253 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */
254