xref: /linux-6.15/include/linux/nvmem-provider.h (revision 6fdbc7b9)
1b1c1db98SBartosz Golaszewski /* SPDX-License-Identifier: GPL-2.0 */
2eace75cfSSrinivas Kandagatla /*
3eace75cfSSrinivas Kandagatla  * nvmem framework provider.
4eace75cfSSrinivas Kandagatla  *
5eace75cfSSrinivas Kandagatla  * Copyright (C) 2015 Srinivas Kandagatla <[email protected]>
6eace75cfSSrinivas Kandagatla  * Copyright (C) 2013 Maxime Ripard <[email protected]>
7eace75cfSSrinivas Kandagatla  */
8eace75cfSSrinivas Kandagatla 
9eace75cfSSrinivas Kandagatla #ifndef _LINUX_NVMEM_PROVIDER_H
10eace75cfSSrinivas Kandagatla #define _LINUX_NVMEM_PROVIDER_H
11eace75cfSSrinivas Kandagatla 
12fc29fd82SMiquel Raynal #include <linux/device.h>
13814c978fSMiquel Raynal #include <linux/device/driver.h>
1442a6e099SArnd Bergmann #include <linux/err.h>
1542a6e099SArnd Bergmann #include <linux/errno.h>
162a127da4SKhouloud Touil #include <linux/gpio/consumer.h>
1742a6e099SArnd Bergmann 
18eace75cfSSrinivas Kandagatla struct nvmem_device;
19795ddd18SSrinivas Kandagatla typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
20795ddd18SSrinivas Kandagatla 				void *val, size_t bytes);
21795ddd18SSrinivas Kandagatla typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
22795ddd18SSrinivas Kandagatla 				 void *val, size_t bytes);
235008062fSSrinivas Kandagatla /* used for vendor specific post processing of cell data */
245d8e6e6cSMichael Walle typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
258a134fd9SMichael Walle 					 unsigned int offset, void *buf,
268a134fd9SMichael Walle 					 size_t bytes);
27eace75cfSSrinivas Kandagatla 
2816688453SAlexandre Belloni enum nvmem_type {
2916688453SAlexandre Belloni 	NVMEM_TYPE_UNKNOWN = 0,
3016688453SAlexandre Belloni 	NVMEM_TYPE_EEPROM,
3116688453SAlexandre Belloni 	NVMEM_TYPE_OTP,
3216688453SAlexandre Belloni 	NVMEM_TYPE_BATTERY_BACKED,
33fd307a4aSJiri Prchal 	NVMEM_TYPE_FRAM,
3416688453SAlexandre Belloni };
3516688453SAlexandre Belloni 
36731aa3faSSrinivas Kandagatla #define NVMEM_DEVID_NONE	(-1)
37731aa3faSSrinivas Kandagatla #define NVMEM_DEVID_AUTO	(-2)
38731aa3faSSrinivas Kandagatla 
390b2ed745SAndrey Smirnov /**
40fd3bb8f5SEvan Green  * struct nvmem_keepout - NVMEM register keepout range.
41fd3bb8f5SEvan Green  *
42fd3bb8f5SEvan Green  * @start:	The first byte offset to avoid.
43fd3bb8f5SEvan Green  * @end:	One beyond the last byte offset to avoid.
44fd3bb8f5SEvan Green  * @value:	The byte to fill reads with for this region.
45fd3bb8f5SEvan Green  */
46fd3bb8f5SEvan Green struct nvmem_keepout {
47fd3bb8f5SEvan Green 	unsigned int start;
48fd3bb8f5SEvan Green 	unsigned int end;
49fd3bb8f5SEvan Green 	unsigned char value;
50fd3bb8f5SEvan Green };
51fd3bb8f5SEvan Green 
52fd3bb8f5SEvan Green /**
53fbd03d27SMichael Walle  * struct nvmem_cell_info - NVMEM cell description
54fbd03d27SMichael Walle  * @name:	Name.
55fbd03d27SMichael Walle  * @offset:	Offset within the NVMEM device.
5655d4980cSRafał Miłecki  * @raw_len:	Length of raw data (without post processing).
57fbd03d27SMichael Walle  * @bytes:	Length of the cell.
58fbd03d27SMichael Walle  * @bit_offset:	Bit offset if cell is smaller than a byte.
59fbd03d27SMichael Walle  * @nbits:	Number of bits.
60fbd03d27SMichael Walle  * @np:		Optional device_node pointer.
61345ec382SMichael Walle  * @read_post_process:	Callback for optional post processing of cell data
62345ec382SMichael Walle  *			on reads.
638a134fd9SMichael Walle  * @priv:	Opaque data passed to the read_post_process hook.
64fbd03d27SMichael Walle  */
65fbd03d27SMichael Walle struct nvmem_cell_info {
66fbd03d27SMichael Walle 	const char		*name;
67fbd03d27SMichael Walle 	unsigned int		offset;
6855d4980cSRafał Miłecki 	size_t			raw_len;
69fbd03d27SMichael Walle 	unsigned int		bytes;
70fbd03d27SMichael Walle 	unsigned int		bit_offset;
71fbd03d27SMichael Walle 	unsigned int		nbits;
72fbd03d27SMichael Walle 	struct device_node	*np;
73345ec382SMichael Walle 	nvmem_cell_post_process_t read_post_process;
748a134fd9SMichael Walle 	void			*priv;
75fbd03d27SMichael Walle };
76fbd03d27SMichael Walle 
77fbd03d27SMichael Walle /**
780b2ed745SAndrey Smirnov  * struct nvmem_config - NVMEM device configuration
790b2ed745SAndrey Smirnov  *
800b2ed745SAndrey Smirnov  * @dev:	Parent device.
810b2ed745SAndrey Smirnov  * @name:	Optional name.
820b2ed745SAndrey Smirnov  * @id:		Optional device ID used in full name. Ignored if name is NULL.
830b2ed745SAndrey Smirnov  * @owner:	Pointer to exporter module. Used for refcounting.
840b2ed745SAndrey Smirnov  * @cells:	Optional array of pre-defined NVMEM cells.
850b2ed745SAndrey Smirnov  * @ncells:	Number of elements in cells.
862cc3b37fSRafał Miłecki  * @add_legacy_fixed_of_cells:	Read fixed NVMEM cells from old OF syntax.
871172460eSMiquel Raynal  * @fixup_dt_cell_info: Will be called before a cell is added. Can be
881172460eSMiquel Raynal  *		used to modify the nvmem_cell_info.
89fd3bb8f5SEvan Green  * @keepout:	Optional array of keepout ranges (sorted ascending by start).
90fd3bb8f5SEvan Green  * @nkeepout:	Number of elements in the keepout array.
9116688453SAlexandre Belloni  * @type:	Type of the nvmem storage
920b2ed745SAndrey Smirnov  * @read_only:	Device is read-only.
930b2ed745SAndrey Smirnov  * @root_only:	Device is accessibly to root only.
941333a677SMichael Walle  * @of_node:	If given, this will be used instead of the parent's of_node.
95*6fdbc7b9SThéo Lebrun  * @reg_read:	Callback to read data; return zero if successful.
96*6fdbc7b9SThéo Lebrun  * @reg_write:	Callback to write data; return zero if successful.
970b2ed745SAndrey Smirnov  * @size:	Device size.
980b2ed745SAndrey Smirnov  * @word_size:	Minimum read/write access granularity.
990b2ed745SAndrey Smirnov  * @stride:	Minimum read/write access stride.
1000b2ed745SAndrey Smirnov  * @priv:	User context passed to read/write callbacks.
101f6c052afSChristophe Kerello  * @ignore_wp:  Write Protect pin is managed by the provider.
102266570f4SMichael Walle  * @layout:	Fixed layout associated with this nvmem device.
1030b2ed745SAndrey Smirnov  *
1040b2ed745SAndrey Smirnov  * Note: A default "nvmem<id>" name will be assigned to the device if
1050b2ed745SAndrey Smirnov  * no name is specified in its configuration. In such case "<id>" is
1060b2ed745SAndrey Smirnov  * generated with ida_simple_get() and provided id field is ignored.
107fd0f4906SAndrey Smirnov  *
108fd0f4906SAndrey Smirnov  * Note: Specifying name and setting id to -1 implies a unique device
109fd0f4906SAndrey Smirnov  * whose name is provided as-is (kept unaltered).
1100b2ed745SAndrey Smirnov  */
111eace75cfSSrinivas Kandagatla struct nvmem_config {
112eace75cfSSrinivas Kandagatla 	struct device		*dev;
113eace75cfSSrinivas Kandagatla 	const char		*name;
114eace75cfSSrinivas Kandagatla 	int			id;
115eace75cfSSrinivas Kandagatla 	struct module		*owner;
116eace75cfSSrinivas Kandagatla 	const struct nvmem_cell_info	*cells;
117eace75cfSSrinivas Kandagatla 	int			ncells;
1182cc3b37fSRafał Miłecki 	bool			add_legacy_fixed_of_cells;
1191172460eSMiquel Raynal 	void (*fixup_dt_cell_info)(struct nvmem_device *nvmem,
1201172460eSMiquel Raynal 				   struct nvmem_cell_info *cell);
121fd3bb8f5SEvan Green 	const struct nvmem_keepout *keepout;
122fd3bb8f5SEvan Green 	unsigned int		nkeepout;
12316688453SAlexandre Belloni 	enum nvmem_type		type;
124eace75cfSSrinivas Kandagatla 	bool			read_only;
125811b0d65SAndrew Lunn 	bool			root_only;
126f6c052afSChristophe Kerello 	bool			ignore_wp;
127266570f4SMichael Walle 	struct nvmem_layout	*layout;
1281333a677SMichael Walle 	struct device_node	*of_node;
129795ddd18SSrinivas Kandagatla 	nvmem_reg_read_t	reg_read;
130795ddd18SSrinivas Kandagatla 	nvmem_reg_write_t	reg_write;
131795ddd18SSrinivas Kandagatla 	int	size;
132795ddd18SSrinivas Kandagatla 	int	word_size;
133795ddd18SSrinivas Kandagatla 	int	stride;
134795ddd18SSrinivas Kandagatla 	void	*priv;
135b6c217abSAndrew Lunn 	/* To be only used by old driver/misc/eeprom drivers */
136b6c217abSAndrew Lunn 	bool			compat;
137b6c217abSAndrew Lunn 	struct device		*base_dev;
138eace75cfSSrinivas Kandagatla };
139eace75cfSSrinivas Kandagatla 
140b985f4cbSBartosz Golaszewski /**
141b985f4cbSBartosz Golaszewski  * struct nvmem_cell_table - NVMEM cell definitions for given provider
142b985f4cbSBartosz Golaszewski  *
143b985f4cbSBartosz Golaszewski  * @nvmem_name:		Provider name.
144b985f4cbSBartosz Golaszewski  * @cells:		Array of cell definitions.
145b985f4cbSBartosz Golaszewski  * @ncells:		Number of cell definitions in the array.
146b985f4cbSBartosz Golaszewski  * @node:		List node.
147b985f4cbSBartosz Golaszewski  *
148b985f4cbSBartosz Golaszewski  * This structure together with related helper functions is provided for users
149b985f4cbSBartosz Golaszewski  * that don't can't access the nvmem provided structure but wish to register
150b985f4cbSBartosz Golaszewski  * cell definitions for it e.g. board files registering an EEPROM device.
151b985f4cbSBartosz Golaszewski  */
152b985f4cbSBartosz Golaszewski struct nvmem_cell_table {
153b985f4cbSBartosz Golaszewski 	const char		*nvmem_name;
154b985f4cbSBartosz Golaszewski 	const struct nvmem_cell_info	*cells;
155b985f4cbSBartosz Golaszewski 	size_t			ncells;
156b985f4cbSBartosz Golaszewski 	struct list_head	node;
157b985f4cbSBartosz Golaszewski };
158b985f4cbSBartosz Golaszewski 
159266570f4SMichael Walle /**
160266570f4SMichael Walle  * struct nvmem_layout - NVMEM layout definitions
161266570f4SMichael Walle  *
162fc29fd82SMiquel Raynal  * @dev:		Device-model layout device.
163fc29fd82SMiquel Raynal  * @nvmem:		The underlying NVMEM device
164fc29fd82SMiquel Raynal  * @add_cells:		Will be called if a nvmem device is found which
165fc29fd82SMiquel Raynal  *			has this layout. The function will add layout
166fc29fd82SMiquel Raynal  *			specific cells with nvmem_add_one_cell().
167266570f4SMichael Walle  *
168266570f4SMichael Walle  * A nvmem device can hold a well defined structure which can just be
169266570f4SMichael Walle  * evaluated during runtime. For example a TLV list, or a list of "name=val"
170266570f4SMichael Walle  * pairs. A nvmem layout can parse the nvmem device and add appropriate
171266570f4SMichael Walle  * cells.
172266570f4SMichael Walle  */
173266570f4SMichael Walle struct nvmem_layout {
174fc29fd82SMiquel Raynal 	struct device dev;
175fc29fd82SMiquel Raynal 	struct nvmem_device *nvmem;
176401df0d4SRafał Miłecki 	int (*add_cells)(struct nvmem_layout *layout);
177fc29fd82SMiquel Raynal };
178266570f4SMichael Walle 
179fc29fd82SMiquel Raynal struct nvmem_layout_driver {
180fc29fd82SMiquel Raynal 	struct device_driver driver;
181fc29fd82SMiquel Raynal 	int (*probe)(struct nvmem_layout *layout);
182fc29fd82SMiquel Raynal 	void (*remove)(struct nvmem_layout *layout);
183266570f4SMichael Walle };
184266570f4SMichael Walle 
185eace75cfSSrinivas Kandagatla #if IS_ENABLED(CONFIG_NVMEM)
186eace75cfSSrinivas Kandagatla 
187eace75cfSSrinivas Kandagatla struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
188bf58e882SBartosz Golaszewski void nvmem_unregister(struct nvmem_device *nvmem);
189eace75cfSSrinivas Kandagatla 
190f1f50ecaSAndrey Smirnov struct nvmem_device *devm_nvmem_register(struct device *dev,
191f1f50ecaSAndrey Smirnov 					 const struct nvmem_config *cfg);
192f1f50ecaSAndrey Smirnov 
193b985f4cbSBartosz Golaszewski void nvmem_add_cell_table(struct nvmem_cell_table *table);
194b985f4cbSBartosz Golaszewski void nvmem_del_cell_table(struct nvmem_cell_table *table);
195b985f4cbSBartosz Golaszewski 
1962ded6830SMichael Walle int nvmem_add_one_cell(struct nvmem_device *nvmem,
1972ded6830SMichael Walle 		       const struct nvmem_cell_info *info);
1982ded6830SMichael Walle 
199fc29fd82SMiquel Raynal int nvmem_layout_register(struct nvmem_layout *layout);
200266570f4SMichael Walle void nvmem_layout_unregister(struct nvmem_layout *layout);
201266570f4SMichael Walle 
2026d0ca4a2SKrzysztof Kozlowski #define nvmem_layout_driver_register(drv) \
2036d0ca4a2SKrzysztof Kozlowski 	__nvmem_layout_driver_register(drv, THIS_MODULE)
2046d0ca4a2SKrzysztof Kozlowski int __nvmem_layout_driver_register(struct nvmem_layout_driver *drv,
2056d0ca4a2SKrzysztof Kozlowski 				   struct module *owner);
206fc29fd82SMiquel Raynal void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv);
207fc29fd82SMiquel Raynal #define module_nvmem_layout_driver(__nvmem_layout_driver)		\
208fc29fd82SMiquel Raynal 	module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \
209fc29fd82SMiquel Raynal 		      nvmem_layout_driver_unregister)
210fc29fd82SMiquel Raynal 
211eace75cfSSrinivas Kandagatla #else
212eace75cfSSrinivas Kandagatla 
nvmem_register(const struct nvmem_config * c)213eace75cfSSrinivas Kandagatla static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
214eace75cfSSrinivas Kandagatla {
21520167b70SBartosz Golaszewski 	return ERR_PTR(-EOPNOTSUPP);
216eace75cfSSrinivas Kandagatla }
217eace75cfSSrinivas Kandagatla 
nvmem_unregister(struct nvmem_device * nvmem)218bf58e882SBartosz Golaszewski static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
219eace75cfSSrinivas Kandagatla 
220f1f50ecaSAndrey Smirnov static inline struct nvmem_device *
devm_nvmem_register(struct device * dev,const struct nvmem_config * c)221f1f50ecaSAndrey Smirnov devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
222f1f50ecaSAndrey Smirnov {
223f1f50ecaSAndrey Smirnov 	return nvmem_register(c);
224f1f50ecaSAndrey Smirnov }
225f1f50ecaSAndrey Smirnov 
nvmem_add_cell_table(struct nvmem_cell_table * table)226b985f4cbSBartosz Golaszewski static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
nvmem_del_cell_table(struct nvmem_cell_table * table)227b985f4cbSBartosz Golaszewski 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)2282ded6830SMichael Walle static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
2292ded6830SMichael Walle 				     const struct nvmem_cell_info *info)
2302ded6830SMichael Walle {
2312ded6830SMichael Walle 	return -EOPNOTSUPP;
2322ded6830SMichael Walle }
233f1f50ecaSAndrey Smirnov 
nvmem_layout_register(struct nvmem_layout * layout)234266570f4SMichael Walle static inline int nvmem_layout_register(struct nvmem_layout *layout)
235266570f4SMichael Walle {
236266570f4SMichael Walle 	return -EOPNOTSUPP;
237266570f4SMichael Walle }
238266570f4SMichael Walle 
nvmem_layout_unregister(struct nvmem_layout * layout)239266570f4SMichael Walle static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
240266570f4SMichael Walle 
241eace75cfSSrinivas Kandagatla #endif /* CONFIG_NVMEM */
242814c978fSMiquel Raynal 
2434a1a4023SMiquel Raynal #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
2444a1a4023SMiquel Raynal 
2454a1a4023SMiquel Raynal /**
2464a1a4023SMiquel Raynal  * of_nvmem_layout_get_container() - Get OF node of layout container
2474a1a4023SMiquel Raynal  *
2484a1a4023SMiquel Raynal  * @nvmem: nvmem device
2494a1a4023SMiquel Raynal  *
2504a1a4023SMiquel Raynal  * Return: a node pointer with refcount incremented or NULL if no
2514a1a4023SMiquel Raynal  * container exists. Use of_node_put() on it when done.
2524a1a4023SMiquel Raynal  */
2534a1a4023SMiquel Raynal struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem);
2544a1a4023SMiquel Raynal 
2554a1a4023SMiquel Raynal #else  /* CONFIG_NVMEM && CONFIG_OF */
2564a1a4023SMiquel Raynal 
of_nvmem_layout_get_container(struct nvmem_device * nvmem)2574a1a4023SMiquel Raynal static inline struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
2584a1a4023SMiquel Raynal {
2594a1a4023SMiquel Raynal 	return NULL;
2604a1a4023SMiquel Raynal }
2614a1a4023SMiquel Raynal 
2624a1a4023SMiquel Raynal #endif /* CONFIG_NVMEM && CONFIG_OF */
2634a1a4023SMiquel Raynal 
264eace75cfSSrinivas Kandagatla #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
265