xref: /linux-6.15/include/linux/gpio.h (revision 7828b7bb)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * NOTE: This header *must not* be included.
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 /* make these flag values available regardless of GPIO kconfig options */
20 #define GPIOF_DIR_OUT	(0 << 0)
21 #define GPIOF_DIR_IN	(1 << 0)
22 
23 #define GPIOF_INIT_LOW	(0 << 1)
24 #define GPIOF_INIT_HIGH	(1 << 1)
25 
26 #define GPIOF_IN		(GPIOF_DIR_IN)
27 #define GPIOF_OUT_INIT_LOW	(GPIOF_DIR_OUT | GPIOF_INIT_LOW)
28 #define GPIOF_OUT_INIT_HIGH	(GPIOF_DIR_OUT | GPIOF_INIT_HIGH)
29 
30 /* Gpio pin is active-low */
31 #define GPIOF_ACTIVE_LOW        (1 << 2)
32 
33 /**
34  * struct gpio - a structure describing a GPIO with configuration
35  * @gpio:	the GPIO number
36  * @flags:	GPIO configuration as specified by GPIOF_*
37  * @label:	a literal description string of this GPIO
38  */
39 struct gpio {
40 	unsigned	gpio;
41 	unsigned long	flags;
42 	const char	*label;
43 };
44 
45 #ifdef CONFIG_GPIOLIB
46 
47 #include <linux/gpio/consumer.h>
48 
49 /*
50  * "valid" GPIO numbers are nonnegative and may be passed to
51  * setup routines like gpio_request().  Only some valid numbers
52  * can successfully be requested and used.
53  *
54  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
55  * platform data and other tables.
56  */
57 static inline bool gpio_is_valid(int number)
58 {
59 	/* only non-negative numbers are valid */
60 	return number >= 0;
61 }
62 
63 /*
64  * Platforms may implement their GPIO interface with library code,
65  * at a small performance cost for non-inlined operations and some
66  * extra memory (for code and for per-GPIO table entries).
67  */
68 
69 /*
70  * At the end we want all GPIOs to be dynamically allocated from 0.
71  * However, some legacy drivers still perform fixed allocation.
72  * Until they are all fixed, leave 0-512 space for them.
73  */
74 #define GPIO_DYNAMIC_BASE	512
75 /*
76  * Define the maximum of the possible GPIO in the global numberspace.
77  * While the GPIO base and numbers are positive, we limit it with signed
78  * maximum as a lot of code is using negative values for special cases.
79  */
80 #define GPIO_DYNAMIC_MAX	INT_MAX
81 
82 /* Always use the library code for GPIO management calls,
83  * or when sleeping may be involved.
84  */
85 int gpio_request(unsigned gpio, const char *label);
86 void gpio_free(unsigned gpio);
87 
88 static inline int gpio_direction_input(unsigned gpio)
89 {
90 	return gpiod_direction_input(gpio_to_desc(gpio));
91 }
92 static inline int gpio_direction_output(unsigned gpio, int value)
93 {
94 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
95 }
96 
97 static inline int gpio_get_value_cansleep(unsigned gpio)
98 {
99 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
100 }
101 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
102 {
103 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
104 }
105 
106 static inline int gpio_get_value(unsigned gpio)
107 {
108 	return gpiod_get_raw_value(gpio_to_desc(gpio));
109 }
110 static inline void gpio_set_value(unsigned gpio, int value)
111 {
112 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
113 }
114 
115 static inline int gpio_to_irq(unsigned gpio)
116 {
117 	return gpiod_to_irq(gpio_to_desc(gpio));
118 }
119 
120 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
121 
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);
125 
126 #else /* ! CONFIG_GPIOLIB */
127 
128 #include <linux/kernel.h>
129 
130 #include <asm/bug.h>
131 #include <asm/errno.h>
132 
133 static inline bool gpio_is_valid(int number)
134 {
135 	return false;
136 }
137 
138 static inline int gpio_request(unsigned gpio, const char *label)
139 {
140 	return -ENOSYS;
141 }
142 
143 static inline int gpio_request_one(unsigned gpio,
144 					unsigned long flags, const char *label)
145 {
146 	return -ENOSYS;
147 }
148 
149 static inline void gpio_free(unsigned gpio)
150 {
151 	might_sleep();
152 
153 	/* GPIO can never have been requested */
154 	WARN_ON(1);
155 }
156 
157 static inline int gpio_direction_input(unsigned gpio)
158 {
159 	return -ENOSYS;
160 }
161 
162 static inline int gpio_direction_output(unsigned gpio, int value)
163 {
164 	return -ENOSYS;
165 }
166 
167 static inline int gpio_get_value(unsigned gpio)
168 {
169 	/* GPIO can never have been requested or set as {in,out}put */
170 	WARN_ON(1);
171 	return 0;
172 }
173 
174 static inline void gpio_set_value(unsigned gpio, int value)
175 {
176 	/* GPIO can never have been requested or set as output */
177 	WARN_ON(1);
178 }
179 
180 static inline int gpio_get_value_cansleep(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_cansleep(unsigned gpio, int value)
188 {
189 	/* GPIO can never have been requested or set as output */
190 	WARN_ON(1);
191 }
192 
193 static inline int gpio_to_irq(unsigned gpio)
194 {
195 	/* GPIO can never have been requested or set as input */
196 	WARN_ON(1);
197 	return -EINVAL;
198 }
199 
200 static inline int devm_gpio_request(struct device *dev, unsigned gpio,
201 				    const char *label)
202 {
203 	WARN_ON(1);
204 	return -EINVAL;
205 }
206 
207 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
208 					unsigned long flags, const char *label)
209 {
210 	WARN_ON(1);
211 	return -EINVAL;
212 }
213 
214 #endif /* ! CONFIG_GPIOLIB */
215 
216 #endif /* __LINUX_GPIO_H */
217