blob: 6a37ef0dc59cb1da029621a216de795b98963556 [file] [log] [blame]
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07001#ifndef __LINUX_GPIO_CONSUMER_H
2#define __LINUX_GPIO_CONSUMER_H
3
4#include <linux/err.h>
5#include <linux/kernel.h>
6
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07007struct device;
Alexandre Courbot79a9bec2013-10-17 10:21:36 -07008
9/**
10 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
11 * preferable to the old integer-based handles.
12 *
13 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
14 * until the GPIO is released.
15 */
16struct gpio_desc;
17
Lars-Peter Clausena3485d02014-02-04 13:42:10 +010018#ifdef CONFIG_GPIOLIB
19
Alexandre Courbotbae48da2013-10-17 10:21:38 -070020/* Acquire and dispose GPIOs */
21struct gpio_desc *__must_check gpiod_get(struct device *dev,
22 const char *con_id);
23struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
24 const char *con_id,
25 unsigned int idx);
Thierry Reding29a1f2332014-04-25 17:10:06 +020026struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
27 const char *con_id);
28struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
29 const char *con_id,
30 unsigned int index);
31
Alexandre Courbotbae48da2013-10-17 10:21:38 -070032void gpiod_put(struct gpio_desc *desc);
33
34struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
35 const char *con_id);
36struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
37 const char *con_id,
38 unsigned int idx);
Thierry Reding29a1f2332014-04-25 17:10:06 +020039struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
40 const char *con_id);
41struct gpio_desc *__must_check
42devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
43 unsigned int index);
44
Alexandre Courbotbae48da2013-10-17 10:21:38 -070045void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
46
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070047int gpiod_get_direction(const struct gpio_desc *desc);
48int gpiod_direction_input(struct gpio_desc *desc);
49int gpiod_direction_output(struct gpio_desc *desc, int value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +010050int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070051
52/* Value get/set from non-sleeping context */
53int gpiod_get_value(const struct gpio_desc *desc);
54void gpiod_set_value(struct gpio_desc *desc, int value);
55int gpiod_get_raw_value(const struct gpio_desc *desc);
56void gpiod_set_raw_value(struct gpio_desc *desc, int value);
57
58/* Value get/set from sleeping context */
59int gpiod_get_value_cansleep(const struct gpio_desc *desc);
60void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
61int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
62void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
63
64int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
65
66int gpiod_is_active_low(const struct gpio_desc *desc);
67int gpiod_cansleep(const struct gpio_desc *desc);
68
69int gpiod_to_irq(const struct gpio_desc *desc);
70
71/* Convert between the old gpio_ and new gpiod_ interfaces */
72struct gpio_desc *gpio_to_desc(unsigned gpio);
73int desc_to_gpio(const struct gpio_desc *desc);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070074
75#else /* CONFIG_GPIOLIB */
76
77static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
78 const char *con_id)
79{
80 return ERR_PTR(-ENOSYS);
81}
82static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
83 const char *con_id,
84 unsigned int idx)
85{
86 return ERR_PTR(-ENOSYS);
87}
Thierry Reding29a1f2332014-04-25 17:10:06 +020088
89static inline struct gpio_desc *__must_check
90gpiod_get_optional(struct device *dev, const char *con_id)
91{
92 return ERR_PTR(-ENOSYS);
93}
94
95static inline struct gpio_desc *__must_check
96gpiod_get_index_optional(struct device *dev, const char *con_id,
97 unsigned int index)
98{
99 return ERR_PTR(-ENOSYS);
100}
101
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700102static inline void gpiod_put(struct gpio_desc *desc)
103{
104 might_sleep();
105
106 /* GPIO can never have been requested */
107 WARN_ON(1);
108}
109
110static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
111 const char *con_id)
112{
113 return ERR_PTR(-ENOSYS);
114}
115static inline
116struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
117 const char *con_id,
118 unsigned int idx)
119{
120 return ERR_PTR(-ENOSYS);
121}
Thierry Reding29a1f2332014-04-25 17:10:06 +0200122
123static inline struct gpio_desc *__must_check
124devm_gpiod_get_optional(struct device *dev, const char *con_id)
125{
126 return ERR_PTR(-ENOSYS);
127}
128
129static inline struct gpio_desc *__must_check
130devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
131 unsigned int index)
132{
133 return ERR_PTR(-ENOSYS);
134}
135
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700136static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
137{
138 might_sleep();
139
140 /* GPIO can never have been requested */
141 WARN_ON(1);
142}
143
144
145static inline int gpiod_get_direction(const struct gpio_desc *desc)
146{
147 /* GPIO can never have been requested */
148 WARN_ON(1);
149 return -ENOSYS;
150}
151static inline int gpiod_direction_input(struct gpio_desc *desc)
152{
153 /* GPIO can never have been requested */
154 WARN_ON(1);
155 return -ENOSYS;
156}
157static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
158{
159 /* GPIO can never have been requested */
160 WARN_ON(1);
161 return -ENOSYS;
162}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100163static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
164{
165 /* GPIO can never have been requested */
166 WARN_ON(1);
167 return -ENOSYS;
168}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700169
170
171static inline int gpiod_get_value(const struct gpio_desc *desc)
172{
173 /* GPIO can never have been requested */
174 WARN_ON(1);
175 return 0;
176}
177static inline void gpiod_set_value(struct gpio_desc *desc, int value)
178{
179 /* GPIO can never have been requested */
180 WARN_ON(1);
181}
182static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
183{
184 /* GPIO can never have been requested */
185 WARN_ON(1);
186 return 0;
187}
188static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
189{
190 /* GPIO can never have been requested */
191 WARN_ON(1);
192}
193
194static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
195{
196 /* GPIO can never have been requested */
197 WARN_ON(1);
198 return 0;
199}
200static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
201{
202 /* GPIO can never have been requested */
203 WARN_ON(1);
204}
205static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
206{
207 /* GPIO can never have been requested */
208 WARN_ON(1);
209 return 0;
210}
211static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
212 int value)
213{
214 /* GPIO can never have been requested */
215 WARN_ON(1);
216}
217
218static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
219{
220 /* GPIO can never have been requested */
221 WARN_ON(1);
222 return -ENOSYS;
223}
224
225static inline int gpiod_is_active_low(const struct gpio_desc *desc)
226{
227 /* GPIO can never have been requested */
228 WARN_ON(1);
229 return 0;
230}
231static inline int gpiod_cansleep(const struct gpio_desc *desc)
232{
233 /* GPIO can never have been requested */
234 WARN_ON(1);
235 return 0;
236}
237
238static inline int gpiod_to_irq(const struct gpio_desc *desc)
239{
240 /* GPIO can never have been requested */
241 WARN_ON(1);
242 return -EINVAL;
243}
244
245static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
246{
247 return ERR_PTR(-EINVAL);
248}
249static inline int desc_to_gpio(const struct gpio_desc *desc)
250{
251 /* GPIO can never have been requested */
252 WARN_ON(1);
253 return -EINVAL;
254}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700255
256
257#endif /* CONFIG_GPIOLIB */
258
259#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
260
261int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
262int gpiod_export_link(struct device *dev, const char *name,
263 struct gpio_desc *desc);
264int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
265void gpiod_unexport(struct gpio_desc *desc);
266
267#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
268
269static inline int gpiod_export(struct gpio_desc *desc,
270 bool direction_may_change)
271{
272 return -ENOSYS;
273}
274
275static inline int gpiod_export_link(struct device *dev, const char *name,
276 struct gpio_desc *desc)
277{
278 return -ENOSYS;
279}
280
281static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
282{
283 return -ENOSYS;
284}
285
286static inline void gpiod_unexport(struct gpio_desc *desc)
287{
288}
289
290#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
291
292#endif