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