1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
5 #include <linux/kernel.h>
13 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
14 * preferable to the old integer-based handles.
16 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
17 * until the GPIO is released.
21 /* Acquire and dispose GPIOs */
22 struct gpio_desc *__must_check gpiod_get(struct device *dev,
24 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
27 void gpiod_put(struct gpio_desc *desc);
29 int gpiod_get_direction(const struct gpio_desc *desc);
30 int gpiod_direction_input(struct gpio_desc *desc);
31 int gpiod_direction_output(struct gpio_desc *desc, int value);
33 /* Value get/set from non-sleeping context */
34 int gpiod_get_value(const struct gpio_desc *desc);
35 void gpiod_set_value(struct gpio_desc *desc, int value);
36 int gpiod_get_raw_value(const struct gpio_desc *desc);
37 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
39 /* Value get/set from sleeping context */
40 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
41 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
42 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
43 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
45 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
47 int gpiod_is_active_low(const struct gpio_desc *desc);
48 int gpiod_cansleep(const struct gpio_desc *desc);
50 int gpiod_to_irq(const struct gpio_desc *desc);
52 /* Convert between the old gpio_ and new gpiod_ interfaces */
53 struct gpio_desc *gpio_to_desc(unsigned gpio);
54 int desc_to_gpio(const struct gpio_desc *desc);
55 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
57 #else /* CONFIG_GPIOLIB */
59 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
62 return ERR_PTR(-ENOSYS);
65 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
69 return ERR_PTR(-ENOSYS);
72 static inline void gpiod_put(struct gpio_desc *desc)
76 /* GPIO can never have been requested */
80 static inline int gpiod_get_direction(const struct gpio_desc *desc)
82 /* GPIO can never have been requested */
87 static inline int gpiod_direction_input(struct gpio_desc *desc)
89 /* GPIO can never have been requested */
94 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
96 /* GPIO can never have been requested */
101 static inline int gpiod_get_value(const struct gpio_desc *desc)
103 /* GPIO can never have been requested */
108 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
110 /* GPIO can never have been requested */
114 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
116 /* GPIO can never have been requested */
121 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
123 /* GPIO can never have been requested */
127 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
129 /* GPIO can never have been requested */
134 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
136 /* GPIO can never have been requested */
140 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
142 /* GPIO can never have been requested */
147 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
150 /* GPIO can never have been requested */
154 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
156 /* GPIO can never have been requested */
161 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
163 /* GPIO can never have been requested */
168 static inline int gpiod_cansleep(const struct gpio_desc *desc)
170 /* GPIO can never have been requested */
175 static inline int gpiod_to_irq(const struct gpio_desc *desc)
177 /* GPIO can never have been requested */
182 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
184 return ERR_PTR(-EINVAL);
187 static inline int desc_to_gpio(const struct gpio_desc *desc)
189 /* GPIO can never have been requested */
194 static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
196 /* GPIO can never have been requested */
198 return ERR_PTR(-ENODEV);
201 #endif /* CONFIG_GPIOLIB */
203 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
205 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
206 int gpiod_export_link(struct device *dev, const char *name,
207 struct gpio_desc *desc);
208 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
209 void gpiod_unexport(struct gpio_desc *desc);
211 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
213 static inline int gpiod_export(struct gpio_desc *desc,
214 bool direction_may_change)
219 static inline int gpiod_export_link(struct device *dev, const char *name,
220 struct gpio_desc *desc)
225 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
230 static inline void gpiod_unexport(struct gpio_desc *desc)
234 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
236 #ifdef CONFIG_GPIO_DEVRES
237 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
239 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
242 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
244 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
247 return ERR_PTR(-ENOSYS);
251 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
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 */