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