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