xref: /linux-6.15/include/linux/gpio/consumer.h (revision ef264cf0)
1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
3 
4 #include <linux/bug.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 
8 struct device;
9 
10 /**
11  * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
12  * preferable to the old integer-based handles.
13  *
14  * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
15  * until the GPIO is released.
16  */
17 struct gpio_desc;
18 
19 #ifdef CONFIG_GPIOLIB
20 
21 /* Acquire and dispose GPIOs */
22 struct gpio_desc *__must_check gpiod_get(struct device *dev,
23 					 const char *con_id);
24 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
25 					       const char *con_id,
26 					       unsigned int idx);
27 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
28 						  const char *con_id);
29 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
30 							const char *con_id,
31 							unsigned int index);
32 
33 void gpiod_put(struct gpio_desc *desc);
34 
35 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
36 					      const char *con_id);
37 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
38 						    const char *con_id,
39 						    unsigned int idx);
40 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
41 						       const char *con_id);
42 struct gpio_desc *__must_check
43 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
44 			      unsigned int index);
45 
46 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
47 
48 int gpiod_get_direction(const struct gpio_desc *desc);
49 int gpiod_direction_input(struct gpio_desc *desc);
50 int gpiod_direction_output(struct gpio_desc *desc, int value);
51 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
52 
53 /* Value get/set from non-sleeping context */
54 int gpiod_get_value(const struct gpio_desc *desc);
55 void gpiod_set_value(struct gpio_desc *desc, int value);
56 int gpiod_get_raw_value(const struct gpio_desc *desc);
57 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
58 
59 /* Value get/set from sleeping context */
60 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
61 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
62 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
63 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
64 
65 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
66 
67 int gpiod_is_active_low(const struct gpio_desc *desc);
68 int gpiod_cansleep(const struct gpio_desc *desc);
69 
70 int gpiod_to_irq(const struct gpio_desc *desc);
71 
72 /* Convert between the old gpio_ and new gpiod_ interfaces */
73 struct gpio_desc *gpio_to_desc(unsigned gpio);
74 int desc_to_gpio(const struct gpio_desc *desc);
75 
76 #else /* CONFIG_GPIOLIB */
77 
78 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
79 						       const char *con_id)
80 {
81 	return ERR_PTR(-ENOSYS);
82 }
83 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
84 							     const char *con_id,
85 							     unsigned int idx)
86 {
87 	return ERR_PTR(-ENOSYS);
88 }
89 
90 static inline struct gpio_desc *__must_check
91 gpiod_get_optional(struct device *dev, const char *con_id)
92 {
93 	return ERR_PTR(-ENOSYS);
94 }
95 
96 static inline struct gpio_desc *__must_check
97 gpiod_get_index_optional(struct device *dev, const char *con_id,
98 			 unsigned int index)
99 {
100 	return ERR_PTR(-ENOSYS);
101 }
102 
103 static inline void gpiod_put(struct gpio_desc *desc)
104 {
105 	might_sleep();
106 
107 	/* GPIO can never have been requested */
108 	WARN_ON(1);
109 }
110 
111 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
112 							    const char *con_id)
113 {
114 	return ERR_PTR(-ENOSYS);
115 }
116 static inline
117 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
118 						    const char *con_id,
119 						    unsigned int idx)
120 {
121 	return ERR_PTR(-ENOSYS);
122 }
123 
124 static inline struct gpio_desc *__must_check
125 devm_gpiod_get_optional(struct device *dev, const char *con_id)
126 {
127 	return ERR_PTR(-ENOSYS);
128 }
129 
130 static inline struct gpio_desc *__must_check
131 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
132 			      unsigned int index)
133 {
134 	return ERR_PTR(-ENOSYS);
135 }
136 
137 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
138 {
139 	might_sleep();
140 
141 	/* GPIO can never have been requested */
142 	WARN_ON(1);
143 }
144 
145 
146 static inline int gpiod_get_direction(const struct gpio_desc *desc)
147 {
148 	/* GPIO can never have been requested */
149 	WARN_ON(1);
150 	return -ENOSYS;
151 }
152 static inline int gpiod_direction_input(struct gpio_desc *desc)
153 {
154 	/* GPIO can never have been requested */
155 	WARN_ON(1);
156 	return -ENOSYS;
157 }
158 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
159 {
160 	/* GPIO can never have been requested */
161 	WARN_ON(1);
162 	return -ENOSYS;
163 }
164 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
165 {
166 	/* GPIO can never have been requested */
167 	WARN_ON(1);
168 	return -ENOSYS;
169 }
170 
171 
172 static inline int gpiod_get_value(const struct gpio_desc *desc)
173 {
174 	/* GPIO can never have been requested */
175 	WARN_ON(1);
176 	return 0;
177 }
178 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
179 {
180 	/* GPIO can never have been requested */
181 	WARN_ON(1);
182 }
183 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
184 {
185 	/* GPIO can never have been requested */
186 	WARN_ON(1);
187 	return 0;
188 }
189 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
190 {
191 	/* GPIO can never have been requested */
192 	WARN_ON(1);
193 }
194 
195 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
196 {
197 	/* GPIO can never have been requested */
198 	WARN_ON(1);
199 	return 0;
200 }
201 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
202 {
203 	/* GPIO can never have been requested */
204 	WARN_ON(1);
205 }
206 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
207 {
208 	/* GPIO can never have been requested */
209 	WARN_ON(1);
210 	return 0;
211 }
212 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
213 						int value)
214 {
215 	/* GPIO can never have been requested */
216 	WARN_ON(1);
217 }
218 
219 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
220 {
221 	/* GPIO can never have been requested */
222 	WARN_ON(1);
223 	return -ENOSYS;
224 }
225 
226 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
227 {
228 	/* GPIO can never have been requested */
229 	WARN_ON(1);
230 	return 0;
231 }
232 static inline int gpiod_cansleep(const struct gpio_desc *desc)
233 {
234 	/* GPIO can never have been requested */
235 	WARN_ON(1);
236 	return 0;
237 }
238 
239 static inline int gpiod_to_irq(const struct gpio_desc *desc)
240 {
241 	/* GPIO can never have been requested */
242 	WARN_ON(1);
243 	return -EINVAL;
244 }
245 
246 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
247 {
248 	return ERR_PTR(-EINVAL);
249 }
250 static inline int desc_to_gpio(const struct gpio_desc *desc)
251 {
252 	/* GPIO can never have been requested */
253 	WARN_ON(1);
254 	return -EINVAL;
255 }
256 
257 
258 #endif /* CONFIG_GPIOLIB */
259 
260 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
261 
262 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
263 int gpiod_export_link(struct device *dev, const char *name,
264 		      struct gpio_desc *desc);
265 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
266 void gpiod_unexport(struct gpio_desc *desc);
267 
268 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
269 
270 static inline int gpiod_export(struct gpio_desc *desc,
271 			       bool direction_may_change)
272 {
273 	return -ENOSYS;
274 }
275 
276 static inline int gpiod_export_link(struct device *dev, const char *name,
277 				    struct gpio_desc *desc)
278 {
279 	return -ENOSYS;
280 }
281 
282 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
283 {
284 	return -ENOSYS;
285 }
286 
287 static inline void gpiod_unexport(struct gpio_desc *desc)
288 {
289 }
290 
291 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
292 
293 #endif
294