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>
15 * struct gpio_descs - Struct containing an array of descriptors that can be
16 * obtained using gpiod_get_array()
18 * @info: Pointer to the opaque gpio_array structure
19 * @ndescs: Number of held descriptors
20 * @desc: Array of pointers to GPIO descriptors
23 struct gpio_array *info;
25 struct gpio_desc *desc[];
28 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
29 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
30 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
31 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
32 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
35 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to
36 * configure direction and output value. These values
39 * @GPIOD_ASIS: Don't change anything
40 * @GPIOD_IN: Set lines to input mode
41 * @GPIOD_OUT_LOW: Set lines to output and drive them low
42 * @GPIOD_OUT_HIGH: Set lines to output and drive them high
43 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low
44 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high
48 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
49 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
50 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
51 GPIOD_FLAGS_BIT_DIR_VAL,
52 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
53 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
58 /* Return the number of GPIOs associated with a device / function */
59 int gpiod_count(struct device *dev, const char *con_id);
61 /* Acquire and dispose GPIOs */
62 struct gpio_desc *__must_check gpiod_get(struct device *dev,
64 enum gpiod_flags flags);
65 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
68 enum gpiod_flags flags);
69 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
71 enum gpiod_flags flags);
72 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
75 enum gpiod_flags flags);
76 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
78 enum gpiod_flags flags);
79 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
81 enum gpiod_flags flags);
82 void gpiod_put(struct gpio_desc *desc);
83 void gpiod_put_array(struct gpio_descs *descs);
85 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
87 enum gpiod_flags flags);
88 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
91 enum gpiod_flags flags);
92 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
94 enum gpiod_flags flags);
95 struct gpio_desc *__must_check
96 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
97 unsigned int index, enum gpiod_flags flags);
98 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
100 enum gpiod_flags flags);
101 struct gpio_descs *__must_check
102 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
103 enum gpiod_flags flags);
104 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
105 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
106 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
108 int gpiod_get_direction(struct gpio_desc *desc);
109 int gpiod_direction_input(struct gpio_desc *desc);
110 int gpiod_direction_output(struct gpio_desc *desc, int value);
111 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
112 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
113 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
115 /* Value get/set from non-sleeping context */
116 int gpiod_get_value(const struct gpio_desc *desc);
117 int 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);
121 void gpiod_set_value(struct gpio_desc *desc, int value);
122 int 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);
126 int gpiod_get_raw_value(const struct gpio_desc *desc);
127 int 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);
131 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
132 int 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);
137 /* Value get/set from sleeping context */
138 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
139 int 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);
143 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
144 int 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);
148 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
149 int 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);
153 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
154 int 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);
159 int gpiod_set_config(struct gpio_desc *desc, unsigned long config);
160 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce);
161 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162 void gpiod_toggle_active_low(struct gpio_desc *desc);
164 int gpiod_is_active_low(const struct gpio_desc *desc);
165 int gpiod_cansleep(const struct gpio_desc *desc);
167 int gpiod_to_irq(const struct gpio_desc *desc);
168 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
170 /* Convert between the old gpio_ and new gpiod_ interfaces */
171 struct gpio_desc *gpio_to_desc(unsigned gpio);
172 int desc_to_gpio(const struct gpio_desc *desc);
174 /* Child properties interface */
175 struct fwnode_handle;
177 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
178 const char *propname, int index,
179 enum gpiod_flags dflags,
181 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
182 const char *con_id, int index,
183 enum gpiod_flags flags,
185 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
186 struct fwnode_handle *child,
187 const char *con_id, int index,
188 enum gpiod_flags flags,
191 #else /* CONFIG_GPIOLIB */
193 #include <linux/kernel.h>
195 static inline int gpiod_count(struct device *dev, const char *con_id)
200 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
202 enum gpiod_flags flags)
204 return ERR_PTR(-ENOSYS);
206 static inline struct gpio_desc *__must_check
207 gpiod_get_index(struct device *dev,
210 enum gpiod_flags flags)
212 return ERR_PTR(-ENOSYS);
215 static inline struct gpio_desc *__must_check
216 gpiod_get_optional(struct device *dev, const char *con_id,
217 enum gpiod_flags flags)
222 static inline struct gpio_desc *__must_check
223 gpiod_get_index_optional(struct device *dev, const char *con_id,
224 unsigned int index, enum gpiod_flags flags)
229 static inline struct gpio_descs *__must_check
230 gpiod_get_array(struct device *dev, const char *con_id,
231 enum gpiod_flags flags)
233 return ERR_PTR(-ENOSYS);
236 static inline struct gpio_descs *__must_check
237 gpiod_get_array_optional(struct device *dev, const char *con_id,
238 enum gpiod_flags flags)
243 static inline void gpiod_put(struct gpio_desc *desc)
247 /* GPIO can never have been requested */
251 static inline void devm_gpiod_unhinge(struct device *dev,
252 struct gpio_desc *desc)
256 /* GPIO can never have been requested */
260 static inline void gpiod_put_array(struct gpio_descs *descs)
264 /* GPIO can never have been requested */
268 static inline struct gpio_desc *__must_check
269 devm_gpiod_get(struct device *dev,
271 enum gpiod_flags flags)
273 return ERR_PTR(-ENOSYS);
276 struct gpio_desc *__must_check
277 devm_gpiod_get_index(struct device *dev,
280 enum gpiod_flags flags)
282 return ERR_PTR(-ENOSYS);
285 static inline struct gpio_desc *__must_check
286 devm_gpiod_get_optional(struct device *dev, const char *con_id,
287 enum gpiod_flags flags)
292 static inline struct gpio_desc *__must_check
293 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
294 unsigned int index, enum gpiod_flags flags)
299 static inline struct gpio_descs *__must_check
300 devm_gpiod_get_array(struct device *dev, const char *con_id,
301 enum gpiod_flags flags)
303 return ERR_PTR(-ENOSYS);
306 static inline struct gpio_descs *__must_check
307 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
308 enum gpiod_flags flags)
313 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
317 /* GPIO can never have been requested */
321 static inline void devm_gpiod_put_array(struct device *dev,
322 struct gpio_descs *descs)
326 /* GPIO can never have been requested */
331 static inline int gpiod_get_direction(const struct gpio_desc *desc)
333 /* GPIO can never have been requested */
337 static inline int gpiod_direction_input(struct gpio_desc *desc)
339 /* GPIO can never have been requested */
343 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
345 /* GPIO can never have been requested */
349 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
351 /* GPIO can never have been requested */
355 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
361 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
367 static inline int gpiod_get_value(const struct gpio_desc *desc)
369 /* GPIO can never have been requested */
373 static inline int gpiod_get_array_value(unsigned int array_size,
374 struct gpio_desc **desc_array,
375 struct gpio_array *array_info,
376 unsigned long *value_bitmap)
378 /* GPIO can never have been requested */
382 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
384 /* GPIO can never have been requested */
387 static inline int gpiod_set_array_value(unsigned int array_size,
388 struct gpio_desc **desc_array,
389 struct gpio_array *array_info,
390 unsigned long *value_bitmap)
392 /* GPIO can never have been requested */
396 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
398 /* GPIO can never have been requested */
402 static inline int gpiod_get_raw_array_value(unsigned int array_size,
403 struct gpio_desc **desc_array,
404 struct gpio_array *array_info,
405 unsigned long *value_bitmap)
407 /* GPIO can never have been requested */
411 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
413 /* GPIO can never have been requested */
416 static inline int gpiod_set_raw_array_value(unsigned int array_size,
417 struct gpio_desc **desc_array,
418 struct gpio_array *array_info,
419 unsigned long *value_bitmap)
421 /* GPIO can never have been requested */
426 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
428 /* GPIO can never have been requested */
432 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
433 struct gpio_desc **desc_array,
434 struct gpio_array *array_info,
435 unsigned long *value_bitmap)
437 /* GPIO can never have been requested */
441 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
443 /* GPIO can never have been requested */
446 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
447 struct gpio_desc **desc_array,
448 struct gpio_array *array_info,
449 unsigned long *value_bitmap)
451 /* GPIO can never have been requested */
455 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
457 /* GPIO can never have been requested */
461 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
462 struct gpio_desc **desc_array,
463 struct gpio_array *array_info,
464 unsigned long *value_bitmap)
466 /* GPIO can never have been requested */
470 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
473 /* GPIO can never have been requested */
476 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
477 struct gpio_desc **desc_array,
478 struct gpio_array *array_info,
479 unsigned long *value_bitmap)
481 /* GPIO can never have been requested */
486 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
488 /* GPIO can never have been requested */
493 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
495 /* GPIO can never have been requested */
500 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
502 /* GPIO can never have been requested */
507 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
509 /* GPIO can never have been requested */
513 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
515 /* GPIO can never have been requested */
519 static inline int gpiod_cansleep(const struct gpio_desc *desc)
521 /* GPIO can never have been requested */
526 static inline int gpiod_to_irq(const struct gpio_desc *desc)
528 /* GPIO can never have been requested */
533 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
536 /* GPIO can never have been requested */
541 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
546 static inline int desc_to_gpio(const struct gpio_desc *desc)
548 /* GPIO can never have been requested */
553 /* Child properties interface */
554 struct fwnode_handle;
557 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
558 const char *propname, int index,
559 enum gpiod_flags dflags,
562 return ERR_PTR(-ENOSYS);
566 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
567 const char *con_id, int index,
568 enum gpiod_flags flags,
571 return ERR_PTR(-ENOSYS);
575 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
576 struct fwnode_handle *fwnode,
577 const char *con_id, int index,
578 enum gpiod_flags flags,
581 return ERR_PTR(-ENOSYS);
584 #endif /* CONFIG_GPIOLIB */
587 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
588 struct fwnode_handle *fwnode,
590 enum gpiod_flags flags,
593 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
598 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
599 const char *con_id, int index,
600 struct fwnode_handle *child,
601 enum gpiod_flags flags,
604 return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
609 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
611 struct fwnode_handle *child,
612 enum gpiod_flags flags,
615 return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
618 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
621 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
622 const char *propname, int index,
623 enum gpiod_flags dflags,
626 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
631 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
632 const char *propname, int index,
633 enum gpiod_flags dflags,
636 return ERR_PTR(-ENOSYS);
639 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
641 #ifdef CONFIG_GPIOLIB
644 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
645 const struct device_node *node,
646 const char *propname, int index,
647 enum gpiod_flags dflags,
650 #else /* CONFIG_GPIOLIB */
655 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
656 const struct device_node *node,
657 const char *propname, int index,
658 enum gpiod_flags dflags,
661 return ERR_PTR(-ENOSYS);
664 #endif /* CONFIG_GPIOLIB */
666 struct acpi_gpio_params {
667 unsigned int crs_entry_index;
668 unsigned int line_index;
672 struct acpi_gpio_mapping {
674 const struct acpi_gpio_params *data;
677 /* Ignore IoRestriction field */
678 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
680 * When ACPI GPIO mapping table is in use the index parameter inside it
681 * refers to the GPIO resource in _CRS method. That index has no
682 * distinction of actual type of the resource. When consumer wants to
683 * get GpioIo type explicitly, this quirk may be used.
685 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
686 /* Use given pin as an absolute GPIO number in the system */
687 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
694 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
696 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
697 const struct acpi_gpio_mapping *gpios);
698 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
700 int devm_acpi_dev_add_driver_gpios(struct device *dev,
701 const struct acpi_gpio_mapping *gpios);
703 struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin, char *label);
705 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
707 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
708 const struct acpi_gpio_mapping *gpios)
712 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
714 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
715 const struct acpi_gpio_mapping *gpios)
720 static inline struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin,
723 return ERR_PTR(-ENOSYS);
726 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
729 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
731 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
732 int gpiod_export_link(struct device *dev, const char *name,
733 struct gpio_desc *desc);
734 void gpiod_unexport(struct gpio_desc *desc);
736 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
738 static inline int gpiod_export(struct gpio_desc *desc,
739 bool direction_may_change)
744 static inline int gpiod_export_link(struct device *dev, const char *name,
745 struct gpio_desc *desc)
750 static inline void gpiod_unexport(struct gpio_desc *desc)
754 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */