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);
161 void gpiod_toggle_active_low(struct gpio_desc *desc);
163 int gpiod_is_active_low(const struct gpio_desc *desc);
164 int gpiod_cansleep(const struct gpio_desc *desc);
166 int gpiod_to_irq(const struct gpio_desc *desc);
167 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
169 /* Convert between the old gpio_ and new gpiod_ interfaces */
170 struct gpio_desc *gpio_to_desc(unsigned gpio);
171 int desc_to_gpio(const struct gpio_desc *desc);
173 /* Child properties interface */
174 struct fwnode_handle;
176 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
177 const char *propname, int index,
178 enum gpiod_flags dflags,
180 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
181 const char *con_id, int index,
182 enum gpiod_flags flags,
184 struct 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,
190 #else /* CONFIG_GPIOLIB */
192 static inline int gpiod_count(struct device *dev, const char *con_id)
197 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
199 enum gpiod_flags flags)
201 return ERR_PTR(-ENOSYS);
203 static inline struct gpio_desc *__must_check
204 gpiod_get_index(struct device *dev,
207 enum gpiod_flags flags)
209 return ERR_PTR(-ENOSYS);
212 static inline struct gpio_desc *__must_check
213 gpiod_get_optional(struct device *dev, const char *con_id,
214 enum gpiod_flags flags)
219 static inline struct gpio_desc *__must_check
220 gpiod_get_index_optional(struct device *dev, const char *con_id,
221 unsigned int index, enum gpiod_flags flags)
226 static inline struct gpio_descs *__must_check
227 gpiod_get_array(struct device *dev, const char *con_id,
228 enum gpiod_flags flags)
230 return ERR_PTR(-ENOSYS);
233 static inline struct gpio_descs *__must_check
234 gpiod_get_array_optional(struct device *dev, const char *con_id,
235 enum gpiod_flags flags)
240 static inline void gpiod_put(struct gpio_desc *desc)
244 /* GPIO can never have been requested */
248 static inline void devm_gpiod_unhinge(struct device *dev,
249 struct gpio_desc *desc)
253 /* GPIO can never have been requested */
257 static inline void gpiod_put_array(struct gpio_descs *descs)
261 /* GPIO can never have been requested */
265 static inline struct gpio_desc *__must_check
266 devm_gpiod_get(struct device *dev,
268 enum gpiod_flags flags)
270 return ERR_PTR(-ENOSYS);
273 struct gpio_desc *__must_check
274 devm_gpiod_get_index(struct device *dev,
277 enum gpiod_flags flags)
279 return ERR_PTR(-ENOSYS);
282 static inline struct gpio_desc *__must_check
283 devm_gpiod_get_optional(struct device *dev, const char *con_id,
284 enum gpiod_flags flags)
289 static inline struct gpio_desc *__must_check
290 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
291 unsigned int index, enum gpiod_flags flags)
296 static inline struct gpio_descs *__must_check
297 devm_gpiod_get_array(struct device *dev, const char *con_id,
298 enum gpiod_flags flags)
300 return ERR_PTR(-ENOSYS);
303 static inline struct gpio_descs *__must_check
304 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
305 enum gpiod_flags flags)
310 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
314 /* GPIO can never have been requested */
318 static inline void devm_gpiod_put_array(struct device *dev,
319 struct gpio_descs *descs)
323 /* GPIO can never have been requested */
328 static inline int gpiod_get_direction(const struct gpio_desc *desc)
330 /* GPIO can never have been requested */
334 static inline int gpiod_direction_input(struct gpio_desc *desc)
336 /* GPIO can never have been requested */
340 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
342 /* GPIO can never have been requested */
346 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
348 /* GPIO can never have been requested */
354 static inline int gpiod_get_value(const struct gpio_desc *desc)
356 /* GPIO can never have been requested */
360 static 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)
365 /* GPIO can never have been requested */
369 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
371 /* GPIO can never have been requested */
374 static 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)
379 /* GPIO can never have been requested */
383 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
385 /* GPIO can never have been requested */
389 static 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)
394 /* GPIO can never have been requested */
398 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
400 /* GPIO can never have been requested */
403 static 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)
408 /* GPIO can never have been requested */
413 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
415 /* GPIO can never have been requested */
419 static 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)
424 /* GPIO can never have been requested */
428 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
430 /* GPIO can never have been requested */
433 static 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)
438 /* GPIO can never have been requested */
442 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
444 /* GPIO can never have been requested */
448 static 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)
453 /* GPIO can never have been requested */
457 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
460 /* GPIO can never have been requested */
463 static 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)
468 /* GPIO can never have been requested */
473 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
475 /* GPIO can never have been requested */
480 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
482 /* GPIO can never have been requested */
487 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
489 /* GPIO can never have been requested */
493 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
495 /* GPIO can never have been requested */
499 static inline int gpiod_cansleep(const struct gpio_desc *desc)
501 /* GPIO can never have been requested */
506 static inline int gpiod_to_irq(const struct gpio_desc *desc)
508 /* GPIO can never have been requested */
513 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
516 /* GPIO can never have been requested */
521 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
526 static inline int desc_to_gpio(const struct gpio_desc *desc)
528 /* GPIO can never have been requested */
533 /* Child properties interface */
534 struct fwnode_handle;
537 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
538 const char *propname, int index,
539 enum gpiod_flags dflags,
542 return ERR_PTR(-ENOSYS);
546 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
547 const char *con_id, int index,
548 enum gpiod_flags flags,
551 return ERR_PTR(-ENOSYS);
555 struct 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,
561 return ERR_PTR(-ENOSYS);
564 #endif /* CONFIG_GPIOLIB */
567 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
568 struct fwnode_handle *fwnode,
570 enum gpiod_flags flags,
573 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
578 struct 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,
584 return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
589 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
591 struct fwnode_handle *child,
592 enum gpiod_flags flags,
595 return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
598 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
601 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
602 const char *propname, int index,
603 enum gpiod_flags dflags,
606 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
611 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
612 const char *propname, int index,
613 enum gpiod_flags dflags,
616 return ERR_PTR(-ENOSYS);
619 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
621 #ifdef CONFIG_GPIOLIB
624 struct 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,
630 #else /* CONFIG_GPIOLIB */
635 struct 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,
641 return ERR_PTR(-ENOSYS);
644 #endif /* CONFIG_GPIOLIB */
646 struct acpi_gpio_params {
647 unsigned int crs_entry_index;
648 unsigned int line_index;
652 struct acpi_gpio_mapping {
654 const struct acpi_gpio_params *data;
657 /* Ignore IoRestriction field */
658 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
660 * When ACPI GPIO mapping table is in use the index parameter inside it
661 * refers to the GPIO resource in _CRS method. That index has no
662 * distinction of actual type of the resource. When consumer wants to
663 * get GpioIo type explicitly, this quirk may be used.
665 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
670 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
674 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
675 const struct acpi_gpio_mapping *gpios);
676 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
678 int devm_acpi_dev_add_driver_gpios(struct device *dev,
679 const struct acpi_gpio_mapping *gpios);
680 void devm_acpi_dev_remove_driver_gpios(struct device *dev);
682 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
686 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
687 const struct acpi_gpio_mapping *gpios)
691 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
693 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
694 const struct acpi_gpio_mapping *gpios)
698 static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
700 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
703 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
705 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
706 int gpiod_export_link(struct device *dev, const char *name,
707 struct gpio_desc *desc);
708 void gpiod_unexport(struct gpio_desc *desc);
710 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
712 static inline int gpiod_export(struct gpio_desc *desc,
713 bool direction_may_change)
718 static inline int gpiod_export_link(struct device *dev, const char *name,
719 struct gpio_desc *desc)
724 static inline void gpiod_unexport(struct gpio_desc *desc)
728 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */