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);
113 /* Value get/set from non-sleeping context */
114 int gpiod_get_value(const struct gpio_desc *desc);
115 int gpiod_get_array_value(unsigned int array_size,
116 struct gpio_desc **desc_array,
117 struct gpio_array *array_info,
118 unsigned long *value_bitmap);
119 void gpiod_set_value(struct gpio_desc *desc, int value);
120 int gpiod_set_array_value(unsigned int array_size,
121 struct gpio_desc **desc_array,
122 struct gpio_array *array_info,
123 unsigned long *value_bitmap);
124 int gpiod_get_raw_value(const struct gpio_desc *desc);
125 int gpiod_get_raw_array_value(unsigned int array_size,
126 struct gpio_desc **desc_array,
127 struct gpio_array *array_info,
128 unsigned long *value_bitmap);
129 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
130 int gpiod_set_raw_array_value(unsigned int array_size,
131 struct gpio_desc **desc_array,
132 struct gpio_array *array_info,
133 unsigned long *value_bitmap);
135 /* Value get/set from sleeping context */
136 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
137 int gpiod_get_array_value_cansleep(unsigned int array_size,
138 struct gpio_desc **desc_array,
139 struct gpio_array *array_info,
140 unsigned long *value_bitmap);
141 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
142 int gpiod_set_array_value_cansleep(unsigned int array_size,
143 struct gpio_desc **desc_array,
144 struct gpio_array *array_info,
145 unsigned long *value_bitmap);
146 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
147 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
148 struct gpio_desc **desc_array,
149 struct gpio_array *array_info,
150 unsigned long *value_bitmap);
151 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
152 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
153 struct gpio_desc **desc_array,
154 struct gpio_array *array_info,
155 unsigned long *value_bitmap);
157 int gpiod_set_config(struct gpio_desc *desc, unsigned long config);
158 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce);
159 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
160 void gpiod_toggle_active_low(struct gpio_desc *desc);
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 *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
180 const char *con_id, int index,
181 enum gpiod_flags flags,
183 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
184 struct fwnode_handle *child,
185 const char *con_id, int index,
186 enum gpiod_flags flags,
189 #else /* CONFIG_GPIOLIB */
191 #include <linux/kernel.h>
193 static inline int gpiod_count(struct device *dev, const char *con_id)
198 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
200 enum gpiod_flags flags)
202 return ERR_PTR(-ENOSYS);
204 static inline struct gpio_desc *__must_check
205 gpiod_get_index(struct device *dev,
208 enum gpiod_flags flags)
210 return ERR_PTR(-ENOSYS);
213 static inline struct gpio_desc *__must_check
214 gpiod_get_optional(struct device *dev, const char *con_id,
215 enum gpiod_flags flags)
220 static inline struct gpio_desc *__must_check
221 gpiod_get_index_optional(struct device *dev, const char *con_id,
222 unsigned int index, enum gpiod_flags flags)
227 static inline struct gpio_descs *__must_check
228 gpiod_get_array(struct device *dev, const char *con_id,
229 enum gpiod_flags flags)
231 return ERR_PTR(-ENOSYS);
234 static inline struct gpio_descs *__must_check
235 gpiod_get_array_optional(struct device *dev, const char *con_id,
236 enum gpiod_flags flags)
241 static inline void gpiod_put(struct gpio_desc *desc)
245 /* GPIO can never have been requested */
249 static inline void devm_gpiod_unhinge(struct device *dev,
250 struct gpio_desc *desc)
254 /* GPIO can never have been requested */
258 static inline void gpiod_put_array(struct gpio_descs *descs)
262 /* GPIO can never have been requested */
266 static inline struct gpio_desc *__must_check
267 devm_gpiod_get(struct device *dev,
269 enum gpiod_flags flags)
271 return ERR_PTR(-ENOSYS);
274 struct gpio_desc *__must_check
275 devm_gpiod_get_index(struct device *dev,
278 enum gpiod_flags flags)
280 return ERR_PTR(-ENOSYS);
283 static inline struct gpio_desc *__must_check
284 devm_gpiod_get_optional(struct device *dev, const char *con_id,
285 enum gpiod_flags flags)
290 static inline struct gpio_desc *__must_check
291 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
292 unsigned int index, enum gpiod_flags flags)
297 static inline struct gpio_descs *__must_check
298 devm_gpiod_get_array(struct device *dev, const char *con_id,
299 enum gpiod_flags flags)
301 return ERR_PTR(-ENOSYS);
304 static inline struct gpio_descs *__must_check
305 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
306 enum gpiod_flags flags)
311 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
315 /* GPIO can never have been requested */
319 static inline void devm_gpiod_put_array(struct device *dev,
320 struct gpio_descs *descs)
324 /* GPIO can never have been requested */
329 static inline int gpiod_get_direction(const struct gpio_desc *desc)
331 /* GPIO can never have been requested */
335 static inline int gpiod_direction_input(struct gpio_desc *desc)
337 /* GPIO can never have been requested */
341 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
343 /* GPIO can never have been requested */
347 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
349 /* GPIO can never have been requested */
355 static inline int gpiod_get_value(const struct gpio_desc *desc)
357 /* GPIO can never have been requested */
361 static inline int gpiod_get_array_value(unsigned int array_size,
362 struct gpio_desc **desc_array,
363 struct gpio_array *array_info,
364 unsigned long *value_bitmap)
366 /* GPIO can never have been requested */
370 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
372 /* GPIO can never have been requested */
375 static inline int gpiod_set_array_value(unsigned int array_size,
376 struct gpio_desc **desc_array,
377 struct gpio_array *array_info,
378 unsigned long *value_bitmap)
380 /* GPIO can never have been requested */
384 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
386 /* GPIO can never have been requested */
390 static inline int gpiod_get_raw_array_value(unsigned int array_size,
391 struct gpio_desc **desc_array,
392 struct gpio_array *array_info,
393 unsigned long *value_bitmap)
395 /* GPIO can never have been requested */
399 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
401 /* GPIO can never have been requested */
404 static inline int gpiod_set_raw_array_value(unsigned int array_size,
405 struct gpio_desc **desc_array,
406 struct gpio_array *array_info,
407 unsigned long *value_bitmap)
409 /* GPIO can never have been requested */
414 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
416 /* GPIO can never have been requested */
420 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
421 struct gpio_desc **desc_array,
422 struct gpio_array *array_info,
423 unsigned long *value_bitmap)
425 /* GPIO can never have been requested */
429 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
431 /* GPIO can never have been requested */
434 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
435 struct gpio_desc **desc_array,
436 struct gpio_array *array_info,
437 unsigned long *value_bitmap)
439 /* GPIO can never have been requested */
443 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
445 /* GPIO can never have been requested */
449 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
450 struct gpio_desc **desc_array,
451 struct gpio_array *array_info,
452 unsigned long *value_bitmap)
454 /* GPIO can never have been requested */
458 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
461 /* GPIO can never have been requested */
464 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
465 struct gpio_desc **desc_array,
466 struct gpio_array *array_info,
467 unsigned long *value_bitmap)
469 /* GPIO can never have been requested */
474 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
476 /* GPIO can never have been requested */
481 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
483 /* GPIO can never have been requested */
488 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
490 /* GPIO can never have been requested */
495 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
497 /* GPIO can never have been requested */
501 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
503 /* GPIO can never have been requested */
507 static inline int gpiod_cansleep(const struct gpio_desc *desc)
509 /* GPIO can never have been requested */
514 static inline int gpiod_to_irq(const struct gpio_desc *desc)
516 /* GPIO can never have been requested */
521 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
524 /* GPIO can never have been requested */
529 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
534 static inline int desc_to_gpio(const struct gpio_desc *desc)
536 /* GPIO can never have been requested */
541 /* Child properties interface */
542 struct fwnode_handle;
545 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
546 const char *propname, int index,
547 enum gpiod_flags dflags,
550 return ERR_PTR(-ENOSYS);
554 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
555 const char *con_id, int index,
556 enum gpiod_flags flags,
559 return ERR_PTR(-ENOSYS);
563 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
564 struct fwnode_handle *fwnode,
565 const char *con_id, int index,
566 enum gpiod_flags flags,
569 return ERR_PTR(-ENOSYS);
572 #endif /* CONFIG_GPIOLIB */
575 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
576 struct fwnode_handle *fwnode,
578 enum gpiod_flags flags,
581 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
586 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
587 const char *con_id, int index,
588 struct fwnode_handle *child,
589 enum gpiod_flags flags,
592 return devm_fwnode_gpiod_get_index(dev, child, con_id, index,
597 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
599 struct fwnode_handle *child,
600 enum gpiod_flags flags,
603 return devm_fwnode_gpiod_get_index(dev, child, con_id, 0, flags, label);
606 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_OF_GPIO)
609 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
610 const char *propname, int index,
611 enum gpiod_flags dflags,
614 #else /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
619 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
620 const char *propname, int index,
621 enum gpiod_flags dflags,
624 return ERR_PTR(-ENOSYS);
627 #endif /* CONFIG_GPIOLIB && CONFIG_OF_GPIO */
629 #ifdef CONFIG_GPIOLIB
632 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
633 const struct device_node *node,
634 const char *propname, int index,
635 enum gpiod_flags dflags,
638 #else /* CONFIG_GPIOLIB */
643 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
644 const struct device_node *node,
645 const char *propname, int index,
646 enum gpiod_flags dflags,
649 return ERR_PTR(-ENOSYS);
652 #endif /* CONFIG_GPIOLIB */
654 struct acpi_gpio_params {
655 unsigned int crs_entry_index;
656 unsigned int line_index;
660 struct acpi_gpio_mapping {
662 const struct acpi_gpio_params *data;
665 /* Ignore IoRestriction field */
666 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
668 * When ACPI GPIO mapping table is in use the index parameter inside it
669 * refers to the GPIO resource in _CRS method. That index has no
670 * distinction of actual type of the resource. When consumer wants to
671 * get GpioIo type explicitly, this quirk may be used.
673 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
674 /* Use given pin as an absolute GPIO number in the system */
675 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
682 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
684 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
685 const struct acpi_gpio_mapping *gpios);
686 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
688 int devm_acpi_dev_add_driver_gpios(struct device *dev,
689 const struct acpi_gpio_mapping *gpios);
691 struct gpio_desc *acpi_get_and_request_gpiod(char *path, int pin, char *label);
693 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
695 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
696 const struct acpi_gpio_mapping *gpios)
700 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
702 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
703 const struct acpi_gpio_mapping *gpios)
708 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
711 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
713 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
714 int gpiod_export_link(struct device *dev, const char *name,
715 struct gpio_desc *desc);
716 void gpiod_unexport(struct gpio_desc *desc);
718 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
720 static inline int gpiod_export(struct gpio_desc *desc,
721 bool direction_may_change)
726 static inline int gpiod_export_link(struct device *dev, const char *name,
727 struct gpio_desc *desc)
732 static inline void gpiod_unexport(struct gpio_desc *desc)
736 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */