1 /* SPDX-License-Identifier: GPL-2.0 */
3 * NOTE: This header *must not* be included.
5 * This is the LEGACY GPIO bulk include file, including legacy APIs. It is
6 * used for GPIO drivers still referencing the global GPIO numberspace,
7 * and should not be included in new code.
9 * If you're implementing a GPIO driver, only include <linux/gpio/driver.h>
10 * If you're implementing a GPIO consumer, only include <linux/gpio/consumer.h>
12 #ifndef __LINUX_GPIO_H
13 #define __LINUX_GPIO_H
15 #include <linux/types.h>
19 /* make these flag values available regardless of GPIO kconfig options */
20 #define GPIOF_DIR_OUT (0 << 0)
21 #define GPIOF_DIR_IN (1 << 0)
23 #define GPIOF_INIT_LOW (0 << 1)
24 #define GPIOF_INIT_HIGH (1 << 1)
26 #define GPIOF_IN (GPIOF_DIR_IN)
27 #define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW)
28 #define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
30 /* Gpio pin is active-low */
31 #define GPIOF_ACTIVE_LOW (1 << 2)
34 * struct gpio - a structure describing a GPIO with configuration
35 * @gpio: the GPIO number
36 * @flags: GPIO configuration as specified by GPIOF_*
37 * @label: a literal description string of this GPIO
47 #include <linux/gpio/consumer.h>
50 * "valid" GPIO numbers are nonnegative and may be passed to
51 * setup routines like gpio_request(). Only some valid numbers
52 * can successfully be requested and used.
54 * Invalid GPIO numbers are useful for indicating no-such-GPIO in
55 * platform data and other tables.
57 static inline bool gpio_is_valid(int number)
59 /* only non-negative numbers are valid */
64 * Platforms may implement their GPIO interface with library code,
65 * at a small performance cost for non-inlined operations and some
66 * extra memory (for code and for per-GPIO table entries).
70 * At the end we want all GPIOs to be dynamically allocated from 0.
71 * However, some legacy drivers still perform fixed allocation.
72 * Until they are all fixed, leave 0-512 space for them.
74 #define GPIO_DYNAMIC_BASE 512
76 * Define the maximum of the possible GPIO in the global numberspace.
77 * While the GPIO base and numbers are positive, we limit it with signed
78 * maximum as a lot of code is using negative values for special cases.
80 #define GPIO_DYNAMIC_MAX INT_MAX
82 /* Always use the library code for GPIO management calls,
83 * or when sleeping may be involved.
85 int gpio_request(unsigned gpio, const char *label);
86 void gpio_free(unsigned gpio);
88 static inline int gpio_direction_input(unsigned gpio)
90 return gpiod_direction_input(gpio_to_desc(gpio));
92 static inline int gpio_direction_output(unsigned gpio, int value)
94 return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
97 static inline int gpio_get_value_cansleep(unsigned gpio)
99 return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
101 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
103 return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
106 static inline int gpio_get_value(unsigned gpio)
108 return gpiod_get_raw_value(gpio_to_desc(gpio));
110 static inline void gpio_set_value(unsigned gpio, int value)
112 return gpiod_set_raw_value(gpio_to_desc(gpio), value);
115 static inline int gpio_to_irq(unsigned gpio)
117 return gpiod_to_irq(gpio_to_desc(gpio));
120 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
122 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
123 int devm_gpio_request_one(struct device *dev, unsigned gpio,
124 unsigned long flags, const char *label);
126 #else /* ! CONFIG_GPIOLIB */
128 #include <linux/kernel.h>
131 #include <asm/errno.h>
133 static inline bool gpio_is_valid(int number)
138 static inline int gpio_request(unsigned gpio, const char *label)
143 static inline int gpio_request_one(unsigned gpio,
144 unsigned long flags, const char *label)
149 static inline void gpio_free(unsigned gpio)
153 /* GPIO can never have been requested */
157 static inline int gpio_direction_input(unsigned gpio)
162 static inline int gpio_direction_output(unsigned gpio, int value)
167 static inline int gpio_get_value(unsigned gpio)
169 /* GPIO can never have been requested or set as {in,out}put */
174 static inline void gpio_set_value(unsigned gpio, int value)
176 /* GPIO can never have been requested or set as output */
180 static inline int gpio_get_value_cansleep(unsigned gpio)
182 /* GPIO can never have been requested or set as {in,out}put */
187 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
189 /* GPIO can never have been requested or set as output */
193 static inline int gpio_to_irq(unsigned gpio)
195 /* GPIO can never have been requested or set as input */
200 static inline int devm_gpio_request(struct device *dev, unsigned gpio,
207 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
208 unsigned long flags, const char *label)
214 #endif /* ! CONFIG_GPIOLIB */
216 #endif /* __LINUX_GPIO_H */