xref: /linux-6.15/include/linux/gpio/consumer.h (revision bb66fc67)
1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
3 
4 #include <linux/err.h>
5 #include <linux/kernel.h>
6 
7 struct device;
8 
9 /**
10  * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
11  * preferable to the old integer-based handles.
12  *
13  * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
14  * until the GPIO is released.
15  */
16 struct gpio_desc;
17 
18 #ifdef CONFIG_GPIOLIB
19 
20 /* Acquire and dispose GPIOs */
21 struct gpio_desc *__must_check gpiod_get(struct device *dev,
22 					 const char *con_id);
23 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
24 					       const char *con_id,
25 					       unsigned int idx);
26 void gpiod_put(struct gpio_desc *desc);
27 
28 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
29 					      const char *con_id);
30 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
31 						    const char *con_id,
32 						    unsigned int idx);
33 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
34 
35 int gpiod_get_direction(const struct gpio_desc *desc);
36 int gpiod_direction_input(struct gpio_desc *desc);
37 int gpiod_direction_output(struct gpio_desc *desc, int value);
38 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
39 
40 /* Value get/set from non-sleeping context */
41 int gpiod_get_value(const struct gpio_desc *desc);
42 void gpiod_set_value(struct gpio_desc *desc, int value);
43 int gpiod_get_raw_value(const struct gpio_desc *desc);
44 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
45 
46 /* Value get/set from sleeping context */
47 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
48 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
49 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
50 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
51 
52 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
53 
54 int gpiod_is_active_low(const struct gpio_desc *desc);
55 int gpiod_cansleep(const struct gpio_desc *desc);
56 
57 int gpiod_to_irq(const struct gpio_desc *desc);
58 
59 /* Convert between the old gpio_ and new gpiod_ interfaces */
60 struct gpio_desc *gpio_to_desc(unsigned gpio);
61 int desc_to_gpio(const struct gpio_desc *desc);
62 
63 #else /* CONFIG_GPIOLIB */
64 
65 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
66 						       const char *con_id)
67 {
68 	return ERR_PTR(-ENOSYS);
69 }
70 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
71 							     const char *con_id,
72 							     unsigned int idx)
73 {
74 	return ERR_PTR(-ENOSYS);
75 }
76 static inline void gpiod_put(struct gpio_desc *desc)
77 {
78 	might_sleep();
79 
80 	/* GPIO can never have been requested */
81 	WARN_ON(1);
82 }
83 
84 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
85 							    const char *con_id)
86 {
87 	return ERR_PTR(-ENOSYS);
88 }
89 static inline
90 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
91 						    const char *con_id,
92 						    unsigned int idx)
93 {
94 	return ERR_PTR(-ENOSYS);
95 }
96 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
97 {
98 	might_sleep();
99 
100 	/* GPIO can never have been requested */
101 	WARN_ON(1);
102 }
103 
104 
105 static inline int gpiod_get_direction(const struct gpio_desc *desc)
106 {
107 	/* GPIO can never have been requested */
108 	WARN_ON(1);
109 	return -ENOSYS;
110 }
111 static inline int gpiod_direction_input(struct gpio_desc *desc)
112 {
113 	/* GPIO can never have been requested */
114 	WARN_ON(1);
115 	return -ENOSYS;
116 }
117 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
118 {
119 	/* GPIO can never have been requested */
120 	WARN_ON(1);
121 	return -ENOSYS;
122 }
123 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
124 {
125 	/* GPIO can never have been requested */
126 	WARN_ON(1);
127 	return -ENOSYS;
128 }
129 
130 
131 static inline int gpiod_get_value(const struct gpio_desc *desc)
132 {
133 	/* GPIO can never have been requested */
134 	WARN_ON(1);
135 	return 0;
136 }
137 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
138 {
139 	/* GPIO can never have been requested */
140 	WARN_ON(1);
141 }
142 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
143 {
144 	/* GPIO can never have been requested */
145 	WARN_ON(1);
146 	return 0;
147 }
148 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
149 {
150 	/* GPIO can never have been requested */
151 	WARN_ON(1);
152 }
153 
154 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
155 {
156 	/* GPIO can never have been requested */
157 	WARN_ON(1);
158 	return 0;
159 }
160 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
161 {
162 	/* GPIO can never have been requested */
163 	WARN_ON(1);
164 }
165 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
166 {
167 	/* GPIO can never have been requested */
168 	WARN_ON(1);
169 	return 0;
170 }
171 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
172 						int value)
173 {
174 	/* GPIO can never have been requested */
175 	WARN_ON(1);
176 }
177 
178 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
179 {
180 	/* GPIO can never have been requested */
181 	WARN_ON(1);
182 	return -ENOSYS;
183 }
184 
185 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
186 {
187 	/* GPIO can never have been requested */
188 	WARN_ON(1);
189 	return 0;
190 }
191 static inline int gpiod_cansleep(const struct gpio_desc *desc)
192 {
193 	/* GPIO can never have been requested */
194 	WARN_ON(1);
195 	return 0;
196 }
197 
198 static inline int gpiod_to_irq(const struct gpio_desc *desc)
199 {
200 	/* GPIO can never have been requested */
201 	WARN_ON(1);
202 	return -EINVAL;
203 }
204 
205 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
206 {
207 	return ERR_PTR(-EINVAL);
208 }
209 static inline int desc_to_gpio(const struct gpio_desc *desc)
210 {
211 	/* GPIO can never have been requested */
212 	WARN_ON(1);
213 	return -EINVAL;
214 }
215 
216 
217 #endif /* CONFIG_GPIOLIB */
218 
219 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
220 
221 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
222 int gpiod_export_link(struct device *dev, const char *name,
223 		      struct gpio_desc *desc);
224 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
225 void gpiod_unexport(struct gpio_desc *desc);
226 
227 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
228 
229 static inline int gpiod_export(struct gpio_desc *desc,
230 			       bool direction_may_change)
231 {
232 	return -ENOSYS;
233 }
234 
235 static inline int gpiod_export_link(struct device *dev, const char *name,
236 				    struct gpio_desc *desc)
237 {
238 	return -ENOSYS;
239 }
240 
241 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
242 {
243 	return -ENOSYS;
244 }
245 
246 static inline void gpiod_unexport(struct gpio_desc *desc)
247 {
248 }
249 
250 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
251 
252 #endif
253