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