1 #ifndef __LINUX_GPIO_CONSUMER_H
2 #define __LINUX_GPIO_CONSUMER_H
6 #include <linux/kernel.h>
11 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are
12 * preferable to the old integer-based handles.
14 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid
15 * until the GPIO is released.
19 #define GPIOD_FLAGS_BIT_DIR_SET BIT(0)
20 #define GPIOD_FLAGS_BIT_DIR_OUT BIT(1)
21 #define GPIOD_FLAGS_BIT_DIR_VAL BIT(2)
24 * Optional flags that can be passed to one of gpiod_* to configure direction
25 * and output value. These values cannot be OR'd.
29 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET,
30 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT,
31 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT |
32 GPIOD_FLAGS_BIT_DIR_VAL,
37 /* Acquire and dispose GPIOs */
38 struct gpio_desc *__must_check __gpiod_get(struct device *dev,
40 enum gpiod_flags flags);
41 struct gpio_desc *__must_check __gpiod_get_index(struct device *dev,
44 enum gpiod_flags flags);
45 struct gpio_desc *__must_check __gpiod_get_optional(struct device *dev,
47 enum gpiod_flags flags);
48 struct gpio_desc *__must_check __gpiod_get_index_optional(struct device *dev,
51 enum gpiod_flags flags);
52 void gpiod_put(struct gpio_desc *desc);
54 struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
56 enum gpiod_flags flags);
57 struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
60 enum gpiod_flags flags);
61 struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
63 enum gpiod_flags flags);
64 struct gpio_desc *__must_check
65 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
66 unsigned int index, enum gpiod_flags flags);
67 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
69 int gpiod_get_direction(struct gpio_desc *desc);
70 int gpiod_direction_input(struct gpio_desc *desc);
71 int gpiod_direction_output(struct gpio_desc *desc, int value);
72 int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
74 /* Value get/set from non-sleeping context */
75 int gpiod_get_value(const struct gpio_desc *desc);
76 void gpiod_set_value(struct gpio_desc *desc, int value);
77 void gpiod_set_array(unsigned int array_size,
78 struct gpio_desc **desc_array, int *value_array);
79 int gpiod_get_raw_value(const struct gpio_desc *desc);
80 void gpiod_set_raw_value(struct gpio_desc *desc, int value);
81 void gpiod_set_raw_array(unsigned int array_size,
82 struct gpio_desc **desc_array, int *value_array);
84 /* Value get/set from sleeping context */
85 int gpiod_get_value_cansleep(const struct gpio_desc *desc);
86 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
87 void gpiod_set_array_cansleep(unsigned int array_size,
88 struct gpio_desc **desc_array,
90 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc);
91 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value);
92 void gpiod_set_raw_array_cansleep(unsigned int array_size,
93 struct gpio_desc **desc_array,
96 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
98 int gpiod_is_active_low(const struct gpio_desc *desc);
99 int gpiod_cansleep(const struct gpio_desc *desc);
101 int gpiod_to_irq(const struct gpio_desc *desc);
103 /* Convert between the old gpio_ and new gpiod_ interfaces */
104 struct gpio_desc *gpio_to_desc(unsigned gpio);
105 int desc_to_gpio(const struct gpio_desc *desc);
107 /* Child properties interface */
108 struct fwnode_handle;
110 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
111 const char *propname);
112 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
113 struct fwnode_handle *child);
114 #else /* CONFIG_GPIOLIB */
116 static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
118 enum gpiod_flags flags)
120 return ERR_PTR(-ENOSYS);
122 static inline struct gpio_desc *__must_check
123 __gpiod_get_index(struct device *dev,
126 enum gpiod_flags flags)
128 return ERR_PTR(-ENOSYS);
131 static inline struct gpio_desc *__must_check
132 __gpiod_get_optional(struct device *dev, const char *con_id,
133 enum gpiod_flags flags)
135 return ERR_PTR(-ENOSYS);
138 static inline struct gpio_desc *__must_check
139 __gpiod_get_index_optional(struct device *dev, const char *con_id,
140 unsigned int index, enum gpiod_flags flags)
142 return ERR_PTR(-ENOSYS);
145 static inline void gpiod_put(struct gpio_desc *desc)
149 /* GPIO can never have been requested */
153 static inline struct gpio_desc *__must_check
154 __devm_gpiod_get(struct device *dev,
156 enum gpiod_flags flags)
158 return ERR_PTR(-ENOSYS);
161 struct gpio_desc *__must_check
162 __devm_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 __devm_gpiod_get_optional(struct device *dev, const char *con_id,
172 enum gpiod_flags flags)
174 return ERR_PTR(-ENOSYS);
177 static inline struct gpio_desc *__must_check
178 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
179 unsigned int index, enum gpiod_flags flags)
181 return ERR_PTR(-ENOSYS);
184 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
188 /* GPIO can never have been requested */
193 static inline int gpiod_get_direction(const struct gpio_desc *desc)
195 /* GPIO can never have been requested */
199 static inline int gpiod_direction_input(struct gpio_desc *desc)
201 /* GPIO can never have been requested */
205 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
207 /* GPIO can never have been requested */
211 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
213 /* GPIO can never have been requested */
219 static inline int gpiod_get_value(const struct gpio_desc *desc)
221 /* GPIO can never have been requested */
225 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
227 /* GPIO can never have been requested */
230 static inline void gpiod_set_array(unsigned int array_size,
231 struct gpio_desc **desc_array,
234 /* GPIO can never have been requested */
237 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
239 /* GPIO can never have been requested */
243 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
245 /* GPIO can never have been requested */
248 static inline void gpiod_set_raw_array(unsigned int array_size,
249 struct gpio_desc **desc_array,
252 /* GPIO can never have been requested */
256 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
258 /* GPIO can never have been requested */
262 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
264 /* GPIO can never have been requested */
267 static inline void gpiod_set_array_cansleep(unsigned int array_size,
268 struct gpio_desc **desc_array,
271 /* GPIO can never have been requested */
274 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
276 /* GPIO can never have been requested */
280 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
283 /* GPIO can never have been requested */
286 static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
287 struct gpio_desc **desc_array,
290 /* GPIO can never have been requested */
294 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
296 /* GPIO can never have been requested */
301 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
303 /* GPIO can never have been requested */
307 static inline int gpiod_cansleep(const struct gpio_desc *desc)
309 /* GPIO can never have been requested */
314 static inline int gpiod_to_irq(const struct gpio_desc *desc)
316 /* GPIO can never have been requested */
321 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
323 return ERR_PTR(-EINVAL);
325 static inline int desc_to_gpio(const struct gpio_desc *desc)
327 /* GPIO can never have been requested */
332 #endif /* CONFIG_GPIOLIB */
335 * Vararg-hacks! This is done to transition the kernel to always pass
336 * the options flags argument to the below functions. During a transition
337 * phase these vararg macros make both old-and-newstyle code compile,
338 * but when all calls to the elder API are removed, these should go away
339 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
342 #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
343 #define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
344 #define __gpiod_get_index(dev, con_id, index, flags, ...) \
345 __gpiod_get_index(dev, con_id, index, flags)
346 #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
347 #define __gpiod_get_optional(dev, con_id, flags, ...) \
348 __gpiod_get_optional(dev, con_id, flags)
349 #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
350 #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
351 __gpiod_get_index_optional(dev, con_id, index, flags)
352 #define gpiod_get_index_optional(varargs...) \
353 __gpiod_get_index_optional(varargs, GPIOD_ASIS)
354 #define __devm_gpiod_get(dev, con_id, flags, ...) \
355 __devm_gpiod_get(dev, con_id, flags)
356 #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
357 #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
358 __devm_gpiod_get_index(dev, con_id, index, flags)
359 #define devm_gpiod_get_index(varargs...) \
360 __devm_gpiod_get_index(varargs, GPIOD_ASIS)
361 #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
362 __devm_gpiod_get_optional(dev, con_id, flags)
363 #define devm_gpiod_get_optional(varargs...) \
364 __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
365 #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
366 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
367 #define devm_gpiod_get_index_optional(varargs...) \
368 __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
370 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
372 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
373 int gpiod_export_link(struct device *dev, const char *name,
374 struct gpio_desc *desc);
375 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
376 void gpiod_unexport(struct gpio_desc *desc);
378 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
380 static inline int gpiod_export(struct gpio_desc *desc,
381 bool direction_may_change)
386 static inline int gpiod_export_link(struct device *dev, const char *name,
387 struct gpio_desc *desc)
392 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
397 static inline void gpiod_unexport(struct gpio_desc *desc)
401 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */