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