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 size_t nvmem_dev_size(struct nvmem_device *nvmem);
85 
86 void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries,
87 			    size_t nentries);
88 void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries,
89 			    size_t nentries);
90 
91 int nvmem_register_notifier(struct notifier_block *nb);
92 int nvmem_unregister_notifier(struct notifier_block *nb);
93 
94 struct nvmem_device *nvmem_device_find(void *data,
95 			int (*match)(struct device *dev, const void *data));
96 
97 #else
98 
nvmem_cell_get(struct device * dev,const char * id)99 static inline struct nvmem_cell *nvmem_cell_get(struct device *dev,
100 						const char *id)
101 {
102 	return ERR_PTR(-EOPNOTSUPP);
103 }
104 
devm_nvmem_cell_get(struct device * dev,const char * id)105 static inline struct nvmem_cell *devm_nvmem_cell_get(struct device *dev,
106 						     const char *id)
107 {
108 	return ERR_PTR(-EOPNOTSUPP);
109 }
110 
devm_nvmem_cell_put(struct device * dev,struct nvmem_cell * cell)111 static inline void devm_nvmem_cell_put(struct device *dev,
112 				       struct nvmem_cell *cell)
113 {
114 
115 }
nvmem_cell_put(struct nvmem_cell * cell)116 static inline void nvmem_cell_put(struct nvmem_cell *cell)
117 {
118 }
119 
nvmem_cell_read(struct nvmem_cell * cell,size_t * len)120 static inline void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
121 {
122 	return ERR_PTR(-EOPNOTSUPP);
123 }
124 
nvmem_cell_write(struct nvmem_cell * cell,void * buf,size_t len)125 static inline int nvmem_cell_write(struct nvmem_cell *cell,
126 				   void *buf, size_t len)
127 {
128 	return -EOPNOTSUPP;
129 }
130 
nvmem_cell_read_u16(struct device * dev,const char * cell_id,u16 * val)131 static inline int nvmem_cell_read_u16(struct device *dev,
132 				      const char *cell_id, u16 *val)
133 {
134 	return -EOPNOTSUPP;
135 }
136 
nvmem_cell_read_u32(struct device * dev,const char * cell_id,u32 * val)137 static inline int nvmem_cell_read_u32(struct device *dev,
138 				      const char *cell_id, u32 *val)
139 {
140 	return -EOPNOTSUPP;
141 }
142 
nvmem_cell_read_u64(struct device * dev,const char * cell_id,u64 * val)143 static inline int nvmem_cell_read_u64(struct device *dev,
144 				      const char *cell_id, u64 *val)
145 {
146 	return -EOPNOTSUPP;
147 }
148 
nvmem_cell_read_variable_le_u32(struct device * dev,const char * cell_id,u32 * val)149 static inline int nvmem_cell_read_variable_le_u32(struct device *dev,
150 						 const char *cell_id,
151 						 u32 *val)
152 {
153 	return -EOPNOTSUPP;
154 }
155 
nvmem_cell_read_variable_le_u64(struct device * dev,const char * cell_id,u64 * val)156 static inline int nvmem_cell_read_variable_le_u64(struct device *dev,
157 						  const char *cell_id,
158 						  u64 *val)
159 {
160 	return -EOPNOTSUPP;
161 }
162 
nvmem_device_get(struct device * dev,const char * name)163 static inline struct nvmem_device *nvmem_device_get(struct device *dev,
164 						    const char *name)
165 {
166 	return ERR_PTR(-EOPNOTSUPP);
167 }
168 
devm_nvmem_device_get(struct device * dev,const char * name)169 static inline struct nvmem_device *devm_nvmem_device_get(struct device *dev,
170 							 const char *name)
171 {
172 	return ERR_PTR(-EOPNOTSUPP);
173 }
174 
nvmem_device_put(struct nvmem_device * nvmem)175 static inline void nvmem_device_put(struct nvmem_device *nvmem)
176 {
177 }
178 
devm_nvmem_device_put(struct device * dev,struct nvmem_device * nvmem)179 static inline void devm_nvmem_device_put(struct device *dev,
180 					 struct nvmem_device *nvmem)
181 {
182 }
183 
nvmem_device_cell_read(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)184 static inline ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
185 					 struct nvmem_cell_info *info,
186 					 void *buf)
187 {
188 	return -EOPNOTSUPP;
189 }
190 
nvmem_device_cell_write(struct nvmem_device * nvmem,struct nvmem_cell_info * info,void * buf)191 static inline int nvmem_device_cell_write(struct nvmem_device *nvmem,
192 					  struct nvmem_cell_info *info,
193 					  void *buf)
194 {
195 	return -EOPNOTSUPP;
196 }
197 
nvmem_device_read(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)198 static inline int nvmem_device_read(struct nvmem_device *nvmem,
199 				    unsigned int offset, size_t bytes,
200 				    void *buf)
201 {
202 	return -EOPNOTSUPP;
203 }
204 
nvmem_device_write(struct nvmem_device * nvmem,unsigned int offset,size_t bytes,void * buf)205 static inline int nvmem_device_write(struct nvmem_device *nvmem,
206 				     unsigned int offset, size_t bytes,
207 				     void *buf)
208 {
209 	return -EOPNOTSUPP;
210 }
211 
nvmem_dev_name(struct nvmem_device * nvmem)212 static inline const char *nvmem_dev_name(struct nvmem_device *nvmem)
213 {
214 	return NULL;
215 }
216 
217 static inline void
nvmem_add_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)218 nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {}
219 static inline void
nvmem_del_cell_lookups(struct nvmem_cell_lookup * entries,size_t nentries)220 nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries) {}
221 
nvmem_register_notifier(struct notifier_block * nb)222 static inline int nvmem_register_notifier(struct notifier_block *nb)
223 {
224 	return -EOPNOTSUPP;
225 }
226 
nvmem_unregister_notifier(struct notifier_block * nb)227 static inline int nvmem_unregister_notifier(struct notifier_block *nb)
228 {
229 	return -EOPNOTSUPP;
230 }
231 
nvmem_device_find(void * data,int (* match)(struct device * dev,const void * data))232 static inline struct nvmem_device *nvmem_device_find(void *data,
233 			int (*match)(struct device *dev, const void *data))
234 {
235 	return NULL;
236 }
237 
238 #endif /* CONFIG_NVMEM */
239 
240 #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
241 struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
242 				     const char *id);
243 struct nvmem_device *of_nvmem_device_get(struct device_node *np,
244 					 const char *name);
245 struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem);
246 #else
of_nvmem_cell_get(struct device_node * np,const char * id)247 static inline struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
248 						   const char *id)
249 {
250 	return ERR_PTR(-EOPNOTSUPP);
251 }
252 
of_nvmem_device_get(struct device_node * np,const char * name)253 static inline struct nvmem_device *of_nvmem_device_get(struct device_node *np,
254 						       const char *name)
255 {
256 	return ERR_PTR(-EOPNOTSUPP);
257 }
258 
259 static inline struct device_node *
of_nvmem_layout_get_container(struct nvmem_device * nvmem)260 of_nvmem_layout_get_container(struct nvmem_device *nvmem)
261 {
262 	return NULL;
263 }
264 #endif /* CONFIG_NVMEM && CONFIG_OF */
265 
266 #endif  /* ifndef _LINUX_NVMEM_CONSUMER_H */
267