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,
146 /* FIXME: delete this helper when users are switched over */
147 static inline struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
148 const char *con_id, struct fwnode_handle *child)
150 return devm_fwnode_get_index_gpiod_from_child(dev, con_id,
156 #else /* CONFIG_GPIOLIB */
158 static inline int gpiod_count(struct device *dev, const char *con_id)
163 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
165 enum gpiod_flags flags)
167 return ERR_PTR(-ENOSYS);
169 static inline struct gpio_desc *__must_check
170 gpiod_get_index(struct device *dev,
173 enum gpiod_flags flags)
175 return ERR_PTR(-ENOSYS);
178 static inline struct gpio_desc *__must_check
179 gpiod_get_optional(struct device *dev, const char *con_id,
180 enum gpiod_flags flags)
182 return ERR_PTR(-ENOSYS);
185 static inline struct gpio_desc *__must_check
186 gpiod_get_index_optional(struct device *dev, const char *con_id,
187 unsigned int index, enum gpiod_flags flags)
189 return ERR_PTR(-ENOSYS);
192 static inline struct gpio_descs *__must_check
193 gpiod_get_array(struct device *dev, const char *con_id,
194 enum gpiod_flags flags)
196 return ERR_PTR(-ENOSYS);
199 static inline struct gpio_descs *__must_check
200 gpiod_get_array_optional(struct device *dev, const char *con_id,
201 enum gpiod_flags flags)
203 return ERR_PTR(-ENOSYS);
206 static inline void gpiod_put(struct gpio_desc *desc)
210 /* GPIO can never have been requested */
214 static inline void gpiod_put_array(struct gpio_descs *descs)
218 /* GPIO can never have been requested */
222 static inline struct gpio_desc *__must_check
223 devm_gpiod_get(struct device *dev,
225 enum gpiod_flags flags)
227 return ERR_PTR(-ENOSYS);
230 struct gpio_desc *__must_check
231 devm_gpiod_get_index(struct device *dev,
234 enum gpiod_flags flags)
236 return ERR_PTR(-ENOSYS);
239 static inline struct gpio_desc *__must_check
240 devm_gpiod_get_optional(struct device *dev, const char *con_id,
241 enum gpiod_flags flags)
243 return ERR_PTR(-ENOSYS);
246 static inline struct gpio_desc *__must_check
247 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
248 unsigned int index, enum gpiod_flags flags)
250 return ERR_PTR(-ENOSYS);
253 static inline struct gpio_descs *__must_check
254 devm_gpiod_get_array(struct device *dev, const char *con_id,
255 enum gpiod_flags flags)
257 return ERR_PTR(-ENOSYS);
260 static inline struct gpio_descs *__must_check
261 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
262 enum gpiod_flags flags)
264 return ERR_PTR(-ENOSYS);
267 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
271 /* GPIO can never have been requested */
275 static inline void devm_gpiod_put_array(struct device *dev,
276 struct gpio_descs *descs)
280 /* GPIO can never have been requested */
285 static inline int gpiod_get_direction(const struct gpio_desc *desc)
287 /* GPIO can never have been requested */
291 static inline int gpiod_direction_input(struct gpio_desc *desc)
293 /* GPIO can never have been requested */
297 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
299 /* GPIO can never have been requested */
303 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
305 /* GPIO can never have been requested */
311 static inline int gpiod_get_value(const struct gpio_desc *desc)
313 /* GPIO can never have been requested */
317 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
319 /* GPIO can never have been requested */
322 static inline void gpiod_set_array_value(unsigned int array_size,
323 struct gpio_desc **desc_array,
326 /* GPIO can never have been requested */
329 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
331 /* GPIO can never have been requested */
335 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
337 /* GPIO can never have been requested */
340 static inline void gpiod_set_raw_array_value(unsigned int array_size,
341 struct gpio_desc **desc_array,
344 /* GPIO can never have been requested */
348 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
350 /* GPIO can never have been requested */
354 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
356 /* GPIO can never have been requested */
359 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
360 struct gpio_desc **desc_array,
363 /* GPIO can never have been requested */
366 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
368 /* GPIO can never have been requested */
372 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
375 /* GPIO can never have been requested */
378 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
379 struct gpio_desc **desc_array,
382 /* GPIO can never have been requested */
386 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
388 /* GPIO can never have been requested */
393 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
395 /* GPIO can never have been requested */
399 static inline int gpiod_cansleep(const struct gpio_desc *desc)
401 /* GPIO can never have been requested */
406 static inline int gpiod_to_irq(const struct gpio_desc *desc)
408 /* GPIO can never have been requested */
413 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
415 return ERR_PTR(-EINVAL);
418 static inline int desc_to_gpio(const struct gpio_desc *desc)
420 /* GPIO can never have been requested */
425 /* Child properties interface */
426 struct fwnode_handle;
429 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
430 const char *propname, int index,
431 enum gpiod_flags dflags,
434 return ERR_PTR(-ENOSYS);
438 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
439 const char *con_id, int index,
440 struct fwnode_handle *child,
441 enum gpiod_flags flags,
444 return ERR_PTR(-ENOSYS);
447 /* FIXME: delete this when all users are switched over */
448 static inline struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
449 const char *con_id, struct fwnode_handle *child)
451 return ERR_PTR(-ENOSYS);
454 #endif /* CONFIG_GPIOLIB */
457 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
459 struct fwnode_handle *child,
460 enum gpiod_flags flags,
463 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
467 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
469 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
470 int gpiod_export_link(struct device *dev, const char *name,
471 struct gpio_desc *desc);
472 void gpiod_unexport(struct gpio_desc *desc);
474 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
476 static inline int gpiod_export(struct gpio_desc *desc,
477 bool direction_may_change)
482 static inline int gpiod_export_link(struct device *dev, const char *name,
483 struct gpio_desc *desc)
488 static inline void gpiod_unexport(struct gpio_desc *desc)
492 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */