blob: 366a3fdbdbeaad8ad0466c8c1fb064d4fecafdb1 [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);
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200133struct gpio_desc *gpio_name_to_desc(const char *name);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700134
Mika Westerberg40b73182014-10-21 13:33:59 +0200135/* Child properties interface */
136struct fwnode_handle;
137
138struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
139 const char *propname);
140struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100141 const char *con_id,
Mika Westerberg40b73182014-10-21 13:33:59 +0200142 struct fwnode_handle *child);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700143#else /* CONFIG_GPIOLIB */
144
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100145static inline int gpiod_count(struct device *dev, const char *con_id)
146{
147 return 0;
148}
149
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100150static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
151 const char *con_id,
152 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700153{
154 return ERR_PTR(-ENOSYS);
155}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200156static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100157gpiod_get_index(struct device *dev,
158 const char *con_id,
159 unsigned int idx,
160 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700161{
162 return ERR_PTR(-ENOSYS);
163}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200164
165static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100166gpiod_get_optional(struct device *dev, const char *con_id,
167 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200168{
169 return ERR_PTR(-ENOSYS);
170}
171
172static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100173gpiod_get_index_optional(struct device *dev, const char *con_id,
174 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200175{
176 return ERR_PTR(-ENOSYS);
177}
178
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100179static inline struct gpio_descs *__must_check
180gpiod_get_array(struct device *dev, const char *con_id,
181 enum gpiod_flags flags)
182{
183 return ERR_PTR(-ENOSYS);
184}
185
186static inline struct gpio_descs *__must_check
187gpiod_get_array_optional(struct device *dev, const char *con_id,
188 enum gpiod_flags flags)
189{
190 return ERR_PTR(-ENOSYS);
191}
192
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700193static inline void gpiod_put(struct gpio_desc *desc)
194{
195 might_sleep();
196
197 /* GPIO can never have been requested */
198 WARN_ON(1);
199}
200
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100201static inline void gpiod_put_array(struct gpio_descs *descs)
202{
203 might_sleep();
204
205 /* GPIO can never have been requested */
206 WARN_ON(1);
207}
208
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200209static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100210devm_gpiod_get(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200211 const char *con_id,
212 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700213{
214 return ERR_PTR(-ENOSYS);
215}
216static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200217struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100218devm_gpiod_get_index(struct device *dev,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200219 const char *con_id,
220 unsigned int idx,
221 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700222{
223 return ERR_PTR(-ENOSYS);
224}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200225
226static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100227devm_gpiod_get_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200228 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200229{
230 return ERR_PTR(-ENOSYS);
231}
232
233static inline struct gpio_desc *__must_check
Uwe Kleine-Königb17d1bf2015-02-11 11:52:37 +0100234devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200235 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200236{
237 return ERR_PTR(-ENOSYS);
238}
239
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100240static inline struct gpio_descs *__must_check
241devm_gpiod_get_array(struct device *dev, const char *con_id,
242 enum gpiod_flags flags)
243{
244 return ERR_PTR(-ENOSYS);
245}
246
247static inline struct gpio_descs *__must_check
248devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
249 enum gpiod_flags flags)
250{
251 return ERR_PTR(-ENOSYS);
252}
253
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700254static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
255{
256 might_sleep();
257
258 /* GPIO can never have been requested */
259 WARN_ON(1);
260}
261
Rojhalat Ibrahim331758e2015-02-11 17:28:02 +0100262static inline void devm_gpiod_put_array(struct device *dev,
263 struct gpio_descs *descs)
264{
265 might_sleep();
266
267 /* GPIO can never have been requested */
268 WARN_ON(1);
269}
270
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700271
272static inline int gpiod_get_direction(const struct gpio_desc *desc)
273{
274 /* GPIO can never have been requested */
275 WARN_ON(1);
276 return -ENOSYS;
277}
278static inline int gpiod_direction_input(struct gpio_desc *desc)
279{
280 /* GPIO can never have been requested */
281 WARN_ON(1);
282 return -ENOSYS;
283}
284static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
285{
286 /* GPIO can never have been requested */
287 WARN_ON(1);
288 return -ENOSYS;
289}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100290static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
291{
292 /* GPIO can never have been requested */
293 WARN_ON(1);
294 return -ENOSYS;
295}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700296
297
298static inline int gpiod_get_value(const struct gpio_desc *desc)
299{
300 /* GPIO can never have been requested */
301 WARN_ON(1);
302 return 0;
303}
304static inline void gpiod_set_value(struct gpio_desc *desc, int value)
305{
306 /* GPIO can never have been requested */
307 WARN_ON(1);
308}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200309static inline void gpiod_set_array_value(unsigned int array_size,
310 struct gpio_desc **desc_array,
311 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100312{
313 /* GPIO can never have been requested */
314 WARN_ON(1);
315}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700316static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
317{
318 /* GPIO can never have been requested */
319 WARN_ON(1);
320 return 0;
321}
322static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
323{
324 /* GPIO can never have been requested */
325 WARN_ON(1);
326}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200327static inline void gpiod_set_raw_array_value(unsigned int array_size,
328 struct gpio_desc **desc_array,
329 int *value_array)
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100330{
331 /* GPIO can never have been requested */
332 WARN_ON(1);
333}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700334
335static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
336{
337 /* GPIO can never have been requested */
338 WARN_ON(1);
339 return 0;
340}
341static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
342{
343 /* GPIO can never have been requested */
344 WARN_ON(1);
345}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200346static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100347 struct gpio_desc **desc_array,
348 int *value_array)
349{
350 /* GPIO can never have been requested */
351 WARN_ON(1);
352}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700353static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
354{
355 /* GPIO can never have been requested */
356 WARN_ON(1);
357 return 0;
358}
359static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
360 int value)
361{
362 /* GPIO can never have been requested */
363 WARN_ON(1);
364}
Rojhalat Ibrahim3fff99b2015-05-13 11:04:56 +0200365static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100366 struct gpio_desc **desc_array,
367 int *value_array)
368{
369 /* GPIO can never have been requested */
370 WARN_ON(1);
371}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700372
373static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
374{
375 /* GPIO can never have been requested */
376 WARN_ON(1);
377 return -ENOSYS;
378}
379
380static inline int gpiod_is_active_low(const struct gpio_desc *desc)
381{
382 /* GPIO can never have been requested */
383 WARN_ON(1);
384 return 0;
385}
386static inline int gpiod_cansleep(const struct gpio_desc *desc)
387{
388 /* GPIO can never have been requested */
389 WARN_ON(1);
390 return 0;
391}
392
393static inline int gpiod_to_irq(const struct gpio_desc *desc)
394{
395 /* GPIO can never have been requested */
396 WARN_ON(1);
397 return -EINVAL;
398}
399
400static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
401{
402 return ERR_PTR(-EINVAL);
403}
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200404
405static inline struct gpio_desc *gpio_name_to_desc(const char *name)
406{
407 return ERR_PTR(-EINVAL);
408}
409
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700410static inline int desc_to_gpio(const struct gpio_desc *desc)
411{
412 /* GPIO can never have been requested */
413 WARN_ON(1);
414 return -EINVAL;
415}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700416
Geert Uytterhoeven496e7ce2015-05-07 01:08:08 -0700417/* Child properties interface */
418struct fwnode_handle;
419
420static inline struct gpio_desc *fwnode_get_named_gpiod(
421 struct fwnode_handle *fwnode, const char *propname)
422{
423 return ERR_PTR(-ENOSYS);
424}
425
426static inline struct gpio_desc *devm_get_gpiod_from_child(
427 struct device *dev, const char *con_id, struct fwnode_handle *child)
428{
429 return ERR_PTR(-ENOSYS);
430}
431
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700432#endif /* CONFIG_GPIOLIB */
433
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700434#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
435
436int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
437int gpiod_export_link(struct device *dev, const char *name,
438 struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700439void gpiod_unexport(struct gpio_desc *desc);
440
441#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
442
443static inline int gpiod_export(struct gpio_desc *desc,
444 bool direction_may_change)
445{
446 return -ENOSYS;
447}
448
449static inline int gpiod_export_link(struct device *dev, const char *name,
450 struct gpio_desc *desc)
451{
452 return -ENOSYS;
453}
454
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700455static inline void gpiod_unexport(struct gpio_desc *desc)
456{
457}
458
459#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
460
461#endif