Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / include / linux / gpio / consumer.h
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 void 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
172 static inline int gpiod_count(struct device *dev, const char *con_id)
173 {
174         return 0;
175 }
176
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
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
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
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
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
214 gpiod_get_array_optional(struct device *dev, const char *con_id,
215                          enum gpiod_flags flags)
216 {
217         return NULL;
218 }
219
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(1);
226 }
227
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(1);
234 }
235
236 static inline struct gpio_desc *__must_check
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
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
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
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
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
275 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
276                               enum gpiod_flags flags)
277 {
278         return NULL;
279 }
280
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(1);
287 }
288
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(1);
296 }
297
298
299 static inline int gpiod_get_direction(const struct gpio_desc *desc)
300 {
301         /* GPIO can never have been requested */
302         WARN_ON(1);
303         return -ENOSYS;
304 }
305 static inline int gpiod_direction_input(struct gpio_desc *desc)
306 {
307         /* GPIO can never have been requested */
308         WARN_ON(1);
309         return -ENOSYS;
310 }
311 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
312 {
313         /* GPIO can never have been requested */
314         WARN_ON(1);
315         return -ENOSYS;
316 }
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(1);
321         return -ENOSYS;
322 }
323
324
325 static inline int gpiod_get_value(const struct gpio_desc *desc)
326 {
327         /* GPIO can never have been requested */
328         WARN_ON(1);
329         return 0;
330 }
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(1);
337         return 0;
338 }
339 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
340 {
341         /* GPIO can never have been requested */
342         WARN_ON(1);
343 }
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(1);
350 }
351 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
352 {
353         /* GPIO can never have been requested */
354         WARN_ON(1);
355         return 0;
356 }
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(1);
363         return 0;
364 }
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(1);
369 }
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(1);
376         return 0;
377 }
378
379 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
380 {
381         /* GPIO can never have been requested */
382         WARN_ON(1);
383         return 0;
384 }
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(1);
391         return 0;
392 }
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(1);
397 }
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(1);
404 }
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(1);
409         return 0;
410 }
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(1);
417         return 0;
418 }
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(1);
424 }
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(1);
431         return 0;
432 }
433
434 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
435 {
436         /* GPIO can never have been requested */
437         WARN_ON(1);
438         return -ENOSYS;
439 }
440
441 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
442 {
443         /* GPIO can never have been requested */
444         WARN_ON(1);
445         return -ENOSYS;
446 }
447
448 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
449 {
450         /* GPIO can never have been requested */
451         WARN_ON(1);
452         return 0;
453 }
454 static inline int gpiod_cansleep(const struct gpio_desc *desc)
455 {
456         /* GPIO can never have been requested */
457         WARN_ON(1);
458         return 0;
459 }
460
461 static inline int gpiod_to_irq(const struct gpio_desc *desc)
462 {
463         /* GPIO can never have been requested */
464         WARN_ON(1);
465         return -EINVAL;
466 }
467
468 static inline void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
469 {
470         /* GPIO can never have been requested */
471         WARN_ON(1);
472 }
473
474 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
475 {
476         return ERR_PTR(-EINVAL);
477 }
478
479 static inline int desc_to_gpio(const struct gpio_desc *desc)
480 {
481         /* GPIO can never have been requested */
482         WARN_ON(1);
483         return -EINVAL;
484 }
485
486 /* Child properties interface */
487 struct device_node;
488 struct fwnode_handle;
489
490 static inline
491 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
492                                               struct device_node *node,
493                                               const char *propname, int index,
494                                               enum gpiod_flags dflags,
495                                               const char *label)
496 {
497         return ERR_PTR(-ENOSYS);
498 }
499
500 static inline
501 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
502                                          const char *propname, int index,
503                                          enum gpiod_flags dflags,
504                                          const char *label)
505 {
506         return ERR_PTR(-ENOSYS);
507 }
508
509 static inline
510 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
511                                                 const char *con_id, int index,
512                                                 struct fwnode_handle *child,
513                                                 enum gpiod_flags flags,
514                                                 const char *label)
515 {
516         return ERR_PTR(-ENOSYS);
517 }
518
519 #endif /* CONFIG_GPIOLIB */
520
521 static inline
522 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
523                                                    const char *con_id,
524                                                    struct fwnode_handle *child,
525                                                    enum gpiod_flags flags,
526                                                    const char *label)
527 {
528         return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
529                                                       flags, label);
530 }
531
532 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
533
534 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
535 int gpiod_export_link(struct device *dev, const char *name,
536                       struct gpio_desc *desc);
537 void gpiod_unexport(struct gpio_desc *desc);
538
539 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
540
541 static inline int gpiod_export(struct gpio_desc *desc,
542                                bool direction_may_change)
543 {
544         return -ENOSYS;
545 }
546
547 static inline int gpiod_export_link(struct device *dev, const char *name,
548                                     struct gpio_desc *desc)
549 {
550         return -ENOSYS;
551 }
552
553 static inline void gpiod_unexport(struct gpio_desc *desc)
554 {
555 }
556
557 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
558
559 #endif