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