xref: /linux/include/linux/gpio.h (revision fcb117e0758d1462128a50c5788555e03b48833b)
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 #ifdef CONFIG_GPIOLIB
17 #include <linux/gpio/consumer.h>
18 #endif
19 
20 #ifdef CONFIG_GPIOLIB_LEGACY
21 
22 struct device;
23 
24 /* make these flag values available regardless of GPIO kconfig options */
25 #define GPIOF_IN		((1 << 0))
26 #define GPIOF_OUT_INIT_LOW	((0 << 0) | (0 << 1))
27 #define GPIOF_OUT_INIT_HIGH	((0 << 0) | (1 << 1))
28 
29 #ifdef CONFIG_GPIOLIB
30 /*
31  * "valid" GPIO numbers are nonnegative and may be passed to
32  * setup routines like gpio_request().  Only some valid numbers
33  * can successfully be requested and used.
34  *
35  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
36  * platform data and other tables.
37  */
gpio_is_valid(int number)38 static inline bool gpio_is_valid(int number)
39 {
40 	/* only non-negative numbers are valid */
41 	return number >= 0;
42 }
43 
44 /*
45  * Platforms may implement their GPIO interface with library code,
46  * at a small performance cost for non-inlined operations and some
47  * extra memory (for code and for per-GPIO table entries).
48  */
49 
50 /* Always use the library code for GPIO management calls,
51  * or when sleeping may be involved.
52  */
53 int gpio_request(unsigned gpio, const char *label);
54 void gpio_free(unsigned gpio);
55 
gpio_direction_input(unsigned gpio)56 static inline int gpio_direction_input(unsigned gpio)
57 {
58 	return gpiod_direction_input(gpio_to_desc(gpio));
59 }
gpio_direction_output(unsigned gpio,int value)60 static inline int gpio_direction_output(unsigned gpio, int value)
61 {
62 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
63 }
64 
gpio_get_value_cansleep(unsigned gpio)65 static inline int gpio_get_value_cansleep(unsigned gpio)
66 {
67 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
68 }
gpio_set_value_cansleep(unsigned gpio,int value)69 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
70 {
71 	gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
72 }
73 
gpio_get_value(unsigned gpio)74 static inline int gpio_get_value(unsigned gpio)
75 {
76 	return gpiod_get_raw_value(gpio_to_desc(gpio));
77 }
gpio_set_value(unsigned gpio,int value)78 static inline void gpio_set_value(unsigned gpio, int value)
79 {
80 	gpiod_set_raw_value(gpio_to_desc(gpio), value);
81 }
82 
gpio_to_irq(unsigned gpio)83 static inline int gpio_to_irq(unsigned gpio)
84 {
85 	return gpiod_to_irq(gpio_to_desc(gpio));
86 }
87 
88 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
89 
90 int devm_gpio_request_one(struct device *dev, unsigned gpio,
91 			  unsigned long flags, const char *label);
92 
93 #else /* ! CONFIG_GPIOLIB */
94 
95 #include <linux/kernel.h>
96 
97 #include <asm/bug.h>
98 #include <asm/errno.h>
99 
gpio_is_valid(int number)100 static inline bool gpio_is_valid(int number)
101 {
102 	return false;
103 }
104 
gpio_request(unsigned gpio,const char * label)105 static inline int gpio_request(unsigned gpio, const char *label)
106 {
107 	return -ENOSYS;
108 }
109 
gpio_request_one(unsigned gpio,unsigned long flags,const char * label)110 static inline int gpio_request_one(unsigned gpio,
111 					unsigned long flags, const char *label)
112 {
113 	return -ENOSYS;
114 }
115 
gpio_free(unsigned gpio)116 static inline void gpio_free(unsigned gpio)
117 {
118 	might_sleep();
119 
120 	/* GPIO can never have been requested */
121 	WARN_ON(1);
122 }
123 
gpio_direction_input(unsigned gpio)124 static inline int gpio_direction_input(unsigned gpio)
125 {
126 	return -ENOSYS;
127 }
128 
gpio_direction_output(unsigned gpio,int value)129 static inline int gpio_direction_output(unsigned gpio, int value)
130 {
131 	return -ENOSYS;
132 }
133 
gpio_get_value(unsigned gpio)134 static inline int gpio_get_value(unsigned gpio)
135 {
136 	/* GPIO can never have been requested or set as {in,out}put */
137 	WARN_ON(1);
138 	return 0;
139 }
140 
gpio_set_value(unsigned gpio,int value)141 static inline void gpio_set_value(unsigned gpio, int value)
142 {
143 	/* GPIO can never have been requested or set as output */
144 	WARN_ON(1);
145 }
146 
gpio_get_value_cansleep(unsigned gpio)147 static inline int gpio_get_value_cansleep(unsigned gpio)
148 {
149 	/* GPIO can never have been requested or set as {in,out}put */
150 	WARN_ON(1);
151 	return 0;
152 }
153 
gpio_set_value_cansleep(unsigned gpio,int value)154 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
155 {
156 	/* GPIO can never have been requested or set as output */
157 	WARN_ON(1);
158 }
159 
gpio_to_irq(unsigned gpio)160 static inline int gpio_to_irq(unsigned gpio)
161 {
162 	/* GPIO can never have been requested or set as input */
163 	WARN_ON(1);
164 	return -EINVAL;
165 }
166 
devm_gpio_request_one(struct device * dev,unsigned gpio,unsigned long flags,const char * label)167 static inline int devm_gpio_request_one(struct device *dev, unsigned gpio,
168 					unsigned long flags, const char *label)
169 {
170 	WARN_ON(1);
171 	return -EINVAL;
172 }
173 
174 #endif /* ! CONFIG_GPIOLIB */
175 #endif /* CONFIG_GPIOLIB_LEGACY */
176 #endif /* __LINUX_GPIO_H */
177