xref: /linux-6.15/include/linux/gpio/consumer.h (revision b9762beb)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
4 
5 #include <linux/bug.h>
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 
9 struct device;
10 
11 /**
12  * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
13  * preferable to the old integer-based handles.
14  *
15  * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
16  * until the GPIO is released.
17  */
18 struct gpio_desc;
19 
20 /**
21  * Struct containing an array of descriptors that can be obtained using
22  * gpiod_get_array().
23  */
24 struct gpio_descs {
25 	unsigned int ndescs;
26 	struct gpio_desc *desc[];
27 };
28 
29 #define GPIOD_FLAGS_BIT_DIR_SET		BIT(0)
30 #define GPIOD_FLAGS_BIT_DIR_OUT		BIT(1)
31 #define GPIOD_FLAGS_BIT_DIR_VAL		BIT(2)
32 #define GPIOD_FLAGS_BIT_OPEN_DRAIN	BIT(3)
33 
34 /**
35  * Optional flags that can be passed to one of gpiod_* to configure direction
36  * and output value. These values cannot be OR'd.
37  */
38 enum gpiod_flags {
39 	GPIOD_ASIS	= 0,
40 	GPIOD_IN	= GPIOD_FLAGS_BIT_DIR_SET,
41 	GPIOD_OUT_LOW	= GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
42 	GPIOD_OUT_HIGH	= GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
43 			  GPIOD_FLAGS_BIT_DIR_VAL,
44 	GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
45 	GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
46 };
47 
48 #ifdef CONFIG_GPIOLIB
49 
50 /* Return the number of GPIOs associated with a device / function */
51 int gpiod_count(struct device *dev, const char *con_id);
52 
53 /* Acquire and dispose GPIOs */
54 struct gpio_desc *__must_check gpiod_get(struct device *dev,
55 					 const char *con_id,
56 					 enum gpiod_flags flags);
57 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
58 					       const char *con_id,
59 					       unsigned int idx,
60 					       enum gpiod_flags flags);
61 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
62 						  const char *con_id,
63 						  enum gpiod_flags flags);
64 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
65 							const char *con_id,
66 							unsigned int index,
67 							enum gpiod_flags flags);
68 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
69 						const char *con_id,
70 						enum gpiod_flags flags);
71 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
72 							const char *con_id,
73 							enum gpiod_flags flags);
74 void gpiod_put(struct gpio_desc *desc);
75 void gpiod_put_array(struct gpio_descs *descs);
76 
77 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
78 					      const char *con_id,
79 					      enum gpiod_flags flags);
80 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
81 						    const char *con_id,
82 						    unsigned int idx,
83 						    enum gpiod_flags flags);
84 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
85 						       const char *con_id,
86 						       enum gpiod_flags flags);
87 struct gpio_desc *__must_check
88 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
89 			      unsigned int index, enum gpiod_flags flags);
90 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
91 						     const char *con_id,
92 						     enum gpiod_flags flags);
93 struct gpio_descs *__must_check
94 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
95 			      enum gpiod_flags flags);
96 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
97 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
98 
99 int gpiod_get_direction(struct gpio_desc *desc);
100 int gpiod_direction_input(struct gpio_desc *desc);
101 int gpiod_direction_output(struct gpio_desc *desc, int value);
102 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
103 
104 /* Value get/set from non-sleeping context */
105 int gpiod_get_value(const struct gpio_desc *desc);
106 int gpiod_get_array_value(unsigned int array_size,
107 			  struct gpio_desc **desc_array,
108 			  unsigned long *value_bitmap);
109 void gpiod_set_value(struct gpio_desc *desc, int value);
110 void gpiod_set_array_value(unsigned int array_size,
111 			   struct gpio_desc **desc_array,
112 			   unsigned long *value_bitmap);
113 int gpiod_get_raw_value(const struct gpio_desc *desc);
114 int gpiod_get_raw_array_value(unsigned int array_size,
115 			      struct gpio_desc **desc_array,
116 			      unsigned long *value_bitmap);
117 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
118 int gpiod_set_raw_array_value(unsigned int array_size,
119 			       struct gpio_desc **desc_array,
120 			       unsigned long *value_bitmap);
121 
122 /* Value get/set from sleeping context */
123 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
124 int gpiod_get_array_value_cansleep(unsigned int array_size,
125 				   struct gpio_desc **desc_array,
126 				   unsigned long *value_bitmap);
127 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
128 void gpiod_set_array_value_cansleep(unsigned int array_size,
129 				    struct gpio_desc **desc_array,
130 				    unsigned long *value_bitmap);
131 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
132 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
133 				       struct gpio_desc **desc_array,
134 				       unsigned long *value_bitmap);
135 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
136 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
137 					struct gpio_desc **desc_array,
138 					unsigned long *value_bitmap);
139 
140 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
141 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
142 
143 int gpiod_is_active_low(const struct gpio_desc *desc);
144 int gpiod_cansleep(const struct gpio_desc *desc);
145 
146 int gpiod_to_irq(const struct gpio_desc *desc);
147 void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
148 
149 /* Convert between the old gpio_ and new gpiod_ interfaces */
150 struct gpio_desc *gpio_to_desc(unsigned gpio);
151 int desc_to_gpio(const struct gpio_desc *desc);
152 
153 /* Child properties interface */
154 struct device_node;
155 struct fwnode_handle;
156 
157 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
158 					      struct device_node *node,
159 					      const char *propname, int index,
160 					      enum gpiod_flags dflags,
161 					      const char *label);
162 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
163 					 const char *propname, int index,
164 					 enum gpiod_flags dflags,
165 					 const char *label);
166 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
167 						const char *con_id, int index,
168 						struct fwnode_handle *child,
169 						enum gpiod_flags flags,
170 						const char *label);
171 
172 #else /* CONFIG_GPIOLIB */
173 
174 static inline int gpiod_count(struct device *dev, const char *con_id)
175 {
176 	return 0;
177 }
178 
179 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
180 						       const char *con_id,
181 						       enum gpiod_flags flags)
182 {
183 	return ERR_PTR(-ENOSYS);
184 }
185 static inline struct gpio_desc *__must_check
186 gpiod_get_index(struct device *dev,
187 		const char *con_id,
188 		unsigned int idx,
189 		enum gpiod_flags flags)
190 {
191 	return ERR_PTR(-ENOSYS);
192 }
193 
194 static inline struct gpio_desc *__must_check
195 gpiod_get_optional(struct device *dev, const char *con_id,
196 		   enum gpiod_flags flags)
197 {
198 	return NULL;
199 }
200 
201 static inline struct gpio_desc *__must_check
202 gpiod_get_index_optional(struct device *dev, const char *con_id,
203 			 unsigned int index, enum gpiod_flags flags)
204 {
205 	return NULL;
206 }
207 
208 static inline struct gpio_descs *__must_check
209 gpiod_get_array(struct device *dev, const char *con_id,
210 		enum gpiod_flags flags)
211 {
212 	return ERR_PTR(-ENOSYS);
213 }
214 
215 static inline struct gpio_descs *__must_check
216 gpiod_get_array_optional(struct device *dev, const char *con_id,
217 			 enum gpiod_flags flags)
218 {
219 	return NULL;
220 }
221 
222 static inline void gpiod_put(struct gpio_desc *desc)
223 {
224 	might_sleep();
225 
226 	/* GPIO can never have been requested */
227 	WARN_ON(1);
228 }
229 
230 static inline void gpiod_put_array(struct gpio_descs *descs)
231 {
232 	might_sleep();
233 
234 	/* GPIO can never have been requested */
235 	WARN_ON(1);
236 }
237 
238 static inline struct gpio_desc *__must_check
239 devm_gpiod_get(struct device *dev,
240 		 const char *con_id,
241 		 enum gpiod_flags flags)
242 {
243 	return ERR_PTR(-ENOSYS);
244 }
245 static inline
246 struct gpio_desc *__must_check
247 devm_gpiod_get_index(struct device *dev,
248 		       const char *con_id,
249 		       unsigned int idx,
250 		       enum gpiod_flags flags)
251 {
252 	return ERR_PTR(-ENOSYS);
253 }
254 
255 static inline struct gpio_desc *__must_check
256 devm_gpiod_get_optional(struct device *dev, const char *con_id,
257 			  enum gpiod_flags flags)
258 {
259 	return NULL;
260 }
261 
262 static inline struct gpio_desc *__must_check
263 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
264 				unsigned int index, enum gpiod_flags flags)
265 {
266 	return NULL;
267 }
268 
269 static inline struct gpio_descs *__must_check
270 devm_gpiod_get_array(struct device *dev, const char *con_id,
271 		     enum gpiod_flags flags)
272 {
273 	return ERR_PTR(-ENOSYS);
274 }
275 
276 static inline struct gpio_descs *__must_check
277 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
278 			      enum gpiod_flags flags)
279 {
280 	return NULL;
281 }
282 
283 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
284 {
285 	might_sleep();
286 
287 	/* GPIO can never have been requested */
288 	WARN_ON(1);
289 }
290 
291 static inline void devm_gpiod_put_array(struct device *dev,
292 					struct gpio_descs *descs)
293 {
294 	might_sleep();
295 
296 	/* GPIO can never have been requested */
297 	WARN_ON(1);
298 }
299 
300 
301 static inline int gpiod_get_direction(const struct gpio_desc *desc)
302 {
303 	/* GPIO can never have been requested */
304 	WARN_ON(1);
305 	return -ENOSYS;
306 }
307 static inline int gpiod_direction_input(struct gpio_desc *desc)
308 {
309 	/* GPIO can never have been requested */
310 	WARN_ON(1);
311 	return -ENOSYS;
312 }
313 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
314 {
315 	/* GPIO can never have been requested */
316 	WARN_ON(1);
317 	return -ENOSYS;
318 }
319 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
320 {
321 	/* GPIO can never have been requested */
322 	WARN_ON(1);
323 	return -ENOSYS;
324 }
325 
326 
327 static inline int gpiod_get_value(const struct gpio_desc *desc)
328 {
329 	/* GPIO can never have been requested */
330 	WARN_ON(1);
331 	return 0;
332 }
333 static inline int gpiod_get_array_value(unsigned int array_size,
334 					struct gpio_desc **desc_array,
335 					unsigned long *value_bitmap)
336 {
337 	/* GPIO can never have been requested */
338 	WARN_ON(1);
339 	return 0;
340 }
341 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
342 {
343 	/* GPIO can never have been requested */
344 	WARN_ON(1);
345 }
346 static inline void gpiod_set_array_value(unsigned int array_size,
347 					 struct gpio_desc **desc_array,
348 					 unsigned long *value_bitmap)
349 {
350 	/* GPIO can never have been requested */
351 	WARN_ON(1);
352 }
353 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
354 {
355 	/* GPIO can never have been requested */
356 	WARN_ON(1);
357 	return 0;
358 }
359 static inline int gpiod_get_raw_array_value(unsigned int array_size,
360 					    struct gpio_desc **desc_array,
361 					    unsigned long *value_bitmap)
362 {
363 	/* GPIO can never have been requested */
364 	WARN_ON(1);
365 	return 0;
366 }
367 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
368 {
369 	/* GPIO can never have been requested */
370 	WARN_ON(1);
371 }
372 static inline int gpiod_set_raw_array_value(unsigned int array_size,
373 					     struct gpio_desc **desc_array,
374 					     unsigned long *value_bitmap)
375 {
376 	/* GPIO can never have been requested */
377 	WARN_ON(1);
378 	return 0;
379 }
380 
381 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
382 {
383 	/* GPIO can never have been requested */
384 	WARN_ON(1);
385 	return 0;
386 }
387 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
388 				     struct gpio_desc **desc_array,
389 				     unsigned long *value_bitmap)
390 {
391 	/* GPIO can never have been requested */
392 	WARN_ON(1);
393 	return 0;
394 }
395 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
396 {
397 	/* GPIO can never have been requested */
398 	WARN_ON(1);
399 }
400 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
401 					    struct gpio_desc **desc_array,
402 					    unsigned long *value_bitmap)
403 {
404 	/* GPIO can never have been requested */
405 	WARN_ON(1);
406 }
407 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
408 {
409 	/* GPIO can never have been requested */
410 	WARN_ON(1);
411 	return 0;
412 }
413 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
414 					       struct gpio_desc **desc_array,
415 					       unsigned long *value_bitmap)
416 {
417 	/* GPIO can never have been requested */
418 	WARN_ON(1);
419 	return 0;
420 }
421 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
422 						int value)
423 {
424 	/* GPIO can never have been requested */
425 	WARN_ON(1);
426 }
427 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
428 						struct gpio_desc **desc_array,
429 						unsigned long *value_bitmap)
430 {
431 	/* GPIO can never have been requested */
432 	WARN_ON(1);
433 	return 0;
434 }
435 
436 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
437 {
438 	/* GPIO can never have been requested */
439 	WARN_ON(1);
440 	return -ENOSYS;
441 }
442 
443 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
444 {
445 	/* GPIO can never have been requested */
446 	WARN_ON(1);
447 	return -ENOSYS;
448 }
449 
450 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
451 {
452 	/* GPIO can never have been requested */
453 	WARN_ON(1);
454 	return 0;
455 }
456 static inline int gpiod_cansleep(const struct gpio_desc *desc)
457 {
458 	/* GPIO can never have been requested */
459 	WARN_ON(1);
460 	return 0;
461 }
462 
463 static inline int gpiod_to_irq(const struct gpio_desc *desc)
464 {
465 	/* GPIO can never have been requested */
466 	WARN_ON(1);
467 	return -EINVAL;
468 }
469 
470 static inline void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
471 {
472 	/* GPIO can never have been requested */
473 	WARN_ON(1);
474 }
475 
476 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
477 {
478 	return ERR_PTR(-EINVAL);
479 }
480 
481 static inline int desc_to_gpio(const struct gpio_desc *desc)
482 {
483 	/* GPIO can never have been requested */
484 	WARN_ON(1);
485 	return -EINVAL;
486 }
487 
488 /* Child properties interface */
489 struct device_node;
490 struct fwnode_handle;
491 
492 static inline
493 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
494 					      struct device_node *node,
495 					      const char *propname, int index,
496 					      enum gpiod_flags dflags,
497 					      const char *label)
498 {
499 	return ERR_PTR(-ENOSYS);
500 }
501 
502 static inline
503 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
504 					 const char *propname, int index,
505 					 enum gpiod_flags dflags,
506 					 const char *label)
507 {
508 	return ERR_PTR(-ENOSYS);
509 }
510 
511 static inline
512 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
513 						const char *con_id, int index,
514 						struct fwnode_handle *child,
515 						enum gpiod_flags flags,
516 						const char *label)
517 {
518 	return ERR_PTR(-ENOSYS);
519 }
520 
521 #endif /* CONFIG_GPIOLIB */
522 
523 static inline
524 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
525 						   const char *con_id,
526 						   struct fwnode_handle *child,
527 						   enum gpiod_flags flags,
528 						   const char *label)
529 {
530 	return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
531 						      flags, label);
532 }
533 
534 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
535 
536 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
537 int gpiod_export_link(struct device *dev, const char *name,
538 		      struct gpio_desc *desc);
539 void gpiod_unexport(struct gpio_desc *desc);
540 
541 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
542 
543 static inline int gpiod_export(struct gpio_desc *desc,
544 			       bool direction_may_change)
545 {
546 	return -ENOSYS;
547 }
548 
549 static inline int gpiod_export_link(struct device *dev, const char *name,
550 				    struct gpio_desc *desc)
551 {
552 	return -ENOSYS;
553 }
554 
555 static inline void gpiod_unexport(struct gpio_desc *desc)
556 {
557 }
558 
559 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
560 
561 #endif
562