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