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/types.h>
17 * struct gpio_descs - Struct containing an array of descriptors that can be
18 * obtained using gpiod_get_array()
20 * @info: Pointer to the opaque gpio_array structure
21 * @ndescs: Number of held descriptors
22 * @desc: Array of pointers to GPIO descriptors
25 struct gpio_array *info;
27 struct gpio_desc *desc[];
30 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
31 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
32 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
33 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
34 /* GPIOD_FLAGS_BIT_NONEXCLUSIVE is DEPRECATED, don't use in new code. */
35 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
38 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to
39 * configure direction and output value. These values
42 * @GPIOD_ASIS: Don't change anything
43 * @GPIOD_IN: Set lines to input mode
44 * @GPIOD_OUT_LOW: Set lines to output and drive them low
45 * @GPIOD_OUT_HIGH: Set lines to output and drive them high
46 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low
47 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high
51 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
52 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
53 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
54 GPIOD_FLAGS_BIT_DIR_VAL,
55 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
56 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
61 /* Return the number of GPIOs associated with a device / function */
62 int gpiod_count(struct device *dev, const char *con_id);
64 /* Acquire and dispose GPIOs */
65 struct gpio_desc *__must_check gpiod_get(struct device *dev,
67 enum gpiod_flags flags);
68 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
71 enum gpiod_flags flags);
72 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
74 enum gpiod_flags flags);
75 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
78 enum gpiod_flags flags);
79 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
81 enum gpiod_flags flags);
82 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
84 enum gpiod_flags flags);
85 void gpiod_put(struct gpio_desc *desc);
86 void gpiod_put_array(struct gpio_descs *descs);
88 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
90 enum gpiod_flags flags);
91 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
94 enum gpiod_flags flags);
95 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
97 enum gpiod_flags flags);
98 struct gpio_desc *__must_check
99 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
100 unsigned int index, enum gpiod_flags flags);
101 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
103 enum gpiod_flags flags);
104 struct gpio_descs *__must_check
105 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
106 enum gpiod_flags flags);
107 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
108 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
109 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
111 int gpiod_get_direction(struct gpio_desc *desc);
112 int gpiod_direction_input(struct gpio_desc *desc);
113 int gpiod_direction_output(struct gpio_desc *desc, int value);
114 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
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 int 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 int 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 int 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 int 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 void gpiod_toggle_active_low(struct gpio_desc *desc);
164 int gpiod_is_active_low(const struct gpio_desc *desc);
165 int gpiod_cansleep(const struct gpio_desc *desc);
167 int gpiod_to_irq(const struct gpio_desc *desc);
168 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
170 /* Convert between the old gpio_ and new gpiod_ interfaces */
171 struct gpio_desc *gpio_to_desc(unsigned gpio);
172 int desc_to_gpio(const struct gpio_desc *desc);
174 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
175 const char *con_id, int index,
176 enum gpiod_flags flags,
178 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
179 struct fwnode_handle *child,
180 const char *con_id, int index,
181 enum gpiod_flags flags,
184 bool gpiod_is_equal(struct gpio_desc *desc, struct gpio_desc *other);
186 #else /* CONFIG_GPIOLIB */
188 #include <linux/bug.h>
189 #include <linux/kernel.h>
191 static inline int gpiod_count(struct device *dev, const char *con_id)
196 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
198 enum gpiod_flags flags)
200 return ERR_PTR(-ENOSYS);
202 static inline struct gpio_desc *__must_check
203 gpiod_get_index(struct device *dev,
206 enum gpiod_flags flags)
208 return ERR_PTR(-ENOSYS);
211 static inline struct gpio_desc *__must_check
212 gpiod_get_optional(struct device *dev, const char *con_id,
213 enum gpiod_flags flags)
218 static inline struct gpio_desc *__must_check
219 gpiod_get_index_optional(struct device *dev, const char *con_id,
220 unsigned int index, enum gpiod_flags flags)
225 static inline struct gpio_descs *__must_check
226 gpiod_get_array(struct device *dev, const char *con_id,
227 enum gpiod_flags flags)
229 return ERR_PTR(-ENOSYS);
232 static inline struct gpio_descs *__must_check
233 gpiod_get_array_optional(struct device *dev, const char *con_id,
234 enum gpiod_flags flags)
239 static inline void gpiod_put(struct gpio_desc *desc)
243 /* GPIO can never have been requested */
247 static inline void devm_gpiod_unhinge(struct device *dev,
248 struct gpio_desc *desc)
252 /* GPIO can never have been requested */
256 static inline void gpiod_put_array(struct gpio_descs *descs)
260 /* GPIO can never have been requested */
264 static inline struct gpio_desc *__must_check
265 devm_gpiod_get(struct device *dev,
267 enum gpiod_flags flags)
269 return ERR_PTR(-ENOSYS);
272 struct gpio_desc *__must_check
273 devm_gpiod_get_index(struct device *dev,
276 enum gpiod_flags flags)
278 return ERR_PTR(-ENOSYS);
281 static inline struct gpio_desc *__must_check
282 devm_gpiod_get_optional(struct device *dev, const char *con_id,
283 enum gpiod_flags flags)
288 static inline struct gpio_desc *__must_check
289 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
290 unsigned int index, enum gpiod_flags flags)
295 static inline struct gpio_descs *__must_check
296 devm_gpiod_get_array(struct device *dev, const char *con_id,
297 enum gpiod_flags flags)
299 return ERR_PTR(-ENOSYS);
302 static inline struct gpio_descs *__must_check
303 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
304 enum gpiod_flags flags)
309 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
313 /* GPIO can never have been requested */
317 static inline void devm_gpiod_put_array(struct device *dev,
318 struct gpio_descs *descs)
322 /* GPIO can never have been requested */
327 static inline int gpiod_get_direction(const struct gpio_desc *desc)
329 /* GPIO can never have been requested */
333 static inline int gpiod_direction_input(struct gpio_desc *desc)
335 /* GPIO can never have been requested */
339 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
341 /* GPIO can never have been requested */
345 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
347 /* GPIO can never have been requested */
351 static inline int gpiod_get_value(const struct gpio_desc *desc)
353 /* GPIO can never have been requested */
357 static inline int gpiod_get_array_value(unsigned int array_size,
358 struct gpio_desc **desc_array,
359 struct gpio_array *array_info,
360 unsigned long *value_bitmap)
362 /* GPIO can never have been requested */
366 static inline int gpiod_set_value(struct gpio_desc *desc, int value)
368 /* GPIO can never have been requested */
372 static inline int gpiod_set_array_value(unsigned int array_size,
373 struct gpio_desc **desc_array,
374 struct gpio_array *array_info,
375 unsigned long *value_bitmap)
377 /* GPIO can never have been requested */
381 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
383 /* GPIO can never have been requested */
387 static inline int gpiod_get_raw_array_value(unsigned int array_size,
388 struct gpio_desc **desc_array,
389 struct gpio_array *array_info,
390 unsigned long *value_bitmap)
392 /* GPIO can never have been requested */
396 static inline int gpiod_set_raw_value(struct gpio_desc *desc, int value)
398 /* GPIO can never have been requested */
402 static inline int gpiod_set_raw_array_value(unsigned int array_size,
403 struct gpio_desc **desc_array,
404 struct gpio_array *array_info,
405 unsigned long *value_bitmap)
407 /* GPIO can never have been requested */
412 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
414 /* GPIO can never have been requested */
418 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
419 struct gpio_desc **desc_array,
420 struct gpio_array *array_info,
421 unsigned long *value_bitmap)
423 /* GPIO can never have been requested */
427 static inline int gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
429 /* 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 int gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
460 /* 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 void gpiod_toggle_active_low(struct gpio_desc *desc)
490 /* GPIO can never have been requested */
494 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
496 /* GPIO can never have been requested */
500 static inline int gpiod_cansleep(const struct gpio_desc *desc)
502 /* GPIO can never have been requested */
507 static inline int gpiod_to_irq(const struct gpio_desc *desc)
509 /* GPIO can never have been requested */
514 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
517 /* GPIO can never have been requested */
522 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
527 static inline int desc_to_gpio(const struct gpio_desc *desc)
529 /* GPIO can never have been requested */
535 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
536 const char *con_id, int index,
537 enum gpiod_flags flags,
540 return ERR_PTR(-ENOSYS);
544 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
545 struct fwnode_handle *fwnode,
546 const char *con_id, int index,
547 enum gpiod_flags flags,
550 return ERR_PTR(-ENOSYS);
554 gpiod_is_equal(struct gpio_desc *desc, struct gpio_desc *other)
556 WARN_ON(desc || other);
560 #endif /* CONFIG_GPIOLIB */
562 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE)
563 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
564 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags);
567 #include <linux/bug.h>
569 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
572 if (!IS_ENABLED(CONFIG_GPIOLIB))
577 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
580 if (!IS_ENABLED(CONFIG_GPIOLIB))
585 #endif /* CONFIG_GPIOLIB && CONFIG_HTE */
588 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
589 struct fwnode_handle *fwnode,
591 enum gpiod_flags flags,
594 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
598 struct acpi_gpio_params {
599 unsigned int crs_entry_index;
600 unsigned short line_index;
604 struct acpi_gpio_mapping {
606 const struct acpi_gpio_params *data;
609 /* Ignore IoRestriction field */
610 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
612 * When ACPI GPIO mapping table is in use the index parameter inside it
613 * refers to the GPIO resource in _CRS method. That index has no
614 * distinction of actual type of the resource. When consumer wants to
615 * get GpioIo type explicitly, this quirk may be used.
617 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
618 /* Use given pin as an absolute GPIO number in the system */
619 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
624 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
626 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
627 const struct acpi_gpio_mapping *gpios);
628 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
630 int devm_acpi_dev_add_driver_gpios(struct device *dev,
631 const struct acpi_gpio_mapping *gpios);
633 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
635 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
636 const struct acpi_gpio_mapping *gpios)
640 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
642 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
643 const struct acpi_gpio_mapping *gpios)
648 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
651 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
653 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
654 int gpiod_export_link(struct device *dev, const char *name,
655 struct gpio_desc *desc);
656 void gpiod_unexport(struct gpio_desc *desc);
658 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
660 static inline int gpiod_export(struct gpio_desc *desc,
661 bool direction_may_change)
666 static inline int gpiod_export_link(struct device *dev, const char *name,
667 struct gpio_desc *desc)
672 static inline void gpiod_unexport(struct gpio_desc *desc)
676 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
678 static inline int gpiod_multi_set_value_cansleep(struct gpio_descs *descs,
679 unsigned long *value_bitmap)
681 if (IS_ERR_OR_NULL(descs))
682 return PTR_ERR_OR_ZERO(descs);
684 return gpiod_set_array_value_cansleep(descs->ndescs, descs->desc,
685 descs->info, value_bitmap);