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 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
28 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
32 void gpiod_put(struct gpio_desc *desc);
34 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
36 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
39 struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
41 struct gpio_desc *__must_check
42 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
45 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
47 int gpiod_get_direction(const struct gpio_desc *desc);
48 int gpiod_direction_input(struct gpio_desc *desc);
49 int gpiod_direction_output(struct gpio_desc *desc, int value);
50 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
52 /* Value get/set from non-sleeping context */
53 int gpiod_get_value(const struct gpio_desc *desc);
54 void gpiod_set_value(struct gpio_desc *desc, int value);
55 int gpiod_get_raw_value(const struct gpio_desc *desc);
56 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
58 /* Value get/set from sleeping context */
59 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
60 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
61 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
62 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
64 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
66 int gpiod_is_active_low(const struct gpio_desc *desc);
67 int gpiod_cansleep(const struct gpio_desc *desc);
69 int gpiod_to_irq(const struct gpio_desc *desc);
71 /* Convert between the old gpio_ and new gpiod_ interfaces */
72 struct gpio_desc *gpio_to_desc(unsigned gpio);
73 int desc_to_gpio(const struct gpio_desc *desc);
75 #else /* CONFIG_GPIOLIB */
77 static inline struct gpio_desc *__must_check gpiod_get(struct device *dev,
80 return ERR_PTR(-ENOSYS);
82 static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
86 return ERR_PTR(-ENOSYS);
89 static inline struct gpio_desc *__must_check
90 gpiod_get_optional(struct device *dev, const char *con_id)
92 return ERR_PTR(-ENOSYS);
95 static inline struct gpio_desc *__must_check
96 gpiod_get_index_optional(struct device *dev, const char *con_id,
99 return ERR_PTR(-ENOSYS);
102 static inline void gpiod_put(struct gpio_desc *desc)
106 /* GPIO can never have been requested */
110 static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
113 return ERR_PTR(-ENOSYS);
116 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
120 return ERR_PTR(-ENOSYS);
123 static inline struct gpio_desc *__must_check
124 devm_gpiod_get_optional(struct device *dev, const char *con_id)
126 return ERR_PTR(-ENOSYS);
129 static inline struct gpio_desc *__must_check
130 devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
133 return ERR_PTR(-ENOSYS);
136 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
140 /* GPIO can never have been requested */
145 static inline int gpiod_get_direction(const struct gpio_desc *desc)
147 /* GPIO can never have been requested */
151 static inline int gpiod_direction_input(struct gpio_desc *desc)
153 /* GPIO can never have been requested */
157 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
159 /* GPIO can never have been requested */
163 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
165 /* GPIO can never have been requested */
171 static inline int gpiod_get_value(const struct gpio_desc *desc)
173 /* GPIO can never have been requested */
177 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
179 /* GPIO can never have been requested */
182 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
184 /* GPIO can never have been requested */
188 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
190 /* GPIO can never have been requested */
194 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
196 /* GPIO can never have been requested */
200 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
202 /* GPIO can never have been requested */
205 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
207 /* GPIO can never have been requested */
211 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
214 /* GPIO can never have been requested */
218 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
220 /* GPIO can never have been requested */
225 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
227 /* GPIO can never have been requested */
231 static inline int gpiod_cansleep(const struct gpio_desc *desc)
233 /* GPIO can never have been requested */
238 static inline int gpiod_to_irq(const struct gpio_desc *desc)
240 /* GPIO can never have been requested */
245 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
247 return ERR_PTR(-EINVAL);
249 static inline int desc_to_gpio(const struct gpio_desc *desc)
251 /* GPIO can never have been requested */
257 #endif /* CONFIG_GPIOLIB */
259 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
261 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
262 int gpiod_export_link(struct device *dev, const char *name,
263 struct gpio_desc *desc);
264 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
265 void gpiod_unexport(struct gpio_desc *desc);
267 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
269 static inline int gpiod_export(struct gpio_desc *desc,
270 bool direction_may_change)
275 static inline int gpiod_export_link(struct device *dev, const char *name,
276 struct gpio_desc *desc)
281 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
286 static inline void gpiod_unexport(struct gpio_desc *desc)
290 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */