1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
5 #include <linux/bits.h>
7 #include <linux/compiler_types.h>
13 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
14 * preferable to the old integer-based handles.
16 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
17 * until the GPIO is released.
22 * Opaque descriptor for a structure of GPIO array attributes. This structure
23 * is attached to struct gpiod_descs obtained from gpiod_get_array() and can be
24 * passed back to get/set array functions in order to activate fast processing
30 * Struct containing an array of descriptors that can be obtained using
34 struct gpio_array *info;
36 struct gpio_desc *desc[];
39 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
40 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
41 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
42 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
43 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
46 * Optional flags that can be passed to one of gpiod_* to configure direction
47 * and output value. These values cannot be OR'd.
51 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
52 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
53 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
54 GPIOD_FLAGS_BIT_DIR_VAL,
55 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
56 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
61 /* Return the number of GPIOs associated with a device / function */
62 int gpiod_count(struct device *dev, const char *con_id);
64 /* Acquire and dispose GPIOs */
65 struct gpio_desc *__must_check gpiod_get(struct device *dev,
67 enum gpiod_flags flags);
68 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
71 enum gpiod_flags flags);
72 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
74 enum gpiod_flags flags);
75 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
78 enum gpiod_flags flags);
79 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
81 enum gpiod_flags flags);
82 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
84 enum gpiod_flags flags);
85 void gpiod_put(struct gpio_desc *desc);
86 void gpiod_put_array(struct gpio_descs *descs);
88 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
90 enum gpiod_flags flags);
91 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
94 enum gpiod_flags flags);
95 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
97 enum gpiod_flags flags);
98 struct gpio_desc *__must_check
99 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
100 unsigned int index, enum gpiod_flags flags);
101 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
103 enum gpiod_flags flags);
104 struct gpio_descs *__must_check
105 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
106 enum gpiod_flags flags);
107 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
108 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
109 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
111 int gpiod_get_direction(struct gpio_desc *desc);
112 int gpiod_direction_input(struct gpio_desc *desc);
113 int gpiod_direction_output(struct gpio_desc *desc, int value);
114 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
116 /* Value get/set from non-sleeping context */
117 int gpiod_get_value(const struct gpio_desc *desc);
118 int gpiod_get_array_value(unsigned int array_size,
119 struct gpio_desc **desc_array,
120 struct gpio_array *array_info,
121 unsigned long *value_bitmap);
122 void gpiod_set_value(struct gpio_desc *desc, int value);
123 int gpiod_set_array_value(unsigned int array_size,
124 struct gpio_desc **desc_array,
125 struct gpio_array *array_info,
126 unsigned long *value_bitmap);
127 int gpiod_get_raw_value(const struct gpio_desc *desc);
128 int gpiod_get_raw_array_value(unsigned int array_size,
129 struct gpio_desc **desc_array,
130 struct gpio_array *array_info,
131 unsigned long *value_bitmap);
132 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
133 int gpiod_set_raw_array_value(unsigned int array_size,
134 struct gpio_desc **desc_array,
135 struct gpio_array *array_info,
136 unsigned long *value_bitmap);
138 /* Value get/set from sleeping context */
139 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
140 int gpiod_get_array_value_cansleep(unsigned int array_size,
141 struct gpio_desc **desc_array,
142 struct gpio_array *array_info,
143 unsigned long *value_bitmap);
144 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
145 int gpiod_set_array_value_cansleep(unsigned int array_size,
146 struct gpio_desc **desc_array,
147 struct gpio_array *array_info,
148 unsigned long *value_bitmap);
149 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
150 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
151 struct gpio_desc **desc_array,
152 struct gpio_array *array_info,
153 unsigned long *value_bitmap);
154 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
155 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
156 struct gpio_desc **desc_array,
157 struct gpio_array *array_info,
158 unsigned long *value_bitmap);
160 int gpiod_set_config(struct gpio_desc *desc, unsigned long config);
161 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce);
162 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
163 void gpiod_toggle_active_low(struct gpio_desc *desc);
165 int gpiod_is_active_low(const struct gpio_desc *desc);
166 int gpiod_cansleep(const struct gpio_desc *desc);
168 int gpiod_to_irq(const struct gpio_desc *desc);
169 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
171 /* Convert between the old gpio_ and new gpiod_ interfaces */
172 struct gpio_desc *gpio_to_desc(unsigned gpio);
173 int desc_to_gpio(const struct gpio_desc *desc);
175 /* Child properties interface */
176 struct fwnode_handle;
178 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
179 const char *propname, int index,
180 enum gpiod_flags dflags,
182 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
183 const char *con_id, int index,
184 enum gpiod_flags flags,
186 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
187 struct fwnode_handle *child,
188 const char *con_id, int index,
189 enum gpiod_flags flags,
192 #else /* CONFIG_GPIOLIB */
194 #include <linux/kernel.h>
196 static inline int gpiod_count(struct device *dev, const char *con_id)
201 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
203 enum gpiod_flags flags)
205 return ERR_PTR(-ENOSYS);
207 static inline struct gpio_desc *__must_check
208 gpiod_get_index(struct device *dev,
211 enum gpiod_flags flags)
213 return ERR_PTR(-ENOSYS);
216 static inline struct gpio_desc *__must_check
217 gpiod_get_optional(struct device *dev, const char *con_id,
218 enum gpiod_flags flags)
223 static inline struct gpio_desc *__must_check
224 gpiod_get_index_optional(struct device *dev, const char *con_id,
225 unsigned int index, enum gpiod_flags flags)
230 static inline struct gpio_descs *__must_check
231 gpiod_get_array(struct device *dev, const char *con_id,
232 enum gpiod_flags flags)
234 return ERR_PTR(-ENOSYS);
237 static inline struct gpio_descs *__must_check
238 gpiod_get_array_optional(struct device *dev, const char *con_id,
239 enum gpiod_flags flags)
244 static inline void gpiod_put(struct gpio_desc *desc)
248 /* GPIO can never have been requested */
252 static inline void devm_gpiod_unhinge(struct device *dev,
253 struct gpio_desc *desc)
257 /* GPIO can never have been requested */
261 static inline void gpiod_put_array(struct gpio_descs *descs)
265 /* GPIO can never have been requested */
269 static inline struct gpio_desc *__must_check
270 devm_gpiod_get(struct device *dev,
272 enum gpiod_flags flags)
274 return ERR_PTR(-ENOSYS);
277 struct gpio_desc *__must_check
278 devm_gpiod_get_index(struct device *dev,
281 enum gpiod_flags flags)
283 return ERR_PTR(-ENOSYS);
286 static inline struct gpio_desc *__must_check
287 devm_gpiod_get_optional(struct device *dev, const char *con_id,
288 enum gpiod_flags flags)
293 static inline struct gpio_desc *__must_check
294 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
295 unsigned int index, enum gpiod_flags flags)
300 static inline struct gpio_descs *__must_check
301 devm_gpiod_get_array(struct device *dev, const char *con_id,
302 enum gpiod_flags flags)
304 return ERR_PTR(-ENOSYS);
307 static inline struct gpio_descs *__must_check
308 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
309 enum gpiod_flags flags)
314 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
318 /* GPIO can never have been requested */
322 static inline void devm_gpiod_put_array(struct device *dev,
323 struct gpio_descs *descs)
327 /* GPIO can never have been requested */
332 static inline int gpiod_get_direction(const struct gpio_desc *desc)
334 /* GPIO can never have been requested */
338 static inline int gpiod_direction_input(struct gpio_desc *desc)
340 /* GPIO can never have been requested */
344 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
346 /* GPIO can never have been requested */
350 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
352 /* GPIO can never have been requested */
358 static inline int gpiod_get_value(const struct gpio_desc *desc)
360 /* GPIO can never have been requested */
364 static inline int gpiod_get_array_value(unsigned int array_size,
365 struct gpio_desc **desc_array,
366 struct gpio_array *array_info,
367 unsigned long *value_bitmap)
369 /* GPIO can never have been requested */
373 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
375 /* GPIO can never have been requested */
378 static inline int gpiod_set_array_value(unsigned int array_size,
379 struct gpio_desc **desc_array,
380 struct gpio_array *array_info,
381 unsigned long *value_bitmap)
383 /* GPIO can never have been requested */
387 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
389 /* GPIO can never have been requested */
393 static inline int gpiod_get_raw_array_value(unsigned int array_size,
394 struct gpio_desc **desc_array,
395 struct gpio_array *array_info,
396 unsigned long *value_bitmap)
398 /* GPIO can never have been requested */
402 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
404 /* GPIO can never have been requested */
407 static inline int gpiod_set_raw_array_value(unsigned int array_size,
408 struct gpio_desc **desc_array,
409 struct gpio_array *array_info,
410 unsigned long *value_bitmap)
412 /* GPIO can never have been requested */
417 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
419 /* GPIO can never have been requested */
423 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
424 struct gpio_desc **desc_array,
425 struct gpio_array *array_info,
426 unsigned long *value_bitmap)
428 /* GPIO can never have been requested */
432 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
434 /* GPIO can never have been requested */
437 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
438 struct gpio_desc **desc_array,
439 struct gpio_array *array_info,
440 unsigned long *value_bitmap)
442 /* GPIO can never have been requested */
446 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
448 /* GPIO can never have been requested */
452 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
453 struct gpio_desc **desc_array,
454 struct gpio_array *array_info,
455 unsigned long *value_bitmap)
457 /* GPIO can never have been requested */
461 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
464 /* GPIO can never have been requested */
467 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
468 struct gpio_desc **desc_array,
469 struct gpio_array *array_info,
470 unsigned long *value_bitmap)
472 /* GPIO can never have been requested */
477 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
479 /* GPIO can never have been requested */
484 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
486 /* GPIO can never have been requested */
491 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
493 /* GPIO can never have been requested */
498 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
500 /* GPIO can never have been requested */
504 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
506 /* GPIO can never have been requested */
510 static inline int gpiod_cansleep(const struct gpio_desc *desc)
512 /* GPIO can never have been requested */
517 static inline int gpiod_to_irq(const struct gpio_desc *desc)
519 /* GPIO can never have been requested */
524 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
527 /* GPIO can never have been requested */
532 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
537 static inline int desc_to_gpio(const struct gpio_desc *desc)
539 /* GPIO can never have been requested */
544 /* Child properties interface */
545 struct fwnode_handle;
548 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
549 const char *propname, int index,
550 enum gpiod_flags dflags,
553 return ERR_PTR(-ENOSYS);
557 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
558 const char *con_id, int index,
559 enum gpiod_flags flags,
562 return ERR_PTR(-ENOSYS);
566 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
567 struct fwnode_handle *fwnode,
568 const char *con_id, int index,
569 enum gpiod_flags flags,
572 return ERR_PTR(-ENOSYS);
575 #endif /* CONFIG_GPIOLIB */
578 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
579 struct fwnode_handle *fwnode,
581 enum gpiod_flags flags,
584 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
589 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
590 const char *con_id, int index,
591 struct fwnode_handle *child,
592 enum gpiod_flags flags,
595 return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
600 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
602 struct fwnode_handle *child,
603 enum gpiod_flags flags,
606 return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
609 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
612 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
613 const char *propname, int index,
614 enum gpiod_flags dflags,
617 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
622 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
623 const char *propname, int index,
624 enum gpiod_flags dflags,
627 return ERR_PTR(-ENOSYS);
630 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
632 #ifdef CONFIG_GPIOLIB
635 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
636 const struct device_node *node,
637 const char *propname, int index,
638 enum gpiod_flags dflags,
641 #else /* CONFIG_GPIOLIB */
646 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
647 const struct device_node *node,
648 const char *propname, int index,
649 enum gpiod_flags dflags,
652 return ERR_PTR(-ENOSYS);
655 #endif /* CONFIG_GPIOLIB */
657 struct acpi_gpio_params {
658 unsigned int crs_entry_index;
659 unsigned int line_index;
663 struct acpi_gpio_mapping {
665 const struct acpi_gpio_params *data;
668 /* Ignore IoRestriction field */
669 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
671 * When ACPI GPIO mapping table is in use the index parameter inside it
672 * refers to the GPIO resource in _CRS method. That index has no
673 * distinction of actual type of the resource. When consumer wants to
674 * get GpioIo type explicitly, this quirk may be used.
676 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
677 /* Use given pin as an absolute GPIO number in the system */
678 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
685 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
687 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
688 const struct acpi_gpio_mapping *gpios);
689 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
691 int devm_acpi_dev_add_driver_gpios(struct device *dev,
692 const struct acpi_gpio_mapping *gpios);
693 void devm_acpi_dev_remove_driver_gpios(struct device *dev);
695 struct gpio_desc *acpi_get_and_request_gpiod(char *path, int pin, char *label);
697 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
699 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
700 const struct acpi_gpio_mapping *gpios)
704 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
706 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
707 const struct acpi_gpio_mapping *gpios)
711 static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
713 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
716 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
718 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
719 int gpiod_export_link(struct device *dev, const char *name,
720 struct gpio_desc *desc);
721 void gpiod_unexport(struct gpio_desc *desc);
723 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
725 static inline int gpiod_export(struct gpio_desc *desc,
726 bool direction_may_change)
731 static inline int gpiod_export_link(struct device *dev, const char *name,
732 struct gpio_desc *desc)
737 static inline void gpiod_unexport(struct gpio_desc *desc)
741 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */