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,
114 struct fwnode_handle *child);
115 #else /* CONFIG_GPIOLIB */
117 static inline struct gpio_desc *__must_check __gpiod_get(struct device *dev,
119 enum gpiod_flags flags)
121 return ERR_PTR(-ENOSYS);
123 static inline struct gpio_desc *__must_check
124 __gpiod_get_index(struct device *dev,
127 enum gpiod_flags flags)
129 return ERR_PTR(-ENOSYS);
132 static inline struct gpio_desc *__must_check
133 __gpiod_get_optional(struct device *dev, const char *con_id,
134 enum gpiod_flags flags)
136 return ERR_PTR(-ENOSYS);
139 static inline struct gpio_desc *__must_check
140 __gpiod_get_index_optional(struct device *dev, const char *con_id,
141 unsigned int index, enum gpiod_flags flags)
143 return ERR_PTR(-ENOSYS);
146 static inline void gpiod_put(struct gpio_desc *desc)
150 /* GPIO can never have been requested */
154 static inline struct gpio_desc *__must_check
155 __devm_gpiod_get(struct device *dev,
157 enum gpiod_flags flags)
159 return ERR_PTR(-ENOSYS);
162 struct gpio_desc *__must_check
163 __devm_gpiod_get_index(struct device *dev,
166 enum gpiod_flags flags)
168 return ERR_PTR(-ENOSYS);
171 static inline struct gpio_desc *__must_check
172 __devm_gpiod_get_optional(struct device *dev, const char *con_id,
173 enum gpiod_flags flags)
175 return ERR_PTR(-ENOSYS);
178 static inline struct gpio_desc *__must_check
179 __devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
180 unsigned int index, enum gpiod_flags flags)
182 return ERR_PTR(-ENOSYS);
185 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
189 /* GPIO can never have been requested */
194 static inline int gpiod_get_direction(const struct gpio_desc *desc)
196 /* GPIO can never have been requested */
200 static inline int gpiod_direction_input(struct gpio_desc *desc)
202 /* GPIO can never have been requested */
206 static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
208 /* GPIO can never have been requested */
212 static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
214 /* GPIO can never have been requested */
220 static inline int gpiod_get_value(const struct gpio_desc *desc)
222 /* GPIO can never have been requested */
226 static inline void gpiod_set_value(struct gpio_desc *desc, int value)
228 /* GPIO can never have been requested */
231 static inline void gpiod_set_array(unsigned int array_size,
232 struct gpio_desc **desc_array,
235 /* GPIO can never have been requested */
238 static inline int gpiod_get_raw_value(const struct gpio_desc *desc)
240 /* GPIO can never have been requested */
244 static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)
246 /* GPIO can never have been requested */
249 static inline void gpiod_set_raw_array(unsigned int array_size,
250 struct gpio_desc **desc_array,
253 /* GPIO can never have been requested */
257 static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc)
259 /* GPIO can never have been requested */
263 static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
265 /* GPIO can never have been requested */
268 static inline void gpiod_set_array_cansleep(unsigned int array_size,
269 struct gpio_desc **desc_array,
272 /* GPIO can never have been requested */
275 static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
277 /* GPIO can never have been requested */
281 static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,
284 /* GPIO can never have been requested */
287 static inline void gpiod_set_raw_array_cansleep(unsigned int array_size,
288 struct gpio_desc **desc_array,
291 /* GPIO can never have been requested */
295 static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
297 /* GPIO can never have been requested */
302 static inline int gpiod_is_active_low(const struct gpio_desc *desc)
304 /* GPIO can never have been requested */
308 static inline int gpiod_cansleep(const struct gpio_desc *desc)
310 /* GPIO can never have been requested */
315 static inline int gpiod_to_irq(const struct gpio_desc *desc)
317 /* GPIO can never have been requested */
322 static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
324 return ERR_PTR(-EINVAL);
326 static inline int desc_to_gpio(const struct gpio_desc *desc)
328 /* GPIO can never have been requested */
333 #endif /* CONFIG_GPIOLIB */
336 * Vararg-hacks! This is done to transition the kernel to always pass
337 * the options flags argument to the below functions. During a transition
338 * phase these vararg macros make both old-and-newstyle code compile,
339 * but when all calls to the elder API are removed, these should go away
340 * and the __gpiod_get() etc functions above be renamed just gpiod_get()
343 #define __gpiod_get(dev, con_id, flags, ...) __gpiod_get(dev, con_id, flags)
344 #define gpiod_get(varargs...) __gpiod_get(varargs, GPIOD_ASIS)
345 #define __gpiod_get_index(dev, con_id, index, flags, ...) \
346 __gpiod_get_index(dev, con_id, index, flags)
347 #define gpiod_get_index(varargs...) __gpiod_get_index(varargs, GPIOD_ASIS)
348 #define __gpiod_get_optional(dev, con_id, flags, ...) \
349 __gpiod_get_optional(dev, con_id, flags)
350 #define gpiod_get_optional(varargs...) __gpiod_get_optional(varargs, GPIOD_ASIS)
351 #define __gpiod_get_index_optional(dev, con_id, index, flags, ...) \
352 __gpiod_get_index_optional(dev, con_id, index, flags)
353 #define gpiod_get_index_optional(varargs...) \
354 __gpiod_get_index_optional(varargs, GPIOD_ASIS)
355 #define __devm_gpiod_get(dev, con_id, flags, ...) \
356 __devm_gpiod_get(dev, con_id, flags)
357 #define devm_gpiod_get(varargs...) __devm_gpiod_get(varargs, GPIOD_ASIS)
358 #define __devm_gpiod_get_index(dev, con_id, index, flags, ...) \
359 __devm_gpiod_get_index(dev, con_id, index, flags)
360 #define devm_gpiod_get_index(varargs...) \
361 __devm_gpiod_get_index(varargs, GPIOD_ASIS)
362 #define __devm_gpiod_get_optional(dev, con_id, flags, ...) \
363 __devm_gpiod_get_optional(dev, con_id, flags)
364 #define devm_gpiod_get_optional(varargs...) \
365 __devm_gpiod_get_optional(varargs, GPIOD_ASIS)
366 #define __devm_gpiod_get_index_optional(dev, con_id, index, flags, ...) \
367 __devm_gpiod_get_index_optional(dev, con_id, index, flags)
368 #define devm_gpiod_get_index_optional(varargs...) \
369 __devm_gpiod_get_index_optional(varargs, GPIOD_ASIS)
371 #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
373 int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
374 int gpiod_export_link(struct device *dev, const char *name,
375 struct gpio_desc *desc);
376 int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
377 void gpiod_unexport(struct gpio_desc *desc);
379 #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
381 static inline int gpiod_export(struct gpio_desc *desc,
382 bool direction_may_change)
387 static inline int gpiod_export_link(struct device *dev, const char *name,
388 struct gpio_desc *desc)
393 static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
398 static inline void gpiod_unexport(struct gpio_desc *desc)
402 #endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */