blob: f32f49e96c0b15a7ec492ca12f266593c6d8dc77 [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,
Alexander Steinb2987d72017-01-12 17:39:24 +0100139 enum gpiod_flags dflags,
140 const char *label);
Boris Brezillon4b094792017-02-02 14:53:10 +0100141struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
142 const char *con_id,
143 struct fwnode_handle *child,
144 enum gpiod_flags flags,
145 const char *label);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700146#else /* CONFIG_GPIOLIB */
147
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100148static inline int gpiod_count(struct device *dev, const char *con_id)
149{
150 return 0;
151}
152
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100153static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
154 const char *con_id,
155 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156{
157 return ERR_PTR(-ENOSYS);
158}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200159static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100160gpiod_get_index(struct device *dev,
161 const char *con_id,
162 unsigned int idx,
163 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700164{
165 return ERR_PTR(-ENOSYS);
166}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200167
168static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100169gpiod_get_optional(struct device *dev, const char *con_id,
170 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200171{
172 return ERR_PTR(-ENOSYS);
173}
174
175static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100176gpiod_get_index_optional(struct device *dev, const char *con_id,
177 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200178{
179 return ERR_PTR(-ENOSYS);
180}
181
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100182static inline struct gpio_descs *__must_check
183gpiod_get_array(struct device *dev, const char *con_id,
184 enum gpiod_flags flags)
185{
186 return ERR_PTR(-ENOSYS);
187}
188
189static inline struct gpio_descs *__must_check
190gpiod_get_array_optional(struct device *dev, const char *con_id,
191 enum gpiod_flags flags)
192{
193 return ERR_PTR(-ENOSYS);
194}
195
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700196static inline void gpiod_put(struct gpio_desc *desc)
197{
198 might_sleep();
199
200 /* GPIO can never have been requested */
201 WARN_ON(1);
202}
203
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100204static inline void gpiod_put_array(struct gpio_descs *descs)
205{
206 might_sleep();
207
208 /* GPIO can never have been requested */
209 WARN_ON(1);
210}
211
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200212static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100213devm_gpiod_get(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200214 const char *con_id,
215 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700216{
217 return ERR_PTR(-ENOSYS);
218}
219static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200220struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100221devm_gpiod_get_index(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200222 const char *con_id,
223 unsigned int idx,
224 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700225{
226 return ERR_PTR(-ENOSYS);
227}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200228
229static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100230devm_gpiod_get_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200231 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200232{
233 return ERR_PTR(-ENOSYS);
234}
235
236static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100237devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200238 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200239{
240 return ERR_PTR(-ENOSYS);
241}
242
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100243static inline struct gpio_descs *__must_check
244devm_gpiod_get_array(struct device *dev, const char *con_id,
245 enum gpiod_flags flags)
246{
247 return ERR_PTR(-ENOSYS);
248}
249
250static inline struct gpio_descs *__must_check
251devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
252 enum gpiod_flags flags)
253{
254 return ERR_PTR(-ENOSYS);
255}
256
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700257static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
258{
259 might_sleep();
260
261 /* GPIO can never have been requested */
262 WARN_ON(1);
263}
264
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100265static inline void devm_gpiod_put_array(struct device *dev,
266 struct gpio_descs *descs)
267{
268 might_sleep();
269
270 /* GPIO can never have been requested */
271 WARN_ON(1);
272}
273
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700274
275static inline int gpiod_get_direction(const struct gpio_desc *desc)
276{
277 /* GPIO can never have been requested */
278 WARN_ON(1);
279 return -ENOSYS;
280}
281static inline int gpiod_direction_input(struct gpio_desc *desc)
282{
283 /* GPIO can never have been requested */
284 WARN_ON(1);
285 return -ENOSYS;
286}
287static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
288{
289 /* GPIO can never have been requested */
290 WARN_ON(1);
291 return -ENOSYS;
292}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100293static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
294{
295 /* GPIO can never have been requested */
296 WARN_ON(1);
297 return -ENOSYS;
298}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700299
300
301static inline int gpiod_get_value(const struct gpio_desc *desc)
302{
303 /* GPIO can never have been requested */
304 WARN_ON(1);
305 return 0;
306}
307static inline void gpiod_set_value(struct gpio_desc *desc, int value)
308{
309 /* GPIO can never have been requested */
310 WARN_ON(1);
311}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200312static inline void gpiod_set_array_value(unsigned int array_size,
313 struct gpio_desc **desc_array,
314 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100315{
316 /* GPIO can never have been requested */
317 WARN_ON(1);
318}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700319static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
320{
321 /* GPIO can never have been requested */
322 WARN_ON(1);
323 return 0;
324}
325static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
326{
327 /* GPIO can never have been requested */
328 WARN_ON(1);
329}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200330static inline void gpiod_set_raw_array_value(unsigned int array_size,
331 struct gpio_desc **desc_array,
332 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100333{
334 /* GPIO can never have been requested */
335 WARN_ON(1);
336}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700337
338static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
339{
340 /* GPIO can never have been requested */
341 WARN_ON(1);
342 return 0;
343}
344static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
345{
346 /* GPIO can never have been requested */
347 WARN_ON(1);
348}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200349static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100350 struct gpio_desc **desc_array,
351 int *value_array)
352{
353 /* GPIO can never have been requested */
354 WARN_ON(1);
355}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700356static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
357{
358 /* GPIO can never have been requested */
359 WARN_ON(1);
360 return 0;
361}
362static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
363 int value)
364{
365 /* GPIO can never have been requested */
366 WARN_ON(1);
367}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200368static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100369 struct gpio_desc **desc_array,
370 int *value_array)
371{
372 /* GPIO can never have been requested */
373 WARN_ON(1);
374}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700375
376static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
377{
378 /* GPIO can never have been requested */
379 WARN_ON(1);
380 return -ENOSYS;
381}
382
383static inline int gpiod_is_active_low(const struct gpio_desc *desc)
384{
385 /* GPIO can never have been requested */
386 WARN_ON(1);
387 return 0;
388}
389static inline int gpiod_cansleep(const struct gpio_desc *desc)
390{
391 /* GPIO can never have been requested */
392 WARN_ON(1);
393 return 0;
394}
395
396static inline int gpiod_to_irq(const struct gpio_desc *desc)
397{
398 /* GPIO can never have been requested */
399 WARN_ON(1);
400 return -EINVAL;
401}
402
403static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
404{
405 return ERR_PTR(-EINVAL);
406}
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200407
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700408static inline int desc_to_gpio(const struct gpio_desc *desc)
409{
410 /* GPIO can never have been requested */
411 WARN_ON(1);
412 return -EINVAL;
413}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700414
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700415/* Child properties interface */
416struct fwnode_handle;
417
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200418static inline
419struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
420 const char *propname,
Alexander Steinb2987d72017-01-12 17:39:24 +0100421 enum gpiod_flags dflags,
422 const char *label)
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700423{
424 return ERR_PTR(-ENOSYS);
425}
426
Andy Shevchenkoa264d102017-01-09 16:02:28 +0200427static inline
Boris Brezillon4b094792017-02-02 14:53:10 +0100428struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
429 const char *con_id,
430 struct fwnode_handle *child,
431 enum gpiod_flags flags,
432 const char *label)
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700433{
434 return ERR_PTR(-ENOSYS);
435}
436
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700437#endif /* CONFIG_GPIOLIB */
438
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700439#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
440
441int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
442int gpiod_export_link(struct device *dev, const char *name,
443 struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700444void gpiod_unexport(struct gpio_desc *desc);
445
446#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
447
448static inline int gpiod_export(struct gpio_desc *desc,
449 bool direction_may_change)
450{
451 return -ENOSYS;
452}
453
454static inline int gpiod_export_link(struct device *dev, const char *name,
455 struct gpio_desc *desc)
456{
457 return -ENOSYS;
458}
459
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700460static inline void gpiod_unexport(struct gpio_desc *desc)
461{
462}
463
464#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
465
466#endif