1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * nvmem framework consumer.
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_CONSUMER_H
10 #define _LINUX_NVMEM_CONSUMER_H
11 
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/notifier.h>
15 
16 struct device;
17 struct device_node;
18 /* consumer cookie */
19 struct nvmem_cell;
20 struct nvmem_device;
21 struct nvmem_cell_info;
22 
23 /**
24  * struct nvmem_cell_lookup - cell lookup entry
25  *
26  * @nvmem_name:	Name of the provider.
27  * @cell_name:	Name of the nvmem cell as defined in the name field of
28  *		struct nvmem_cell_info.
29  * @dev_id:	Name of the consumer device that will be associated with
30  *		this cell.
31  * @con_id:	Connector id for this cell lookup.
32  */
33 struct nvmem_cell_lookup {
34 	const char		*nvmem_name;
35 	const char		*cell_name;
36 	const char		*dev_id;
37 	const char		*con_id;
38 	struct list_head	node;
39 };
40 
41 enum {
42 	NVMEM_ADD = 1,
43 	NVMEM_REMOVE,
44 	NVMEM_CELL_ADD,
45 	NVMEM_CELL_REMOVE,
46 	NVMEM_LAYOUT_ADD,
47 	NVMEM_LAYOUT_REMOVE,
48 };
49 
50 #if IS_ENABLED(CONFIG_NVMEM)
51 
52 /* Cell based interface */
53 struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id);
54 struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id);
55 void nvmem_cell_put(struct nvmem_cell *cell);
56 void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
57 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
58 int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
59 int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val);
60 int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val);
61 int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val);
62 int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val);
63 int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
64 				    u32 *val);
65 int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
66 				    u64 *val);
67 
68 /* direct nvmem device read/write interface */
69 struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
70 struct nvmem_device *devm_nvmem_device_get(struct device *dev,
71 					   const char *name);
72 void nvmem_device_put(struct nvmem_device *nvmem);
73 void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);
74 int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
75 		      size_t bytes, void *buf);
76 int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
77 		       size_t bytes, void *buf);
78 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
79 			   struct nvmem_cell_info *info, void *buf);
80 int nvmem_device_cell_write(struct nvmem_device *nvmem,
81 			    struct nvmem_cell_info *info, void *buf);
82 
83 const char *nvmem_dev_name(struct nvmem_device *nvmem);
84 
85 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries,
86 			    size_t nentries);
87 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries,
88 			    size_t nentries);
89 
90 int nvmem_register_notifier(struct notifier_block *nb);
91 int nvmem_unregister_notifier(struct notifier_block *nb);
92 
93 struct nvmem_device *nvmem_device_find(void *data,
94 			int (*match)(struct device *dev, const void *data));
95 
96 #else
97 
nvmem_cell_get(struct device * dev,const char * id)98 static inline struct nvmem_cell *nvmem_cell_get(struct device *dev,
99 						const char *id)
100 {
101 	return ERR_PTR(-EOPNOTSUPP);
102 }
103 
devm_nvmem_cell_get(struct device * dev,const char * id)104 static inline struct nvmem_cell *devm_nvmem_cell_get(struct device *dev,
105 						     const char *id)
106 {
107 	return ERR_PTR(-EOPNOTSUPP);
108 }
109 
devm_nvmem_cell_put(struct device * dev,struct nvmem_cell * cell)110 static inline void devm_nvmem_cell_put(struct device *dev,
111 				       struct nvmem_cell *cell)
112 {
113 
114 }
nvmem_cell_put(struct nvmem_cell * cell)115 static inline void nvmem_cell_put(struct nvmem_cell *cell)
116 {
117 }
118 
nvmem_cell_read(struct nvmem_cell * cell,size_t * len)119 static inline void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
120 {
121 	return ERR_PTR(-EOPNOTSUPP);
122 }
123 
nvmem_cell_write(struct nvmem_cell * cell,void * buf,size_t len)124 static inline int nvmem_cell_write(struct nvmem_cell *cell,
125 				   void *buf, size_t len)
126 {
127 	return -EOPNOTSUPP;
128 }
129 
nvmem_cell_read_u16(struct device * dev,const char * cell_id,u16 * val)130 static inline int nvmem_cell_read_u16(struct device *dev,
131 				      const char *cell_id, u16 *val)
132 {
133 	return -EOPNOTSUPP;
134 }
135 
nvmem_cell_read_u32(struct device * dev,const char * cell_id,u32 * val)136 static inline int nvmem_cell_read_u32(struct device *dev,
137 				      const char *cell_id, u32 *val)
138 {
139 	return -EOPNOTSUPP;
140 }
141 
nvmem_cell_read_u64(struct device * dev,const char * cell_id,u64 * val)142 static inline int nvmem_cell_read_u64(struct device *dev,
143 				      const char *cell_id, u64 *val)
144 {
145 	return -EOPNOTSUPP;
146 }
147 
nvmem_cell_read_variable_le_u32(struct device * dev,const char * cell_id,u32 * val)148 static inline int nvmem_cell_read_variable_le_u32(struct device *dev,
149 						 const char *cell_id,
150 						 u32 *val)
151 {
152 	return -EOPNOTSUPP;
153 }
154 
nvmem_cell_read_variable_le_u64(struct device * dev,const char * cell_id,u64 * val)155 static inline int nvmem_cell_read_variable_le_u64(struct device *dev,
156 						  const char *cell_id,
157 						  u64 *val)
158 {
159 	return -EOPNOTSUPP;
160 }
161 
nvmem_device_get(struct device * dev,const char * name)162 static inline struct nvmem_device *nvmem_device_get(struct device *dev,
163 						    const char *name)
164 {
165 	return ERR_PTR(-EOPNOTSUPP);
166 }
167 
devm_nvmem_device_get(struct device * dev,const char * name)168 static inline struct nvmem_device *devm_nvmem_device_get(struct device *dev,
169 							 const char *name)
170 {
171 	return ERR_PTR(-EOPNOTSUPP);
172 }
173 
nvmem_device_put(struct nvmem_device * nvmem)174 static inline void nvmem_device_put(struct nvmem_device *nvmem)
175 {
176 }
177 
devm_nvmem_device_put(struct device * dev,struct nvmem_device * nvmem)178 static inline void devm_nvmem_device_put(struct device *dev,
179 					 struct nvmem_device *nvmem)
180 {
181 }
182 
nvmem_device_cell_read(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)183 static inline ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
184 					 struct nvmem_cell_info *info,
185 					 void *buf)
186 {
187 	return -EOPNOTSUPP;
188 }
189 
nvmem_device_cell_write(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)190 static inline int nvmem_device_cell_write(struct nvmem_device *nvmem,
191 					  struct nvmem_cell_info *info,
192 					  void *buf)
193 {
194 	return -EOPNOTSUPP;
195 }
196 
nvmem_device_read(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)197 static inline int nvmem_device_read(struct nvmem_device *nvmem,
198 				    unsigned int offset, size_t bytes,
199 				    void *buf)
200 {
201 	return -EOPNOTSUPP;
202 }
203 
nvmem_device_write(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)204 static inline int nvmem_device_write(struct nvmem_device *nvmem,
205 				     unsigned int offset, size_t bytes,
206 				     void *buf)
207 {
208 	return -EOPNOTSUPP;
209 }
210 
nvmem_dev_name(struct nvmem_device * nvmem)211 static inline const char *nvmem_dev_name(struct nvmem_device *nvmem)
212 {
213 	return NULL;
214 }
215 
216 static inline void
nvmem_add_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)217 nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {}
218 static inline void
nvmem_del_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)219 nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {}
220 
nvmem_register_notifier(struct notifier_block * nb)221 static inline int nvmem_register_notifier(struct notifier_block *nb)
222 {
223 	return -EOPNOTSUPP;
224 }
225 
nvmem_unregister_notifier(struct notifier_block * nb)226 static inline int nvmem_unregister_notifier(struct notifier_block *nb)
227 {
228 	return -EOPNOTSUPP;
229 }
230 
nvmem_device_find(void * data,int (* match)(struct device * dev,const void * data))231 static inline struct nvmem_device *nvmem_device_find(void *data,
232 			int (*match)(struct device *dev, const void *data))
233 {
234 	return NULL;
235 }
236 
237 #endif /* CONFIG_NVMEM */
238 
239 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
240 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
241 				     const char *id);
242 struct nvmem_device *of_nvmem_device_get(struct device_node *np,
243 					 const char *name);
244 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem);
245 #else
of_nvmem_cell_get(struct device_node * np,const char * id)246 static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
247 						   const char *id)
248 {
249 	return ERR_PTR(-EOPNOTSUPP);
250 }
251 
of_nvmem_device_get(struct device_node * np,const char * name)252 static inline struct nvmem_device *of_nvmem_device_get(struct device_node *np,
253 						       const char *name)
254 {
255 	return ERR_PTR(-EOPNOTSUPP);
256 }
257 
258 static inline struct device_node *
of_nvmem_layout_get_container(struct nvmem_device * nvmem)259 of_nvmem_layout_get_container(struct nvmem_device *nvmem)
260 {
261 	return NULL;
262 }
263 #endif /* CONFIG_NVMEM && CONFIG_OF */
264 
265 #endif  /* ifndef _LINUX_NVMEM_CONSUMER_H */
266