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 * @fixup_dt_cell_info: Will be called before a cell is added. Can be
87 * used to modify the nvmem_cell_info.
88 * @keepout: Optional array of keepout ranges (sorted ascending by start).
89 * @nkeepout: Number of elements in the keepout array.
90 * @type: Type of the nvmem storage
91 * @read_only: Device is read-only.
92 * @root_only: Device is accessibly to root only.
93 * @of_node: If given, this will be used instead of the parent's of_node.
94 * @no_of_node: Device should not use the parent's of_node even if it's !NULL.
95 * @reg_read: Callback to read data.
96 * @reg_write: Callback to write data.
97 * @size: Device size.
98 * @word_size: Minimum read/write access granularity.
99 * @stride: Minimum read/write access stride.
100 * @priv: User context passed to read/write callbacks.
101 * @ignore_wp: Write Protect pin is managed by the provider.
102 * @layout: Fixed layout associated with this nvmem device.
103 *
104 * Note: A default "nvmem<id>" name will be assigned to the device if
105 * no name is specified in its configuration. In such case "<id>" is
106 * generated with ida_simple_get() and provided id field is ignored.
107 *
108 * Note: Specifying name and setting id to -1 implies a unique device
109 * whose name is provided as-is (kept unaltered).
110 */
111 struct nvmem_config {
112 struct device *dev;
113 const char *name;
114 int id;
115 struct module *owner;
116 const struct nvmem_cell_info *cells;
117 int ncells;
118 bool add_legacy_fixed_of_cells;
119 void (*fixup_dt_cell_info)(struct nvmem_device *nvmem,
120 struct nvmem_cell_info *cell);
121 const struct nvmem_keepout *keepout;
122 unsigned int nkeepout;
123 enum nvmem_type type;
124 bool read_only;
125 bool root_only;
126 bool ignore_wp;
127 struct nvmem_layout *layout;
128 struct device_node *of_node;
129 bool no_of_node;
130 nvmem_reg_read_t reg_read;
131 nvmem_reg_write_t reg_write;
132 int size;
133 int word_size;
134 int stride;
135 void *priv;
136 /* To be only used by old driver/misc/eeprom drivers */
137 bool compat;
138 struct device *base_dev;
139 };
140
141 /**
142 * struct nvmem_cell_table - NVMEM cell definitions for given provider
143 *
144 * @nvmem_name: Provider name.
145 * @cells: Array of cell definitions.
146 * @ncells: Number of cell definitions in the array.
147 * @node: List node.
148 *
149 * This structure together with related helper functions is provided for users
150 * that don't can't access the nvmem provided structure but wish to register
151 * cell definitions for it e.g. board files registering an EEPROM device.
152 */
153 struct nvmem_cell_table {
154 const char *nvmem_name;
155 const struct nvmem_cell_info *cells;
156 size_t ncells;
157 struct list_head node;
158 };
159
160 /**
161 * struct nvmem_layout - NVMEM layout definitions
162 *
163 * @name: Layout name.
164 * @of_match_table: Open firmware match table.
165 * @add_cells: Called to populate the layout using
166 * nvmem_add_one_cell().
167 * @owner: Pointer to struct module.
168 * @node: List node.
169 *
170 * A nvmem device can hold a well defined structure which can just be
171 * evaluated during runtime. For example a TLV list, or a list of "name=val"
172 * pairs. A nvmem layout can parse the nvmem device and add appropriate
173 * cells.
174 */
175 struct nvmem_layout {
176 const char *name;
177 const struct of_device_id *of_match_table;
178 int (*add_cells)(struct device *dev, struct nvmem_device *nvmem);
179
180 /* private */
181 struct module *owner;
182 struct list_head node;
183 };
184
185 #if IS_ENABLED(CONFIG_NVMEM)
186
187 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
188 void nvmem_unregister(struct nvmem_device *nvmem);
189
190 struct nvmem_device *devm_nvmem_register(struct device *dev,
191 const struct nvmem_config *cfg);
192
193 void nvmem_add_cell_table(struct nvmem_cell_table *table);
194 void nvmem_del_cell_table(struct nvmem_cell_table *table);
195
196 int nvmem_add_one_cell(struct nvmem_device *nvmem,
197 const struct nvmem_cell_info *info);
198
199 int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
200 #define nvmem_layout_register(layout) \
201 __nvmem_layout_register(layout, THIS_MODULE)
202 void nvmem_layout_unregister(struct nvmem_layout *layout);
203
204 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
205 struct nvmem_layout *layout);
206
207 #else
208
nvmem_register(const struct nvmem_config * c)209 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
210 {
211 return ERR_PTR(-EOPNOTSUPP);
212 }
213
nvmem_unregister(struct nvmem_device * nvmem)214 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
215
216 static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)217 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
218 {
219 return nvmem_register(c);
220 }
221
nvmem_add_cell_table(struct nvmem_cell_table * table)222 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
nvmem_del_cell_table(struct nvmem_cell_table * table)223 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)224 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
225 const struct nvmem_cell_info *info)
226 {
227 return -EOPNOTSUPP;
228 }
229
nvmem_layout_register(struct nvmem_layout * layout)230 static inline int nvmem_layout_register(struct nvmem_layout *layout)
231 {
232 return -EOPNOTSUPP;
233 }
234
nvmem_layout_unregister(struct nvmem_layout * layout)235 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
236
237 static inline const void *
nvmem_layout_get_match_data(struct nvmem_device * nvmem,struct nvmem_layout * layout)238 nvmem_layout_get_match_data(struct nvmem_device *nvmem,
239 struct nvmem_layout *layout)
240 {
241 return NULL;
242 }
243
244 #endif /* CONFIG_NVMEM */
245
246 #define module_nvmem_layout_driver(__layout_driver) \
247 module_driver(__layout_driver, nvmem_layout_register, \
248 nvmem_layout_unregister)
249
250 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */
251