xref: /linux/include/linux/nvmem-provider.h (revision c26f4fbd58375bd6ef74f95eb73d61762ad97c59)
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.h>
13 #include <linux/device/driver.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/gpio/consumer.h>
17 
18 struct nvmem_device;
19 typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
20 				void *val, size_t bytes);
21 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
22 				 void *val, size_t bytes);
23 /* used for vendor specific post processing of cell data */
24 typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
25 					 unsigned int offset, void *buf,
26 					 size_t bytes);
27 
28 enum nvmem_type {
29 	NVMEM_TYPE_UNKNOWN = 0,
30 	NVMEM_TYPE_EEPROM,
31 	NVMEM_TYPE_OTP,
32 	NVMEM_TYPE_BATTERY_BACKED,
33 	NVMEM_TYPE_FRAM,
34 };
35 
36 #define NVMEM_DEVID_NONE	(-1)
37 #define NVMEM_DEVID_AUTO	(-2)
38 
39 /**
40  * struct nvmem_keepout - NVMEM register keepout range.
41  *
42  * @start:	The first byte offset to avoid.
43  * @end:	One beyond the last byte offset to avoid.
44  * @value:	The byte to fill reads with for this region.
45  */
46 struct nvmem_keepout {
47 	unsigned int start;
48 	unsigned int end;
49 	unsigned char value;
50 };
51 
52 /**
53  * struct nvmem_cell_info - NVMEM cell description
54  * @name:	Name.
55  * @offset:	Offset within the NVMEM device.
56  * @raw_len:	Length of raw data (without post processing).
57  * @bytes:	Length of the cell.
58  * @bit_offset:	Bit offset if cell is smaller than a byte.
59  * @nbits:	Number of bits.
60  * @np:		Optional device_node pointer.
61  * @read_post_process:	Callback for optional post processing of cell data
62  *			on reads.
63  * @priv:	Opaque data passed to the read_post_process hook.
64  */
65 struct nvmem_cell_info {
66 	const char		*name;
67 	unsigned int		offset;
68 	size_t			raw_len;
69 	unsigned int		bytes;
70 	unsigned int		bit_offset;
71 	unsigned int		nbits;
72 	struct device_node	*np;
73 	nvmem_cell_post_process_t read_post_process;
74 	void			*priv;
75 };
76 
77 /**
78  * struct nvmem_config - NVMEM device configuration
79  *
80  * @dev:	Parent device.
81  * @name:	Optional name.
82  * @id:		Optional device ID used in full name. Ignored if name is NULL.
83  * @owner:	Pointer to exporter module. Used for refcounting.
84  * @cells:	Optional array of pre-defined NVMEM cells.
85  * @ncells:	Number of elements in cells.
86  * @add_legacy_fixed_of_cells:	Read fixed NVMEM cells from old OF syntax.
87  * @fixup_dt_cell_info: Will be called before a cell is added. Can be
88  *		used to modify the nvmem_cell_info.
89  * @keepout:	Optional array of keepout ranges (sorted ascending by start).
90  * @nkeepout:	Number of elements in the keepout array.
91  * @type:	Type of the nvmem storage
92  * @read_only:	Device is read-only.
93  * @root_only:	Device is accessibly to root only.
94  * @of_node:	If given, this will be used instead of the parent's of_node.
95  * @reg_read:	Callback to read data; return zero if successful.
96  * @reg_write:	Callback to write data; return zero if successful.
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 	nvmem_reg_read_t	reg_read;
130 	nvmem_reg_write_t	reg_write;
131 	int	size;
132 	int	word_size;
133 	int	stride;
134 	void	*priv;
135 	/* To be only used by old driver/misc/eeprom drivers */
136 	bool			compat;
137 	struct device		*base_dev;
138 };
139 
140 /**
141  * struct nvmem_layout - NVMEM layout definitions
142  *
143  * @dev:		Device-model layout device.
144  * @nvmem:		The underlying NVMEM device
145  * @add_cells:		Will be called if a nvmem device is found which
146  *			has this layout. The function will add layout
147  *			specific cells with nvmem_add_one_cell().
148  *
149  * A nvmem device can hold a well defined structure which can just be
150  * evaluated during runtime. For example a TLV list, or a list of "name=val"
151  * pairs. A nvmem layout can parse the nvmem device and add appropriate
152  * cells.
153  */
154 struct nvmem_layout {
155 	struct device dev;
156 	struct nvmem_device *nvmem;
157 	int (*add_cells)(struct nvmem_layout *layout);
158 };
159 
160 struct nvmem_layout_driver {
161 	struct device_driver driver;
162 	int (*probe)(struct nvmem_layout *layout);
163 	void (*remove)(struct nvmem_layout *layout);
164 };
165 
166 #if IS_ENABLED(CONFIG_NVMEM)
167 
168 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
169 void nvmem_unregister(struct nvmem_device *nvmem);
170 
171 struct nvmem_device *devm_nvmem_register(struct device *dev,
172 					 const struct nvmem_config *cfg);
173 
174 int nvmem_add_one_cell(struct nvmem_device *nvmem,
175 		       const struct nvmem_cell_info *info);
176 
177 int nvmem_layout_register(struct nvmem_layout *layout);
178 void nvmem_layout_unregister(struct nvmem_layout *layout);
179 
180 #define nvmem_layout_driver_register(drv) \
181 	__nvmem_layout_driver_register(drv, THIS_MODULE)
182 int __nvmem_layout_driver_register(struct nvmem_layout_driver *drv,
183 				   struct module *owner);
184 void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv);
185 #define module_nvmem_layout_driver(__nvmem_layout_driver)		\
186 	module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \
187 		      nvmem_layout_driver_unregister)
188 
189 #else
190 
nvmem_register(const struct nvmem_config * c)191 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
192 {
193 	return ERR_PTR(-EOPNOTSUPP);
194 }
195 
nvmem_unregister(struct nvmem_device * nvmem)196 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
197 
198 static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)199 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
200 {
201 	return nvmem_register(c);
202 }
203 
nvmem_add_one_cell(struct nvmem_device * nvmem,const struct nvmem_cell_info * info)204 static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
205 				     const struct nvmem_cell_info *info)
206 {
207 	return -EOPNOTSUPP;
208 }
209 
nvmem_layout_register(struct nvmem_layout * layout)210 static inline int nvmem_layout_register(struct nvmem_layout *layout)
211 {
212 	return -EOPNOTSUPP;
213 }
214 
nvmem_layout_unregister(struct nvmem_layout * layout)215 static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
216 
217 #endif /* CONFIG_NVMEM */
218 
219 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
220 
221 /**
222  * of_nvmem_layout_get_container() - Get OF node of layout container
223  *
224  * @nvmem: nvmem device
225  *
226  * Return: a node pointer with refcount incremented or NULL if no
227  * container exists. Use of_node_put() on it when done.
228  */
229 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem);
230 
231 #else  /* CONFIG_NVMEM && CONFIG_OF */
232 
of_nvmem_layout_get_container(struct nvmem_device * nvmem)233 static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
234 {
235 	return NULL;
236 }
237 
238 #endif /* CONFIG_NVMEM && CONFIG_OF */
239 
240 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
241