xref: /openbmc/linux/include/linux/gpio.h (revision 8e7a49e0)
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/errno.h>
16 
17 /* see Documentation/driver-api/gpio/legacy.rst */
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 /* Gpio pin is open drain */
34 #define GPIOF_OPEN_DRAIN	(1 << 3)
35 
36 /* Gpio pin is open source */
37 #define GPIOF_OPEN_SOURCE	(1 << 4)
38 
39 #define GPIOF_EXPORT		(1 << 5)
40 #define GPIOF_EXPORT_CHANGEABLE	(1 << 6)
41 #define GPIOF_EXPORT_DIR_FIXED	(GPIOF_EXPORT)
42 #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
43 
44 /**
45  * struct gpio - a structure describing a GPIO with configuration
46  * @gpio:	the GPIO number
47  * @flags:	GPIO configuration as specified by GPIOF_*
48  * @label:	a literal description string of this GPIO
49  */
50 struct gpio {
51 	unsigned	gpio;
52 	unsigned long	flags;
53 	const char	*label;
54 };
55 
56 #ifdef CONFIG_GPIOLIB
57 
58 #ifdef CONFIG_ARCH_HAVE_CUSTOM_GPIO_H
59 #include <asm/gpio.h>
60 #else
61 
62 #include <asm-generic/gpio.h>
63 
64 static inline int gpio_get_value(unsigned int gpio)
65 {
66 	return __gpio_get_value(gpio);
67 }
68 
69 static inline void gpio_set_value(unsigned int gpio, int value)
70 {
71 	__gpio_set_value(gpio, value);
72 }
73 
74 static inline int gpio_cansleep(unsigned int gpio)
75 {
76 	return __gpio_cansleep(gpio);
77 }
78 
79 static inline int gpio_to_irq(unsigned int gpio)
80 {
81 	return __gpio_to_irq(gpio);
82 }
83 
84 #endif /* ! CONFIG_ARCH_HAVE_CUSTOM_GPIO_H */
85 
86 /* CONFIG_GPIOLIB: bindings for managed devices that want to request gpios */
87 
88 struct device;
89 
90 int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
91 int devm_gpio_request_one(struct device *dev, unsigned gpio,
92 			  unsigned long flags, const char *label);
93 
94 #else /* ! CONFIG_GPIOLIB */
95 
96 #include <linux/bug.h>
97 #include <linux/kernel.h>
98 #include <linux/types.h>
99 
100 struct device;
101 struct gpio_chip;
102 
103 static inline bool gpio_is_valid(int number)
104 {
105 	return false;
106 }
107 
108 static inline int gpio_request(unsigned gpio, const char *label)
109 {
110 	return -ENOSYS;
111 }
112 
113 static inline int gpio_request_one(unsigned gpio,
114 					unsigned long flags, const char *label)
115 {
116 	return -ENOSYS;
117 }
118 
119 static inline int gpio_request_array(const struct gpio *array, size_t num)
120 {
121 	return -ENOSYS;
122 }
123 
124 static inline void gpio_free(unsigned gpio)
125 {
126 	might_sleep();
127 
128 	/* GPIO can never have been requested */
129 	WARN_ON(1);
130 }
131 
132 static inline void gpio_free_array(const struct gpio *array, size_t num)
133 {
134 	might_sleep();
135 
136 	/* GPIO can never have been requested */
137 	WARN_ON(1);
138 }
139 
140 static inline int gpio_direction_input(unsigned gpio)
141 {
142 	return -ENOSYS;
143 }
144 
145 static inline int gpio_direction_output(unsigned gpio, int value)
146 {
147 	return -ENOSYS;
148 }
149 
150 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
151 {
152 	return -ENOSYS;
153 }
154 
155 static inline int gpio_get_value(unsigned gpio)
156 {
157 	/* GPIO can never have been requested or set as {in,out}put */
158 	WARN_ON(1);
159 	return 0;
160 }
161 
162 static inline void gpio_set_value(unsigned gpio, int value)
163 {
164 	/* GPIO can never have been requested or set as output */
165 	WARN_ON(1);
166 }
167 
168 static inline int gpio_cansleep(unsigned gpio)
169 {
170 	/* GPIO can never have been requested or set as {in,out}put */
171 	WARN_ON(1);
172 	return 0;
173 }
174 
175 static inline int gpio_get_value_cansleep(unsigned gpio)
176 {
177 	/* GPIO can never have been requested or set as {in,out}put */
178 	WARN_ON(1);
179 	return 0;
180 }
181 
182 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
183 {
184 	/* GPIO can never have been requested or set as output */
185 	WARN_ON(1);
186 }
187 
188 static inline int gpio_export(unsigned gpio, bool direction_may_change)
189 {
190 	/* GPIO can never have been requested or set as {in,out}put */
191 	WARN_ON(1);
192 	return -EINVAL;
193 }
194 
195 static inline void gpio_unexport(unsigned gpio)
196 {
197 	/* GPIO can never have been exported */
198 	WARN_ON(1);
199 }
200 
201 static inline int gpio_to_irq(unsigned gpio)
202 {
203 	/* GPIO can never have been requested or set as input */
204 	WARN_ON(1);
205 	return -EINVAL;
206 }
207 
208 static inline int devm_gpio_request(struct device *dev, unsigned gpio,
209 				    const char *label)
210 {
211 	WARN_ON(1);
212 	return -EINVAL;
213 }
214 
215 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
216 					unsigned long flags, const char *label)
217 {
218 	WARN_ON(1);
219 	return -EINVAL;
220 }
221 
222 #endif /* ! CONFIG_GPIOLIB */
223 
224 #endif /* __LINUX_GPIO_H */
225