xref: /linux-6.15/include/linux/gpio.h (revision eccb7a00)
1 /* SPDX-License-Identifier: GPL-2.0 */
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  */
12 #ifndef __LINUX_GPIO_H
13 #define __LINUX_GPIO_H
14 
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 
18 /* see Documentation/driver-api/gpio/legacy.rst */
19 
20 /* make these flag values available regardless of GPIO kconfig options */
21 #define GPIOF_DIR_OUT	(0 << 0)
22 #define GPIOF_DIR_IN	(1 << 0)
23 
24 #define GPIOF_INIT_LOW	(0 << 1)
25 #define GPIOF_INIT_HIGH	(1 << 1)
26 
27 #define GPIOF_IN		(GPIOF_DIR_IN)
28 #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
29 #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
30 
31 /* Gpio pin is active-low */
32 #define GPIOF_ACTIVE_LOW        (1 << 2)
33 
34 /* Gpio pin is open drain */
35 #define GPIOF_OPEN_DRAIN	(1 << 3)
36 
37 /* Gpio pin is open source */
38 #define GPIOF_OPEN_SOURCE	(1 << 4)
39 
40 #define GPIOF_EXPORT		(1 << 5)
41 #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
42 #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
43 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
44 
45 /**
46  * struct gpio - a structure describing a GPIO with configuration
47  * @gpio:	the GPIO number
48  * @flags:	GPIO configuration as specified by GPIOF_*
49  * @label:	a literal description string of this GPIO
50  */
51 struct gpio {
52 	unsigned	gpio;
53 	unsigned long	flags;
54 	const char	*label;
55 };
56 
57 #ifdef CONFIG_GPIOLIB
58 
59 #include <linux/gpio/consumer.h>
60 
61 /*
62  * "valid" GPIO numbers are nonnegative and may be passed to
63  * setup routines like gpio_request().  Only some valid numbers
64  * can successfully be requested and used.
65  *
66  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
67  * platform data and other tables.
68  */
69 static inline bool gpio_is_valid(int number)
70 {
71 	/* only non-negative numbers are valid */
72 	return number >= 0;
73 }
74 
75 /*
76  * Platforms may implement their GPIO interface with library code,
77  * at a small performance cost for non-inlined operations and some
78  * extra memory (for code and for per-GPIO table entries).
79  */
80 
81 /*
82  * At the end we want all GPIOs to be dynamically allocated from 0.
83  * However, some legacy drivers still perform fixed allocation.
84  * Until they are all fixed, leave 0-512 space for them.
85  */
86 #define GPIO_DYNAMIC_BASE	512
87 
88 /* Always use the library code for GPIO management calls,
89  * or when sleeping may be involved.
90  */
91 int gpio_request(unsigned gpio, const char *label);
92 void gpio_free(unsigned gpio);
93 
94 static inline int gpio_direction_input(unsigned gpio)
95 {
96 	return gpiod_direction_input(gpio_to_desc(gpio));
97 }
98 static inline int gpio_direction_output(unsigned gpio, int value)
99 {
100 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
101 }
102 
103 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
104 {
105 	return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
106 }
107 
108 static inline int gpio_get_value_cansleep(unsigned gpio)
109 {
110 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
111 }
112 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
113 {
114 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
115 }
116 
117 static inline int gpio_get_value(unsigned gpio)
118 {
119 	return gpiod_get_raw_value(gpio_to_desc(gpio));
120 }
121 static inline void gpio_set_value(unsigned gpio, int value)
122 {
123 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
124 }
125 
126 static inline int gpio_cansleep(unsigned gpio)
127 {
128 	return gpiod_cansleep(gpio_to_desc(gpio));
129 }
130 
131 static inline int gpio_to_irq(unsigned gpio)
132 {
133 	return gpiod_to_irq(gpio_to_desc(gpio));
134 }
135 
136 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
137 int gpio_request_array(const struct gpio *array, size_t num);
138 void gpio_free_array(const struct gpio *array, size_t num);
139 
140 /*
141  * A sysfs interface can be exported by individual drivers if they want,
142  * but more typically is configured entirely from userspace.
143  */
144 static inline int gpio_export(unsigned gpio, bool direction_may_change)
145 {
146 	return gpiod_export(gpio_to_desc(gpio), direction_may_change);
147 }
148 
149 static inline void gpio_unexport(unsigned gpio)
150 {
151 	gpiod_unexport(gpio_to_desc(gpio));
152 }
153 
154 /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
155 
156 struct device;
157 
158 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
159 int devm_gpio_request_one(struct device *dev, unsigned gpio,
160 			  unsigned long flags, const char *label);
161 
162 #else /* ! CONFIG_GPIOLIB */
163 
164 #include <linux/bug.h>
165 #include <linux/kernel.h>
166 
167 struct device;
168 struct gpio_chip;
169 
170 static inline bool gpio_is_valid(int number)
171 {
172 	return false;
173 }
174 
175 static inline int gpio_request(unsigned gpio, const char *label)
176 {
177 	return -ENOSYS;
178 }
179 
180 static inline int gpio_request_one(unsigned gpio,
181 					unsigned long flags, const char *label)
182 {
183 	return -ENOSYS;
184 }
185 
186 static inline int gpio_request_array(const struct gpio *array, size_t num)
187 {
188 	return -ENOSYS;
189 }
190 
191 static inline void gpio_free(unsigned gpio)
192 {
193 	might_sleep();
194 
195 	/* GPIO can never have been requested */
196 	WARN_ON(1);
197 }
198 
199 static inline void gpio_free_array(const struct gpio *array, size_t num)
200 {
201 	might_sleep();
202 
203 	/* GPIO can never have been requested */
204 	WARN_ON(1);
205 }
206 
207 static inline int gpio_direction_input(unsigned gpio)
208 {
209 	return -ENOSYS;
210 }
211 
212 static inline int gpio_direction_output(unsigned gpio, int value)
213 {
214 	return -ENOSYS;
215 }
216 
217 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
218 {
219 	return -ENOSYS;
220 }
221 
222 static inline int gpio_get_value(unsigned gpio)
223 {
224 	/* GPIO can never have been requested or set as {in,out}put */
225 	WARN_ON(1);
226 	return 0;
227 }
228 
229 static inline void gpio_set_value(unsigned gpio, int value)
230 {
231 	/* GPIO can never have been requested or set as output */
232 	WARN_ON(1);
233 }
234 
235 static inline int gpio_cansleep(unsigned gpio)
236 {
237 	/* GPIO can never have been requested or set as {in,out}put */
238 	WARN_ON(1);
239 	return 0;
240 }
241 
242 static inline int gpio_get_value_cansleep(unsigned gpio)
243 {
244 	/* GPIO can never have been requested or set as {in,out}put */
245 	WARN_ON(1);
246 	return 0;
247 }
248 
249 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
250 {
251 	/* GPIO can never have been requested or set as output */
252 	WARN_ON(1);
253 }
254 
255 static inline int gpio_export(unsigned gpio, bool direction_may_change)
256 {
257 	/* GPIO can never have been requested or set as {in,out}put */
258 	WARN_ON(1);
259 	return -EINVAL;
260 }
261 
262 static inline void gpio_unexport(unsigned gpio)
263 {
264 	/* GPIO can never have been exported */
265 	WARN_ON(1);
266 }
267 
268 static inline int gpio_to_irq(unsigned gpio)
269 {
270 	/* GPIO can never have been requested or set as input */
271 	WARN_ON(1);
272 	return -EINVAL;
273 }
274 
275 static inline int devm_gpio_request(struct device *dev, unsigned gpio,
276 				    const char *label)
277 {
278 	WARN_ON(1);
279 	return -EINVAL;
280 }
281 
282 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
283 					unsigned long flags, const char *label)
284 {
285 	WARN_ON(1);
286 	return -EINVAL;
287 }
288 
289 #endif /* ! CONFIG_GPIOLIB */
290 
291 #endif /* __LINUX_GPIO_H */
292