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