1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
7 #include <linux/kernel.h>
12 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
13 * preferable to the old integer-based handles.
15 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
16 * until the GPIO is released.
21 * Opaque descriptor for a structure of GPIO array attributes. This structure
22 * is attached to struct gpiod_descs obtained from gpiod_get_array() and can be
23 * passed back to get/set array functions in order to activate fast processing
29 * Struct containing an array of descriptors that can be obtained using
33 struct gpio_array *info;
35 struct gpio_desc *desc[];
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)
45 * Optional flags that can be passed to one of gpiod_* to configure direction
46 * and output value. These values cannot be OR'd.
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,
60 /* Return the number of GPIOs associated with a device / function */
61 int gpiod_count(struct device *dev, const char *con_id);
63 /* Acquire and dispose GPIOs */
64 struct gpio_desc *__must_check gpiod_get(struct device *dev,
66 enum gpiod_flags flags);
67 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
70 enum gpiod_flags flags);
71 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
73 enum gpiod_flags flags);
74 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
77 enum gpiod_flags flags);
78 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
80 enum gpiod_flags flags);
81 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
83 enum gpiod_flags flags);
84 void gpiod_put(struct gpio_desc *desc);
85 void gpiod_put_array(struct gpio_descs *descs);
87 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
89 enum gpiod_flags flags);
90 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
93 enum gpiod_flags flags);
94 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
96 enum gpiod_flags flags);
97 struct gpio_desc *__must_check
98 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
99 unsigned int index, enum gpiod_flags flags);
100 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
102 enum gpiod_flags flags);
103 struct gpio_descs *__must_check
104 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
105 enum gpiod_flags flags);
106 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
107 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
108 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
110 int gpiod_get_direction(struct gpio_desc *desc);
111 int gpiod_direction_input(struct gpio_desc *desc);
112 int gpiod_direction_output(struct gpio_desc *desc, int value);
113 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
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_debounce(struct gpio_desc *desc, unsigned debounce);
160 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162 int gpiod_is_active_low(const struct gpio_desc *desc);
163 int gpiod_cansleep(const struct gpio_desc *desc);
165 int gpiod_to_irq(const struct gpio_desc *desc);
166 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
168 /* Convert between the old gpio_ and new gpiod_ interfaces */
169 struct gpio_desc *gpio_to_desc(unsigned gpio);
170 int desc_to_gpio(const struct gpio_desc *desc);
172 /* Child properties interface */
173 struct fwnode_handle;
175 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
176 const char *propname, int index,
177 enum gpiod_flags dflags,
179 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
180 const char *con_id, int index,
181 struct fwnode_handle *child,
182 enum gpiod_flags flags,
185 #else /* CONFIG_GPIOLIB */
187 static inline int gpiod_count(struct device *dev, const char *con_id)
192 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
194 enum gpiod_flags flags)
196 return ERR_PTR(-ENOSYS);
198 static inline struct gpio_desc *__must_check
199 gpiod_get_index(struct device *dev,
202 enum gpiod_flags flags)
204 return ERR_PTR(-ENOSYS);
207 static inline struct gpio_desc *__must_check
208 gpiod_get_optional(struct device *dev, const char *con_id,
209 enum gpiod_flags flags)
214 static inline struct gpio_desc *__must_check
215 gpiod_get_index_optional(struct device *dev, const char *con_id,
216 unsigned int index, enum gpiod_flags flags)
221 static inline struct gpio_descs *__must_check
222 gpiod_get_array(struct device *dev, const char *con_id,
223 enum gpiod_flags flags)
225 return ERR_PTR(-ENOSYS);
228 static inline struct gpio_descs *__must_check
229 gpiod_get_array_optional(struct device *dev, const char *con_id,
230 enum gpiod_flags flags)
235 static inline void gpiod_put(struct gpio_desc *desc)
239 /* GPIO can never have been requested */
243 static inline void devm_gpiod_unhinge(struct device *dev,
244 struct gpio_desc *desc)
248 /* GPIO can never have been requested */
252 static inline void gpiod_put_array(struct gpio_descs *descs)
256 /* GPIO can never have been requested */
260 static inline struct gpio_desc *__must_check
261 devm_gpiod_get(struct device *dev,
263 enum gpiod_flags flags)
265 return ERR_PTR(-ENOSYS);
268 struct gpio_desc *__must_check
269 devm_gpiod_get_index(struct device *dev,
272 enum gpiod_flags flags)
274 return ERR_PTR(-ENOSYS);
277 static inline struct gpio_desc *__must_check
278 devm_gpiod_get_optional(struct device *dev, const char *con_id,
279 enum gpiod_flags flags)
284 static inline struct gpio_desc *__must_check
285 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
286 unsigned int index, enum gpiod_flags flags)
291 static inline struct gpio_descs *__must_check
292 devm_gpiod_get_array(struct device *dev, const char *con_id,
293 enum gpiod_flags flags)
295 return ERR_PTR(-ENOSYS);
298 static inline struct gpio_descs *__must_check
299 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
300 enum gpiod_flags flags)
305 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
309 /* GPIO can never have been requested */
313 static inline void devm_gpiod_put_array(struct device *dev,
314 struct gpio_descs *descs)
318 /* GPIO can never have been requested */
323 static inline int gpiod_get_direction(const struct gpio_desc *desc)
325 /* GPIO can never have been requested */
329 static inline int gpiod_direction_input(struct gpio_desc *desc)
331 /* GPIO can never have been requested */
335 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
337 /* GPIO can never have been requested */
341 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
343 /* GPIO can never have been requested */
349 static inline int gpiod_get_value(const struct gpio_desc *desc)
351 /* GPIO can never have been requested */
355 static inline int gpiod_get_array_value(unsigned int array_size,
356 struct gpio_desc **desc_array,
357 struct gpio_array *array_info,
358 unsigned long *value_bitmap)
360 /* GPIO can never have been requested */
364 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
366 /* GPIO can never have been requested */
369 static inline int gpiod_set_array_value(unsigned int array_size,
370 struct gpio_desc **desc_array,
371 struct gpio_array *array_info,
372 unsigned long *value_bitmap)
374 /* GPIO can never have been requested */
378 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
380 /* GPIO can never have been requested */
384 static inline int gpiod_get_raw_array_value(unsigned int array_size,
385 struct gpio_desc **desc_array,
386 struct gpio_array *array_info,
387 unsigned long *value_bitmap)
389 /* GPIO can never have been requested */
393 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
395 /* GPIO can never have been requested */
398 static inline int gpiod_set_raw_array_value(unsigned int array_size,
399 struct gpio_desc **desc_array,
400 struct gpio_array *array_info,
401 unsigned long *value_bitmap)
403 /* GPIO can never have been requested */
408 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
410 /* GPIO can never have been requested */
414 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
415 struct gpio_desc **desc_array,
416 struct gpio_array *array_info,
417 unsigned long *value_bitmap)
419 /* GPIO can never have been requested */
423 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
425 /* GPIO can never have been requested */
428 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
429 struct gpio_desc **desc_array,
430 struct gpio_array *array_info,
431 unsigned long *value_bitmap)
433 /* GPIO can never have been requested */
437 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
439 /* GPIO can never have been requested */
443 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
444 struct gpio_desc **desc_array,
445 struct gpio_array *array_info,
446 unsigned long *value_bitmap)
448 /* GPIO can never have been requested */
452 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
455 /* GPIO can never have been requested */
458 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
459 struct gpio_desc **desc_array,
460 struct gpio_array *array_info,
461 unsigned long *value_bitmap)
463 /* GPIO can never have been requested */
468 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
470 /* GPIO can never have been requested */
475 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
477 /* GPIO can never have been requested */
482 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
484 /* GPIO can never have been requested */
488 static inline int gpiod_cansleep(const struct gpio_desc *desc)
490 /* GPIO can never have been requested */
495 static inline int gpiod_to_irq(const struct gpio_desc *desc)
497 /* GPIO can never have been requested */
502 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
505 /* GPIO can never have been requested */
510 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
515 static inline int desc_to_gpio(const struct gpio_desc *desc)
517 /* GPIO can never have been requested */
522 /* Child properties interface */
523 struct fwnode_handle;
526 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
527 const char *propname, int index,
528 enum gpiod_flags dflags,
531 return ERR_PTR(-ENOSYS);
535 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
536 const char *con_id, int index,
537 struct fwnode_handle *child,
538 enum gpiod_flags flags,
541 return ERR_PTR(-ENOSYS);
544 #endif /* CONFIG_GPIOLIB */
547 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
549 struct fwnode_handle *child,
550 enum gpiod_flags flags,
553 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
557 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
560 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
561 const char *propname, int index,
562 enum gpiod_flags dflags,
565 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
570 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
571 const char *propname, int index,
572 enum gpiod_flags dflags,
575 return ERR_PTR(-ENOSYS);
578 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
580 #ifdef CONFIG_GPIOLIB
583 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
584 struct device_node *node,
585 const char *propname, int index,
586 enum gpiod_flags dflags,
589 #else /* CONFIG_GPIOLIB */
594 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
595 struct device_node *node,
596 const char *propname, int index,
597 enum gpiod_flags dflags,
600 return ERR_PTR(-ENOSYS);
603 #endif /* CONFIG_GPIOLIB */
605 struct acpi_gpio_params {
606 unsigned int crs_entry_index;
607 unsigned int line_index;
611 struct acpi_gpio_mapping {
613 const struct acpi_gpio_params *data;
616 /* Ignore IoRestriction field */
617 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
619 * When ACPI GPIO mapping table is in use the index parameter inside it
620 * refers to the GPIO resource in _CRS method. That index has no
621 * distinction of actual type of the resource. When consumer wants to
622 * get GpioIo type explicitly, this quirk may be used.
624 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
629 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
633 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
634 const struct acpi_gpio_mapping *gpios);
635 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
637 int devm_acpi_dev_add_driver_gpios(struct device *dev,
638 const struct acpi_gpio_mapping *gpios);
639 void devm_acpi_dev_remove_driver_gpios(struct device *dev);
641 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
645 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
646 const struct acpi_gpio_mapping *gpios)
650 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
652 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
653 const struct acpi_gpio_mapping *gpios)
657 static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
659 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
662 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
664 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
665 int gpiod_export_link(struct device *dev, const char *name,
666 struct gpio_desc *desc);
667 void gpiod_unexport(struct gpio_desc *desc);
669 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
671 static inline int gpiod_export(struct gpio_desc *desc,
672 bool direction_may_change)
677 static inline int gpiod_export_link(struct device *dev, const char *name,
678 struct gpio_desc *desc)
683 static inline void gpiod_unexport(struct gpio_desc *desc)
687 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */