1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_GPIO_H
3 #define _ASM_GENERIC_GPIO_H
4 
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 
8 #ifdef CONFIG_GPIOLIB
9 
10 #include <linux/compiler.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/gpio/consumer.h>
13 
14 /* Platforms may implement their GPIO interface with library code,
15  * at a small performance cost for non-inlined operations and some
16  * extra memory (for code and for per-GPIO table entries).
17  *
18  * While the GPIO programming interface defines valid GPIO numbers
19  * to be in the range 0..MAX_INT, this library restricts them to the
20  * smaller range 0..ARCH_NR_GPIOS-1.
21  *
22  * ARCH_NR_GPIOS is somewhat arbitrary; it usually reflects the sum of
23  * builtin/SoC GPIOs plus a number of GPIOs on expanders; the latter is
24  * actually an estimate of a board-specific value.
25  */
26 
27 #ifndef ARCH_NR_GPIOS
28 #if defined(CONFIG_ARCH_NR_GPIO) && CONFIG_ARCH_NR_GPIO > 0
29 #define ARCH_NR_GPIOS CONFIG_ARCH_NR_GPIO
30 #else
31 #define ARCH_NR_GPIOS		512
32 #endif
33 #endif
34 
35 /*
36  * "valid" GPIO numbers are nonnegative and may be passed to
37  * setup routines like gpio_request().  only some valid numbers
38  * can successfully be requested and used.
39  *
40  * Invalid GPIO numbers are useful for indicating no-such-GPIO in
41  * platform data and other tables.
42  */
43 
gpio_is_valid(int number)44 static inline bool gpio_is_valid(int number)
45 {
46 	return number >= 0 && number < ARCH_NR_GPIOS;
47 }
48 
49 struct device;
50 struct gpio;
51 struct seq_file;
52 struct module;
53 struct device_node;
54 struct gpio_desc;
55 
56 /* caller holds gpio_lock *OR* gpio is marked as requested */
gpio_to_chip(unsigned gpio)57 static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
58 {
59 	return gpiod_to_chip(gpio_to_desc(gpio));
60 }
61 
62 /* Always use the library code for GPIO management calls,
63  * or when sleeping may be involved.
64  */
65 extern int gpio_request(unsigned gpio, const char *label);
66 extern void gpio_free(unsigned gpio);
67 
gpio_direction_input(unsigned gpio)68 static inline int gpio_direction_input(unsigned gpio)
69 {
70 	return gpiod_direction_input(gpio_to_desc(gpio));
71 }
gpio_direction_output(unsigned gpio,int value)72 static inline int gpio_direction_output(unsigned gpio, int value)
73 {
74 	return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
75 }
76 
gpio_set_debounce(unsigned gpio,unsigned debounce)77 static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)
78 {
79 	return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
80 }
81 
gpio_get_value_cansleep(unsigned gpio)82 static inline int gpio_get_value_cansleep(unsigned gpio)
83 {
84 	return gpiod_get_raw_value_cansleep(gpio_to_desc(gpio));
85 }
gpio_set_value_cansleep(unsigned gpio,int value)86 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
87 {
88 	return gpiod_set_raw_value_cansleep(gpio_to_desc(gpio), value);
89 }
90 
91 
92 /* A platform's <asm/gpio.h> code may want to inline the I/O calls when
93  * the GPIO is constant and refers to some always-present controller,
94  * giving direct access to chip registers and tight bitbanging loops.
95  */
__gpio_get_value(unsigned gpio)96 static inline int __gpio_get_value(unsigned gpio)
97 {
98 	return gpiod_get_raw_value(gpio_to_desc(gpio));
99 }
__gpio_set_value(unsigned gpio,int value)100 static inline void __gpio_set_value(unsigned gpio, int value)
101 {
102 	return gpiod_set_raw_value(gpio_to_desc(gpio), value);
103 }
104 
__gpio_cansleep(unsigned gpio)105 static inline int __gpio_cansleep(unsigned gpio)
106 {
107 	return gpiod_cansleep(gpio_to_desc(gpio));
108 }
109 
__gpio_to_irq(unsigned gpio)110 static inline int __gpio_to_irq(unsigned gpio)
111 {
112 	return gpiod_to_irq(gpio_to_desc(gpio));
113 }
114 
115 extern int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
116 extern int gpio_request_array(const struct gpio *array, size_t num);
117 extern void gpio_free_array(const struct gpio *array, size_t num);
118 
119 /*
120  * A sysfs interface can be exported by individual drivers if they want,
121  * but more typically is configured entirely from userspace.
122  */
gpio_export(unsigned gpio,bool direction_may_change)123 static inline int gpio_export(unsigned gpio, bool direction_may_change)
124 {
125 	return gpiod_export(gpio_to_desc(gpio), direction_may_change);
126 }
127 
gpio_export_link(struct device * dev,const char * name,unsigned gpio)128 static inline int gpio_export_link(struct device *dev, const char *name,
129 				   unsigned gpio)
130 {
131 	return gpiod_export_link(dev, name, gpio_to_desc(gpio));
132 }
133 
gpio_unexport(unsigned gpio)134 static inline void gpio_unexport(unsigned gpio)
135 {
136 	gpiod_unexport(gpio_to_desc(gpio));
137 }
138 
139 #else	/* !CONFIG_GPIOLIB */
140 
141 #include <linux/kernel.h>
142 
gpio_is_valid(int number)143 static inline bool gpio_is_valid(int number)
144 {
145 	/* only non-negative numbers are valid */
146 	return number >= 0;
147 }
148 
149 /* platforms that don't directly support access to GPIOs through I2C, SPI,
150  * or other blocking infrastructure can use these wrappers.
151  */
152 
gpio_cansleep(unsigned gpio)153 static inline int gpio_cansleep(unsigned gpio)
154 {
155 	return 0;
156 }
157 
gpio_get_value_cansleep(unsigned gpio)158 static inline int gpio_get_value_cansleep(unsigned gpio)
159 {
160 	might_sleep();
161 	return __gpio_get_value(gpio);
162 }
163 
gpio_set_value_cansleep(unsigned gpio,int value)164 static inline void gpio_set_value_cansleep(unsigned gpio, int value)
165 {
166 	might_sleep();
167 	__gpio_set_value(gpio, value);
168 }
169 
170 #endif /* !CONFIG_GPIOLIB */
171 
172 #endif /* _ASM_GENERIC_GPIO_H */
173