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 /** 2401 * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor 2402 * @gc: GPIO chip 2403 * @hwnum: hardware number of the GPIO for which to request the descriptor 2404 * @label: label for the GPIO 2405 * @lflags: lookup flags for this GPIO or 0 if default, this can be used to 2406 * specify things like line inversion semantics with the machine flags 2407 * such as GPIO_OUT_LOW 2408 * @dflags: descriptor request flags for this GPIO or 0 if default, this 2409 * can be used to specify consumer semantics such as open drain 2410 * 2411 * Function allows GPIO chip drivers to request and use their own GPIO 2412 * descriptors via gpiolib API. Difference to gpiod_request() is that this 2413 * function will not increase reference count of the GPIO chip module. This 2414 * allows the GPIO chip module to be unloaded as needed (we assume that the 2415 * GPIO chip driver handles freeing the GPIOs it has requested). 2416 * 2417 * Returns: 2418 * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error 2419 * code on failure. 2420 */ 2421 struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *gc, 2422 unsigned int hwnum, 2423 const char *label, 2424 enum gpio_lookup_flags lflags, 2425 enum gpiod_flags dflags) 2426 { 2427 struct gpio_desc *desc = gpiochip_get_desc(gc, hwnum); 2428 int ret; 2429 2430 if (IS_ERR(desc)) { 2431 chip_err(gc, "failed to get GPIO descriptor\n"); 2432 return desc; 2433 } 2434 2435 ret = gpiod_request_commit(desc, label); 2436 if (ret < 0) 2437 return ERR_PTR(ret); 2438 2439 ret = gpiod_configure_flags(desc, label, lflags, dflags); 2440 if (ret) { 2441 chip_err(gc, "setup of own GPIO %s failed\n", label); 2442 gpiod_free_commit(desc); 2443 return ERR_PTR(ret); 2444 } 2445 2446 return desc; 2447 } 2448 EXPORT_SYMBOL_GPL(gpiochip_request_own_desc); 2449 2450 /** 2451 * gpiochip_free_own_desc - Free GPIO requested by the chip driver 2452 * @desc: GPIO descriptor to free 2453 * 2454 * Function frees the given GPIO requested previously with 2455 * gpiochip_request_own_desc(). 2456 */ 2457 void gpiochip_free_own_desc(struct gpio_desc *desc) 2458 { 2459 if (desc) 2460 gpiod_free_commit(desc); 2461 } 2462 EXPORT_SYMBOL_GPL(gpiochip_free_own_desc); 2463 2464 /* 2465 * Drivers MUST set GPIO direction before making get/set calls. In 2466 * some cases this is done in early boot, before IRQs are enabled. 2467 * 2468 * As a rule these aren't called more than once (except for drivers 2469 * using the open-drain emulation idiom) so these are natural places 2470 * to accumulate extra debugging checks. Note that we can't (yet) 2471 * rely on gpio_request() having been called beforehand. 2472 */ 2473 2474 static int gpio_do_set_config(struct gpio_chip *gc, unsigned int offset, 2475 unsigned long config) 2476 { 2477 if (!gc->set_config) 2478 return -ENOTSUPP; 2479 2480 return gc->set_config(gc, offset, config); 2481 } 2482 2483 static int gpio_set_config_with_argument(struct gpio_desc *desc, 2484 enum pin_config_param mode, 2485 u32 argument) 2486 { 2487 unsigned long config; 2488 2489 CLASS(gpio_chip_guard, guard)(desc); 2490 if (!guard.gc) 2491 return -ENODEV; 2492 2493 config = pinconf_to_config_packed(mode, argument); 2494 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); 2495 } 2496 2497 static int gpio_set_config_with_argument_optional(struct gpio_desc *desc, 2498 enum pin_config_param mode, 2499 u32 argument) 2500 { 2501 struct device *dev = &desc->gdev->dev; 2502 int gpio = gpio_chip_hwgpio(desc); 2503 int ret; 2504 2505 ret = gpio_set_config_with_argument(desc, mode, argument); 2506 if (ret != -ENOTSUPP) 2507 return ret; 2508 2509 switch (mode) { 2510 case PIN_CONFIG_PERSIST_STATE: 2511 dev_dbg(dev, "Persistence not supported for GPIO %d\n", gpio); 2512 break; 2513 default: 2514 break; 2515 } 2516 2517 return 0; 2518 } 2519 2520 static int gpio_set_config(struct gpio_desc *desc, enum pin_config_param mode) 2521 { 2522 return gpio_set_config_with_argument(desc, mode, 0); 2523 } 2524 2525 static int gpio_set_bias(struct gpio_desc *desc) 2526 { 2527 enum pin_config_param bias; 2528 unsigned long flags; 2529 unsigned int arg; 2530 2531 flags = READ_ONCE(desc->flags); 2532 2533 if (test_bit(FLAG_BIAS_DISABLE, &flags)) 2534 bias = PIN_CONFIG_BIAS_DISABLE; 2535 else if (test_bit(FLAG_PULL_UP, &flags)) 2536 bias = PIN_CONFIG_BIAS_PULL_UP; 2537 else if (test_bit(FLAG_PULL_DOWN, &flags)) 2538 bias = PIN_CONFIG_BIAS_PULL_DOWN; 2539 else 2540 return 0; 2541 2542 switch (bias) { 2543 case PIN_CONFIG_BIAS_PULL_DOWN: 2544 case PIN_CONFIG_BIAS_PULL_UP: 2545 arg = 1; 2546 break; 2547 2548 default: 2549 arg = 0; 2550 break; 2551 } 2552 2553 return gpio_set_config_with_argument_optional(desc, bias, arg); 2554 } 2555 2556 /** 2557 * gpio_set_debounce_timeout() - Set debounce timeout 2558 * @desc: GPIO descriptor to set the debounce timeout 2559 * @debounce: Debounce timeout in microseconds 2560 * 2561 * The function calls the certain GPIO driver to set debounce timeout 2562 * in the hardware. 2563 * 2564 * Returns 0 on success, or negative error code otherwise. 2565 */ 2566 int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce) 2567 { 2568 return gpio_set_config_with_argument_optional(desc, 2569 PIN_CONFIG_INPUT_DEBOUNCE, 2570 debounce); 2571 } 2572 2573 /** 2574 * gpiod_direction_input - set the GPIO direction to input 2575 * @desc: GPIO to set to input 2576 * 2577 * Set the direction of the passed GPIO to input, such as gpiod_get_value() can 2578 * be called safely on it. 2579 * 2580 * Return 0 in case of success, else an error code. 2581 */ 2582 int gpiod_direction_input(struct gpio_desc *desc) 2583 { 2584 int ret = 0; 2585 2586 VALIDATE_DESC(desc); 2587 2588 CLASS(gpio_chip_guard, guard)(desc); 2589 if (!guard.gc) 2590 return -ENODEV; 2591 2592 /* 2593 * It is legal to have no .get() and .direction_input() specified if 2594 * the chip is output-only, but you can't specify .direction_input() 2595 * and not support the .get() operation, that doesn't make sense. 2596 */ 2597 if (!guard.gc->get && guard.gc->direction_input) { 2598 gpiod_warn(desc, 2599 "%s: missing get() but have direction_input()\n", 2600 __func__); 2601 return -EIO; 2602 } 2603 2604 /* 2605 * If we have a .direction_input() callback, things are simple, 2606 * just call it. Else we are some input-only chip so try to check the 2607 * direction (if .get_direction() is supported) else we silently 2608 * assume we are in input mode after this. 2609 */ 2610 if (guard.gc->direction_input) { 2611 ret = guard.gc->direction_input(guard.gc, 2612 gpio_chip_hwgpio(desc)); 2613 } else if (guard.gc->get_direction && 2614 (guard.gc->get_direction(guard.gc, 2615 gpio_chip_hwgpio(desc)) != 1)) { 2616 gpiod_warn(desc, 2617 "%s: missing direction_input() operation and line is output\n", 2618 __func__); 2619 return -EIO; 2620 } 2621 if (ret == 0) { 2622 clear_bit(FLAG_IS_OUT, &desc->flags); 2623 ret = gpio_set_bias(desc); 2624 } 2625 2626 trace_gpio_direction(desc_to_gpio(desc), 1, ret); 2627 2628 return ret; 2629 } 2630 EXPORT_SYMBOL_GPL(gpiod_direction_input); 2631 2632 static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value) 2633 { 2634 int val = !!value, ret = 0; 2635 2636 CLASS(gpio_chip_guard, guard)(desc); 2637 if (!guard.gc) 2638 return -ENODEV; 2639 2640 /* 2641 * It's OK not to specify .direction_output() if the gpiochip is 2642 * output-only, but if there is then not even a .set() operation it 2643 * is pretty tricky to drive the output line. 2644 */ 2645 if (!guard.gc->set && !guard.gc->direction_output) { 2646 gpiod_warn(desc, 2647 "%s: missing set() and direction_output() operations\n", 2648 __func__); 2649 return -EIO; 2650 } 2651 2652 if (guard.gc->direction_output) { 2653 ret = guard.gc->direction_output(guard.gc, 2654 gpio_chip_hwgpio(desc), val); 2655 } else { 2656 /* Check that we are in output mode if we can */ 2657 if (guard.gc->get_direction && 2658 guard.gc->get_direction(guard.gc, gpio_chip_hwgpio(desc))) { 2659 gpiod_warn(desc, 2660 "%s: missing direction_output() operation\n", 2661 __func__); 2662 return -EIO; 2663 } 2664 /* 2665 * If we can't actively set the direction, we are some 2666 * output-only chip, so just drive the output as desired. 2667 */ 2668 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val); 2669 } 2670 2671 if (!ret) 2672 set_bit(FLAG_IS_OUT, &desc->flags); 2673 trace_gpio_value(desc_to_gpio(desc), 0, val); 2674 trace_gpio_direction(desc_to_gpio(desc), 0, ret); 2675 return ret; 2676 } 2677 2678 /** 2679 * gpiod_direction_output_raw - set the GPIO direction to output 2680 * @desc: GPIO to set to output 2681 * @value: initial output value of the GPIO 2682 * 2683 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can 2684 * be called safely on it. The initial value of the output must be specified 2685 * as raw value on the physical line without regard for the ACTIVE_LOW status. 2686 * 2687 * Return 0 in case of success, else an error code. 2688 */ 2689 int gpiod_direction_output_raw(struct gpio_desc *desc, int value) 2690 { 2691 VALIDATE_DESC(desc); 2692 return gpiod_direction_output_raw_commit(desc, value); 2693 } 2694 EXPORT_SYMBOL_GPL(gpiod_direction_output_raw); 2695 2696 /** 2697 * gpiod_direction_output - set the GPIO direction to output 2698 * @desc: GPIO to set to output 2699 * @value: initial output value of the GPIO 2700 * 2701 * Set the direction of the passed GPIO to output, such as gpiod_set_value() can 2702 * be called safely on it. The initial value of the output must be specified 2703 * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 2704 * account. 2705 * 2706 * Return 0 in case of success, else an error code. 2707 */ 2708 int gpiod_direction_output(struct gpio_desc *desc, int value) 2709 { 2710 unsigned long flags; 2711 int ret; 2712 2713 VALIDATE_DESC(desc); 2714 2715 flags = READ_ONCE(desc->flags); 2716 2717 if (test_bit(FLAG_ACTIVE_LOW, &flags)) 2718 value = !value; 2719 else 2720 value = !!value; 2721 2722 /* GPIOs used for enabled IRQs shall not be set as output */ 2723 if (test_bit(FLAG_USED_AS_IRQ, &flags) && 2724 test_bit(FLAG_IRQ_IS_ENABLED, &flags)) { 2725 gpiod_err(desc, 2726 "%s: tried to set a GPIO tied to an IRQ as output\n", 2727 __func__); 2728 return -EIO; 2729 } 2730 2731 if (test_bit(FLAG_OPEN_DRAIN, &flags)) { 2732 /* First see if we can enable open drain in hardware */ 2733 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_DRAIN); 2734 if (!ret) 2735 goto set_output_value; 2736 /* Emulate open drain by not actively driving the line high */ 2737 if (value) { 2738 ret = gpiod_direction_input(desc); 2739 goto set_output_flag; 2740 } 2741 } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) { 2742 ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE); 2743 if (!ret) 2744 goto set_output_value; 2745 /* Emulate open source by not actively driving the line low */ 2746 if (!value) { 2747 ret = gpiod_direction_input(desc); 2748 goto set_output_flag; 2749 } 2750 } else { 2751 gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL); 2752 } 2753 2754 set_output_value: 2755 ret = gpio_set_bias(desc); 2756 if (ret) 2757 return ret; 2758 return gpiod_direction_output_raw_commit(desc, value); 2759 2760 set_output_flag: 2761 /* 2762 * When emulating open-source or open-drain functionalities by not 2763 * actively driving the line (setting mode to input) we still need to 2764 * set the IS_OUT flag or otherwise we won't be able to set the line 2765 * value anymore. 2766 */ 2767 if (ret == 0) 2768 set_bit(FLAG_IS_OUT, &desc->flags); 2769 return ret; 2770 } 2771 EXPORT_SYMBOL_GPL(gpiod_direction_output); 2772 2773 /** 2774 * gpiod_enable_hw_timestamp_ns - Enable hardware timestamp in nanoseconds. 2775 * 2776 * @desc: GPIO to enable. 2777 * @flags: Flags related to GPIO edge. 2778 * 2779 * Return 0 in case of success, else negative error code. 2780 */ 2781 int gpiod_enable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags) 2782 { 2783 int ret = 0; 2784 2785 VALIDATE_DESC(desc); 2786 2787 CLASS(gpio_chip_guard, guard)(desc); 2788 if (!guard.gc) 2789 return -ENODEV; 2790 2791 if (!guard.gc->en_hw_timestamp) { 2792 gpiod_warn(desc, "%s: hw ts not supported\n", __func__); 2793 return -ENOTSUPP; 2794 } 2795 2796 ret = guard.gc->en_hw_timestamp(guard.gc, 2797 gpio_chip_hwgpio(desc), flags); 2798 if (ret) 2799 gpiod_warn(desc, "%s: hw ts request failed\n", __func__); 2800 2801 return ret; 2802 } 2803 EXPORT_SYMBOL_GPL(gpiod_enable_hw_timestamp_ns); 2804 2805 /** 2806 * gpiod_disable_hw_timestamp_ns - Disable hardware timestamp. 2807 * 2808 * @desc: GPIO to disable. 2809 * @flags: Flags related to GPIO edge, same value as used during enable call. 2810 * 2811 * Return 0 in case of success, else negative error code. 2812 */ 2813 int gpiod_disable_hw_timestamp_ns(struct gpio_desc *desc, unsigned long flags) 2814 { 2815 int ret = 0; 2816 2817 VALIDATE_DESC(desc); 2818 2819 CLASS(gpio_chip_guard, guard)(desc); 2820 if (!guard.gc) 2821 return -ENODEV; 2822 2823 if (!guard.gc->dis_hw_timestamp) { 2824 gpiod_warn(desc, "%s: hw ts not supported\n", __func__); 2825 return -ENOTSUPP; 2826 } 2827 2828 ret = guard.gc->dis_hw_timestamp(guard.gc, gpio_chip_hwgpio(desc), 2829 flags); 2830 if (ret) 2831 gpiod_warn(desc, "%s: hw ts release failed\n", __func__); 2832 2833 return ret; 2834 } 2835 EXPORT_SYMBOL_GPL(gpiod_disable_hw_timestamp_ns); 2836 2837 /** 2838 * gpiod_set_config - sets @config for a GPIO 2839 * @desc: descriptor of the GPIO for which to set the configuration 2840 * @config: Same packed config format as generic pinconf 2841 * 2842 * Returns: 2843 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the 2844 * configuration. 2845 */ 2846 int gpiod_set_config(struct gpio_desc *desc, unsigned long config) 2847 { 2848 VALIDATE_DESC(desc); 2849 2850 CLASS(gpio_chip_guard, guard)(desc); 2851 if (!guard.gc) 2852 return -ENODEV; 2853 2854 return gpio_do_set_config(guard.gc, gpio_chip_hwgpio(desc), config); 2855 } 2856 EXPORT_SYMBOL_GPL(gpiod_set_config); 2857 2858 /** 2859 * gpiod_set_debounce - sets @debounce time for a GPIO 2860 * @desc: descriptor of the GPIO for which to set debounce time 2861 * @debounce: debounce time in microseconds 2862 * 2863 * Returns: 2864 * 0 on success, %-ENOTSUPP if the controller doesn't support setting the 2865 * debounce time. 2866 */ 2867 int gpiod_set_debounce(struct gpio_desc *desc, unsigned int debounce) 2868 { 2869 unsigned long config; 2870 2871 config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce); 2872 return gpiod_set_config(desc, config); 2873 } 2874 EXPORT_SYMBOL_GPL(gpiod_set_debounce); 2875 2876 /** 2877 * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset 2878 * @desc: descriptor of the GPIO for which to configure persistence 2879 * @transitory: True to lose state on suspend or reset, false for persistence 2880 * 2881 * Returns: 2882 * 0 on success, otherwise a negative error code. 2883 */ 2884 int gpiod_set_transitory(struct gpio_desc *desc, bool transitory) 2885 { 2886 VALIDATE_DESC(desc); 2887 /* 2888 * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for 2889 * persistence state. 2890 */ 2891 assign_bit(FLAG_TRANSITORY, &desc->flags, transitory); 2892 2893 /* If the driver supports it, set the persistence state now */ 2894 return gpio_set_config_with_argument_optional(desc, 2895 PIN_CONFIG_PERSIST_STATE, 2896 !transitory); 2897 } 2898 2899 /** 2900 * gpiod_is_active_low - test whether a GPIO is active-low or not 2901 * @desc: the gpio descriptor to test 2902 * 2903 * Returns 1 if the GPIO is active-low, 0 otherwise. 2904 */ 2905 int gpiod_is_active_low(const struct gpio_desc *desc) 2906 { 2907 VALIDATE_DESC(desc); 2908 return test_bit(FLAG_ACTIVE_LOW, &desc->flags); 2909 } 2910 EXPORT_SYMBOL_GPL(gpiod_is_active_low); 2911 2912 /** 2913 * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not 2914 * @desc: the gpio descriptor to change 2915 */ 2916 void gpiod_toggle_active_low(struct gpio_desc *desc) 2917 { 2918 VALIDATE_DESC_VOID(desc); 2919 change_bit(FLAG_ACTIVE_LOW, &desc->flags); 2920 } 2921 EXPORT_SYMBOL_GPL(gpiod_toggle_active_low); 2922 2923 static int gpio_chip_get_value(struct gpio_chip *gc, const struct gpio_desc *desc) 2924 { 2925 return gc->get ? gc->get(gc, gpio_chip_hwgpio(desc)) : -EIO; 2926 } 2927 2928 /* I/O calls are only valid after configuration completed; the relevant 2929 * "is this a valid GPIO" error checks should already have been done. 2930 * 2931 * "Get" operations are often inlinable as reading a pin value register, 2932 * and masking the relevant bit in that register. 2933 * 2934 * When "set" operations are inlinable, they involve writing that mask to 2935 * one register to set a low value, or a different register to set it high. 2936 * Otherwise locking is needed, so there may be little value to inlining. 2937 * 2938 *------------------------------------------------------------------------ 2939 * 2940 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers 2941 * have requested the GPIO. That can include implicit requesting by 2942 * a direction setting call. Marking a gpio as requested locks its chip 2943 * in memory, guaranteeing that these table lookups need no more locking 2944 * and that gpiochip_remove() will fail. 2945 * 2946 * REVISIT when debugging, consider adding some instrumentation to ensure 2947 * that the GPIO was actually requested. 2948 */ 2949 2950 static int gpiod_get_raw_value_commit(const struct gpio_desc *desc) 2951 { 2952 struct gpio_device *gdev; 2953 struct gpio_chip *gc; 2954 int value; 2955 2956 /* FIXME Unable to use gpio_chip_guard due to const desc. */ 2957 gdev = desc->gdev; 2958 2959 guard(srcu)(&gdev->srcu); 2960 2961 gc = srcu_dereference(gdev->chip, &gdev->srcu); 2962 if (!gc) 2963 return -ENODEV; 2964 2965 value = gpio_chip_get_value(gc, desc); 2966 value = value < 0 ? value : !!value; 2967 trace_gpio_value(desc_to_gpio(desc), 1, value); 2968 return value; 2969 } 2970 2971 static int gpio_chip_get_multiple(struct gpio_chip *gc, 2972 unsigned long *mask, unsigned long *bits) 2973 { 2974 if (gc->get_multiple) 2975 return gc->get_multiple(gc, mask, bits); 2976 if (gc->get) { 2977 int i, value; 2978 2979 for_each_set_bit(i, mask, gc->ngpio) { 2980 value = gc->get(gc, i); 2981 if (value < 0) 2982 return value; 2983 __assign_bit(i, bits, value); 2984 } 2985 return 0; 2986 } 2987 return -EIO; 2988 } 2989 2990 /* The 'other' chip must be protected with its GPIO device's SRCU. */ 2991 static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc) 2992 { 2993 guard(srcu)(&gdev->srcu); 2994 2995 return gc == srcu_dereference(gdev->chip, &gdev->srcu); 2996 } 2997 2998 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 2999 unsigned int array_size, 3000 struct gpio_desc **desc_array, 3001 struct gpio_array *array_info, 3002 unsigned long *value_bitmap) 3003 { 3004 int ret, i = 0; 3005 3006 /* 3007 * Validate array_info against desc_array and its size. 3008 * It should immediately follow desc_array if both 3009 * have been obtained from the same gpiod_get_array() call. 3010 */ 3011 if (array_info && array_info->desc == desc_array && 3012 array_size <= array_info->size && 3013 (void *)array_info == desc_array + array_info->size) { 3014 if (!can_sleep) 3015 WARN_ON(array_info->chip->can_sleep); 3016 3017 ret = gpio_chip_get_multiple(array_info->chip, 3018 array_info->get_mask, 3019 value_bitmap); 3020 if (ret) 3021 return ret; 3022 3023 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 3024 bitmap_xor(value_bitmap, value_bitmap, 3025 array_info->invert_mask, array_size); 3026 3027 i = find_first_zero_bit(array_info->get_mask, array_size); 3028 if (i == array_size) 3029 return 0; 3030 } else { 3031 array_info = NULL; 3032 } 3033 3034 while (i < array_size) { 3035 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); 3036 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); 3037 unsigned long *mask, *bits; 3038 int first, j; 3039 3040 CLASS(gpio_chip_guard, guard)(desc_array[i]); 3041 if (!guard.gc) 3042 return -ENODEV; 3043 3044 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { 3045 mask = fastpath_mask; 3046 bits = fastpath_bits; 3047 } else { 3048 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; 3049 3050 mask = bitmap_alloc(guard.gc->ngpio, flags); 3051 if (!mask) 3052 return -ENOMEM; 3053 3054 bits = bitmap_alloc(guard.gc->ngpio, flags); 3055 if (!bits) { 3056 bitmap_free(mask); 3057 return -ENOMEM; 3058 } 3059 } 3060 3061 bitmap_zero(mask, guard.gc->ngpio); 3062 3063 if (!can_sleep) 3064 WARN_ON(guard.gc->can_sleep); 3065 3066 /* collect all inputs belonging to the same chip */ 3067 first = i; 3068 do { 3069 const struct gpio_desc *desc = desc_array[i]; 3070 int hwgpio = gpio_chip_hwgpio(desc); 3071 3072 __set_bit(hwgpio, mask); 3073 i++; 3074 3075 if (array_info) 3076 i = find_next_zero_bit(array_info->get_mask, 3077 array_size, i); 3078 } while ((i < array_size) && 3079 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); 3080 3081 ret = gpio_chip_get_multiple(guard.gc, mask, bits); 3082 if (ret) { 3083 if (mask != fastpath_mask) 3084 bitmap_free(mask); 3085 if (bits != fastpath_bits) 3086 bitmap_free(bits); 3087 return ret; 3088 } 3089 3090 for (j = first; j < i; ) { 3091 const struct gpio_desc *desc = desc_array[j]; 3092 int hwgpio = gpio_chip_hwgpio(desc); 3093 int value = test_bit(hwgpio, bits); 3094 3095 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3096 value = !value; 3097 __assign_bit(j, value_bitmap, value); 3098 trace_gpio_value(desc_to_gpio(desc), 1, value); 3099 j++; 3100 3101 if (array_info) 3102 j = find_next_zero_bit(array_info->get_mask, i, 3103 j); 3104 } 3105 3106 if (mask != fastpath_mask) 3107 bitmap_free(mask); 3108 if (bits != fastpath_bits) 3109 bitmap_free(bits); 3110 } 3111 return 0; 3112 } 3113 3114 /** 3115 * gpiod_get_raw_value() - return a gpio's raw value 3116 * @desc: gpio whose value will be returned 3117 * 3118 * Return the GPIO's raw value, i.e. the value of the physical line disregarding 3119 * its ACTIVE_LOW status, or negative errno on failure. 3120 * 3121 * This function can be called from contexts where we cannot sleep, and will 3122 * complain if the GPIO chip functions potentially sleep. 3123 */ 3124 int gpiod_get_raw_value(const struct gpio_desc *desc) 3125 { 3126 VALIDATE_DESC(desc); 3127 /* Should be using gpiod_get_raw_value_cansleep() */ 3128 WARN_ON(desc->gdev->can_sleep); 3129 return gpiod_get_raw_value_commit(desc); 3130 } 3131 EXPORT_SYMBOL_GPL(gpiod_get_raw_value); 3132 3133 /** 3134 * gpiod_get_value() - return a gpio's value 3135 * @desc: gpio whose value will be returned 3136 * 3137 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into 3138 * account, or negative errno on failure. 3139 * 3140 * This function can be called from contexts where we cannot sleep, and will 3141 * complain if the GPIO chip functions potentially sleep. 3142 */ 3143 int gpiod_get_value(const struct gpio_desc *desc) 3144 { 3145 int value; 3146 3147 VALIDATE_DESC(desc); 3148 /* Should be using gpiod_get_value_cansleep() */ 3149 WARN_ON(desc->gdev->can_sleep); 3150 3151 value = gpiod_get_raw_value_commit(desc); 3152 if (value < 0) 3153 return value; 3154 3155 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3156 value = !value; 3157 3158 return value; 3159 } 3160 EXPORT_SYMBOL_GPL(gpiod_get_value); 3161 3162 /** 3163 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs 3164 * @array_size: number of elements in the descriptor array / value bitmap 3165 * @desc_array: array of GPIO descriptors whose values will be read 3166 * @array_info: information on applicability of fast bitmap processing path 3167 * @value_bitmap: bitmap to store the read values 3168 * 3169 * Read the raw values of the GPIOs, i.e. the values of the physical lines 3170 * without regard for their ACTIVE_LOW status. Return 0 in case of success, 3171 * else an error code. 3172 * 3173 * This function can be called from contexts where we cannot sleep, 3174 * and it will complain if the GPIO chip functions potentially sleep. 3175 */ 3176 int gpiod_get_raw_array_value(unsigned int array_size, 3177 struct gpio_desc **desc_array, 3178 struct gpio_array *array_info, 3179 unsigned long *value_bitmap) 3180 { 3181 if (!desc_array) 3182 return -EINVAL; 3183 return gpiod_get_array_value_complex(true, false, array_size, 3184 desc_array, array_info, 3185 value_bitmap); 3186 } 3187 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); 3188 3189 /** 3190 * gpiod_get_array_value() - read values from an array of GPIOs 3191 * @array_size: number of elements in the descriptor array / value bitmap 3192 * @desc_array: array of GPIO descriptors whose values will be read 3193 * @array_info: information on applicability of fast bitmap processing path 3194 * @value_bitmap: bitmap to store the read values 3195 * 3196 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3197 * into account. Return 0 in case of success, else an error code. 3198 * 3199 * This function can be called from contexts where we cannot sleep, 3200 * and it will complain if the GPIO chip functions potentially sleep. 3201 */ 3202 int gpiod_get_array_value(unsigned int array_size, 3203 struct gpio_desc **desc_array, 3204 struct gpio_array *array_info, 3205 unsigned long *value_bitmap) 3206 { 3207 if (!desc_array) 3208 return -EINVAL; 3209 return gpiod_get_array_value_complex(false, false, array_size, 3210 desc_array, array_info, 3211 value_bitmap); 3212 } 3213 EXPORT_SYMBOL_GPL(gpiod_get_array_value); 3214 3215 /* 3216 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. 3217 * @desc: gpio descriptor whose state need to be set. 3218 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 3219 */ 3220 static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) 3221 { 3222 int ret = 0, offset = gpio_chip_hwgpio(desc); 3223 3224 CLASS(gpio_chip_guard, guard)(desc); 3225 if (!guard.gc) 3226 return; 3227 3228 if (value) { 3229 ret = guard.gc->direction_input(guard.gc, offset); 3230 } else { 3231 ret = guard.gc->direction_output(guard.gc, offset, 0); 3232 if (!ret) 3233 set_bit(FLAG_IS_OUT, &desc->flags); 3234 } 3235 trace_gpio_direction(desc_to_gpio(desc), value, ret); 3236 if (ret < 0) 3237 gpiod_err(desc, 3238 "%s: Error in set_value for open drain err %d\n", 3239 __func__, ret); 3240 } 3241 3242 /* 3243 * _gpio_set_open_source_value() - Set the open source gpio's value. 3244 * @desc: gpio descriptor whose state need to be set. 3245 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 3246 */ 3247 static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) 3248 { 3249 int ret = 0, offset = gpio_chip_hwgpio(desc); 3250 3251 CLASS(gpio_chip_guard, guard)(desc); 3252 if (!guard.gc) 3253 return; 3254 3255 if (value) { 3256 ret = guard.gc->direction_output(guard.gc, offset, 1); 3257 if (!ret) 3258 set_bit(FLAG_IS_OUT, &desc->flags); 3259 } else { 3260 ret = guard.gc->direction_input(guard.gc, offset); 3261 } 3262 trace_gpio_direction(desc_to_gpio(desc), !value, ret); 3263 if (ret < 0) 3264 gpiod_err(desc, 3265 "%s: Error in set_value for open source err %d\n", 3266 __func__, ret); 3267 } 3268 3269 static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) 3270 { 3271 CLASS(gpio_chip_guard, guard)(desc); 3272 if (!guard.gc) 3273 return; 3274 3275 trace_gpio_value(desc_to_gpio(desc), 0, value); 3276 guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), value); 3277 } 3278 3279 /* 3280 * set multiple outputs on the same chip; 3281 * use the chip's set_multiple function if available; 3282 * otherwise set the outputs sequentially; 3283 * @chip: the GPIO chip we operate on 3284 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word 3285 * defines which outputs are to be changed 3286 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word 3287 * defines the values the outputs specified by mask are to be set to 3288 */ 3289 static void gpio_chip_set_multiple(struct gpio_chip *gc, 3290 unsigned long *mask, unsigned long *bits) 3291 { 3292 if (gc->set_multiple) { 3293 gc->set_multiple(gc, mask, bits); 3294 } else { 3295 unsigned int i; 3296 3297 /* set outputs if the corresponding mask bit is set */ 3298 for_each_set_bit(i, mask, gc->ngpio) 3299 gc->set(gc, i, test_bit(i, bits)); 3300 } 3301 } 3302 3303 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 3304 unsigned int array_size, 3305 struct gpio_desc **desc_array, 3306 struct gpio_array *array_info, 3307 unsigned long *value_bitmap) 3308 { 3309 int i = 0; 3310 3311 /* 3312 * Validate array_info against desc_array and its size. 3313 * It should immediately follow desc_array if both 3314 * have been obtained from the same gpiod_get_array() call. 3315 */ 3316 if (array_info && array_info->desc == desc_array && 3317 array_size <= array_info->size && 3318 (void *)array_info == desc_array + array_info->size) { 3319 if (!can_sleep) 3320 WARN_ON(array_info->chip->can_sleep); 3321 3322 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 3323 bitmap_xor(value_bitmap, value_bitmap, 3324 array_info->invert_mask, array_size); 3325 3326 gpio_chip_set_multiple(array_info->chip, array_info->set_mask, 3327 value_bitmap); 3328 3329 i = find_first_zero_bit(array_info->set_mask, array_size); 3330 if (i == array_size) 3331 return 0; 3332 } else { 3333 array_info = NULL; 3334 } 3335 3336 while (i < array_size) { 3337 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); 3338 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); 3339 unsigned long *mask, *bits; 3340 int count = 0; 3341 3342 CLASS(gpio_chip_guard, guard)(desc_array[i]); 3343 if (!guard.gc) 3344 return -ENODEV; 3345 3346 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { 3347 mask = fastpath_mask; 3348 bits = fastpath_bits; 3349 } else { 3350 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; 3351 3352 mask = bitmap_alloc(guard.gc->ngpio, flags); 3353 if (!mask) 3354 return -ENOMEM; 3355 3356 bits = bitmap_alloc(guard.gc->ngpio, flags); 3357 if (!bits) { 3358 bitmap_free(mask); 3359 return -ENOMEM; 3360 } 3361 } 3362 3363 bitmap_zero(mask, guard.gc->ngpio); 3364 3365 if (!can_sleep) 3366 WARN_ON(guard.gc->can_sleep); 3367 3368 do { 3369 struct gpio_desc *desc = desc_array[i]; 3370 int hwgpio = gpio_chip_hwgpio(desc); 3371 int value = test_bit(i, value_bitmap); 3372 3373 /* 3374 * Pins applicable for fast input but not for 3375 * fast output processing may have been already 3376 * inverted inside the fast path, skip them. 3377 */ 3378 if (!raw && !(array_info && 3379 test_bit(i, array_info->invert_mask)) && 3380 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3381 value = !value; 3382 trace_gpio_value(desc_to_gpio(desc), 0, value); 3383 /* 3384 * collect all normal outputs belonging to the same chip 3385 * open drain and open source outputs are set individually 3386 */ 3387 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { 3388 gpio_set_open_drain_value_commit(desc, value); 3389 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { 3390 gpio_set_open_source_value_commit(desc, value); 3391 } else { 3392 __set_bit(hwgpio, mask); 3393 __assign_bit(hwgpio, bits, value); 3394 count++; 3395 } 3396 i++; 3397 3398 if (array_info) 3399 i = find_next_zero_bit(array_info->set_mask, 3400 array_size, i); 3401 } while ((i < array_size) && 3402 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); 3403 /* push collected bits to outputs */ 3404 if (count != 0) 3405 gpio_chip_set_multiple(guard.gc, mask, bits); 3406 3407 if (mask != fastpath_mask) 3408 bitmap_free(mask); 3409 if (bits != fastpath_bits) 3410 bitmap_free(bits); 3411 } 3412 return 0; 3413 } 3414 3415 /** 3416 * gpiod_set_raw_value() - assign a gpio's raw value 3417 * @desc: gpio whose value will be assigned 3418 * @value: value to assign 3419 * 3420 * Set the raw value of the GPIO, i.e. the value of its physical line without 3421 * regard for its ACTIVE_LOW status. 3422 * 3423 * This function can be called from contexts where we cannot sleep, and will 3424 * complain if the GPIO chip functions potentially sleep. 3425 */ 3426 void gpiod_set_raw_value(struct gpio_desc *desc, int value) 3427 { 3428 VALIDATE_DESC_VOID(desc); 3429 /* Should be using gpiod_set_raw_value_cansleep() */ 3430 WARN_ON(desc->gdev->can_sleep); 3431 gpiod_set_raw_value_commit(desc, value); 3432 } 3433 EXPORT_SYMBOL_GPL(gpiod_set_raw_value); 3434 3435 /** 3436 * gpiod_set_value_nocheck() - set a GPIO line value without checking 3437 * @desc: the descriptor to set the value on 3438 * @value: value to set 3439 * 3440 * This sets the value of a GPIO line backing a descriptor, applying 3441 * different semantic quirks like active low and open drain/source 3442 * handling. 3443 */ 3444 static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value) 3445 { 3446 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3447 value = !value; 3448 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 3449 gpio_set_open_drain_value_commit(desc, value); 3450 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 3451 gpio_set_open_source_value_commit(desc, value); 3452 else 3453 gpiod_set_raw_value_commit(desc, value); 3454 } 3455 3456 /** 3457 * gpiod_set_value() - assign a gpio's value 3458 * @desc: gpio whose value will be assigned 3459 * @value: value to assign 3460 * 3461 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, 3462 * OPEN_DRAIN and OPEN_SOURCE flags into account. 3463 * 3464 * This function can be called from contexts where we cannot sleep, and will 3465 * complain if the GPIO chip functions potentially sleep. 3466 */ 3467 void gpiod_set_value(struct gpio_desc *desc, int value) 3468 { 3469 VALIDATE_DESC_VOID(desc); 3470 /* Should be using gpiod_set_value_cansleep() */ 3471 WARN_ON(desc->gdev->can_sleep); 3472 gpiod_set_value_nocheck(desc, value); 3473 } 3474 EXPORT_SYMBOL_GPL(gpiod_set_value); 3475 3476 /** 3477 * gpiod_set_raw_array_value() - assign values to an array of GPIOs 3478 * @array_size: number of elements in the descriptor array / value bitmap 3479 * @desc_array: array of GPIO descriptors whose values will be assigned 3480 * @array_info: information on applicability of fast bitmap processing path 3481 * @value_bitmap: bitmap of values to assign 3482 * 3483 * Set the raw values of the GPIOs, i.e. the values of the physical lines 3484 * without regard for their ACTIVE_LOW status. 3485 * 3486 * This function can be called from contexts where we cannot sleep, and will 3487 * complain if the GPIO chip functions potentially sleep. 3488 */ 3489 int gpiod_set_raw_array_value(unsigned int array_size, 3490 struct gpio_desc **desc_array, 3491 struct gpio_array *array_info, 3492 unsigned long *value_bitmap) 3493 { 3494 if (!desc_array) 3495 return -EINVAL; 3496 return gpiod_set_array_value_complex(true, false, array_size, 3497 desc_array, array_info, value_bitmap); 3498 } 3499 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); 3500 3501 /** 3502 * gpiod_set_array_value() - assign values to an array of GPIOs 3503 * @array_size: number of elements in the descriptor array / value bitmap 3504 * @desc_array: array of GPIO descriptors whose values will be assigned 3505 * @array_info: information on applicability of fast bitmap processing path 3506 * @value_bitmap: bitmap of values to assign 3507 * 3508 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3509 * into account. 3510 * 3511 * This function can be called from contexts where we cannot sleep, and will 3512 * complain if the GPIO chip functions potentially sleep. 3513 */ 3514 int gpiod_set_array_value(unsigned int array_size, 3515 struct gpio_desc **desc_array, 3516 struct gpio_array *array_info, 3517 unsigned long *value_bitmap) 3518 { 3519 if (!desc_array) 3520 return -EINVAL; 3521 return gpiod_set_array_value_complex(false, false, array_size, 3522 desc_array, array_info, 3523 value_bitmap); 3524 } 3525 EXPORT_SYMBOL_GPL(gpiod_set_array_value); 3526 3527 /** 3528 * gpiod_cansleep() - report whether gpio value access may sleep 3529 * @desc: gpio to check 3530 * 3531 */ 3532 int gpiod_cansleep(const struct gpio_desc *desc) 3533 { 3534 VALIDATE_DESC(desc); 3535 return desc->gdev->can_sleep; 3536 } 3537 EXPORT_SYMBOL_GPL(gpiod_cansleep); 3538 3539 /** 3540 * gpiod_set_consumer_name() - set the consumer name for the descriptor 3541 * @desc: gpio to set the consumer name on 3542 * @name: the new consumer name 3543 */ 3544 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) 3545 { 3546 VALIDATE_DESC(desc); 3547 3548 return desc_set_label(desc, name); 3549 } 3550 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); 3551 3552 /** 3553 * gpiod_to_irq() - return the IRQ corresponding to a GPIO 3554 * @desc: gpio whose IRQ will be returned (already requested) 3555 * 3556 * Return the IRQ corresponding to the passed GPIO, or an error code in case of 3557 * error. 3558 */ 3559 int gpiod_to_irq(const struct gpio_desc *desc) 3560 { 3561 struct gpio_device *gdev; 3562 struct gpio_chip *gc; 3563 int offset; 3564 3565 /* 3566 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics 3567 * requires this function to not return zero on an invalid descriptor 3568 * but rather a negative error number. 3569 */ 3570 if (!desc || IS_ERR(desc)) 3571 return -EINVAL; 3572 3573 gdev = desc->gdev; 3574 /* FIXME Cannot use gpio_chip_guard due to const desc. */ 3575 guard(srcu)(&gdev->srcu); 3576 gc = srcu_dereference(gdev->chip, &gdev->srcu); 3577 if (!gc) 3578 return -ENODEV; 3579 3580 offset = gpio_chip_hwgpio(desc); 3581 if (gc->to_irq) { 3582 int retirq = gc->to_irq(gc, offset); 3583 3584 /* Zero means NO_IRQ */ 3585 if (!retirq) 3586 return -ENXIO; 3587 3588 return retirq; 3589 } 3590 #ifdef CONFIG_GPIOLIB_IRQCHIP 3591 if (gc->irq.chip) { 3592 /* 3593 * Avoid race condition with other code, which tries to lookup 3594 * an IRQ before the irqchip has been properly registered, 3595 * i.e. while gpiochip is still being brought up. 3596 */ 3597 return -EPROBE_DEFER; 3598 } 3599 #endif 3600 return -ENXIO; 3601 } 3602 EXPORT_SYMBOL_GPL(gpiod_to_irq); 3603 3604 /** 3605 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ 3606 * @gc: the chip the GPIO to lock belongs to 3607 * @offset: the offset of the GPIO to lock as IRQ 3608 * 3609 * This is used directly by GPIO drivers that want to lock down 3610 * a certain GPIO line to be used for IRQs. 3611 */ 3612 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) 3613 { 3614 struct gpio_desc *desc; 3615 3616 desc = gpiochip_get_desc(gc, offset); 3617 if (IS_ERR(desc)) 3618 return PTR_ERR(desc); 3619 3620 /* 3621 * If it's fast: flush the direction setting if something changed 3622 * behind our back 3623 */ 3624 if (!gc->can_sleep && gc->get_direction) { 3625 int dir = gpiod_get_direction(desc); 3626 3627 if (dir < 0) { 3628 chip_err(gc, "%s: cannot get GPIO direction\n", 3629 __func__); 3630 return dir; 3631 } 3632 } 3633 3634 /* To be valid for IRQ the line needs to be input or open drain */ 3635 if (test_bit(FLAG_IS_OUT, &desc->flags) && 3636 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 3637 chip_err(gc, 3638 "%s: tried to flag a GPIO set as output for IRQ\n", 3639 __func__); 3640 return -EIO; 3641 } 3642 3643 set_bit(FLAG_USED_AS_IRQ, &desc->flags); 3644 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3645 3646 return 0; 3647 } 3648 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); 3649 3650 /** 3651 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ 3652 * @gc: the chip the GPIO to lock belongs to 3653 * @offset: the offset of the GPIO to lock as IRQ 3654 * 3655 * This is used directly by GPIO drivers that want to indicate 3656 * that a certain GPIO is no longer used exclusively for IRQ. 3657 */ 3658 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset) 3659 { 3660 struct gpio_desc *desc; 3661 3662 desc = gpiochip_get_desc(gc, offset); 3663 if (IS_ERR(desc)) 3664 return; 3665 3666 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); 3667 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3668 } 3669 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); 3670 3671 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset) 3672 { 3673 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 3674 3675 if (!IS_ERR(desc) && 3676 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) 3677 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3678 } 3679 EXPORT_SYMBOL_GPL(gpiochip_disable_irq); 3680 3681 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset) 3682 { 3683 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 3684 3685 if (!IS_ERR(desc) && 3686 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { 3687 /* 3688 * We must not be output when using IRQ UNLESS we are 3689 * open drain. 3690 */ 3691 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && 3692 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); 3693 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 3694 } 3695 } 3696 EXPORT_SYMBOL_GPL(gpiochip_enable_irq); 3697 3698 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset) 3699 { 3700 if (offset >= gc->ngpio) 3701 return false; 3702 3703 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); 3704 } 3705 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); 3706 3707 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset) 3708 { 3709 int ret; 3710 3711 if (!try_module_get(gc->gpiodev->owner)) 3712 return -ENODEV; 3713 3714 ret = gpiochip_lock_as_irq(gc, offset); 3715 if (ret) { 3716 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); 3717 module_put(gc->gpiodev->owner); 3718 return ret; 3719 } 3720 return 0; 3721 } 3722 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); 3723 3724 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset) 3725 { 3726 gpiochip_unlock_as_irq(gc, offset); 3727 module_put(gc->gpiodev->owner); 3728 } 3729 EXPORT_SYMBOL_GPL(gpiochip_relres_irq); 3730 3731 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset) 3732 { 3733 if (offset >= gc->ngpio) 3734 return false; 3735 3736 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); 3737 } 3738 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); 3739 3740 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset) 3741 { 3742 if (offset >= gc->ngpio) 3743 return false; 3744 3745 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); 3746 } 3747 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); 3748 3749 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset) 3750 { 3751 if (offset >= gc->ngpio) 3752 return false; 3753 3754 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); 3755 } 3756 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); 3757 3758 /** 3759 * gpiod_get_raw_value_cansleep() - return a gpio's raw value 3760 * @desc: gpio whose value will be returned 3761 * 3762 * Return the GPIO's raw value, i.e. the value of the physical line disregarding 3763 * its ACTIVE_LOW status, or negative errno on failure. 3764 * 3765 * This function is to be called from contexts that can sleep. 3766 */ 3767 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 3768 { 3769 might_sleep(); 3770 VALIDATE_DESC(desc); 3771 return gpiod_get_raw_value_commit(desc); 3772 } 3773 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); 3774 3775 /** 3776 * gpiod_get_value_cansleep() - return a gpio's value 3777 * @desc: gpio whose value will be returned 3778 * 3779 * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into 3780 * account, or negative errno on failure. 3781 * 3782 * This function is to be called from contexts that can sleep. 3783 */ 3784 int gpiod_get_value_cansleep(const struct gpio_desc *desc) 3785 { 3786 int value; 3787 3788 might_sleep(); 3789 VALIDATE_DESC(desc); 3790 value = gpiod_get_raw_value_commit(desc); 3791 if (value < 0) 3792 return value; 3793 3794 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3795 value = !value; 3796 3797 return value; 3798 } 3799 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); 3800 3801 /** 3802 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs 3803 * @array_size: number of elements in the descriptor array / value bitmap 3804 * @desc_array: array of GPIO descriptors whose values will be read 3805 * @array_info: information on applicability of fast bitmap processing path 3806 * @value_bitmap: bitmap to store the read values 3807 * 3808 * Read the raw values of the GPIOs, i.e. the values of the physical lines 3809 * without regard for their ACTIVE_LOW status. Return 0 in case of success, 3810 * else an error code. 3811 * 3812 * This function is to be called from contexts that can sleep. 3813 */ 3814 int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 3815 struct gpio_desc **desc_array, 3816 struct gpio_array *array_info, 3817 unsigned long *value_bitmap) 3818 { 3819 might_sleep(); 3820 if (!desc_array) 3821 return -EINVAL; 3822 return gpiod_get_array_value_complex(true, true, array_size, 3823 desc_array, array_info, 3824 value_bitmap); 3825 } 3826 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); 3827 3828 /** 3829 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs 3830 * @array_size: number of elements in the descriptor array / value bitmap 3831 * @desc_array: array of GPIO descriptors whose values will be read 3832 * @array_info: information on applicability of fast bitmap processing path 3833 * @value_bitmap: bitmap to store the read values 3834 * 3835 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3836 * into account. Return 0 in case of success, else an error code. 3837 * 3838 * This function is to be called from contexts that can sleep. 3839 */ 3840 int gpiod_get_array_value_cansleep(unsigned int array_size, 3841 struct gpio_desc **desc_array, 3842 struct gpio_array *array_info, 3843 unsigned long *value_bitmap) 3844 { 3845 might_sleep(); 3846 if (!desc_array) 3847 return -EINVAL; 3848 return gpiod_get_array_value_complex(false, true, array_size, 3849 desc_array, array_info, 3850 value_bitmap); 3851 } 3852 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); 3853 3854 /** 3855 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value 3856 * @desc: gpio whose value will be assigned 3857 * @value: value to assign 3858 * 3859 * Set the raw value of the GPIO, i.e. the value of its physical line without 3860 * regard for its ACTIVE_LOW status. 3861 * 3862 * This function is to be called from contexts that can sleep. 3863 */ 3864 void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) 3865 { 3866 might_sleep(); 3867 VALIDATE_DESC_VOID(desc); 3868 gpiod_set_raw_value_commit(desc, value); 3869 } 3870 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); 3871 3872 /** 3873 * gpiod_set_value_cansleep() - assign a gpio's value 3874 * @desc: gpio whose value will be assigned 3875 * @value: value to assign 3876 * 3877 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 3878 * account 3879 * 3880 * This function is to be called from contexts that can sleep. 3881 */ 3882 void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 3883 { 3884 might_sleep(); 3885 VALIDATE_DESC_VOID(desc); 3886 gpiod_set_value_nocheck(desc, value); 3887 } 3888 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 3889 3890 /** 3891 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs 3892 * @array_size: number of elements in the descriptor array / value bitmap 3893 * @desc_array: array of GPIO descriptors whose values will be assigned 3894 * @array_info: information on applicability of fast bitmap processing path 3895 * @value_bitmap: bitmap of values to assign 3896 * 3897 * Set the raw values of the GPIOs, i.e. the values of the physical lines 3898 * without regard for their ACTIVE_LOW status. 3899 * 3900 * This function is to be called from contexts that can sleep. 3901 */ 3902 int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 3903 struct gpio_desc **desc_array, 3904 struct gpio_array *array_info, 3905 unsigned long *value_bitmap) 3906 { 3907 might_sleep(); 3908 if (!desc_array) 3909 return -EINVAL; 3910 return gpiod_set_array_value_complex(true, true, array_size, desc_array, 3911 array_info, value_bitmap); 3912 } 3913 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); 3914 3915 /** 3916 * gpiod_add_lookup_tables() - register GPIO device consumers 3917 * @tables: list of tables of consumers to register 3918 * @n: number of tables in the list 3919 */ 3920 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) 3921 { 3922 unsigned int i; 3923 3924 mutex_lock(&gpio_lookup_lock); 3925 3926 for (i = 0; i < n; i++) 3927 list_add_tail(&tables[i]->list, &gpio_lookup_list); 3928 3929 mutex_unlock(&gpio_lookup_lock); 3930 } 3931 3932 /** 3933 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs 3934 * @array_size: number of elements in the descriptor array / value bitmap 3935 * @desc_array: array of GPIO descriptors whose values will be assigned 3936 * @array_info: information on applicability of fast bitmap processing path 3937 * @value_bitmap: bitmap of values to assign 3938 * 3939 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3940 * into account. 3941 * 3942 * This function is to be called from contexts that can sleep. 3943 */ 3944 int gpiod_set_array_value_cansleep(unsigned int array_size, 3945 struct gpio_desc **desc_array, 3946 struct gpio_array *array_info, 3947 unsigned long *value_bitmap) 3948 { 3949 might_sleep(); 3950 if (!desc_array) 3951 return -EINVAL; 3952 return gpiod_set_array_value_complex(false, true, array_size, 3953 desc_array, array_info, 3954 value_bitmap); 3955 } 3956 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); 3957 3958 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action) 3959 { 3960 blocking_notifier_call_chain(&desc->gdev->line_state_notifier, 3961 action, desc); 3962 } 3963 3964 /** 3965 * gpiod_add_lookup_table() - register GPIO device consumers 3966 * @table: table of consumers to register 3967 */ 3968 void gpiod_add_lookup_table(struct gpiod_lookup_table *table) 3969 { 3970 gpiod_add_lookup_tables(&table, 1); 3971 } 3972 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); 3973 3974 /** 3975 * gpiod_remove_lookup_table() - unregister GPIO device consumers 3976 * @table: table of consumers to unregister 3977 */ 3978 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) 3979 { 3980 /* Nothing to remove */ 3981 if (!table) 3982 return; 3983 3984 mutex_lock(&gpio_lookup_lock); 3985 3986 list_del(&table->list); 3987 3988 mutex_unlock(&gpio_lookup_lock); 3989 } 3990 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); 3991 3992 /** 3993 * gpiod_add_hogs() - register a set of GPIO hogs from machine code 3994 * @hogs: table of gpio hog entries with a zeroed sentinel at the end 3995 */ 3996 void gpiod_add_hogs(struct gpiod_hog *hogs) 3997 { 3998 struct gpiod_hog *hog; 3999 4000 mutex_lock(&gpio_machine_hogs_mutex); 4001 4002 for (hog = &hogs[0]; hog->chip_label; hog++) { 4003 list_add_tail(&hog->list, &gpio_machine_hogs); 4004 4005 /* 4006 * The chip may have been registered earlier, so check if it 4007 * exists and, if so, try to hog the line now. 4008 */ 4009 struct gpio_device *gdev __free(gpio_device_put) = 4010 gpio_device_find_by_label(hog->chip_label); 4011 if (gdev) 4012 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog); 4013 } 4014 4015 mutex_unlock(&gpio_machine_hogs_mutex); 4016 } 4017 EXPORT_SYMBOL_GPL(gpiod_add_hogs); 4018 4019 void gpiod_remove_hogs(struct gpiod_hog *hogs) 4020 { 4021 struct gpiod_hog *hog; 4022 4023 mutex_lock(&gpio_machine_hogs_mutex); 4024 for (hog = &hogs[0]; hog->chip_label; hog++) 4025 list_del(&hog->list); 4026 mutex_unlock(&gpio_machine_hogs_mutex); 4027 } 4028 EXPORT_SYMBOL_GPL(gpiod_remove_hogs); 4029 4030 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) 4031 { 4032 const char *dev_id = dev ? dev_name(dev) : NULL; 4033 struct gpiod_lookup_table *table; 4034 4035 list_for_each_entry(table, &gpio_lookup_list, list) { 4036 if (table->dev_id && dev_id) { 4037 /* 4038 * Valid strings on both ends, must be identical to have 4039 * a match 4040 */ 4041 if (!strcmp(table->dev_id, dev_id)) 4042 return table; 4043 } else { 4044 /* 4045 * One of the pointers is NULL, so both must be to have 4046 * a match 4047 */ 4048 if (dev_id == table->dev_id) 4049 return table; 4050 } 4051 } 4052 4053 return NULL; 4054 } 4055 4056 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 4057 unsigned int idx, unsigned long *flags) 4058 { 4059 struct gpio_desc *desc = ERR_PTR(-ENOENT); 4060 struct gpiod_lookup_table *table; 4061 struct gpiod_lookup *p; 4062 struct gpio_chip *gc; 4063 4064 guard(mutex)(&gpio_lookup_lock); 4065 4066 table = gpiod_find_lookup_table(dev); 4067 if (!table) 4068 return desc; 4069 4070 for (p = &table->table[0]; p->key; p++) { 4071 /* idx must always match exactly */ 4072 if (p->idx != idx) 4073 continue; 4074 4075 /* If the lookup entry has a con_id, require exact match */ 4076 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) 4077 continue; 4078 4079 if (p->chip_hwnum == U16_MAX) { 4080 desc = gpio_name_to_desc(p->key); 4081 if (desc) { 4082 *flags = p->flags; 4083 return desc; 4084 } 4085 4086 dev_warn(dev, "cannot find GPIO line %s, deferring\n", 4087 p->key); 4088 return ERR_PTR(-EPROBE_DEFER); 4089 } 4090 4091 struct gpio_device *gdev __free(gpio_device_put) = 4092 gpio_device_find_by_label(p->key); 4093 if (!gdev) { 4094 /* 4095 * As the lookup table indicates a chip with 4096 * p->key should exist, assume it may 4097 * still appear later and let the interested 4098 * consumer be probed again or let the Deferred 4099 * Probe infrastructure handle the error. 4100 */ 4101 dev_warn(dev, "cannot find GPIO chip %s, deferring\n", 4102 p->key); 4103 return ERR_PTR(-EPROBE_DEFER); 4104 } 4105 4106 gc = gpio_device_get_chip(gdev); 4107 4108 if (gc->ngpio <= p->chip_hwnum) { 4109 dev_err(dev, 4110 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", 4111 idx, p->chip_hwnum, gc->ngpio - 1, 4112 gc->label); 4113 return ERR_PTR(-EINVAL); 4114 } 4115 4116 desc = gpio_device_get_desc(gdev, p->chip_hwnum); 4117 *flags = p->flags; 4118 4119 return desc; 4120 } 4121 4122 return desc; 4123 } 4124 4125 static int platform_gpio_count(struct device *dev, const char *con_id) 4126 { 4127 struct gpiod_lookup_table *table; 4128 struct gpiod_lookup *p; 4129 unsigned int count = 0; 4130 4131 scoped_guard(mutex, &gpio_lookup_lock) { 4132 table = gpiod_find_lookup_table(dev); 4133 if (!table) 4134 return -ENOENT; 4135 4136 for (p = &table->table[0]; p->key; p++) { 4137 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || 4138 (!con_id && !p->con_id)) 4139 count++; 4140 } 4141 } 4142 4143 if (!count) 4144 return -ENOENT; 4145 4146 return count; 4147 } 4148 4149 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode, 4150 struct device *consumer, 4151 const char *con_id, 4152 unsigned int idx, 4153 enum gpiod_flags *flags, 4154 unsigned long *lookupflags) 4155 { 4156 struct gpio_desc *desc = ERR_PTR(-ENOENT); 4157 4158 if (is_of_node(fwnode)) { 4159 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n", 4160 fwnode, con_id); 4161 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags); 4162 } else if (is_acpi_node(fwnode)) { 4163 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n", 4164 fwnode, con_id); 4165 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags); 4166 } else if (is_software_node(fwnode)) { 4167 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n", 4168 fwnode, con_id); 4169 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags); 4170 } 4171 4172 return desc; 4173 } 4174 4175 struct gpio_desc *gpiod_find_and_request(struct device *consumer, 4176 struct fwnode_handle *fwnode, 4177 const char *con_id, 4178 unsigned int idx, 4179 enum gpiod_flags flags, 4180 const char *label, 4181 bool platform_lookup_allowed) 4182 { 4183 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; 4184 /* 4185 * scoped_guard() is implemented as a for loop, meaning static 4186 * analyzers will complain about these two not being initialized. 4187 */ 4188 struct gpio_desc *desc = NULL; 4189 int ret = 0; 4190 4191 scoped_guard(srcu, &gpio_devices_srcu) { 4192 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, 4193 &flags, &lookupflags); 4194 if (gpiod_not_found(desc) && platform_lookup_allowed) { 4195 /* 4196 * Either we are not using DT or ACPI, or their lookup 4197 * did not return a result. In that case, use platform 4198 * lookup as a fallback. 4199 */ 4200 dev_dbg(consumer, 4201 "using lookup tables for GPIO lookup\n"); 4202 desc = gpiod_find(consumer, con_id, idx, &lookupflags); 4203 } 4204 4205 if (IS_ERR(desc)) { 4206 dev_dbg(consumer, "No GPIO consumer %s found\n", 4207 con_id); 4208 return desc; 4209 } 4210 4211 /* 4212 * If a connection label was passed use that, else attempt to use 4213 * the device name as label 4214 */ 4215 ret = gpiod_request(desc, label); 4216 } 4217 if (ret) { 4218 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 4219 return ERR_PTR(ret); 4220 4221 /* 4222 * This happens when there are several consumers for 4223 * the same GPIO line: we just return here without 4224 * further initialization. It is a bit of a hack. 4225 * This is necessary to support fixed regulators. 4226 * 4227 * FIXME: Make this more sane and safe. 4228 */ 4229 dev_info(consumer, 4230 "nonexclusive access to GPIO for %s\n", con_id); 4231 return desc; 4232 } 4233 4234 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); 4235 if (ret < 0) { 4236 dev_dbg(consumer, "setup of GPIO %s failed\n", con_id); 4237 gpiod_put(desc); 4238 return ERR_PTR(ret); 4239 } 4240 4241 gpiod_line_state_notify(desc, GPIOLINE_CHANGED_REQUESTED); 4242 4243 return desc; 4244 } 4245 4246 /** 4247 * fwnode_gpiod_get_index - obtain a GPIO from firmware node 4248 * @fwnode: handle of the firmware node 4249 * @con_id: function within the GPIO consumer 4250 * @index: index of the GPIO to obtain for the consumer 4251 * @flags: GPIO initialization flags 4252 * @label: label to attach to the requested GPIO 4253 * 4254 * This function can be used for drivers that get their configuration 4255 * from opaque firmware. 4256 * 4257 * The function properly finds the corresponding GPIO using whatever is the 4258 * underlying firmware interface and then makes sure that the GPIO 4259 * descriptor is requested before it is returned to the caller. 4260 * 4261 * Returns: 4262 * On successful request the GPIO pin is configured in accordance with 4263 * provided @flags. 4264 * 4265 * In case of error an ERR_PTR() is returned. 4266 */ 4267 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 4268 const char *con_id, 4269 int index, 4270 enum gpiod_flags flags, 4271 const char *label) 4272 { 4273 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false); 4274 } 4275 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); 4276 4277 /** 4278 * gpiod_count - return the number of GPIOs associated with a device / function 4279 * or -ENOENT if no GPIO has been assigned to the requested function 4280 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4281 * @con_id: function within the GPIO consumer 4282 */ 4283 int gpiod_count(struct device *dev, const char *con_id) 4284 { 4285 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 4286 int count = -ENOENT; 4287 4288 if (is_of_node(fwnode)) 4289 count = of_gpio_count(fwnode, con_id); 4290 else if (is_acpi_node(fwnode)) 4291 count = acpi_gpio_count(fwnode, con_id); 4292 else if (is_software_node(fwnode)) 4293 count = swnode_gpio_count(fwnode, con_id); 4294 4295 if (count < 0) 4296 count = platform_gpio_count(dev, con_id); 4297 4298 return count; 4299 } 4300 EXPORT_SYMBOL_GPL(gpiod_count); 4301 4302 /** 4303 * gpiod_get - obtain a GPIO for a given GPIO function 4304 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4305 * @con_id: function within the GPIO consumer 4306 * @flags: optional GPIO initialization flags 4307 * 4308 * Return the GPIO descriptor corresponding to the function con_id of device 4309 * dev, -ENOENT if no GPIO has been assigned to the requested function, or 4310 * another IS_ERR() code if an error occurred while trying to acquire the GPIO. 4311 */ 4312 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, 4313 enum gpiod_flags flags) 4314 { 4315 return gpiod_get_index(dev, con_id, 0, flags); 4316 } 4317 EXPORT_SYMBOL_GPL(gpiod_get); 4318 4319 /** 4320 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function 4321 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4322 * @con_id: function within the GPIO consumer 4323 * @flags: optional GPIO initialization flags 4324 * 4325 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to 4326 * the requested function it will return NULL. This is convenient for drivers 4327 * that need to handle optional GPIOs. 4328 */ 4329 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 4330 const char *con_id, 4331 enum gpiod_flags flags) 4332 { 4333 return gpiod_get_index_optional(dev, con_id, 0, flags); 4334 } 4335 EXPORT_SYMBOL_GPL(gpiod_get_optional); 4336 4337 4338 /** 4339 * gpiod_configure_flags - helper function to configure a given GPIO 4340 * @desc: gpio whose value will be assigned 4341 * @con_id: function within the GPIO consumer 4342 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 4343 * of_find_gpio() or of_get_gpio_hog() 4344 * @dflags: gpiod_flags - optional GPIO initialization flags 4345 * 4346 * Return 0 on success, -ENOENT if no GPIO has been assigned to the 4347 * requested function and/or index, or another IS_ERR() code if an error 4348 * occurred while trying to acquire the GPIO. 4349 */ 4350 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 4351 unsigned long lflags, enum gpiod_flags dflags) 4352 { 4353 int ret; 4354 4355 if (lflags & GPIO_ACTIVE_LOW) 4356 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 4357 4358 if (lflags & GPIO_OPEN_DRAIN) 4359 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 4360 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { 4361 /* 4362 * This enforces open drain mode from the consumer side. 4363 * This is necessary for some busses like I2C, but the lookup 4364 * should *REALLY* have specified them as open drain in the 4365 * first place, so print a little warning here. 4366 */ 4367 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 4368 gpiod_warn(desc, 4369 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); 4370 } 4371 4372 if (lflags & GPIO_OPEN_SOURCE) 4373 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 4374 4375 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) || 4376 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) || 4377 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) { 4378 gpiod_err(desc, 4379 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n"); 4380 return -EINVAL; 4381 } 4382 4383 if (lflags & GPIO_PULL_UP) 4384 set_bit(FLAG_PULL_UP, &desc->flags); 4385 else if (lflags & GPIO_PULL_DOWN) 4386 set_bit(FLAG_PULL_DOWN, &desc->flags); 4387 else if (lflags & GPIO_PULL_DISABLE) 4388 set_bit(FLAG_BIAS_DISABLE, &desc->flags); 4389 4390 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); 4391 if (ret < 0) 4392 return ret; 4393 4394 /* No particular flag request, return here... */ 4395 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { 4396 gpiod_dbg(desc, "no flags found for %s\n", con_id); 4397 return 0; 4398 } 4399 4400 /* Process flags */ 4401 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) 4402 ret = gpiod_direction_output(desc, 4403 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); 4404 else 4405 ret = gpiod_direction_input(desc); 4406 4407 return ret; 4408 } 4409 4410 /** 4411 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function 4412 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4413 * @con_id: function within the GPIO consumer 4414 * @idx: index of the GPIO to obtain in the consumer 4415 * @flags: optional GPIO initialization flags 4416 * 4417 * This variant of gpiod_get() allows to access GPIOs other than the first 4418 * defined one for functions that define several GPIOs. 4419 * 4420 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the 4421 * requested function and/or index, or another IS_ERR() code if an error 4422 * occurred while trying to acquire the GPIO. 4423 */ 4424 struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 4425 const char *con_id, 4426 unsigned int idx, 4427 enum gpiod_flags flags) 4428 { 4429 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 4430 const char *devname = dev ? dev_name(dev) : "?"; 4431 const char *label = con_id ?: devname; 4432 4433 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true); 4434 } 4435 EXPORT_SYMBOL_GPL(gpiod_get_index); 4436 4437 /** 4438 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO 4439 * function 4440 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4441 * @con_id: function within the GPIO consumer 4442 * @index: index of the GPIO to obtain in the consumer 4443 * @flags: optional GPIO initialization flags 4444 * 4445 * This is equivalent to gpiod_get_index(), except that when no GPIO with the 4446 * specified index was assigned to the requested function it will return NULL. 4447 * This is convenient for drivers that need to handle optional GPIOs. 4448 */ 4449 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 4450 const char *con_id, 4451 unsigned int index, 4452 enum gpiod_flags flags) 4453 { 4454 struct gpio_desc *desc; 4455 4456 desc = gpiod_get_index(dev, con_id, index, flags); 4457 if (gpiod_not_found(desc)) 4458 return NULL; 4459 4460 return desc; 4461 } 4462 EXPORT_SYMBOL_GPL(gpiod_get_index_optional); 4463 4464 /** 4465 * gpiod_hog - Hog the specified GPIO desc given the provided flags 4466 * @desc: gpio whose value will be assigned 4467 * @name: gpio line name 4468 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 4469 * of_find_gpio() or of_get_gpio_hog() 4470 * @dflags: gpiod_flags - optional GPIO initialization flags 4471 */ 4472 int gpiod_hog(struct gpio_desc *desc, const char *name, 4473 unsigned long lflags, enum gpiod_flags dflags) 4474 { 4475 struct gpio_device *gdev = desc->gdev; 4476 struct gpio_desc *local_desc; 4477 int hwnum; 4478 int ret; 4479 4480 CLASS(gpio_chip_guard, guard)(desc); 4481 if (!guard.gc) 4482 return -ENODEV; 4483 4484 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags)) 4485 return 0; 4486 4487 hwnum = gpio_chip_hwgpio(desc); 4488 4489 local_desc = gpiochip_request_own_desc(guard.gc, hwnum, name, 4490 lflags, dflags); 4491 if (IS_ERR(local_desc)) { 4492 clear_bit(FLAG_IS_HOGGED, &desc->flags); 4493 ret = PTR_ERR(local_desc); 4494 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", 4495 name, gdev->label, hwnum, ret); 4496 return ret; 4497 } 4498 4499 gpiod_dbg(desc, "hogged as %s%s\n", 4500 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", 4501 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? 4502 (dflags & GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low" : ""); 4503 4504 return 0; 4505 } 4506 4507 /** 4508 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog 4509 * @gc: gpio chip to act on 4510 */ 4511 static void gpiochip_free_hogs(struct gpio_chip *gc) 4512 { 4513 struct gpio_desc *desc; 4514 4515 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED) 4516 gpiochip_free_own_desc(desc); 4517 } 4518 4519 /** 4520 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function 4521 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4522 * @con_id: function within the GPIO consumer 4523 * @flags: optional GPIO initialization flags 4524 * 4525 * This function acquires all the GPIOs defined under a given function. 4526 * 4527 * Return a struct gpio_descs containing an array of descriptors, -ENOENT if 4528 * no GPIO has been assigned to the requested function, or another IS_ERR() 4529 * code if an error occurred while trying to acquire the GPIOs. 4530 */ 4531 struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 4532 const char *con_id, 4533 enum gpiod_flags flags) 4534 { 4535 struct gpio_desc *desc; 4536 struct gpio_descs *descs; 4537 struct gpio_array *array_info = NULL; 4538 struct gpio_chip *gc; 4539 int count, bitmap_size; 4540 size_t descs_size; 4541 4542 count = gpiod_count(dev, con_id); 4543 if (count < 0) 4544 return ERR_PTR(count); 4545 4546 descs_size = struct_size(descs, desc, count); 4547 descs = kzalloc(descs_size, GFP_KERNEL); 4548 if (!descs) 4549 return ERR_PTR(-ENOMEM); 4550 4551 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { 4552 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); 4553 if (IS_ERR(desc)) { 4554 gpiod_put_array(descs); 4555 return ERR_CAST(desc); 4556 } 4557 4558 descs->desc[descs->ndescs] = desc; 4559 4560 gc = gpiod_to_chip(desc); 4561 /* 4562 * If pin hardware number of array member 0 is also 0, select 4563 * its chip as a candidate for fast bitmap processing path. 4564 */ 4565 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { 4566 struct gpio_descs *array; 4567 4568 bitmap_size = BITS_TO_LONGS(gc->ngpio > count ? 4569 gc->ngpio : count); 4570 4571 array = krealloc(descs, descs_size + 4572 struct_size(array_info, invert_mask, 3 * bitmap_size), 4573 GFP_KERNEL | __GFP_ZERO); 4574 if (!array) { 4575 gpiod_put_array(descs); 4576 return ERR_PTR(-ENOMEM); 4577 } 4578 4579 descs = array; 4580 4581 array_info = (void *)descs + descs_size; 4582 array_info->get_mask = array_info->invert_mask + 4583 bitmap_size; 4584 array_info->set_mask = array_info->get_mask + 4585 bitmap_size; 4586 4587 array_info->desc = descs->desc; 4588 array_info->size = count; 4589 array_info->chip = gc; 4590 bitmap_set(array_info->get_mask, descs->ndescs, 4591 count - descs->ndescs); 4592 bitmap_set(array_info->set_mask, descs->ndescs, 4593 count - descs->ndescs); 4594 descs->info = array_info; 4595 } 4596 4597 /* If there is no cache for fast bitmap processing path, continue */ 4598 if (!array_info) 4599 continue; 4600 4601 /* Unmark array members which don't belong to the 'fast' chip */ 4602 if (array_info->chip != gc) { 4603 __clear_bit(descs->ndescs, array_info->get_mask); 4604 __clear_bit(descs->ndescs, array_info->set_mask); 4605 } 4606 /* 4607 * Detect array members which belong to the 'fast' chip 4608 * but their pins are not in hardware order. 4609 */ 4610 else if (gpio_chip_hwgpio(desc) != descs->ndescs) { 4611 /* 4612 * Don't use fast path if all array members processed so 4613 * far belong to the same chip as this one but its pin 4614 * hardware number is different from its array index. 4615 */ 4616 if (bitmap_full(array_info->get_mask, descs->ndescs)) { 4617 array_info = NULL; 4618 } else { 4619 __clear_bit(descs->ndescs, 4620 array_info->get_mask); 4621 __clear_bit(descs->ndescs, 4622 array_info->set_mask); 4623 } 4624 } else { 4625 /* Exclude open drain or open source from fast output */ 4626 if (gpiochip_line_is_open_drain(gc, descs->ndescs) || 4627 gpiochip_line_is_open_source(gc, descs->ndescs)) 4628 __clear_bit(descs->ndescs, 4629 array_info->set_mask); 4630 /* Identify 'fast' pins which require invertion */ 4631 if (gpiod_is_active_low(desc)) 4632 __set_bit(descs->ndescs, 4633 array_info->invert_mask); 4634 } 4635 } 4636 if (array_info) 4637 dev_dbg(dev, 4638 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", 4639 array_info->chip->label, array_info->size, 4640 *array_info->get_mask, *array_info->set_mask, 4641 *array_info->invert_mask); 4642 return descs; 4643 } 4644 EXPORT_SYMBOL_GPL(gpiod_get_array); 4645 4646 /** 4647 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO 4648 * function 4649 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4650 * @con_id: function within the GPIO consumer 4651 * @flags: optional GPIO initialization flags 4652 * 4653 * This is equivalent to gpiod_get_array(), except that when no GPIO was 4654 * assigned to the requested function it will return NULL. 4655 */ 4656 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 4657 const char *con_id, 4658 enum gpiod_flags flags) 4659 { 4660 struct gpio_descs *descs; 4661 4662 descs = gpiod_get_array(dev, con_id, flags); 4663 if (gpiod_not_found(descs)) 4664 return NULL; 4665 4666 return descs; 4667 } 4668 EXPORT_SYMBOL_GPL(gpiod_get_array_optional); 4669 4670 /** 4671 * gpiod_put - dispose of a GPIO descriptor 4672 * @desc: GPIO descriptor to dispose of 4673 * 4674 * No descriptor can be used after gpiod_put() has been called on it. 4675 */ 4676 void gpiod_put(struct gpio_desc *desc) 4677 { 4678 if (desc) 4679 gpiod_free(desc); 4680 } 4681 EXPORT_SYMBOL_GPL(gpiod_put); 4682 4683 /** 4684 * gpiod_put_array - dispose of multiple GPIO descriptors 4685 * @descs: struct gpio_descs containing an array of descriptors 4686 */ 4687 void gpiod_put_array(struct gpio_descs *descs) 4688 { 4689 unsigned int i; 4690 4691 for (i = 0; i < descs->ndescs; i++) 4692 gpiod_put(descs->desc[i]); 4693 4694 kfree(descs); 4695 } 4696 EXPORT_SYMBOL_GPL(gpiod_put_array); 4697 4698 static int gpio_stub_drv_probe(struct device *dev) 4699 { 4700 /* 4701 * The DT node of some GPIO chips have a "compatible" property, but 4702 * never have a struct device added and probed by a driver to register 4703 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause 4704 * the consumers of the GPIO chip to get probe deferred forever because 4705 * they will be waiting for a device associated with the GPIO chip 4706 * firmware node to get added and bound to a driver. 4707 * 4708 * To allow these consumers to probe, we associate the struct 4709 * gpio_device of the GPIO chip with the firmware node and then simply 4710 * bind it to this stub driver. 4711 */ 4712 return 0; 4713 } 4714 4715 static struct device_driver gpio_stub_drv = { 4716 .name = "gpio_stub_drv", 4717 .bus = &gpio_bus_type, 4718 .probe = gpio_stub_drv_probe, 4719 }; 4720 4721 static int __init gpiolib_dev_init(void) 4722 { 4723 int ret; 4724 4725 /* Register GPIO sysfs bus */ 4726 ret = bus_register(&gpio_bus_type); 4727 if (ret < 0) { 4728 pr_err("gpiolib: could not register GPIO bus type\n"); 4729 return ret; 4730 } 4731 4732 ret = driver_register(&gpio_stub_drv); 4733 if (ret < 0) { 4734 pr_err("gpiolib: could not register GPIO stub driver\n"); 4735 bus_unregister(&gpio_bus_type); 4736 return ret; 4737 } 4738 4739 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME); 4740 if (ret < 0) { 4741 pr_err("gpiolib: failed to allocate char dev region\n"); 4742 driver_unregister(&gpio_stub_drv); 4743 bus_unregister(&gpio_bus_type); 4744 return ret; 4745 } 4746 4747 gpiolib_initialized = true; 4748 gpiochip_setup_devs(); 4749 4750 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO) 4751 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier)); 4752 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */ 4753 4754 return ret; 4755 } 4756 core_initcall(gpiolib_dev_init); 4757 4758 #ifdef CONFIG_DEBUG_FS 4759 4760 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) 4761 { 4762 bool active_low, is_irq, is_out; 4763 unsigned int gpio = gdev->base; 4764 struct gpio_desc *desc; 4765 struct gpio_chip *gc; 4766 int value; 4767 4768 guard(srcu)(&gdev->srcu); 4769 4770 gc = srcu_dereference(gdev->chip, &gdev->srcu); 4771 if (!gc) { 4772 seq_puts(s, "Underlying GPIO chip is gone\n"); 4773 return; 4774 } 4775 4776 for_each_gpio_desc(gc, desc) { 4777 guard(srcu)(&desc->srcu); 4778 if (test_bit(FLAG_REQUESTED, &desc->flags)) { 4779 gpiod_get_direction(desc); 4780 is_out = test_bit(FLAG_IS_OUT, &desc->flags); 4781 value = gpio_chip_get_value(gc, desc); 4782 is_irq = test_bit(FLAG_USED_AS_IRQ, &desc->flags); 4783 active_low = test_bit(FLAG_ACTIVE_LOW, &desc->flags); 4784 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s\n", 4785 gpio, desc->name ?: "", gpiod_get_label(desc), 4786 is_out ? "out" : "in ", 4787 value >= 0 ? (value ? "hi" : "lo") : "? ", 4788 is_irq ? "IRQ " : "", 4789 active_low ? "ACTIVE LOW" : ""); 4790 } else if (desc->name) { 4791 seq_printf(s, " gpio-%-3d (%-20.20s)\n", gpio, desc->name); 4792 } 4793 4794 gpio++; 4795 } 4796 } 4797 4798 struct gpiolib_seq_priv { 4799 bool newline; 4800 int idx; 4801 }; 4802 4803 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) 4804 { 4805 struct gpiolib_seq_priv *priv; 4806 struct gpio_device *gdev; 4807 loff_t index = *pos; 4808 4809 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 4810 if (!priv) 4811 return NULL; 4812 4813 s->private = priv; 4814 priv->idx = srcu_read_lock(&gpio_devices_srcu); 4815 4816 list_for_each_entry_srcu(gdev, &gpio_devices, list, 4817 srcu_read_lock_held(&gpio_devices_srcu)) { 4818 if (index-- == 0) 4819 return gdev; 4820 } 4821 4822 return NULL; 4823 } 4824 4825 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) 4826 { 4827 struct gpiolib_seq_priv *priv = s->private; 4828 struct gpio_device *gdev = v, *next; 4829 4830 next = list_entry_rcu(gdev->list.next, struct gpio_device, list); 4831 gdev = &next->list == &gpio_devices ? NULL : next; 4832 priv->newline = true; 4833 ++*pos; 4834 4835 return gdev; 4836 } 4837 4838 static void gpiolib_seq_stop(struct seq_file *s, void *v) 4839 { 4840 struct gpiolib_seq_priv *priv = s->private; 4841 4842 srcu_read_unlock(&gpio_devices_srcu, priv->idx); 4843 kfree(priv); 4844 } 4845 4846 static int gpiolib_seq_show(struct seq_file *s, void *v) 4847 { 4848 struct gpiolib_seq_priv *priv = s->private; 4849 struct gpio_device *gdev = v; 4850 struct gpio_chip *gc; 4851 struct device *parent; 4852 4853 guard(srcu)(&gdev->srcu); 4854 4855 gc = srcu_dereference(gdev->chip, &gdev->srcu); 4856 if (!gc) { 4857 seq_printf(s, "%s%s: (dangling chip)", 4858 priv->newline ? "\n" : "", 4859 dev_name(&gdev->dev)); 4860 return 0; 4861 } 4862 4863 seq_printf(s, "%s%s: GPIOs %d-%d", priv->newline ? "\n" : "", 4864 dev_name(&gdev->dev), 4865 gdev->base, gdev->base + gdev->ngpio - 1); 4866 parent = gc->parent; 4867 if (parent) 4868 seq_printf(s, ", parent: %s/%s", 4869 parent->bus ? parent->bus->name : "no-bus", 4870 dev_name(parent)); 4871 if (gc->label) 4872 seq_printf(s, ", %s", gc->label); 4873 if (gc->can_sleep) 4874 seq_printf(s, ", can sleep"); 4875 seq_printf(s, ":\n"); 4876 4877 if (gc->dbg_show) 4878 gc->dbg_show(s, gc); 4879 else 4880 gpiolib_dbg_show(s, gdev); 4881 4882 return 0; 4883 } 4884 4885 static const struct seq_operations gpiolib_sops = { 4886 .start = gpiolib_seq_start, 4887 .next = gpiolib_seq_next, 4888 .stop = gpiolib_seq_stop, 4889 .show = gpiolib_seq_show, 4890 }; 4891 DEFINE_SEQ_ATTRIBUTE(gpiolib); 4892 4893 static int __init gpiolib_debugfs_init(void) 4894 { 4895 /* /sys/kernel/debug/gpio */ 4896 debugfs_create_file("gpio", 0444, NULL, NULL, &gpiolib_fops); 4897 return 0; 4898 } 4899 subsys_initcall(gpiolib_debugfs_init); 4900 4901 #endif /* DEBUG_FS */ 4902