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