]>
Commit | Line | Data |
---|---|---|
79a9becd AC |
1 | #ifndef __LINUX_GPIO_CONSUMER_H |
2 | #define __LINUX_GPIO_CONSUMER_H | |
3 | ||
cdf86cd2 | 4 | #include <linux/bug.h> |
79a9becd AC |
5 | #include <linux/err.h> |
6 | #include <linux/kernel.h> | |
7 | ||
79a9becd | 8 | struct device; |
79a9becd AC |
9 | |
10 | /** | |
11 | * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are | |
12 | * preferable to the old integer-based handles. | |
13 | * | |
14 | * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid | |
15 | * until the GPIO is released. | |
16 | */ | |
17 | struct gpio_desc; | |
18 | ||
66858527 RI |
19 | /** |
20 | * Struct containing an array of descriptors that can be obtained using | |
21 | * gpiod_get_array(). | |
22 | */ | |
23 | struct gpio_descs { | |
24 | unsigned int ndescs; | |
25 | struct gpio_desc *desc[]; | |
26 | }; | |
27 | ||
39b2bbe3 AC |
28 | #define GPIOD_FLAGS_BIT_DIR_SET BIT(0) |
29 | #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) | |
30 | #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) | |
31 | ||
32 | /** | |
33 | * Optional flags that can be passed to one of gpiod_* to configure direction | |
34 | * and output value. These values cannot be OR'd. | |
35 | */ | |
36 | enum gpiod_flags { | |
37 | GPIOD_ASIS = 0, | |
38 | GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, | |
39 | GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, | |
40 | GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | | |
41 | GPIOD_FLAGS_BIT_DIR_VAL, | |
42 | }; | |
43 | ||
58b84f6a LW |
44 | #ifdef CONFIG_GPIOLIB |
45 | ||
66858527 RI |
46 | /* Return the number of GPIOs associated with a device / function */ |
47 | int gpiod_count(struct device *dev, const char *con_id); | |
48 | ||
bae48da2 | 49 | /* Acquire and dispose GPIOs */ |
b17d1bf1 | 50 | struct gpio_desc *__must_check gpiod_get(struct device *dev, |
39b2bbe3 AC |
51 | const char *con_id, |
52 | enum gpiod_flags flags); | |
b17d1bf1 | 53 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, |
bae48da2 | 54 | const char *con_id, |
39b2bbe3 AC |
55 | unsigned int idx, |
56 | enum gpiod_flags flags); | |
b17d1bf1 | 57 | struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
58 | const char *con_id, |
59 | enum gpiod_flags flags); | |
b17d1bf1 | 60 | struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, |
29a1f233 | 61 | const char *con_id, |
39b2bbe3 AC |
62 | unsigned int index, |
63 | enum gpiod_flags flags); | |
66858527 RI |
64 | struct gpio_descs *__must_check gpiod_get_array(struct device *dev, |
65 | const char *con_id, | |
66 | enum gpiod_flags flags); | |
67 | struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, | |
68 | const char *con_id, | |
69 | enum gpiod_flags flags); | |
bae48da2 | 70 | void gpiod_put(struct gpio_desc *desc); |
66858527 | 71 | void gpiod_put_array(struct gpio_descs *descs); |
bae48da2 | 72 | |
b17d1bf1 | 73 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
39b2bbe3 AC |
74 | const char *con_id, |
75 | enum gpiod_flags flags); | |
b17d1bf1 | 76 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
bae48da2 | 77 | const char *con_id, |
39b2bbe3 AC |
78 | unsigned int idx, |
79 | enum gpiod_flags flags); | |
b17d1bf1 | 80 | struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, |
39b2bbe3 AC |
81 | const char *con_id, |
82 | enum gpiod_flags flags); | |
29a1f233 | 83 | struct gpio_desc *__must_check |
b17d1bf1 | 84 | devm_gpiod_get_index_optional(struct device *dev, const char *con_id, |
39b2bbe3 | 85 | unsigned int index, enum gpiod_flags flags); |
331758ee RI |
86 | struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, |
87 | const char *con_id, | |
88 | enum gpiod_flags flags); | |
89 | struct gpio_descs *__must_check | |
90 | devm_gpiod_get_array_optional(struct device *dev, const char *con_id, | |
91 | enum gpiod_flags flags); | |
bae48da2 | 92 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); |
331758ee | 93 | void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); |
bae48da2 | 94 | |
8e53b0f1 | 95 | int gpiod_get_direction(struct gpio_desc *desc); |
79a9becd AC |
96 | int gpiod_direction_input(struct gpio_desc *desc); |
97 | int gpiod_direction_output(struct gpio_desc *desc, int value); | |
ef70bbe1 | 98 | int gpiod_direction_output_raw(struct gpio_desc *desc, int value); |
79a9becd AC |
99 | |
100 | /* Value get/set from non-sleeping context */ | |
101 | int gpiod_get_value(const struct gpio_desc *desc); | |
102 | void gpiod_set_value(struct gpio_desc *desc, int value); | |
3fff99bc RI |
103 | void gpiod_set_array_value(unsigned int array_size, |
104 | struct gpio_desc **desc_array, int *value_array); | |
79a9becd AC |
105 | int gpiod_get_raw_value(const struct gpio_desc *desc); |
106 | void gpiod_set_raw_value(struct gpio_desc *desc, int value); | |
3fff99bc RI |
107 | void gpiod_set_raw_array_value(unsigned int array_size, |
108 | struct gpio_desc **desc_array, | |
109 | int *value_array); | |
79a9becd AC |
110 | |
111 | /* Value get/set from sleeping context */ | |
112 | int gpiod_get_value_cansleep(const struct gpio_desc *desc); | |
113 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); | |
3fff99bc RI |
114 | void gpiod_set_array_value_cansleep(unsigned int array_size, |
115 | struct gpio_desc **desc_array, | |
116 | int *value_array); | |
79a9becd AC |
117 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); |
118 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); | |
3fff99bc RI |
119 | void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
120 | struct gpio_desc **desc_array, | |
121 | int *value_array); | |
79a9becd AC |
122 | |
123 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); | |
124 | ||
125 | int gpiod_is_active_low(const struct gpio_desc *desc); | |
126 | int gpiod_cansleep(const struct gpio_desc *desc); | |
127 | ||
128 | int gpiod_to_irq(const struct gpio_desc *desc); | |
129 | ||
130 | /* Convert between the old gpio_ and new gpiod_ interfaces */ | |
131 | struct gpio_desc *gpio_to_desc(unsigned gpio); | |
132 | int desc_to_gpio(const struct gpio_desc *desc); | |
79a9becd | 133 | |
40b73183 MW |
134 | /* Child properties interface */ |
135 | struct fwnode_handle; | |
136 | ||
137 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
537b94da | 138 | const char *propname, int index, |
b2987d74 AS |
139 | enum gpiod_flags dflags, |
140 | const char *label); | |
537b94da BB |
141 | struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, |
142 | const char *con_id, int index, | |
143 | struct fwnode_handle *child, | |
144 | enum gpiod_flags flags, | |
145 | const char *label); | |
3498d869 | 146 | |
79a9becd AC |
147 | #else /* CONFIG_GPIOLIB */ |
148 | ||
66858527 RI |
149 | static inline int gpiod_count(struct device *dev, const char *con_id) |
150 | { | |
151 | return 0; | |
152 | } | |
153 | ||
b17d1bf1 UKK |
154 | static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, |
155 | const char *con_id, | |
156 | enum gpiod_flags flags) | |
79a9becd AC |
157 | { |
158 | return ERR_PTR(-ENOSYS); | |
159 | } | |
0dbc8b7a | 160 | static inline struct gpio_desc *__must_check |
b17d1bf1 UKK |
161 | gpiod_get_index(struct device *dev, |
162 | const char *con_id, | |
163 | unsigned int idx, | |
164 | enum gpiod_flags flags) | |
79a9becd AC |
165 | { |
166 | return ERR_PTR(-ENOSYS); | |
167 | } | |
29a1f233 TR |
168 | |
169 | static inline struct gpio_desc *__must_check | |
b17d1bf1 UKK |
170 | gpiod_get_optional(struct device *dev, const char *con_id, |
171 | enum gpiod_flags flags) | |
29a1f233 | 172 | { |
22c40367 | 173 | return NULL; |
29a1f233 TR |
174 | } |
175 | ||
176 | static inline struct gpio_desc *__must_check | |
b17d1bf1 UKK |
177 | gpiod_get_index_optional(struct device *dev, const char *con_id, |
178 | unsigned int index, enum gpiod_flags flags) | |
29a1f233 | 179 | { |
22c40367 | 180 | return NULL; |
29a1f233 TR |
181 | } |
182 | ||
66858527 RI |
183 | static inline struct gpio_descs *__must_check |
184 | gpiod_get_array(struct device *dev, const char *con_id, | |
185 | enum gpiod_flags flags) | |
186 | { | |
187 | return ERR_PTR(-ENOSYS); | |
188 | } | |
189 | ||
190 | static inline struct gpio_descs *__must_check | |
191 | gpiod_get_array_optional(struct device *dev, const char *con_id, | |
192 | enum gpiod_flags flags) | |
193 | { | |
22c40367 | 194 | return NULL; |
66858527 RI |
195 | } |
196 | ||
79a9becd AC |
197 | static inline void gpiod_put(struct gpio_desc *desc) |
198 | { | |
199 | might_sleep(); | |
200 | ||
201 | /* GPIO can never have been requested */ | |
202 | WARN_ON(1); | |
203 | } | |
204 | ||
66858527 RI |
205 | static inline void gpiod_put_array(struct gpio_descs *descs) |
206 | { | |
207 | might_sleep(); | |
208 | ||
209 | /* GPIO can never have been requested */ | |
210 | WARN_ON(1); | |
211 | } | |
212 | ||
0dbc8b7a | 213 | static inline struct gpio_desc *__must_check |
b17d1bf1 | 214 | devm_gpiod_get(struct device *dev, |
0dbc8b7a LW |
215 | const char *con_id, |
216 | enum gpiod_flags flags) | |
79a9becd AC |
217 | { |
218 | return ERR_PTR(-ENOSYS); | |
219 | } | |
220 | static inline | |
0dbc8b7a | 221 | struct gpio_desc *__must_check |
b17d1bf1 | 222 | devm_gpiod_get_index(struct device *dev, |
0dbc8b7a LW |
223 | const char *con_id, |
224 | unsigned int idx, | |
225 | enum gpiod_flags flags) | |
79a9becd AC |
226 | { |
227 | return ERR_PTR(-ENOSYS); | |
228 | } | |
29a1f233 TR |
229 | |
230 | static inline struct gpio_desc *__must_check | |
b17d1bf1 | 231 | devm_gpiod_get_optional(struct device *dev, const char *con_id, |
0dbc8b7a | 232 | enum gpiod_flags flags) |
29a1f233 | 233 | { |
22c40367 | 234 | return NULL; |
29a1f233 TR |
235 | } |
236 | ||
237 | static inline struct gpio_desc *__must_check | |
b17d1bf1 | 238 | devm_gpiod_get_index_optional(struct device *dev, const char *con_id, |
0dbc8b7a | 239 | unsigned int index, enum gpiod_flags flags) |
29a1f233 | 240 | { |
22c40367 | 241 | return NULL; |
29a1f233 TR |
242 | } |
243 | ||
331758ee RI |
244 | static inline struct gpio_descs *__must_check |
245 | devm_gpiod_get_array(struct device *dev, const char *con_id, | |
246 | enum gpiod_flags flags) | |
247 | { | |
248 | return ERR_PTR(-ENOSYS); | |
249 | } | |
250 | ||
251 | static inline struct gpio_descs *__must_check | |
252 | devm_gpiod_get_array_optional(struct device *dev, const char *con_id, | |
253 | enum gpiod_flags flags) | |
254 | { | |
22c40367 | 255 | return NULL; |
331758ee RI |
256 | } |
257 | ||
79a9becd | 258 | static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
331758ee RI |
259 | { |
260 | might_sleep(); | |
261 | ||
262 | /* GPIO can never have been requested */ | |
263 | WARN_ON(1); | |
264 | } | |
265 | ||
266 | static inline void devm_gpiod_put_array(struct device *dev, | |
267 | struct gpio_descs *descs) | |
79a9becd AC |
268 | { |
269 | might_sleep(); | |
270 | ||
271 | /* GPIO can never have been requested */ | |
272 | WARN_ON(1); | |
273 | } | |
274 | ||
275 | ||
276 | static inline int gpiod_get_direction(const struct gpio_desc *desc) | |
277 | { | |
278 | /* GPIO can never have been requested */ | |
279 | WARN_ON(1); | |
280 | return -ENOSYS; | |
281 | } | |
282 | static inline int gpiod_direction_input(struct gpio_desc *desc) | |
283 | { | |
284 | /* GPIO can never have been requested */ | |
285 | WARN_ON(1); | |
286 | return -ENOSYS; | |
287 | } | |
288 | static inline int gpiod_direction_output(struct gpio_desc *desc, int value) | |
289 | { | |
290 | /* GPIO can never have been requested */ | |
291 | WARN_ON(1); | |
292 | return -ENOSYS; | |
293 | } | |
ef70bbe1 PZ |
294 | static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
295 | { | |
296 | /* GPIO can never have been requested */ | |
297 | WARN_ON(1); | |
298 | return -ENOSYS; | |
299 | } | |
79a9becd AC |
300 | |
301 | ||
302 | static inline int gpiod_get_value(const struct gpio_desc *desc) | |
303 | { | |
304 | /* GPIO can never have been requested */ | |
305 | WARN_ON(1); | |
306 | return 0; | |
307 | } | |
308 | static inline void gpiod_set_value(struct gpio_desc *desc, int value) | |
309 | { | |
310 | /* GPIO can never have been requested */ | |
311 | WARN_ON(1); | |
312 | } | |
3fff99bc RI |
313 | static inline void gpiod_set_array_value(unsigned int array_size, |
314 | struct gpio_desc **desc_array, | |
315 | int *value_array) | |
5f424243 RI |
316 | { |
317 | /* GPIO can never have been requested */ | |
318 | WARN_ON(1); | |
319 | } | |
79a9becd AC |
320 | static inline int gpiod_get_raw_value(const struct gpio_desc *desc) |
321 | { | |
322 | /* GPIO can never have been requested */ | |
323 | WARN_ON(1); | |
324 | return 0; | |
325 | } | |
326 | static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) | |
327 | { | |
328 | /* GPIO can never have been requested */ | |
329 | WARN_ON(1); | |
330 | } | |
3fff99bc RI |
331 | static inline void gpiod_set_raw_array_value(unsigned int array_size, |
332 | struct gpio_desc **desc_array, | |
333 | int *value_array) | |
5f424243 RI |
334 | { |
335 | /* GPIO can never have been requested */ | |
336 | WARN_ON(1); | |
337 | } | |
79a9becd AC |
338 | |
339 | static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
340 | { | |
341 | /* GPIO can never have been requested */ | |
342 | WARN_ON(1); | |
343 | return 0; | |
344 | } | |
345 | static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
346 | { | |
347 | /* GPIO can never have been requested */ | |
348 | WARN_ON(1); | |
349 | } | |
3fff99bc | 350 | static inline void gpiod_set_array_value_cansleep(unsigned int array_size, |
5f424243 RI |
351 | struct gpio_desc **desc_array, |
352 | int *value_array) | |
353 | { | |
354 | /* GPIO can never have been requested */ | |
355 | WARN_ON(1); | |
356 | } | |
79a9becd AC |
357 | static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
358 | { | |
359 | /* GPIO can never have been requested */ | |
360 | WARN_ON(1); | |
361 | return 0; | |
362 | } | |
363 | static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, | |
364 | int value) | |
365 | { | |
366 | /* GPIO can never have been requested */ | |
367 | WARN_ON(1); | |
368 | } | |
3fff99bc | 369 | static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, |
5f424243 RI |
370 | struct gpio_desc **desc_array, |
371 | int *value_array) | |
372 | { | |
373 | /* GPIO can never have been requested */ | |
374 | WARN_ON(1); | |
375 | } | |
79a9becd AC |
376 | |
377 | static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | |
378 | { | |
379 | /* GPIO can never have been requested */ | |
380 | WARN_ON(1); | |
381 | return -ENOSYS; | |
382 | } | |
383 | ||
384 | static inline int gpiod_is_active_low(const struct gpio_desc *desc) | |
385 | { | |
386 | /* GPIO can never have been requested */ | |
387 | WARN_ON(1); | |
388 | return 0; | |
389 | } | |
390 | static inline int gpiod_cansleep(const struct gpio_desc *desc) | |
391 | { | |
392 | /* GPIO can never have been requested */ | |
393 | WARN_ON(1); | |
394 | return 0; | |
395 | } | |
396 | ||
397 | static inline int gpiod_to_irq(const struct gpio_desc *desc) | |
398 | { | |
399 | /* GPIO can never have been requested */ | |
400 | WARN_ON(1); | |
401 | return -EINVAL; | |
402 | } | |
403 | ||
404 | static inline struct gpio_desc *gpio_to_desc(unsigned gpio) | |
405 | { | |
406 | return ERR_PTR(-EINVAL); | |
407 | } | |
c0017ed7 | 408 | |
79a9becd AC |
409 | static inline int desc_to_gpio(const struct gpio_desc *desc) |
410 | { | |
411 | /* GPIO can never have been requested */ | |
412 | WARN_ON(1); | |
413 | return -EINVAL; | |
414 | } | |
79a9becd | 415 | |
496e7ce2 GU |
416 | /* Child properties interface */ |
417 | struct fwnode_handle; | |
418 | ||
a264d10f AS |
419 | static inline |
420 | struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, | |
537b94da | 421 | const char *propname, int index, |
b2987d74 AS |
422 | enum gpiod_flags dflags, |
423 | const char *label) | |
496e7ce2 GU |
424 | { |
425 | return ERR_PTR(-ENOSYS); | |
426 | } | |
427 | ||
537b94da BB |
428 | static inline |
429 | struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, | |
430 | const char *con_id, int index, | |
431 | struct fwnode_handle *child, | |
432 | enum gpiod_flags flags, | |
433 | const char *label) | |
434 | { | |
435 | return ERR_PTR(-ENOSYS); | |
436 | } | |
437 | ||
438 | #endif /* CONFIG_GPIOLIB */ | |
439 | ||
a264d10f | 440 | static inline |
4b094797 BB |
441 | struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, |
442 | const char *con_id, | |
443 | struct fwnode_handle *child, | |
444 | enum gpiod_flags flags, | |
445 | const char *label) | |
496e7ce2 | 446 | { |
537b94da BB |
447 | return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, |
448 | flags, label); | |
496e7ce2 GU |
449 | } |
450 | ||
79a9becd AC |
451 | #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) |
452 | ||
453 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change); | |
454 | int gpiod_export_link(struct device *dev, const char *name, | |
455 | struct gpio_desc *desc); | |
79a9becd AC |
456 | void gpiod_unexport(struct gpio_desc *desc); |
457 | ||
458 | #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ | |
459 | ||
460 | static inline int gpiod_export(struct gpio_desc *desc, | |
461 | bool direction_may_change) | |
462 | { | |
463 | return -ENOSYS; | |
464 | } | |
465 | ||
466 | static inline int gpiod_export_link(struct device *dev, const char *name, | |
467 | struct gpio_desc *desc) | |
468 | { | |
469 | return -ENOSYS; | |
470 | } | |
471 | ||
79a9becd AC |
472 | static inline void gpiod_unexport(struct gpio_desc *desc) |
473 | { | |
474 | } | |
475 | ||
476 | #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ | |
477 | ||
478 | #endif |