xref: /linux-6.15/include/linux/gpio.h (revision dc6abbbd)
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 /**
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 
47 #ifdef CONFIG_GPIOLIB
48 
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)
60 {
61 	/* only non-negative numbers are valid */
62 	return number >= 0;
63 }
64 
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  * Define the maximum of the possible GPIO in the global numberspace.
79  * While the GPIO base and numbers are positive, we limit it with signed
80  * maximum as a lot of code is using negative values for special cases.
81  */
82 #define GPIO_DYNAMIC_MAX	INT_MAX
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_to_irq(unsigned gpio)
118 {
119 	return gpiod_to_irq(gpio_to_desc(gpio));
120 }
121 
122 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
123 
124 /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
125 
126 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
127 int devm_gpio_request_one(struct device *dev, unsigned gpio,
128 			  unsigned long flags, const char *label);
129 
130 #else /* ! CONFIG_GPIOLIB */
131 
132 #include <linux/kernel.h>
133 
134 #include <asm/bug.h>
135 #include <asm/errno.h>
136 
137 static inline bool gpio_is_valid(int number)
138 {
139 	return false;
140 }
141 
142 static inline int gpio_request(unsigned gpio, const char *label)
143 {
144 	return -ENOSYS;
145 }
146 
147 static inline int gpio_request_one(unsigned gpio,
148 					unsigned long flags, const char *label)
149 {
150 	return -ENOSYS;
151 }
152 
153 static inline void gpio_free(unsigned gpio)
154 {
155 	might_sleep();
156 
157 	/* GPIO can never have been requested */
158 	WARN_ON(1);
159 }
160 
161 static inline int gpio_direction_input(unsigned gpio)
162 {
163 	return -ENOSYS;
164 }
165 
166 static inline int gpio_direction_output(unsigned gpio, int value)
167 {
168 	return -ENOSYS;
169 }
170 
171 static inline int gpio_get_value(unsigned gpio)
172 {
173 	/* GPIO can never have been requested or set as {in,out}put */
174 	WARN_ON(1);
175 	return 0;
176 }
177 
178 static inline void gpio_set_value(unsigned gpio, int value)
179 {
180 	/* GPIO can never have been requested or set as output */
181 	WARN_ON(1);
182 }
183 
184 static inline int gpio_get_value_cansleep(unsigned gpio)
185 {
186 	/* GPIO can never have been requested or set as {in,out}put */
187 	WARN_ON(1);
188 	return 0;
189 }
190 
191 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
192 {
193 	/* GPIO can never have been requested or set as output */
194 	WARN_ON(1);
195 }
196 
197 static inline int gpio_to_irq(unsigned gpio)
198 {
199 	/* GPIO can never have been requested or set as input */
200 	WARN_ON(1);
201 	return -EINVAL;
202 }
203 
204 static inline int devm_gpio_request(struct device *dev, unsigned gpio,
205 				    const char *label)
206 {
207 	WARN_ON(1);
208 	return -EINVAL;
209 }
210 
211 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
212 					unsigned long flags, const char *label)
213 {
214 	WARN_ON(1);
215 	return -EINVAL;
216 }
217 
218 #endif /* ! CONFIG_GPIOLIB */
219 
220 #endif /* __LINUX_GPIO_H */
221