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)
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,
47 /* Return the number of GPIOs associated with a device / function */
48 int gpiod_count(struct device *dev, const char *con_id);
50 /* Acquire and dispose GPIOs */
51 struct gpio_desc *__must_check gpiod_get(struct device *dev,
53 enum gpiod_flags flags);
54 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
57 enum gpiod_flags flags);
58 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
60 enum gpiod_flags flags);
61 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
64 enum gpiod_flags flags);
65 struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
67 enum gpiod_flags flags);
68 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
70 enum gpiod_flags flags);
71 void gpiod_put(struct gpio_desc *desc);
72 void gpiod_put_array(struct gpio_descs *descs);
74 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
76 enum gpiod_flags flags);
77 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
80 enum gpiod_flags flags);
81 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
83 enum gpiod_flags flags);
84 struct gpio_desc *__must_check
85 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
86 unsigned int index, enum gpiod_flags flags);
87 struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
89 enum gpiod_flags flags);
90 struct gpio_descs *__must_check
91 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
92 enum gpiod_flags flags);
93 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
94 void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
96 int gpiod_get_direction(struct gpio_desc *desc);
97 int gpiod_direction_input(struct gpio_desc *desc);
98 int gpiod_direction_output(struct gpio_desc *desc, int value);
99 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
101 /* Value get/set from non-sleeping context */
102 int gpiod_get_value(const struct gpio_desc *desc);
103 void gpiod_set_value(struct gpio_desc *desc, int value);
104 void gpiod_set_array_value(unsigned int array_size,
105 struct gpio_desc **desc_array, int *value_array);
106 int gpiod_get_raw_value(const struct gpio_desc *desc);
107 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
108 void gpiod_set_raw_array_value(unsigned int array_size,
109 struct gpio_desc **desc_array,
112 /* Value get/set from sleeping context */
113 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
114 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
115 void gpiod_set_array_value_cansleep(unsigned int array_size,
116 struct gpio_desc **desc_array,
118 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
119 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
120 void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
121 struct gpio_desc **desc_array,
124 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
126 int gpiod_is_active_low(const struct gpio_desc *desc);
127 int gpiod_cansleep(const struct gpio_desc *desc);
129 int gpiod_to_irq(const struct gpio_desc *desc);
131 /* Convert between the old gpio_ and new gpiod_ interfaces */
132 struct gpio_desc *gpio_to_desc(unsigned gpio);
133 int desc_to_gpio(const struct gpio_desc *desc);
135 /* Child properties interface */
136 struct fwnode_handle;
138 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
139 const char *propname, int index,
140 enum gpiod_flags dflags,
142 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
143 const char *con_id, int index,
144 struct fwnode_handle *child,
145 enum gpiod_flags flags,
148 #else /* CONFIG_GPIOLIB */
150 static inline int gpiod_count(struct device *dev, const char *con_id)
155 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
157 enum gpiod_flags flags)
159 return ERR_PTR(-ENOSYS);
161 static inline struct gpio_desc *__must_check
162 gpiod_get_index(struct device *dev,
165 enum gpiod_flags flags)
167 return ERR_PTR(-ENOSYS);
170 static inline struct gpio_desc *__must_check
171 gpiod_get_optional(struct device *dev, const char *con_id,
172 enum gpiod_flags flags)
177 static inline struct gpio_desc *__must_check
178 gpiod_get_index_optional(struct device *dev, const char *con_id,
179 unsigned int index, enum gpiod_flags flags)
184 static inline struct gpio_descs *__must_check
185 gpiod_get_array(struct device *dev, const char *con_id,
186 enum gpiod_flags flags)
188 return ERR_PTR(-ENOSYS);
191 static inline struct gpio_descs *__must_check
192 gpiod_get_array_optional(struct device *dev, const char *con_id,
193 enum gpiod_flags flags)
198 static inline void gpiod_put(struct gpio_desc *desc)
202 /* GPIO can never have been requested */
206 static inline void gpiod_put_array(struct gpio_descs *descs)
210 /* GPIO can never have been requested */
214 static inline struct gpio_desc *__must_check
215 devm_gpiod_get(struct device *dev,
217 enum gpiod_flags flags)
219 return ERR_PTR(-ENOSYS);
222 struct gpio_desc *__must_check
223 devm_gpiod_get_index(struct device *dev,
226 enum gpiod_flags flags)
228 return ERR_PTR(-ENOSYS);
231 static inline struct gpio_desc *__must_check
232 devm_gpiod_get_optional(struct device *dev, const char *con_id,
233 enum gpiod_flags flags)
238 static inline struct gpio_desc *__must_check
239 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
240 unsigned int index, enum gpiod_flags flags)
245 static inline struct gpio_descs *__must_check
246 devm_gpiod_get_array(struct device *dev, const char *con_id,
247 enum gpiod_flags flags)
249 return ERR_PTR(-ENOSYS);
252 static inline struct gpio_descs *__must_check
253 devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
254 enum gpiod_flags flags)
259 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
263 /* GPIO can never have been requested */
267 static inline void devm_gpiod_put_array(struct device *dev,
268 struct gpio_descs *descs)
272 /* GPIO can never have been requested */
277 static inline int gpiod_get_direction(const struct gpio_desc *desc)
279 /* GPIO can never have been requested */
283 static inline int gpiod_direction_input(struct gpio_desc *desc)
285 /* GPIO can never have been requested */
289 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
291 /* GPIO can never have been requested */
295 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
297 /* GPIO can never have been requested */
303 static inline int gpiod_get_value(const struct gpio_desc *desc)
305 /* GPIO can never have been requested */
309 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
311 /* GPIO can never have been requested */
314 static inline void gpiod_set_array_value(unsigned int array_size,
315 struct gpio_desc **desc_array,
318 /* GPIO can never have been requested */
321 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
323 /* GPIO can never have been requested */
327 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
329 /* GPIO can never have been requested */
332 static inline void gpiod_set_raw_array_value(unsigned int array_size,
333 struct gpio_desc **desc_array,
336 /* GPIO can never have been requested */
340 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
342 /* GPIO can never have been requested */
346 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
348 /* GPIO can never have been requested */
351 static inline void gpiod_set_array_value_cansleep(unsigned int array_size,
352 struct gpio_desc **desc_array,
355 /* GPIO can never have been requested */
358 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
360 /* GPIO can never have been requested */
364 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
367 /* GPIO can never have been requested */
370 static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size,
371 struct gpio_desc **desc_array,
374 /* GPIO can never have been requested */
378 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
380 /* GPIO can never have been requested */
385 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
387 /* GPIO can never have been requested */
391 static inline int gpiod_cansleep(const struct gpio_desc *desc)
393 /* GPIO can never have been requested */
398 static inline int gpiod_to_irq(const struct gpio_desc *desc)
400 /* GPIO can never have been requested */
405 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
410 static inline int desc_to_gpio(const struct gpio_desc *desc)
412 /* GPIO can never have been requested */
417 /* Child properties interface */
418 struct fwnode_handle;
421 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
422 const char *propname, int index,
423 enum gpiod_flags dflags,
426 return ERR_PTR(-ENOSYS);
430 struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
431 const char *con_id, int index,
432 struct fwnode_handle *child,
433 enum gpiod_flags flags,
436 return ERR_PTR(-ENOSYS);
439 #endif /* CONFIG_GPIOLIB */
442 struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev,
444 struct fwnode_handle *child,
445 enum gpiod_flags flags,
448 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child,
452 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
454 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
455 int gpiod_export_link(struct device *dev, const char *name,
456 struct gpio_desc *desc);
457 void gpiod_unexport(struct gpio_desc *desc);
459 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
461 static inline int gpiod_export(struct gpio_desc *desc,
462 bool direction_may_change)
467 static inline int gpiod_export_link(struct device *dev, const char *name,
468 struct gpio_desc *desc)
473 static inline void gpiod_unexport(struct gpio_desc *desc)
477 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */