Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / include / linux / gpio / consumer.h
blobdb2dfbae8edbd12059826183b1c0f73c7a58ff40
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>
8 struct acpi_device;
9 struct device;
10 struct fwnode_handle;
12 struct gpio_array;
13 struct gpio_desc;
15 /**
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
23 struct gpio_descs {
24 struct gpio_array *info;
25 unsigned int ndescs;
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)
35 /**
36 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to
37 * configure direction and output value. These values
38 * cannot be OR'd.
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
47 enum gpiod_flags {
48 GPIOD_ASIS = 0,
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,
57 #ifdef CONFIG_GPIOLIB
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,
64 const char *con_id,
65 enum gpiod_flags flags);
66 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
67 const char *con_id,
68 unsigned int idx,
69 enum gpiod_flags flags);
70 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
71 const char *con_id,
72 enum gpiod_flags flags);
73 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
74 const char *con_id,
75 unsigned int index,
76 enum gpiod_flags flags);
77 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
78 const char *con_id,
79 enum gpiod_flags flags);
80 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
81 const char *con_id,
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,
87 const char *con_id,
88 enum gpiod_flags flags);
89 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
90 const char *con_id,
91 unsigned int idx,
92 enum gpiod_flags flags);
93 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
94 const char *con_id,
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,
100 const char *con_id,
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 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,
177 const char *label);
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,
182 const char *label);
184 #else /* CONFIG_GPIOLIB */
186 #include <linux/err.h>
187 #include <linux/kernel.h>
189 #include <asm/bug.h>
191 static inline int gpiod_count(struct device *dev, const char *con_id)
193 return 0;
196 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
197 const char *con_id,
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,
204 const char *con_id,
205 unsigned int idx,
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)
215 return NULL;
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)
222 return NULL;
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)
236 return NULL;
239 static inline void gpiod_put(struct gpio_desc *desc)
241 might_sleep();
243 /* GPIO can never have been requested */
244 WARN_ON(desc);
247 static inline void devm_gpiod_unhinge(struct device *dev,
248 struct gpio_desc *desc)
250 might_sleep();
252 /* GPIO can never have been requested */
253 WARN_ON(desc);
256 static inline void gpiod_put_array(struct gpio_descs *descs)
258 might_sleep();
260 /* GPIO can never have been requested */
261 WARN_ON(descs);
264 static inline struct gpio_desc *__must_check
265 devm_gpiod_get(struct device *dev,
266 const char *con_id,
267 enum gpiod_flags flags)
269 return ERR_PTR(-ENOSYS);
271 static inline
272 struct gpio_desc *__must_check
273 devm_gpiod_get_index(struct device *dev,
274 const char *con_id,
275 unsigned int idx,
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)
285 return NULL;
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)
292 return NULL;
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)
306 return NULL;
309 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
311 might_sleep();
313 /* GPIO can never have been requested */
314 WARN_ON(desc);
317 static inline void devm_gpiod_put_array(struct device *dev,
318 struct gpio_descs *descs)
320 might_sleep();
322 /* GPIO can never have been requested */
323 WARN_ON(descs);
327 static inline int gpiod_get_direction(const struct gpio_desc *desc)
329 /* GPIO can never have been requested */
330 WARN_ON(desc);
331 return -ENOSYS;
333 static inline int gpiod_direction_input(struct gpio_desc *desc)
335 /* GPIO can never have been requested */
336 WARN_ON(desc);
337 return -ENOSYS;
339 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
341 /* GPIO can never have been requested */
342 WARN_ON(desc);
343 return -ENOSYS;
345 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
347 /* GPIO can never have been requested */
348 WARN_ON(desc);
349 return -ENOSYS;
351 static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc,
352 unsigned long flags)
354 WARN_ON(desc);
355 return -ENOSYS;
357 static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc,
358 unsigned long flags)
360 WARN_ON(desc);
361 return -ENOSYS;
363 static inline int gpiod_get_value(const struct gpio_desc *desc)
365 /* GPIO can never have been requested */
366 WARN_ON(desc);
367 return 0;
369 static inline int gpiod_get_array_value(unsigned int array_size,
370 struct gpio_desc **desc_array,
371 struct gpio_array *array_info,
372 unsigned long *value_bitmap)
374 /* GPIO can never have been requested */
375 WARN_ON(desc_array);
376 return 0;
378 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
380 /* GPIO can never have been requested */
381 WARN_ON(desc);
383 static inline int gpiod_set_array_value(unsigned int array_size,
384 struct gpio_desc **desc_array,
385 struct gpio_array *array_info,
386 unsigned long *value_bitmap)
388 /* GPIO can never have been requested */
389 WARN_ON(desc_array);
390 return 0;
392 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
394 /* GPIO can never have been requested */
395 WARN_ON(desc);
396 return 0;
398 static inline int gpiod_get_raw_array_value(unsigned int array_size,
399 struct gpio_desc **desc_array,
400 struct gpio_array *array_info,
401 unsigned long *value_bitmap)
403 /* GPIO can never have been requested */
404 WARN_ON(desc_array);
405 return 0;
407 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
409 /* GPIO can never have been requested */
410 WARN_ON(desc);
412 static inline int gpiod_set_raw_array_value(unsigned int array_size,
413 struct gpio_desc **desc_array,
414 struct gpio_array *array_info,
415 unsigned long *value_bitmap)
417 /* GPIO can never have been requested */
418 WARN_ON(desc_array);
419 return 0;
422 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
424 /* GPIO can never have been requested */
425 WARN_ON(desc);
426 return 0;
428 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
429 struct gpio_desc **desc_array,
430 struct gpio_array *array_info,
431 unsigned long *value_bitmap)
433 /* GPIO can never have been requested */
434 WARN_ON(desc_array);
435 return 0;
437 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
439 /* GPIO can never have been requested */
440 WARN_ON(desc);
442 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
443 struct gpio_desc **desc_array,
444 struct gpio_array *array_info,
445 unsigned long *value_bitmap)
447 /* GPIO can never have been requested */
448 WARN_ON(desc_array);
449 return 0;
451 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
453 /* GPIO can never have been requested */
454 WARN_ON(desc);
455 return 0;
457 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
458 struct gpio_desc **desc_array,
459 struct gpio_array *array_info,
460 unsigned long *value_bitmap)
462 /* GPIO can never have been requested */
463 WARN_ON(desc_array);
464 return 0;
466 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
467 int value)
469 /* GPIO can never have been requested */
470 WARN_ON(desc);
472 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
473 struct gpio_desc **desc_array,
474 struct gpio_array *array_info,
475 unsigned long *value_bitmap)
477 /* GPIO can never have been requested */
478 WARN_ON(desc_array);
479 return 0;
482 static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
484 /* GPIO can never have been requested */
485 WARN_ON(desc);
486 return -ENOSYS;
489 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce)
491 /* GPIO can never have been requested */
492 WARN_ON(desc);
493 return -ENOSYS;
496 static inline void gpiod_toggle_active_low(struct gpio_desc *desc)
498 /* GPIO can never have been requested */
499 WARN_ON(desc);
502 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
504 /* GPIO can never have been requested */
505 WARN_ON(desc);
506 return 0;
508 static inline int gpiod_cansleep(const struct gpio_desc *desc)
510 /* GPIO can never have been requested */
511 WARN_ON(desc);
512 return 0;
515 static inline int gpiod_to_irq(const struct gpio_desc *desc)
517 /* GPIO can never have been requested */
518 WARN_ON(desc);
519 return -EINVAL;
522 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
523 const char *name)
525 /* GPIO can never have been requested */
526 WARN_ON(desc);
527 return -EINVAL;
530 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
532 return NULL;
535 static inline int desc_to_gpio(const struct gpio_desc *desc)
537 /* GPIO can never have been requested */
538 WARN_ON(desc);
539 return -EINVAL;
542 static inline
543 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode,
544 const char *con_id, int index,
545 enum gpiod_flags flags,
546 const char *label)
548 return ERR_PTR(-ENOSYS);
551 static inline
552 struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
553 struct fwnode_handle *fwnode,
554 const char *con_id, int index,
555 enum gpiod_flags flags,
556 const char *label)
558 return ERR_PTR(-ENOSYS);
561 #endif /* CONFIG_GPIOLIB */
563 static inline
564 struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
565 struct fwnode_handle *fwnode,
566 const char *con_id,
567 enum gpiod_flags flags,
568 const char *label)
570 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
571 flags, label);
574 struct acpi_gpio_params {
575 unsigned int crs_entry_index;
576 unsigned int line_index;
577 bool active_low;
580 struct acpi_gpio_mapping {
581 const char *name;
582 const struct acpi_gpio_params *data;
583 unsigned int size;
585 /* Ignore IoRestriction field */
586 #define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0)
588 * When ACPI GPIO mapping table is in use the index parameter inside it
589 * refers to the GPIO resource in _CRS method. That index has no
590 * distinction of actual type of the resource. When consumer wants to
591 * get GpioIo type explicitly, this quirk may be used.
593 #define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1)
594 /* Use given pin as an absolute GPIO number in the system */
595 #define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2)
597 unsigned int quirks;
600 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI)
602 int acpi_dev_add_driver_gpios(struct acpi_device *adev,
603 const struct acpi_gpio_mapping *gpios);
604 void acpi_dev_remove_driver_gpios(struct acpi_device *adev);
606 int devm_acpi_dev_add_driver_gpios(struct device *dev,
607 const struct acpi_gpio_mapping *gpios);
609 #else /* CONFIG_GPIOLIB && CONFIG_ACPI */
611 #include <linux/err.h>
613 static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev,
614 const struct acpi_gpio_mapping *gpios)
616 return -ENXIO;
618 static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {}
620 static inline int devm_acpi_dev_add_driver_gpios(struct device *dev,
621 const struct acpi_gpio_mapping *gpios)
623 return -ENXIO;
626 #endif /* CONFIG_GPIOLIB && CONFIG_ACPI */
629 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
631 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
632 int gpiod_export_link(struct device *dev, const char *name,
633 struct gpio_desc *desc);
634 void gpiod_unexport(struct gpio_desc *desc);
636 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
638 #include <asm/errno.h>
640 static inline int gpiod_export(struct gpio_desc *desc,
641 bool direction_may_change)
643 return -ENOSYS;
646 static inline int gpiod_export_link(struct device *dev, const char *name,
647 struct gpio_desc *desc)
649 return -ENOSYS;
652 static inline void gpiod_unexport(struct gpio_desc *desc)
656 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
658 #endif