blob: 930d10049d8d50ac4d8c29e402c4471de19b5b6b [file] [log] [blame]
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001#ifndef __LINUX_GPIO_CONSUMER_H
2#define __LINUX_GPIO_CONSUMER_H
3
Arnd Bergmanncdf86cd22014-05-08 15:42:25 +02004#include <linux/bug.h>
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07005#include <linux/err.h>
6#include <linux/kernel.h>
7
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07008struct device;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07009
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 */
17struct gpio_desc;
18
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010019/**
20 * Struct containing an array of descriptors that can be obtained using
21 * gpiod_get_array().
22 */
23struct gpio_descs {
24 unsigned int ndescs;
25 struct gpio_desc *desc[];
26};
27
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090028#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 */
36enum 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
Linus Walleij58b84f62014-08-19 12:00:53 -050044#ifdef CONFIG_GPIOLIB
45
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010046/* Return the number of GPIOs associated with a device / function */
47int gpiod_count(struct device *dev, const char *con_id);
48
Alexandre Courbotbae48da2013-10-17 10:21:38 -070049/* Acquire and dispose GPIOs */
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010050struct gpio_desc *__must_check gpiod_get(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090051 const char *con_id,
52 enum gpiod_flags flags);
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010053struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -070054 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090055 unsigned int idx,
56 enum gpiod_flags flags);
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010057struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090058 const char *con_id,
59 enum gpiod_flags flags);
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010060struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +020061 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090062 unsigned int index,
63 enum gpiod_flags flags);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010064struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
65 const char *con_id,
66 enum gpiod_flags flags);
67struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
68 const char *con_id,
69 enum gpiod_flags flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070070void gpiod_put(struct gpio_desc *desc);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010071void gpiod_put_array(struct gpio_descs *descs);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070072
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010073struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090074 const char *con_id,
75 enum gpiod_flags flags);
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010076struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -070077 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090078 unsigned int idx,
79 enum gpiod_flags flags);
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010080struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090081 const char *con_id,
82 enum gpiod_flags flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +020083struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +010084devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090085 unsigned int index, enum gpiod_flags flags);
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +010086struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
87 const char *con_id,
88 enum gpiod_flags flags);
89struct gpio_descs *__must_check
90devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
91 enum gpiod_flags flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070092void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +010093void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070094
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +090095int gpiod_get_direction(struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070096int gpiod_direction_input(struct gpio_desc *desc);
97int gpiod_direction_output(struct gpio_desc *desc, int value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +010098int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070099
100/* Value get/set from non-sleeping context */
101int gpiod_get_value(const struct gpio_desc *desc);
102void gpiod_set_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200103void gpiod_set_array_value(unsigned int array_size,
104 struct gpio_desc **desc_array, int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700105int gpiod_get_raw_value(const struct gpio_desc *desc);
106void gpiod_set_raw_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200107void gpiod_set_raw_array_value(unsigned int array_size,
108 struct gpio_desc **desc_array,
109 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700110
111/* Value get/set from sleeping context */
112int gpiod_get_value_cansleep(const struct gpio_desc *desc);
113void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200114void gpiod_set_array_value_cansleep(unsigned int array_size,
115 struct gpio_desc **desc_array,
116 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700117int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
118void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200119void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
120 struct gpio_desc **desc_array,
121 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700122
123int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
124
125int gpiod_is_active_low(const struct gpio_desc *desc);
126int gpiod_cansleep(const struct gpio_desc *desc);
127
128int gpiod_to_irq(const struct gpio_desc *desc);
129
130/* Convert between the old gpio_ and new gpiod_ interfaces */
131struct gpio_desc *gpio_to_desc(unsigned gpio);
132int desc_to_gpio(const struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700133
Mika Westerberg40b73182014-10-21 13:33:59 +0200134/* Child properties interface */
135struct fwnode_handle;
136
137struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200138 const char *propname,
139 enum gpiod_flags dflags);
Mika Westerberg40b73182014-10-21 13:33:59 +0200140struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100141 const char *con_id,
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200142 struct fwnode_handle *child,
143 enum gpiod_flags flags);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700144#else /* CONFIG_GPIOLIB */
145
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100146static inline int gpiod_count(struct device *dev, const char *con_id)
147{
148 return 0;
149}
150
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100151static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
152 const char *con_id,
153 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700154{
155 return ERR_PTR(-ENOSYS);
156}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200157static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100158gpiod_get_index(struct device *dev,
159 const char *con_id,
160 unsigned int idx,
161 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700162{
163 return ERR_PTR(-ENOSYS);
164}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200165
166static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100167gpiod_get_optional(struct device *dev, const char *con_id,
168 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200169{
170 return ERR_PTR(-ENOSYS);
171}
172
173static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100174gpiod_get_index_optional(struct device *dev, const char *con_id,
175 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200176{
177 return ERR_PTR(-ENOSYS);
178}
179
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100180static inline struct gpio_descs *__must_check
181gpiod_get_array(struct device *dev, const char *con_id,
182 enum gpiod_flags flags)
183{
184 return ERR_PTR(-ENOSYS);
185}
186
187static inline struct gpio_descs *__must_check
188gpiod_get_array_optional(struct device *dev, const char *con_id,
189 enum gpiod_flags flags)
190{
191 return ERR_PTR(-ENOSYS);
192}
193
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700194static inline void gpiod_put(struct gpio_desc *desc)
195{
196 might_sleep();
197
198 /* GPIO can never have been requested */
199 WARN_ON(1);
200}
201
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100202static inline void gpiod_put_array(struct gpio_descs *descs)
203{
204 might_sleep();
205
206 /* GPIO can never have been requested */
207 WARN_ON(1);
208}
209
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200210static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100211devm_gpiod_get(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200212 const char *con_id,
213 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700214{
215 return ERR_PTR(-ENOSYS);
216}
217static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200218struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100219devm_gpiod_get_index(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200220 const char *con_id,
221 unsigned int idx,
222 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700223{
224 return ERR_PTR(-ENOSYS);
225}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200226
227static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100228devm_gpiod_get_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200229 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200230{
231 return ERR_PTR(-ENOSYS);
232}
233
234static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100235devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200236 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200237{
238 return ERR_PTR(-ENOSYS);
239}
240
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100241static inline struct gpio_descs *__must_check
242devm_gpiod_get_array(struct device *dev, const char *con_id,
243 enum gpiod_flags flags)
244{
245 return ERR_PTR(-ENOSYS);
246}
247
248static inline struct gpio_descs *__must_check
249devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
250 enum gpiod_flags flags)
251{
252 return ERR_PTR(-ENOSYS);
253}
254
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700255static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
256{
257 might_sleep();
258
259 /* GPIO can never have been requested */
260 WARN_ON(1);
261}
262
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100263static inline void devm_gpiod_put_array(struct device *dev,
264 struct gpio_descs *descs)
265{
266 might_sleep();
267
268 /* GPIO can never have been requested */
269 WARN_ON(1);
270}
271
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700272
273static inline int gpiod_get_direction(const struct gpio_desc *desc)
274{
275 /* GPIO can never have been requested */
276 WARN_ON(1);
277 return -ENOSYS;
278}
279static inline int gpiod_direction_input(struct gpio_desc *desc)
280{
281 /* GPIO can never have been requested */
282 WARN_ON(1);
283 return -ENOSYS;
284}
285static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
286{
287 /* GPIO can never have been requested */
288 WARN_ON(1);
289 return -ENOSYS;
290}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100291static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
292{
293 /* GPIO can never have been requested */
294 WARN_ON(1);
295 return -ENOSYS;
296}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700297
298
299static inline int gpiod_get_value(const struct gpio_desc *desc)
300{
301 /* GPIO can never have been requested */
302 WARN_ON(1);
303 return 0;
304}
305static inline void gpiod_set_value(struct gpio_desc *desc, int value)
306{
307 /* GPIO can never have been requested */
308 WARN_ON(1);
309}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200310static inline void gpiod_set_array_value(unsigned int array_size,
311 struct gpio_desc **desc_array,
312 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100313{
314 /* GPIO can never have been requested */
315 WARN_ON(1);
316}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700317static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
318{
319 /* GPIO can never have been requested */
320 WARN_ON(1);
321 return 0;
322}
323static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
324{
325 /* GPIO can never have been requested */
326 WARN_ON(1);
327}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200328static inline void gpiod_set_raw_array_value(unsigned int array_size,
329 struct gpio_desc **desc_array,
330 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100331{
332 /* GPIO can never have been requested */
333 WARN_ON(1);
334}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700335
336static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
337{
338 /* GPIO can never have been requested */
339 WARN_ON(1);
340 return 0;
341}
342static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
343{
344 /* GPIO can never have been requested */
345 WARN_ON(1);
346}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200347static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100348 struct gpio_desc **desc_array,
349 int *value_array)
350{
351 /* GPIO can never have been requested */
352 WARN_ON(1);
353}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700354static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
355{
356 /* GPIO can never have been requested */
357 WARN_ON(1);
358 return 0;
359}
360static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
361 int value)
362{
363 /* GPIO can never have been requested */
364 WARN_ON(1);
365}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200366static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100367 struct gpio_desc **desc_array,
368 int *value_array)
369{
370 /* GPIO can never have been requested */
371 WARN_ON(1);
372}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700373
374static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
375{
376 /* GPIO can never have been requested */
377 WARN_ON(1);
378 return -ENOSYS;
379}
380
381static inline int gpiod_is_active_low(const struct gpio_desc *desc)
382{
383 /* GPIO can never have been requested */
384 WARN_ON(1);
385 return 0;
386}
387static inline int gpiod_cansleep(const struct gpio_desc *desc)
388{
389 /* GPIO can never have been requested */
390 WARN_ON(1);
391 return 0;
392}
393
394static inline int gpiod_to_irq(const struct gpio_desc *desc)
395{
396 /* GPIO can never have been requested */
397 WARN_ON(1);
398 return -EINVAL;
399}
400
401static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
402{
403 return ERR_PTR(-EINVAL);
404}
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200405
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700406static inline int desc_to_gpio(const struct gpio_desc *desc)
407{
408 /* GPIO can never have been requested */
409 WARN_ON(1);
410 return -EINVAL;
411}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700412
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700413/* Child properties interface */
414struct fwnode_handle;
415
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200416static inline
417struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
418 const char *propname,
419 enum gpiod_flags dflags)
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700420{
421 return ERR_PTR(-ENOSYS);
422}
423
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200424static inline
425struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
426 const char *con_id,
427 struct fwnode_handle *child,
428 enum gpiod_flags flags)
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700429{
430 return ERR_PTR(-ENOSYS);
431}
432
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700433#endif /* CONFIG_GPIOLIB */
434
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700435#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
436
437int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
438int gpiod_export_link(struct device *dev, const char *name,
439 struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700440void gpiod_unexport(struct gpio_desc *desc);
441
442#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
443
444static inline int gpiod_export(struct gpio_desc *desc,
445 bool direction_may_change)
446{
447 return -ENOSYS;
448}
449
450static inline int gpiod_export_link(struct device *dev, const char *name,
451 struct gpio_desc *desc)
452{
453 return -ENOSYS;
454}
455
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700456static inline void gpiod_unexport(struct gpio_desc *desc)
457{
458}
459
460#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
461
462#endif