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