1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
6 #include <linux/kernel.h>
11 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
12 * preferable to the old integer-based handles.
14 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
15 * until the GPIO is released.
20 * Struct containing an array of descriptors that can be obtained using
25 struct gpio_desc *desc[];
28 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
29 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
30 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
33 * Optional flags that can be passed to one of gpiod_* to configure direction
34 * and output value. These values cannot be OR'd.
38 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
39 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
40 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
41 GPIOD_FLAGS_BIT_DIR_VAL,
46 /* Return the number of GPIOs associated with a device / function */
47 int gpiod_count(struct device *dev, const char *con_id);
49 /* Acquire and dispose GPIOs */
50 struct gpio_desc *__must_check __gpiod_get(struct device *dev,
52 enum gpiod_flags flags);
53 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
56 enum gpiod_flags flags);
57 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
59 enum gpiod_flags flags);
60 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
63 enum gpiod_flags flags);
64 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
66 enum gpiod_flags flags);
67 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
69 enum gpiod_flags flags);
70 void gpiod_put(struct gpio_desc *desc);
71 void gpiod_put_array(struct gpio_descs *descs);
73 struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
75 enum gpiod_flags flags);
76 struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
79 enum gpiod_flags flags);
80 struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
82 enum gpiod_flags flags);
83 struct gpio_desc *__must_check
84 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
85 unsigned int index, enum gpiod_flags flags);
86 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
88 enum gpiod_flags flags);
89 struct gpio_descs *__must_check
90 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
91 enum gpiod_flags flags);
92 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
93 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
95 int gpiod_get_direction(struct gpio_desc *desc);
96 int gpiod_direction_input(struct gpio_desc *desc);
97 int gpiod_direction_output(struct gpio_desc *desc, int value);
98 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
100 /* Value get/set from non-sleeping context */
101 int gpiod_get_value(const struct gpio_desc *desc);
102 void gpiod_set_value(struct gpio_desc *desc, int value);
103 void gpiod_set_array(unsigned int array_size,
104 struct gpio_desc **desc_array, int *value_array);
105 int gpiod_get_raw_value(const struct gpio_desc *desc);
106 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
107 void gpiod_set_raw_array(unsigned int array_size,
108 struct gpio_desc **desc_array, int *value_array);
110 /* Value get/set from sleeping context */
111 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
112 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
113 void gpiod_set_array_cansleep(unsigned int array_size,
114 struct gpio_desc **desc_array,
116 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
117 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
118 void gpiod_set_raw_array_cansleep(unsigned int array_size,
119 struct gpio_desc **desc_array,
122 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
124 int gpiod_is_active_low(const struct gpio_desc *desc);
125 int gpiod_cansleep(const struct gpio_desc *desc);
127 int gpiod_to_irq(const struct gpio_desc *desc);
129 /* Convert between the old gpio_ and new gpiod_ interfaces */
130 struct gpio_desc *gpio_to_desc(unsigned gpio);
131 int desc_to_gpio(const struct gpio_desc *desc);
133 /* Child properties interface */
134 struct fwnode_handle;
136 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
137 const char *propname);
138 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
140 struct fwnode_handle *child);
141 #else /* CONFIG_GPIOLIB */
143 static inline int gpiod_count(struct device *dev, const char *con_id)
148 static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
150 enum gpiod_flags flags)
152 return ERR_PTR(-ENOSYS);
154 static inline struct gpio_desc *__must_check
155 __gpiod_get_index(struct device *dev,
158 enum gpiod_flags flags)
160 return ERR_PTR(-ENOSYS);
163 static inline struct gpio_desc *__must_check
164 __gpiod_get_optional(struct device *dev, const char *con_id,
165 enum gpiod_flags flags)
167 return ERR_PTR(-ENOSYS);
170 static inline struct gpio_desc *__must_check
171 __gpiod_get_index_optional(struct device *dev, const char *con_id,
172 unsigned int index, enum gpiod_flags flags)
174 return ERR_PTR(-ENOSYS);
177 static inline struct gpio_descs *__must_check
178 gpiod_get_array(struct device *dev, const char *con_id,
179 enum gpiod_flags flags)
181 return ERR_PTR(-ENOSYS);
184 static inline struct gpio_descs *__must_check
185 gpiod_get_array_optional(struct device *dev, const char *con_id,
186 enum gpiod_flags flags)
188 return ERR_PTR(-ENOSYS);
191 static inline void gpiod_put(struct gpio_desc *desc)
195 /* GPIO can never have been requested */
199 static inline void gpiod_put_array(struct gpio_descs *descs)
203 /* GPIO can never have been requested */
207 static inline struct gpio_desc *__must_check
208 __devm_gpiod_get(struct device *dev,
210 enum gpiod_flags flags)
212 return ERR_PTR(-ENOSYS);
215 struct gpio_desc *__must_check
216 __devm_gpiod_get_index(struct device *dev,
219 enum gpiod_flags flags)
221 return ERR_PTR(-ENOSYS);
224 static inline struct gpio_desc *__must_check
225 __devm_gpiod_get_optional(struct device *dev, const char *con_id,
226 enum gpiod_flags flags)
228 return ERR_PTR(-ENOSYS);
231 static inline struct gpio_desc *__must_check
232 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
233 unsigned int index, enum gpiod_flags flags)
235 return ERR_PTR(-ENOSYS);
238 static inline struct gpio_descs *__must_check
239 devm_gpiod_get_array(struct device *dev, const char *con_id,
240 enum gpiod_flags flags)
242 return ERR_PTR(-ENOSYS);
245 static inline struct gpio_descs *__must_check
246 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
247 enum gpiod_flags flags)
249 return ERR_PTR(-ENOSYS);
252 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
256 /* GPIO can never have been requested */
260 static inline void devm_gpiod_put_array(struct device *dev,
261 struct gpio_descs *descs)
265 /* GPIO can never have been requested */
270 static inline int gpiod_get_direction(const struct gpio_desc *desc)
272 /* GPIO can never have been requested */
276 static inline int gpiod_direction_input(struct gpio_desc *desc)
278 /* GPIO can never have been requested */
282 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
284 /* GPIO can never have been requested */
288 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
290 /* GPIO can never have been requested */
296 static inline int gpiod_get_value(const struct gpio_desc *desc)
298 /* GPIO can never have been requested */
302 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
304 /* GPIO can never have been requested */
307 static inline void gpiod_set_array(unsigned int array_size,
308 struct gpio_desc **desc_array,
311 /* GPIO can never have been requested */
314 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
316 /* GPIO can never have been requested */
320 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
322 /* GPIO can never have been requested */
325 static inline void gpiod_set_raw_array(unsigned int array_size,
326 struct gpio_desc **desc_array,
329 /* GPIO can never have been requested */
333 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
335 /* GPIO can never have been requested */
339 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
341 /* GPIO can never have been requested */
344 static inline void gpiod_set_array_cansleep(unsigned int array_size,
345 struct gpio_desc **desc_array,
348 /* GPIO can never have been requested */
351 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
353 /* GPIO can never have been requested */
357 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
360 /* GPIO can never have been requested */
363 static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
364 struct gpio_desc **desc_array,
367 /* GPIO can never have been requested */
371 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
373 /* GPIO can never have been requested */
378 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
380 /* GPIO can never have been requested */
384 static inline int gpiod_cansleep(const struct gpio_desc *desc)
386 /* GPIO can never have been requested */
391 static inline int gpiod_to_irq(const struct gpio_desc *desc)
393 /* GPIO can never have been requested */
398 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
400 return ERR_PTR(-EINVAL);
402 static inline int desc_to_gpio(const struct gpio_desc *desc)
404 /* GPIO can never have been requested */
409 #endif /* CONFIG_GPIOLIB */
412 * Vararg-hacks! This is done to transition the kernel to always pass
413 * the options flags argument to the below functions. During a transition
414 * phase these vararg macros make both old-and-newstyle code compile,
415 * but when all calls to the elder API are removed, these should go away
416 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
419 #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
420 #define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
421 #define __gpiod_get_index(dev, con_id, index, flags, ...) \
422 __gpiod_get_index(dev, con_id, index, flags)
423 #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
424 #define __gpiod_get_optional(dev, con_id, flags, ...) \
425 __gpiod_get_optional(dev, con_id, flags)
426 #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
427 #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
428 __gpiod_get_index_optional(dev, con_id, index, flags)
429 #define gpiod_get_index_optional(varargs...) \
430 __gpiod_get_index_optional(varargs, GPIOD_ASIS)
431 #define __devm_gpiod_get(dev, con_id, flags, ...) \
432 __devm_gpiod_get(dev, con_id, flags)
433 #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
434 #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
435 __devm_gpiod_get_index(dev, con_id, index, flags)
436 #define devm_gpiod_get_index(varargs...) \
437 __devm_gpiod_get_index(varargs, GPIOD_ASIS)
438 #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
439 __devm_gpiod_get_optional(dev, con_id, flags)
440 #define devm_gpiod_get_optional(varargs...) \
441 __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
442 #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
443 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
444 #define devm_gpiod_get_index_optional(varargs...) \
445 __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
447 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
449 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
450 int gpiod_export_link(struct device *dev, const char *name,
451 struct gpio_desc *desc);
452 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
453 void gpiod_unexport(struct gpio_desc *desc);
455 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
457 static inline int gpiod_export(struct gpio_desc *desc,
458 bool direction_may_change)
463 static inline int gpiod_export_link(struct device *dev, const char *name,
464 struct gpio_desc *desc)
469 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
474 static inline void gpiod_unexport(struct gpio_desc *desc)
478 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */