1
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
9struct device;
10
11
12
13
14
15
16
17
18struct gpio_desc;
19
20
21
22
23
24
25
26struct gpio_array;
27
28
29
30
31
32struct gpio_descs {
33 struct gpio_array *info;
34 unsigned int ndescs;
35 struct gpio_desc *desc[];
36};
37
38#define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
39#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
40#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
41#define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
42#define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
43
44
45
46
47
48enum gpiod_flags {
49 GPIOD_ASIS = 0,
50 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
51 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
52 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
53 GPIOD_FLAGS_BIT_DIR_VAL,
54 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
55 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
56};
57
58#ifdef CONFIG_GPIOLIB
59
60
61int gpiod_count(struct device *dev, const char *con_id);
62
63
64struct gpio_desc *__must_check gpiod_get(struct device *dev,
65 const char *con_id,
66 enum gpiod_flags flags);
67struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
68 const char *con_id,
69 unsigned int idx,
70 enum gpiod_flags flags);
71struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
72 const char *con_id,
73 enum gpiod_flags flags);
74struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
75 const char *con_id,
76 unsigned int index,
77 enum gpiod_flags flags);
78struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
79 const char *con_id,
80 enum gpiod_flags flags);
81struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
82 const char *con_id,
83 enum gpiod_flags flags);
84void gpiod_put(struct gpio_desc *desc);
85void gpiod_put_array(struct gpio_descs *descs);
86
87struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
88 const char *con_id,
89 enum gpiod_flags flags);
90struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
91 const char *con_id,
92 unsigned int idx,
93 enum gpiod_flags flags);
94struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
95 const char *con_id,
96 enum gpiod_flags flags);
97struct gpio_desc *__must_check
98devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
99 unsigned int index, enum gpiod_flags flags);
100struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
101 const char *con_id,
102 enum gpiod_flags flags);
103struct gpio_descs *__must_check
104devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
105 enum gpiod_flags flags);
106void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
107void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
108void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
109
110int gpiod_get_direction(struct gpio_desc *desc);
111int gpiod_direction_input(struct gpio_desc *desc);
112int gpiod_direction_output(struct gpio_desc *desc, int value);
113int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
114
115
116int gpiod_get_value(const struct gpio_desc *desc);
117int gpiod_get_array_value(unsigned int array_size,
118 struct gpio_desc **desc_array,
119 struct gpio_array *array_info,
120 unsigned long *value_bitmap);
121void gpiod_set_value(struct gpio_desc *desc, int value);
122int gpiod_set_array_value(unsigned int array_size,
123 struct gpio_desc **desc_array,
124 struct gpio_array *array_info,
125 unsigned long *value_bitmap);
126int gpiod_get_raw_value(const struct gpio_desc *desc);
127int gpiod_get_raw_array_value(unsigned int array_size,
128 struct gpio_desc **desc_array,
129 struct gpio_array *array_info,
130 unsigned long *value_bitmap);
131void gpiod_set_raw_value(struct gpio_desc *desc, int value);
132int gpiod_set_raw_array_value(unsigned int array_size,
133 struct gpio_desc **desc_array,
134 struct gpio_array *array_info,
135 unsigned long *value_bitmap);
136
137
138int gpiod_get_value_cansleep(const struct gpio_desc *desc);
139int gpiod_get_array_value_cansleep(unsigned int array_size,
140 struct gpio_desc **desc_array,
141 struct gpio_array *array_info,
142 unsigned long *value_bitmap);
143void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
144int gpiod_set_array_value_cansleep(unsigned int array_size,
145 struct gpio_desc **desc_array,
146 struct gpio_array *array_info,
147 unsigned long *value_bitmap);
148int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
149int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
150 struct gpio_desc **desc_array,
151 struct gpio_array *array_info,
152 unsigned long *value_bitmap);
153void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
154int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
155 struct gpio_desc **desc_array,
156 struct gpio_array *array_info,
157 unsigned long *value_bitmap);
158
159int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
160int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
161void gpiod_toggle_active_low(struct gpio_desc *desc);
162
163int gpiod_is_active_low(const struct gpio_desc *desc);
164int gpiod_cansleep(const struct gpio_desc *desc);
165
166int gpiod_to_irq(const struct gpio_desc *desc);
167int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
168
169
170struct gpio_desc *gpio_to_desc(unsigned gpio);
171int desc_to_gpio(const struct gpio_desc *desc);
172
173
174struct fwnode_handle;
175
176struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
177 const char *propname, int index,
178 enum gpiod_flags dflags,
179 const char *label);
180struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
181 const char *con_id, int index,
182 enum gpiod_flags flags,
183 const char *label);
184struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
185 struct fwnode_handle *child,
186 const char *con_id, int index,
187 enum gpiod_flags flags,
188 const char *label);
189
190#else
191
192static inline int gpiod_count(struct device *dev, const char *con_id)
193{
194 return 0;
195}
196
197static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
198 const char *con_id,
199 enum gpiod_flags flags)
200{
201 return ERR_PTR(-ENOSYS);
202}
203static inline struct gpio_desc *__must_check
204gpiod_get_index(struct device *dev,
205 const char *con_id,
206 unsigned int idx,
207 enum gpiod_flags flags)
208{
209 return ERR_PTR(-ENOSYS);
210}
211
212static inline struct gpio_desc *__must_check
213gpiod_get_optional(struct device *dev, const char *con_id,
214 enum gpiod_flags flags)
215{
216 return NULL;
217}
218
219static inline struct gpio_desc *__must_check
220gpiod_get_index_optional(struct device *dev, const char *con_id,
221 unsigned int index, enum gpiod_flags flags)
222{
223 return NULL;
224}
225
226static inline struct gpio_descs *__must_check
227gpiod_get_array(struct device *dev, const char *con_id,
228 enum gpiod_flags flags)
229{
230 return ERR_PTR(-ENOSYS);
231}
232
233static inline struct gpio_descs *__must_check
234gpiod_get_array_optional(struct device *dev, const char *con_id,
235 enum gpiod_flags flags)
236{
237 return NULL;
238}
239
240static inline void gpiod_put(struct gpio_desc *desc)
241{
242 might_sleep();
243
244
245 WARN_ON(desc);
246}
247
248static inline void devm_gpiod_unhinge(struct device *dev,
249 struct gpio_desc *desc)
250{
251 might_sleep();
252
253
254 WARN_ON(desc);
255}
256
257static inline void gpiod_put_array(struct gpio_descs *descs)
258{
259 might_sleep();
260
261
262 WARN_ON(descs);
263}
264
265static inline struct gpio_desc *__must_check
266devm_gpiod_get(struct device *dev,
267 const char *con_id,
268 enum gpiod_flags flags)
269{
270 return ERR_PTR(-ENOSYS);
271}
272static inline
273struct gpio_desc *__must_check
274devm_gpiod_get_index(struct device *dev,
275 const char *con_id,
276 unsigned int idx,
277 enum gpiod_flags flags)
278{
279 return ERR_PTR(-ENOSYS);
280}
281
282static inline struct gpio_desc *__must_check
283devm_gpiod_get_optional(struct device *dev, const char *con_id,
284 enum gpiod_flags flags)
285{
286 return NULL;
287}
288
289static inline struct gpio_desc *__must_check
290devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
291 unsigned int index, enum gpiod_flags flags)
292{
293 return NULL;
294}
295
296static inline struct gpio_descs *__must_check
297devm_gpiod_get_array(struct device *dev, const char *con_id,
298 enum gpiod_flags flags)
299{
300 return ERR_PTR(-ENOSYS);
301}
302
303static inline struct gpio_descs *__must_check
304devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
305 enum gpiod_flags flags)
306{
307 return NULL;
308}
309
310static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
311{
312 might_sleep();
313
314
315 WARN_ON(desc);
316}
317
318static inline void devm_gpiod_put_array(struct device *dev,
319 struct gpio_descs *descs)
320{
321 might_sleep();
322
323
324 WARN_ON(descs);
325}
326
327
328static inline int gpiod_get_direction(const struct gpio_desc *desc)
329{
330
331 WARN_ON(desc);
332 return -ENOSYS;
333}
334static inline int gpiod_direction_input(struct gpio_desc *desc)
335{
336
337 WARN_ON(desc);
338 return -ENOSYS;
339}
340static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
341{
342
343 WARN_ON(desc);
344 return -ENOSYS;
345}
346static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
347{
348
349 WARN_ON(desc);
350 return -ENOSYS;
351}
352
353
354static inline int gpiod_get_value(const struct gpio_desc *desc)
355{
356
357 WARN_ON(desc);
358 return 0;
359}
360static inline int gpiod_get_array_value(unsigned int array_size,
361 struct gpio_desc **desc_array,
362 struct gpio_array *array_info,
363 unsigned long *value_bitmap)
364{
365
366 WARN_ON(desc_array);
367 return 0;
368}
369static inline void gpiod_set_value(struct gpio_desc *desc, int value)
370{
371
372 WARN_ON(desc);
373}
374static inline int gpiod_set_array_value(unsigned int array_size,
375 struct gpio_desc **desc_array,
376 struct gpio_array *array_info,
377 unsigned long *value_bitmap)
378{
379
380 WARN_ON(desc_array);
381 return 0;
382}
383static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
384{
385
386 WARN_ON(desc);
387 return 0;
388}
389static inline int gpiod_get_raw_array_value(unsigned int array_size,
390 struct gpio_desc **desc_array,
391 struct gpio_array *array_info,
392 unsigned long *value_bitmap)
393{
394
395 WARN_ON(desc_array);
396 return 0;
397}
398static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
399{
400
401 WARN_ON(desc);
402}
403static inline int gpiod_set_raw_array_value(unsigned int array_size,
404 struct gpio_desc **desc_array,
405 struct gpio_array *array_info,
406 unsigned long *value_bitmap)
407{
408
409 WARN_ON(desc_array);
410 return 0;
411}
412
413static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
414{
415
416 WARN_ON(desc);
417 return 0;
418}
419static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
420 struct gpio_desc **desc_array,
421 struct gpio_array *array_info,
422 unsigned long *value_bitmap)
423{
424
425 WARN_ON(desc_array);
426 return 0;
427}
428static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
429{
430
431 WARN_ON(desc);
432}
433static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
434 struct gpio_desc **desc_array,
435 struct gpio_array *array_info,
436 unsigned long *value_bitmap)
437{
438
439 WARN_ON(desc_array);
440 return 0;
441}
442static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
443{
444
445 WARN_ON(desc);
446 return 0;
447}
448static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
449 struct gpio_desc **desc_array,
450 struct gpio_array *array_info,
451 unsigned long *value_bitmap)
452{
453
454 WARN_ON(desc_array);
455 return 0;
456}
457static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
458 int value)
459{
460
461 WARN_ON(desc);
462}
463static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
464 struct gpio_desc **desc_array,
465 struct gpio_array *array_info,
466 unsigned long *value_bitmap)
467{
468
469 WARN_ON(desc_array);
470 return 0;
471}
472
473static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
474{
475
476 WARN_ON(desc);
477 return -ENOSYS;
478}
479
480static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
481{
482
483 WARN_ON(desc);
484 return -ENOSYS;
485}
486
487static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
488{
489
490 WARN_ON(desc);
491}
492
493static inline int gpiod_is_active_low(const struct gpio_desc *desc)
494{
495
496 WARN_ON(desc);
497 return 0;
498}
499static inline int gpiod_cansleep(const struct gpio_desc *desc)
500{
501
502 WARN_ON(desc);
503 return 0;
504}
505
506static inline int gpiod_to_irq(const struct gpio_desc *desc)
507{
508
509 WARN_ON(desc);
510 return -EINVAL;
511}
512
513static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
514 const char *name)
515{
516
517 WARN_ON(desc);
518 return -EINVAL;
519}
520
521static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
522{
523 return NULL;
524}
525
526static inline int desc_to_gpio(const struct gpio_desc *desc)
527{
528
529 WARN_ON(desc);
530 return -EINVAL;
531}
532
533
534struct fwnode_handle;
535
536static inline
537struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
538 const char *propname, int index,
539 enum gpiod_flags dflags,
540 const char *label)
541{
542 return ERR_PTR(-ENOSYS);
543}
544
545static inline
546struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
547 const char *con_id, int index,
548 enum gpiod_flags flags,
549 const char *label)
550{
551 return ERR_PTR(-ENOSYS);
552}
553
554static inline
555struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
556 struct fwnode_handle *fwnode,
557 const char *con_id, int index,
558 enum gpiod_flags flags,
559 const char *label)
560{
561 return ERR_PTR(-ENOSYS);
562}
563
564#endif
565
566static inline
567struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
568 struct fwnode_handle *fwnode,
569 const char *con_id,
570 enum gpiod_flags flags,
571 const char *label)
572{
573 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
574 flags, label);
575}
576
577static inline
578struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
579 const char *con_id, int index,
580 struct fwnode_handle *child,
581 enum gpiod_flags flags,
582 const char *label)
583{
584 return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
585 flags, label);
586}
587
588static inline
589struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
590 const char *con_id,
591 struct fwnode_handle *child,
592 enum gpiod_flags flags,
593 const char *label)
594{
595 return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
596}
597
598#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
599struct device_node;
600
601struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
602 const char *propname, int index,
603 enum gpiod_flags dflags,
604 const char *label);
605
606#else
607
608struct device_node;
609
610static inline
611struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
612 const char *propname, int index,
613 enum gpiod_flags dflags,
614 const char *label)
615{
616 return ERR_PTR(-ENOSYS);
617}
618
619#endif
620
621#ifdef CONFIG_GPIOLIB
622struct device_node;
623
624struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
625 struct device_node *node,
626 const char *propname, int index,
627 enum gpiod_flags dflags,
628 const char *label);
629
630#else
631
632struct device_node;
633
634static inline
635struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
636 struct device_node *node,
637 const char *propname, int index,
638 enum gpiod_flags dflags,
639 const char *label)
640{
641 return ERR_PTR(-ENOSYS);
642}
643
644#endif
645
646struct acpi_gpio_params {
647 unsigned int crs_entry_index;
648 unsigned int line_index;
649 bool active_low;
650};
651
652struct acpi_gpio_mapping {
653 const char *name;
654 const struct acpi_gpio_params *data;
655 unsigned int size;
656
657
658#define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
659
660
661
662
663
664
665#define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
666
667 unsigned int quirks;
668};
669
670#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
671
672struct acpi_device;
673
674int acpi_dev_add_driver_gpios(struct acpi_device *adev,
675 const struct acpi_gpio_mapping *gpios);
676void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
677
678int devm_acpi_dev_add_driver_gpios(struct device *dev,
679 const struct acpi_gpio_mapping *gpios);
680void devm_acpi_dev_remove_driver_gpios(struct device *dev);
681
682#else
683
684struct acpi_device;
685
686static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
687 const struct acpi_gpio_mapping *gpios)
688{
689 return -ENXIO;
690}
691static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
692
693static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
694 const struct acpi_gpio_mapping *gpios)
695{
696 return -ENXIO;
697}
698static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
699
700#endif
701
702
703#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
704
705int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
706int gpiod_export_link(struct device *dev, const char *name,
707 struct gpio_desc *desc);
708void gpiod_unexport(struct gpio_desc *desc);
709
710#else
711
712static inline int gpiod_export(struct gpio_desc *desc,
713 bool direction_may_change)
714{
715 return -ENOSYS;
716}
717
718static inline int gpiod_export_link(struct device *dev, const char *name,
719 struct gpio_desc *desc)
720{
721 return -ENOSYS;
722}
723
724static inline void gpiod_unexport(struct gpio_desc *desc)
725{
726}
727
728#endif
729
730#endif
731