at v4.20 589 lines 17 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 * Opaque descriptor for a structure of GPIO array attributes. This structure 22 * is attached to struct gpiod_descs obtained from gpiod_get_array() and can be 23 * passed back to get/set array functions in order to activate fast processing 24 * path if applicable. 25 */ 26struct gpio_array; 27 28/** 29 * Struct containing an array of descriptors that can be obtained using 30 * gpiod_get_array(). 31 */ 32struct gpio_descs { 33 struct gpio_array *info; 34 unsigned int ndescs; 35 struct gpio_desc *desc[]; 36}; 37 38#define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 39#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 40#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 41#define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3) 42#define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4) 43 44/** 45 * Optional flags that can be passed to one of gpiod_* to configure direction 46 * and output value. These values cannot be OR'd. 47 */ 48enum gpiod_flags { 49 GPIOD_ASIS = 0, 50 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 51 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 52 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 53 GPIOD_FLAGS_BIT_DIR_VAL, 54 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN, 55 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN, 56}; 57 58#ifdef CONFIG_GPIOLIB 59 60/* Return the number of GPIOs associated with a device / function */ 61int gpiod_count(struct device *dev, const char *con_id); 62 63/* Acquire and dispose GPIOs */ 64struct gpio_desc *__must_check gpiod_get(struct device *dev, 65 const char *con_id, 66 enum gpiod_flags flags); 67struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 68 const char *con_id, 69 unsigned int idx, 70 enum gpiod_flags flags); 71struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 72 const char *con_id, 73 enum gpiod_flags flags); 74struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 75 const char *con_id, 76 unsigned int index, 77 enum gpiod_flags flags); 78struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 79 const char *con_id, 80 enum gpiod_flags flags); 81struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 82 const char *con_id, 83 enum gpiod_flags flags); 84void gpiod_put(struct gpio_desc *desc); 85void gpiod_put_array(struct gpio_descs *descs); 86 87struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 88 const char *con_id, 89 enum gpiod_flags flags); 90struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 91 const char *con_id, 92 unsigned int idx, 93 enum gpiod_flags flags); 94struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 95 const char *con_id, 96 enum gpiod_flags flags); 97struct gpio_desc *__must_check 98devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 99 unsigned int index, enum gpiod_flags flags); 100struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 101 const char *con_id, 102 enum gpiod_flags flags); 103struct gpio_descs *__must_check 104devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 105 enum gpiod_flags flags); 106void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 107void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 108 109int gpiod_get_direction(struct gpio_desc *desc); 110int gpiod_direction_input(struct gpio_desc *desc); 111int gpiod_direction_output(struct gpio_desc *desc, int value); 112int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 113 114/* Value get/set from non-sleeping context */ 115int gpiod_get_value(const struct gpio_desc *desc); 116int gpiod_get_array_value(unsigned int array_size, 117 struct gpio_desc **desc_array, 118 struct gpio_array *array_info, 119 unsigned long *value_bitmap); 120void gpiod_set_value(struct gpio_desc *desc, int value); 121int gpiod_set_array_value(unsigned int array_size, 122 struct gpio_desc **desc_array, 123 struct gpio_array *array_info, 124 unsigned long *value_bitmap); 125int gpiod_get_raw_value(const struct gpio_desc *desc); 126int gpiod_get_raw_array_value(unsigned int array_size, 127 struct gpio_desc **desc_array, 128 struct gpio_array *array_info, 129 unsigned long *value_bitmap); 130void gpiod_set_raw_value(struct gpio_desc *desc, int value); 131int gpiod_set_raw_array_value(unsigned int array_size, 132 struct gpio_desc **desc_array, 133 struct gpio_array *array_info, 134 unsigned long *value_bitmap); 135 136/* Value get/set from sleeping context */ 137int gpiod_get_value_cansleep(const struct gpio_desc *desc); 138int gpiod_get_array_value_cansleep(unsigned int array_size, 139 struct gpio_desc **desc_array, 140 struct gpio_array *array_info, 141 unsigned long *value_bitmap); 142void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 143int gpiod_set_array_value_cansleep(unsigned int array_size, 144 struct gpio_desc **desc_array, 145 struct gpio_array *array_info, 146 unsigned long *value_bitmap); 147int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 148int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 149 struct gpio_desc **desc_array, 150 struct gpio_array *array_info, 151 unsigned long *value_bitmap); 152void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 153int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 154 struct gpio_desc **desc_array, 155 struct gpio_array *array_info, 156 unsigned long *value_bitmap); 157 158int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 159int gpiod_set_transitory(struct gpio_desc *desc, bool transitory); 160 161int gpiod_is_active_low(const struct gpio_desc *desc); 162int gpiod_cansleep(const struct gpio_desc *desc); 163 164int gpiod_to_irq(const struct gpio_desc *desc); 165void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 166 167/* Convert between the old gpio_ and new gpiod_ interfaces */ 168struct gpio_desc *gpio_to_desc(unsigned gpio); 169int desc_to_gpio(const struct gpio_desc *desc); 170 171/* Child properties interface */ 172struct device_node; 173struct fwnode_handle; 174 175struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 176 struct device_node *node, 177 const char *propname, int index, 178 enum gpiod_flags dflags, 179 const char *label); 180struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 181 const char *propname, int index, 182 enum gpiod_flags dflags, 183 const char *label); 184struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 185 const char *con_id, int index, 186 struct fwnode_handle *child, 187 enum gpiod_flags flags, 188 const char *label); 189 190#else /* CONFIG_GPIOLIB */ 191 192static inline int gpiod_count(struct device *dev, const char *con_id) 193{ 194 return 0; 195} 196 197static inline struct gpio_desc *__must_check gpiod_get(struct device *dev, 198 const char *con_id, 199 enum gpiod_flags flags) 200{ 201 return ERR_PTR(-ENOSYS); 202} 203static inline struct gpio_desc *__must_check 204gpiod_get_index(struct device *dev, 205 const char *con_id, 206 unsigned int idx, 207 enum gpiod_flags flags) 208{ 209 return ERR_PTR(-ENOSYS); 210} 211 212static inline struct gpio_desc *__must_check 213gpiod_get_optional(struct device *dev, const char *con_id, 214 enum gpiod_flags flags) 215{ 216 return NULL; 217} 218 219static inline struct gpio_desc *__must_check 220gpiod_get_index_optional(struct device *dev, const char *con_id, 221 unsigned int index, enum gpiod_flags flags) 222{ 223 return NULL; 224} 225 226static inline struct gpio_descs *__must_check 227gpiod_get_array(struct device *dev, const char *con_id, 228 enum gpiod_flags flags) 229{ 230 return ERR_PTR(-ENOSYS); 231} 232 233static inline struct gpio_descs *__must_check 234gpiod_get_array_optional(struct device *dev, const char *con_id, 235 enum gpiod_flags flags) 236{ 237 return NULL; 238} 239 240static inline void gpiod_put(struct gpio_desc *desc) 241{ 242 might_sleep(); 243 244 /* GPIO can never have been requested */ 245 WARN_ON(1); 246} 247 248static inline void gpiod_put_array(struct gpio_descs *descs) 249{ 250 might_sleep(); 251 252 /* GPIO can never have been requested */ 253 WARN_ON(1); 254} 255 256static inline struct gpio_desc *__must_check 257devm_gpiod_get(struct device *dev, 258 const char *con_id, 259 enum gpiod_flags flags) 260{ 261 return ERR_PTR(-ENOSYS); 262} 263static inline 264struct gpio_desc *__must_check 265devm_gpiod_get_index(struct device *dev, 266 const char *con_id, 267 unsigned int idx, 268 enum gpiod_flags flags) 269{ 270 return ERR_PTR(-ENOSYS); 271} 272 273static inline struct gpio_desc *__must_check 274devm_gpiod_get_optional(struct device *dev, const char *con_id, 275 enum gpiod_flags flags) 276{ 277 return NULL; 278} 279 280static inline struct gpio_desc *__must_check 281devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 282 unsigned int index, enum gpiod_flags flags) 283{ 284 return NULL; 285} 286 287static inline struct gpio_descs *__must_check 288devm_gpiod_get_array(struct device *dev, const char *con_id, 289 enum gpiod_flags flags) 290{ 291 return ERR_PTR(-ENOSYS); 292} 293 294static inline struct gpio_descs *__must_check 295devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 296 enum gpiod_flags flags) 297{ 298 return NULL; 299} 300 301static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 302{ 303 might_sleep(); 304 305 /* GPIO can never have been requested */ 306 WARN_ON(1); 307} 308 309static inline void devm_gpiod_put_array(struct device *dev, 310 struct gpio_descs *descs) 311{ 312 might_sleep(); 313 314 /* GPIO can never have been requested */ 315 WARN_ON(1); 316} 317 318 319static inline int gpiod_get_direction(const struct gpio_desc *desc) 320{ 321 /* GPIO can never have been requested */ 322 WARN_ON(1); 323 return -ENOSYS; 324} 325static inline int gpiod_direction_input(struct gpio_desc *desc) 326{ 327 /* GPIO can never have been requested */ 328 WARN_ON(1); 329 return -ENOSYS; 330} 331static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 332{ 333 /* GPIO can never have been requested */ 334 WARN_ON(1); 335 return -ENOSYS; 336} 337static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 338{ 339 /* GPIO can never have been requested */ 340 WARN_ON(1); 341 return -ENOSYS; 342} 343 344 345static inline int gpiod_get_value(const struct gpio_desc *desc) 346{ 347 /* GPIO can never have been requested */ 348 WARN_ON(1); 349 return 0; 350} 351static inline int gpiod_get_array_value(unsigned int array_size, 352 struct gpio_desc **desc_array, 353 struct gpio_array *array_info, 354 unsigned long *value_bitmap) 355{ 356 /* GPIO can never have been requested */ 357 WARN_ON(1); 358 return 0; 359} 360static inline void gpiod_set_value(struct gpio_desc *desc, int value) 361{ 362 /* GPIO can never have been requested */ 363 WARN_ON(1); 364} 365static inline int gpiod_set_array_value(unsigned int array_size, 366 struct gpio_desc **desc_array, 367 struct gpio_array *array_info, 368 unsigned long *value_bitmap) 369{ 370 /* GPIO can never have been requested */ 371 WARN_ON(1); 372 return 0; 373} 374static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 375{ 376 /* GPIO can never have been requested */ 377 WARN_ON(1); 378 return 0; 379} 380static inline int gpiod_get_raw_array_value(unsigned int array_size, 381 struct gpio_desc **desc_array, 382 struct gpio_array *array_info, 383 unsigned long *value_bitmap) 384{ 385 /* GPIO can never have been requested */ 386 WARN_ON(1); 387 return 0; 388} 389static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value) 390{ 391 /* GPIO can never have been requested */ 392 WARN_ON(1); 393} 394static inline int gpiod_set_raw_array_value(unsigned int array_size, 395 struct gpio_desc **desc_array, 396 struct gpio_array *array_info, 397 unsigned long *value_bitmap) 398{ 399 /* GPIO can never have been requested */ 400 WARN_ON(1); 401 return 0; 402} 403 404static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 405{ 406 /* GPIO can never have been requested */ 407 WARN_ON(1); 408 return 0; 409} 410static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 411 struct gpio_desc **desc_array, 412 struct gpio_array *array_info, 413 unsigned long *value_bitmap) 414{ 415 /* GPIO can never have been requested */ 416 WARN_ON(1); 417 return 0; 418} 419static inline void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 420{ 421 /* GPIO can never have been requested */ 422 WARN_ON(1); 423} 424static inline int gpiod_set_array_value_cansleep(unsigned int array_size, 425 struct gpio_desc **desc_array, 426 struct gpio_array *array_info, 427 unsigned long *value_bitmap) 428{ 429 /* GPIO can never have been requested */ 430 WARN_ON(1); 431 return 0; 432} 433static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 434{ 435 /* GPIO can never have been requested */ 436 WARN_ON(1); 437 return 0; 438} 439static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 440 struct gpio_desc **desc_array, 441 struct gpio_array *array_info, 442 unsigned long *value_bitmap) 443{ 444 /* GPIO can never have been requested */ 445 WARN_ON(1); 446 return 0; 447} 448static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 449 int value) 450{ 451 /* GPIO can never have been requested */ 452 WARN_ON(1); 453} 454static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 455 struct gpio_desc **desc_array, 456 struct gpio_array *array_info, 457 unsigned long *value_bitmap) 458{ 459 /* GPIO can never have been requested */ 460 WARN_ON(1); 461 return 0; 462} 463 464static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 465{ 466 /* GPIO can never have been requested */ 467 WARN_ON(1); 468 return -ENOSYS; 469} 470 471static inline int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 472{ 473 /* GPIO can never have been requested */ 474 WARN_ON(1); 475 return -ENOSYS; 476} 477 478static inline int gpiod_is_active_low(const struct gpio_desc *desc) 479{ 480 /* GPIO can never have been requested */ 481 WARN_ON(1); 482 return 0; 483} 484static inline int gpiod_cansleep(const struct gpio_desc *desc) 485{ 486 /* GPIO can never have been requested */ 487 WARN_ON(1); 488 return 0; 489} 490 491static inline int gpiod_to_irq(const struct gpio_desc *desc) 492{ 493 /* GPIO can never have been requested */ 494 WARN_ON(1); 495 return -EINVAL; 496} 497 498static inline void gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) 499{ 500 /* GPIO can never have been requested */ 501 WARN_ON(1); 502} 503 504static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 505{ 506 return ERR_PTR(-EINVAL); 507} 508 509static inline int desc_to_gpio(const struct gpio_desc *desc) 510{ 511 /* GPIO can never have been requested */ 512 WARN_ON(1); 513 return -EINVAL; 514} 515 516/* Child properties interface */ 517struct device_node; 518struct fwnode_handle; 519 520static inline 521struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, 522 struct device_node *node, 523 const char *propname, int index, 524 enum gpiod_flags dflags, 525 const char *label) 526{ 527 return ERR_PTR(-ENOSYS); 528} 529 530static inline 531struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 532 const char *propname, int index, 533 enum gpiod_flags dflags, 534 const char *label) 535{ 536 return ERR_PTR(-ENOSYS); 537} 538 539static inline 540struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, 541 const char *con_id, int index, 542 struct fwnode_handle *child, 543 enum gpiod_flags flags, 544 const char *label) 545{ 546 return ERR_PTR(-ENOSYS); 547} 548 549#endif /* CONFIG_GPIOLIB */ 550 551static inline 552struct gpio_desc *devm_fwnode_get_gpiod_from_child(struct device *dev, 553 const char *con_id, 554 struct fwnode_handle *child, 555 enum gpiod_flags flags, 556 const char *label) 557{ 558 return devm_fwnode_get_index_gpiod_from_child(dev, con_id, 0, child, 559 flags, label); 560} 561 562#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 563 564int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 565int gpiod_export_link(struct device *dev, const char *name, 566 struct gpio_desc *desc); 567void gpiod_unexport(struct gpio_desc *desc); 568 569#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 570 571static inline int gpiod_export(struct gpio_desc *desc, 572 bool direction_may_change) 573{ 574 return -ENOSYS; 575} 576 577static inline int gpiod_export_link(struct device *dev, const char *name, 578 struct gpio_desc *desc) 579{ 580 return -ENOSYS; 581} 582 583static inline void gpiod_unexport(struct gpio_desc *desc) 584{ 585} 586 587#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 588 589#endif