blob: 33eb52fd0932611a5f12e80b0d682a3c5e7e3feb [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 */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090050struct gpio_desc *__must_check __gpiod_get(struct device *dev,
51 const char *con_id,
52 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090053struct 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);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090057struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
58 const char *con_id,
59 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090060struct 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
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090073struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
74 const char *con_id,
75 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090076struct 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);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090080struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
81 const char *con_id,
82 enum gpiod_flags flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +020083struct gpio_desc *__must_check
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090084__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
85 unsigned int index, enum gpiod_flags flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070086void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
87
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +090088int gpiod_get_direction(struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070089int gpiod_direction_input(struct gpio_desc *desc);
90int gpiod_direction_output(struct gpio_desc *desc, int value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +010091int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070092
93/* Value get/set from non-sleeping context */
94int gpiod_get_value(const struct gpio_desc *desc);
95void gpiod_set_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010096void gpiod_set_array(unsigned int array_size,
97 struct gpio_desc **desc_array, int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070098int gpiod_get_raw_value(const struct gpio_desc *desc);
99void gpiod_set_raw_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100100void gpiod_set_raw_array(unsigned int array_size,
101 struct gpio_desc **desc_array, int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700102
103/* Value get/set from sleeping context */
104int gpiod_get_value_cansleep(const struct gpio_desc *desc);
105void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100106void gpiod_set_array_cansleep(unsigned int array_size,
107 struct gpio_desc **desc_array,
108 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
110void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100111void gpiod_set_raw_array_cansleep(unsigned int array_size,
112 struct gpio_desc **desc_array,
113 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700114
115int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
116
117int gpiod_is_active_low(const struct gpio_desc *desc);
118int gpiod_cansleep(const struct gpio_desc *desc);
119
120int gpiod_to_irq(const struct gpio_desc *desc);
121
122/* Convert between the old gpio_ and new gpiod_ interfaces */
123struct gpio_desc *gpio_to_desc(unsigned gpio);
124int desc_to_gpio(const struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700125
Mika Westerberg40b73182014-10-21 13:33:59 +0200126/* Child properties interface */
127struct fwnode_handle;
128
129struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
130 const char *propname);
131struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100132 const char *con_id,
Mika Westerberg40b73182014-10-21 13:33:59 +0200133 struct fwnode_handle *child);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700134#else /* CONFIG_GPIOLIB */
135
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100136static inline int gpiod_count(struct device *dev, const char *con_id)
137{
138 return 0;
139}
140
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200141static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
142 const char *con_id,
143 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700144{
145 return ERR_PTR(-ENOSYS);
146}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200147static inline struct gpio_desc *__must_check
148__gpiod_get_index(struct device *dev,
149 const char *con_id,
150 unsigned int idx,
151 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700152{
153 return ERR_PTR(-ENOSYS);
154}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200155
156static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200157__gpiod_get_optional(struct device *dev, const char *con_id,
158 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200159{
160 return ERR_PTR(-ENOSYS);
161}
162
163static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200164__gpiod_get_index_optional(struct device *dev, const char *con_id,
165 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200166{
167 return ERR_PTR(-ENOSYS);
168}
169
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100170static inline struct gpio_descs *__must_check
171gpiod_get_array(struct device *dev, const char *con_id,
172 enum gpiod_flags flags)
173{
174 return ERR_PTR(-ENOSYS);
175}
176
177static inline struct gpio_descs *__must_check
178gpiod_get_array_optional(struct device *dev, const char *con_id,
179 enum gpiod_flags flags)
180{
181 return ERR_PTR(-ENOSYS);
182}
183
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700184static inline void gpiod_put(struct gpio_desc *desc)
185{
186 might_sleep();
187
188 /* GPIO can never have been requested */
189 WARN_ON(1);
190}
191
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100192static inline void gpiod_put_array(struct gpio_descs *descs)
193{
194 might_sleep();
195
196 /* GPIO can never have been requested */
197 WARN_ON(1);
198}
199
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200200static inline struct gpio_desc *__must_check
201__devm_gpiod_get(struct device *dev,
202 const char *con_id,
203 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700204{
205 return ERR_PTR(-ENOSYS);
206}
207static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200208struct gpio_desc *__must_check
209__devm_gpiod_get_index(struct device *dev,
210 const char *con_id,
211 unsigned int idx,
212 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700213{
214 return ERR_PTR(-ENOSYS);
215}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200216
217static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200218__devm_gpiod_get_optional(struct device *dev, const char *con_id,
219 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200220{
221 return ERR_PTR(-ENOSYS);
222}
223
224static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200225__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
226 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200227{
228 return ERR_PTR(-ENOSYS);
229}
230
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700231static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
232{
233 might_sleep();
234
235 /* GPIO can never have been requested */
236 WARN_ON(1);
237}
238
239
240static inline int gpiod_get_direction(const struct gpio_desc *desc)
241{
242 /* GPIO can never have been requested */
243 WARN_ON(1);
244 return -ENOSYS;
245}
246static inline int gpiod_direction_input(struct gpio_desc *desc)
247{
248 /* GPIO can never have been requested */
249 WARN_ON(1);
250 return -ENOSYS;
251}
252static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
253{
254 /* GPIO can never have been requested */
255 WARN_ON(1);
256 return -ENOSYS;
257}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100258static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
259{
260 /* GPIO can never have been requested */
261 WARN_ON(1);
262 return -ENOSYS;
263}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700264
265
266static inline int gpiod_get_value(const struct gpio_desc *desc)
267{
268 /* GPIO can never have been requested */
269 WARN_ON(1);
270 return 0;
271}
272static inline void gpiod_set_value(struct gpio_desc *desc, int value)
273{
274 /* GPIO can never have been requested */
275 WARN_ON(1);
276}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100277static inline void gpiod_set_array(unsigned int array_size,
278 struct gpio_desc **desc_array,
279 int *value_array)
280{
281 /* GPIO can never have been requested */
282 WARN_ON(1);
283}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700284static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
285{
286 /* GPIO can never have been requested */
287 WARN_ON(1);
288 return 0;
289}
290static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
291{
292 /* GPIO can never have been requested */
293 WARN_ON(1);
294}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100295static inline void gpiod_set_raw_array(unsigned int array_size,
296 struct gpio_desc **desc_array,
297 int *value_array)
298{
299 /* GPIO can never have been requested */
300 WARN_ON(1);
301}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700302
303static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
304{
305 /* GPIO can never have been requested */
306 WARN_ON(1);
307 return 0;
308}
309static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
310{
311 /* GPIO can never have been requested */
312 WARN_ON(1);
313}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100314static inline void gpiod_set_array_cansleep(unsigned int array_size,
315 struct gpio_desc **desc_array,
316 int *value_array)
317{
318 /* GPIO can never have been requested */
319 WARN_ON(1);
320}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700321static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
322{
323 /* GPIO can never have been requested */
324 WARN_ON(1);
325 return 0;
326}
327static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
328 int value)
329{
330 /* GPIO can never have been requested */
331 WARN_ON(1);
332}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100333static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
334 struct gpio_desc **desc_array,
335 int *value_array)
336{
337 /* GPIO can never have been requested */
338 WARN_ON(1);
339}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700340
341static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
342{
343 /* GPIO can never have been requested */
344 WARN_ON(1);
345 return -ENOSYS;
346}
347
348static inline int gpiod_is_active_low(const struct gpio_desc *desc)
349{
350 /* GPIO can never have been requested */
351 WARN_ON(1);
352 return 0;
353}
354static inline int gpiod_cansleep(const struct gpio_desc *desc)
355{
356 /* GPIO can never have been requested */
357 WARN_ON(1);
358 return 0;
359}
360
361static inline int gpiod_to_irq(const struct gpio_desc *desc)
362{
363 /* GPIO can never have been requested */
364 WARN_ON(1);
365 return -EINVAL;
366}
367
368static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
369{
370 return ERR_PTR(-EINVAL);
371}
372static inline int desc_to_gpio(const struct gpio_desc *desc)
373{
374 /* GPIO can never have been requested */
375 WARN_ON(1);
376 return -EINVAL;
377}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700378
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700379#endif /* CONFIG_GPIOLIB */
380
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200381/*
382 * Vararg-hacks! This is done to transition the kernel to always pass
383 * the options flags argument to the below functions. During a transition
384 * phase these vararg macros make both old-and-newstyle code compile,
385 * but when all calls to the elder API are removed, these should go away
386 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
387 * etc.
388 */
389#define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100390#define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200391#define __gpiod_get_index(dev, con_id, index, flags, ...) \
392 __gpiod_get_index(dev, con_id, index, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100393#define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200394#define __gpiod_get_optional(dev, con_id, flags, ...) \
395 __gpiod_get_optional(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100396#define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200397#define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
398 __gpiod_get_index_optional(dev, con_id, index, flags)
399#define gpiod_get_index_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100400 __gpiod_get_index_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200401#define __devm_gpiod_get(dev, con_id, flags, ...) \
402 __devm_gpiod_get(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100403#define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200404#define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
405 __devm_gpiod_get_index(dev, con_id, index, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100406#define devm_gpiod_get_index(varargs...) \
407 __devm_gpiod_get_index(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200408#define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
409 __devm_gpiod_get_optional(dev, con_id, flags)
410#define devm_gpiod_get_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100411 __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200412#define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
413 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
414#define devm_gpiod_get_index_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100415 __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200416
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700417#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
418
419int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
420int gpiod_export_link(struct device *dev, const char *name,
421 struct gpio_desc *desc);
422int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
423void gpiod_unexport(struct gpio_desc *desc);
424
425#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
426
427static inline int gpiod_export(struct gpio_desc *desc,
428 bool direction_may_change)
429{
430 return -ENOSYS;
431}
432
433static inline int gpiod_export_link(struct device *dev, const char *name,
434 struct gpio_desc *desc)
435{
436 return -ENOSYS;
437}
438
439static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
440{
441 return -ENOSYS;
442}
443
444static inline void gpiod_unexport(struct gpio_desc *desc)
445{
446}
447
448#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
449
450#endif