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 * Opaque descriptor for a structure of GPIO array attributes. This structure
22 * is attached to struct gpiod_descs obtained from gpiod_get_array() and can be
23 * passed back to get/set array functions in order to activate fast processing
29 * Struct containing an array of descriptors that can be obtained using
33 struct gpio_array *info;
35 struct gpio_desc *desc[];
38 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
39 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
40 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
41 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
42 #define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4)
45 * Optional flags that can be passed to one of gpiod_* to configure direction
46 * and output value. These values cannot be OR'd.
50 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
51 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
52 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
53 GPIOD_FLAGS_BIT_DIR_VAL,
54 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN,
55 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN,
60 /* Return the number of GPIOs associated with a device / function */
61 int gpiod_count(struct device *dev, const char *con_id);
63 /* Acquire and dispose GPIOs */
64 struct gpio_desc *__must_check gpiod_get(struct device *dev,
66 enum gpiod_flags flags);
67 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
70 enum gpiod_flags flags);
71 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
73 enum gpiod_flags flags);
74 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
77 enum gpiod_flags flags);
78 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
80 enum gpiod_flags flags);
81 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
83 enum gpiod_flags flags);
84 void gpiod_put(struct gpio_desc *desc);
85 void gpiod_put_array(struct gpio_descs *descs);
87 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
89 enum gpiod_flags flags);
90 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
93 enum gpiod_flags flags);
94 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
96 enum gpiod_flags flags);
97 struct gpio_desc *__must_check
98 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
99 unsigned int index, enum gpiod_flags flags);
100 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
102 enum gpiod_flags flags);
103 struct gpio_descs *__must_check
104 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
105 enum gpiod_flags flags);
106 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
107 void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
108 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
110 int gpiod_get_direction(struct gpio_desc *desc);
111 int gpiod_direction_input(struct gpio_desc *desc);
112 int gpiod_direction_output(struct gpio_desc *desc, int value);
113 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
115 /* Value get/set from non-sleeping context */
116 int gpiod_get_value(const struct gpio_desc *desc);
117 int gpiod_get_array_value(unsigned int array_size,
118 struct gpio_desc **desc_array,
119 struct gpio_array *array_info,
120 unsigned long *value_bitmap);
121 void gpiod_set_value(struct gpio_desc *desc, int value);
122 int gpiod_set_array_value(unsigned int array_size,
123 struct gpio_desc **desc_array,
124 struct gpio_array *array_info,
125 unsigned long *value_bitmap);
126 int gpiod_get_raw_value(const struct gpio_desc *desc);
127 int gpiod_get_raw_array_value(unsigned int array_size,
128 struct gpio_desc **desc_array,
129 struct gpio_array *array_info,
130 unsigned long *value_bitmap);
131 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
132 int gpiod_set_raw_array_value(unsigned int array_size,
133 struct gpio_desc **desc_array,
134 struct gpio_array *array_info,
135 unsigned long *value_bitmap);
137 /* Value get/set from sleeping context */
138 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
139 int gpiod_get_array_value_cansleep(unsigned int array_size,
140 struct gpio_desc **desc_array,
141 struct gpio_array *array_info,
142 unsigned long *value_bitmap);
143 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
144 int gpiod_set_array_value_cansleep(unsigned int array_size,
145 struct gpio_desc **desc_array,
146 struct gpio_array *array_info,
147 unsigned long *value_bitmap);
148 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
149 int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
150 struct gpio_desc **desc_array,
151 struct gpio_array *array_info,
152 unsigned long *value_bitmap);
153 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
154 int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
155 struct gpio_desc **desc_array,
156 struct gpio_array *array_info,
157 unsigned long *value_bitmap);
159 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
160 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory);
162 int gpiod_is_active_low(const struct gpio_desc *desc);
163 int gpiod_cansleep(const struct gpio_desc *desc);
165 int gpiod_to_irq(const struct gpio_desc *desc);
166 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
168 /* Convert between the old gpio_ and new gpiod_ interfaces */
169 struct gpio_desc *gpio_to_desc(unsigned gpio);
170 int desc_to_gpio(const struct gpio_desc *desc);
172 /* Child properties interface */
174 struct fwnode_handle;
176 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
177 const char *propname, int index,
178 enum gpiod_flags dflags,
180 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
181 struct device_node *node,
182 const char *propname, int index,
183 enum gpiod_flags dflags,
185 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
186 const char *propname, int index,
187 enum gpiod_flags dflags,
189 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
190 const char *con_id, int index,
191 struct fwnode_handle *child,
192 enum gpiod_flags flags,
195 #else /* CONFIG_GPIOLIB */
197 static inline int gpiod_count(struct device *dev, const char *con_id)
202 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
204 enum gpiod_flags flags)
206 return ERR_PTR(-ENOSYS);
208 static inline struct gpio_desc *__must_check
209 gpiod_get_index(struct device *dev,
212 enum gpiod_flags flags)
214 return ERR_PTR(-ENOSYS);
217 static inline struct gpio_desc *__must_check
218 gpiod_get_optional(struct device *dev, const char *con_id,
219 enum gpiod_flags flags)
224 static inline struct gpio_desc *__must_check
225 gpiod_get_index_optional(struct device *dev, const char *con_id,
226 unsigned int index, enum gpiod_flags flags)
231 static inline struct gpio_descs *__must_check
232 gpiod_get_array(struct device *dev, const char *con_id,
233 enum gpiod_flags flags)
235 return ERR_PTR(-ENOSYS);
238 static inline struct gpio_descs *__must_check
239 gpiod_get_array_optional(struct device *dev, const char *con_id,
240 enum gpiod_flags flags)
245 static inline void gpiod_put(struct gpio_desc *desc)
249 /* GPIO can never have been requested */
253 static inline void devm_gpiod_unhinge(struct device *dev,
254 struct gpio_desc *desc)
258 /* GPIO can never have been requested */
262 static inline void gpiod_put_array(struct gpio_descs *descs)
266 /* GPIO can never have been requested */
270 static inline struct gpio_desc *__must_check
271 devm_gpiod_get(struct device *dev,
273 enum gpiod_flags flags)
275 return ERR_PTR(-ENOSYS);
278 struct gpio_desc *__must_check
279 devm_gpiod_get_index(struct device *dev,
282 enum gpiod_flags flags)
284 return ERR_PTR(-ENOSYS);
287 static inline struct gpio_desc *__must_check
288 devm_gpiod_get_optional(struct device *dev, const char *con_id,
289 enum gpiod_flags flags)
294 static inline struct gpio_desc *__must_check
295 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
296 unsigned int index, enum gpiod_flags flags)
301 static inline struct gpio_descs *__must_check
302 devm_gpiod_get_array(struct device *dev, const char *con_id,
303 enum gpiod_flags flags)
305 return ERR_PTR(-ENOSYS);
308 static inline struct gpio_descs *__must_check
309 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
310 enum gpiod_flags flags)
315 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
319 /* GPIO can never have been requested */
323 static inline void devm_gpiod_put_array(struct device *dev,
324 struct gpio_descs *descs)
328 /* GPIO can never have been requested */
333 static inline int gpiod_get_direction(const struct gpio_desc *desc)
335 /* GPIO can never have been requested */
339 static inline int gpiod_direction_input(struct gpio_desc *desc)
341 /* GPIO can never have been requested */
345 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
347 /* GPIO can never have been requested */
351 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
353 /* GPIO can never have been requested */
359 static inline int gpiod_get_value(const struct gpio_desc *desc)
361 /* GPIO can never have been requested */
365 static inline int gpiod_get_array_value(unsigned int array_size,
366 struct gpio_desc **desc_array,
367 struct gpio_array *array_info,
368 unsigned long *value_bitmap)
370 /* GPIO can never have been requested */
374 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
376 /* GPIO can never have been requested */
379 static inline int gpiod_set_array_value(unsigned int array_size,
380 struct gpio_desc **desc_array,
381 struct gpio_array *array_info,
382 unsigned long *value_bitmap)
384 /* GPIO can never have been requested */
388 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
390 /* GPIO can never have been requested */
394 static inline int gpiod_get_raw_array_value(unsigned int array_size,
395 struct gpio_desc **desc_array,
396 struct gpio_array *array_info,
397 unsigned long *value_bitmap)
399 /* GPIO can never have been requested */
403 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
405 /* GPIO can never have been requested */
408 static inline int gpiod_set_raw_array_value(unsigned int array_size,
409 struct gpio_desc **desc_array,
410 struct gpio_array *array_info,
411 unsigned long *value_bitmap)
413 /* GPIO can never have been requested */
418 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
420 /* GPIO can never have been requested */
424 static inline int gpiod_get_array_value_cansleep(unsigned int array_size,
425 struct gpio_desc **desc_array,
426 struct gpio_array *array_info,
427 unsigned long *value_bitmap)
429 /* GPIO can never have been requested */
433 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
435 /* GPIO can never have been requested */
438 static inline int gpiod_set_array_value_cansleep(unsigned int array_size,
439 struct gpio_desc **desc_array,
440 struct gpio_array *array_info,
441 unsigned long *value_bitmap)
443 /* GPIO can never have been requested */
447 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
449 /* GPIO can never have been requested */
453 static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
454 struct gpio_desc **desc_array,
455 struct gpio_array *array_info,
456 unsigned long *value_bitmap)
458 /* GPIO can never have been requested */
462 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
465 /* GPIO can never have been requested */
468 static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
469 struct gpio_desc **desc_array,
470 struct gpio_array *array_info,
471 unsigned long *value_bitmap)
473 /* GPIO can never have been requested */
478 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
480 /* GPIO can never have been requested */
485 static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
487 /* GPIO can never have been requested */
492 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
494 /* GPIO can never have been requested */
498 static inline int gpiod_cansleep(const struct gpio_desc *desc)
500 /* GPIO can never have been requested */
505 static inline int gpiod_to_irq(const struct gpio_desc *desc)
507 /* GPIO can never have been requested */
512 static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
515 /* GPIO can never have been requested */
520 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
525 static inline int desc_to_gpio(const struct gpio_desc *desc)
527 /* GPIO can never have been requested */
532 /* Child properties interface */
534 struct fwnode_handle;
537 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
538 const char *propname, int index,
539 enum gpiod_flags dflags,
542 return ERR_PTR(-ENOSYS);
546 struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
547 struct device_node *node,
548 const char *propname, int index,
549 enum gpiod_flags dflags,
552 return ERR_PTR(-ENOSYS);
556 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
557 const char *propname, int index,
558 enum gpiod_flags dflags,
561 return ERR_PTR(-ENOSYS);
565 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
566 const char *con_id, int index,
567 struct fwnode_handle *child,
568 enum gpiod_flags flags,
571 return ERR_PTR(-ENOSYS);
574 #endif /* CONFIG_GPIOLIB */
577 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
579 struct fwnode_handle *child,
580 enum gpiod_flags flags,
583 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
587 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
589 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
590 int gpiod_export_link(struct device *dev, const char *name,
591 struct gpio_desc *desc);
592 void gpiod_unexport(struct gpio_desc *desc);
594 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
596 static inline int gpiod_export(struct gpio_desc *desc,
597 bool direction_may_change)
602 static inline int gpiod_export_link(struct device *dev, const char *name,
603 struct gpio_desc *desc)
608 static inline void gpiod_unexport(struct gpio_desc *desc)
612 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */