consumer.h revision ef70bbe1aaa612f75360e5df5952fddec50b7ca9
1#ifndef __LINUX_GPIO_CONSUMER_H 2#define __LINUX_GPIO_CONSUMER_H 3 4#include <linux/err.h> 5#include <linux/kernel.h> 6 7#ifdef CONFIG_GPIOLIB 8 9struct device; 10struct gpio_chip; 11 12/** 13 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are 14 * preferable to the old integer-based handles. 15 * 16 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid 17 * until the GPIO is released. 18 */ 19struct gpio_desc; 20 21/* Acquire and dispose GPIOs */ 22struct gpio_desc *__must_check gpiod_get(struct device *dev, 23 const char *con_id); 24struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 25 const char *con_id, 26 unsigned int idx); 27void gpiod_put(struct gpio_desc *desc); 28 29struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 30 const char *con_id); 31struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 32 const char *con_id, 33 unsigned int idx); 34void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 35 36int gpiod_get_direction(const struct gpio_desc *desc); 37int gpiod_direction_input(struct gpio_desc *desc); 38int gpiod_direction_output(struct gpio_desc *desc, int value); 39int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 40 41/* Value get/set from non-sleeping context */ 42int gpiod_get_value(const struct gpio_desc *desc); 43void gpiod_set_value(struct gpio_desc *desc, int value); 44int gpiod_get_raw_value(const struct gpio_desc *desc); 45void gpiod_set_raw_value(struct gpio_desc *desc, int value); 46 47/* Value get/set from sleeping context */ 48int gpiod_get_value_cansleep(const struct gpio_desc *desc); 49void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 50int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 51void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 52 53int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 54 55int gpiod_is_active_low(const struct gpio_desc *desc); 56int gpiod_cansleep(const struct gpio_desc *desc); 57 58int gpiod_to_irq(const struct gpio_desc *desc); 59 60/* Convert between the old gpio_ and new gpiod_ interfaces */ 61struct gpio_desc *gpio_to_desc(unsigned gpio); 62int desc_to_gpio(const struct gpio_desc *desc); 63struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc); 64 65#else /* CONFIG_GPIOLIB */ 66 67static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 68 const char *con_id) 69{ 70 return ERR_PTR(-ENOSYS); 71} 72static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 73 const char *con_id, 74 unsigned int idx) 75{ 76 return ERR_PTR(-ENOSYS); 77} 78static inline void gpiod_put(struct gpio_desc *desc) 79{ 80 might_sleep(); 81 82 /* GPIO can never have been requested */ 83 WARN_ON(1); 84} 85 86static inline struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 87 const char *con_id) 88{ 89 return ERR_PTR(-ENOSYS); 90} 91static inline 92struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 93 const char *con_id, 94 unsigned int idx) 95{ 96 return ERR_PTR(-ENOSYS); 97} 98static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 99{ 100 might_sleep(); 101 102 /* GPIO can never have been requested */ 103 WARN_ON(1); 104} 105 106 107static inline int gpiod_get_direction(const struct gpio_desc *desc) 108{ 109 /* GPIO can never have been requested */ 110 WARN_ON(1); 111 return -ENOSYS; 112} 113static inline int gpiod_direction_input(struct gpio_desc *desc) 114{ 115 /* GPIO can never have been requested */ 116 WARN_ON(1); 117 return -ENOSYS; 118} 119static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 120{ 121 /* GPIO can never have been requested */ 122 WARN_ON(1); 123 return -ENOSYS; 124} 125static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 126{ 127 /* GPIO can never have been requested */ 128 WARN_ON(1); 129 return -ENOSYS; 130} 131 132 133static inline int gpiod_get_value(const struct gpio_desc *desc) 134{ 135 /* GPIO can never have been requested */ 136 WARN_ON(1); 137 return 0; 138} 139static inline void gpiod_set_value(struct gpio_desc *desc, int value) 140{ 141 /* GPIO can never have been requested */ 142 WARN_ON(1); 143} 144static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 145{ 146 /* GPIO can never have been requested */ 147 WARN_ON(1); 148 return 0; 149} 150static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 151{ 152 /* GPIO can never have been requested */ 153 WARN_ON(1); 154} 155 156static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 157{ 158 /* GPIO can never have been requested */ 159 WARN_ON(1); 160 return 0; 161} 162static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 163{ 164 /* GPIO can never have been requested */ 165 WARN_ON(1); 166} 167static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 168{ 169 /* GPIO can never have been requested */ 170 WARN_ON(1); 171 return 0; 172} 173static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 174 int value) 175{ 176 /* GPIO can never have been requested */ 177 WARN_ON(1); 178} 179 180static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 181{ 182 /* GPIO can never have been requested */ 183 WARN_ON(1); 184 return -ENOSYS; 185} 186 187static inline int gpiod_is_active_low(const struct gpio_desc *desc) 188{ 189 /* GPIO can never have been requested */ 190 WARN_ON(1); 191 return 0; 192} 193static inline int gpiod_cansleep(const struct gpio_desc *desc) 194{ 195 /* GPIO can never have been requested */ 196 WARN_ON(1); 197 return 0; 198} 199 200static inline int gpiod_to_irq(const struct gpio_desc *desc) 201{ 202 /* GPIO can never have been requested */ 203 WARN_ON(1); 204 return -EINVAL; 205} 206 207static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 208{ 209 return ERR_PTR(-EINVAL); 210} 211static inline int desc_to_gpio(const struct gpio_desc *desc) 212{ 213 /* GPIO can never have been requested */ 214 WARN_ON(1); 215 return -EINVAL; 216} 217static inline struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) 218{ 219 /* GPIO can never have been requested */ 220 WARN_ON(1); 221 return ERR_PTR(-ENODEV); 222} 223 224 225#endif /* CONFIG_GPIOLIB */ 226 227#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 228 229int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 230int gpiod_export_link(struct device *dev, const char *name, 231 struct gpio_desc *desc); 232int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); 233void gpiod_unexport(struct gpio_desc *desc); 234 235#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 236 237static inline int gpiod_export(struct gpio_desc *desc, 238 bool direction_may_change) 239{ 240 return -ENOSYS; 241} 242 243static inline int gpiod_export_link(struct device *dev, const char *name, 244 struct gpio_desc *desc) 245{ 246 return -ENOSYS; 247} 248 249static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 250{ 251 return -ENOSYS; 252} 253 254static inline void gpiod_unexport(struct gpio_desc *desc) 255{ 256} 257 258#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 259 260#endif 261