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 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
31 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
34 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
36 int gpiod_get_direction(const struct gpio_desc *desc);
37 int gpiod_direction_input(struct gpio_desc *desc);
38 int gpiod_direction_output(struct gpio_desc *desc, int value);
39 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
41 /* Value get/set from non-sleeping context */
42 int gpiod_get_value(const struct gpio_desc *desc);
43 void gpiod_set_value(struct gpio_desc *desc, int value);
44 int gpiod_get_raw_value(const struct gpio_desc *desc);
45 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
47 /* Value get/set from sleeping context */
48 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
49 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
50 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
51 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
53 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
55 int gpiod_is_active_low(const struct gpio_desc *desc);
56 int gpiod_cansleep(const struct gpio_desc *desc);
58 int gpiod_to_irq(const struct gpio_desc *desc);
60 /* Convert between the old gpio_ and new gpiod_ interfaces */
61 struct gpio_desc *gpio_to_desc(unsigned gpio);
62 int desc_to_gpio(const struct gpio_desc *desc);
63 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
65 #else /* CONFIG_GPIOLIB */
67 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
70 return ERR_PTR(-ENOSYS);
72 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
76 return ERR_PTR(-ENOSYS);
78 static inline void gpiod_put(struct gpio_desc *desc)
82 /* GPIO can never have been requested */
86 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
89 return ERR_PTR(-ENOSYS);
92 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
96 return ERR_PTR(-ENOSYS);
98 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
102 /* GPIO can never have been requested */
107 static inline int gpiod_get_direction(const struct gpio_desc *desc)
109 /* GPIO can never have been requested */
113 static inline int gpiod_direction_input(struct gpio_desc *desc)
115 /* GPIO can never have been requested */
119 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
121 /* GPIO can never have been requested */
125 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
127 /* GPIO can never have been requested */
133 static inline int gpiod_get_value(const struct gpio_desc *desc)
135 /* GPIO can never have been requested */
139 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
141 /* GPIO can never have been requested */
144 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
146 /* GPIO can never have been requested */
150 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
152 /* GPIO can never have been requested */
156 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
158 /* GPIO can never have been requested */
162 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
164 /* GPIO can never have been requested */
167 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
169 /* GPIO can never have been requested */
173 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
176 /* GPIO can never have been requested */
180 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
182 /* GPIO can never have been requested */
187 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
189 /* GPIO can never have been requested */
193 static inline int gpiod_cansleep(const struct gpio_desc *desc)
195 /* GPIO can never have been requested */
200 static inline int gpiod_to_irq(const struct gpio_desc *desc)
202 /* GPIO can never have been requested */
207 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
209 return ERR_PTR(-EINVAL);
211 static inline int desc_to_gpio(const struct gpio_desc *desc)
213 /* GPIO can never have been requested */
217 static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
219 /* GPIO can never have been requested */
221 return ERR_PTR(-ENODEV);
225 #endif /* CONFIG_GPIOLIB */
227 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
229 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
230 int gpiod_export_link(struct device *dev, const char *name,
231 struct gpio_desc *desc);
232 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
233 void gpiod_unexport(struct gpio_desc *desc);
235 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
237 static inline int gpiod_export(struct gpio_desc *desc,
238 bool direction_may_change)
243 static inline int gpiod_export_link(struct device *dev, const char *name,
244 struct gpio_desc *desc)
249 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
254 static inline void gpiod_unexport(struct gpio_desc *desc)
258 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */