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