1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GPIO_CONSUMER_H
3 #define __LINUX_GPIO_CONSUMER_H
7 #include <linux/kernel.h>
12 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
13 * preferable to the old integer-based handles.
15 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
16 * until the GPIO is released.
21 * Struct containing an array of descriptors that can be obtained using
26 struct gpio_desc *desc[];
29 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
30 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
31 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
32 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
35 * Optional flags that can be passed to one of gpiod_* to configure direction
36 * and output value. These values cannot be OR'd.
40 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
41 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
42 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
43 GPIOD_FLAGS_BIT_DIR_VAL,
44 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |
45 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_OPEN_DRAIN,
46 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |
47 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_DIR_VAL |
48 GPIOD_FLAGS_BIT_OPEN_DRAIN,
53 /* Return the number of GPIOs associated with a device / function */
54 int gpiod_count(struct device *dev, const char *con_id);
56 /* Acquire and dispose GPIOs */
57 struct gpio_desc *__must_check gpiod_get(struct device *dev,
59 enum gpiod_flags flags);
60 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
63 enum gpiod_flags flags);
64 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
66 enum gpiod_flags flags);
67 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
70 enum gpiod_flags flags);
71 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
73 enum gpiod_flags flags);
74 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
76 enum gpiod_flags flags);
77 void gpiod_put(struct gpio_desc *desc);
78 void gpiod_put_array(struct gpio_descs *descs);
80 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
82 enum gpiod_flags flags);
83 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
86 enum gpiod_flags flags);
87 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
89 enum gpiod_flags flags);
90 struct gpio_desc *__must_check
91 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
92 unsigned int index, enum gpiod_flags flags);
93 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
95 enum gpiod_flags flags);
96 struct gpio_descs *__must_check
97 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
98 enum gpiod_flags flags);
99 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
100 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
102 int gpiod_get_direction(struct gpio_desc *desc);
103 int gpiod_direction_input(struct gpio_desc *desc);
104 int gpiod_direction_output(struct gpio_desc *desc, int value);
105 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
107 /* Value get/set from non-sleeping context */
108 int gpiod_get_value(const struct gpio_desc *desc);
109 int gpiod_get_array_value(unsigned int array_size,
110 struct gpio_desc **desc_array, int *value_array);
111 void gpiod_set_value(struct gpio_desc *desc, int value);
112 void gpiod_set_array_value(unsigned int array_size,
113 struct gpio_desc **desc_array, int *value_array);
114 int gpiod_get_raw_value(const struct gpio_desc *desc);
115 int gpiod_get_raw_array_value(unsigned int array_size,
116 struct gpio_desc **desc_array,
118 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
119 void gpiod_set_raw_array_value(unsigned int array_size,
120 struct gpio_desc **desc_array,
123 /* Value get/set from sleeping context */
124 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
125 int gpiod_get_array_value_cansleep(unsigned int array_size,
126 struct gpio_desc **desc_array,
128 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
129 void gpiod_set_array_value_cansleep(unsigned int array_size,
130 struct gpio_desc **desc_array,
132 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
133 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
134 struct gpio_desc **desc_array,
136 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
137 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
138 struct gpio_desc **desc_array,
141 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
143 int gpiod_is_active_low(const struct gpio_desc *desc);
144 int gpiod_cansleep(const struct gpio_desc *desc);
146 int gpiod_to_irq(const struct gpio_desc *desc);
148 /* Convert between the old gpio_ and new gpiod_ interfaces */
149 struct gpio_desc *gpio_to_desc(unsigned gpio);
150 int desc_to_gpio(const struct gpio_desc *desc);
152 /* Child properties interface */
153 struct fwnode_handle;
155 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
156 const char *propname, int index,
157 enum gpiod_flags dflags,
159 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
160 const char *con_id, int index,
161 struct fwnode_handle *child,
162 enum gpiod_flags flags,
165 #else /* CONFIG_GPIOLIB */
167 static inline int gpiod_count(struct device *dev, const char *con_id)
172 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
174 enum gpiod_flags flags)
176 return ERR_PTR(-ENOSYS);
178 static inline struct gpio_desc *__must_check
179 gpiod_get_index(struct device *dev,
182 enum gpiod_flags flags)
184 return ERR_PTR(-ENOSYS);
187 static inline struct gpio_desc *__must_check
188 gpiod_get_optional(struct device *dev, const char *con_id,
189 enum gpiod_flags flags)
194 static inline struct gpio_desc *__must_check
195 gpiod_get_index_optional(struct device *dev, const char *con_id,
196 unsigned int index, enum gpiod_flags flags)
201 static inline struct gpio_descs *__must_check
202 gpiod_get_array(struct device *dev, const char *con_id,
203 enum gpiod_flags flags)
205 return ERR_PTR(-ENOSYS);
208 static inline struct gpio_descs *__must_check
209 gpiod_get_array_optional(struct device *dev, const char *con_id,
210 enum gpiod_flags flags)
215 static inline void gpiod_put(struct gpio_desc *desc)
219 /* GPIO can never have been requested */
223 static inline void gpiod_put_array(struct gpio_descs *descs)
227 /* GPIO can never have been requested */
231 static inline struct gpio_desc *__must_check
232 devm_gpiod_get(struct device *dev,
234 enum gpiod_flags flags)
236 return ERR_PTR(-ENOSYS);
239 struct gpio_desc *__must_check
240 devm_gpiod_get_index(struct device *dev,
243 enum gpiod_flags flags)
245 return ERR_PTR(-ENOSYS);
248 static inline struct gpio_desc *__must_check
249 devm_gpiod_get_optional(struct device *dev, const char *con_id,
250 enum gpiod_flags flags)
255 static inline struct gpio_desc *__must_check
256 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
257 unsigned int index, enum gpiod_flags flags)
262 static inline struct gpio_descs *__must_check
263 devm_gpiod_get_array(struct device *dev, const char *con_id,
264 enum gpiod_flags flags)
266 return ERR_PTR(-ENOSYS);
269 static inline struct gpio_descs *__must_check
270 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
271 enum gpiod_flags flags)
276 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
280 /* GPIO can never have been requested */
284 static inline void devm_gpiod_put_array(struct device *dev,
285 struct gpio_descs *descs)
289 /* GPIO can never have been requested */
294 static inline int gpiod_get_direction(const struct gpio_desc *desc)
296 /* GPIO can never have been requested */
300 static inline int gpiod_direction_input(struct gpio_desc *desc)
302 /* GPIO can never have been requested */
306 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
308 /* GPIO can never have been requested */
312 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
314 /* GPIO can never have been requested */
320 static inline int gpiod_get_value(const struct gpio_desc *desc)
322 /* GPIO can never have been requested */
326 static inline int gpiod_get_array_value(unsigned int array_size,
327 struct gpio_desc **desc_array,
330 /* GPIO can never have been requested */
334 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
336 /* GPIO can never have been requested */
339 static inline void gpiod_set_array_value(unsigned int array_size,
340 struct gpio_desc **desc_array,
343 /* GPIO can never have been requested */
346 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
348 /* GPIO can never have been requested */
352 static inline int gpiod_get_raw_array_value(unsigned int array_size,
353 struct gpio_desc **desc_array,
356 /* GPIO can never have been requested */
360 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
362 /* GPIO can never have been requested */
365 static inline void gpiod_set_raw_array_value(unsigned int array_size,
366 struct gpio_desc **desc_array,
369 /* GPIO can never have been requested */
373 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
375 /* GPIO can never have been requested */
379 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
380 struct gpio_desc **desc_array,
383 /* GPIO can never have been requested */
387 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
389 /* GPIO can never have been requested */
392 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
393 struct gpio_desc **desc_array,
396 /* GPIO can never have been requested */
399 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
401 /* GPIO can never have been requested */
405 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
406 struct gpio_desc **desc_array,
409 /* GPIO can never have been requested */
413 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
416 /* GPIO can never have been requested */
419 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
420 struct gpio_desc **desc_array,
423 /* GPIO can never have been requested */
427 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
429 /* GPIO can never have been requested */
434 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
436 /* GPIO can never have been requested */
440 static inline int gpiod_cansleep(const struct gpio_desc *desc)
442 /* GPIO can never have been requested */
447 static inline int gpiod_to_irq(const struct gpio_desc *desc)
449 /* GPIO can never have been requested */
454 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
456 return ERR_PTR(-EINVAL);
459 static inline int desc_to_gpio(const struct gpio_desc *desc)
461 /* GPIO can never have been requested */
466 /* Child properties interface */
467 struct fwnode_handle;
470 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
471 const char *propname, int index,
472 enum gpiod_flags dflags,
475 return ERR_PTR(-ENOSYS);
479 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
480 const char *con_id, int index,
481 struct fwnode_handle *child,
482 enum gpiod_flags flags,
485 return ERR_PTR(-ENOSYS);
488 #endif /* CONFIG_GPIOLIB */
491 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
493 struct fwnode_handle *child,
494 enum gpiod_flags flags,
497 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
501 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
503 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
504 int gpiod_export_link(struct device *dev, const char *name,
505 struct gpio_desc *desc);
506 void gpiod_unexport(struct gpio_desc *desc);
508 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
510 static inline int gpiod_export(struct gpio_desc *desc,
511 bool direction_may_change)
516 static inline int gpiod_export_link(struct device *dev, const char *name,
517 struct gpio_desc *desc)
522 static inline void gpiod_unexport(struct gpio_desc *desc)
526 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */