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_value(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_value(unsigned int array_size,
108 struct gpio_desc **desc_array,
111 /* Value get/set from sleeping context */
112 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
113 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
114 void gpiod_set_array_value_cansleep(unsigned int array_size,
115 struct gpio_desc **desc_array,
117 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
118 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
119 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
120 struct gpio_desc **desc_array,
123 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
125 int gpiod_is_active_low(const struct gpio_desc *desc);
126 int gpiod_cansleep(const struct gpio_desc *desc);
128 int gpiod_to_irq(const struct gpio_desc *desc);
130 /* Convert between the old gpio_ and new gpiod_ interfaces */
131 struct gpio_desc *gpio_to_desc(unsigned gpio);
132 int desc_to_gpio(const struct gpio_desc *desc);
134 /* Child properties interface */
135 struct fwnode_handle;
137 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
138 const char *propname, int index,
139 enum gpiod_flags dflags,
141 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
142 const char *con_id, int index,
143 struct fwnode_handle *child,
144 enum gpiod_flags flags,
147 #else /* CONFIG_GPIOLIB */
149 static inline int gpiod_count(struct device *dev, const char *con_id)
154 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
156 enum gpiod_flags flags)
158 return ERR_PTR(-ENOSYS);
160 static inline struct gpio_desc *__must_check
161 gpiod_get_index(struct device *dev,
164 enum gpiod_flags flags)
166 return ERR_PTR(-ENOSYS);
169 static inline struct gpio_desc *__must_check
170 gpiod_get_optional(struct device *dev, const char *con_id,
171 enum gpiod_flags flags)
173 return ERR_PTR(-ENOSYS);
176 static inline struct gpio_desc *__must_check
177 gpiod_get_index_optional(struct device *dev, const char *con_id,
178 unsigned int index, enum gpiod_flags flags)
180 return ERR_PTR(-ENOSYS);
183 static inline struct gpio_descs *__must_check
184 gpiod_get_array(struct device *dev, const char *con_id,
185 enum gpiod_flags flags)
187 return ERR_PTR(-ENOSYS);
190 static inline struct gpio_descs *__must_check
191 gpiod_get_array_optional(struct device *dev, const char *con_id,
192 enum gpiod_flags flags)
194 return ERR_PTR(-ENOSYS);
197 static inline void gpiod_put(struct gpio_desc *desc)
201 /* GPIO can never have been requested */
205 static inline void gpiod_put_array(struct gpio_descs *descs)
209 /* GPIO can never have been requested */
213 static inline struct gpio_desc *__must_check
214 devm_gpiod_get(struct device *dev,
216 enum gpiod_flags flags)
218 return ERR_PTR(-ENOSYS);
221 struct gpio_desc *__must_check
222 devm_gpiod_get_index(struct device *dev,
225 enum gpiod_flags flags)
227 return ERR_PTR(-ENOSYS);
230 static inline struct gpio_desc *__must_check
231 devm_gpiod_get_optional(struct device *dev, const char *con_id,
232 enum gpiod_flags flags)
234 return ERR_PTR(-ENOSYS);
237 static inline struct gpio_desc *__must_check
238 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
239 unsigned int index, enum gpiod_flags flags)
241 return ERR_PTR(-ENOSYS);
244 static inline struct gpio_descs *__must_check
245 devm_gpiod_get_array(struct device *dev, const char *con_id,
246 enum gpiod_flags flags)
248 return ERR_PTR(-ENOSYS);
251 static inline struct gpio_descs *__must_check
252 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
253 enum gpiod_flags flags)
255 return ERR_PTR(-ENOSYS);
258 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
262 /* GPIO can never have been requested */
266 static inline void devm_gpiod_put_array(struct device *dev,
267 struct gpio_descs *descs)
271 /* GPIO can never have been requested */
276 static inline int gpiod_get_direction(const struct gpio_desc *desc)
278 /* GPIO can never have been requested */
282 static inline int gpiod_direction_input(struct gpio_desc *desc)
284 /* GPIO can never have been requested */
288 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
290 /* GPIO can never have been requested */
294 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
296 /* GPIO can never have been requested */
302 static inline int gpiod_get_value(const struct gpio_desc *desc)
304 /* GPIO can never have been requested */
308 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
310 /* GPIO can never have been requested */
313 static inline void gpiod_set_array_value(unsigned int array_size,
314 struct gpio_desc **desc_array,
317 /* GPIO can never have been requested */
320 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
322 /* GPIO can never have been requested */
326 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
328 /* GPIO can never have been requested */
331 static inline void gpiod_set_raw_array_value(unsigned int array_size,
332 struct gpio_desc **desc_array,
335 /* GPIO can never have been requested */
339 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
341 /* GPIO can never have been requested */
345 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
347 /* GPIO can never have been requested */
350 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
351 struct gpio_desc **desc_array,
354 /* GPIO can never have been requested */
357 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
359 /* GPIO can never have been requested */
363 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
366 /* GPIO can never have been requested */
369 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
370 struct gpio_desc **desc_array,
373 /* GPIO can never have been requested */
377 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
379 /* GPIO can never have been requested */
384 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
386 /* GPIO can never have been requested */
390 static inline int gpiod_cansleep(const struct gpio_desc *desc)
392 /* GPIO can never have been requested */
397 static inline int gpiod_to_irq(const struct gpio_desc *desc)
399 /* GPIO can never have been requested */
404 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
406 return ERR_PTR(-EINVAL);
409 static inline int desc_to_gpio(const struct gpio_desc *desc)
411 /* GPIO can never have been requested */
416 /* Child properties interface */
417 struct fwnode_handle;
420 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
421 const char *propname, int index,
422 enum gpiod_flags dflags,
425 return ERR_PTR(-ENOSYS);
429 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
430 const char *con_id, int index,
431 struct fwnode_handle *child,
432 enum gpiod_flags flags,
435 return ERR_PTR(-ENOSYS);
438 #endif /* CONFIG_GPIOLIB */
441 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
443 struct fwnode_handle *child,
444 enum gpiod_flags flags,
447 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
451 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
453 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
454 int gpiod_export_link(struct device *dev, const char *name,
455 struct gpio_desc *desc);
456 void gpiod_unexport(struct gpio_desc *desc);
458 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
460 static inline int gpiod_export(struct gpio_desc *desc,
461 bool direction_may_change)
466 static inline int gpiod_export_link(struct device *dev, const char *name,
467 struct gpio_desc *desc)
472 static inline void gpiod_unexport(struct gpio_desc *desc)
476 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */