blob: ed20759229eb5e5dedbbd4ddad158081ec248f1c [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
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090019#define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
20#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
21#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
22
23/**
24 * Optional flags that can be passed to one of gpiod_* to configure direction
25 * and output value. These values cannot be OR'd.
26 */
27enum gpiod_flags {
28 GPIOD_ASIS = 0,
29 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
30 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
31 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
32 GPIOD_FLAGS_BIT_DIR_VAL,
33};
34
Linus Walleij58b84f62014-08-19 12:00:53 -050035#ifdef CONFIG_GPIOLIB
36
Alexandre Courbotbae48da2013-10-17 10:21:38 -070037/* Acquire and dispose GPIOs */
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090038struct gpio_desc *__must_check __gpiod_get(struct device *dev,
39 const char *con_id,
40 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090041struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -070042 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090043 unsigned int idx,
44 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090045struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
46 const char *con_id,
47 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090048struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
Thierry Reding29a1f2332014-04-25 17:10:06 +020049 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090050 unsigned int index,
51 enum gpiod_flags flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070052void gpiod_put(struct gpio_desc *desc);
53
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090054struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
55 const char *con_id,
56 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090057struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
Alexandre Courbotbae48da2013-10-17 10:21:38 -070058 const char *con_id,
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090059 unsigned int idx,
60 enum gpiod_flags flags);
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090061struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
62 const char *con_id,
63 enum gpiod_flags flags);
Thierry Reding29a1f2332014-04-25 17:10:06 +020064struct gpio_desc *__must_check
Alexandre Courbot39b2bbe2014-07-25 23:38:36 +090065__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
66 unsigned int index, enum gpiod_flags flags);
Alexandre Courbotbae48da2013-10-17 10:21:38 -070067void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
68
Alexandre Courbot8e53b0f2014-11-25 17:16:31 +090069int gpiod_get_direction(struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070070int gpiod_direction_input(struct gpio_desc *desc);
71int gpiod_direction_output(struct gpio_desc *desc, int value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +010072int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070073
74/* Value get/set from non-sleeping context */
75int gpiod_get_value(const struct gpio_desc *desc);
76void gpiod_set_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010077void gpiod_set_array(unsigned int array_size,
78 struct gpio_desc **desc_array, int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070079int gpiod_get_raw_value(const struct gpio_desc *desc);
80void gpiod_set_raw_value(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010081void gpiod_set_raw_array(unsigned int array_size,
82 struct gpio_desc **desc_array, int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070083
84/* Value get/set from sleeping context */
85int gpiod_get_value_cansleep(const struct gpio_desc *desc);
86void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010087void gpiod_set_array_cansleep(unsigned int array_size,
88 struct gpio_desc **desc_array,
89 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070090int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
91void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +010092void gpiod_set_raw_array_cansleep(unsigned int array_size,
93 struct gpio_desc **desc_array,
94 int *value_array);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070095
96int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
97
98int gpiod_is_active_low(const struct gpio_desc *desc);
99int gpiod_cansleep(const struct gpio_desc *desc);
100
101int gpiod_to_irq(const struct gpio_desc *desc);
102
103/* Convert between the old gpio_ and new gpiod_ interfaces */
104struct gpio_desc *gpio_to_desc(unsigned gpio);
105int desc_to_gpio(const struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700106
Mika Westerberg40b73182014-10-21 13:33:59 +0200107/* Child properties interface */
108struct fwnode_handle;
109
110struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
111 const char *propname);
112struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
Olliver Schinagl1feb57a2015-01-21 22:33:46 +0100113 const char *con_id,
Mika Westerberg40b73182014-10-21 13:33:59 +0200114 struct fwnode_handle *child);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700115#else /* CONFIG_GPIOLIB */
116
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200117static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
118 const char *con_id,
119 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700120{
121 return ERR_PTR(-ENOSYS);
122}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200123static inline struct gpio_desc *__must_check
124__gpiod_get_index(struct device *dev,
125 const char *con_id,
126 unsigned int idx,
127 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700128{
129 return ERR_PTR(-ENOSYS);
130}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200131
132static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200133__gpiod_get_optional(struct device *dev, const char *con_id,
134 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200135{
136 return ERR_PTR(-ENOSYS);
137}
138
139static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200140__gpiod_get_index_optional(struct device *dev, const char *con_id,
141 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200142{
143 return ERR_PTR(-ENOSYS);
144}
145
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700146static inline void gpiod_put(struct gpio_desc *desc)
147{
148 might_sleep();
149
150 /* GPIO can never have been requested */
151 WARN_ON(1);
152}
153
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200154static inline struct gpio_desc *__must_check
155__devm_gpiod_get(struct device *dev,
156 const char *con_id,
157 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700158{
159 return ERR_PTR(-ENOSYS);
160}
161static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200162struct gpio_desc *__must_check
163__devm_gpiod_get_index(struct device *dev,
164 const char *con_id,
165 unsigned int idx,
166 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700167{
168 return ERR_PTR(-ENOSYS);
169}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200170
171static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200172__devm_gpiod_get_optional(struct device *dev, const char *con_id,
173 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200174{
175 return ERR_PTR(-ENOSYS);
176}
177
178static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200179__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
180 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200181{
182 return ERR_PTR(-ENOSYS);
183}
184
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700185static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
186{
187 might_sleep();
188
189 /* GPIO can never have been requested */
190 WARN_ON(1);
191}
192
193
194static inline int gpiod_get_direction(const struct gpio_desc *desc)
195{
196 /* GPIO can never have been requested */
197 WARN_ON(1);
198 return -ENOSYS;
199}
200static inline int gpiod_direction_input(struct gpio_desc *desc)
201{
202 /* GPIO can never have been requested */
203 WARN_ON(1);
204 return -ENOSYS;
205}
206static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
207{
208 /* GPIO can never have been requested */
209 WARN_ON(1);
210 return -ENOSYS;
211}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100212static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
213{
214 /* GPIO can never have been requested */
215 WARN_ON(1);
216 return -ENOSYS;
217}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700218
219
220static inline int gpiod_get_value(const struct gpio_desc *desc)
221{
222 /* GPIO can never have been requested */
223 WARN_ON(1);
224 return 0;
225}
226static inline void gpiod_set_value(struct gpio_desc *desc, int value)
227{
228 /* GPIO can never have been requested */
229 WARN_ON(1);
230}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100231static inline void gpiod_set_array(unsigned int array_size,
232 struct gpio_desc **desc_array,
233 int *value_array)
234{
235 /* GPIO can never have been requested */
236 WARN_ON(1);
237}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700238static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
239{
240 /* GPIO can never have been requested */
241 WARN_ON(1);
242 return 0;
243}
244static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
245{
246 /* GPIO can never have been requested */
247 WARN_ON(1);
248}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100249static inline void gpiod_set_raw_array(unsigned int array_size,
250 struct gpio_desc **desc_array,
251 int *value_array)
252{
253 /* GPIO can never have been requested */
254 WARN_ON(1);
255}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700256
257static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
258{
259 /* GPIO can never have been requested */
260 WARN_ON(1);
261 return 0;
262}
263static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
264{
265 /* GPIO can never have been requested */
266 WARN_ON(1);
267}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100268static inline void gpiod_set_array_cansleep(unsigned int array_size,
269 struct gpio_desc **desc_array,
270 int *value_array)
271{
272 /* GPIO can never have been requested */
273 WARN_ON(1);
274}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700275static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
276{
277 /* GPIO can never have been requested */
278 WARN_ON(1);
279 return 0;
280}
281static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
282 int value)
283{
284 /* GPIO can never have been requested */
285 WARN_ON(1);
286}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100287static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
288 struct gpio_desc **desc_array,
289 int *value_array)
290{
291 /* GPIO can never have been requested */
292 WARN_ON(1);
293}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700294
295static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
296{
297 /* GPIO can never have been requested */
298 WARN_ON(1);
299 return -ENOSYS;
300}
301
302static inline int gpiod_is_active_low(const struct gpio_desc *desc)
303{
304 /* GPIO can never have been requested */
305 WARN_ON(1);
306 return 0;
307}
308static inline int gpiod_cansleep(const struct gpio_desc *desc)
309{
310 /* GPIO can never have been requested */
311 WARN_ON(1);
312 return 0;
313}
314
315static inline int gpiod_to_irq(const struct gpio_desc *desc)
316{
317 /* GPIO can never have been requested */
318 WARN_ON(1);
319 return -EINVAL;
320}
321
322static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
323{
324 return ERR_PTR(-EINVAL);
325}
326static inline int desc_to_gpio(const struct gpio_desc *desc)
327{
328 /* GPIO can never have been requested */
329 WARN_ON(1);
330 return -EINVAL;
331}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700332
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700333#endif /* CONFIG_GPIOLIB */
334
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200335/*
336 * Vararg-hacks! This is done to transition the kernel to always pass
337 * the options flags argument to the below functions. During a transition
338 * phase these vararg macros make both old-and-newstyle code compile,
339 * but when all calls to the elder API are removed, these should go away
340 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
341 * etc.
342 */
343#define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100344#define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200345#define __gpiod_get_index(dev, con_id, index, flags, ...) \
346 __gpiod_get_index(dev, con_id, index, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100347#define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200348#define __gpiod_get_optional(dev, con_id, flags, ...) \
349 __gpiod_get_optional(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100350#define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200351#define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
352 __gpiod_get_index_optional(dev, con_id, index, flags)
353#define gpiod_get_index_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100354 __gpiod_get_index_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200355#define __devm_gpiod_get(dev, con_id, flags, ...) \
356 __devm_gpiod_get(dev, con_id, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100357#define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200358#define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
359 __devm_gpiod_get_index(dev, con_id, index, flags)
Olliver Schinagld34541b2015-01-07 09:44:57 +0100360#define devm_gpiod_get_index(varargs...) \
361 __devm_gpiod_get_index(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200362#define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
363 __devm_gpiod_get_optional(dev, con_id, flags)
364#define devm_gpiod_get_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100365 __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200366#define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
367 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
368#define devm_gpiod_get_index_optional(varargs...) \
Olliver Schinagld34541b2015-01-07 09:44:57 +0100369 __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200370
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700371#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
372
373int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
374int gpiod_export_link(struct device *dev, const char *name,
375 struct gpio_desc *desc);
376int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
377void gpiod_unexport(struct gpio_desc *desc);
378
379#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
380
381static inline int gpiod_export(struct gpio_desc *desc,
382 bool direction_may_change)
383{
384 return -ENOSYS;
385}
386
387static inline int gpiod_export_link(struct device *dev, const char *name,
388 struct gpio_desc *desc)
389{
390 return -ENOSYS;
391}
392
393static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
394{
395 return -ENOSYS;
396}
397
398static inline void gpiod_unexport(struct gpio_desc *desc)
399{
400}
401
402#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
403
404#endif