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