at v4.14 479 lines 13 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef __LINUX_GPIO_CONSUMER_H 3#define __LINUX_GPIO_CONSUMER_H 4 5#include <linux/bug.h> 6#include <linux/err.h> 7#include <linux/kernel.h> 8 9struct device; 10 11/** 12 * Opaque descriptor for a GPIO. These are obtained using gpiod_get() and are 13 * preferable to the old integer-based handles. 14 * 15 * Contrary to integers, a pointer to a gpio_desc is guaranteed to be valid 16 * until the GPIO is released. 17 */ 18struct gpio_desc; 19 20/** 21 * Struct containing an array of descriptors that can be obtained using 22 * gpiod_get_array(). 23 */ 24struct gpio_descs { 25 unsigned int ndescs; 26 struct gpio_desc *desc[]; 27}; 28 29#define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 30#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 31#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 32 33/** 34 * Optional flags that can be passed to one of gpiod_* to configure direction 35 * and output value. These values cannot be OR'd. 36 */ 37enum gpiod_flags { 38 GPIOD_ASIS = 0, 39 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 40 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 41 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 42 GPIOD_FLAGS_BIT_DIR_VAL, 43}; 44 45#ifdef CONFIG_GPIOLIB 46 47/* Return the number of GPIOs associated with a device / function */ 48int gpiod_count(struct device *dev, const char *con_id); 49 50/* Acquire and dispose GPIOs */ 51struct gpio_desc *__must_check gpiod_get(struct device *dev, 52 const char *con_id, 53 enum gpiod_flags flags); 54struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 55 const char *con_id, 56 unsigned int idx, 57 enum gpiod_flags flags); 58struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 59 const char *con_id, 60 enum gpiod_flags flags); 61struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 62 const char *con_id, 63 unsigned int index, 64 enum gpiod_flags flags); 65struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 66 const char *con_id, 67 enum gpiod_flags flags); 68struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 69 const char *con_id, 70 enum gpiod_flags flags); 71void gpiod_put(struct gpio_desc *desc); 72void gpiod_put_array(struct gpio_descs *descs); 73 74struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 75 const char *con_id, 76 enum gpiod_flags flags); 77struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 78 const char *con_id, 79 unsigned int idx, 80 enum gpiod_flags flags); 81struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 82 const char *con_id, 83 enum gpiod_flags flags); 84struct gpio_desc *__must_check 85devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 86 unsigned int index, enum gpiod_flags flags); 87struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 88 const char *con_id, 89 enum gpiod_flags flags); 90struct gpio_descs *__must_check 91devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 92 enum gpiod_flags flags); 93void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 94void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 95 96int gpiod_get_direction(struct gpio_desc *desc); 97int gpiod_direction_input(struct gpio_desc *desc); 98int gpiod_direction_output(struct gpio_desc *desc, int value); 99int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 100 101/* Value get/set from non-sleeping context */ 102int gpiod_get_value(const struct gpio_desc *desc); 103void gpiod_set_value(struct gpio_desc *desc, int value); 104void gpiod_set_array_value(unsigned int array_size, 105 struct gpio_desc **desc_array, int *value_array); 106int gpiod_get_raw_value(const struct gpio_desc *desc); 107void gpiod_set_raw_value(struct gpio_desc *desc, int value); 108void gpiod_set_raw_array_value(unsigned int array_size, 109 struct gpio_desc **desc_array, 110 int *value_array); 111 112/* Value get/set from sleeping context */ 113int gpiod_get_value_cansleep(const struct gpio_desc *desc); 114void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 115void gpiod_set_array_value_cansleep(unsigned int array_size, 116 struct gpio_desc **desc_array, 117 int *value_array); 118int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 119void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 120void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 121 struct gpio_desc **desc_array, 122 int *value_array); 123 124int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 125 126int gpiod_is_active_low(const struct gpio_desc *desc); 127int gpiod_cansleep(const struct gpio_desc *desc); 128 129int gpiod_to_irq(const struct gpio_desc *desc); 130 131/* Convert between the old gpio_ and new gpiod_ interfaces */ 132struct gpio_desc *gpio_to_desc(unsigned gpio); 133int desc_to_gpio(const struct gpio_desc *desc); 134 135/* Child properties interface */ 136struct fwnode_handle; 137 138struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 139 const char *propname, int index, 140 enum gpiod_flags dflags, 141 const char *label); 142struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 143 const char *con_id, int index, 144 struct fwnode_handle *child, 145 enum gpiod_flags flags, 146 const char *label); 147 148#else /* CONFIG_GPIOLIB */ 149 150static inline int gpiod_count(struct device *dev, const char *con_id) 151{ 152 return 0; 153} 154 155static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 156 const char *con_id, 157 enum gpiod_flags flags) 158{ 159 return ERR_PTR(-ENOSYS); 160} 161static inline struct gpio_desc *__must_check 162gpiod_get_index(struct device *dev, 163 const char *con_id, 164 unsigned int idx, 165 enum gpiod_flags flags) 166{ 167 return ERR_PTR(-ENOSYS); 168} 169 170static inline struct gpio_desc *__must_check 171gpiod_get_optional(struct device *dev, const char *con_id, 172 enum gpiod_flags flags) 173{ 174 return NULL; 175} 176 177static inline struct gpio_desc *__must_check 178gpiod_get_index_optional(struct device *dev, const char *con_id, 179 unsigned int index, enum gpiod_flags flags) 180{ 181 return NULL; 182} 183 184static inline struct gpio_descs *__must_check 185gpiod_get_array(struct device *dev, const char *con_id, 186 enum gpiod_flags flags) 187{ 188 return ERR_PTR(-ENOSYS); 189} 190 191static inline struct gpio_descs *__must_check 192gpiod_get_array_optional(struct device *dev, const char *con_id, 193 enum gpiod_flags flags) 194{ 195 return NULL; 196} 197 198static inline void gpiod_put(struct gpio_desc *desc) 199{ 200 might_sleep(); 201 202 /* GPIO can never have been requested */ 203 WARN_ON(1); 204} 205 206static inline void gpiod_put_array(struct gpio_descs *descs) 207{ 208 might_sleep(); 209 210 /* GPIO can never have been requested */ 211 WARN_ON(1); 212} 213 214static inline struct gpio_desc *__must_check 215devm_gpiod_get(struct device *dev, 216 const char *con_id, 217 enum gpiod_flags flags) 218{ 219 return ERR_PTR(-ENOSYS); 220} 221static inline 222struct gpio_desc *__must_check 223devm_gpiod_get_index(struct device *dev, 224 const char *con_id, 225 unsigned int idx, 226 enum gpiod_flags flags) 227{ 228 return ERR_PTR(-ENOSYS); 229} 230 231static inline struct gpio_desc *__must_check 232devm_gpiod_get_optional(struct device *dev, const char *con_id, 233 enum gpiod_flags flags) 234{ 235 return NULL; 236} 237 238static inline struct gpio_desc *__must_check 239devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 240 unsigned int index, enum gpiod_flags flags) 241{ 242 return NULL; 243} 244 245static inline struct gpio_descs *__must_check 246devm_gpiod_get_array(struct device *dev, const char *con_id, 247 enum gpiod_flags flags) 248{ 249 return ERR_PTR(-ENOSYS); 250} 251 252static inline struct gpio_descs *__must_check 253devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 254 enum gpiod_flags flags) 255{ 256 return NULL; 257} 258 259static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 260{ 261 might_sleep(); 262 263 /* GPIO can never have been requested */ 264 WARN_ON(1); 265} 266 267static inline void devm_gpiod_put_array(struct device *dev, 268 struct gpio_descs *descs) 269{ 270 might_sleep(); 271 272 /* GPIO can never have been requested */ 273 WARN_ON(1); 274} 275 276 277static inline int gpiod_get_direction(const struct gpio_desc *desc) 278{ 279 /* GPIO can never have been requested */ 280 WARN_ON(1); 281 return -ENOSYS; 282} 283static inline int gpiod_direction_input(struct gpio_desc *desc) 284{ 285 /* GPIO can never have been requested */ 286 WARN_ON(1); 287 return -ENOSYS; 288} 289static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 290{ 291 /* GPIO can never have been requested */ 292 WARN_ON(1); 293 return -ENOSYS; 294} 295static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 296{ 297 /* GPIO can never have been requested */ 298 WARN_ON(1); 299 return -ENOSYS; 300} 301 302 303static inline int gpiod_get_value(const struct gpio_desc *desc) 304{ 305 /* GPIO can never have been requested */ 306 WARN_ON(1); 307 return 0; 308} 309static inline void gpiod_set_value(struct gpio_desc *desc, int value) 310{ 311 /* GPIO can never have been requested */ 312 WARN_ON(1); 313} 314static inline void gpiod_set_array_value(unsigned int array_size, 315 struct gpio_desc **desc_array, 316 int *value_array) 317{ 318 /* GPIO can never have been requested */ 319 WARN_ON(1); 320} 321static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 322{ 323 /* GPIO can never have been requested */ 324 WARN_ON(1); 325 return 0; 326} 327static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 328{ 329 /* GPIO can never have been requested */ 330 WARN_ON(1); 331} 332static inline void gpiod_set_raw_array_value(unsigned int array_size, 333 struct gpio_desc **desc_array, 334 int *value_array) 335{ 336 /* GPIO can never have been requested */ 337 WARN_ON(1); 338} 339 340static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 341{ 342 /* GPIO can never have been requested */ 343 WARN_ON(1); 344 return 0; 345} 346static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 347{ 348 /* GPIO can never have been requested */ 349 WARN_ON(1); 350} 351static inline void gpiod_set_array_value_cansleep(unsigned int array_size, 352 struct gpio_desc **desc_array, 353 int *value_array) 354{ 355 /* GPIO can never have been requested */ 356 WARN_ON(1); 357} 358static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 359{ 360 /* GPIO can never have been requested */ 361 WARN_ON(1); 362 return 0; 363} 364static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 365 int value) 366{ 367 /* GPIO can never have been requested */ 368 WARN_ON(1); 369} 370static inline void gpiod_set_raw_array_value_cansleep(unsigned int array_size, 371 struct gpio_desc **desc_array, 372 int *value_array) 373{ 374 /* GPIO can never have been requested */ 375 WARN_ON(1); 376} 377 378static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 379{ 380 /* GPIO can never have been requested */ 381 WARN_ON(1); 382 return -ENOSYS; 383} 384 385static inline int gpiod_is_active_low(const struct gpio_desc *desc) 386{ 387 /* GPIO can never have been requested */ 388 WARN_ON(1); 389 return 0; 390} 391static inline int gpiod_cansleep(const struct gpio_desc *desc) 392{ 393 /* GPIO can never have been requested */ 394 WARN_ON(1); 395 return 0; 396} 397 398static inline int gpiod_to_irq(const struct gpio_desc *desc) 399{ 400 /* GPIO can never have been requested */ 401 WARN_ON(1); 402 return -EINVAL; 403} 404 405static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 406{ 407 return ERR_PTR(-EINVAL); 408} 409 410static inline int desc_to_gpio(const struct gpio_desc *desc) 411{ 412 /* GPIO can never have been requested */ 413 WARN_ON(1); 414 return -EINVAL; 415} 416 417/* Child properties interface */ 418struct fwnode_handle; 419 420static inline 421struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 422 const char *propname, int index, 423 enum gpiod_flags dflags, 424 const char *label) 425{ 426 return ERR_PTR(-ENOSYS); 427} 428 429static inline 430struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 431 const char *con_id, int index, 432 struct fwnode_handle *child, 433 enum gpiod_flags flags, 434 const char *label) 435{ 436 return ERR_PTR(-ENOSYS); 437} 438 439#endif /* CONFIG_GPIOLIB */ 440 441static inline 442struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, 443 const char *con_id, 444 struct fwnode_handle *child, 445 enum gpiod_flags flags, 446 const char *label) 447{ 448 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, 449 flags, label); 450} 451 452#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 453 454int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 455int gpiod_export_link(struct device *dev, const char *name, 456 struct gpio_desc *desc); 457void gpiod_unexport(struct gpio_desc *desc); 458 459#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 460 461static inline int gpiod_export(struct gpio_desc *desc, 462 bool direction_may_change) 463{ 464 return -ENOSYS; 465} 466 467static inline int gpiod_export_link(struct device *dev, const char *name, 468 struct gpio_desc *desc) 469{ 470 return -ENOSYS; 471} 472 473static inline void gpiod_unexport(struct gpio_desc *desc) 474{ 475} 476 477#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 478 479#endif