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)
31 #define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3)
34 * Optional flags that can be passed to one of gpiod_* to configure direction
35 * and output value. These values cannot be OR'd.
39 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
40 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
41 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
42 GPIOD_FLAGS_BIT_DIR_VAL,
43 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |
44 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_OPEN_DRAIN,
45 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_FLAGS_BIT_DIR_SET |
46 GPIOD_FLAGS_BIT_DIR_OUT | GPIOD_FLAGS_BIT_DIR_VAL |
47 GPIOD_FLAGS_BIT_OPEN_DRAIN,
52 /* Return the number of GPIOs associated with a device / function */
53 int gpiod_count(struct device *dev, const char *con_id);
55 /* Acquire and dispose GPIOs */
56 struct gpio_desc *__must_check gpiod_get(struct device *dev,
58 enum gpiod_flags flags);
59 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
62 enum gpiod_flags flags);
63 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
65 enum gpiod_flags flags);
66 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
69 enum gpiod_flags flags);
70 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
72 enum gpiod_flags flags);
73 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
75 enum gpiod_flags flags);
76 void gpiod_put(struct gpio_desc *desc);
77 void gpiod_put_array(struct gpio_descs *descs);
79 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
81 enum gpiod_flags flags);
82 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
85 enum gpiod_flags flags);
86 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
88 enum gpiod_flags flags);
89 struct gpio_desc *__must_check
90 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
91 unsigned int index, enum gpiod_flags flags);
92 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
94 enum gpiod_flags flags);
95 struct gpio_descs *__must_check
96 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
97 enum gpiod_flags flags);
98 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
99 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
101 int gpiod_get_direction(struct gpio_desc *desc);
102 int gpiod_direction_input(struct gpio_desc *desc);
103 int gpiod_direction_output(struct gpio_desc *desc, int value);
104 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
106 /* Value get/set from non-sleeping context */
107 int gpiod_get_value(const struct gpio_desc *desc);
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 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
113 void gpiod_set_raw_array_value(unsigned int array_size,
114 struct gpio_desc **desc_array,
117 /* Value get/set from sleeping context */
118 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
119 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
120 void gpiod_set_array_value_cansleep(unsigned int array_size,
121 struct gpio_desc **desc_array,
123 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
124 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
125 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
126 struct gpio_desc **desc_array,
129 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
131 int gpiod_is_active_low(const struct gpio_desc *desc);
132 int gpiod_cansleep(const struct gpio_desc *desc);
134 int gpiod_to_irq(const struct gpio_desc *desc);
136 /* Convert between the old gpio_ and new gpiod_ interfaces */
137 struct gpio_desc *gpio_to_desc(unsigned gpio);
138 int desc_to_gpio(const struct gpio_desc *desc);
140 /* Child properties interface */
141 struct fwnode_handle;
143 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
144 const char *propname, int index,
145 enum gpiod_flags dflags,
147 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
148 const char *con_id, int index,
149 struct fwnode_handle *child,
150 enum gpiod_flags flags,
153 #else /* CONFIG_GPIOLIB */
155 static inline int gpiod_count(struct device *dev, const char *con_id)
160 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
162 enum gpiod_flags flags)
164 return ERR_PTR(-ENOSYS);
166 static inline struct gpio_desc *__must_check
167 gpiod_get_index(struct device *dev,
170 enum gpiod_flags flags)
172 return ERR_PTR(-ENOSYS);
175 static inline struct gpio_desc *__must_check
176 gpiod_get_optional(struct device *dev, const char *con_id,
177 enum gpiod_flags flags)
182 static inline struct gpio_desc *__must_check
183 gpiod_get_index_optional(struct device *dev, const char *con_id,
184 unsigned int index, enum gpiod_flags flags)
189 static inline struct gpio_descs *__must_check
190 gpiod_get_array(struct device *dev, const char *con_id,
191 enum gpiod_flags flags)
193 return ERR_PTR(-ENOSYS);
196 static inline struct gpio_descs *__must_check
197 gpiod_get_array_optional(struct device *dev, const char *con_id,
198 enum gpiod_flags flags)
203 static inline void gpiod_put(struct gpio_desc *desc)
207 /* GPIO can never have been requested */
211 static inline void gpiod_put_array(struct gpio_descs *descs)
215 /* GPIO can never have been requested */
219 static inline struct gpio_desc *__must_check
220 devm_gpiod_get(struct device *dev,
222 enum gpiod_flags flags)
224 return ERR_PTR(-ENOSYS);
227 struct gpio_desc *__must_check
228 devm_gpiod_get_index(struct device *dev,
231 enum gpiod_flags flags)
233 return ERR_PTR(-ENOSYS);
236 static inline struct gpio_desc *__must_check
237 devm_gpiod_get_optional(struct device *dev, const char *con_id,
238 enum gpiod_flags flags)
243 static inline struct gpio_desc *__must_check
244 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
245 unsigned int index, enum gpiod_flags flags)
250 static inline struct gpio_descs *__must_check
251 devm_gpiod_get_array(struct device *dev, const char *con_id,
252 enum gpiod_flags flags)
254 return ERR_PTR(-ENOSYS);
257 static inline struct gpio_descs *__must_check
258 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
259 enum gpiod_flags flags)
264 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
268 /* GPIO can never have been requested */
272 static inline void devm_gpiod_put_array(struct device *dev,
273 struct gpio_descs *descs)
277 /* GPIO can never have been requested */
282 static inline int gpiod_get_direction(const struct gpio_desc *desc)
284 /* GPIO can never have been requested */
288 static inline int gpiod_direction_input(struct gpio_desc *desc)
290 /* GPIO can never have been requested */
294 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
296 /* GPIO can never have been requested */
300 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
302 /* GPIO can never have been requested */
308 static inline int gpiod_get_value(const struct gpio_desc *desc)
310 /* GPIO can never have been requested */
314 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
316 /* GPIO can never have been requested */
319 static inline void gpiod_set_array_value(unsigned int array_size,
320 struct gpio_desc **desc_array,
323 /* GPIO can never have been requested */
326 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
328 /* GPIO can never have been requested */
332 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
334 /* GPIO can never have been requested */
337 static inline void gpiod_set_raw_array_value(unsigned int array_size,
338 struct gpio_desc **desc_array,
341 /* GPIO can never have been requested */
345 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
347 /* GPIO can never have been requested */
351 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
353 /* GPIO can never have been requested */
356 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
357 struct gpio_desc **desc_array,
360 /* GPIO can never have been requested */
363 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
365 /* GPIO can never have been requested */
369 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
372 /* GPIO can never have been requested */
375 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
376 struct gpio_desc **desc_array,
379 /* GPIO can never have been requested */
383 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
385 /* GPIO can never have been requested */
390 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
392 /* GPIO can never have been requested */
396 static inline int gpiod_cansleep(const struct gpio_desc *desc)
398 /* GPIO can never have been requested */
403 static inline int gpiod_to_irq(const struct gpio_desc *desc)
405 /* GPIO can never have been requested */
410 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
412 return ERR_PTR(-EINVAL);
415 static inline int desc_to_gpio(const struct gpio_desc *desc)
417 /* GPIO can never have been requested */
422 /* Child properties interface */
423 struct fwnode_handle;
426 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
427 const char *propname, int index,
428 enum gpiod_flags dflags,
431 return ERR_PTR(-ENOSYS);
435 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
436 const char *con_id, int index,
437 struct fwnode_handle *child,
438 enum gpiod_flags flags,
441 return ERR_PTR(-ENOSYS);
444 #endif /* CONFIG_GPIOLIB */
447 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
449 struct fwnode_handle *child,
450 enum gpiod_flags flags,
453 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
457 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
459 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
460 int gpiod_export_link(struct device *dev, const char *name,
461 struct gpio_desc *desc);
462 void gpiod_unexport(struct gpio_desc *desc);
464 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
466 static inline int gpiod_export(struct gpio_desc *desc,
467 bool direction_may_change)
472 static inline int gpiod_export_link(struct device *dev, const char *name,
473 struct gpio_desc *desc)
478 static inline void gpiod_unexport(struct gpio_desc *desc)
482 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */