1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/bitmap.h> 3 #include <linux/kernel.h> 4 #include <linux/module.h> 5 #include <linux/interrupt.h> 6 #include <linux/irq.h> 7 #include <linux/spinlock.h> 8 #include <linux/list.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/debugfs.h> 12 #include <linux/seq_file.h> 13 #include <linux/gpio.h> 14 #include <linux/idr.h> 15 #include <linux/slab.h> 16 #include <linux/acpi.h> 17 #include <linux/gpio/driver.h> 18 #include <linux/gpio/machine.h> 19 #include <linux/pinctrl/consumer.h> 20 #include <linux/fs.h> 21 #include <linux/compat.h> 22 #include <linux/file.h> 23 #include <uapi/linux/gpio.h> 24 25 #include "gpiolib.h" 26 #include "gpiolib-of.h" 27 #include "gpiolib-acpi.h" 28 #include "gpiolib-cdev.h" 29 #include "gpiolib-sysfs.h" 30 31 #define CREATE_TRACE_POINTS 32 #include <trace/events/gpio.h> 33 34 /* Implementation infrastructure for GPIO interfaces. 35 * 36 * The GPIO programming interface allows for inlining speed-critical 37 * get/set operations for common cases, so that access to SOC-integrated 38 * GPIOs can sometimes cost only an instruction or two per bit. 39 */ 40 41 42 /* When debugging, extend minimal trust to callers and platform code. 43 * Also emit diagnostic messages that may help initial bringup, when 44 * board setup or driver bugs are most common. 45 * 46 * Otherwise, minimize overhead in what may be bitbanging codepaths. 47 */ 48 #ifdef DEBUG 49 #define extra_checks 1 50 #else 51 #define extra_checks 0 52 #endif 53 54 /* Device and char device-related information */ 55 static DEFINE_IDA(gpio_ida); 56 static dev_t gpio_devt; 57 #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */ 58 static struct bus_type gpio_bus_type = { 59 .name = "gpio", 60 }; 61 62 /* 63 * Number of GPIOs to use for the fast path in set array 64 */ 65 #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT 66 67 /* gpio_lock prevents conflicts during gpio_desc[] table updates. 68 * While any GPIO is requested, its gpio_chip is not removable; 69 * each GPIO's "requested" flag serves as a lock and refcount. 70 */ 71 DEFINE_SPINLOCK(gpio_lock); 72 73 static DEFINE_MUTEX(gpio_lookup_lock); 74 static LIST_HEAD(gpio_lookup_list); 75 LIST_HEAD(gpio_devices); 76 77 static DEFINE_MUTEX(gpio_machine_hogs_mutex); 78 static LIST_HEAD(gpio_machine_hogs); 79 80 static void gpiochip_free_hogs(struct gpio_chip *gc); 81 static int gpiochip_add_irqchip(struct gpio_chip *gc, 82 struct lock_class_key *lock_key, 83 struct lock_class_key *request_key); 84 static void gpiochip_irqchip_remove(struct gpio_chip *gc); 85 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc); 86 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc); 87 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc); 88 89 static bool gpiolib_initialized; 90 91 static inline void desc_set_label(struct gpio_desc *d, const char *label) 92 { 93 d->label = label; 94 } 95 96 /** 97 * gpio_to_desc - Convert a GPIO number to its descriptor 98 * @gpio: global GPIO number 99 * 100 * Returns: 101 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO 102 * with the given number exists in the system. 103 */ 104 struct gpio_desc *gpio_to_desc(unsigned gpio) 105 { 106 struct gpio_device *gdev; 107 unsigned long flags; 108 109 spin_lock_irqsave(&gpio_lock, flags); 110 111 list_for_each_entry(gdev, &gpio_devices, list) { 112 if (gdev->base <= gpio && 113 gdev->base + gdev->ngpio > gpio) { 114 spin_unlock_irqrestore(&gpio_lock, flags); 115 return &gdev->descs[gpio - gdev->base]; 116 } 117 } 118 119 spin_unlock_irqrestore(&gpio_lock, flags); 120 121 if (!gpio_is_valid(gpio)) 122 WARN(1, "invalid GPIO %d\n", gpio); 123 124 return NULL; 125 } 126 EXPORT_SYMBOL_GPL(gpio_to_desc); 127 128 /** 129 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given 130 * hardware number for this chip 131 * @gc: GPIO chip 132 * @hwnum: hardware number of the GPIO for this chip 133 * 134 * Returns: 135 * A pointer to the GPIO descriptor or ``ERR_PTR(-EINVAL)`` if no GPIO exists 136 * in the given chip for the specified hardware number. 137 */ 138 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, 139 unsigned int hwnum) 140 { 141 struct gpio_device *gdev = gc->gpiodev; 142 143 if (hwnum >= gdev->ngpio) 144 return ERR_PTR(-EINVAL); 145 146 return &gdev->descs[hwnum]; 147 } 148 EXPORT_SYMBOL_GPL(gpiochip_get_desc); 149 150 /** 151 * desc_to_gpio - convert a GPIO descriptor to the integer namespace 152 * @desc: GPIO descriptor 153 * 154 * This should disappear in the future but is needed since we still 155 * use GPIO numbers for error messages and sysfs nodes. 156 * 157 * Returns: 158 * The global GPIO number for the GPIO specified by its descriptor. 159 */ 160 int desc_to_gpio(const struct gpio_desc *desc) 161 { 162 return desc->gdev->base + (desc - &desc->gdev->descs[0]); 163 } 164 EXPORT_SYMBOL_GPL(desc_to_gpio); 165 166 167 /** 168 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs 169 * @desc: descriptor to return the chip of 170 */ 171 struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) 172 { 173 if (!desc || !desc->gdev) 174 return NULL; 175 return desc->gdev->chip; 176 } 177 EXPORT_SYMBOL_GPL(gpiod_to_chip); 178 179 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 180 static int gpiochip_find_base(int ngpio) 181 { 182 struct gpio_device *gdev; 183 int base = ARCH_NR_GPIOS - ngpio; 184 185 list_for_each_entry_reverse(gdev, &gpio_devices, list) { 186 /* found a free space? */ 187 if (gdev->base + gdev->ngpio <= base) 188 break; 189 else 190 /* nope, check the space right before the chip */ 191 base = gdev->base - ngpio; 192 } 193 194 if (gpio_is_valid(base)) { 195 pr_debug("%s: found new base at %d\n", __func__, base); 196 return base; 197 } else { 198 pr_err("%s: cannot find free range\n", __func__); 199 return -ENOSPC; 200 } 201 } 202 203 /** 204 * gpiod_get_direction - return the current direction of a GPIO 205 * @desc: GPIO to get the direction of 206 * 207 * Returns 0 for output, 1 for input, or an error code in case of error. 208 * 209 * This function may sleep if gpiod_cansleep() is true. 210 */ 211 int gpiod_get_direction(struct gpio_desc *desc) 212 { 213 struct gpio_chip *gc; 214 unsigned offset; 215 int ret; 216 217 gc = gpiod_to_chip(desc); 218 offset = gpio_chip_hwgpio(desc); 219 220 /* 221 * Open drain emulation using input mode may incorrectly report 222 * input here, fix that up. 223 */ 224 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && 225 test_bit(FLAG_IS_OUT, &desc->flags)) 226 return 0; 227 228 if (!gc->get_direction) 229 return -ENOTSUPP; 230 231 ret = gc->get_direction(gc, offset); 232 if (ret < 0) 233 return ret; 234 235 /* GPIOF_DIR_IN or other positive, otherwise GPIOF_DIR_OUT */ 236 if (ret > 0) 237 ret = 1; 238 239 assign_bit(FLAG_IS_OUT, &desc->flags, !ret); 240 241 return ret; 242 } 243 EXPORT_SYMBOL_GPL(gpiod_get_direction); 244 245 /* 246 * Add a new chip to the global chips list, keeping the list of chips sorted 247 * by range(means [base, base + ngpio - 1]) order. 248 * 249 * Return -EBUSY if the new chip overlaps with some other chip's integer 250 * space. 251 */ 252 static int gpiodev_add_to_list(struct gpio_device *gdev) 253 { 254 struct gpio_device *prev, *next; 255 256 if (list_empty(&gpio_devices)) { 257 /* initial entry in list */ 258 list_add_tail(&gdev->list, &gpio_devices); 259 return 0; 260 } 261 262 next = list_entry(gpio_devices.next, struct gpio_device, list); 263 if (gdev->base + gdev->ngpio <= next->base) { 264 /* add before first entry */ 265 list_add(&gdev->list, &gpio_devices); 266 return 0; 267 } 268 269 prev = list_entry(gpio_devices.prev, struct gpio_device, list); 270 if (prev->base + prev->ngpio <= gdev->base) { 271 /* add behind last entry */ 272 list_add_tail(&gdev->list, &gpio_devices); 273 return 0; 274 } 275 276 list_for_each_entry_safe(prev, next, &gpio_devices, list) { 277 /* at the end of the list */ 278 if (&next->list == &gpio_devices) 279 break; 280 281 /* add between prev and next */ 282 if (prev->base + prev->ngpio <= gdev->base 283 && gdev->base + gdev->ngpio <= next->base) { 284 list_add(&gdev->list, &prev->list); 285 return 0; 286 } 287 } 288 289 dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n"); 290 return -EBUSY; 291 } 292 293 /* 294 * Convert a GPIO name to its descriptor 295 * Note that there is no guarantee that GPIO names are globally unique! 296 * Hence this function will return, if it exists, a reference to the first GPIO 297 * line found that matches the given name. 298 */ 299 static struct gpio_desc *gpio_name_to_desc(const char * const name) 300 { 301 struct gpio_device *gdev; 302 unsigned long flags; 303 304 if (!name) 305 return NULL; 306 307 spin_lock_irqsave(&gpio_lock, flags); 308 309 list_for_each_entry(gdev, &gpio_devices, list) { 310 int i; 311 312 for (i = 0; i != gdev->ngpio; ++i) { 313 struct gpio_desc *desc = &gdev->descs[i]; 314 315 if (!desc->name) 316 continue; 317 318 if (!strcmp(desc->name, name)) { 319 spin_unlock_irqrestore(&gpio_lock, flags); 320 return desc; 321 } 322 } 323 } 324 325 spin_unlock_irqrestore(&gpio_lock, flags); 326 327 return NULL; 328 } 329 330 /* 331 * Take the names from gc->names and assign them to their GPIO descriptors. 332 * Warn if a name is already used for a GPIO line on a different GPIO chip. 333 * 334 * Note that: 335 * 1. Non-unique names are still accepted, 336 * 2. Name collisions within the same GPIO chip are not reported. 337 */ 338 static int gpiochip_set_desc_names(struct gpio_chip *gc) 339 { 340 struct gpio_device *gdev = gc->gpiodev; 341 int i; 342 343 /* First check all names if they are unique */ 344 for (i = 0; i != gc->ngpio; ++i) { 345 struct gpio_desc *gpio; 346 347 gpio = gpio_name_to_desc(gc->names[i]); 348 if (gpio) 349 dev_warn(&gdev->dev, 350 "Detected name collision for GPIO name '%s'\n", 351 gc->names[i]); 352 } 353 354 /* Then add all names to the GPIO descriptors */ 355 for (i = 0; i != gc->ngpio; ++i) 356 gdev->descs[i].name = gc->names[i]; 357 358 return 0; 359 } 360 361 /* 362 * devprop_gpiochip_set_names - Set GPIO line names using device properties 363 * @chip: GPIO chip whose lines should be named, if possible 364 * 365 * Looks for device property "gpio-line-names" and if it exists assigns 366 * GPIO line names for the chip. The memory allocated for the assigned 367 * names belong to the underlying software node and should not be released 368 * by the caller. 369 */ 370 static int devprop_gpiochip_set_names(struct gpio_chip *chip) 371 { 372 struct gpio_device *gdev = chip->gpiodev; 373 struct device *dev = chip->parent; 374 const char **names; 375 int ret, i; 376 int count; 377 378 /* GPIO chip may not have a parent device whose properties we inspect. */ 379 if (!dev) 380 return 0; 381 382 count = device_property_string_array_count(dev, "gpio-line-names"); 383 if (count < 0) 384 return 0; 385 386 if (count > gdev->ngpio) { 387 dev_warn(&gdev->dev, "gpio-line-names is length %d but should be at most length %d", 388 count, gdev->ngpio); 389 count = gdev->ngpio; 390 } 391 392 names = kcalloc(count, sizeof(*names), GFP_KERNEL); 393 if (!names) 394 return -ENOMEM; 395 396 ret = device_property_read_string_array(dev, "gpio-line-names", 397 names, count); 398 if (ret < 0) { 399 dev_warn(&gdev->dev, "failed to read GPIO line names\n"); 400 kfree(names); 401 return ret; 402 } 403 404 for (i = 0; i < count; i++) 405 gdev->descs[i].name = names[i]; 406 407 kfree(names); 408 409 return 0; 410 } 411 412 static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc) 413 { 414 unsigned long *p; 415 416 p = bitmap_alloc(gc->ngpio, GFP_KERNEL); 417 if (!p) 418 return NULL; 419 420 /* Assume by default all GPIOs are valid */ 421 bitmap_fill(p, gc->ngpio); 422 423 return p; 424 } 425 426 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) 427 { 428 if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask)) 429 return 0; 430 431 gc->valid_mask = gpiochip_allocate_mask(gc); 432 if (!gc->valid_mask) 433 return -ENOMEM; 434 435 return 0; 436 } 437 438 static int gpiochip_init_valid_mask(struct gpio_chip *gc) 439 { 440 if (gc->init_valid_mask) 441 return gc->init_valid_mask(gc, 442 gc->valid_mask, 443 gc->ngpio); 444 445 return 0; 446 } 447 448 static void gpiochip_free_valid_mask(struct gpio_chip *gc) 449 { 450 bitmap_free(gc->valid_mask); 451 gc->valid_mask = NULL; 452 } 453 454 static int gpiochip_add_pin_ranges(struct gpio_chip *gc) 455 { 456 if (gc->add_pin_ranges) 457 return gc->add_pin_ranges(gc); 458 459 return 0; 460 } 461 462 bool gpiochip_line_is_valid(const struct gpio_chip *gc, 463 unsigned int offset) 464 { 465 /* No mask means all valid */ 466 if (likely(!gc->valid_mask)) 467 return true; 468 return test_bit(offset, gc->valid_mask); 469 } 470 EXPORT_SYMBOL_GPL(gpiochip_line_is_valid); 471 472 static void gpiodevice_release(struct device *dev) 473 { 474 struct gpio_device *gdev = dev_get_drvdata(dev); 475 476 list_del(&gdev->list); 477 ida_free(&gpio_ida, gdev->id); 478 kfree_const(gdev->label); 479 kfree(gdev->descs); 480 kfree(gdev); 481 } 482 483 static int gpiochip_setup_dev(struct gpio_device *gdev) 484 { 485 int ret; 486 487 ret = gpiolib_cdev_register(gdev, gpio_devt); 488 if (ret) 489 return ret; 490 491 ret = gpiochip_sysfs_register(gdev); 492 if (ret) 493 goto err_remove_device; 494 495 /* From this point, the .release() function cleans up gpio_device */ 496 gdev->dev.release = gpiodevice_release; 497 dev_dbg(&gdev->dev, "registered GPIOs %d to %d on %s\n", gdev->base, 498 gdev->base + gdev->ngpio - 1, gdev->chip->label ? : "generic"); 499 500 return 0; 501 502 err_remove_device: 503 gpiolib_cdev_unregister(gdev); 504 return ret; 505 } 506 507 static void gpiochip_machine_hog(struct gpio_chip *gc, struct gpiod_hog *hog) 508 { 509 struct gpio_desc *desc; 510 int rv; 511 512 desc = gpiochip_get_desc(gc, hog->chip_hwnum); 513 if (IS_ERR(desc)) { 514 chip_err(gc, "%s: unable to get GPIO desc: %ld\n", __func__, 515 PTR_ERR(desc)); 516 return; 517 } 518 519 if (test_bit(FLAG_IS_HOGGED, &desc->flags)) 520 return; 521 522 rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags); 523 if (rv) 524 gpiod_err(desc, "%s: unable to hog GPIO line (%s:%u): %d\n", 525 __func__, gc->label, hog->chip_hwnum, rv); 526 } 527 528 static void machine_gpiochip_add(struct gpio_chip *gc) 529 { 530 struct gpiod_hog *hog; 531 532 mutex_lock(&gpio_machine_hogs_mutex); 533 534 list_for_each_entry(hog, &gpio_machine_hogs, list) { 535 if (!strcmp(gc->label, hog->chip_label)) 536 gpiochip_machine_hog(gc, hog); 537 } 538 539 mutex_unlock(&gpio_machine_hogs_mutex); 540 } 541 542 static void gpiochip_setup_devs(void) 543 { 544 struct gpio_device *gdev; 545 int ret; 546 547 list_for_each_entry(gdev, &gpio_devices, list) { 548 ret = gpiochip_setup_dev(gdev); 549 if (ret) 550 dev_err(&gdev->dev, 551 "Failed to initialize gpio device (%d)\n", ret); 552 } 553 } 554 555 int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, 556 struct lock_class_key *lock_key, 557 struct lock_class_key *request_key) 558 { 559 unsigned long flags; 560 int ret = 0; 561 unsigned i; 562 int base = gc->base; 563 struct gpio_device *gdev; 564 565 /* 566 * First: allocate and populate the internal stat container, and 567 * set up the struct device. 568 */ 569 gdev = kzalloc(sizeof(*gdev), GFP_KERNEL); 570 if (!gdev) 571 return -ENOMEM; 572 gdev->dev.bus = &gpio_bus_type; 573 gdev->chip = gc; 574 gc->gpiodev = gdev; 575 if (gc->parent) { 576 gdev->dev.parent = gc->parent; 577 gdev->dev.of_node = gc->parent->of_node; 578 } 579 580 #ifdef CONFIG_OF_GPIO 581 /* If the gpiochip has an assigned OF node this takes precedence */ 582 if (gc->of_node) 583 gdev->dev.of_node = gc->of_node; 584 else 585 gc->of_node = gdev->dev.of_node; 586 #endif 587 588 gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL); 589 if (gdev->id < 0) { 590 ret = gdev->id; 591 goto err_free_gdev; 592 } 593 dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id); 594 device_initialize(&gdev->dev); 595 dev_set_drvdata(&gdev->dev, gdev); 596 if (gc->parent && gc->parent->driver) 597 gdev->owner = gc->parent->driver->owner; 598 else if (gc->owner) 599 /* TODO: remove chip->owner */ 600 gdev->owner = gc->owner; 601 else 602 gdev->owner = THIS_MODULE; 603 604 gdev->descs = kcalloc(gc->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL); 605 if (!gdev->descs) { 606 ret = -ENOMEM; 607 goto err_free_ida; 608 } 609 610 if (gc->ngpio == 0) { 611 chip_err(gc, "tried to insert a GPIO chip with zero lines\n"); 612 ret = -EINVAL; 613 goto err_free_descs; 614 } 615 616 if (gc->ngpio > FASTPATH_NGPIO) 617 chip_warn(gc, "line cnt %u is greater than fast path cnt %u\n", 618 gc->ngpio, FASTPATH_NGPIO); 619 620 gdev->label = kstrdup_const(gc->label ?: "unknown", GFP_KERNEL); 621 if (!gdev->label) { 622 ret = -ENOMEM; 623 goto err_free_descs; 624 } 625 626 gdev->ngpio = gc->ngpio; 627 gdev->data = data; 628 629 spin_lock_irqsave(&gpio_lock, flags); 630 631 /* 632 * TODO: this allocates a Linux GPIO number base in the global 633 * GPIO numberspace for this chip. In the long run we want to 634 * get *rid* of this numberspace and use only descriptors, but 635 * it may be a pipe dream. It will not happen before we get rid 636 * of the sysfs interface anyways. 637 */ 638 if (base < 0) { 639 base = gpiochip_find_base(gc->ngpio); 640 if (base < 0) { 641 ret = base; 642 spin_unlock_irqrestore(&gpio_lock, flags); 643 goto err_free_label; 644 } 645 /* 646 * TODO: it should not be necessary to reflect the assigned 647 * base outside of the GPIO subsystem. Go over drivers and 648 * see if anyone makes use of this, else drop this and assign 649 * a poison instead. 650 */ 651 gc->base = base; 652 } 653 gdev->base = base; 654 655 ret = gpiodev_add_to_list(gdev); 656 if (ret) { 657 spin_unlock_irqrestore(&gpio_lock, flags); 658 goto err_free_label; 659 } 660 661 for (i = 0; i < gc->ngpio; i++) 662 gdev->descs[i].gdev = gdev; 663 664 spin_unlock_irqrestore(&gpio_lock, flags); 665 666 BLOCKING_INIT_NOTIFIER_HEAD(&gdev->notifier); 667 668 #ifdef CONFIG_PINCTRL 669 INIT_LIST_HEAD(&gdev->pin_ranges); 670 #endif 671 672 if (gc->names) 673 ret = gpiochip_set_desc_names(gc); 674 else 675 ret = devprop_gpiochip_set_names(gc); 676 if (ret) 677 goto err_remove_from_list; 678 679 ret = gpiochip_alloc_valid_mask(gc); 680 if (ret) 681 goto err_remove_from_list; 682 683 ret = of_gpiochip_add(gc); 684 if (ret) 685 goto err_free_gpiochip_mask; 686 687 ret = gpiochip_init_valid_mask(gc); 688 if (ret) 689 goto err_remove_of_chip; 690 691 for (i = 0; i < gc->ngpio; i++) { 692 struct gpio_desc *desc = &gdev->descs[i]; 693 694 if (gc->get_direction && gpiochip_line_is_valid(gc, i)) { 695 assign_bit(FLAG_IS_OUT, 696 &desc->flags, !gc->get_direction(gc, i)); 697 } else { 698 assign_bit(FLAG_IS_OUT, 699 &desc->flags, !gc->direction_input); 700 } 701 } 702 703 ret = gpiochip_add_pin_ranges(gc); 704 if (ret) 705 goto err_remove_of_chip; 706 707 acpi_gpiochip_add(gc); 708 709 machine_gpiochip_add(gc); 710 711 ret = gpiochip_irqchip_init_valid_mask(gc); 712 if (ret) 713 goto err_remove_acpi_chip; 714 715 ret = gpiochip_irqchip_init_hw(gc); 716 if (ret) 717 goto err_remove_acpi_chip; 718 719 ret = gpiochip_add_irqchip(gc, lock_key, request_key); 720 if (ret) 721 goto err_remove_irqchip_mask; 722 723 /* 724 * By first adding the chardev, and then adding the device, 725 * we get a device node entry in sysfs under 726 * /sys/bus/gpio/devices/gpiochipN/dev that can be used for 727 * coldplug of device nodes and other udev business. 728 * We can do this only if gpiolib has been initialized. 729 * Otherwise, defer until later. 730 */ 731 if (gpiolib_initialized) { 732 ret = gpiochip_setup_dev(gdev); 733 if (ret) 734 goto err_remove_irqchip; 735 } 736 return 0; 737 738 err_remove_irqchip: 739 gpiochip_irqchip_remove(gc); 740 err_remove_irqchip_mask: 741 gpiochip_irqchip_free_valid_mask(gc); 742 err_remove_acpi_chip: 743 acpi_gpiochip_remove(gc); 744 err_remove_of_chip: 745 gpiochip_free_hogs(gc); 746 of_gpiochip_remove(gc); 747 err_free_gpiochip_mask: 748 gpiochip_remove_pin_ranges(gc); 749 gpiochip_free_valid_mask(gc); 750 err_remove_from_list: 751 spin_lock_irqsave(&gpio_lock, flags); 752 list_del(&gdev->list); 753 spin_unlock_irqrestore(&gpio_lock, flags); 754 err_free_label: 755 kfree_const(gdev->label); 756 err_free_descs: 757 kfree(gdev->descs); 758 err_free_ida: 759 ida_free(&gpio_ida, gdev->id); 760 err_free_gdev: 761 /* failures here can mean systems won't boot... */ 762 pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__, 763 gdev->base, gdev->base + gdev->ngpio - 1, 764 gc->label ? : "generic", ret); 765 kfree(gdev); 766 return ret; 767 } 768 EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key); 769 770 /** 771 * gpiochip_get_data() - get per-subdriver data for the chip 772 * @gc: GPIO chip 773 * 774 * Returns: 775 * The per-subdriver data for the chip. 776 */ 777 void *gpiochip_get_data(struct gpio_chip *gc) 778 { 779 return gc->gpiodev->data; 780 } 781 EXPORT_SYMBOL_GPL(gpiochip_get_data); 782 783 /** 784 * gpiochip_remove() - unregister a gpio_chip 785 * @gc: the chip to unregister 786 * 787 * A gpio_chip with any GPIOs still requested may not be removed. 788 */ 789 void gpiochip_remove(struct gpio_chip *gc) 790 { 791 struct gpio_device *gdev = gc->gpiodev; 792 unsigned long flags; 793 unsigned int i; 794 795 /* FIXME: should the legacy sysfs handling be moved to gpio_device? */ 796 gpiochip_sysfs_unregister(gdev); 797 gpiochip_free_hogs(gc); 798 /* Numb the device, cancelling all outstanding operations */ 799 gdev->chip = NULL; 800 gpiochip_irqchip_remove(gc); 801 acpi_gpiochip_remove(gc); 802 of_gpiochip_remove(gc); 803 gpiochip_remove_pin_ranges(gc); 804 gpiochip_free_valid_mask(gc); 805 /* 806 * We accept no more calls into the driver from this point, so 807 * NULL the driver data pointer 808 */ 809 gdev->data = NULL; 810 811 spin_lock_irqsave(&gpio_lock, flags); 812 for (i = 0; i < gdev->ngpio; i++) { 813 if (gpiochip_is_requested(gc, i)) 814 break; 815 } 816 spin_unlock_irqrestore(&gpio_lock, flags); 817 818 if (i != gdev->ngpio) 819 dev_crit(&gdev->dev, 820 "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n"); 821 822 /* 823 * The gpiochip side puts its use of the device to rest here: 824 * if there are no userspace clients, the chardev and device will 825 * be removed, else it will be dangling until the last user is 826 * gone. 827 */ 828 gpiolib_cdev_unregister(gdev); 829 put_device(&gdev->dev); 830 } 831 EXPORT_SYMBOL_GPL(gpiochip_remove); 832 833 /** 834 * gpiochip_find() - iterator for locating a specific gpio_chip 835 * @data: data to pass to match function 836 * @match: Callback function to check gpio_chip 837 * 838 * Similar to bus_find_device. It returns a reference to a gpio_chip as 839 * determined by a user supplied @match callback. The callback should return 840 * 0 if the device doesn't match and non-zero if it does. If the callback is 841 * non-zero, this function will return to the caller and not iterate over any 842 * more gpio_chips. 843 */ 844 struct gpio_chip *gpiochip_find(void *data, 845 int (*match)(struct gpio_chip *gc, 846 void *data)) 847 { 848 struct gpio_device *gdev; 849 struct gpio_chip *gc = NULL; 850 unsigned long flags; 851 852 spin_lock_irqsave(&gpio_lock, flags); 853 list_for_each_entry(gdev, &gpio_devices, list) 854 if (gdev->chip && match(gdev->chip, data)) { 855 gc = gdev->chip; 856 break; 857 } 858 859 spin_unlock_irqrestore(&gpio_lock, flags); 860 861 return gc; 862 } 863 EXPORT_SYMBOL_GPL(gpiochip_find); 864 865 static int gpiochip_match_name(struct gpio_chip *gc, void *data) 866 { 867 const char *name = data; 868 869 return !strcmp(gc->label, name); 870 } 871 872 static struct gpio_chip *find_chip_by_name(const char *name) 873 { 874 return gpiochip_find((void *)name, gpiochip_match_name); 875 } 876 877 #ifdef CONFIG_GPIOLIB_IRQCHIP 878 879 /* 880 * The following is irqchip helper code for gpiochips. 881 */ 882 883 static int gpiochip_irqchip_init_hw(struct gpio_chip *gc) 884 { 885 struct gpio_irq_chip *girq = &gc->irq; 886 887 if (!girq->init_hw) 888 return 0; 889 890 return girq->init_hw(gc); 891 } 892 893 static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) 894 { 895 struct gpio_irq_chip *girq = &gc->irq; 896 897 if (!girq->init_valid_mask) 898 return 0; 899 900 girq->valid_mask = gpiochip_allocate_mask(gc); 901 if (!girq->valid_mask) 902 return -ENOMEM; 903 904 girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio); 905 906 return 0; 907 } 908 909 static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc) 910 { 911 bitmap_free(gc->irq.valid_mask); 912 gc->irq.valid_mask = NULL; 913 } 914 915 bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gc, 916 unsigned int offset) 917 { 918 if (!gpiochip_line_is_valid(gc, offset)) 919 return false; 920 /* No mask means all valid */ 921 if (likely(!gc->irq.valid_mask)) 922 return true; 923 return test_bit(offset, gc->irq.valid_mask); 924 } 925 EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid); 926 927 /** 928 * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip 929 * @gc: the gpiochip to set the irqchip chain to 930 * @parent_irq: the irq number corresponding to the parent IRQ for this 931 * cascaded irqchip 932 * @parent_handler: the parent interrupt handler for the accumulated IRQ 933 * coming out of the gpiochip. If the interrupt is nested rather than 934 * cascaded, pass NULL in this handler argument 935 */ 936 static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc, 937 unsigned int parent_irq, 938 irq_flow_handler_t parent_handler) 939 { 940 struct gpio_irq_chip *girq = &gc->irq; 941 struct device *dev = &gc->gpiodev->dev; 942 943 if (!girq->domain) { 944 chip_err(gc, "called %s before setting up irqchip\n", 945 __func__); 946 return; 947 } 948 949 if (parent_handler) { 950 if (gc->can_sleep) { 951 chip_err(gc, 952 "you cannot have chained interrupts on a chip that may sleep\n"); 953 return; 954 } 955 girq->parents = devm_kcalloc(dev, 1, 956 sizeof(*girq->parents), 957 GFP_KERNEL); 958 if (!girq->parents) { 959 chip_err(gc, "out of memory allocating parent IRQ\n"); 960 return; 961 } 962 girq->parents[0] = parent_irq; 963 girq->num_parents = 1; 964 /* 965 * The parent irqchip is already using the chip_data for this 966 * irqchip, so our callbacks simply use the handler_data. 967 */ 968 irq_set_chained_handler_and_data(parent_irq, parent_handler, 969 gc); 970 } 971 } 972 973 /** 974 * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip 975 * @gc: the gpiochip to set the irqchip nested handler to 976 * @irqchip: the irqchip to nest to the gpiochip 977 * @parent_irq: the irq number corresponding to the parent IRQ for this 978 * nested irqchip 979 */ 980 void gpiochip_set_nested_irqchip(struct gpio_chip *gc, 981 struct irq_chip *irqchip, 982 unsigned int parent_irq) 983 { 984 gpiochip_set_cascaded_irqchip(gc, parent_irq, NULL); 985 } 986 EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip); 987 988 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 989 990 /** 991 * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip 992 * to a gpiochip 993 * @gc: the gpiochip to set the irqchip hierarchical handler to 994 * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt 995 * will then percolate up to the parent 996 */ 997 static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc, 998 struct irq_chip *irqchip) 999 { 1000 /* DT will deal with mapping each IRQ as we go along */ 1001 if (is_of_node(gc->irq.fwnode)) 1002 return; 1003 1004 /* 1005 * This is for legacy and boardfile "irqchip" fwnodes: allocate 1006 * irqs upfront instead of dynamically since we don't have the 1007 * dynamic type of allocation that hardware description languages 1008 * provide. Once all GPIO drivers using board files are gone from 1009 * the kernel we can delete this code, but for a transitional period 1010 * it is necessary to keep this around. 1011 */ 1012 if (is_fwnode_irqchip(gc->irq.fwnode)) { 1013 int i; 1014 int ret; 1015 1016 for (i = 0; i < gc->ngpio; i++) { 1017 struct irq_fwspec fwspec; 1018 unsigned int parent_hwirq; 1019 unsigned int parent_type; 1020 struct gpio_irq_chip *girq = &gc->irq; 1021 1022 /* 1023 * We call the child to parent translation function 1024 * only to check if the child IRQ is valid or not. 1025 * Just pick the rising edge type here as that is what 1026 * we likely need to support. 1027 */ 1028 ret = girq->child_to_parent_hwirq(gc, i, 1029 IRQ_TYPE_EDGE_RISING, 1030 &parent_hwirq, 1031 &parent_type); 1032 if (ret) { 1033 chip_err(gc, "skip set-up on hwirq %d\n", 1034 i); 1035 continue; 1036 } 1037 1038 fwspec.fwnode = gc->irq.fwnode; 1039 /* This is the hwirq for the GPIO line side of things */ 1040 fwspec.param[0] = girq->child_offset_to_irq(gc, i); 1041 /* Just pick something */ 1042 fwspec.param[1] = IRQ_TYPE_EDGE_RISING; 1043 fwspec.param_count = 2; 1044 ret = __irq_domain_alloc_irqs(gc->irq.domain, 1045 /* just pick something */ 1046 -1, 1047 1, 1048 NUMA_NO_NODE, 1049 &fwspec, 1050 false, 1051 NULL); 1052 if (ret < 0) { 1053 chip_err(gc, 1054 "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n", 1055 i, parent_hwirq, 1056 ret); 1057 } 1058 } 1059 } 1060 1061 chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__); 1062 1063 return; 1064 } 1065 1066 static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d, 1067 struct irq_fwspec *fwspec, 1068 unsigned long *hwirq, 1069 unsigned int *type) 1070 { 1071 /* We support standard DT translation */ 1072 if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) { 1073 return irq_domain_translate_twocell(d, fwspec, hwirq, type); 1074 } 1075 1076 /* This is for board files and others not using DT */ 1077 if (is_fwnode_irqchip(fwspec->fwnode)) { 1078 int ret; 1079 1080 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type); 1081 if (ret) 1082 return ret; 1083 WARN_ON(*type == IRQ_TYPE_NONE); 1084 return 0; 1085 } 1086 return -EINVAL; 1087 } 1088 1089 static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d, 1090 unsigned int irq, 1091 unsigned int nr_irqs, 1092 void *data) 1093 { 1094 struct gpio_chip *gc = d->host_data; 1095 irq_hw_number_t hwirq; 1096 unsigned int type = IRQ_TYPE_NONE; 1097 struct irq_fwspec *fwspec = data; 1098 void *parent_arg; 1099 unsigned int parent_hwirq; 1100 unsigned int parent_type; 1101 struct gpio_irq_chip *girq = &gc->irq; 1102 int ret; 1103 1104 /* 1105 * The nr_irqs parameter is always one except for PCI multi-MSI 1106 * so this should not happen. 1107 */ 1108 WARN_ON(nr_irqs != 1); 1109 1110 ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type); 1111 if (ret) 1112 return ret; 1113 1114 chip_dbg(gc, "allocate IRQ %d, hwirq %lu\n", irq, hwirq); 1115 1116 ret = girq->child_to_parent_hwirq(gc, hwirq, type, 1117 &parent_hwirq, &parent_type); 1118 if (ret) { 1119 chip_err(gc, "can't look up hwirq %lu\n", hwirq); 1120 return ret; 1121 } 1122 chip_dbg(gc, "found parent hwirq %u\n", parent_hwirq); 1123 1124 /* 1125 * We set handle_bad_irq because the .set_type() should 1126 * always be invoked and set the right type of handler. 1127 */ 1128 irq_domain_set_info(d, 1129 irq, 1130 hwirq, 1131 gc->irq.chip, 1132 gc, 1133 girq->handler, 1134 NULL, NULL); 1135 irq_set_probe(irq); 1136 1137 /* This parent only handles asserted level IRQs */ 1138 parent_arg = girq->populate_parent_alloc_arg(gc, parent_hwirq, parent_type); 1139 if (!parent_arg) 1140 return -ENOMEM; 1141 1142 chip_dbg(gc, "alloc_irqs_parent for %d parent hwirq %d\n", 1143 irq, parent_hwirq); 1144 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); 1145 ret = irq_domain_alloc_irqs_parent(d, irq, 1, parent_arg); 1146 /* 1147 * If the parent irqdomain is msi, the interrupts have already 1148 * been allocated, so the EEXIST is good. 1149 */ 1150 if (irq_domain_is_msi(d->parent) && (ret == -EEXIST)) 1151 ret = 0; 1152 if (ret) 1153 chip_err(gc, 1154 "failed to allocate parent hwirq %d for hwirq %lu\n", 1155 parent_hwirq, hwirq); 1156 1157 kfree(parent_arg); 1158 return ret; 1159 } 1160 1161 static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *gc, 1162 unsigned int offset) 1163 { 1164 return offset; 1165 } 1166 1167 static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops) 1168 { 1169 ops->activate = gpiochip_irq_domain_activate; 1170 ops->deactivate = gpiochip_irq_domain_deactivate; 1171 ops->alloc = gpiochip_hierarchy_irq_domain_alloc; 1172 ops->free = irq_domain_free_irqs_common; 1173 1174 /* 1175 * We only allow overriding the translate() function for 1176 * hierarchical chips, and this should only be done if the user 1177 * really need something other than 1:1 translation. 1178 */ 1179 if (!ops->translate) 1180 ops->translate = gpiochip_hierarchy_irq_domain_translate; 1181 } 1182 1183 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) 1184 { 1185 if (!gc->irq.child_to_parent_hwirq || 1186 !gc->irq.fwnode) { 1187 chip_err(gc, "missing irqdomain vital data\n"); 1188 return -EINVAL; 1189 } 1190 1191 if (!gc->irq.child_offset_to_irq) 1192 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop; 1193 1194 if (!gc->irq.populate_parent_alloc_arg) 1195 gc->irq.populate_parent_alloc_arg = 1196 gpiochip_populate_parent_fwspec_twocell; 1197 1198 gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops); 1199 1200 gc->irq.domain = irq_domain_create_hierarchy( 1201 gc->irq.parent_domain, 1202 0, 1203 gc->ngpio, 1204 gc->irq.fwnode, 1205 &gc->irq.child_irq_domain_ops, 1206 gc); 1207 1208 if (!gc->irq.domain) 1209 return -ENOMEM; 1210 1211 gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip); 1212 1213 return 0; 1214 } 1215 1216 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) 1217 { 1218 return !!gc->irq.parent_domain; 1219 } 1220 1221 void *gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *gc, 1222 unsigned int parent_hwirq, 1223 unsigned int parent_type) 1224 { 1225 struct irq_fwspec *fwspec; 1226 1227 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); 1228 if (!fwspec) 1229 return NULL; 1230 1231 fwspec->fwnode = gc->irq.parent_domain->fwnode; 1232 fwspec->param_count = 2; 1233 fwspec->param[0] = parent_hwirq; 1234 fwspec->param[1] = parent_type; 1235 1236 return fwspec; 1237 } 1238 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell); 1239 1240 void *gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *gc, 1241 unsigned int parent_hwirq, 1242 unsigned int parent_type) 1243 { 1244 struct irq_fwspec *fwspec; 1245 1246 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL); 1247 if (!fwspec) 1248 return NULL; 1249 1250 fwspec->fwnode = gc->irq.parent_domain->fwnode; 1251 fwspec->param_count = 4; 1252 fwspec->param[0] = 0; 1253 fwspec->param[1] = parent_hwirq; 1254 fwspec->param[2] = 0; 1255 fwspec->param[3] = parent_type; 1256 1257 return fwspec; 1258 } 1259 EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell); 1260 1261 #else 1262 1263 static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc) 1264 { 1265 return -EINVAL; 1266 } 1267 1268 static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc) 1269 { 1270 return false; 1271 } 1272 1273 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 1274 1275 /** 1276 * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip 1277 * @d: the irqdomain used by this irqchip 1278 * @irq: the global irq number used by this GPIO irqchip irq 1279 * @hwirq: the local IRQ/GPIO line offset on this gpiochip 1280 * 1281 * This function will set up the mapping for a certain IRQ line on a 1282 * gpiochip by assigning the gpiochip as chip data, and using the irqchip 1283 * stored inside the gpiochip. 1284 */ 1285 int gpiochip_irq_map(struct irq_domain *d, unsigned int irq, 1286 irq_hw_number_t hwirq) 1287 { 1288 struct gpio_chip *gc = d->host_data; 1289 int ret = 0; 1290 1291 if (!gpiochip_irqchip_irq_valid(gc, hwirq)) 1292 return -ENXIO; 1293 1294 irq_set_chip_data(irq, gc); 1295 /* 1296 * This lock class tells lockdep that GPIO irqs are in a different 1297 * category than their parents, so it won't report false recursion. 1298 */ 1299 irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key); 1300 irq_set_chip_and_handler(irq, gc->irq.chip, gc->irq.handler); 1301 /* Chips that use nested thread handlers have them marked */ 1302 if (gc->irq.threaded) 1303 irq_set_nested_thread(irq, 1); 1304 irq_set_noprobe(irq); 1305 1306 if (gc->irq.num_parents == 1) 1307 ret = irq_set_parent(irq, gc->irq.parents[0]); 1308 else if (gc->irq.map) 1309 ret = irq_set_parent(irq, gc->irq.map[hwirq]); 1310 1311 if (ret < 0) 1312 return ret; 1313 1314 /* 1315 * No set-up of the hardware will happen if IRQ_TYPE_NONE 1316 * is passed as default type. 1317 */ 1318 if (gc->irq.default_type != IRQ_TYPE_NONE) 1319 irq_set_irq_type(irq, gc->irq.default_type); 1320 1321 return 0; 1322 } 1323 EXPORT_SYMBOL_GPL(gpiochip_irq_map); 1324 1325 void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq) 1326 { 1327 struct gpio_chip *gc = d->host_data; 1328 1329 if (gc->irq.threaded) 1330 irq_set_nested_thread(irq, 0); 1331 irq_set_chip_and_handler(irq, NULL, NULL); 1332 irq_set_chip_data(irq, NULL); 1333 } 1334 EXPORT_SYMBOL_GPL(gpiochip_irq_unmap); 1335 1336 static const struct irq_domain_ops gpiochip_domain_ops = { 1337 .map = gpiochip_irq_map, 1338 .unmap = gpiochip_irq_unmap, 1339 /* Virtually all GPIO irqchips are twocell:ed */ 1340 .xlate = irq_domain_xlate_twocell, 1341 }; 1342 1343 /* 1344 * TODO: move these activate/deactivate in under the hierarchicial 1345 * irqchip implementation as static once SPMI and SSBI (all external 1346 * users) are phased over. 1347 */ 1348 /** 1349 * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ 1350 * @domain: The IRQ domain used by this IRQ chip 1351 * @data: Outermost irq_data associated with the IRQ 1352 * @reserve: If set, only reserve an interrupt vector instead of assigning one 1353 * 1354 * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be 1355 * used as the activate function for the &struct irq_domain_ops. The host_data 1356 * for the IRQ domain must be the &struct gpio_chip. 1357 */ 1358 int gpiochip_irq_domain_activate(struct irq_domain *domain, 1359 struct irq_data *data, bool reserve) 1360 { 1361 struct gpio_chip *gc = domain->host_data; 1362 1363 return gpiochip_lock_as_irq(gc, data->hwirq); 1364 } 1365 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate); 1366 1367 /** 1368 * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ 1369 * @domain: The IRQ domain used by this IRQ chip 1370 * @data: Outermost irq_data associated with the IRQ 1371 * 1372 * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to 1373 * be used as the deactivate function for the &struct irq_domain_ops. The 1374 * host_data for the IRQ domain must be the &struct gpio_chip. 1375 */ 1376 void gpiochip_irq_domain_deactivate(struct irq_domain *domain, 1377 struct irq_data *data) 1378 { 1379 struct gpio_chip *gc = domain->host_data; 1380 1381 return gpiochip_unlock_as_irq(gc, data->hwirq); 1382 } 1383 EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate); 1384 1385 static int gpiochip_to_irq(struct gpio_chip *gc, unsigned offset) 1386 { 1387 struct irq_domain *domain = gc->irq.domain; 1388 1389 if (!gpiochip_irqchip_irq_valid(gc, offset)) 1390 return -ENXIO; 1391 1392 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 1393 if (irq_domain_is_hierarchy(domain)) { 1394 struct irq_fwspec spec; 1395 1396 spec.fwnode = domain->fwnode; 1397 spec.param_count = 2; 1398 spec.param[0] = gc->irq.child_offset_to_irq(gc, offset); 1399 spec.param[1] = IRQ_TYPE_NONE; 1400 1401 return irq_create_fwspec_mapping(&spec); 1402 } 1403 #endif 1404 1405 return irq_create_mapping(domain, offset); 1406 } 1407 1408 static int gpiochip_irq_reqres(struct irq_data *d) 1409 { 1410 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1411 1412 return gpiochip_reqres_irq(gc, d->hwirq); 1413 } 1414 1415 static void gpiochip_irq_relres(struct irq_data *d) 1416 { 1417 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1418 1419 gpiochip_relres_irq(gc, d->hwirq); 1420 } 1421 1422 static void gpiochip_irq_mask(struct irq_data *d) 1423 { 1424 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1425 1426 if (gc->irq.irq_mask) 1427 gc->irq.irq_mask(d); 1428 gpiochip_disable_irq(gc, d->hwirq); 1429 } 1430 1431 static void gpiochip_irq_unmask(struct irq_data *d) 1432 { 1433 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1434 1435 gpiochip_enable_irq(gc, d->hwirq); 1436 if (gc->irq.irq_unmask) 1437 gc->irq.irq_unmask(d); 1438 } 1439 1440 static void gpiochip_irq_enable(struct irq_data *d) 1441 { 1442 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1443 1444 gpiochip_enable_irq(gc, d->hwirq); 1445 gc->irq.irq_enable(d); 1446 } 1447 1448 static void gpiochip_irq_disable(struct irq_data *d) 1449 { 1450 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 1451 1452 gc->irq.irq_disable(d); 1453 gpiochip_disable_irq(gc, d->hwirq); 1454 } 1455 1456 static void gpiochip_set_irq_hooks(struct gpio_chip *gc) 1457 { 1458 struct irq_chip *irqchip = gc->irq.chip; 1459 1460 if (!irqchip->irq_request_resources && 1461 !irqchip->irq_release_resources) { 1462 irqchip->irq_request_resources = gpiochip_irq_reqres; 1463 irqchip->irq_release_resources = gpiochip_irq_relres; 1464 } 1465 if (WARN_ON(gc->irq.irq_enable)) 1466 return; 1467 /* Check if the irqchip already has this hook... */ 1468 if (irqchip->irq_enable == gpiochip_irq_enable) { 1469 /* 1470 * ...and if so, give a gentle warning that this is bad 1471 * practice. 1472 */ 1473 chip_info(gc, 1474 "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n"); 1475 return; 1476 } 1477 1478 if (irqchip->irq_disable) { 1479 gc->irq.irq_disable = irqchip->irq_disable; 1480 irqchip->irq_disable = gpiochip_irq_disable; 1481 } else { 1482 gc->irq.irq_mask = irqchip->irq_mask; 1483 irqchip->irq_mask = gpiochip_irq_mask; 1484 } 1485 1486 if (irqchip->irq_enable) { 1487 gc->irq.irq_enable = irqchip->irq_enable; 1488 irqchip->irq_enable = gpiochip_irq_enable; 1489 } else { 1490 gc->irq.irq_unmask = irqchip->irq_unmask; 1491 irqchip->irq_unmask = gpiochip_irq_unmask; 1492 } 1493 } 1494 1495 /** 1496 * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip 1497 * @gc: the GPIO chip to add the IRQ chip to 1498 * @lock_key: lockdep class for IRQ lock 1499 * @request_key: lockdep class for IRQ request 1500 */ 1501 static int gpiochip_add_irqchip(struct gpio_chip *gc, 1502 struct lock_class_key *lock_key, 1503 struct lock_class_key *request_key) 1504 { 1505 struct irq_chip *irqchip = gc->irq.chip; 1506 const struct irq_domain_ops *ops = NULL; 1507 struct device_node *np; 1508 unsigned int type; 1509 unsigned int i; 1510 1511 if (!irqchip) 1512 return 0; 1513 1514 if (gc->irq.parent_handler && gc->can_sleep) { 1515 chip_err(gc, "you cannot have chained interrupts on a chip that may sleep\n"); 1516 return -EINVAL; 1517 } 1518 1519 np = gc->gpiodev->dev.of_node; 1520 type = gc->irq.default_type; 1521 1522 /* 1523 * Specifying a default trigger is a terrible idea if DT or ACPI is 1524 * used to configure the interrupts, as you may end up with 1525 * conflicting triggers. Tell the user, and reset to NONE. 1526 */ 1527 if (WARN(np && type != IRQ_TYPE_NONE, 1528 "%s: Ignoring %u default trigger\n", np->full_name, type)) 1529 type = IRQ_TYPE_NONE; 1530 1531 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { 1532 acpi_handle_warn(ACPI_HANDLE(gc->parent), 1533 "Ignoring %u default trigger\n", type); 1534 type = IRQ_TYPE_NONE; 1535 } 1536 1537 gc->to_irq = gpiochip_to_irq; 1538 gc->irq.default_type = type; 1539 gc->irq.lock_key = lock_key; 1540 gc->irq.request_key = request_key; 1541 1542 /* If a parent irqdomain is provided, let's build a hierarchy */ 1543 if (gpiochip_hierarchy_is_hierarchical(gc)) { 1544 int ret = gpiochip_hierarchy_add_domain(gc); 1545 if (ret) 1546 return ret; 1547 } else { 1548 /* Some drivers provide custom irqdomain ops */ 1549 if (gc->irq.domain_ops) 1550 ops = gc->irq.domain_ops; 1551 1552 if (!ops) 1553 ops = &gpiochip_domain_ops; 1554 gc->irq.domain = irq_domain_add_simple(np, 1555 gc->ngpio, 1556 gc->irq.first, 1557 ops, gc); 1558 if (!gc->irq.domain) 1559 return -EINVAL; 1560 } 1561 1562 if (gc->irq.parent_handler) { 1563 void *data = gc->irq.parent_handler_data ?: gc; 1564 1565 for (i = 0; i < gc->irq.num_parents; i++) { 1566 /* 1567 * The parent IRQ chip is already using the chip_data 1568 * for this IRQ chip, so our callbacks simply use the 1569 * handler_data. 1570 */ 1571 irq_set_chained_handler_and_data(gc->irq.parents[i], 1572 gc->irq.parent_handler, 1573 data); 1574 } 1575 } 1576 1577 gpiochip_set_irq_hooks(gc); 1578 1579 acpi_gpiochip_request_interrupts(gc); 1580 1581 return 0; 1582 } 1583 1584 /** 1585 * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip 1586 * @gc: the gpiochip to remove the irqchip from 1587 * 1588 * This is called only from gpiochip_remove() 1589 */ 1590 static void gpiochip_irqchip_remove(struct gpio_chip *gc) 1591 { 1592 struct irq_chip *irqchip = gc->irq.chip; 1593 unsigned int offset; 1594 1595 acpi_gpiochip_free_interrupts(gc); 1596 1597 if (irqchip && gc->irq.parent_handler) { 1598 struct gpio_irq_chip *irq = &gc->irq; 1599 unsigned int i; 1600 1601 for (i = 0; i < irq->num_parents; i++) 1602 irq_set_chained_handler_and_data(irq->parents[i], 1603 NULL, NULL); 1604 } 1605 1606 /* Remove all IRQ mappings and delete the domain */ 1607 if (gc->irq.domain) { 1608 unsigned int irq; 1609 1610 for (offset = 0; offset < gc->ngpio; offset++) { 1611 if (!gpiochip_irqchip_irq_valid(gc, offset)) 1612 continue; 1613 1614 irq = irq_find_mapping(gc->irq.domain, offset); 1615 irq_dispose_mapping(irq); 1616 } 1617 1618 irq_domain_remove(gc->irq.domain); 1619 } 1620 1621 if (irqchip) { 1622 if (irqchip->irq_request_resources == gpiochip_irq_reqres) { 1623 irqchip->irq_request_resources = NULL; 1624 irqchip->irq_release_resources = NULL; 1625 } 1626 if (irqchip->irq_enable == gpiochip_irq_enable) { 1627 irqchip->irq_enable = gc->irq.irq_enable; 1628 irqchip->irq_disable = gc->irq.irq_disable; 1629 } 1630 } 1631 gc->irq.irq_enable = NULL; 1632 gc->irq.irq_disable = NULL; 1633 gc->irq.chip = NULL; 1634 1635 gpiochip_irqchip_free_valid_mask(gc); 1636 } 1637 1638 /** 1639 * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip 1640 * @gc: the gpiochip to add the irqchip to 1641 * @irqchip: the irqchip to add to the gpiochip 1642 * @first_irq: if not dynamically assigned, the base (first) IRQ to 1643 * allocate gpiochip irqs from 1644 * @handler: the irq handler to use (often a predefined irq core function) 1645 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE 1646 * to have the core avoid setting up any default type in the hardware. 1647 * @threaded: whether this irqchip uses a nested thread handler 1648 * @lock_key: lockdep class for IRQ lock 1649 * @request_key: lockdep class for IRQ request 1650 * 1651 * This function closely associates a certain irqchip with a certain 1652 * gpiochip, providing an irq domain to translate the local IRQs to 1653 * global irqs in the gpiolib core, and making sure that the gpiochip 1654 * is passed as chip data to all related functions. Driver callbacks 1655 * need to use gpiochip_get_data() to get their local state containers back 1656 * from the gpiochip passed as chip data. An irqdomain will be stored 1657 * in the gpiochip that shall be used by the driver to handle IRQ number 1658 * translation. The gpiochip will need to be initialized and registered 1659 * before calling this function. 1660 * 1661 * This function will handle two cell:ed simple IRQs and assumes all 1662 * the pins on the gpiochip can generate a unique IRQ. Everything else 1663 * need to be open coded. 1664 */ 1665 int gpiochip_irqchip_add_key(struct gpio_chip *gc, 1666 struct irq_chip *irqchip, 1667 unsigned int first_irq, 1668 irq_flow_handler_t handler, 1669 unsigned int type, 1670 bool threaded, 1671 struct lock_class_key *lock_key, 1672 struct lock_class_key *request_key) 1673 { 1674 struct device_node *of_node; 1675 1676 if (!gc || !irqchip) 1677 return -EINVAL; 1678 1679 if (!gc->parent) { 1680 chip_err(gc, "missing gpiochip .dev parent pointer\n"); 1681 return -EINVAL; 1682 } 1683 gc->irq.threaded = threaded; 1684 of_node = gc->parent->of_node; 1685 #ifdef CONFIG_OF_GPIO 1686 /* 1687 * If the gpiochip has an assigned OF node this takes precedence 1688 * FIXME: get rid of this and use gc->parent->of_node 1689 * everywhere 1690 */ 1691 if (gc->of_node) 1692 of_node = gc->of_node; 1693 #endif 1694 /* 1695 * Specifying a default trigger is a terrible idea if DT or ACPI is 1696 * used to configure the interrupts, as you may end-up with 1697 * conflicting triggers. Tell the user, and reset to NONE. 1698 */ 1699 if (WARN(of_node && type != IRQ_TYPE_NONE, 1700 "%pOF: Ignoring %d default trigger\n", of_node, type)) 1701 type = IRQ_TYPE_NONE; 1702 if (has_acpi_companion(gc->parent) && type != IRQ_TYPE_NONE) { 1703 acpi_handle_warn(ACPI_HANDLE(gc->parent), 1704 "Ignoring %d default trigger\n", type); 1705 type = IRQ_TYPE_NONE; 1706 } 1707 1708 gc->irq.chip = irqchip; 1709 gc->irq.handler = handler; 1710 gc->irq.default_type = type; 1711 gc->to_irq = gpiochip_to_irq; 1712 gc->irq.lock_key = lock_key; 1713 gc->irq.request_key = request_key; 1714 gc->irq.domain = irq_domain_add_simple(of_node, 1715 gc->ngpio, first_irq, 1716 &gpiochip_domain_ops, gc); 1717 if (!gc->irq.domain) { 1718 gc->irq.chip = NULL; 1719 return -EINVAL; 1720 } 1721 1722 gpiochip_set_irq_hooks(gc); 1723 1724 acpi_gpiochip_request_interrupts(gc); 1725 1726 return 0; 1727 } 1728 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key); 1729 1730 /** 1731 * gpiochip_irqchip_add_domain() - adds an irqdomain to a gpiochip 1732 * @gc: the gpiochip to add the irqchip to 1733 * @domain: the irqdomain to add to the gpiochip 1734 * 1735 * This function adds an IRQ domain to the gpiochip. 1736 */ 1737 int gpiochip_irqchip_add_domain(struct gpio_chip *gc, 1738 struct irq_domain *domain) 1739 { 1740 if (!domain) 1741 return -EINVAL; 1742 1743 gc->to_irq = gpiochip_to_irq; 1744 gc->irq.domain = domain; 1745 1746 return 0; 1747 } 1748 EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_domain); 1749 1750 #else /* CONFIG_GPIOLIB_IRQCHIP */ 1751 1752 static inline int gpiochip_add_irqchip(struct gpio_chip *gc, 1753 struct lock_class_key *lock_key, 1754 struct lock_class_key *request_key) 1755 { 1756 return 0; 1757 } 1758 static void gpiochip_irqchip_remove(struct gpio_chip *gc) {} 1759 1760 static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gc) 1761 { 1762 return 0; 1763 } 1764 1765 static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc) 1766 { 1767 return 0; 1768 } 1769 static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc) 1770 { } 1771 1772 #endif /* CONFIG_GPIOLIB_IRQCHIP */ 1773 1774 /** 1775 * gpiochip_generic_request() - request the gpio function for a pin 1776 * @gc: the gpiochip owning the GPIO 1777 * @offset: the offset of the GPIO to request for GPIO function 1778 */ 1779 int gpiochip_generic_request(struct gpio_chip *gc, unsigned offset) 1780 { 1781 #ifdef CONFIG_PINCTRL 1782 if (list_empty(&gc->gpiodev->pin_ranges)) 1783 return 0; 1784 #endif 1785 1786 return pinctrl_gpio_request(gc->gpiodev->base + offset); 1787 } 1788 EXPORT_SYMBOL_GPL(gpiochip_generic_request); 1789 1790 /** 1791 * gpiochip_generic_free() - free the gpio function from a pin 1792 * @gc: the gpiochip to request the gpio function for 1793 * @offset: the offset of the GPIO to free from GPIO function 1794 */ 1795 void gpiochip_generic_free(struct gpio_chip *gc, unsigned offset) 1796 { 1797 pinctrl_gpio_free(gc->gpiodev->base + offset); 1798 } 1799 EXPORT_SYMBOL_GPL(gpiochip_generic_free); 1800 1801 /** 1802 * gpiochip_generic_config() - apply configuration for a pin 1803 * @gc: the gpiochip owning the GPIO 1804 * @offset: the offset of the GPIO to apply the configuration 1805 * @config: the configuration to be applied 1806 */ 1807 int gpiochip_generic_config(struct gpio_chip *gc, unsigned offset, 1808 unsigned long config) 1809 { 1810 return pinctrl_gpio_set_config(gc->gpiodev->base + offset, config); 1811 } 1812 EXPORT_SYMBOL_GPL(gpiochip_generic_config); 1813 1814 #ifdef CONFIG_PINCTRL 1815 1816 /** 1817 * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping 1818 * @gc: the gpiochip to add the range for 1819 * @pctldev: the pin controller to map to 1820 * @gpio_offset: the start offset in the current gpio_chip number space 1821 * @pin_group: name of the pin group inside the pin controller 1822 * 1823 * Calling this function directly from a DeviceTree-supported 1824 * pinctrl driver is DEPRECATED. Please see Section 2.1 of 1825 * Documentation/devicetree/bindings/gpio/gpio.txt on how to 1826 * bind pinctrl and gpio drivers via the "gpio-ranges" property. 1827 */ 1828 int gpiochip_add_pingroup_range(struct gpio_chip *gc, 1829 struct pinctrl_dev *pctldev, 1830 unsigned int gpio_offset, const char *pin_group) 1831 { 1832 struct gpio_pin_range *pin_range; 1833 struct gpio_device *gdev = gc->gpiodev; 1834 int ret; 1835 1836 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); 1837 if (!pin_range) { 1838 chip_err(gc, "failed to allocate pin ranges\n"); 1839 return -ENOMEM; 1840 } 1841 1842 /* Use local offset as range ID */ 1843 pin_range->range.id = gpio_offset; 1844 pin_range->range.gc = gc; 1845 pin_range->range.name = gc->label; 1846 pin_range->range.base = gdev->base + gpio_offset; 1847 pin_range->pctldev = pctldev; 1848 1849 ret = pinctrl_get_group_pins(pctldev, pin_group, 1850 &pin_range->range.pins, 1851 &pin_range->range.npins); 1852 if (ret < 0) { 1853 kfree(pin_range); 1854 return ret; 1855 } 1856 1857 pinctrl_add_gpio_range(pctldev, &pin_range->range); 1858 1859 chip_dbg(gc, "created GPIO range %d->%d ==> %s PINGRP %s\n", 1860 gpio_offset, gpio_offset + pin_range->range.npins - 1, 1861 pinctrl_dev_get_devname(pctldev), pin_group); 1862 1863 list_add_tail(&pin_range->node, &gdev->pin_ranges); 1864 1865 return 0; 1866 } 1867 EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); 1868 1869 /** 1870 * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping 1871 * @gc: the gpiochip to add the range for 1872 * @pinctl_name: the dev_name() of the pin controller to map to 1873 * @gpio_offset: the start offset in the current gpio_chip number space 1874 * @pin_offset: the start offset in the pin controller number space 1875 * @npins: the number of pins from the offset of each pin space (GPIO and 1876 * pin controller) to accumulate in this range 1877 * 1878 * Returns: 1879 * 0 on success, or a negative error-code on failure. 1880 * 1881 * Calling this function directly from a DeviceTree-supported 1882 * pinctrl driver is DEPRECATED. Please see Section 2.1 of 1883 * Documentation/devicetree/bindings/gpio/gpio.txt on how to 1884 * bind pinctrl and gpio drivers via the "gpio-ranges" property. 1885 */ 1886 int gpiochip_add_pin_range(struct gpio_chip *gc, const char *pinctl_name, 1887 unsigned int gpio_offset, unsigned int pin_offset, 1888 unsigned int npins) 1889 { 1890 struct gpio_pin_range *pin_range; 1891 struct gpio_device *gdev = gc->gpiodev; 1892 int ret; 1893 1894 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); 1895 if (!pin_range) { 1896 chip_err(gc, "failed to allocate pin ranges\n"); 1897 return -ENOMEM; 1898 } 1899 1900 /* Use local offset as range ID */ 1901 pin_range->range.id = gpio_offset; 1902 pin_range->range.gc = gc; 1903 pin_range->range.name = gc->label; 1904 pin_range->range.base = gdev->base + gpio_offset; 1905 pin_range->range.pin_base = pin_offset; 1906 pin_range->range.npins = npins; 1907 pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, 1908 &pin_range->range); 1909 if (IS_ERR(pin_range->pctldev)) { 1910 ret = PTR_ERR(pin_range->pctldev); 1911 chip_err(gc, "could not create pin range\n"); 1912 kfree(pin_range); 1913 return ret; 1914 } 1915 chip_dbg(gc, "created GPIO range %d->%d ==> %s PIN %d->%d\n", 1916 gpio_offset, gpio_offset + npins - 1, 1917 pinctl_name, 1918 pin_offset, pin_offset + npins - 1); 1919 1920 list_add_tail(&pin_range->node, &gdev->pin_ranges); 1921 1922 return 0; 1923 } 1924 EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); 1925 1926 /** 1927 * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings 1928 * @gc: the chip to remove all the mappings for 1929 */ 1930 void gpiochip_remove_pin_ranges(struct gpio_chip *gc) 1931 { 1932 struct gpio_pin_range *pin_range, *tmp; 1933 struct gpio_device *gdev = gc->gpiodev; 1934 1935 list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) { 1936 list_del(&pin_range->node); 1937 pinctrl_remove_gpio_range(pin_range->pctldev, 1938 &pin_range->range); 1939 kfree(pin_range); 1940 } 1941 } 1942 EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); 1943 1944 #endif /* CONFIG_PINCTRL */ 1945 1946 /* These "optional" allocation calls help prevent drivers from stomping 1947 * on each other, and help provide better diagnostics in debugfs. 1948 * They're called even less than the "set direction" calls. 1949 */ 1950 static int gpiod_request_commit(struct gpio_desc *desc, const char *label) 1951 { 1952 struct gpio_chip *gc = desc->gdev->chip; 1953 int ret; 1954 unsigned long flags; 1955 unsigned offset; 1956 1957 if (label) { 1958 label = kstrdup_const(label, GFP_KERNEL); 1959 if (!label) 1960 return -ENOMEM; 1961 } 1962 1963 spin_lock_irqsave(&gpio_lock, flags); 1964 1965 /* NOTE: gpio_request() can be called in early boot, 1966 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. 1967 */ 1968 1969 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { 1970 desc_set_label(desc, label ? : "?"); 1971 ret = 0; 1972 } else { 1973 kfree_const(label); 1974 ret = -EBUSY; 1975 goto done; 1976 } 1977 1978 if (gc->request) { 1979 /* gc->request may sleep */ 1980 spin_unlock_irqrestore(&gpio_lock, flags); 1981 offset = gpio_chip_hwgpio(desc); 1982 if (gpiochip_line_is_valid(gc, offset)) 1983 ret = gc->request(gc, offset); 1984 else 1985 ret = -EINVAL; 1986 spin_lock_irqsave(&gpio_lock, flags); 1987 1988 if (ret < 0) { 1989 desc_set_label(desc, NULL); 1990 kfree_const(label); 1991 clear_bit(FLAG_REQUESTED, &desc->flags); 1992 goto done; 1993 } 1994 } 1995 if (gc->get_direction) { 1996 /* gc->get_direction may sleep */ 1997 spin_unlock_irqrestore(&gpio_lock, flags); 1998 gpiod_get_direction(desc); 1999 spin_lock_irqsave(&gpio_lock, flags); 2000 } 2001 done: 2002 spin_unlock_irqrestore(&gpio_lock, flags); 2003 return ret; 2004 } 2005 2006 /* 2007 * This descriptor validation needs to be inserted verbatim into each 2008 * function taking a descriptor, so we need to use a preprocessor 2009 * macro to avoid endless duplication. If the desc is NULL it is an 2010 * optional GPIO and calls should just bail out. 2011 */ 2012 static int validate_desc(const struct gpio_desc *desc, const char *func) 2013 { 2014 if (!desc) 2015 return 0; 2016 if (IS_ERR(desc)) { 2017 pr_warn("%s: invalid GPIO (errorpointer)\n", func); 2018 return PTR_ERR(desc); 2019 } 2020 if (!desc->gdev) { 2021 pr_warn("%s: invalid GPIO (no device)\n", func); 2022 return -EINVAL; 2023 } 2024 if (!desc->gdev->chip) { 2025 dev_warn(&desc->gdev->dev, 2026 "%s: backing chip is gone\n", func); 2027 return 0; 2028 } 2029 return 1; 2030 } 2031 2032 #define VALIDATE_DESC(desc) do { \ 2033 int __valid = validate_desc(desc, __func__); \ 2034 if (__valid <= 0) \ 2035 return __valid; \ 2036 } while (0) 2037 2038 #define VALIDATE_DESC_VOID(desc) do { \ 2039 int __valid = validate_desc(desc, __func__); \ 2040 if (__valid <= 0) \ 2041 return; \ 2042 } while (0) 2043 2044 int gpiod_request(struct gpio_desc *desc, const char *label) 2045 { 2046 int ret = -EPROBE_DEFER; 2047 struct gpio_device *gdev; 2048 2049 VALIDATE_DESC(desc); 2050 gdev = desc->gdev; 2051 2052 if (try_module_get(gdev->owner)) { 2053 ret = gpiod_request_commit(desc, label); 2054 if (ret < 0) 2055 module_put(gdev->owner); 2056 else 2057 get_device(&gdev->dev); 2058 } 2059 2060 if (ret) 2061 gpiod_dbg(desc, "%s: status %d\n", __func__, ret); 2062 2063 return ret; 2064 } 2065 2066 static bool gpiod_free_commit(struct gpio_desc *desc) 2067 { 2068 bool ret = false; 2069 unsigned long flags; 2070 struct gpio_chip *gc; 2071 2072 might_sleep(); 2073 2074 gpiod_unexport(desc); 2075 2076 spin_lock_irqsave(&gpio_lock, flags); 2077 2078 gc = desc->gdev->chip; 2079 if (gc && test_bit(FLAG_REQUESTED, &desc->flags)) { 2080 if (gc->free) { 2081 spin_unlock_irqrestore(&gpio_lock, flags); 2082 might_sleep_if(gc->can_sleep); 2083 gc->free(gc, gpio_chip_hwgpio(desc)); 2084 spin_lock_irqsave(&gpio_lock, flags); 2085 } 2086 kfree_const(desc->label); 2087 desc_set_label(desc, NULL); 2088 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); 2089 clear_bit(FLAG_REQUESTED, &desc->flags); 2090 clear_bit(FLAG_OPEN_DRAIN, &desc->flags); 2091 clear_bit(FLAG_OPEN_SOURCE, &desc->flags); 2092 clear_bit(FLAG_PULL_UP, &desc->flags); 2093 clear_bit(FLAG_PULL_DOWN, &desc->flags); 2094 clear_bit(FLAG_BIAS_DISABLE, &desc->flags); 2095 clear_bit(FLAG_EDGE_RISING, &desc->flags); 2096 clear_bit(FLAG_EDGE_FALLING, &desc->flags); 2097 clear_bit(FLAG_IS_HOGGED, &desc->flags); 2098 #ifdef CONFIG_OF_DYNAMIC 2099 desc->hog = NULL; 2100 #endif 2101 #ifdef CONFIG_GPIO_CDEV 2102 WRITE_ONCE(desc->debounce_period_us, 0); 2103 #endif 2104 ret = true; 2105 } 2106 2107 spin_unlock_irqrestore(&gpio_lock, flags); 2108 blocking_notifier_call_chain(&desc->gdev->notifier, 2109 GPIOLINE_CHANGED_RELEASED, desc); 2110 2111 return ret; 2112 } 2113 2114 void gpiod_free(struct gpio_desc *desc) 2115 { 2116 if (desc && desc->gdev && gpiod_free_commit(desc)) { 2117 module_put(desc->gdev->owner); 2118 put_device(&desc->gdev->dev); 2119 } else { 2120 WARN_ON(extra_checks); 2121 } 2122 } 2123 2124 /** 2125 * gpiochip_is_requested - return string iff signal was requested 2126 * @gc: controller managing the signal 2127 * @offset: of signal within controller's 0..(ngpio - 1) range 2128 * 2129 * Returns NULL if the GPIO is not currently requested, else a string. 2130 * The string returned is the label passed to gpio_request(); if none has been 2131 * passed it is a meaningless, non-NULL constant. 2132 * 2133 * This function is for use by GPIO controller drivers. The label can 2134 * help with diagnostics, and knowing that the signal is used as a GPIO 2135 * can help avoid accidentally multiplexing it to another controller. 2136 */ 2137 const char *gpiochip_is_requested(struct gpio_chip *gc, unsigned offset) 2138 { 2139 struct gpio_desc *desc; 2140 2141 if (offset >= gc->ngpio) 2142 return NULL; 2143 2144 desc = gpiochip_get_desc(gc, offset); 2145 if (IS_ERR(desc)) 2146 return NULL; 2147 2148 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) 2149 return NULL; 2150 return desc->label; 2151 } 2152 EXPORT_SYMBOL_GPL(gpiochip_is_requested); 2153 2154 /** 2155 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor 2156 * @gc: GPIO chip 2157 * @hwnum: hardware number of the GPIO for which to request the descriptor 2158 * @label: label for the GPIO 2159 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to 2160 * specify things like line inversion semantics with the machine flags 2161 * such as GPIO_OUT_LOW 2162 * @dflags: descriptor request flags for this GPIO or 0 if default, this 2163 * can be used to specify consumer semantics such as open drain 2164 * 2165 * Function allows GPIO chip drivers to request and use their own GPIO 2166 * descriptors via gpiolib API. Difference to gpiod_request() is that this 2167 * function will not increase reference count of the GPIO chip module. This 2168 * allows the GPIO chip module to be unloaded as needed (we assume that the 2169 * GPIO chip driver handles freeing the GPIOs it has requested). 2170 * 2171 * Returns: 2172 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error 2173 * code on failure. 2174 */ 2175 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, 2176 unsigned int hwnum, 2177 const char *label, 2178 enum gpio_lookup_flags lflags, 2179 enum gpiod_flags dflags) 2180 { 2181 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum); 2182 int ret; 2183 2184 if (IS_ERR(desc)) { 2185 chip_err(gc, "failed to get GPIO descriptor\n"); 2186 return desc; 2187 } 2188 2189 ret = gpiod_request_commit(desc, label); 2190 if (ret < 0) 2191 return ERR_PTR(ret); 2192 2193 ret = gpiod_configure_flags(desc, label, lflags, dflags); 2194 if (ret) { 2195 chip_err(gc, "setup of own GPIO %s failed\n", label); 2196 gpiod_free_commit(desc); 2197 return ERR_PTR(ret); 2198 } 2199 2200 return desc; 2201 } 2202 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); 2203 2204 /** 2205 * gpiochip_free_own_desc - Free GPIO requested by the chip driver 2206 * @desc: GPIO descriptor to free 2207 * 2208 * Function frees the given GPIO requested previously with 2209 * gpiochip_request_own_desc(). 2210 */ 2211 void gpiochip_free_own_desc(struct gpio_desc *desc) 2212 { 2213 if (desc) 2214 gpiod_free_commit(desc); 2215 } 2216 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); 2217 2218 /* 2219 * Drivers MUST set GPIO direction before making get/set calls. In 2220 * some cases this is done in early boot, before IRQs are enabled. 2221 * 2222 * As a rule these aren't called more than once (except for drivers 2223 * using the open-drain emulation idiom) so these are natural places 2224 * to accumulate extra debugging checks. Note that we can't (yet) 2225 * rely on gpio_request() having been called beforehand. 2226 */ 2227 2228 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, 2229 unsigned long config) 2230 { 2231 if (!gc->set_config) 2232 return -ENOTSUPP; 2233 2234 return gc->set_config(gc, offset, config); 2235 } 2236 2237 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode) 2238 { 2239 struct gpio_chip *gc = desc->gdev->chip; 2240 unsigned long config; 2241 unsigned arg; 2242 2243 switch (mode) { 2244 case PIN_CONFIG_BIAS_PULL_DOWN: 2245 case PIN_CONFIG_BIAS_PULL_UP: 2246 arg = 1; 2247 break; 2248 2249 default: 2250 arg = 0; 2251 } 2252 2253 config = PIN_CONF_PACKED(mode, arg); 2254 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); 2255 } 2256 2257 static int gpio_set_bias(struct gpio_desc *desc) 2258 { 2259 int bias = 0; 2260 int ret = 0; 2261 2262 if (test_bit(FLAG_BIAS_DISABLE, &desc->flags)) 2263 bias = PIN_CONFIG_BIAS_DISABLE; 2264 else if (test_bit(FLAG_PULL_UP, &desc->flags)) 2265 bias = PIN_CONFIG_BIAS_PULL_UP; 2266 else if (test_bit(FLAG_PULL_DOWN, &desc->flags)) 2267 bias = PIN_CONFIG_BIAS_PULL_DOWN; 2268 2269 if (bias) { 2270 ret = gpio_set_config(desc, bias); 2271 if (ret != -ENOTSUPP) 2272 return ret; 2273 } 2274 return 0; 2275 } 2276 2277 /** 2278 * gpiod_direction_input - set the GPIO direction to input 2279 * @desc: GPIO to set to input 2280 * 2281 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can 2282 * be called safely on it. 2283 * 2284 * Return 0 in case of success, else an error code. 2285 */ 2286 int gpiod_direction_input(struct gpio_desc *desc) 2287 { 2288 struct gpio_chip *gc; 2289 int ret = 0; 2290 2291 VALIDATE_DESC(desc); 2292 gc = desc->gdev->chip; 2293 2294 /* 2295 * It is legal to have no .get() and .direction_input() specified if 2296 * the chip is output-only, but you can't specify .direction_input() 2297 * and not support the .get() operation, that doesn't make sense. 2298 */ 2299 if (!gc->get && gc->direction_input) { 2300 gpiod_warn(desc, 2301 "%s: missing get() but have direction_input()\n", 2302 __func__); 2303 return -EIO; 2304 } 2305 2306 /* 2307 * If we have a .direction_input() callback, things are simple, 2308 * just call it. Else we are some input-only chip so try to check the 2309 * direction (if .get_direction() is supported) else we silently 2310 * assume we are in input mode after this. 2311 */ 2312 if (gc->direction_input) { 2313 ret = gc->direction_input(gc, gpio_chip_hwgpio(desc)); 2314 } else if (gc->get_direction && 2315 (gc->get_direction(gc, gpio_chip_hwgpio(desc)) != 1)) { 2316 gpiod_warn(desc, 2317 "%s: missing direction_input() operation and line is output\n", 2318 __func__); 2319 return -EIO; 2320 } 2321 if (ret == 0) { 2322 clear_bit(FLAG_IS_OUT, &desc->flags); 2323 ret = gpio_set_bias(desc); 2324 } 2325 2326 trace_gpio_direction(desc_to_gpio(desc), 1, ret); 2327 2328 return ret; 2329 } 2330 EXPORT_SYMBOL_GPL(gpiod_direction_input); 2331 2332 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) 2333 { 2334 struct gpio_chip *gc = desc->gdev->chip; 2335 int val = !!value; 2336 int ret = 0; 2337 2338 /* 2339 * It's OK not to specify .direction_output() if the gpiochip is 2340 * output-only, but if there is then not even a .set() operation it 2341 * is pretty tricky to drive the output line. 2342 */ 2343 if (!gc->set && !gc->direction_output) { 2344 gpiod_warn(desc, 2345 "%s: missing set() and direction_output() operations\n", 2346 __func__); 2347 return -EIO; 2348 } 2349 2350 if (gc->direction_output) { 2351 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val); 2352 } else { 2353 /* Check that we are in output mode if we can */ 2354 if (gc->get_direction && 2355 gc->get_direction(gc, gpio_chip_hwgpio(desc))) { 2356 gpiod_warn(desc, 2357 "%s: missing direction_output() operation\n", 2358 __func__); 2359 return -EIO; 2360 } 2361 /* 2362 * If we can't actively set the direction, we are some 2363 * output-only chip, so just drive the output as desired. 2364 */ 2365 gc->set(gc, gpio_chip_hwgpio(desc), val); 2366 } 2367 2368 if (!ret) 2369 set_bit(FLAG_IS_OUT, &desc->flags); 2370 trace_gpio_value(desc_to_gpio(desc), 0, val); 2371 trace_gpio_direction(desc_to_gpio(desc), 0, ret); 2372 return ret; 2373 } 2374 2375 /** 2376 * gpiod_direction_output_raw - set the GPIO direction to output 2377 * @desc: GPIO to set to output 2378 * @value: initial output value of the GPIO 2379 * 2380 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can 2381 * be called safely on it. The initial value of the output must be specified 2382 * as raw value on the physical line without regard for the ACTIVE_LOW status. 2383 * 2384 * Return 0 in case of success, else an error code. 2385 */ 2386 int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 2387 { 2388 VALIDATE_DESC(desc); 2389 return gpiod_direction_output_raw_commit(desc, value); 2390 } 2391 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); 2392 2393 /** 2394 * gpiod_direction_output - set the GPIO direction to output 2395 * @desc: GPIO to set to output 2396 * @value: initial output value of the GPIO 2397 * 2398 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can 2399 * be called safely on it. The initial value of the output must be specified 2400 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 2401 * account. 2402 * 2403 * Return 0 in case of success, else an error code. 2404 */ 2405 int gpiod_direction_output(struct gpio_desc *desc, int value) 2406 { 2407 int ret; 2408 2409 VALIDATE_DESC(desc); 2410 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2411 value = !value; 2412 else 2413 value = !!value; 2414 2415 /* GPIOs used for enabled IRQs shall not be set as output */ 2416 if (test_bit(FLAG_USED_AS_IRQ, &desc->flags) && 2417 test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) { 2418 gpiod_err(desc, 2419 "%s: tried to set a GPIO tied to an IRQ as output\n", 2420 __func__); 2421 return -EIO; 2422 } 2423 2424 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 2425 /* First see if we can enable open drain in hardware */ 2426 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN); 2427 if (!ret) 2428 goto set_output_value; 2429 /* Emulate open drain by not actively driving the line high */ 2430 if (value) { 2431 ret = gpiod_direction_input(desc); 2432 goto set_output_flag; 2433 } 2434 } 2435 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { 2436 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE); 2437 if (!ret) 2438 goto set_output_value; 2439 /* Emulate open source by not actively driving the line low */ 2440 if (!value) { 2441 ret = gpiod_direction_input(desc); 2442 goto set_output_flag; 2443 } 2444 } else { 2445 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL); 2446 } 2447 2448 set_output_value: 2449 ret = gpio_set_bias(desc); 2450 if (ret) 2451 return ret; 2452 return gpiod_direction_output_raw_commit(desc, value); 2453 2454 set_output_flag: 2455 /* 2456 * When emulating open-source or open-drain functionalities by not 2457 * actively driving the line (setting mode to input) we still need to 2458 * set the IS_OUT flag or otherwise we won't be able to set the line 2459 * value anymore. 2460 */ 2461 if (ret == 0) 2462 set_bit(FLAG_IS_OUT, &desc->flags); 2463 return ret; 2464 } 2465 EXPORT_SYMBOL_GPL(gpiod_direction_output); 2466 2467 /** 2468 * gpiod_set_config - sets @config for a GPIO 2469 * @desc: descriptor of the GPIO for which to set the configuration 2470 * @config: Same packed config format as generic pinconf 2471 * 2472 * Returns: 2473 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the 2474 * configuration. 2475 */ 2476 int gpiod_set_config(struct gpio_desc *desc, unsigned long config) 2477 { 2478 struct gpio_chip *gc; 2479 2480 VALIDATE_DESC(desc); 2481 gc = desc->gdev->chip; 2482 2483 return gpio_do_set_config(gc, gpio_chip_hwgpio(desc), config); 2484 } 2485 EXPORT_SYMBOL_GPL(gpiod_set_config); 2486 2487 /** 2488 * gpiod_set_debounce - sets @debounce time for a GPIO 2489 * @desc: descriptor of the GPIO for which to set debounce time 2490 * @debounce: debounce time in microseconds 2491 * 2492 * Returns: 2493 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the 2494 * debounce time. 2495 */ 2496 int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 2497 { 2498 unsigned long config; 2499 2500 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); 2501 return gpiod_set_config(desc, config); 2502 } 2503 EXPORT_SYMBOL_GPL(gpiod_set_debounce); 2504 2505 /** 2506 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset 2507 * @desc: descriptor of the GPIO for which to configure persistence 2508 * @transitory: True to lose state on suspend or reset, false for persistence 2509 * 2510 * Returns: 2511 * 0 on success, otherwise a negative error code. 2512 */ 2513 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 2514 { 2515 struct gpio_chip *gc; 2516 unsigned long packed; 2517 int gpio; 2518 int rc; 2519 2520 VALIDATE_DESC(desc); 2521 /* 2522 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for 2523 * persistence state. 2524 */ 2525 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); 2526 2527 /* If the driver supports it, set the persistence state now */ 2528 gc = desc->gdev->chip; 2529 if (!gc->set_config) 2530 return 0; 2531 2532 packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE, 2533 !transitory); 2534 gpio = gpio_chip_hwgpio(desc); 2535 rc = gpio_do_set_config(gc, gpio, packed); 2536 if (rc == -ENOTSUPP) { 2537 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n", 2538 gpio); 2539 return 0; 2540 } 2541 2542 return rc; 2543 } 2544 EXPORT_SYMBOL_GPL(gpiod_set_transitory); 2545 2546 /** 2547 * gpiod_is_active_low - test whether a GPIO is active-low or not 2548 * @desc: the gpio descriptor to test 2549 * 2550 * Returns 1 if the GPIO is active-low, 0 otherwise. 2551 */ 2552 int gpiod_is_active_low(const struct gpio_desc *desc) 2553 { 2554 VALIDATE_DESC(desc); 2555 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); 2556 } 2557 EXPORT_SYMBOL_GPL(gpiod_is_active_low); 2558 2559 /** 2560 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not 2561 * @desc: the gpio descriptor to change 2562 */ 2563 void gpiod_toggle_active_low(struct gpio_desc *desc) 2564 { 2565 VALIDATE_DESC_VOID(desc); 2566 change_bit(FLAG_ACTIVE_LOW, &desc->flags); 2567 } 2568 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); 2569 2570 /* I/O calls are only valid after configuration completed; the relevant 2571 * "is this a valid GPIO" error checks should already have been done. 2572 * 2573 * "Get" operations are often inlinable as reading a pin value register, 2574 * and masking the relevant bit in that register. 2575 * 2576 * When "set" operations are inlinable, they involve writing that mask to 2577 * one register to set a low value, or a different register to set it high. 2578 * Otherwise locking is needed, so there may be little value to inlining. 2579 * 2580 *------------------------------------------------------------------------ 2581 * 2582 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers 2583 * have requested the GPIO. That can include implicit requesting by 2584 * a direction setting call. Marking a gpio as requested locks its chip 2585 * in memory, guaranteeing that these table lookups need no more locking 2586 * and that gpiochip_remove() will fail. 2587 * 2588 * REVISIT when debugging, consider adding some instrumentation to ensure 2589 * that the GPIO was actually requested. 2590 */ 2591 2592 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) 2593 { 2594 struct gpio_chip *gc; 2595 int offset; 2596 int value; 2597 2598 gc = desc->gdev->chip; 2599 offset = gpio_chip_hwgpio(desc); 2600 value = gc->get ? gc->get(gc, offset) : -EIO; 2601 value = value < 0 ? value : !!value; 2602 trace_gpio_value(desc_to_gpio(desc), 1, value); 2603 return value; 2604 } 2605 2606 static int gpio_chip_get_multiple(struct gpio_chip *gc, 2607 unsigned long *mask, unsigned long *bits) 2608 { 2609 if (gc->get_multiple) { 2610 return gc->get_multiple(gc, mask, bits); 2611 } else if (gc->get) { 2612 int i, value; 2613 2614 for_each_set_bit(i, mask, gc->ngpio) { 2615 value = gc->get(gc, i); 2616 if (value < 0) 2617 return value; 2618 __assign_bit(i, bits, value); 2619 } 2620 return 0; 2621 } 2622 return -EIO; 2623 } 2624 2625 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 2626 unsigned int array_size, 2627 struct gpio_desc **desc_array, 2628 struct gpio_array *array_info, 2629 unsigned long *value_bitmap) 2630 { 2631 int ret, i = 0; 2632 2633 /* 2634 * Validate array_info against desc_array and its size. 2635 * It should immediately follow desc_array if both 2636 * have been obtained from the same gpiod_get_array() call. 2637 */ 2638 if (array_info && array_info->desc == desc_array && 2639 array_size <= array_info->size && 2640 (void *)array_info == desc_array + array_info->size) { 2641 if (!can_sleep) 2642 WARN_ON(array_info->chip->can_sleep); 2643 2644 ret = gpio_chip_get_multiple(array_info->chip, 2645 array_info->get_mask, 2646 value_bitmap); 2647 if (ret) 2648 return ret; 2649 2650 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 2651 bitmap_xor(value_bitmap, value_bitmap, 2652 array_info->invert_mask, array_size); 2653 2654 i = find_first_zero_bit(array_info->get_mask, array_size); 2655 if (i == array_size) 2656 return 0; 2657 } else { 2658 array_info = NULL; 2659 } 2660 2661 while (i < array_size) { 2662 struct gpio_chip *gc = desc_array[i]->gdev->chip; 2663 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; 2664 unsigned long *mask, *bits; 2665 int first, j, ret; 2666 2667 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { 2668 mask = fastpath; 2669 } else { 2670 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), 2671 sizeof(*mask), 2672 can_sleep ? GFP_KERNEL : GFP_ATOMIC); 2673 if (!mask) 2674 return -ENOMEM; 2675 } 2676 2677 bits = mask + BITS_TO_LONGS(gc->ngpio); 2678 bitmap_zero(mask, gc->ngpio); 2679 2680 if (!can_sleep) 2681 WARN_ON(gc->can_sleep); 2682 2683 /* collect all inputs belonging to the same chip */ 2684 first = i; 2685 do { 2686 const struct gpio_desc *desc = desc_array[i]; 2687 int hwgpio = gpio_chip_hwgpio(desc); 2688 2689 __set_bit(hwgpio, mask); 2690 i++; 2691 2692 if (array_info) 2693 i = find_next_zero_bit(array_info->get_mask, 2694 array_size, i); 2695 } while ((i < array_size) && 2696 (desc_array[i]->gdev->chip == gc)); 2697 2698 ret = gpio_chip_get_multiple(gc, mask, bits); 2699 if (ret) { 2700 if (mask != fastpath) 2701 kfree(mask); 2702 return ret; 2703 } 2704 2705 for (j = first; j < i; ) { 2706 const struct gpio_desc *desc = desc_array[j]; 2707 int hwgpio = gpio_chip_hwgpio(desc); 2708 int value = test_bit(hwgpio, bits); 2709 2710 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2711 value = !value; 2712 __assign_bit(j, value_bitmap, value); 2713 trace_gpio_value(desc_to_gpio(desc), 1, value); 2714 j++; 2715 2716 if (array_info) 2717 j = find_next_zero_bit(array_info->get_mask, i, 2718 j); 2719 } 2720 2721 if (mask != fastpath) 2722 kfree(mask); 2723 } 2724 return 0; 2725 } 2726 2727 /** 2728 * gpiod_get_raw_value() - return a gpio's raw value 2729 * @desc: gpio whose value will be returned 2730 * 2731 * Return the GPIO's raw value, i.e. the value of the physical line disregarding 2732 * its ACTIVE_LOW status, or negative errno on failure. 2733 * 2734 * This function can be called from contexts where we cannot sleep, and will 2735 * complain if the GPIO chip functions potentially sleep. 2736 */ 2737 int gpiod_get_raw_value(const struct gpio_desc *desc) 2738 { 2739 VALIDATE_DESC(desc); 2740 /* Should be using gpiod_get_raw_value_cansleep() */ 2741 WARN_ON(desc->gdev->chip->can_sleep); 2742 return gpiod_get_raw_value_commit(desc); 2743 } 2744 EXPORT_SYMBOL_GPL(gpiod_get_raw_value); 2745 2746 /** 2747 * gpiod_get_value() - return a gpio's value 2748 * @desc: gpio whose value will be returned 2749 * 2750 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into 2751 * account, or negative errno on failure. 2752 * 2753 * This function can be called from contexts where we cannot sleep, and will 2754 * complain if the GPIO chip functions potentially sleep. 2755 */ 2756 int gpiod_get_value(const struct gpio_desc *desc) 2757 { 2758 int value; 2759 2760 VALIDATE_DESC(desc); 2761 /* Should be using gpiod_get_value_cansleep() */ 2762 WARN_ON(desc->gdev->chip->can_sleep); 2763 2764 value = gpiod_get_raw_value_commit(desc); 2765 if (value < 0) 2766 return value; 2767 2768 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2769 value = !value; 2770 2771 return value; 2772 } 2773 EXPORT_SYMBOL_GPL(gpiod_get_value); 2774 2775 /** 2776 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs 2777 * @array_size: number of elements in the descriptor array / value bitmap 2778 * @desc_array: array of GPIO descriptors whose values will be read 2779 * @array_info: information on applicability of fast bitmap processing path 2780 * @value_bitmap: bitmap to store the read values 2781 * 2782 * Read the raw values of the GPIOs, i.e. the values of the physical lines 2783 * without regard for their ACTIVE_LOW status. Return 0 in case of success, 2784 * else an error code. 2785 * 2786 * This function can be called from contexts where we cannot sleep, 2787 * and it will complain if the GPIO chip functions potentially sleep. 2788 */ 2789 int gpiod_get_raw_array_value(unsigned int array_size, 2790 struct gpio_desc **desc_array, 2791 struct gpio_array *array_info, 2792 unsigned long *value_bitmap) 2793 { 2794 if (!desc_array) 2795 return -EINVAL; 2796 return gpiod_get_array_value_complex(true, false, array_size, 2797 desc_array, array_info, 2798 value_bitmap); 2799 } 2800 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); 2801 2802 /** 2803 * gpiod_get_array_value() - read values from an array of GPIOs 2804 * @array_size: number of elements in the descriptor array / value bitmap 2805 * @desc_array: array of GPIO descriptors whose values will be read 2806 * @array_info: information on applicability of fast bitmap processing path 2807 * @value_bitmap: bitmap to store the read values 2808 * 2809 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 2810 * into account. Return 0 in case of success, else an error code. 2811 * 2812 * This function can be called from contexts where we cannot sleep, 2813 * and it will complain if the GPIO chip functions potentially sleep. 2814 */ 2815 int gpiod_get_array_value(unsigned int array_size, 2816 struct gpio_desc **desc_array, 2817 struct gpio_array *array_info, 2818 unsigned long *value_bitmap) 2819 { 2820 if (!desc_array) 2821 return -EINVAL; 2822 return gpiod_get_array_value_complex(false, false, array_size, 2823 desc_array, array_info, 2824 value_bitmap); 2825 } 2826 EXPORT_SYMBOL_GPL(gpiod_get_array_value); 2827 2828 /* 2829 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. 2830 * @desc: gpio descriptor whose state need to be set. 2831 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 2832 */ 2833 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) 2834 { 2835 int ret = 0; 2836 struct gpio_chip *gc = desc->gdev->chip; 2837 int offset = gpio_chip_hwgpio(desc); 2838 2839 if (value) { 2840 ret = gc->direction_input(gc, offset); 2841 } else { 2842 ret = gc->direction_output(gc, offset, 0); 2843 if (!ret) 2844 set_bit(FLAG_IS_OUT, &desc->flags); 2845 } 2846 trace_gpio_direction(desc_to_gpio(desc), value, ret); 2847 if (ret < 0) 2848 gpiod_err(desc, 2849 "%s: Error in set_value for open drain err %d\n", 2850 __func__, ret); 2851 } 2852 2853 /* 2854 * _gpio_set_open_source_value() - Set the open source gpio's value. 2855 * @desc: gpio descriptor whose state need to be set. 2856 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 2857 */ 2858 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) 2859 { 2860 int ret = 0; 2861 struct gpio_chip *gc = desc->gdev->chip; 2862 int offset = gpio_chip_hwgpio(desc); 2863 2864 if (value) { 2865 ret = gc->direction_output(gc, offset, 1); 2866 if (!ret) 2867 set_bit(FLAG_IS_OUT, &desc->flags); 2868 } else { 2869 ret = gc->direction_input(gc, offset); 2870 } 2871 trace_gpio_direction(desc_to_gpio(desc), !value, ret); 2872 if (ret < 0) 2873 gpiod_err(desc, 2874 "%s: Error in set_value for open source err %d\n", 2875 __func__, ret); 2876 } 2877 2878 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) 2879 { 2880 struct gpio_chip *gc; 2881 2882 gc = desc->gdev->chip; 2883 trace_gpio_value(desc_to_gpio(desc), 0, value); 2884 gc->set(gc, gpio_chip_hwgpio(desc), value); 2885 } 2886 2887 /* 2888 * set multiple outputs on the same chip; 2889 * use the chip's set_multiple function if available; 2890 * otherwise set the outputs sequentially; 2891 * @chip: the GPIO chip we operate on 2892 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word 2893 * defines which outputs are to be changed 2894 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word 2895 * defines the values the outputs specified by mask are to be set to 2896 */ 2897 static void gpio_chip_set_multiple(struct gpio_chip *gc, 2898 unsigned long *mask, unsigned long *bits) 2899 { 2900 if (gc->set_multiple) { 2901 gc->set_multiple(gc, mask, bits); 2902 } else { 2903 unsigned int i; 2904 2905 /* set outputs if the corresponding mask bit is set */ 2906 for_each_set_bit(i, mask, gc->ngpio) 2907 gc->set(gc, i, test_bit(i, bits)); 2908 } 2909 } 2910 2911 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 2912 unsigned int array_size, 2913 struct gpio_desc **desc_array, 2914 struct gpio_array *array_info, 2915 unsigned long *value_bitmap) 2916 { 2917 int i = 0; 2918 2919 /* 2920 * Validate array_info against desc_array and its size. 2921 * It should immediately follow desc_array if both 2922 * have been obtained from the same gpiod_get_array() call. 2923 */ 2924 if (array_info && array_info->desc == desc_array && 2925 array_size <= array_info->size && 2926 (void *)array_info == desc_array + array_info->size) { 2927 if (!can_sleep) 2928 WARN_ON(array_info->chip->can_sleep); 2929 2930 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 2931 bitmap_xor(value_bitmap, value_bitmap, 2932 array_info->invert_mask, array_size); 2933 2934 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, 2935 value_bitmap); 2936 2937 i = find_first_zero_bit(array_info->set_mask, array_size); 2938 if (i == array_size) 2939 return 0; 2940 } else { 2941 array_info = NULL; 2942 } 2943 2944 while (i < array_size) { 2945 struct gpio_chip *gc = desc_array[i]->gdev->chip; 2946 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)]; 2947 unsigned long *mask, *bits; 2948 int count = 0; 2949 2950 if (likely(gc->ngpio <= FASTPATH_NGPIO)) { 2951 mask = fastpath; 2952 } else { 2953 mask = kmalloc_array(2 * BITS_TO_LONGS(gc->ngpio), 2954 sizeof(*mask), 2955 can_sleep ? GFP_KERNEL : GFP_ATOMIC); 2956 if (!mask) 2957 return -ENOMEM; 2958 } 2959 2960 bits = mask + BITS_TO_LONGS(gc->ngpio); 2961 bitmap_zero(mask, gc->ngpio); 2962 2963 if (!can_sleep) 2964 WARN_ON(gc->can_sleep); 2965 2966 do { 2967 struct gpio_desc *desc = desc_array[i]; 2968 int hwgpio = gpio_chip_hwgpio(desc); 2969 int value = test_bit(i, value_bitmap); 2970 2971 /* 2972 * Pins applicable for fast input but not for 2973 * fast output processing may have been already 2974 * inverted inside the fast path, skip them. 2975 */ 2976 if (!raw && !(array_info && 2977 test_bit(i, array_info->invert_mask)) && 2978 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 2979 value = !value; 2980 trace_gpio_value(desc_to_gpio(desc), 0, value); 2981 /* 2982 * collect all normal outputs belonging to the same chip 2983 * open drain and open source outputs are set individually 2984 */ 2985 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { 2986 gpio_set_open_drain_value_commit(desc, value); 2987 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { 2988 gpio_set_open_source_value_commit(desc, value); 2989 } else { 2990 __set_bit(hwgpio, mask); 2991 __assign_bit(hwgpio, bits, value); 2992 count++; 2993 } 2994 i++; 2995 2996 if (array_info) 2997 i = find_next_zero_bit(array_info->set_mask, 2998 array_size, i); 2999 } while ((i < array_size) && 3000 (desc_array[i]->gdev->chip == gc)); 3001 /* push collected bits to outputs */ 3002 if (count != 0) 3003 gpio_chip_set_multiple(gc, mask, bits); 3004 3005 if (mask != fastpath) 3006 kfree(mask); 3007 } 3008 return 0; 3009 } 3010 3011 /** 3012 * gpiod_set_raw_value() - assign a gpio's raw value 3013 * @desc: gpio whose value will be assigned 3014 * @value: value to assign 3015 * 3016 * Set the raw value of the GPIO, i.e. the value of its physical line without 3017 * regard for its ACTIVE_LOW status. 3018 * 3019 * This function can be called from contexts where we cannot sleep, and will 3020 * complain if the GPIO chip functions potentially sleep. 3021 */ 3022 void gpiod_set_raw_value(struct gpio_desc *desc, int value) 3023 { 3024 VALIDATE_DESC_VOID(desc); 3025 /* Should be using gpiod_set_raw_value_cansleep() */ 3026 WARN_ON(desc->gdev->chip->can_sleep); 3027 gpiod_set_raw_value_commit(desc, value); 3028 } 3029 EXPORT_SYMBOL_GPL(gpiod_set_raw_value); 3030 3031 /** 3032 * gpiod_set_value_nocheck() - set a GPIO line value without checking 3033 * @desc: the descriptor to set the value on 3034 * @value: value to set 3035 * 3036 * This sets the value of a GPIO line backing a descriptor, applying 3037 * different semantic quirks like active low and open drain/source 3038 * handling. 3039 */ 3040 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value) 3041 { 3042 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3043 value = !value; 3044 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 3045 gpio_set_open_drain_value_commit(desc, value); 3046 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 3047 gpio_set_open_source_value_commit(desc, value); 3048 else 3049 gpiod_set_raw_value_commit(desc, value); 3050 } 3051 3052 /** 3053 * gpiod_set_value() - assign a gpio's value 3054 * @desc: gpio whose value will be assigned 3055 * @value: value to assign 3056 * 3057 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, 3058 * OPEN_DRAIN and OPEN_SOURCE flags into account. 3059 * 3060 * This function can be called from contexts where we cannot sleep, and will 3061 * complain if the GPIO chip functions potentially sleep. 3062 */ 3063 void gpiod_set_value(struct gpio_desc *desc, int value) 3064 { 3065 VALIDATE_DESC_VOID(desc); 3066 /* Should be using gpiod_set_value_cansleep() */ 3067 WARN_ON(desc->gdev->chip->can_sleep); 3068 gpiod_set_value_nocheck(desc, value); 3069 } 3070 EXPORT_SYMBOL_GPL(gpiod_set_value); 3071 3072 /** 3073 * gpiod_set_raw_array_value() - assign values to an array of GPIOs 3074 * @array_size: number of elements in the descriptor array / value bitmap 3075 * @desc_array: array of GPIO descriptors whose values will be assigned 3076 * @array_info: information on applicability of fast bitmap processing path 3077 * @value_bitmap: bitmap of values to assign 3078 * 3079 * Set the raw values of the GPIOs, i.e. the values of the physical lines 3080 * without regard for their ACTIVE_LOW status. 3081 * 3082 * This function can be called from contexts where we cannot sleep, and will 3083 * complain if the GPIO chip functions potentially sleep. 3084 */ 3085 int gpiod_set_raw_array_value(unsigned int array_size, 3086 struct gpio_desc **desc_array, 3087 struct gpio_array *array_info, 3088 unsigned long *value_bitmap) 3089 { 3090 if (!desc_array) 3091 return -EINVAL; 3092 return gpiod_set_array_value_complex(true, false, array_size, 3093 desc_array, array_info, value_bitmap); 3094 } 3095 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); 3096 3097 /** 3098 * gpiod_set_array_value() - assign values to an array of GPIOs 3099 * @array_size: number of elements in the descriptor array / value bitmap 3100 * @desc_array: array of GPIO descriptors whose values will be assigned 3101 * @array_info: information on applicability of fast bitmap processing path 3102 * @value_bitmap: bitmap of values to assign 3103 * 3104 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3105 * into account. 3106 * 3107 * This function can be called from contexts where we cannot sleep, and will 3108 * complain if the GPIO chip functions potentially sleep. 3109 */ 3110 int gpiod_set_array_value(unsigned int array_size, 3111 struct gpio_desc **desc_array, 3112 struct gpio_array *array_info, 3113 unsigned long *value_bitmap) 3114 { 3115 if (!desc_array) 3116 return -EINVAL; 3117 return gpiod_set_array_value_complex(false, false, array_size, 3118 desc_array, array_info, 3119 value_bitmap); 3120 } 3121 EXPORT_SYMBOL_GPL(gpiod_set_array_value); 3122 3123 /** 3124 * gpiod_cansleep() - report whether gpio value access may sleep 3125 * @desc: gpio to check 3126 * 3127 */ 3128 int gpiod_cansleep(const struct gpio_desc *desc) 3129 { 3130 VALIDATE_DESC(desc); 3131 return desc->gdev->chip->can_sleep; 3132 } 3133 EXPORT_SYMBOL_GPL(gpiod_cansleep); 3134 3135 /** 3136 * gpiod_set_consumer_name() - set the consumer name for the descriptor 3137 * @desc: gpio to set the consumer name on 3138 * @name: the new consumer name 3139 */ 3140 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) 3141 { 3142 VALIDATE_DESC(desc); 3143 if (name) { 3144 name = kstrdup_const(name, GFP_KERNEL); 3145 if (!name) 3146 return -ENOMEM; 3147 } 3148 3149 kfree_const(desc->label); 3150 desc_set_label(desc, name); 3151 3152 return 0; 3153 } 3154 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); 3155 3156 /** 3157 * gpiod_to_irq() - return the IRQ corresponding to a GPIO 3158 * @desc: gpio whose IRQ will be returned (already requested) 3159 * 3160 * Return the IRQ corresponding to the passed GPIO, or an error code in case of 3161 * error. 3162 */ 3163 int gpiod_to_irq(const struct gpio_desc *desc) 3164 { 3165 struct gpio_chip *gc; 3166 int offset; 3167 3168 /* 3169 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics 3170 * requires this function to not return zero on an invalid descriptor 3171 * but rather a negative error number. 3172 */ 3173 if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip) 3174 return -EINVAL; 3175 3176 gc = desc->gdev->chip; 3177 offset = gpio_chip_hwgpio(desc); 3178 if (gc->to_irq) { 3179 int retirq = gc->to_irq(gc, offset); 3180 3181 /* Zero means NO_IRQ */ 3182 if (!retirq) 3183 return -ENXIO; 3184 3185 return retirq; 3186 } 3187 return -ENXIO; 3188 } 3189 EXPORT_SYMBOL_GPL(gpiod_to_irq); 3190 3191 /** 3192 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ 3193 * @gc: the chip the GPIO to lock belongs to 3194 * @offset: the offset of the GPIO to lock as IRQ 3195 * 3196 * This is used directly by GPIO drivers that want to lock down 3197 * a certain GPIO line to be used for IRQs. 3198 */ 3199 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) 3200 { 3201 struct gpio_desc *desc; 3202 3203 desc = gpiochip_get_desc(gc, offset); 3204 if (IS_ERR(desc)) 3205 return PTR_ERR(desc); 3206 3207 /* 3208 * If it's fast: flush the direction setting if something changed 3209 * behind our back 3210 */ 3211 if (!gc->can_sleep && gc->get_direction) { 3212 int dir = gpiod_get_direction(desc); 3213 3214 if (dir < 0) { 3215 chip_err(gc, "%s: cannot get GPIO direction\n", 3216 __func__); 3217 return dir; 3218 } 3219 } 3220 3221 /* To be valid for IRQ the line needs to be input or open drain */ 3222 if (test_bit(FLAG_IS_OUT, &desc->flags) && 3223 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 3224 chip_err(gc, 3225 "%s: tried to flag a GPIO set as output for IRQ\n", 3226 __func__); 3227 return -EIO; 3228 } 3229 3230 set_bit(FLAG_USED_AS_IRQ, &desc->flags); 3231 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3232 3233 /* 3234 * If the consumer has not set up a label (such as when the 3235 * IRQ is referenced from .to_irq()) we set up a label here 3236 * so it is clear this is used as an interrupt. 3237 */ 3238 if (!desc->label) 3239 desc_set_label(desc, "interrupt"); 3240 3241 return 0; 3242 } 3243 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); 3244 3245 /** 3246 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ 3247 * @gc: the chip the GPIO to lock belongs to 3248 * @offset: the offset of the GPIO to lock as IRQ 3249 * 3250 * This is used directly by GPIO drivers that want to indicate 3251 * that a certain GPIO is no longer used exclusively for IRQ. 3252 */ 3253 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset) 3254 { 3255 struct gpio_desc *desc; 3256 3257 desc = gpiochip_get_desc(gc, offset); 3258 if (IS_ERR(desc)) 3259 return; 3260 3261 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); 3262 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3263 3264 /* If we only had this marking, erase it */ 3265 if (desc->label && !strcmp(desc->label, "interrupt")) 3266 desc_set_label(desc, NULL); 3267 } 3268 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); 3269 3270 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset) 3271 { 3272 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 3273 3274 if (!IS_ERR(desc) && 3275 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) 3276 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3277 } 3278 EXPORT_SYMBOL_GPL(gpiochip_disable_irq); 3279 3280 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset) 3281 { 3282 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 3283 3284 if (!IS_ERR(desc) && 3285 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { 3286 /* 3287 * We must not be output when using IRQ UNLESS we are 3288 * open drain. 3289 */ 3290 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && 3291 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); 3292 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3293 } 3294 } 3295 EXPORT_SYMBOL_GPL(gpiochip_enable_irq); 3296 3297 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset) 3298 { 3299 if (offset >= gc->ngpio) 3300 return false; 3301 3302 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); 3303 } 3304 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); 3305 3306 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset) 3307 { 3308 int ret; 3309 3310 if (!try_module_get(gc->gpiodev->owner)) 3311 return -ENODEV; 3312 3313 ret = gpiochip_lock_as_irq(gc, offset); 3314 if (ret) { 3315 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); 3316 module_put(gc->gpiodev->owner); 3317 return ret; 3318 } 3319 return 0; 3320 } 3321 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); 3322 3323 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset) 3324 { 3325 gpiochip_unlock_as_irq(gc, offset); 3326 module_put(gc->gpiodev->owner); 3327 } 3328 EXPORT_SYMBOL_GPL(gpiochip_relres_irq); 3329 3330 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset) 3331 { 3332 if (offset >= gc->ngpio) 3333 return false; 3334 3335 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); 3336 } 3337 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); 3338 3339 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset) 3340 { 3341 if (offset >= gc->ngpio) 3342 return false; 3343 3344 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); 3345 } 3346 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); 3347 3348 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset) 3349 { 3350 if (offset >= gc->ngpio) 3351 return false; 3352 3353 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); 3354 } 3355 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); 3356 3357 /** 3358 * gpiod_get_raw_value_cansleep() - return a gpio's raw value 3359 * @desc: gpio whose value will be returned 3360 * 3361 * Return the GPIO's raw value, i.e. the value of the physical line disregarding 3362 * its ACTIVE_LOW status, or negative errno on failure. 3363 * 3364 * This function is to be called from contexts that can sleep. 3365 */ 3366 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 3367 { 3368 might_sleep_if(extra_checks); 3369 VALIDATE_DESC(desc); 3370 return gpiod_get_raw_value_commit(desc); 3371 } 3372 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); 3373 3374 /** 3375 * gpiod_get_value_cansleep() - return a gpio's value 3376 * @desc: gpio whose value will be returned 3377 * 3378 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into 3379 * account, or negative errno on failure. 3380 * 3381 * This function is to be called from contexts that can sleep. 3382 */ 3383 int gpiod_get_value_cansleep(const struct gpio_desc *desc) 3384 { 3385 int value; 3386 3387 might_sleep_if(extra_checks); 3388 VALIDATE_DESC(desc); 3389 value = gpiod_get_raw_value_commit(desc); 3390 if (value < 0) 3391 return value; 3392 3393 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3394 value = !value; 3395 3396 return value; 3397 } 3398 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); 3399 3400 /** 3401 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs 3402 * @array_size: number of elements in the descriptor array / value bitmap 3403 * @desc_array: array of GPIO descriptors whose values will be read 3404 * @array_info: information on applicability of fast bitmap processing path 3405 * @value_bitmap: bitmap to store the read values 3406 * 3407 * Read the raw values of the GPIOs, i.e. the values of the physical lines 3408 * without regard for their ACTIVE_LOW status. Return 0 in case of success, 3409 * else an error code. 3410 * 3411 * This function is to be called from contexts that can sleep. 3412 */ 3413 int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 3414 struct gpio_desc **desc_array, 3415 struct gpio_array *array_info, 3416 unsigned long *value_bitmap) 3417 { 3418 might_sleep_if(extra_checks); 3419 if (!desc_array) 3420 return -EINVAL; 3421 return gpiod_get_array_value_complex(true, true, array_size, 3422 desc_array, array_info, 3423 value_bitmap); 3424 } 3425 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); 3426 3427 /** 3428 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs 3429 * @array_size: number of elements in the descriptor array / value bitmap 3430 * @desc_array: array of GPIO descriptors whose values will be read 3431 * @array_info: information on applicability of fast bitmap processing path 3432 * @value_bitmap: bitmap to store the read values 3433 * 3434 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3435 * into account. Return 0 in case of success, else an error code. 3436 * 3437 * This function is to be called from contexts that can sleep. 3438 */ 3439 int gpiod_get_array_value_cansleep(unsigned int array_size, 3440 struct gpio_desc **desc_array, 3441 struct gpio_array *array_info, 3442 unsigned long *value_bitmap) 3443 { 3444 might_sleep_if(extra_checks); 3445 if (!desc_array) 3446 return -EINVAL; 3447 return gpiod_get_array_value_complex(false, true, array_size, 3448 desc_array, array_info, 3449 value_bitmap); 3450 } 3451 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); 3452 3453 /** 3454 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value 3455 * @desc: gpio whose value will be assigned 3456 * @value: value to assign 3457 * 3458 * Set the raw value of the GPIO, i.e. the value of its physical line without 3459 * regard for its ACTIVE_LOW status. 3460 * 3461 * This function is to be called from contexts that can sleep. 3462 */ 3463 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) 3464 { 3465 might_sleep_if(extra_checks); 3466 VALIDATE_DESC_VOID(desc); 3467 gpiod_set_raw_value_commit(desc, value); 3468 } 3469 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); 3470 3471 /** 3472 * gpiod_set_value_cansleep() - assign a gpio's value 3473 * @desc: gpio whose value will be assigned 3474 * @value: value to assign 3475 * 3476 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 3477 * account 3478 * 3479 * This function is to be called from contexts that can sleep. 3480 */ 3481 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 3482 { 3483 might_sleep_if(extra_checks); 3484 VALIDATE_DESC_VOID(desc); 3485 gpiod_set_value_nocheck(desc, value); 3486 } 3487 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 3488 3489 /** 3490 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs 3491 * @array_size: number of elements in the descriptor array / value bitmap 3492 * @desc_array: array of GPIO descriptors whose values will be assigned 3493 * @array_info: information on applicability of fast bitmap processing path 3494 * @value_bitmap: bitmap of values to assign 3495 * 3496 * Set the raw values of the GPIOs, i.e. the values of the physical lines 3497 * without regard for their ACTIVE_LOW status. 3498 * 3499 * This function is to be called from contexts that can sleep. 3500 */ 3501 int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 3502 struct gpio_desc **desc_array, 3503 struct gpio_array *array_info, 3504 unsigned long *value_bitmap) 3505 { 3506 might_sleep_if(extra_checks); 3507 if (!desc_array) 3508 return -EINVAL; 3509 return gpiod_set_array_value_complex(true, true, array_size, desc_array, 3510 array_info, value_bitmap); 3511 } 3512 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); 3513 3514 /** 3515 * gpiod_add_lookup_tables() - register GPIO device consumers 3516 * @tables: list of tables of consumers to register 3517 * @n: number of tables in the list 3518 */ 3519 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) 3520 { 3521 unsigned int i; 3522 3523 mutex_lock(&gpio_lookup_lock); 3524 3525 for (i = 0; i < n; i++) 3526 list_add_tail(&tables[i]->list, &gpio_lookup_list); 3527 3528 mutex_unlock(&gpio_lookup_lock); 3529 } 3530 3531 /** 3532 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs 3533 * @array_size: number of elements in the descriptor array / value bitmap 3534 * @desc_array: array of GPIO descriptors whose values will be assigned 3535 * @array_info: information on applicability of fast bitmap processing path 3536 * @value_bitmap: bitmap of values to assign 3537 * 3538 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3539 * into account. 3540 * 3541 * This function is to be called from contexts that can sleep. 3542 */ 3543 int gpiod_set_array_value_cansleep(unsigned int array_size, 3544 struct gpio_desc **desc_array, 3545 struct gpio_array *array_info, 3546 unsigned long *value_bitmap) 3547 { 3548 might_sleep_if(extra_checks); 3549 if (!desc_array) 3550 return -EINVAL; 3551 return gpiod_set_array_value_complex(false, true, array_size, 3552 desc_array, array_info, 3553 value_bitmap); 3554 } 3555 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); 3556 3557 /** 3558 * gpiod_add_lookup_table() - register GPIO device consumers 3559 * @table: table of consumers to register 3560 */ 3561 void gpiod_add_lookup_table(struct gpiod_lookup_table *table) 3562 { 3563 mutex_lock(&gpio_lookup_lock); 3564 3565 list_add_tail(&table->list, &gpio_lookup_list); 3566 3567 mutex_unlock(&gpio_lookup_lock); 3568 } 3569 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); 3570 3571 /** 3572 * gpiod_remove_lookup_table() - unregister GPIO device consumers 3573 * @table: table of consumers to unregister 3574 */ 3575 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) 3576 { 3577 mutex_lock(&gpio_lookup_lock); 3578 3579 list_del(&table->list); 3580 3581 mutex_unlock(&gpio_lookup_lock); 3582 } 3583 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); 3584 3585 /** 3586 * gpiod_add_hogs() - register a set of GPIO hogs from machine code 3587 * @hogs: table of gpio hog entries with a zeroed sentinel at the end 3588 */ 3589 void gpiod_add_hogs(struct gpiod_hog *hogs) 3590 { 3591 struct gpio_chip *gc; 3592 struct gpiod_hog *hog; 3593 3594 mutex_lock(&gpio_machine_hogs_mutex); 3595 3596 for (hog = &hogs[0]; hog->chip_label; hog++) { 3597 list_add_tail(&hog->list, &gpio_machine_hogs); 3598 3599 /* 3600 * The chip may have been registered earlier, so check if it 3601 * exists and, if so, try to hog the line now. 3602 */ 3603 gc = find_chip_by_name(hog->chip_label); 3604 if (gc) 3605 gpiochip_machine_hog(gc, hog); 3606 } 3607 3608 mutex_unlock(&gpio_machine_hogs_mutex); 3609 } 3610 EXPORT_SYMBOL_GPL(gpiod_add_hogs); 3611 3612 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) 3613 { 3614 const char *dev_id = dev ? dev_name(dev) : NULL; 3615 struct gpiod_lookup_table *table; 3616 3617 mutex_lock(&gpio_lookup_lock); 3618 3619 list_for_each_entry(table, &gpio_lookup_list, list) { 3620 if (table->dev_id && dev_id) { 3621 /* 3622 * Valid strings on both ends, must be identical to have 3623 * a match 3624 */ 3625 if (!strcmp(table->dev_id, dev_id)) 3626 goto found; 3627 } else { 3628 /* 3629 * One of the pointers is NULL, so both must be to have 3630 * a match 3631 */ 3632 if (dev_id == table->dev_id) 3633 goto found; 3634 } 3635 } 3636 table = NULL; 3637 3638 found: 3639 mutex_unlock(&gpio_lookup_lock); 3640 return table; 3641 } 3642 3643 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 3644 unsigned int idx, unsigned long *flags) 3645 { 3646 struct gpio_desc *desc = ERR_PTR(-ENOENT); 3647 struct gpiod_lookup_table *table; 3648 struct gpiod_lookup *p; 3649 3650 table = gpiod_find_lookup_table(dev); 3651 if (!table) 3652 return desc; 3653 3654 for (p = &table->table[0]; p->key; p++) { 3655 struct gpio_chip *gc; 3656 3657 /* idx must always match exactly */ 3658 if (p->idx != idx) 3659 continue; 3660 3661 /* If the lookup entry has a con_id, require exact match */ 3662 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) 3663 continue; 3664 3665 if (p->chip_hwnum == U16_MAX) { 3666 desc = gpio_name_to_desc(p->key); 3667 if (desc) { 3668 *flags = p->flags; 3669 return desc; 3670 } 3671 3672 dev_warn(dev, "cannot find GPIO line %s, deferring\n", 3673 p->key); 3674 return ERR_PTR(-EPROBE_DEFER); 3675 } 3676 3677 gc = find_chip_by_name(p->key); 3678 3679 if (!gc) { 3680 /* 3681 * As the lookup table indicates a chip with 3682 * p->key should exist, assume it may 3683 * still appear later and let the interested 3684 * consumer be probed again or let the Deferred 3685 * Probe infrastructure handle the error. 3686 */ 3687 dev_warn(dev, "cannot find GPIO chip %s, deferring\n", 3688 p->key); 3689 return ERR_PTR(-EPROBE_DEFER); 3690 } 3691 3692 if (gc->ngpio <= p->chip_hwnum) { 3693 dev_err(dev, 3694 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", 3695 idx, p->chip_hwnum, gc->ngpio - 1, 3696 gc->label); 3697 return ERR_PTR(-EINVAL); 3698 } 3699 3700 desc = gpiochip_get_desc(gc, p->chip_hwnum); 3701 *flags = p->flags; 3702 3703 return desc; 3704 } 3705 3706 return desc; 3707 } 3708 3709 static int platform_gpio_count(struct device *dev, const char *con_id) 3710 { 3711 struct gpiod_lookup_table *table; 3712 struct gpiod_lookup *p; 3713 unsigned int count = 0; 3714 3715 table = gpiod_find_lookup_table(dev); 3716 if (!table) 3717 return -ENOENT; 3718 3719 for (p = &table->table[0]; p->key; p++) { 3720 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || 3721 (!con_id && !p->con_id)) 3722 count++; 3723 } 3724 if (!count) 3725 return -ENOENT; 3726 3727 return count; 3728 } 3729 3730 /** 3731 * fwnode_gpiod_get_index - obtain a GPIO from firmware node 3732 * @fwnode: handle of the firmware node 3733 * @con_id: function within the GPIO consumer 3734 * @index: index of the GPIO to obtain for the consumer 3735 * @flags: GPIO initialization flags 3736 * @label: label to attach to the requested GPIO 3737 * 3738 * This function can be used for drivers that get their configuration 3739 * from opaque firmware. 3740 * 3741 * The function properly finds the corresponding GPIO using whatever is the 3742 * underlying firmware interface and then makes sure that the GPIO 3743 * descriptor is requested before it is returned to the caller. 3744 * 3745 * Returns: 3746 * On successful request the GPIO pin is configured in accordance with 3747 * provided @flags. 3748 * 3749 * In case of error an ERR_PTR() is returned. 3750 */ 3751 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 3752 const char *con_id, int index, 3753 enum gpiod_flags flags, 3754 const char *label) 3755 { 3756 struct gpio_desc *desc; 3757 char prop_name[32]; /* 32 is max size of property name */ 3758 unsigned int i; 3759 3760 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 3761 if (con_id) 3762 snprintf(prop_name, sizeof(prop_name), "%s-%s", 3763 con_id, gpio_suffixes[i]); 3764 else 3765 snprintf(prop_name, sizeof(prop_name), "%s", 3766 gpio_suffixes[i]); 3767 3768 desc = fwnode_get_named_gpiod(fwnode, prop_name, index, flags, 3769 label); 3770 if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) 3771 break; 3772 } 3773 3774 return desc; 3775 } 3776 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); 3777 3778 /** 3779 * gpiod_count - return the number of GPIOs associated with a device / function 3780 * or -ENOENT if no GPIO has been assigned to the requested function 3781 * @dev: GPIO consumer, can be NULL for system-global GPIOs 3782 * @con_id: function within the GPIO consumer 3783 */ 3784 int gpiod_count(struct device *dev, const char *con_id) 3785 { 3786 int count = -ENOENT; 3787 3788 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) 3789 count = of_gpio_get_count(dev, con_id); 3790 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) 3791 count = acpi_gpio_count(dev, con_id); 3792 3793 if (count < 0) 3794 count = platform_gpio_count(dev, con_id); 3795 3796 return count; 3797 } 3798 EXPORT_SYMBOL_GPL(gpiod_count); 3799 3800 /** 3801 * gpiod_get - obtain a GPIO for a given GPIO function 3802 * @dev: GPIO consumer, can be NULL for system-global GPIOs 3803 * @con_id: function within the GPIO consumer 3804 * @flags: optional GPIO initialization flags 3805 * 3806 * Return the GPIO descriptor corresponding to the function con_id of device 3807 * dev, -ENOENT if no GPIO has been assigned to the requested function, or 3808 * another IS_ERR() code if an error occurred while trying to acquire the GPIO. 3809 */ 3810 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, 3811 enum gpiod_flags flags) 3812 { 3813 return gpiod_get_index(dev, con_id, 0, flags); 3814 } 3815 EXPORT_SYMBOL_GPL(gpiod_get); 3816 3817 /** 3818 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function 3819 * @dev: GPIO consumer, can be NULL for system-global GPIOs 3820 * @con_id: function within the GPIO consumer 3821 * @flags: optional GPIO initialization flags 3822 * 3823 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to 3824 * the requested function it will return NULL. This is convenient for drivers 3825 * that need to handle optional GPIOs. 3826 */ 3827 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 3828 const char *con_id, 3829 enum gpiod_flags flags) 3830 { 3831 return gpiod_get_index_optional(dev, con_id, 0, flags); 3832 } 3833 EXPORT_SYMBOL_GPL(gpiod_get_optional); 3834 3835 3836 /** 3837 * gpiod_configure_flags - helper function to configure a given GPIO 3838 * @desc: gpio whose value will be assigned 3839 * @con_id: function within the GPIO consumer 3840 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 3841 * of_find_gpio() or of_get_gpio_hog() 3842 * @dflags: gpiod_flags - optional GPIO initialization flags 3843 * 3844 * Return 0 on success, -ENOENT if no GPIO has been assigned to the 3845 * requested function and/or index, or another IS_ERR() code if an error 3846 * occurred while trying to acquire the GPIO. 3847 */ 3848 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 3849 unsigned long lflags, enum gpiod_flags dflags) 3850 { 3851 int ret; 3852 3853 if (lflags & GPIO_ACTIVE_LOW) 3854 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 3855 3856 if (lflags & GPIO_OPEN_DRAIN) 3857 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 3858 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { 3859 /* 3860 * This enforces open drain mode from the consumer side. 3861 * This is necessary for some busses like I2C, but the lookup 3862 * should *REALLY* have specified them as open drain in the 3863 * first place, so print a little warning here. 3864 */ 3865 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 3866 gpiod_warn(desc, 3867 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); 3868 } 3869 3870 if (lflags & GPIO_OPEN_SOURCE) 3871 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 3872 3873 if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) { 3874 gpiod_err(desc, 3875 "both pull-up and pull-down enabled, invalid configuration\n"); 3876 return -EINVAL; 3877 } 3878 3879 if (lflags & GPIO_PULL_UP) 3880 set_bit(FLAG_PULL_UP, &desc->flags); 3881 else if (lflags & GPIO_PULL_DOWN) 3882 set_bit(FLAG_PULL_DOWN, &desc->flags); 3883 3884 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); 3885 if (ret < 0) 3886 return ret; 3887 3888 /* No particular flag request, return here... */ 3889 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { 3890 gpiod_dbg(desc, "no flags found for %s\n", con_id); 3891 return 0; 3892 } 3893 3894 /* Process flags */ 3895 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) 3896 ret = gpiod_direction_output(desc, 3897 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); 3898 else 3899 ret = gpiod_direction_input(desc); 3900 3901 return ret; 3902 } 3903 3904 /** 3905 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function 3906 * @dev: GPIO consumer, can be NULL for system-global GPIOs 3907 * @con_id: function within the GPIO consumer 3908 * @idx: index of the GPIO to obtain in the consumer 3909 * @flags: optional GPIO initialization flags 3910 * 3911 * This variant of gpiod_get() allows to access GPIOs other than the first 3912 * defined one for functions that define several GPIOs. 3913 * 3914 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the 3915 * requested function and/or index, or another IS_ERR() code if an error 3916 * occurred while trying to acquire the GPIO. 3917 */ 3918 struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 3919 const char *con_id, 3920 unsigned int idx, 3921 enum gpiod_flags flags) 3922 { 3923 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; 3924 struct gpio_desc *desc = NULL; 3925 int ret; 3926 /* Maybe we have a device name, maybe not */ 3927 const char *devname = dev ? dev_name(dev) : "?"; 3928 3929 dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); 3930 3931 if (dev) { 3932 /* Using device tree? */ 3933 if (IS_ENABLED(CONFIG_OF) && dev->of_node) { 3934 dev_dbg(dev, "using device tree for GPIO lookup\n"); 3935 desc = of_find_gpio(dev, con_id, idx, &lookupflags); 3936 } else if (ACPI_COMPANION(dev)) { 3937 dev_dbg(dev, "using ACPI for GPIO lookup\n"); 3938 desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags); 3939 } 3940 } 3941 3942 /* 3943 * Either we are not using DT or ACPI, or their lookup did not return 3944 * a result. In that case, use platform lookup as a fallback. 3945 */ 3946 if (!desc || desc == ERR_PTR(-ENOENT)) { 3947 dev_dbg(dev, "using lookup tables for GPIO lookup\n"); 3948 desc = gpiod_find(dev, con_id, idx, &lookupflags); 3949 } 3950 3951 if (IS_ERR(desc)) { 3952 dev_dbg(dev, "No GPIO consumer %s found\n", con_id); 3953 return desc; 3954 } 3955 3956 /* 3957 * If a connection label was passed use that, else attempt to use 3958 * the device name as label 3959 */ 3960 ret = gpiod_request(desc, con_id ? con_id : devname); 3961 if (ret < 0) { 3962 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { 3963 /* 3964 * This happens when there are several consumers for 3965 * the same GPIO line: we just return here without 3966 * further initialization. It is a bit if a hack. 3967 * This is necessary to support fixed regulators. 3968 * 3969 * FIXME: Make this more sane and safe. 3970 */ 3971 dev_info(dev, "nonexclusive access to GPIO for %s\n", 3972 con_id ? con_id : devname); 3973 return desc; 3974 } else { 3975 return ERR_PTR(ret); 3976 } 3977 } 3978 3979 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); 3980 if (ret < 0) { 3981 dev_dbg(dev, "setup of GPIO %s failed\n", con_id); 3982 gpiod_put(desc); 3983 return ERR_PTR(ret); 3984 } 3985 3986 blocking_notifier_call_chain(&desc->gdev->notifier, 3987 GPIOLINE_CHANGED_REQUESTED, desc); 3988 3989 return desc; 3990 } 3991 EXPORT_SYMBOL_GPL(gpiod_get_index); 3992 3993 /** 3994 * fwnode_get_named_gpiod - obtain a GPIO from firmware node 3995 * @fwnode: handle of the firmware node 3996 * @propname: name of the firmware property representing the GPIO 3997 * @index: index of the GPIO to obtain for the consumer 3998 * @dflags: GPIO initialization flags 3999 * @label: label to attach to the requested GPIO 4000 * 4001 * This function can be used for drivers that get their configuration 4002 * from opaque firmware. 4003 * 4004 * The function properly finds the corresponding GPIO using whatever is the 4005 * underlying firmware interface and then makes sure that the GPIO 4006 * descriptor is requested before it is returned to the caller. 4007 * 4008 * Returns: 4009 * On successful request the GPIO pin is configured in accordance with 4010 * provided @dflags. 4011 * 4012 * In case of error an ERR_PTR() is returned. 4013 */ 4014 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode, 4015 const char *propname, int index, 4016 enum gpiod_flags dflags, 4017 const char *label) 4018 { 4019 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 4020 struct gpio_desc *desc = ERR_PTR(-ENODEV); 4021 int ret; 4022 4023 if (!fwnode) 4024 return ERR_PTR(-EINVAL); 4025 4026 if (is_of_node(fwnode)) { 4027 desc = gpiod_get_from_of_node(to_of_node(fwnode), 4028 propname, index, 4029 dflags, 4030 label); 4031 return desc; 4032 } else if (is_acpi_node(fwnode)) { 4033 struct acpi_gpio_info info; 4034 4035 desc = acpi_node_get_gpiod(fwnode, propname, index, &info); 4036 if (IS_ERR(desc)) 4037 return desc; 4038 4039 acpi_gpio_update_gpiod_flags(&dflags, &info); 4040 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info); 4041 } 4042 4043 /* Currently only ACPI takes this path */ 4044 ret = gpiod_request(desc, label); 4045 if (ret) 4046 return ERR_PTR(ret); 4047 4048 ret = gpiod_configure_flags(desc, propname, lflags, dflags); 4049 if (ret < 0) { 4050 gpiod_put(desc); 4051 return ERR_PTR(ret); 4052 } 4053 4054 blocking_notifier_call_chain(&desc->gdev->notifier, 4055 GPIOLINE_CHANGED_REQUESTED, desc); 4056 4057 return desc; 4058 } 4059 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod); 4060 4061 /** 4062 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO 4063 * function 4064 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4065 * @con_id: function within the GPIO consumer 4066 * @index: index of the GPIO to obtain in the consumer 4067 * @flags: optional GPIO initialization flags 4068 * 4069 * This is equivalent to gpiod_get_index(), except that when no GPIO with the 4070 * specified index was assigned to the requested function it will return NULL. 4071 * This is convenient for drivers that need to handle optional GPIOs. 4072 */ 4073 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 4074 const char *con_id, 4075 unsigned int index, 4076 enum gpiod_flags flags) 4077 { 4078 struct gpio_desc *desc; 4079 4080 desc = gpiod_get_index(dev, con_id, index, flags); 4081 if (IS_ERR(desc)) { 4082 if (PTR_ERR(desc) == -ENOENT) 4083 return NULL; 4084 } 4085 4086 return desc; 4087 } 4088 EXPORT_SYMBOL_GPL(gpiod_get_index_optional); 4089 4090 /** 4091 * gpiod_hog - Hog the specified GPIO desc given the provided flags 4092 * @desc: gpio whose value will be assigned 4093 * @name: gpio line name 4094 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 4095 * of_find_gpio() or of_get_gpio_hog() 4096 * @dflags: gpiod_flags - optional GPIO initialization flags 4097 */ 4098 int gpiod_hog(struct gpio_desc *desc, const char *name, 4099 unsigned long lflags, enum gpiod_flags dflags) 4100 { 4101 struct gpio_chip *gc; 4102 struct gpio_desc *local_desc; 4103 int hwnum; 4104 int ret; 4105 4106 gc = gpiod_to_chip(desc); 4107 hwnum = gpio_chip_hwgpio(desc); 4108 4109 local_desc = gpiochip_request_own_desc(gc, hwnum, name, 4110 lflags, dflags); 4111 if (IS_ERR(local_desc)) { 4112 ret = PTR_ERR(local_desc); 4113 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", 4114 name, gc->label, hwnum, ret); 4115 return ret; 4116 } 4117 4118 /* Mark GPIO as hogged so it can be identified and removed later */ 4119 set_bit(FLAG_IS_HOGGED, &desc->flags); 4120 4121 gpiod_info(desc, "hogged as %s%s\n", 4122 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", 4123 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? 4124 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : ""); 4125 4126 return 0; 4127 } 4128 4129 /** 4130 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog 4131 * @gc: gpio chip to act on 4132 */ 4133 static void gpiochip_free_hogs(struct gpio_chip *gc) 4134 { 4135 int id; 4136 4137 for (id = 0; id < gc->ngpio; id++) { 4138 if (test_bit(FLAG_IS_HOGGED, &gc->gpiodev->descs[id].flags)) 4139 gpiochip_free_own_desc(&gc->gpiodev->descs[id]); 4140 } 4141 } 4142 4143 /** 4144 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function 4145 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4146 * @con_id: function within the GPIO consumer 4147 * @flags: optional GPIO initialization flags 4148 * 4149 * This function acquires all the GPIOs defined under a given function. 4150 * 4151 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if 4152 * no GPIO has been assigned to the requested function, or another IS_ERR() 4153 * code if an error occurred while trying to acquire the GPIOs. 4154 */ 4155 struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 4156 const char *con_id, 4157 enum gpiod_flags flags) 4158 { 4159 struct gpio_desc *desc; 4160 struct gpio_descs *descs; 4161 struct gpio_array *array_info = NULL; 4162 struct gpio_chip *gc; 4163 int count, bitmap_size; 4164 4165 count = gpiod_count(dev, con_id); 4166 if (count < 0) 4167 return ERR_PTR(count); 4168 4169 descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL); 4170 if (!descs) 4171 return ERR_PTR(-ENOMEM); 4172 4173 for (descs->ndescs = 0; descs->ndescs < count; ) { 4174 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); 4175 if (IS_ERR(desc)) { 4176 gpiod_put_array(descs); 4177 return ERR_CAST(desc); 4178 } 4179 4180 descs->desc[descs->ndescs] = desc; 4181 4182 gc = gpiod_to_chip(desc); 4183 /* 4184 * If pin hardware number of array member 0 is also 0, select 4185 * its chip as a candidate for fast bitmap processing path. 4186 */ 4187 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { 4188 struct gpio_descs *array; 4189 4190 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? 4191 gc->ngpio : count); 4192 4193 array = kzalloc(struct_size(descs, desc, count) + 4194 struct_size(array_info, invert_mask, 4195 3 * bitmap_size), GFP_KERNEL); 4196 if (!array) { 4197 gpiod_put_array(descs); 4198 return ERR_PTR(-ENOMEM); 4199 } 4200 4201 memcpy(array, descs, 4202 struct_size(descs, desc, descs->ndescs + 1)); 4203 kfree(descs); 4204 4205 descs = array; 4206 array_info = (void *)(descs->desc + count); 4207 array_info->get_mask = array_info->invert_mask + 4208 bitmap_size; 4209 array_info->set_mask = array_info->get_mask + 4210 bitmap_size; 4211 4212 array_info->desc = descs->desc; 4213 array_info->size = count; 4214 array_info->chip = gc; 4215 bitmap_set(array_info->get_mask, descs->ndescs, 4216 count - descs->ndescs); 4217 bitmap_set(array_info->set_mask, descs->ndescs, 4218 count - descs->ndescs); 4219 descs->info = array_info; 4220 } 4221 /* Unmark array members which don't belong to the 'fast' chip */ 4222 if (array_info && array_info->chip != gc) { 4223 __clear_bit(descs->ndescs, array_info->get_mask); 4224 __clear_bit(descs->ndescs, array_info->set_mask); 4225 } 4226 /* 4227 * Detect array members which belong to the 'fast' chip 4228 * but their pins are not in hardware order. 4229 */ 4230 else if (array_info && 4231 gpio_chip_hwgpio(desc) != descs->ndescs) { 4232 /* 4233 * Don't use fast path if all array members processed so 4234 * far belong to the same chip as this one but its pin 4235 * hardware number is different from its array index. 4236 */ 4237 if (bitmap_full(array_info->get_mask, descs->ndescs)) { 4238 array_info = NULL; 4239 } else { 4240 __clear_bit(descs->ndescs, 4241 array_info->get_mask); 4242 __clear_bit(descs->ndescs, 4243 array_info->set_mask); 4244 } 4245 } else if (array_info) { 4246 /* Exclude open drain or open source from fast output */ 4247 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || 4248 gpiochip_line_is_open_source(gc, descs->ndescs)) 4249 __clear_bit(descs->ndescs, 4250 array_info->set_mask); 4251 /* Identify 'fast' pins which require invertion */ 4252 if (gpiod_is_active_low(desc)) 4253 __set_bit(descs->ndescs, 4254 array_info->invert_mask); 4255 } 4256 4257 descs->ndescs++; 4258 } 4259 if (array_info) 4260 dev_dbg(dev, 4261 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", 4262 array_info->chip->label, array_info->size, 4263 *array_info->get_mask, *array_info->set_mask, 4264 *array_info->invert_mask); 4265 return descs; 4266 } 4267 EXPORT_SYMBOL_GPL(gpiod_get_array); 4268 4269 /** 4270 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO 4271 * function 4272 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4273 * @con_id: function within the GPIO consumer 4274 * @flags: optional GPIO initialization flags 4275 * 4276 * This is equivalent to gpiod_get_array(), except that when no GPIO was 4277 * assigned to the requested function it will return NULL. 4278 */ 4279 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 4280 const char *con_id, 4281 enum gpiod_flags flags) 4282 { 4283 struct gpio_descs *descs; 4284 4285 descs = gpiod_get_array(dev, con_id, flags); 4286 if (PTR_ERR(descs) == -ENOENT) 4287 return NULL; 4288 4289 return descs; 4290 } 4291 EXPORT_SYMBOL_GPL(gpiod_get_array_optional); 4292 4293 /** 4294 * gpiod_put - dispose of a GPIO descriptor 4295 * @desc: GPIO descriptor to dispose of 4296 * 4297 * No descriptor can be used after gpiod_put() has been called on it. 4298 */ 4299 void gpiod_put(struct gpio_desc *desc) 4300 { 4301 if (desc) 4302 gpiod_free(desc); 4303 } 4304 EXPORT_SYMBOL_GPL(gpiod_put); 4305 4306 /** 4307 * gpiod_put_array - dispose of multiple GPIO descriptors 4308 * @descs: struct gpio_descs containing an array of descriptors 4309 */ 4310 void gpiod_put_array(struct gpio_descs *descs) 4311 { 4312 unsigned int i; 4313 4314 for (i = 0; i < descs->ndescs; i++) 4315 gpiod_put(descs->desc[i]); 4316 4317 kfree(descs); 4318 } 4319 EXPORT_SYMBOL_GPL(gpiod_put_array); 4320 4321 static int __init gpiolib_dev_init(void) 4322 { 4323 int ret; 4324 4325 /* Register GPIO sysfs bus */ 4326 ret = bus_register(&gpio_bus_type); 4327 if (ret < 0) { 4328 pr_err("gpiolib: could not register GPIO bus type\n"); 4329 return ret; 4330 } 4331 4332 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME); 4333 if (ret < 0) { 4334 pr_err("gpiolib: failed to allocate char dev region\n"); 4335 bus_unregister(&gpio_bus_type); 4336 return ret; 4337 } 4338 4339 gpiolib_initialized = true; 4340 gpiochip_setup_devs(); 4341 4342 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO) 4343 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier)); 4344 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */ 4345 4346 return ret; 4347 } 4348 core_initcall(gpiolib_dev_init); 4349 4350 #ifdef CONFIG_DEBUG_FS 4351 4352 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) 4353 { 4354 unsigned i; 4355 struct gpio_chip *gc = gdev->chip; 4356 unsigned gpio = gdev->base; 4357 struct gpio_desc *gdesc = &gdev->descs[0]; 4358 bool is_out; 4359 bool is_irq; 4360 bool active_low; 4361 4362 for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) { 4363 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) { 4364 if (gdesc->name) { 4365 seq_printf(s, " gpio-%-3d (%-20.20s)\n", 4366 gpio, gdesc->name); 4367 } 4368 continue; 4369 } 4370 4371 gpiod_get_direction(gdesc); 4372 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); 4373 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); 4374 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags); 4375 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s", 4376 gpio, gdesc->name ? gdesc->name : "", gdesc->label, 4377 is_out ? "out" : "in ", 4378 gc->get ? (gc->get(gc, i) ? "hi" : "lo") : "? ", 4379 is_irq ? "IRQ " : "", 4380 active_low ? "ACTIVE LOW" : ""); 4381 seq_printf(s, "\n"); 4382 } 4383 } 4384 4385 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) 4386 { 4387 unsigned long flags; 4388 struct gpio_device *gdev = NULL; 4389 loff_t index = *pos; 4390 4391 s->private = ""; 4392 4393 spin_lock_irqsave(&gpio_lock, flags); 4394 list_for_each_entry(gdev, &gpio_devices, list) 4395 if (index-- == 0) { 4396 spin_unlock_irqrestore(&gpio_lock, flags); 4397 return gdev; 4398 } 4399 spin_unlock_irqrestore(&gpio_lock, flags); 4400 4401 return NULL; 4402 } 4403 4404 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) 4405 { 4406 unsigned long flags; 4407 struct gpio_device *gdev = v; 4408 void *ret = NULL; 4409 4410 spin_lock_irqsave(&gpio_lock, flags); 4411 if (list_is_last(&gdev->list, &gpio_devices)) 4412 ret = NULL; 4413 else 4414 ret = list_entry(gdev->list.next, struct gpio_device, list); 4415 spin_unlock_irqrestore(&gpio_lock, flags); 4416 4417 s->private = "\n"; 4418 ++*pos; 4419 4420 return ret; 4421 } 4422 4423 static void gpiolib_seq_stop(struct seq_file *s, void *v) 4424 { 4425 } 4426 4427 static int gpiolib_seq_show(struct seq_file *s, void *v) 4428 { 4429 struct gpio_device *gdev = v; 4430 struct gpio_chip *gc = gdev->chip; 4431 struct device *parent; 4432 4433 if (!gc) { 4434 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private, 4435 dev_name(&gdev->dev)); 4436 return 0; 4437 } 4438 4439 seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private, 4440 dev_name(&gdev->dev), 4441 gdev->base, gdev->base + gdev->ngpio - 1); 4442 parent = gc->parent; 4443 if (parent) 4444 seq_printf(s, ", parent: %s/%s", 4445 parent->bus ? parent->bus->name : "no-bus", 4446 dev_name(parent)); 4447 if (gc->label) 4448 seq_printf(s, ", %s", gc->label); 4449 if (gc->can_sleep) 4450 seq_printf(s, ", can sleep"); 4451 seq_printf(s, ":\n"); 4452 4453 if (gc->dbg_show) 4454 gc->dbg_show(s, gc); 4455 else 4456 gpiolib_dbg_show(s, gdev); 4457 4458 return 0; 4459 } 4460 4461 static const struct seq_operations gpiolib_sops = { 4462 .start = gpiolib_seq_start, 4463 .next = gpiolib_seq_next, 4464 .stop = gpiolib_seq_stop, 4465 .show = gpiolib_seq_show, 4466 }; 4467 DEFINE_SEQ_ATTRIBUTE(gpiolib); 4468 4469 static int __init gpiolib_debugfs_init(void) 4470 { 4471 /* /sys/kernel/debug/gpio */ 4472 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops); 4473 return 0; 4474 } 4475 subsys_initcall(gpiolib_debugfs_init); 4476 4477 #endif /* DEBUG_FS */ 4478