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