]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/gpio/consumer.h
ed20759229eb5e5dedbbd4ddad158081ec248f1c
[karo-tx-linux.git] / include / linux / gpio / consumer.h
1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
3
4 #include <linux/bug.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7
8 struct device;
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  */
17 struct gpio_desc;
18
19 #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  */
27 enum 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
35 #ifdef CONFIG_GPIOLIB
36
37 /* Acquire and dispose GPIOs */
38 struct gpio_desc *__must_check __gpiod_get(struct device *dev,
39                                          const char *con_id,
40                                          enum gpiod_flags flags);
41 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
42                                                const char *con_id,
43                                                unsigned int idx,
44                                                enum gpiod_flags flags);
45 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
46                                                   const char *con_id,
47                                                   enum gpiod_flags flags);
48 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
49                                                         const char *con_id,
50                                                         unsigned int index,
51                                                         enum gpiod_flags flags);
52 void gpiod_put(struct gpio_desc *desc);
53
54 struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
55                                               const char *con_id,
56                                               enum gpiod_flags flags);
57 struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
58                                                     const char *con_id,
59                                                     unsigned int idx,
60                                                     enum gpiod_flags flags);
61 struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
62                                                        const char *con_id,
63                                                        enum gpiod_flags flags);
64 struct gpio_desc *__must_check
65 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
66                               unsigned int index, enum gpiod_flags flags);
67 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
68
69 int gpiod_get_direction(struct gpio_desc *desc);
70 int gpiod_direction_input(struct gpio_desc *desc);
71 int gpiod_direction_output(struct gpio_desc *desc, int value);
72 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
73
74 /* Value get/set from non-sleeping context */
75 int gpiod_get_value(const struct gpio_desc *desc);
76 void gpiod_set_value(struct gpio_desc *desc, int value);
77 void gpiod_set_array(unsigned int array_size,
78                      struct gpio_desc **desc_array, int *value_array);
79 int gpiod_get_raw_value(const struct gpio_desc *desc);
80 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
81 void gpiod_set_raw_array(unsigned int array_size,
82                          struct gpio_desc **desc_array, int *value_array);
83
84 /* Value get/set from sleeping context */
85 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
86 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
87 void gpiod_set_array_cansleep(unsigned int array_size,
88                               struct gpio_desc **desc_array,
89                               int *value_array);
90 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
91 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
92 void gpiod_set_raw_array_cansleep(unsigned int array_size,
93                                   struct gpio_desc **desc_array,
94                                   int *value_array);
95
96 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
97
98 int gpiod_is_active_low(const struct gpio_desc *desc);
99 int gpiod_cansleep(const struct gpio_desc *desc);
100
101 int gpiod_to_irq(const struct gpio_desc *desc);
102
103 /* Convert between the old gpio_ and new gpiod_ interfaces */
104 struct gpio_desc *gpio_to_desc(unsigned gpio);
105 int desc_to_gpio(const struct gpio_desc *desc);
106
107 /* Child properties interface */
108 struct fwnode_handle;
109
110 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
111                                          const char *propname);
112 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
113                                             const char *con_id,
114                                             struct fwnode_handle *child);
115 #else /* CONFIG_GPIOLIB */
116
117 static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
118                                                 const char *con_id,
119                                                 enum gpiod_flags flags)
120 {
121         return ERR_PTR(-ENOSYS);
122 }
123 static inline struct gpio_desc *__must_check
124 __gpiod_get_index(struct device *dev,
125                   const char *con_id,
126                   unsigned int idx,
127                   enum gpiod_flags flags)
128 {
129         return ERR_PTR(-ENOSYS);
130 }
131
132 static inline struct gpio_desc *__must_check
133 __gpiod_get_optional(struct device *dev, const char *con_id,
134                      enum gpiod_flags flags)
135 {
136         return ERR_PTR(-ENOSYS);
137 }
138
139 static inline struct gpio_desc *__must_check
140 __gpiod_get_index_optional(struct device *dev, const char *con_id,
141                            unsigned int index, enum gpiod_flags flags)
142 {
143         return ERR_PTR(-ENOSYS);
144 }
145
146 static inline void gpiod_put(struct gpio_desc *desc)
147 {
148         might_sleep();
149
150         /* GPIO can never have been requested */
151         WARN_ON(1);
152 }
153
154 static inline struct gpio_desc *__must_check
155 __devm_gpiod_get(struct device *dev,
156                  const char *con_id,
157                  enum gpiod_flags flags)
158 {
159         return ERR_PTR(-ENOSYS);
160 }
161 static inline
162 struct gpio_desc *__must_check
163 __devm_gpiod_get_index(struct device *dev,
164                        const char *con_id,
165                        unsigned int idx,
166                        enum gpiod_flags flags)
167 {
168         return ERR_PTR(-ENOSYS);
169 }
170
171 static inline struct gpio_desc *__must_check
172 __devm_gpiod_get_optional(struct device *dev, const char *con_id,
173                           enum gpiod_flags flags)
174 {
175         return ERR_PTR(-ENOSYS);
176 }
177
178 static inline struct gpio_desc *__must_check
179 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
180                                 unsigned int index, enum gpiod_flags flags)
181 {
182         return ERR_PTR(-ENOSYS);
183 }
184
185 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
186 {
187         might_sleep();
188
189         /* GPIO can never have been requested */
190         WARN_ON(1);
191 }
192
193
194 static inline int gpiod_get_direction(const struct gpio_desc *desc)
195 {
196         /* GPIO can never have been requested */
197         WARN_ON(1);
198         return -ENOSYS;
199 }
200 static inline int gpiod_direction_input(struct gpio_desc *desc)
201 {
202         /* GPIO can never have been requested */
203         WARN_ON(1);
204         return -ENOSYS;
205 }
206 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
207 {
208         /* GPIO can never have been requested */
209         WARN_ON(1);
210         return -ENOSYS;
211 }
212 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
213 {
214         /* GPIO can never have been requested */
215         WARN_ON(1);
216         return -ENOSYS;
217 }
218
219
220 static inline int gpiod_get_value(const struct gpio_desc *desc)
221 {
222         /* GPIO can never have been requested */
223         WARN_ON(1);
224         return 0;
225 }
226 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
227 {
228         /* GPIO can never have been requested */
229         WARN_ON(1);
230 }
231 static inline void gpiod_set_array(unsigned int array_size,
232                                    struct gpio_desc **desc_array,
233                                    int *value_array)
234 {
235         /* GPIO can never have been requested */
236         WARN_ON(1);
237 }
238 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
239 {
240         /* GPIO can never have been requested */
241         WARN_ON(1);
242         return 0;
243 }
244 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
245 {
246         /* GPIO can never have been requested */
247         WARN_ON(1);
248 }
249 static inline void gpiod_set_raw_array(unsigned int array_size,
250                                        struct gpio_desc **desc_array,
251                                        int *value_array)
252 {
253         /* GPIO can never have been requested */
254         WARN_ON(1);
255 }
256
257 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
258 {
259         /* GPIO can never have been requested */
260         WARN_ON(1);
261         return 0;
262 }
263 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
264 {
265         /* GPIO can never have been requested */
266         WARN_ON(1);
267 }
268 static inline void gpiod_set_array_cansleep(unsigned int array_size,
269                                             struct gpio_desc **desc_array,
270                                             int *value_array)
271 {
272         /* GPIO can never have been requested */
273         WARN_ON(1);
274 }
275 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
276 {
277         /* GPIO can never have been requested */
278         WARN_ON(1);
279         return 0;
280 }
281 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
282                                                 int value)
283 {
284         /* GPIO can never have been requested */
285         WARN_ON(1);
286 }
287 static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
288                                                 struct gpio_desc **desc_array,
289                                                 int *value_array)
290 {
291         /* GPIO can never have been requested */
292         WARN_ON(1);
293 }
294
295 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
296 {
297         /* GPIO can never have been requested */
298         WARN_ON(1);
299         return -ENOSYS;
300 }
301
302 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
303 {
304         /* GPIO can never have been requested */
305         WARN_ON(1);
306         return 0;
307 }
308 static inline int gpiod_cansleep(const struct gpio_desc *desc)
309 {
310         /* GPIO can never have been requested */
311         WARN_ON(1);
312         return 0;
313 }
314
315 static inline int gpiod_to_irq(const struct gpio_desc *desc)
316 {
317         /* GPIO can never have been requested */
318         WARN_ON(1);
319         return -EINVAL;
320 }
321
322 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
323 {
324         return ERR_PTR(-EINVAL);
325 }
326 static inline int desc_to_gpio(const struct gpio_desc *desc)
327 {
328         /* GPIO can never have been requested */
329         WARN_ON(1);
330         return -EINVAL;
331 }
332
333 #endif /* CONFIG_GPIOLIB */
334
335 /*
336  * Vararg-hacks! This is done to transition the kernel to always pass
337  * the options flags argument to the below functions. During a transition
338  * phase these vararg macros make both old-and-newstyle code compile,
339  * but when all calls to the elder API are removed, these should go away
340  * and the __gpiod_get() etc functions above be renamed just gpiod_get()
341  * etc.
342  */
343 #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
344 #define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
345 #define __gpiod_get_index(dev, con_id, index, flags, ...)               \
346         __gpiod_get_index(dev, con_id, index, flags)
347 #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
348 #define __gpiod_get_optional(dev, con_id, flags, ...)                   \
349         __gpiod_get_optional(dev, con_id, flags)
350 #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
351 #define __gpiod_get_index_optional(dev, con_id, index, flags, ...)      \
352         __gpiod_get_index_optional(dev, con_id, index, flags)
353 #define gpiod_get_index_optional(varargs...)                            \
354         __gpiod_get_index_optional(varargs, GPIOD_ASIS)
355 #define __devm_gpiod_get(dev, con_id, flags, ...)                       \
356         __devm_gpiod_get(dev, con_id, flags)
357 #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
358 #define __devm_gpiod_get_index(dev, con_id, index, flags, ...)          \
359         __devm_gpiod_get_index(dev, con_id, index, flags)
360 #define devm_gpiod_get_index(varargs...)                                \
361         __devm_gpiod_get_index(varargs, GPIOD_ASIS)
362 #define __devm_gpiod_get_optional(dev, con_id, flags, ...)              \
363         __devm_gpiod_get_optional(dev, con_id, flags)
364 #define devm_gpiod_get_optional(varargs...)                             \
365         __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
366 #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
367         __devm_gpiod_get_index_optional(dev, con_id, index, flags)
368 #define devm_gpiod_get_index_optional(varargs...)                       \
369         __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
370
371 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
372
373 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
374 int gpiod_export_link(struct device *dev, const char *name,
375                       struct gpio_desc *desc);
376 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
377 void gpiod_unexport(struct gpio_desc *desc);
378
379 #else  /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
380
381 static inline int gpiod_export(struct gpio_desc *desc,
382                                bool direction_may_change)
383 {
384         return -ENOSYS;
385 }
386
387 static inline int gpiod_export_link(struct device *dev, const char *name,
388                                     struct gpio_desc *desc)
389 {
390         return -ENOSYS;
391 }
392
393 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
394 {
395         return -ENOSYS;
396 }
397
398 static inline void gpiod_unexport(struct gpio_desc *desc)
399 {
400 }
401
402 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
403
404 #endif