blob: 7a8144fef4065fab8505ffcfe2482cc0b60f3cbb [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;
8struct gpio_chip;
9
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
Lars-Peter Clausena3485d02014-02-04 13:42:10 +010019#ifdef CONFIG_GPIOLIB
20
Alexandre Courbotbae48da2013-10-17 10:21:38 -070021/* Acquire and dispose GPIOs */
22struct gpio_desc *__must_check gpiod_get(struct device *dev,
23 const char *con_id);
24struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
25 const char *con_id,
26 unsigned int idx);
27void gpiod_put(struct gpio_desc *desc);
28
29struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
30 const char *con_id);
31struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
32 const char *con_id,
33 unsigned int idx);
34void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
35
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070036int gpiod_get_direction(const struct gpio_desc *desc);
37int gpiod_direction_input(struct gpio_desc *desc);
38int gpiod_direction_output(struct gpio_desc *desc, int value);
39
40/* Value get/set from non-sleeping context */
41int gpiod_get_value(const struct gpio_desc *desc);
42void gpiod_set_value(struct gpio_desc *desc, int value);
43int gpiod_get_raw_value(const struct gpio_desc *desc);
44void gpiod_set_raw_value(struct gpio_desc *desc, int value);
45
46/* Value get/set from sleeping context */
47int gpiod_get_value_cansleep(const struct gpio_desc *desc);
48void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
49int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
50void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
51
52int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
53
54int gpiod_is_active_low(const struct gpio_desc *desc);
55int gpiod_cansleep(const struct gpio_desc *desc);
56
57int gpiod_to_irq(const struct gpio_desc *desc);
58
59/* Convert between the old gpio_ and new gpiod_ interfaces */
60struct gpio_desc *gpio_to_desc(unsigned gpio);
61int desc_to_gpio(const struct gpio_desc *desc);
62struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
63
64#else /* CONFIG_GPIOLIB */
65
66static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
67 const char *con_id)
68{
69 return ERR_PTR(-ENOSYS);
70}
71static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
72 const char *con_id,
73 unsigned int idx)
74{
75 return ERR_PTR(-ENOSYS);
76}
77static inline void gpiod_put(struct gpio_desc *desc)
78{
79 might_sleep();
80
81 /* GPIO can never have been requested */
82 WARN_ON(1);
83}
84
85static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
86 const char *con_id)
87{
88 return ERR_PTR(-ENOSYS);
89}
90static inline
91struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
92 const char *con_id,
93 unsigned int idx)
94{
95 return ERR_PTR(-ENOSYS);
96}
97static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
98{
99 might_sleep();
100
101 /* GPIO can never have been requested */
102 WARN_ON(1);
103}
104
105
106static inline int gpiod_get_direction(const struct gpio_desc *desc)
107{
108 /* GPIO can never have been requested */
109 WARN_ON(1);
110 return -ENOSYS;
111}
112static inline int gpiod_direction_input(struct gpio_desc *desc)
113{
114 /* GPIO can never have been requested */
115 WARN_ON(1);
116 return -ENOSYS;
117}
118static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
119{
120 /* GPIO can never have been requested */
121 WARN_ON(1);
122 return -ENOSYS;
123}
124
125
126static inline int gpiod_get_value(const struct gpio_desc *desc)
127{
128 /* GPIO can never have been requested */
129 WARN_ON(1);
130 return 0;
131}
132static inline void gpiod_set_value(struct gpio_desc *desc, int value)
133{
134 /* GPIO can never have been requested */
135 WARN_ON(1);
136}
137static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
138{
139 /* GPIO can never have been requested */
140 WARN_ON(1);
141 return 0;
142}
143static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
144{
145 /* GPIO can never have been requested */
146 WARN_ON(1);
147}
148
149static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
150{
151 /* GPIO can never have been requested */
152 WARN_ON(1);
153 return 0;
154}
155static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
156{
157 /* GPIO can never have been requested */
158 WARN_ON(1);
159}
160static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
161{
162 /* GPIO can never have been requested */
163 WARN_ON(1);
164 return 0;
165}
166static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
167 int value)
168{
169 /* GPIO can never have been requested */
170 WARN_ON(1);
171}
172
173static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
174{
175 /* GPIO can never have been requested */
176 WARN_ON(1);
177 return -ENOSYS;
178}
179
180static inline int gpiod_is_active_low(const struct gpio_desc *desc)
181{
182 /* GPIO can never have been requested */
183 WARN_ON(1);
184 return 0;
185}
186static inline int gpiod_cansleep(const struct gpio_desc *desc)
187{
188 /* GPIO can never have been requested */
189 WARN_ON(1);
190 return 0;
191}
192
193static inline int gpiod_to_irq(const struct gpio_desc *desc)
194{
195 /* GPIO can never have been requested */
196 WARN_ON(1);
197 return -EINVAL;
198}
199
200static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
201{
202 return ERR_PTR(-EINVAL);
203}
204static inline int desc_to_gpio(const struct gpio_desc *desc)
205{
206 /* GPIO can never have been requested */
207 WARN_ON(1);
208 return -EINVAL;
209}
210static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
211{
212 /* GPIO can never have been requested */
213 WARN_ON(1);
214 return ERR_PTR(-ENODEV);
215}
216
217
218#endif /* CONFIG_GPIOLIB */
219
220#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
221
222int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
223int gpiod_export_link(struct device *dev, const char *name,
224 struct gpio_desc *desc);
225int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
226void gpiod_unexport(struct gpio_desc *desc);
227
228#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
229
230static inline int gpiod_export(struct gpio_desc *desc,
231 bool direction_may_change)
232{
233 return -ENOSYS;
234}
235
236static inline int gpiod_export_link(struct device *dev, const char *name,
237 struct gpio_desc *desc)
238{
239 return -ENOSYS;
240}
241
242static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
243{
244 return -ENOSYS;
245}
246
247static inline void gpiod_unexport(struct gpio_desc *desc)
248{
249}
250
251#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
252
253#endif