blob: 00b1b70d68ba9fffd417142426c88a040d6320eb [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 Courbot79a9bec2013-10-17 10:21:36 -070069int gpiod_get_direction(const struct gpio_desc *desc);
70int 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);
77int gpiod_get_raw_value(const struct gpio_desc *desc);
78void gpiod_set_raw_value(struct gpio_desc *desc, int value);
79
80/* Value get/set from sleeping context */
81int gpiod_get_value_cansleep(const struct gpio_desc *desc);
82void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
83int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
84void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
85
86int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
87
88int gpiod_is_active_low(const struct gpio_desc *desc);
89int gpiod_cansleep(const struct gpio_desc *desc);
90
91int gpiod_to_irq(const struct gpio_desc *desc);
92
93/* Convert between the old gpio_ and new gpiod_ interfaces */
94struct gpio_desc *gpio_to_desc(unsigned gpio);
95int desc_to_gpio(const struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070096
Mika Westerberg40b73182014-10-21 13:33:59 +020097/* Child properties interface */
98struct fwnode_handle;
99
100struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
101 const char *propname);
102struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
103 struct fwnode_handle *child);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700104#else /* CONFIG_GPIOLIB */
105
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200106static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
107 const char *con_id,
108 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700109{
110 return ERR_PTR(-ENOSYS);
111}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200112static inline struct gpio_desc *__must_check
113__gpiod_get_index(struct device *dev,
114 const char *con_id,
115 unsigned int idx,
116 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700117{
118 return ERR_PTR(-ENOSYS);
119}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200120
121static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200122__gpiod_get_optional(struct device *dev, const char *con_id,
123 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200124{
125 return ERR_PTR(-ENOSYS);
126}
127
128static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200129__gpiod_get_index_optional(struct device *dev, const char *con_id,
130 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200131{
132 return ERR_PTR(-ENOSYS);
133}
134
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700135static inline void gpiod_put(struct gpio_desc *desc)
136{
137 might_sleep();
138
139 /* GPIO can never have been requested */
140 WARN_ON(1);
141}
142
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200143static inline struct gpio_desc *__must_check
144__devm_gpiod_get(struct device *dev,
145 const char *con_id,
146 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700147{
148 return ERR_PTR(-ENOSYS);
149}
150static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200151struct gpio_desc *__must_check
152__devm_gpiod_get_index(struct device *dev,
153 const char *con_id,
154 unsigned int idx,
155 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700156{
157 return ERR_PTR(-ENOSYS);
158}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200159
160static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200161__devm_gpiod_get_optional(struct device *dev, const char *con_id,
162 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200163{
164 return ERR_PTR(-ENOSYS);
165}
166
167static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200168__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
169 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200170{
171 return ERR_PTR(-ENOSYS);
172}
173
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700174static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
175{
176 might_sleep();
177
178 /* GPIO can never have been requested */
179 WARN_ON(1);
180}
181
182
183static inline int gpiod_get_direction(const struct gpio_desc *desc)
184{
185 /* GPIO can never have been requested */
186 WARN_ON(1);
187 return -ENOSYS;
188}
189static inline int gpiod_direction_input(struct gpio_desc *desc)
190{
191 /* GPIO can never have been requested */
192 WARN_ON(1);
193 return -ENOSYS;
194}
195static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
196{
197 /* GPIO can never have been requested */
198 WARN_ON(1);
199 return -ENOSYS;
200}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100201static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
202{
203 /* GPIO can never have been requested */
204 WARN_ON(1);
205 return -ENOSYS;
206}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700207
208
209static inline int gpiod_get_value(const struct gpio_desc *desc)
210{
211 /* GPIO can never have been requested */
212 WARN_ON(1);
213 return 0;
214}
215static inline void gpiod_set_value(struct gpio_desc *desc, int value)
216{
217 /* GPIO can never have been requested */
218 WARN_ON(1);
219}
220static inline int gpiod_get_raw_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_raw_value(struct gpio_desc *desc, int value)
227{
228 /* GPIO can never have been requested */
229 WARN_ON(1);
230}
231
232static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
233{
234 /* GPIO can never have been requested */
235 WARN_ON(1);
236 return 0;
237}
238static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
239{
240 /* GPIO can never have been requested */
241 WARN_ON(1);
242}
243static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
244{
245 /* GPIO can never have been requested */
246 WARN_ON(1);
247 return 0;
248}
249static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
250 int value)
251{
252 /* GPIO can never have been requested */
253 WARN_ON(1);
254}
255
256static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
257{
258 /* GPIO can never have been requested */
259 WARN_ON(1);
260 return -ENOSYS;
261}
262
263static inline int gpiod_is_active_low(const struct gpio_desc *desc)
264{
265 /* GPIO can never have been requested */
266 WARN_ON(1);
267 return 0;
268}
269static inline int gpiod_cansleep(const struct gpio_desc *desc)
270{
271 /* GPIO can never have been requested */
272 WARN_ON(1);
273 return 0;
274}
275
276static inline int gpiod_to_irq(const struct gpio_desc *desc)
277{
278 /* GPIO can never have been requested */
279 WARN_ON(1);
280 return -EINVAL;
281}
282
283static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
284{
285 return ERR_PTR(-EINVAL);
286}
287static inline int desc_to_gpio(const struct gpio_desc *desc)
288{
289 /* GPIO can never have been requested */
290 WARN_ON(1);
291 return -EINVAL;
292}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700293
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700294#endif /* CONFIG_GPIOLIB */
295
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200296/*
297 * Vararg-hacks! This is done to transition the kernel to always pass
298 * the options flags argument to the below functions. During a transition
299 * phase these vararg macros make both old-and-newstyle code compile,
300 * but when all calls to the elder API are removed, these should go away
301 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
302 * etc.
303 */
304#define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
305#define gpiod_get(varargs...) __gpiod_get(varargs, 0)
306#define __gpiod_get_index(dev, con_id, index, flags, ...) \
307 __gpiod_get_index(dev, con_id, index, flags)
308#define gpiod_get_index(varargs...) __gpiod_get_index(varargs, 0)
309#define __gpiod_get_optional(dev, con_id, flags, ...) \
310 __gpiod_get_optional(dev, con_id, flags)
311#define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, 0)
312#define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
313 __gpiod_get_index_optional(dev, con_id, index, flags)
314#define gpiod_get_index_optional(varargs...) \
315 __gpiod_get_index_optional(varargs, 0)
316#define __devm_gpiod_get(dev, con_id, flags, ...) \
317 __devm_gpiod_get(dev, con_id, flags)
318#define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, 0)
319#define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
320 __devm_gpiod_get_index(dev, con_id, index, flags)
321#define devm_gpiod_get_index(varargs...) __devm_gpiod_get_index(varargs, 0)
322#define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
323 __devm_gpiod_get_optional(dev, con_id, flags)
324#define devm_gpiod_get_optional(varargs...) \
325 __devm_gpiod_get_optional(varargs, 0)
326#define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
327 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
328#define devm_gpiod_get_index_optional(varargs...) \
329 __devm_gpiod_get_index_optional(varargs, 0)
330
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700331#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
332
333int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
334int gpiod_export_link(struct device *dev, const char *name,
335 struct gpio_desc *desc);
336int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
337void gpiod_unexport(struct gpio_desc *desc);
338
339#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
340
341static inline int gpiod_export(struct gpio_desc *desc,
342 bool direction_may_change)
343{
344 return -ENOSYS;
345}
346
347static inline int gpiod_export_link(struct device *dev, const char *name,
348 struct gpio_desc *desc)
349{
350 return -ENOSYS;
351}
352
353static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
354{
355 return -ENOSYS;
356}
357
358static inline void gpiod_unexport(struct gpio_desc *desc)
359{
360}
361
362#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
363
364#endif