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 *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
181 const char *con_id, int index,
182 struct fwnode_handle *child,
183 enum gpiod_flags flags,
186 #else /* CONFIG_GPIOLIB */
188 static inline int gpiod_count(struct device *dev, const char *con_id)
193 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
195 enum gpiod_flags flags)
197 return ERR_PTR(-ENOSYS);
199 static inline struct gpio_desc *__must_check
200 gpiod_get_index(struct device *dev,
203 enum gpiod_flags flags)
205 return ERR_PTR(-ENOSYS);
208 static inline struct gpio_desc *__must_check
209 gpiod_get_optional(struct device *dev, const char *con_id,
210 enum gpiod_flags flags)
215 static inline struct gpio_desc *__must_check
216 gpiod_get_index_optional(struct device *dev, const char *con_id,
217 unsigned int index, enum gpiod_flags flags)
222 static inline struct gpio_descs *__must_check
223 gpiod_get_array(struct device *dev, const char *con_id,
224 enum gpiod_flags flags)
226 return ERR_PTR(-ENOSYS);
229 static inline struct gpio_descs *__must_check
230 gpiod_get_array_optional(struct device *dev, const char *con_id,
231 enum gpiod_flags flags)
236 static inline void gpiod_put(struct gpio_desc *desc)
240 /* GPIO can never have been requested */
244 static inline void devm_gpiod_unhinge(struct device *dev,
245 struct gpio_desc *desc)
249 /* GPIO can never have been requested */
253 static inline void gpiod_put_array(struct gpio_descs *descs)
257 /* GPIO can never have been requested */
261 static inline struct gpio_desc *__must_check
262 devm_gpiod_get(struct device *dev,
264 enum gpiod_flags flags)
266 return ERR_PTR(-ENOSYS);
269 struct gpio_desc *__must_check
270 devm_gpiod_get_index(struct device *dev,
273 enum gpiod_flags flags)
275 return ERR_PTR(-ENOSYS);
278 static inline struct gpio_desc *__must_check
279 devm_gpiod_get_optional(struct device *dev, const char *con_id,
280 enum gpiod_flags flags)
285 static inline struct gpio_desc *__must_check
286 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
287 unsigned int index, enum gpiod_flags flags)
292 static inline struct gpio_descs *__must_check
293 devm_gpiod_get_array(struct device *dev, const char *con_id,
294 enum gpiod_flags flags)
296 return ERR_PTR(-ENOSYS);
299 static inline struct gpio_descs *__must_check
300 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
301 enum gpiod_flags flags)
306 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
310 /* GPIO can never have been requested */
314 static inline void devm_gpiod_put_array(struct device *dev,
315 struct gpio_descs *descs)
319 /* GPIO can never have been requested */
324 static inline int gpiod_get_direction(const struct gpio_desc *desc)
326 /* GPIO can never have been requested */
330 static inline int gpiod_direction_input(struct gpio_desc *desc)
332 /* GPIO can never have been requested */
336 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
338 /* GPIO can never have been requested */
342 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
344 /* GPIO can never have been requested */
350 static inline int gpiod_get_value(const struct gpio_desc *desc)
352 /* GPIO can never have been requested */
356 static inline int gpiod_get_array_value(unsigned int array_size,
357 struct gpio_desc **desc_array,
358 struct gpio_array *array_info,
359 unsigned long *value_bitmap)
361 /* GPIO can never have been requested */
365 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
367 /* GPIO can never have been requested */
370 static inline int gpiod_set_array_value(unsigned int array_size,
371 struct gpio_desc **desc_array,
372 struct gpio_array *array_info,
373 unsigned long *value_bitmap)
375 /* GPIO can never have been requested */
379 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
381 /* GPIO can never have been requested */
385 static inline int gpiod_get_raw_array_value(unsigned int array_size,
386 struct gpio_desc **desc_array,
387 struct gpio_array *array_info,
388 unsigned long *value_bitmap)
390 /* GPIO can never have been requested */
394 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
396 /* GPIO can never have been requested */
399 static inline int gpiod_set_raw_array_value(unsigned int array_size,
400 struct gpio_desc **desc_array,
401 struct gpio_array *array_info,
402 unsigned long *value_bitmap)
404 /* GPIO can never have been requested */
409 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
411 /* GPIO can never have been requested */
415 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
416 struct gpio_desc **desc_array,
417 struct gpio_array *array_info,
418 unsigned long *value_bitmap)
420 /* GPIO can never have been requested */
424 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
426 /* GPIO can never have been requested */
429 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
430 struct gpio_desc **desc_array,
431 struct gpio_array *array_info,
432 unsigned long *value_bitmap)
434 /* GPIO can never have been requested */
438 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
440 /* GPIO can never have been requested */
444 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
445 struct gpio_desc **desc_array,
446 struct gpio_array *array_info,
447 unsigned long *value_bitmap)
449 /* GPIO can never have been requested */
453 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
456 /* GPIO can never have been requested */
459 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
460 struct gpio_desc **desc_array,
461 struct gpio_array *array_info,
462 unsigned long *value_bitmap)
464 /* GPIO can never have been requested */
469 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
471 /* GPIO can never have been requested */
476 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
478 /* GPIO can never have been requested */
483 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
485 /* GPIO can never have been requested */
489 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
491 /* GPIO can never have been requested */
495 static inline int gpiod_cansleep(const struct gpio_desc *desc)
497 /* GPIO can never have been requested */
502 static inline int gpiod_to_irq(const struct gpio_desc *desc)
504 /* GPIO can never have been requested */
509 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
512 /* GPIO can never have been requested */
517 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
522 static inline int desc_to_gpio(const struct gpio_desc *desc)
524 /* GPIO can never have been requested */
529 /* Child properties interface */
530 struct fwnode_handle;
533 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
534 const char *propname, int index,
535 enum gpiod_flags dflags,
538 return ERR_PTR(-ENOSYS);
542 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
543 const char *con_id, int index,
544 struct fwnode_handle *child,
545 enum gpiod_flags flags,
548 return ERR_PTR(-ENOSYS);
551 #endif /* CONFIG_GPIOLIB */
554 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
556 struct fwnode_handle *child,
557 enum gpiod_flags flags,
560 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
564 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
567 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
568 const char *propname, int index,
569 enum gpiod_flags dflags,
572 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
577 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
578 const char *propname, int index,
579 enum gpiod_flags dflags,
582 return ERR_PTR(-ENOSYS);
585 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
587 #ifdef CONFIG_GPIOLIB
590 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
591 struct device_node *node,
592 const char *propname, int index,
593 enum gpiod_flags dflags,
596 #else /* CONFIG_GPIOLIB */
601 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
602 struct device_node *node,
603 const char *propname, int index,
604 enum gpiod_flags dflags,
607 return ERR_PTR(-ENOSYS);
610 #endif /* CONFIG_GPIOLIB */
612 struct acpi_gpio_params {
613 unsigned int crs_entry_index;
614 unsigned int line_index;
618 struct acpi_gpio_mapping {
620 const struct acpi_gpio_params *data;
623 /* Ignore IoRestriction field */
624 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
626 * When ACPI GPIO mapping table is in use the index parameter inside it
627 * refers to the GPIO resource in _CRS method. That index has no
628 * distinction of actual type of the resource. When consumer wants to
629 * get GpioIo type explicitly, this quirk may be used.
631 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
636 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
640 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
641 const struct acpi_gpio_mapping *gpios);
642 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
644 int devm_acpi_dev_add_driver_gpios(struct device *dev,
645 const struct acpi_gpio_mapping *gpios);
646 void devm_acpi_dev_remove_driver_gpios(struct device *dev);
648 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
652 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
653 const struct acpi_gpio_mapping *gpios)
657 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
659 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
660 const struct acpi_gpio_mapping *gpios)
664 static inline void devm_acpi_dev_remove_driver_gpios(struct device *dev) {}
666 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
669 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
671 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
672 int gpiod_export_link(struct device *dev, const char *name,
673 struct gpio_desc *desc);
674 void gpiod_unexport(struct gpio_desc *desc);
676 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
678 static inline int gpiod_export(struct gpio_desc *desc,
679 bool direction_may_change)
684 static inline int gpiod_export_link(struct device *dev, const char *name,
685 struct gpio_desc *desc)
690 static inline void gpiod_unexport(struct gpio_desc *desc)
694 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */