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