Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
56a46b61 LW |
2 | /* |
3 | * <linux/gpio.h> | |
4 | * | |
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. | |
8 | * | |
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> | |
11 | */ | |
7560fa60 DB |
12 | #ifndef __LINUX_GPIO_H |
13 | #define __LINUX_GPIO_H | |
14 | ||
eccb7a00 | 15 | #include <linux/types.h> |
7563bbf8 | 16 | |
380c7ba3 AS |
17 | struct device; |
18 | ||
60a86668 | 19 | /* see Documentation/driver-api/gpio/legacy.rst */ |
7560fa60 | 20 | |
c001fb72 RD |
21 | /* make these flag values available regardless of GPIO kconfig options */ |
22 | #define GPIOF_DIR_OUT (0 << 0) | |
23 | #define GPIOF_DIR_IN (1 << 0) | |
24 | ||
25 | #define GPIOF_INIT_LOW (0 << 1) | |
26 | #define GPIOF_INIT_HIGH (1 << 1) | |
27 | ||
28 | #define GPIOF_IN (GPIOF_DIR_IN) | |
29 | #define GPIOF_OUT_INIT_LOW (GPIOF_DIR_OUT | GPIOF_INIT_LOW) | |
30 | #define GPIOF_OUT_INIT_HIGH (GPIOF_DIR_OUT | GPIOF_INIT_HIGH) | |
31 | ||
79a9becd AC |
32 | /* Gpio pin is active-low */ |
33 | #define GPIOF_ACTIVE_LOW (1 << 2) | |
34 | ||
feb83699 MB |
35 | /** |
36 | * struct gpio - a structure describing a GPIO with configuration | |
37 | * @gpio: the GPIO number | |
38 | * @flags: GPIO configuration as specified by GPIOF_* | |
39 | * @label: a literal description string of this GPIO | |
40 | */ | |
41 | struct gpio { | |
42 | unsigned gpio; | |
43 | unsigned long flags; | |
44 | const char *label; | |
45 | }; | |
46 | ||
76ec9d18 | 47 | #ifdef CONFIG_GPIOLIB |
7563bbf8 | 48 | |
eccb7a00 AB |
49 | #include <linux/gpio/consumer.h> |
50 | ||
51 | /* | |
52 | * "valid" GPIO numbers are nonnegative and may be passed to | |
53 | * setup routines like gpio_request(). Only some valid numbers | |
54 | * can successfully be requested and used. | |
55 | * | |
56 | * Invalid GPIO numbers are useful for indicating no-such-GPIO in | |
57 | * platform data and other tables. | |
58 | */ | |
59 | static inline bool gpio_is_valid(int number) | |
7563bbf8 | 60 | { |
eccb7a00 AB |
61 | /* only non-negative numbers are valid */ |
62 | return number >= 0; | |
7563bbf8 MB |
63 | } |
64 | ||
eccb7a00 AB |
65 | /* |
66 | * Platforms may implement their GPIO interface with library code, | |
67 | * at a small performance cost for non-inlined operations and some | |
68 | * extra memory (for code and for per-GPIO table entries). | |
69 | */ | |
70 | ||
71 | /* | |
72 | * At the end we want all GPIOs to be dynamically allocated from 0. | |
73 | * However, some legacy drivers still perform fixed allocation. | |
74 | * Until they are all fixed, leave 0-512 space for them. | |
75 | */ | |
76 | #define GPIO_DYNAMIC_BASE 512 | |
77 | ||
78 | /* Always use the library code for GPIO management calls, | |
79 | * or when sleeping may be involved. | |
80 | */ | |
81 | int gpio_request(unsigned gpio, const char *label); | |
82 | void gpio_free(unsigned gpio); | |
83 | ||
84 | static inline int gpio_direction_input(unsigned gpio) | |
85 | { | |
86 | return gpiod_direction_input(gpio_to_desc(gpio)); | |
87 | } | |
88 | static inline int gpio_direction_output(unsigned gpio, int value) | |
7563bbf8 | 89 | { |
eccb7a00 | 90 | return gpiod_direction_output_raw(gpio_to_desc(gpio), value); |
7563bbf8 MB |
91 | } |
92 | ||
eccb7a00 AB |
93 | static inline int gpio_get_value_cansleep(unsigned gpio) |
94 | { | |
95 | return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio)); | |
96 | } | |
97 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) | |
7563bbf8 | 98 | { |
eccb7a00 AB |
99 | return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value); |
100 | } | |
101 | ||
102 | static inline int gpio_get_value(unsigned gpio) | |
103 | { | |
104 | return gpiod_get_raw_value(gpio_to_desc(gpio)); | |
105 | } | |
106 | static inline void gpio_set_value(unsigned gpio, int value) | |
107 | { | |
108 | return gpiod_set_raw_value(gpio_to_desc(gpio), value); | |
109 | } | |
110 | ||
eccb7a00 AB |
111 | static inline int gpio_to_irq(unsigned gpio) |
112 | { | |
113 | return gpiod_to_irq(gpio_to_desc(gpio)); | |
114 | } | |
115 | ||
116 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label); | |
117 | int gpio_request_array(const struct gpio *array, size_t num); | |
118 | void gpio_free_array(const struct gpio *array, size_t num); | |
119 | ||
403c1d0b LW |
120 | /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */ |
121 | ||
403c1d0b LW |
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); | |
403c1d0b | 125 | |
76ec9d18 | 126 | #else /* ! CONFIG_GPIOLIB */ |
7560fa60 | 127 | |
3d599d1c | 128 | #include <linux/kernel.h> |
6ea0205b | 129 | |
380c7ba3 AS |
130 | #include <asm/bug.h> |
131 | #include <asm/errno.h> | |
a4177ee7 | 132 | |
3474cb3c | 133 | static inline bool gpio_is_valid(int number) |
7560fa60 | 134 | { |
3474cb3c | 135 | return false; |
7560fa60 DB |
136 | } |
137 | ||
d8a3515e | 138 | static inline int gpio_request(unsigned gpio, const char *label) |
7560fa60 DB |
139 | { |
140 | return -ENOSYS; | |
141 | } | |
142 | ||
323b7fe8 | 143 | static inline int gpio_request_one(unsigned gpio, |
5f829e40 WS |
144 | unsigned long flags, const char *label) |
145 | { | |
146 | return -ENOSYS; | |
147 | } | |
148 | ||
7c295975 | 149 | static inline int gpio_request_array(const struct gpio *array, size_t num) |
5f829e40 WS |
150 | { |
151 | return -ENOSYS; | |
152 | } | |
153 | ||
7560fa60 DB |
154 | static inline void gpio_free(unsigned gpio) |
155 | { | |
3d599d1c UKK |
156 | might_sleep(); |
157 | ||
7560fa60 | 158 | /* GPIO can never have been requested */ |
2c96922a MB |
159 | WARN_ON(1); |
160 | } | |
161 | ||
7c295975 | 162 | static inline void gpio_free_array(const struct gpio *array, size_t num) |
5f829e40 WS |
163 | { |
164 | might_sleep(); | |
165 | ||
166 | /* GPIO can never have been requested */ | |
167 | WARN_ON(1); | |
168 | } | |
169 | ||
d8a3515e | 170 | static inline int gpio_direction_input(unsigned gpio) |
7560fa60 DB |
171 | { |
172 | return -ENOSYS; | |
173 | } | |
174 | ||
d8a3515e | 175 | static inline int gpio_direction_output(unsigned gpio, int value) |
7560fa60 DB |
176 | { |
177 | return -ENOSYS; | |
178 | } | |
179 | ||
180 | static inline int gpio_get_value(unsigned gpio) | |
181 | { | |
182 | /* GPIO can never have been requested or set as {in,out}put */ | |
183 | WARN_ON(1); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | static inline void gpio_set_value(unsigned gpio, int value) | |
188 | { | |
189 | /* GPIO can never have been requested or set as output */ | |
190 | WARN_ON(1); | |
191 | } | |
192 | ||
7560fa60 DB |
193 | static inline int gpio_get_value_cansleep(unsigned gpio) |
194 | { | |
195 | /* GPIO can never have been requested or set as {in,out}put */ | |
196 | WARN_ON(1); | |
197 | return 0; | |
198 | } | |
199 | ||
200 | static inline void gpio_set_value_cansleep(unsigned gpio, int value) | |
201 | { | |
202 | /* GPIO can never have been requested or set as output */ | |
203 | WARN_ON(1); | |
204 | } | |
205 | ||
206 | static inline int gpio_to_irq(unsigned gpio) | |
207 | { | |
208 | /* GPIO can never have been requested or set as input */ | |
209 | WARN_ON(1); | |
210 | return -EINVAL; | |
211 | } | |
212 | ||
403c1d0b LW |
213 | static inline int devm_gpio_request(struct device *dev, unsigned gpio, |
214 | const char *label) | |
215 | { | |
216 | WARN_ON(1); | |
217 | return -EINVAL; | |
218 | } | |
7560fa60 | 219 | |
403c1d0b LW |
220 | static inline int devm_gpio_request_one(struct device *dev, unsigned gpio, |
221 | unsigned long flags, const char *label) | |
222 | { | |
223 | WARN_ON(1); | |
224 | return -EINVAL; | |
225 | } | |
6a89a314 | 226 | |
403c1d0b | 227 | #endif /* ! CONFIG_GPIOLIB */ |
6a89a314 | 228 | |
7560fa60 | 229 | #endif /* __LINUX_GPIO_H */ |