blob: bed128e8f4b10c318ff6439933b04dc0845660f9 [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);
26void gpiod_put(struct gpio_desc *desc);
27
28struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
29 const char *con_id);
30struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
31 const char *con_id,
32 unsigned int idx);
33void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
34
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070035int gpiod_get_direction(const struct gpio_desc *desc);
36int gpiod_direction_input(struct gpio_desc *desc);
37int gpiod_direction_output(struct gpio_desc *desc, int value);
Philipp Zabelef70bbe2014-01-07 12:34:11 +010038int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070039
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);
Alexandre Courbot79a9bec2013-10-17 10:21:36 -070062
63#else /* CONFIG_GPIOLIB */
64
65static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
66 const char *con_id)
67{
68 return ERR_PTR(-ENOSYS);
69}
70static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
71 const char *con_id,
72 unsigned int idx)
73{
74 return ERR_PTR(-ENOSYS);
75}
76static inline void gpiod_put(struct gpio_desc *desc)
77{
78 might_sleep();
79
80 /* GPIO can never have been requested */
81 WARN_ON(1);
82}
83
84static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
85 const char *con_id)
86{
87 return ERR_PTR(-ENOSYS);
88}
89static inline
90struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
91 const char *con_id,
92 unsigned int idx)
93{
94 return ERR_PTR(-ENOSYS);
95}
96static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
97{
98 might_sleep();
99
100 /* GPIO can never have been requested */
101 WARN_ON(1);
102}
103
104
105static inline int gpiod_get_direction(const struct gpio_desc *desc)
106{
107 /* GPIO can never have been requested */
108 WARN_ON(1);
109 return -ENOSYS;
110}
111static inline int gpiod_direction_input(struct gpio_desc *desc)
112{
113 /* GPIO can never have been requested */
114 WARN_ON(1);
115 return -ENOSYS;
116}
117static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
118{
119 /* GPIO can never have been requested */
120 WARN_ON(1);
121 return -ENOSYS;
122}
Philipp Zabelef70bbe2014-01-07 12:34:11 +0100123static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
124{
125 /* GPIO can never have been requested */
126 WARN_ON(1);
127 return -ENOSYS;
128}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700129
130
131static inline int gpiod_get_value(const struct gpio_desc *desc)
132{
133 /* GPIO can never have been requested */
134 WARN_ON(1);
135 return 0;
136}
137static inline void gpiod_set_value(struct gpio_desc *desc, int value)
138{
139 /* GPIO can never have been requested */
140 WARN_ON(1);
141}
142static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
143{
144 /* GPIO can never have been requested */
145 WARN_ON(1);
146 return 0;
147}
148static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
149{
150 /* GPIO can never have been requested */
151 WARN_ON(1);
152}
153
154static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
155{
156 /* GPIO can never have been requested */
157 WARN_ON(1);
158 return 0;
159}
160static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
161{
162 /* GPIO can never have been requested */
163 WARN_ON(1);
164}
165static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
166{
167 /* GPIO can never have been requested */
168 WARN_ON(1);
169 return 0;
170}
171static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
172 int value)
173{
174 /* GPIO can never have been requested */
175 WARN_ON(1);
176}
177
178static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
179{
180 /* GPIO can never have been requested */
181 WARN_ON(1);
182 return -ENOSYS;
183}
184
185static inline int gpiod_is_active_low(const struct gpio_desc *desc)
186{
187 /* GPIO can never have been requested */
188 WARN_ON(1);
189 return 0;
190}
191static inline int gpiod_cansleep(const struct gpio_desc *desc)
192{
193 /* GPIO can never have been requested */
194 WARN_ON(1);
195 return 0;
196}
197
198static inline int gpiod_to_irq(const struct gpio_desc *desc)
199{
200 /* GPIO can never have been requested */
201 WARN_ON(1);
202 return -EINVAL;
203}
204
205static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
206{
207 return ERR_PTR(-EINVAL);
208}
209static inline int desc_to_gpio(const struct gpio_desc *desc)
210{
211 /* GPIO can never have been requested */
212 WARN_ON(1);
213 return -EINVAL;
214}
Alexandre Courbot79a9bec2013-10-17 10:21:36 -0700215
216
217#endif /* CONFIG_GPIOLIB */
218
219#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
220
221int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
222int gpiod_export_link(struct device *dev, const char *name,
223 struct gpio_desc *desc);
224int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
225void gpiod_unexport(struct gpio_desc *desc);
226
227#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
228
229static inline int gpiod_export(struct gpio_desc *desc,
230 bool direction_may_change)
231{
232 return -ENOSYS;
233}
234
235static inline int gpiod_export_link(struct device *dev, const char *name,
236 struct gpio_desc *desc)
237{
238 return -ENOSYS;
239}
240
241static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
242{
243 return -ENOSYS;
244}
245
246static inline void gpiod_unexport(struct gpio_desc *desc)
247{
248}
249
250#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
251
252#endif