1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_GPIO_H
3 #define _ASM_GENERIC_GPIO_H
5 #include <linux/types.h>
6 #include <linux/errno.h>
10 #include <linux/compiler.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/gpio/consumer.h>
15 * Platforms may implement their GPIO interface with library code,
16 * at a small performance cost for non-inlined operations and some
17 * extra memory (for code and for per-GPIO table entries).
21 * At the end we want all GPIOs to be dynamically allocated from 0.
22 * However, some legacy drivers still perform fixed allocation.
23 * Until they are all fixed, leave 0-512 space for them.
25 #define GPIO_DYNAMIC_BASE 512
34 /* caller holds gpio_lock *OR* gpio is marked as requested */
35 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
37 return gpiod_to_chip(gpio_to_desc(gpio));
40 /* Always use the library code for GPIO management calls,
41 * or when sleeping may be involved.
43 extern int gpio_request(unsigned gpio, const char *label);
44 extern void gpio_free(unsigned gpio);
46 static inline int gpio_direction_input(unsigned gpio)
48 return gpiod_direction_input(gpio_to_desc(gpio));
50 static inline int gpio_direction_output(unsigned gpio, int value)
52 return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
55 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
57 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
60 static inline int gpio_get_value_cansleep(unsigned gpio)
62 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
64 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
66 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
70 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
71 * the GPIO is constant and refers to some always-present controller,
72 * giving direct access to chip registers and tight bitbanging loops.
74 static inline int __gpio_get_value(unsigned gpio)
76 return gpiod_get_raw_value(gpio_to_desc(gpio));
78 static inline void __gpio_set_value(unsigned gpio, int value)
80 return gpiod_set_raw_value(gpio_to_desc(gpio), value);
83 static inline int __gpio_cansleep(unsigned gpio)
85 return gpiod_cansleep(gpio_to_desc(gpio));
88 static inline int __gpio_to_irq(unsigned gpio)
90 return gpiod_to_irq(gpio_to_desc(gpio));
93 extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
94 extern int gpio_request_array(const struct gpio *array, size_t num);
95 extern void gpio_free_array(const struct gpio *array, size_t num);
98 * A sysfs interface can be exported by individual drivers if they want,
99 * but more typically is configured entirely from userspace.
101 static inline int gpio_export(unsigned gpio, bool direction_may_change)
103 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
106 static inline int gpio_export_link(struct device *dev, const char *name,
109 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
112 static inline void gpio_unexport(unsigned gpio)
114 gpiod_unexport(gpio_to_desc(gpio));
117 #else /* !CONFIG_GPIOLIB */
119 #include <linux/kernel.h>
121 /* platforms that don't directly support access to GPIOs through I2C, SPI,
122 * or other blocking infrastructure can use these wrappers.
125 static inline int gpio_cansleep(unsigned gpio)
130 static inline int gpio_get_value_cansleep(unsigned gpio)
133 return __gpio_get_value(gpio);
136 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
139 __gpio_set_value(gpio, value);
142 #endif /* !CONFIG_GPIOLIB */
145 * "valid" GPIO numbers are nonnegative and may be passed to
146 * setup routines like gpio_request(). only some valid numbers
147 * can successfully be requested and used.
149 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
150 * platform data and other tables.
153 static inline bool gpio_is_valid(int number)
155 /* only non-negative numbers are valid */
159 #endif /* _ASM_GENERIC_GPIO_H */