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