xref: /linux/include/linux/nvmem-provider.h (revision 0be3ff0c)
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/err.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15 
16 struct nvmem_device;
17 struct nvmem_cell_info;
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, unsigned int offset,
24 					  void *buf, size_t bytes);
25 
26 enum nvmem_type {
27 	NVMEM_TYPE_UNKNOWN = 0,
28 	NVMEM_TYPE_EEPROM,
29 	NVMEM_TYPE_OTP,
30 	NVMEM_TYPE_BATTERY_BACKED,
31 	NVMEM_TYPE_FRAM,
32 };
33 
34 #define NVMEM_DEVID_NONE	(-1)
35 #define NVMEM_DEVID_AUTO	(-2)
36 
37 /**
38  * struct nvmem_keepout - NVMEM register keepout range.
39  *
40  * @start:	The first byte offset to avoid.
41  * @end:	One beyond the last byte offset to avoid.
42  * @value:	The byte to fill reads with for this region.
43  */
44 struct nvmem_keepout {
45 	unsigned int start;
46 	unsigned int end;
47 	unsigned char value;
48 };
49 
50 /**
51  * struct nvmem_config - NVMEM device configuration
52  *
53  * @dev:	Parent device.
54  * @name:	Optional name.
55  * @id:		Optional device ID used in full name. Ignored if name is NULL.
56  * @owner:	Pointer to exporter module. Used for refcounting.
57  * @cells:	Optional array of pre-defined NVMEM cells.
58  * @ncells:	Number of elements in cells.
59  * @keepout:	Optional array of keepout ranges (sorted ascending by start).
60  * @nkeepout:	Number of elements in the keepout array.
61  * @type:	Type of the nvmem storage
62  * @read_only:	Device is read-only.
63  * @root_only:	Device is accessibly to root only.
64  * @of_node:	If given, this will be used instead of the parent's of_node.
65  * @no_of_node:	Device should not use the parent's of_node even if it's !NULL.
66  * @reg_read:	Callback to read data.
67  * @reg_write:	Callback to write data.
68  * @cell_post_process:	Callback for vendor specific post processing of cell data
69  * @size:	Device size.
70  * @word_size:	Minimum read/write access granularity.
71  * @stride:	Minimum read/write access stride.
72  * @priv:	User context passed to read/write callbacks.
73  * @wp-gpio:	Write protect pin
74  * @ignore_wp:  Write Protect pin is managed by the provider.
75  *
76  * Note: A default "nvmem<id>" name will be assigned to the device if
77  * no name is specified in its configuration. In such case "<id>" is
78  * generated with ida_simple_get() and provided id field is ignored.
79  *
80  * Note: Specifying name and setting id to -1 implies a unique device
81  * whose name is provided as-is (kept unaltered).
82  */
83 struct nvmem_config {
84 	struct device		*dev;
85 	const char		*name;
86 	int			id;
87 	struct module		*owner;
88 	struct gpio_desc	*wp_gpio;
89 	const struct nvmem_cell_info	*cells;
90 	int			ncells;
91 	const struct nvmem_keepout *keepout;
92 	unsigned int		nkeepout;
93 	enum nvmem_type		type;
94 	bool			read_only;
95 	bool			root_only;
96 	bool			ignore_wp;
97 	struct device_node	*of_node;
98 	bool			no_of_node;
99 	nvmem_reg_read_t	reg_read;
100 	nvmem_reg_write_t	reg_write;
101 	nvmem_cell_post_process_t cell_post_process;
102 	int	size;
103 	int	word_size;
104 	int	stride;
105 	void	*priv;
106 	/* To be only used by old driver/misc/eeprom drivers */
107 	bool			compat;
108 	struct device		*base_dev;
109 };
110 
111 /**
112  * struct nvmem_cell_table - NVMEM cell definitions for given provider
113  *
114  * @nvmem_name:		Provider name.
115  * @cells:		Array of cell definitions.
116  * @ncells:		Number of cell definitions in the array.
117  * @node:		List node.
118  *
119  * This structure together with related helper functions is provided for users
120  * that don't can't access the nvmem provided structure but wish to register
121  * cell definitions for it e.g. board files registering an EEPROM device.
122  */
123 struct nvmem_cell_table {
124 	const char		*nvmem_name;
125 	const struct nvmem_cell_info	*cells;
126 	size_t			ncells;
127 	struct list_head	node;
128 };
129 
130 #if IS_ENABLED(CONFIG_NVMEM)
131 
132 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
133 void nvmem_unregister(struct nvmem_device *nvmem);
134 
135 struct nvmem_device *devm_nvmem_register(struct device *dev,
136 					 const struct nvmem_config *cfg);
137 
138 void nvmem_add_cell_table(struct nvmem_cell_table *table);
139 void nvmem_del_cell_table(struct nvmem_cell_table *table);
140 
141 #else
142 
143 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
144 {
145 	return ERR_PTR(-EOPNOTSUPP);
146 }
147 
148 static inline void nvmem_unregister(struct nvmem_device *nvmem) {}
149 
150 static inline struct nvmem_device *
151 devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
152 {
153 	return nvmem_register(c);
154 }
155 
156 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
157 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
158 
159 #endif /* CONFIG_NVMEM */
160 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
161