1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
5 #include <linux/bits.h>
6 #include <linux/types.h>
16 * struct gpio_descs - Struct containing an array of descriptors that can be
17 * obtained using gpiod_get_array()
19 * @info: Pointer to the opaque gpio_array structure
20 * @ndescs: Number of held descriptors
21 * @desc: Array of pointers to GPIO descriptors
24 struct gpio_array *info;
26 struct gpio_desc *desc[];
29 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
30 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
31 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
32 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
33 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
36 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to
37 * configure direction and output value. These values
40 * @GPIOD_ASIS: Don't change anything
41 * @GPIOD_IN: Set lines to input mode
42 * @GPIOD_OUT_LOW: Set lines to output and drive them low
43 * @GPIOD_OUT_HIGH: Set lines to output and drive them high
44 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low
45 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high
49 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
50 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
51 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
52 GPIOD_FLAGS_BIT_DIR_VAL,
53 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
54 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
59 /* Return the number of GPIOs associated with a device / function */
60 int gpiod_count(struct device *dev, const char *con_id);
62 /* Acquire and dispose GPIOs */
63 struct gpio_desc *__must_check gpiod_get(struct device *dev,
65 enum gpiod_flags flags);
66 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
69 enum gpiod_flags flags);
70 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
72 enum gpiod_flags flags);
73 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
76 enum gpiod_flags flags);
77 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
79 enum gpiod_flags flags);
80 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
82 enum gpiod_flags flags);
83 void gpiod_put(struct gpio_desc *desc);
84 void gpiod_put_array(struct gpio_descs *descs);
86 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
88 enum gpiod_flags flags);
89 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
92 enum gpiod_flags flags);
93 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
95 enum gpiod_flags flags);
96 struct gpio_desc *__must_check
97 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
98 unsigned int index, enum gpiod_flags flags);
99 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
101 enum gpiod_flags flags);
102 struct gpio_descs *__must_check
103 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
104 enum gpiod_flags flags);
105 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
106 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
107 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
109 int gpiod_get_direction(struct gpio_desc *desc);
110 int gpiod_direction_input(struct gpio_desc *desc);
111 int gpiod_direction_output(struct gpio_desc *desc, int value);
112 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
113 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
114 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
116 /* Value get/set from non-sleeping context */
117 int gpiod_get_value(const struct gpio_desc *desc);
118 int gpiod_get_array_value(unsigned int array_size,
119 struct gpio_desc **desc_array,
120 struct gpio_array *array_info,
121 unsigned long *value_bitmap);
122 void gpiod_set_value(struct gpio_desc *desc, int value);
123 int gpiod_set_array_value(unsigned int array_size,
124 struct gpio_desc **desc_array,
125 struct gpio_array *array_info,
126 unsigned long *value_bitmap);
127 int gpiod_get_raw_value(const struct gpio_desc *desc);
128 int gpiod_get_raw_array_value(unsigned int array_size,
129 struct gpio_desc **desc_array,
130 struct gpio_array *array_info,
131 unsigned long *value_bitmap);
132 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
133 int gpiod_set_raw_array_value(unsigned int array_size,
134 struct gpio_desc **desc_array,
135 struct gpio_array *array_info,
136 unsigned long *value_bitmap);
138 /* Value get/set from sleeping context */
139 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
140 int gpiod_get_array_value_cansleep(unsigned int array_size,
141 struct gpio_desc **desc_array,
142 struct gpio_array *array_info,
143 unsigned long *value_bitmap);
144 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
145 int gpiod_set_array_value_cansleep(unsigned int array_size,
146 struct gpio_desc **desc_array,
147 struct gpio_array *array_info,
148 unsigned long *value_bitmap);
149 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
150 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
151 struct gpio_desc **desc_array,
152 struct gpio_array *array_info,
153 unsigned long *value_bitmap);
154 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
155 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
156 struct gpio_desc **desc_array,
157 struct gpio_array *array_info,
158 unsigned long *value_bitmap);
160 int gpiod_set_config(struct gpio_desc *desc, unsigned long config);
161 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce);
162 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
163 void gpiod_toggle_active_low(struct gpio_desc *desc);
165 int gpiod_is_active_low(const struct gpio_desc *desc);
166 int gpiod_cansleep(const struct gpio_desc *desc);
168 int gpiod_to_irq(const struct gpio_desc *desc);
169 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
171 /* Convert between the old gpio_ and new gpiod_ interfaces */
172 struct gpio_desc *gpio_to_desc(unsigned gpio);
173 int desc_to_gpio(const struct gpio_desc *desc);
175 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
176 const char *con_id, int index,
177 enum gpiod_flags flags,
179 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
180 struct fwnode_handle *child,
181 const char *con_id, int index,
182 enum gpiod_flags flags,
185 #else /* CONFIG_GPIOLIB */
187 #include <linux/err.h>
188 #include <linux/kernel.h>
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 */
352 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
358 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
364 static inline int gpiod_get_value(const struct gpio_desc *desc)
366 /* GPIO can never have been requested */
370 static inline int gpiod_get_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 void gpiod_set_value(struct gpio_desc *desc, int value)
381 /* GPIO can never have been requested */
384 static inline int gpiod_set_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 int gpiod_get_raw_value(const struct gpio_desc *desc)
395 /* GPIO can never have been requested */
399 static inline int gpiod_get_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 */
408 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
410 /* GPIO can never have been requested */
413 static inline int gpiod_set_raw_array_value(unsigned int array_size,
414 struct gpio_desc **desc_array,
415 struct gpio_array *array_info,
416 unsigned long *value_bitmap)
418 /* GPIO can never have been requested */
423 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
425 /* GPIO can never have been requested */
429 static inline int gpiod_get_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 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
440 /* GPIO can never have been requested */
443 static inline int gpiod_set_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 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
454 /* GPIO can never have been requested */
458 static inline int gpiod_get_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 */
467 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
470 /* GPIO can never have been requested */
473 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
474 struct gpio_desc **desc_array,
475 struct gpio_array *array_info,
476 unsigned long *value_bitmap)
478 /* GPIO can never have been requested */
483 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
485 /* GPIO can never have been requested */
490 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
492 /* GPIO can never have been requested */
497 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
499 /* GPIO can never have been requested */
504 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
506 /* GPIO can never have been requested */
510 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
512 /* GPIO can never have been requested */
516 static inline int gpiod_cansleep(const struct gpio_desc *desc)
518 /* GPIO can never have been requested */
523 static inline int gpiod_to_irq(const struct gpio_desc *desc)
525 /* GPIO can never have been requested */
530 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
533 /* GPIO can never have been requested */
538 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
543 static inline int desc_to_gpio(const struct gpio_desc *desc)
545 /* GPIO can never have been requested */
551 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
552 const char *con_id, int index,
553 enum gpiod_flags flags,
556 return ERR_PTR(-ENOSYS);
560 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
561 struct fwnode_handle *fwnode,
562 const char *con_id, int index,
563 enum gpiod_flags flags,
566 return ERR_PTR(-ENOSYS);
569 #endif /* CONFIG_GPIOLIB */
572 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
573 struct fwnode_handle *fwnode,
575 enum gpiod_flags flags,
578 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
582 struct acpi_gpio_params {
583 unsigned int crs_entry_index;
584 unsigned int line_index;
588 struct acpi_gpio_mapping {
590 const struct acpi_gpio_params *data;
593 /* Ignore IoRestriction field */
594 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
596 * When ACPI GPIO mapping table is in use the index parameter inside it
597 * refers to the GPIO resource in _CRS method. That index has no
598 * distinction of actual type of the resource. When consumer wants to
599 * get GpioIo type explicitly, this quirk may be used.
601 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
602 /* Use given pin as an absolute GPIO number in the system */
603 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
608 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
610 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
611 const struct acpi_gpio_mapping *gpios);
612 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
614 int devm_acpi_dev_add_driver_gpios(struct device *dev,
615 const struct acpi_gpio_mapping *gpios);
617 struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin, char *label);
619 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
621 #include <linux/err.h>
623 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
624 const struct acpi_gpio_mapping *gpios)
628 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
630 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
631 const struct acpi_gpio_mapping *gpios)
636 static inline struct gpio_desc *acpi_get_and_request_gpiod(char *path, unsigned int pin,
639 return ERR_PTR(-ENOSYS);
642 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
645 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
647 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
648 int gpiod_export_link(struct device *dev, const char *name,
649 struct gpio_desc *desc);
650 void gpiod_unexport(struct gpio_desc *desc);
652 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
654 #include <asm/errno.h>
656 static inline int gpiod_export(struct gpio_desc *desc,
657 bool direction_may_change)
662 static inline int gpiod_export_link(struct device *dev, const char *name,
663 struct gpio_desc *desc)
668 static inline void gpiod_unexport(struct gpio_desc *desc)
672 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */