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