blob: fd85cb120ee0be6d3957b15f7cc8ffeadf7d84c2 [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,
113 struct fwnode_handle *child);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700114#else /* CONFIG_GPIOLIB */
115
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200116static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
117 const char *con_id,
118 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700119{
120 return ERR_PTR(-ENOSYS);
121}
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200122static inline struct gpio_desc *__must_check
123__gpiod_get_index(struct device *dev,
124 const char *con_id,
125 unsigned int idx,
126 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700127{
128 return ERR_PTR(-ENOSYS);
129}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200130
131static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200132__gpiod_get_optional(struct device *dev, const char *con_id,
133 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200134{
135 return ERR_PTR(-ENOSYS);
136}
137
138static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200139__gpiod_get_index_optional(struct device *dev, const char *con_id,
140 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200141{
142 return ERR_PTR(-ENOSYS);
143}
144
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700145static inline void gpiod_put(struct gpio_desc *desc)
146{
147 might_sleep();
148
149 /* GPIO can never have been requested */
150 WARN_ON(1);
151}
152
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200153static inline struct gpio_desc *__must_check
154__devm_gpiod_get(struct device *dev,
155 const char *con_id,
156 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700157{
158 return ERR_PTR(-ENOSYS);
159}
160static inline
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200161struct gpio_desc *__must_check
162__devm_gpiod_get_index(struct device *dev,
163 const char *con_id,
164 unsigned int idx,
165 enum gpiod_flags flags)
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700166{
167 return ERR_PTR(-ENOSYS);
168}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200169
170static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200171__devm_gpiod_get_optional(struct device *dev, const char *con_id,
172 enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200173{
174 return ERR_PTR(-ENOSYS);
175}
176
177static inline struct gpio_desc *__must_check
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200178__devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
179 unsigned int index, enum gpiod_flags flags)
Thierry Reding29a1f2332014-04-25 17:10:06 +0200180{
181 return ERR_PTR(-ENOSYS);
182}
183
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700184static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
185{
186 might_sleep();
187
188 /* GPIO can never have been requested */
189 WARN_ON(1);
190}
191
192
193static inline int gpiod_get_direction(const struct gpio_desc *desc)
194{
195 /* GPIO can never have been requested */
196 WARN_ON(1);
197 return -ENOSYS;
198}
199static inline int gpiod_direction_input(struct gpio_desc *desc)
200{
201 /* GPIO can never have been requested */
202 WARN_ON(1);
203 return -ENOSYS;
204}
205static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
206{
207 /* GPIO can never have been requested */
208 WARN_ON(1);
209 return -ENOSYS;
210}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100211static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
212{
213 /* GPIO can never have been requested */
214 WARN_ON(1);
215 return -ENOSYS;
216}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700217
218
219static inline int gpiod_get_value(const struct gpio_desc *desc)
220{
221 /* GPIO can never have been requested */
222 WARN_ON(1);
223 return 0;
224}
225static inline void gpiod_set_value(struct gpio_desc *desc, int value)
226{
227 /* GPIO can never have been requested */
228 WARN_ON(1);
229}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100230static inline void gpiod_set_array(unsigned int array_size,
231 struct gpio_desc **desc_array,
232 int *value_array)
233{
234 /* GPIO can never have been requested */
235 WARN_ON(1);
236}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700237static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
238{
239 /* GPIO can never have been requested */
240 WARN_ON(1);
241 return 0;
242}
243static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
244{
245 /* GPIO can never have been requested */
246 WARN_ON(1);
247}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100248static inline void gpiod_set_raw_array(unsigned int array_size,
249 struct gpio_desc **desc_array,
250 int *value_array)
251{
252 /* GPIO can never have been requested */
253 WARN_ON(1);
254}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700255
256static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
257{
258 /* GPIO can never have been requested */
259 WARN_ON(1);
260 return 0;
261}
262static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
263{
264 /* GPIO can never have been requested */
265 WARN_ON(1);
266}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100267static inline void gpiod_set_array_cansleep(unsigned int array_size,
268 struct gpio_desc **desc_array,
269 int *value_array)
270{
271 /* GPIO can never have been requested */
272 WARN_ON(1);
273}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700274static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
275{
276 /* GPIO can never have been requested */
277 WARN_ON(1);
278 return 0;
279}
280static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
281 int value)
282{
283 /* GPIO can never have been requested */
284 WARN_ON(1);
285}
Rojhalat Ibrahim5f424242014-11-04 17:12:06 +0100286static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
287 struct gpio_desc **desc_array,
288 int *value_array)
289{
290 /* GPIO can never have been requested */
291 WARN_ON(1);
292}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700293
294static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
295{
296 /* GPIO can never have been requested */
297 WARN_ON(1);
298 return -ENOSYS;
299}
300
301static inline int gpiod_is_active_low(const struct gpio_desc *desc)
302{
303 /* GPIO can never have been requested */
304 WARN_ON(1);
305 return 0;
306}
307static inline int gpiod_cansleep(const struct gpio_desc *desc)
308{
309 /* GPIO can never have been requested */
310 WARN_ON(1);
311 return 0;
312}
313
314static inline int gpiod_to_irq(const struct gpio_desc *desc)
315{
316 /* GPIO can never have been requested */
317 WARN_ON(1);
318 return -EINVAL;
319}
320
321static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
322{
323 return ERR_PTR(-EINVAL);
324}
325static inline int desc_to_gpio(const struct gpio_desc *desc)
326{
327 /* GPIO can never have been requested */
328 WARN_ON(1);
329 return -EINVAL;
330}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700331
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700332#endif /* CONFIG_GPIOLIB */
333
Linus Walleij0dbc8b72014-09-01 15:15:40 +0200334/*
335 * Vararg-hacks! This is done to transition the kernel to always pass
336 * the options flags argument to the below functions. During a transition
337 * phase these vararg macros make both old-and-newstyle code compile,
338 * but when all calls to the elder API are removed, these should go away
339 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
340 * etc.
341 */
342#define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
343#define gpiod_get(varargs...) __gpiod_get(varargs, 0)
344#define __gpiod_get_index(dev, con_id, index, flags, ...) \
345 __gpiod_get_index(dev, con_id, index, flags)
346#define gpiod_get_index(varargs...) __gpiod_get_index(varargs, 0)
347#define __gpiod_get_optional(dev, con_id, flags, ...) \
348 __gpiod_get_optional(dev, con_id, flags)
349#define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, 0)
350#define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
351 __gpiod_get_index_optional(dev, con_id, index, flags)
352#define gpiod_get_index_optional(varargs...) \
353 __gpiod_get_index_optional(varargs, 0)
354#define __devm_gpiod_get(dev, con_id, flags, ...) \
355 __devm_gpiod_get(dev, con_id, flags)
356#define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, 0)
357#define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
358 __devm_gpiod_get_index(dev, con_id, index, flags)
359#define devm_gpiod_get_index(varargs...) __devm_gpiod_get_index(varargs, 0)
360#define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
361 __devm_gpiod_get_optional(dev, con_id, flags)
362#define devm_gpiod_get_optional(varargs...) \
363 __devm_gpiod_get_optional(varargs, 0)
364#define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
365 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
366#define devm_gpiod_get_index_optional(varargs...) \
367 __devm_gpiod_get_index_optional(varargs, 0)
368
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700369#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
370
371int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
372int gpiod_export_link(struct device *dev, const char *name,
373 struct gpio_desc *desc);
374int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
375void gpiod_unexport(struct gpio_desc *desc);
376
377#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
378
379static inline int gpiod_export(struct gpio_desc *desc,
380 bool direction_may_change)
381{
382 return -ENOSYS;
383}
384
385static inline int gpiod_export_link(struct device *dev, const char *name,
386 struct gpio_desc *desc)
387{
388 return -ENOSYS;
389}
390
391static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
392{
393 return -ENOSYS;
394}
395
396static inline void gpiod_unexport(struct gpio_desc *desc)
397{
398}
399
400#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
401
402#endif