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_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
45 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
50 /* Return the number of GPIOs associated with a device / function */
51 int gpiod_count(struct device *dev, const char *con_id);
53 /* Acquire and dispose GPIOs */
54 struct gpio_desc *__must_check gpiod_get(struct device *dev,
56 enum gpiod_flags flags);
57 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
60 enum gpiod_flags flags);
61 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
63 enum gpiod_flags flags);
64 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
67 enum gpiod_flags flags);
68 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
70 enum gpiod_flags flags);
71 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
73 enum gpiod_flags flags);
74 void gpiod_put(struct gpio_desc *desc);
75 void gpiod_put_array(struct gpio_descs *descs);
77 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
79 enum gpiod_flags flags);
80 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
83 enum gpiod_flags flags);
84 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
86 enum gpiod_flags flags);
87 struct gpio_desc *__must_check
88 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
89 unsigned int index, enum gpiod_flags flags);
90 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
92 enum gpiod_flags flags);
93 struct gpio_descs *__must_check
94 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
95 enum gpiod_flags flags);
96 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
97 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
99 int gpiod_get_direction(struct gpio_desc *desc);
100 int gpiod_direction_input(struct gpio_desc *desc);
101 int gpiod_direction_output(struct gpio_desc *desc, int value);
102 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
104 /* Value get/set from non-sleeping context */
105 int gpiod_get_value(const struct gpio_desc *desc);
106 int gpiod_get_array_value(unsigned int array_size,
107 struct gpio_desc **desc_array, int *value_array);
108 void gpiod_set_value(struct gpio_desc *desc, int value);
109 void gpiod_set_array_value(unsigned int array_size,
110 struct gpio_desc **desc_array, int *value_array);
111 int gpiod_get_raw_value(const struct gpio_desc *desc);
112 int gpiod_get_raw_array_value(unsigned int array_size,
113 struct gpio_desc **desc_array,
115 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
116 int gpiod_set_raw_array_value(unsigned int array_size,
117 struct gpio_desc **desc_array,
120 /* Value get/set from sleeping context */
121 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
122 int gpiod_get_array_value_cansleep(unsigned int array_size,
123 struct gpio_desc **desc_array,
125 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
126 void gpiod_set_array_value_cansleep(unsigned int array_size,
127 struct gpio_desc **desc_array,
129 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
130 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
131 struct gpio_desc **desc_array,
133 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
134 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
135 struct gpio_desc **desc_array,
138 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
139 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
141 int gpiod_is_active_low(const struct gpio_desc *desc);
142 int gpiod_cansleep(const struct gpio_desc *desc);
144 int gpiod_to_irq(const struct gpio_desc *desc);
145 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
147 /* Convert between the old gpio_ and new gpiod_ interfaces */
148 struct gpio_desc *gpio_to_desc(unsigned gpio);
149 int desc_to_gpio(const struct gpio_desc *desc);
151 /* Child properties interface */
153 struct fwnode_handle;
155 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
156 struct device_node *node,
157 const char *propname, int index,
158 enum gpiod_flags dflags,
160 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
161 const char *propname, int index,
162 enum gpiod_flags dflags,
164 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
165 const char *con_id, int index,
166 struct fwnode_handle *child,
167 enum gpiod_flags flags,
170 #else /* CONFIG_GPIOLIB */
172 static inline int gpiod_count(struct device *dev, const char *con_id)
177 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
179 enum gpiod_flags flags)
181 return ERR_PTR(-ENOSYS);
183 static inline struct gpio_desc *__must_check
184 gpiod_get_index(struct device *dev,
187 enum gpiod_flags flags)
189 return ERR_PTR(-ENOSYS);
192 static inline struct gpio_desc *__must_check
193 gpiod_get_optional(struct device *dev, const char *con_id,
194 enum gpiod_flags flags)
199 static inline struct gpio_desc *__must_check
200 gpiod_get_index_optional(struct device *dev, const char *con_id,
201 unsigned int index, enum gpiod_flags flags)
206 static inline struct gpio_descs *__must_check
207 gpiod_get_array(struct device *dev, const char *con_id,
208 enum gpiod_flags flags)
210 return ERR_PTR(-ENOSYS);
213 static inline struct gpio_descs *__must_check
214 gpiod_get_array_optional(struct device *dev, const char *con_id,
215 enum gpiod_flags flags)
220 static inline void gpiod_put(struct gpio_desc *desc)
224 /* GPIO can never have been requested */
228 static inline void gpiod_put_array(struct gpio_descs *descs)
232 /* GPIO can never have been requested */
236 static inline struct gpio_desc *__must_check
237 devm_gpiod_get(struct device *dev,
239 enum gpiod_flags flags)
241 return ERR_PTR(-ENOSYS);
244 struct gpio_desc *__must_check
245 devm_gpiod_get_index(struct device *dev,
248 enum gpiod_flags flags)
250 return ERR_PTR(-ENOSYS);
253 static inline struct gpio_desc *__must_check
254 devm_gpiod_get_optional(struct device *dev, const char *con_id,
255 enum gpiod_flags flags)
260 static inline struct gpio_desc *__must_check
261 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
262 unsigned int index, enum gpiod_flags flags)
267 static inline struct gpio_descs *__must_check
268 devm_gpiod_get_array(struct device *dev, const char *con_id,
269 enum gpiod_flags flags)
271 return ERR_PTR(-ENOSYS);
274 static inline struct gpio_descs *__must_check
275 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
276 enum gpiod_flags flags)
281 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
285 /* GPIO can never have been requested */
289 static inline void devm_gpiod_put_array(struct device *dev,
290 struct gpio_descs *descs)
294 /* GPIO can never have been requested */
299 static inline int gpiod_get_direction(const struct gpio_desc *desc)
301 /* GPIO can never have been requested */
305 static inline int gpiod_direction_input(struct gpio_desc *desc)
307 /* GPIO can never have been requested */
311 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
313 /* GPIO can never have been requested */
317 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
319 /* GPIO can never have been requested */
325 static inline int gpiod_get_value(const struct gpio_desc *desc)
327 /* GPIO can never have been requested */
331 static inline int gpiod_get_array_value(unsigned int array_size,
332 struct gpio_desc **desc_array,
335 /* GPIO can never have been requested */
339 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
341 /* GPIO can never have been requested */
344 static inline void gpiod_set_array_value(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(const struct gpio_desc *desc)
353 /* GPIO can never have been requested */
357 static inline int gpiod_get_raw_array_value(unsigned int array_size,
358 struct gpio_desc **desc_array,
361 /* GPIO can never have been requested */
365 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
367 /* GPIO can never have been requested */
370 static inline int gpiod_set_raw_array_value(unsigned int array_size,
371 struct gpio_desc **desc_array,
374 /* GPIO can never have been requested */
379 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
381 /* GPIO can never have been requested */
385 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
386 struct gpio_desc **desc_array,
389 /* GPIO can never have been requested */
393 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
395 /* GPIO can never have been requested */
398 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
399 struct gpio_desc **desc_array,
402 /* GPIO can never have been requested */
405 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
407 /* GPIO can never have been requested */
411 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
412 struct gpio_desc **desc_array,
415 /* GPIO can never have been requested */
419 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
422 /* GPIO can never have been requested */
425 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
426 struct gpio_desc **desc_array,
429 /* GPIO can never have been requested */
434 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
436 /* GPIO can never have been requested */
441 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
443 /* GPIO can never have been requested */
448 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
450 /* GPIO can never have been requested */
454 static inline int gpiod_cansleep(const struct gpio_desc *desc)
456 /* GPIO can never have been requested */
461 static inline int gpiod_to_irq(const struct gpio_desc *desc)
463 /* GPIO can never have been requested */
468 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
471 /* GPIO can never have been requested */
476 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
481 static inline int desc_to_gpio(const struct gpio_desc *desc)
483 /* GPIO can never have been requested */
488 /* Child properties interface */
490 struct fwnode_handle;
493 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
494 struct device_node *node,
495 const char *propname, int index,
496 enum gpiod_flags dflags,
499 return ERR_PTR(-ENOSYS);
503 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
504 const char *propname, int index,
505 enum gpiod_flags dflags,
508 return ERR_PTR(-ENOSYS);
512 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
513 const char *con_id, int index,
514 struct fwnode_handle *child,
515 enum gpiod_flags flags,
518 return ERR_PTR(-ENOSYS);
521 #endif /* CONFIG_GPIOLIB */
524 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
526 struct fwnode_handle *child,
527 enum gpiod_flags flags,
530 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
534 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
536 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
537 int gpiod_export_link(struct device *dev, const char *name,
538 struct gpio_desc *desc);
539 void gpiod_unexport(struct gpio_desc *desc);
541 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
543 static inline int gpiod_export(struct gpio_desc *desc,
544 bool direction_may_change)
549 static inline int gpiod_export_link(struct device *dev, const char *name,
550 struct gpio_desc *desc)
555 static inline void gpiod_unexport(struct gpio_desc *desc)
559 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */