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