at v6.18 689 lines 20 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/bits.h> 6#include <linux/err.h> 7#include <linux/types.h> 8 9struct acpi_device; 10struct device; 11struct fwnode_handle; 12 13struct gpio_array; 14struct gpio_desc; 15 16/** 17 * struct gpio_descs - Struct containing an array of descriptors that can be 18 * obtained using gpiod_get_array() 19 * 20 * @info: Pointer to the opaque gpio_array structure 21 * @ndescs: Number of held descriptors 22 * @desc: Array of pointers to GPIO descriptors 23 */ 24struct gpio_descs { 25 struct gpio_array *info; 26 unsigned int ndescs; 27 struct gpio_desc *desc[]; 28}; 29 30#define GPIOD_FLAGS_BIT_DIR_SET BIT(0) 31#define GPIOD_FLAGS_BIT_DIR_OUT BIT(1) 32#define GPIOD_FLAGS_BIT_DIR_VAL BIT(2) 33#define GPIOD_FLAGS_BIT_OPEN_DRAIN BIT(3) 34/* GPIOD_FLAGS_BIT_NONEXCLUSIVE is DEPRECATED, don't use in new code. */ 35#define GPIOD_FLAGS_BIT_NONEXCLUSIVE BIT(4) 36 37/** 38 * enum gpiod_flags - Optional flags that can be passed to one of gpiod_* to 39 * configure direction and output value. These values 40 * cannot be OR'd. 41 * 42 * @GPIOD_ASIS: Don't change anything 43 * @GPIOD_IN: Set lines to input mode 44 * @GPIOD_OUT_LOW: Set lines to output and drive them low 45 * @GPIOD_OUT_HIGH: Set lines to output and drive them high 46 * @GPIOD_OUT_LOW_OPEN_DRAIN: Set lines to open-drain output and drive them low 47 * @GPIOD_OUT_HIGH_OPEN_DRAIN: Set lines to open-drain output and drive them high 48 */ 49enum gpiod_flags { 50 GPIOD_ASIS = 0, 51 GPIOD_IN = GPIOD_FLAGS_BIT_DIR_SET, 52 GPIOD_OUT_LOW = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT, 53 GPIOD_OUT_HIGH = GPIOD_FLAGS_BIT_DIR_SET | GPIOD_FLAGS_BIT_DIR_OUT | 54 GPIOD_FLAGS_BIT_DIR_VAL, 55 GPIOD_OUT_LOW_OPEN_DRAIN = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_OPEN_DRAIN, 56 GPIOD_OUT_HIGH_OPEN_DRAIN = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_OPEN_DRAIN, 57}; 58 59#ifdef CONFIG_GPIOLIB 60 61/* Return the number of GPIOs associated with a device / function */ 62int gpiod_count(struct device *dev, const char *con_id); 63 64/* Acquire and dispose GPIOs */ 65struct gpio_desc *__must_check gpiod_get(struct device *dev, 66 const char *con_id, 67 enum gpiod_flags flags); 68struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 69 const char *con_id, 70 unsigned int idx, 71 enum gpiod_flags flags); 72struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 73 const char *con_id, 74 enum gpiod_flags flags); 75struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 76 const char *con_id, 77 unsigned int index, 78 enum gpiod_flags flags); 79struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 80 const char *con_id, 81 enum gpiod_flags flags); 82struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 83 const char *con_id, 84 enum gpiod_flags flags); 85void gpiod_put(struct gpio_desc *desc); 86void gpiod_put_array(struct gpio_descs *descs); 87 88struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, 89 const char *con_id, 90 enum gpiod_flags flags); 91struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, 92 const char *con_id, 93 unsigned int idx, 94 enum gpiod_flags flags); 95struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, 96 const char *con_id, 97 enum gpiod_flags flags); 98struct gpio_desc *__must_check 99devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 100 unsigned int index, enum gpiod_flags flags); 101struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, 102 const char *con_id, 103 enum gpiod_flags flags); 104struct gpio_descs *__must_check 105devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 106 enum gpiod_flags flags); 107void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); 108void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc); 109void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); 110 111int gpiod_get_direction(struct gpio_desc *desc); 112int gpiod_direction_input(struct gpio_desc *desc); 113int gpiod_direction_output(struct gpio_desc *desc, int value); 114int gpiod_direction_output_raw(struct gpio_desc *desc, int value); 115 116/* Value get/set from non-sleeping context */ 117int gpiod_get_value(const struct gpio_desc *desc); 118int gpiod_get_array_value(unsigned int array_size, 119 struct gpio_desc **desc_array, 120 struct gpio_array *array_info, 121 unsigned long *value_bitmap); 122int gpiod_set_value(struct gpio_desc *desc, int value); 123int gpiod_set_array_value(unsigned int array_size, 124 struct gpio_desc **desc_array, 125 struct gpio_array *array_info, 126 unsigned long *value_bitmap); 127int gpiod_get_raw_value(const struct gpio_desc *desc); 128int gpiod_get_raw_array_value(unsigned int array_size, 129 struct gpio_desc **desc_array, 130 struct gpio_array *array_info, 131 unsigned long *value_bitmap); 132int gpiod_set_raw_value(struct gpio_desc *desc, int value); 133int gpiod_set_raw_array_value(unsigned int array_size, 134 struct gpio_desc **desc_array, 135 struct gpio_array *array_info, 136 unsigned long *value_bitmap); 137 138/* Value get/set from sleeping context */ 139int gpiod_get_value_cansleep(const struct gpio_desc *desc); 140int gpiod_get_array_value_cansleep(unsigned int array_size, 141 struct gpio_desc **desc_array, 142 struct gpio_array *array_info, 143 unsigned long *value_bitmap); 144int gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 145int gpiod_set_array_value_cansleep(unsigned int array_size, 146 struct gpio_desc **desc_array, 147 struct gpio_array *array_info, 148 unsigned long *value_bitmap); 149int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc); 150int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 151 struct gpio_desc **desc_array, 152 struct gpio_array *array_info, 153 unsigned long *value_bitmap); 154int gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value); 155int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 156 struct gpio_desc **desc_array, 157 struct gpio_array *array_info, 158 unsigned long *value_bitmap); 159 160int gpiod_set_config(struct gpio_desc *desc, unsigned long config); 161int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce); 162void gpiod_toggle_active_low(struct gpio_desc *desc); 163 164int gpiod_is_active_low(const struct gpio_desc *desc); 165int gpiod_cansleep(const struct gpio_desc *desc); 166 167int gpiod_to_irq(const struct gpio_desc *desc); 168int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name); 169 170/* Convert between the old gpio_ and new gpiod_ interfaces */ 171struct gpio_desc *gpio_to_desc(unsigned gpio); 172int desc_to_gpio(const struct gpio_desc *desc); 173 174struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 175 const char *con_id, int index, 176 enum gpiod_flags flags, 177 const char *label); 178struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 179 struct fwnode_handle *child, 180 const char *con_id, int index, 181 enum gpiod_flags flags, 182 const char *label); 183 184bool gpiod_is_equal(const struct gpio_desc *desc, 185 const struct gpio_desc *other); 186 187#else /* CONFIG_GPIOLIB */ 188 189#include <linux/bug.h> 190#include <linux/kernel.h> 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(desc); 246} 247 248static inline void devm_gpiod_unhinge(struct device *dev, 249 struct gpio_desc *desc) 250{ 251 might_sleep(); 252 253 /* GPIO can never have been requested */ 254 WARN_ON(desc); 255} 256 257static inline void gpiod_put_array(struct gpio_descs *descs) 258{ 259 might_sleep(); 260 261 /* GPIO can never have been requested */ 262 WARN_ON(descs); 263} 264 265static inline struct gpio_desc *__must_check 266devm_gpiod_get(struct device *dev, 267 const char *con_id, 268 enum gpiod_flags flags) 269{ 270 return ERR_PTR(-ENOSYS); 271} 272static inline 273struct gpio_desc *__must_check 274devm_gpiod_get_index(struct device *dev, 275 const char *con_id, 276 unsigned int idx, 277 enum gpiod_flags flags) 278{ 279 return ERR_PTR(-ENOSYS); 280} 281 282static inline struct gpio_desc *__must_check 283devm_gpiod_get_optional(struct device *dev, const char *con_id, 284 enum gpiod_flags flags) 285{ 286 return NULL; 287} 288 289static inline struct gpio_desc *__must_check 290devm_gpiod_get_index_optional(struct device *dev, const char *con_id, 291 unsigned int index, enum gpiod_flags flags) 292{ 293 return NULL; 294} 295 296static inline struct gpio_descs *__must_check 297devm_gpiod_get_array(struct device *dev, const char *con_id, 298 enum gpiod_flags flags) 299{ 300 return ERR_PTR(-ENOSYS); 301} 302 303static inline struct gpio_descs *__must_check 304devm_gpiod_get_array_optional(struct device *dev, const char *con_id, 305 enum gpiod_flags flags) 306{ 307 return NULL; 308} 309 310static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) 311{ 312 might_sleep(); 313 314 /* GPIO can never have been requested */ 315 WARN_ON(desc); 316} 317 318static inline void devm_gpiod_put_array(struct device *dev, 319 struct gpio_descs *descs) 320{ 321 might_sleep(); 322 323 /* GPIO can never have been requested */ 324 WARN_ON(descs); 325} 326 327 328static inline int gpiod_get_direction(const struct gpio_desc *desc) 329{ 330 /* GPIO can never have been requested */ 331 WARN_ON(desc); 332 return -ENOSYS; 333} 334static inline int gpiod_direction_input(struct gpio_desc *desc) 335{ 336 /* GPIO can never have been requested */ 337 WARN_ON(desc); 338 return -ENOSYS; 339} 340static inline int gpiod_direction_output(struct gpio_desc *desc, int value) 341{ 342 /* GPIO can never have been requested */ 343 WARN_ON(desc); 344 return -ENOSYS; 345} 346static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 347{ 348 /* GPIO can never have been requested */ 349 WARN_ON(desc); 350 return -ENOSYS; 351} 352static inline int gpiod_get_value(const struct gpio_desc *desc) 353{ 354 /* GPIO can never have been requested */ 355 WARN_ON(desc); 356 return 0; 357} 358static inline int gpiod_get_array_value(unsigned int array_size, 359 struct gpio_desc **desc_array, 360 struct gpio_array *array_info, 361 unsigned long *value_bitmap) 362{ 363 /* GPIO can never have been requested */ 364 WARN_ON(desc_array); 365 return 0; 366} 367static inline int gpiod_set_value(struct gpio_desc *desc, int value) 368{ 369 /* GPIO can never have been requested */ 370 WARN_ON(desc); 371 return 0; 372} 373static inline int gpiod_set_array_value(unsigned int array_size, 374 struct gpio_desc **desc_array, 375 struct gpio_array *array_info, 376 unsigned long *value_bitmap) 377{ 378 /* GPIO can never have been requested */ 379 WARN_ON(desc_array); 380 return 0; 381} 382static inline int gpiod_get_raw_value(const struct gpio_desc *desc) 383{ 384 /* GPIO can never have been requested */ 385 WARN_ON(desc); 386 return 0; 387} 388static inline int gpiod_get_raw_array_value(unsigned int array_size, 389 struct gpio_desc **desc_array, 390 struct gpio_array *array_info, 391 unsigned long *value_bitmap) 392{ 393 /* GPIO can never have been requested */ 394 WARN_ON(desc_array); 395 return 0; 396} 397static inline int gpiod_set_raw_value(struct gpio_desc *desc, int value) 398{ 399 /* GPIO can never have been requested */ 400 WARN_ON(desc); 401 return 0; 402} 403static inline int gpiod_set_raw_array_value(unsigned int array_size, 404 struct gpio_desc **desc_array, 405 struct gpio_array *array_info, 406 unsigned long *value_bitmap) 407{ 408 /* GPIO can never have been requested */ 409 WARN_ON(desc_array); 410 return 0; 411} 412 413static inline int gpiod_get_value_cansleep(const struct gpio_desc *desc) 414{ 415 /* GPIO can never have been requested */ 416 WARN_ON(desc); 417 return 0; 418} 419static inline int gpiod_get_array_value_cansleep(unsigned int array_size, 420 struct gpio_desc **desc_array, 421 struct gpio_array *array_info, 422 unsigned long *value_bitmap) 423{ 424 /* GPIO can never have been requested */ 425 WARN_ON(desc_array); 426 return 0; 427} 428static inline int gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 429{ 430 /* GPIO can never have been requested */ 431 WARN_ON(desc); 432 return 0; 433} 434static inline int gpiod_set_array_value_cansleep(unsigned int array_size, 435 struct gpio_desc **desc_array, 436 struct gpio_array *array_info, 437 unsigned long *value_bitmap) 438{ 439 /* GPIO can never have been requested */ 440 WARN_ON(desc_array); 441 return 0; 442} 443static inline int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 444{ 445 /* GPIO can never have been requested */ 446 WARN_ON(desc); 447 return 0; 448} 449static inline int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 450 struct gpio_desc **desc_array, 451 struct gpio_array *array_info, 452 unsigned long *value_bitmap) 453{ 454 /* GPIO can never have been requested */ 455 WARN_ON(desc_array); 456 return 0; 457} 458static inline int gpiod_set_raw_value_cansleep(struct gpio_desc *desc, 459 int value) 460{ 461 /* GPIO can never have been requested */ 462 WARN_ON(desc); 463 return 0; 464} 465static inline int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 466 struct gpio_desc **desc_array, 467 struct gpio_array *array_info, 468 unsigned long *value_bitmap) 469{ 470 /* GPIO can never have been requested */ 471 WARN_ON(desc_array); 472 return 0; 473} 474 475static inline int gpiod_set_config(struct gpio_desc *desc, unsigned long config) 476{ 477 /* GPIO can never have been requested */ 478 WARN_ON(desc); 479 return -ENOSYS; 480} 481 482static inline int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce) 483{ 484 /* GPIO can never have been requested */ 485 WARN_ON(desc); 486 return -ENOSYS; 487} 488 489static inline void gpiod_toggle_active_low(struct gpio_desc *desc) 490{ 491 /* GPIO can never have been requested */ 492 WARN_ON(desc); 493} 494 495static inline int gpiod_is_active_low(const struct gpio_desc *desc) 496{ 497 /* GPIO can never have been requested */ 498 WARN_ON(desc); 499 return 0; 500} 501static inline int gpiod_cansleep(const struct gpio_desc *desc) 502{ 503 /* GPIO can never have been requested */ 504 WARN_ON(desc); 505 return 0; 506} 507 508static inline int gpiod_to_irq(const struct gpio_desc *desc) 509{ 510 /* GPIO can never have been requested */ 511 WARN_ON(desc); 512 return -EINVAL; 513} 514 515static inline int gpiod_set_consumer_name(struct gpio_desc *desc, 516 const char *name) 517{ 518 /* GPIO can never have been requested */ 519 WARN_ON(desc); 520 return -EINVAL; 521} 522 523static inline struct gpio_desc *gpio_to_desc(unsigned gpio) 524{ 525 return NULL; 526} 527 528static inline int desc_to_gpio(const struct gpio_desc *desc) 529{ 530 /* GPIO can never have been requested */ 531 WARN_ON(desc); 532 return -EINVAL; 533} 534 535static inline 536struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 537 const char *con_id, int index, 538 enum gpiod_flags flags, 539 const char *label) 540{ 541 return ERR_PTR(-ENOSYS); 542} 543 544static inline 545struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, 546 struct fwnode_handle *fwnode, 547 const char *con_id, int index, 548 enum gpiod_flags flags, 549 const char *label) 550{ 551 return ERR_PTR(-ENOSYS); 552} 553 554static inline bool 555gpiod_is_equal(const struct gpio_desc *desc, const struct gpio_desc *other) 556{ 557 WARN_ON(desc || other); 558 return false; 559} 560 561#endif /* CONFIG_GPIOLIB */ 562 563#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE) 564int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 565int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags); 566#else 567 568#include <linux/bug.h> 569 570static inline int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, 571 unsigned long flags) 572{ 573 if (!IS_ENABLED(CONFIG_GPIOLIB)) 574 WARN_ON(desc); 575 576 return -ENOSYS; 577} 578static inline int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, 579 unsigned long flags) 580{ 581 if (!IS_ENABLED(CONFIG_GPIOLIB)) 582 WARN_ON(desc); 583 584 return -ENOSYS; 585} 586#endif /* CONFIG_GPIOLIB && CONFIG_HTE */ 587 588static inline 589struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev, 590 struct fwnode_handle *fwnode, 591 const char *con_id, 592 enum gpiod_flags flags, 593 const char *label) 594{ 595 return devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0, 596 flags, label); 597} 598 599struct acpi_gpio_params { 600 unsigned int crs_entry_index; 601 unsigned short line_index; 602 bool active_low; 603}; 604 605struct acpi_gpio_mapping { 606 const char *name; 607 const struct acpi_gpio_params *data; 608 unsigned int size; 609 610/* Ignore IoRestriction field */ 611#define ACPI_GPIO_QUIRK_NO_IO_RESTRICTION BIT(0) 612/* 613 * When ACPI GPIO mapping table is in use the index parameter inside it 614 * refers to the GPIO resource in _CRS method. That index has no 615 * distinction of actual type of the resource. When consumer wants to 616 * get GpioIo type explicitly, this quirk may be used. 617 */ 618#define ACPI_GPIO_QUIRK_ONLY_GPIOIO BIT(1) 619/* Use given pin as an absolute GPIO number in the system */ 620#define ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER BIT(2) 621 622 unsigned int quirks; 623}; 624 625#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_ACPI) 626 627int acpi_dev_add_driver_gpios(struct acpi_device *adev, 628 const struct acpi_gpio_mapping *gpios); 629void acpi_dev_remove_driver_gpios(struct acpi_device *adev); 630 631int devm_acpi_dev_add_driver_gpios(struct device *dev, 632 const struct acpi_gpio_mapping *gpios); 633 634#else /* CONFIG_GPIOLIB && CONFIG_ACPI */ 635 636static inline int acpi_dev_add_driver_gpios(struct acpi_device *adev, 637 const struct acpi_gpio_mapping *gpios) 638{ 639 return -ENXIO; 640} 641static inline void acpi_dev_remove_driver_gpios(struct acpi_device *adev) {} 642 643static inline int devm_acpi_dev_add_driver_gpios(struct device *dev, 644 const struct acpi_gpio_mapping *gpios) 645{ 646 return -ENXIO; 647} 648 649#endif /* CONFIG_GPIOLIB && CONFIG_ACPI */ 650 651 652#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS) 653 654int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 655int gpiod_export_link(struct device *dev, const char *name, 656 struct gpio_desc *desc); 657void gpiod_unexport(struct gpio_desc *desc); 658 659#else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 660 661static inline int gpiod_export(struct gpio_desc *desc, 662 bool direction_may_change) 663{ 664 return -ENOSYS; 665} 666 667static inline int gpiod_export_link(struct device *dev, const char *name, 668 struct gpio_desc *desc) 669{ 670 return -ENOSYS; 671} 672 673static inline void gpiod_unexport(struct gpio_desc *desc) 674{ 675} 676 677#endif /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */ 678 679static inline int gpiod_multi_set_value_cansleep(struct gpio_descs *descs, 680 unsigned long *value_bitmap) 681{ 682 if (IS_ERR_OR_NULL(descs)) 683 return PTR_ERR_OR_ZERO(descs); 684 685 return gpiod_set_array_value_cansleep(descs->ndescs, descs->desc, 686 descs->info, value_bitmap); 687} 688 689#endif