1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
5 #include <linux/kernel.h>
10 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
11 * preferable to the old integer-based handles.
13 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
14 * until the GPIO is released.
20 /* Acquire and dispose GPIOs */
21 struct gpio_desc *__must_check gpiod_get(struct device *dev,
23 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
26 void gpiod_put(struct gpio_desc *desc);
28 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
30 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
33 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
35 int gpiod_get_direction(const struct gpio_desc *desc);
36 int gpiod_direction_input(struct gpio_desc *desc);
37 int gpiod_direction_output(struct gpio_desc *desc, int value);
38 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
40 /* Value get/set from non-sleeping context */
41 int gpiod_get_value(const struct gpio_desc *desc);
42 void gpiod_set_value(struct gpio_desc *desc, int value);
43 int gpiod_get_raw_value(const struct gpio_desc *desc);
44 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
46 /* Value get/set from sleeping context */
47 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
48 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
49 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
50 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
52 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
54 int gpiod_is_active_low(const struct gpio_desc *desc);
55 int gpiod_cansleep(const struct gpio_desc *desc);
57 int gpiod_to_irq(const struct gpio_desc *desc);
59 /* Convert between the old gpio_ and new gpiod_ interfaces */
60 struct gpio_desc *gpio_to_desc(unsigned gpio);
61 int desc_to_gpio(const struct gpio_desc *desc);
63 #else /* CONFIG_GPIOLIB */
65 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
68 return ERR_PTR(-ENOSYS);
70 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
74 return ERR_PTR(-ENOSYS);
76 static inline void gpiod_put(struct gpio_desc *desc)
80 /* GPIO can never have been requested */
84 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
87 return ERR_PTR(-ENOSYS);
90 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
94 return ERR_PTR(-ENOSYS);
96 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
100 /* GPIO can never have been requested */
105 static inline int gpiod_get_direction(const struct gpio_desc *desc)
107 /* GPIO can never have been requested */
111 static inline int gpiod_direction_input(struct gpio_desc *desc)
113 /* GPIO can never have been requested */
117 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
119 /* GPIO can never have been requested */
123 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
125 /* GPIO can never have been requested */
131 static inline int gpiod_get_value(const struct gpio_desc *desc)
133 /* GPIO can never have been requested */
137 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
139 /* GPIO can never have been requested */
142 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
144 /* GPIO can never have been requested */
148 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
150 /* GPIO can never have been requested */
154 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
156 /* GPIO can never have been requested */
160 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
162 /* GPIO can never have been requested */
165 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
167 /* GPIO can never have been requested */
171 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
174 /* GPIO can never have been requested */
178 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
180 /* GPIO can never have been requested */
185 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
187 /* GPIO can never have been requested */
191 static inline int gpiod_cansleep(const struct gpio_desc *desc)
193 /* GPIO can never have been requested */
198 static inline int gpiod_to_irq(const struct gpio_desc *desc)
200 /* GPIO can never have been requested */
205 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
207 return ERR_PTR(-EINVAL);
209 static inline int desc_to_gpio(const struct gpio_desc *desc)
211 /* GPIO can never have been requested */
217 #endif /* CONFIG_GPIOLIB */
219 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
221 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
222 int gpiod_export_link(struct device *dev, const char *name,
223 struct gpio_desc *desc);
224 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
225 void gpiod_unexport(struct gpio_desc *desc);
227 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
229 static inline int gpiod_export(struct gpio_desc *desc,
230 bool direction_may_change)
235 static inline int gpiod_export_link(struct device *dev, const char *name,
236 struct gpio_desc *desc)
241 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
246 static inline void gpiod_unexport(struct gpio_desc *desc)
250 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */