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 int ret; 3315 3316 lockdep_assert_held(&gc->gpiodev->srcu); 3317 3318 if (gc->get_multiple) { 3319 ret = gc->get_multiple(gc, mask, bits); 3320 if (ret > 0) 3321 return -EBADE; 3322 } 3323 3324 if (gc->get) { 3325 int i, value; 3326 3327 for_each_set_bit(i, mask, gc->ngpio) { 3328 value = gpiochip_get(gc, i); 3329 if (value < 0) 3330 return value; 3331 __assign_bit(i, bits, value); 3332 } 3333 return 0; 3334 } 3335 return -EIO; 3336 } 3337 3338 /* The 'other' chip must be protected with its GPIO device's SRCU. */ 3339 static bool gpio_device_chip_cmp(struct gpio_device *gdev, struct gpio_chip *gc) 3340 { 3341 guard(srcu)(&gdev->srcu); 3342 3343 return gc == srcu_dereference(gdev->chip, &gdev->srcu); 3344 } 3345 3346 int gpiod_get_array_value_complex(bool raw, bool can_sleep, 3347 unsigned int array_size, 3348 struct gpio_desc **desc_array, 3349 struct gpio_array *array_info, 3350 unsigned long *value_bitmap) 3351 { 3352 struct gpio_chip *gc; 3353 int ret, i = 0; 3354 3355 /* 3356 * Validate array_info against desc_array and its size. 3357 * It should immediately follow desc_array if both 3358 * have been obtained from the same gpiod_get_array() call. 3359 */ 3360 if (array_info && array_info->desc == desc_array && 3361 array_size <= array_info->size && 3362 (void *)array_info == desc_array + array_info->size) { 3363 if (!can_sleep) 3364 WARN_ON(array_info->gdev->can_sleep); 3365 3366 guard(srcu)(&array_info->gdev->srcu); 3367 gc = srcu_dereference(array_info->gdev->chip, 3368 &array_info->gdev->srcu); 3369 if (!gc) 3370 return -ENODEV; 3371 3372 ret = gpio_chip_get_multiple(gc, array_info->get_mask, 3373 value_bitmap); 3374 if (ret) 3375 return ret; 3376 3377 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 3378 bitmap_xor(value_bitmap, value_bitmap, 3379 array_info->invert_mask, array_size); 3380 3381 i = find_first_zero_bit(array_info->get_mask, array_size); 3382 if (i == array_size) 3383 return 0; 3384 } else { 3385 array_info = NULL; 3386 } 3387 3388 while (i < array_size) { 3389 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); 3390 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); 3391 unsigned long *mask, *bits; 3392 int first, j; 3393 3394 CLASS(gpio_chip_guard, guard)(desc_array[i]); 3395 if (!guard.gc) 3396 return -ENODEV; 3397 3398 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { 3399 mask = fastpath_mask; 3400 bits = fastpath_bits; 3401 } else { 3402 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; 3403 3404 mask = bitmap_alloc(guard.gc->ngpio, flags); 3405 if (!mask) 3406 return -ENOMEM; 3407 3408 bits = bitmap_alloc(guard.gc->ngpio, flags); 3409 if (!bits) { 3410 bitmap_free(mask); 3411 return -ENOMEM; 3412 } 3413 } 3414 3415 bitmap_zero(mask, guard.gc->ngpio); 3416 3417 if (!can_sleep) 3418 WARN_ON(guard.gc->can_sleep); 3419 3420 /* collect all inputs belonging to the same chip */ 3421 first = i; 3422 do { 3423 const struct gpio_desc *desc = desc_array[i]; 3424 int hwgpio = gpio_chip_hwgpio(desc); 3425 3426 __set_bit(hwgpio, mask); 3427 i++; 3428 3429 if (array_info) 3430 i = find_next_zero_bit(array_info->get_mask, 3431 array_size, i); 3432 } while ((i < array_size) && 3433 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); 3434 3435 ret = gpio_chip_get_multiple(guard.gc, mask, bits); 3436 if (ret) { 3437 if (mask != fastpath_mask) 3438 bitmap_free(mask); 3439 if (bits != fastpath_bits) 3440 bitmap_free(bits); 3441 return ret; 3442 } 3443 3444 for (j = first; j < i; ) { 3445 const struct gpio_desc *desc = desc_array[j]; 3446 int hwgpio = gpio_chip_hwgpio(desc); 3447 int value = test_bit(hwgpio, bits); 3448 3449 if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3450 value = !value; 3451 __assign_bit(j, value_bitmap, value); 3452 trace_gpio_value(desc_to_gpio(desc), 1, value); 3453 j++; 3454 3455 if (array_info) 3456 j = find_next_zero_bit(array_info->get_mask, i, 3457 j); 3458 } 3459 3460 if (mask != fastpath_mask) 3461 bitmap_free(mask); 3462 if (bits != fastpath_bits) 3463 bitmap_free(bits); 3464 } 3465 return 0; 3466 } 3467 3468 /** 3469 * gpiod_get_raw_value() - return a gpio's raw value 3470 * @desc: gpio whose value will be returned 3471 * 3472 * Returns: 3473 * The GPIO's raw value, i.e. the value of the physical line disregarding 3474 * its ACTIVE_LOW status, or negative errno on failure. 3475 * 3476 * This function can be called from contexts where we cannot sleep, and will 3477 * complain if the GPIO chip functions potentially sleep. 3478 */ 3479 int gpiod_get_raw_value(const struct gpio_desc *desc) 3480 { 3481 VALIDATE_DESC(desc); 3482 /* Should be using gpiod_get_raw_value_cansleep() */ 3483 WARN_ON(desc->gdev->can_sleep); 3484 return gpiod_get_raw_value_commit(desc); 3485 } 3486 EXPORT_SYMBOL_GPL(gpiod_get_raw_value); 3487 3488 /** 3489 * gpiod_get_value() - return a gpio's value 3490 * @desc: gpio whose value will be returned 3491 * 3492 * Returns: 3493 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into 3494 * account, or negative errno on failure. 3495 * 3496 * This function can be called from contexts where we cannot sleep, and will 3497 * complain if the GPIO chip functions potentially sleep. 3498 */ 3499 int gpiod_get_value(const struct gpio_desc *desc) 3500 { 3501 int value; 3502 3503 VALIDATE_DESC(desc); 3504 /* Should be using gpiod_get_value_cansleep() */ 3505 WARN_ON(desc->gdev->can_sleep); 3506 3507 value = gpiod_get_raw_value_commit(desc); 3508 if (value < 0) 3509 return value; 3510 3511 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3512 value = !value; 3513 3514 return value; 3515 } 3516 EXPORT_SYMBOL_GPL(gpiod_get_value); 3517 3518 /** 3519 * gpiod_get_raw_array_value() - read raw values from an array of GPIOs 3520 * @array_size: number of elements in the descriptor array / value bitmap 3521 * @desc_array: array of GPIO descriptors whose values will be read 3522 * @array_info: information on applicability of fast bitmap processing path 3523 * @value_bitmap: bitmap to store the read values 3524 * 3525 * Read the raw values of the GPIOs, i.e. the values of the physical lines 3526 * without regard for their ACTIVE_LOW status. 3527 * 3528 * This function can be called from contexts where we cannot sleep, 3529 * and it will complain if the GPIO chip functions potentially sleep. 3530 * 3531 * Returns: 3532 * 0 on success, or negative errno on failure. 3533 */ 3534 int gpiod_get_raw_array_value(unsigned int array_size, 3535 struct gpio_desc **desc_array, 3536 struct gpio_array *array_info, 3537 unsigned long *value_bitmap) 3538 { 3539 if (!desc_array) 3540 return -EINVAL; 3541 return gpiod_get_array_value_complex(true, false, array_size, 3542 desc_array, array_info, 3543 value_bitmap); 3544 } 3545 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value); 3546 3547 /** 3548 * gpiod_get_array_value() - read values from an array of GPIOs 3549 * @array_size: number of elements in the descriptor array / value bitmap 3550 * @desc_array: array of GPIO descriptors whose values will be read 3551 * @array_info: information on applicability of fast bitmap processing path 3552 * @value_bitmap: bitmap to store the read values 3553 * 3554 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3555 * into account. 3556 * 3557 * This function can be called from contexts where we cannot sleep, 3558 * and it will complain if the GPIO chip functions potentially sleep. 3559 * 3560 * Returns: 3561 * 0 on success, or negative errno on failure. 3562 */ 3563 int gpiod_get_array_value(unsigned int array_size, 3564 struct gpio_desc **desc_array, 3565 struct gpio_array *array_info, 3566 unsigned long *value_bitmap) 3567 { 3568 if (!desc_array) 3569 return -EINVAL; 3570 return gpiod_get_array_value_complex(false, false, array_size, 3571 desc_array, array_info, 3572 value_bitmap); 3573 } 3574 EXPORT_SYMBOL_GPL(gpiod_get_array_value); 3575 3576 /* 3577 * gpio_set_open_drain_value_commit() - Set the open drain gpio's value. 3578 * @desc: gpio descriptor whose state need to be set. 3579 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 3580 */ 3581 static int gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) 3582 { 3583 int ret = 0, offset = gpio_chip_hwgpio(desc); 3584 3585 CLASS(gpio_chip_guard, guard)(desc); 3586 if (!guard.gc) 3587 return -ENODEV; 3588 3589 if (value) { 3590 ret = gpiochip_direction_input(guard.gc, offset); 3591 } else { 3592 ret = gpiochip_direction_output(guard.gc, offset, 0); 3593 if (!ret) 3594 set_bit(FLAG_IS_OUT, &desc->flags); 3595 } 3596 trace_gpio_direction(desc_to_gpio(desc), value, ret); 3597 if (ret < 0) 3598 gpiod_err(desc, 3599 "%s: Error in set_value for open drain err %d\n", 3600 __func__, ret); 3601 3602 return ret; 3603 } 3604 3605 /* 3606 * _gpio_set_open_source_value() - Set the open source gpio's value. 3607 * @desc: gpio descriptor whose state need to be set. 3608 * @value: Non-zero for setting it HIGH otherwise it will set to LOW. 3609 */ 3610 static int gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value) 3611 { 3612 int ret = 0, offset = gpio_chip_hwgpio(desc); 3613 3614 CLASS(gpio_chip_guard, guard)(desc); 3615 if (!guard.gc) 3616 return -ENODEV; 3617 3618 if (value) { 3619 ret = gpiochip_direction_output(guard.gc, offset, 1); 3620 if (!ret) 3621 set_bit(FLAG_IS_OUT, &desc->flags); 3622 } else { 3623 ret = gpiochip_direction_input(guard.gc, offset); 3624 } 3625 trace_gpio_direction(desc_to_gpio(desc), !value, ret); 3626 if (ret < 0) 3627 gpiod_err(desc, 3628 "%s: Error in set_value for open source err %d\n", 3629 __func__, ret); 3630 3631 return ret; 3632 } 3633 3634 static int gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value) 3635 { 3636 if (unlikely(!test_bit(FLAG_IS_OUT, &desc->flags))) 3637 return -EPERM; 3638 3639 CLASS(gpio_chip_guard, guard)(desc); 3640 if (!guard.gc) 3641 return -ENODEV; 3642 3643 trace_gpio_value(desc_to_gpio(desc), 0, value); 3644 return gpiochip_set(guard.gc, gpio_chip_hwgpio(desc), value); 3645 } 3646 3647 /* 3648 * set multiple outputs on the same chip; 3649 * use the chip's set_multiple function if available; 3650 * otherwise set the outputs sequentially; 3651 * @chip: the GPIO chip we operate on 3652 * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word 3653 * defines which outputs are to be changed 3654 * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word 3655 * defines the values the outputs specified by mask are to be set to 3656 * 3657 * Returns: 0 on success, negative error number on failure. 3658 */ 3659 static int gpiochip_set_multiple(struct gpio_chip *gc, 3660 unsigned long *mask, unsigned long *bits) 3661 { 3662 unsigned int i; 3663 int ret; 3664 3665 lockdep_assert_held(&gc->gpiodev->srcu); 3666 3667 if (gc->set_multiple_rv) { 3668 ret = gc->set_multiple_rv(gc, mask, bits); 3669 if (ret > 0) 3670 ret = -EBADE; 3671 3672 return ret; 3673 } 3674 3675 if (gc->set_multiple) { 3676 gc->set_multiple(gc, mask, bits); 3677 return 0; 3678 } 3679 3680 /* set outputs if the corresponding mask bit is set */ 3681 for_each_set_bit(i, mask, gc->ngpio) { 3682 ret = gpiochip_set(gc, i, test_bit(i, bits)); 3683 if (ret) 3684 break; 3685 } 3686 3687 return ret; 3688 } 3689 3690 int gpiod_set_array_value_complex(bool raw, bool can_sleep, 3691 unsigned int array_size, 3692 struct gpio_desc **desc_array, 3693 struct gpio_array *array_info, 3694 unsigned long *value_bitmap) 3695 { 3696 struct gpio_chip *gc; 3697 int i = 0, ret; 3698 3699 /* 3700 * Validate array_info against desc_array and its size. 3701 * It should immediately follow desc_array if both 3702 * have been obtained from the same gpiod_get_array() call. 3703 */ 3704 if (array_info && array_info->desc == desc_array && 3705 array_size <= array_info->size && 3706 (void *)array_info == desc_array + array_info->size) { 3707 if (!can_sleep) 3708 WARN_ON(array_info->gdev->can_sleep); 3709 3710 for (i = 0; i < array_size; i++) { 3711 if (unlikely(!test_bit(FLAG_IS_OUT, 3712 &desc_array[i]->flags))) 3713 return -EPERM; 3714 } 3715 3716 guard(srcu)(&array_info->gdev->srcu); 3717 gc = srcu_dereference(array_info->gdev->chip, 3718 &array_info->gdev->srcu); 3719 if (!gc) 3720 return -ENODEV; 3721 3722 if (!raw && !bitmap_empty(array_info->invert_mask, array_size)) 3723 bitmap_xor(value_bitmap, value_bitmap, 3724 array_info->invert_mask, array_size); 3725 3726 ret = gpiochip_set_multiple(gc, array_info->set_mask, 3727 value_bitmap); 3728 if (ret) 3729 return ret; 3730 3731 i = find_first_zero_bit(array_info->set_mask, array_size); 3732 if (i == array_size) 3733 return 0; 3734 } else { 3735 array_info = NULL; 3736 } 3737 3738 while (i < array_size) { 3739 DECLARE_BITMAP(fastpath_mask, FASTPATH_NGPIO); 3740 DECLARE_BITMAP(fastpath_bits, FASTPATH_NGPIO); 3741 unsigned long *mask, *bits; 3742 int count = 0; 3743 3744 CLASS(gpio_chip_guard, guard)(desc_array[i]); 3745 if (!guard.gc) 3746 return -ENODEV; 3747 3748 if (likely(guard.gc->ngpio <= FASTPATH_NGPIO)) { 3749 mask = fastpath_mask; 3750 bits = fastpath_bits; 3751 } else { 3752 gfp_t flags = can_sleep ? GFP_KERNEL : GFP_ATOMIC; 3753 3754 mask = bitmap_alloc(guard.gc->ngpio, flags); 3755 if (!mask) 3756 return -ENOMEM; 3757 3758 bits = bitmap_alloc(guard.gc->ngpio, flags); 3759 if (!bits) { 3760 bitmap_free(mask); 3761 return -ENOMEM; 3762 } 3763 } 3764 3765 bitmap_zero(mask, guard.gc->ngpio); 3766 3767 if (!can_sleep) 3768 WARN_ON(guard.gc->can_sleep); 3769 3770 do { 3771 struct gpio_desc *desc = desc_array[i]; 3772 int hwgpio = gpio_chip_hwgpio(desc); 3773 int value = test_bit(i, value_bitmap); 3774 3775 if (unlikely(!test_bit(FLAG_IS_OUT, &desc->flags))) 3776 return -EPERM; 3777 3778 /* 3779 * Pins applicable for fast input but not for 3780 * fast output processing may have been already 3781 * inverted inside the fast path, skip them. 3782 */ 3783 if (!raw && !(array_info && 3784 test_bit(i, array_info->invert_mask)) && 3785 test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3786 value = !value; 3787 trace_gpio_value(desc_to_gpio(desc), 0, value); 3788 /* 3789 * collect all normal outputs belonging to the same chip 3790 * open drain and open source outputs are set individually 3791 */ 3792 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) { 3793 gpio_set_open_drain_value_commit(desc, value); 3794 } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) { 3795 gpio_set_open_source_value_commit(desc, value); 3796 } else { 3797 __set_bit(hwgpio, mask); 3798 __assign_bit(hwgpio, bits, value); 3799 count++; 3800 } 3801 i++; 3802 3803 if (array_info) 3804 i = find_next_zero_bit(array_info->set_mask, 3805 array_size, i); 3806 } while ((i < array_size) && 3807 gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); 3808 /* push collected bits to outputs */ 3809 if (count != 0) { 3810 ret = gpiochip_set_multiple(guard.gc, mask, bits); 3811 if (ret) 3812 return ret; 3813 } 3814 3815 if (mask != fastpath_mask) 3816 bitmap_free(mask); 3817 if (bits != fastpath_bits) 3818 bitmap_free(bits); 3819 } 3820 return 0; 3821 } 3822 3823 /** 3824 * gpiod_set_raw_value() - assign a gpio's raw value 3825 * @desc: gpio whose value will be assigned 3826 * @value: value to assign 3827 * 3828 * Set the raw value of the GPIO, i.e. the value of its physical line without 3829 * regard for its ACTIVE_LOW status. 3830 * 3831 * This function can be called from contexts where we cannot sleep, and will 3832 * complain if the GPIO chip functions potentially sleep. 3833 * 3834 * Returns: 3835 * 0 on success, negative error number on failure. 3836 */ 3837 int gpiod_set_raw_value(struct gpio_desc *desc, int value) 3838 { 3839 VALIDATE_DESC(desc); 3840 /* Should be using gpiod_set_raw_value_cansleep() */ 3841 WARN_ON(desc->gdev->can_sleep); 3842 return gpiod_set_raw_value_commit(desc, value); 3843 } 3844 EXPORT_SYMBOL_GPL(gpiod_set_raw_value); 3845 3846 /** 3847 * gpiod_set_value_nocheck() - set a GPIO line value without checking 3848 * @desc: the descriptor to set the value on 3849 * @value: value to set 3850 * 3851 * This sets the value of a GPIO line backing a descriptor, applying 3852 * different semantic quirks like active low and open drain/source 3853 * handling. 3854 * 3855 * Returns: 3856 * 0 on success, negative error number on failure. 3857 */ 3858 static int gpiod_set_value_nocheck(struct gpio_desc *desc, int value) 3859 { 3860 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 3861 value = !value; 3862 3863 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 3864 return gpio_set_open_drain_value_commit(desc, value); 3865 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 3866 return gpio_set_open_source_value_commit(desc, value); 3867 3868 return gpiod_set_raw_value_commit(desc, value); 3869 } 3870 3871 /** 3872 * gpiod_set_value() - assign a gpio's value 3873 * @desc: gpio whose value will be assigned 3874 * @value: value to assign 3875 * 3876 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW, 3877 * OPEN_DRAIN and OPEN_SOURCE flags into account. 3878 * 3879 * This function can be called from contexts where we cannot sleep, and will 3880 * complain if the GPIO chip functions potentially sleep. 3881 * 3882 * Returns: 3883 * 0 on success, negative error number on failure. 3884 */ 3885 int gpiod_set_value(struct gpio_desc *desc, int value) 3886 { 3887 VALIDATE_DESC(desc); 3888 /* Should be using gpiod_set_value_cansleep() */ 3889 WARN_ON(desc->gdev->can_sleep); 3890 return gpiod_set_value_nocheck(desc, value); 3891 } 3892 EXPORT_SYMBOL_GPL(gpiod_set_value); 3893 3894 /** 3895 * gpiod_set_raw_array_value() - assign values to an array of GPIOs 3896 * @array_size: number of elements in the descriptor array / value bitmap 3897 * @desc_array: array of GPIO descriptors whose values will be assigned 3898 * @array_info: information on applicability of fast bitmap processing path 3899 * @value_bitmap: bitmap of values to assign 3900 * 3901 * Set the raw values of the GPIOs, i.e. the values of the physical lines 3902 * without regard for their ACTIVE_LOW status. 3903 * 3904 * This function can be called from contexts where we cannot sleep, and will 3905 * complain if the GPIO chip functions potentially sleep. 3906 * 3907 * Returns: 3908 * 0 on success, or negative errno on failure. 3909 */ 3910 int gpiod_set_raw_array_value(unsigned int array_size, 3911 struct gpio_desc **desc_array, 3912 struct gpio_array *array_info, 3913 unsigned long *value_bitmap) 3914 { 3915 if (!desc_array) 3916 return -EINVAL; 3917 return gpiod_set_array_value_complex(true, false, array_size, 3918 desc_array, array_info, value_bitmap); 3919 } 3920 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value); 3921 3922 /** 3923 * gpiod_set_array_value() - assign values to an array of GPIOs 3924 * @array_size: number of elements in the descriptor array / value bitmap 3925 * @desc_array: array of GPIO descriptors whose values will be assigned 3926 * @array_info: information on applicability of fast bitmap processing path 3927 * @value_bitmap: bitmap of values to assign 3928 * 3929 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 3930 * into account. 3931 * 3932 * This function can be called from contexts where we cannot sleep, and will 3933 * complain if the GPIO chip functions potentially sleep. 3934 * 3935 * Returns: 3936 * 0 on success, or negative errno on failure. 3937 */ 3938 int gpiod_set_array_value(unsigned int array_size, 3939 struct gpio_desc **desc_array, 3940 struct gpio_array *array_info, 3941 unsigned long *value_bitmap) 3942 { 3943 if (!desc_array) 3944 return -EINVAL; 3945 return gpiod_set_array_value_complex(false, false, array_size, 3946 desc_array, array_info, 3947 value_bitmap); 3948 } 3949 EXPORT_SYMBOL_GPL(gpiod_set_array_value); 3950 3951 /** 3952 * gpiod_cansleep() - report whether gpio value access may sleep 3953 * @desc: gpio to check 3954 * 3955 * Returns: 3956 * 0 for non-sleepable, 1 for sleepable, or an error code in case of error. 3957 */ 3958 int gpiod_cansleep(const struct gpio_desc *desc) 3959 { 3960 VALIDATE_DESC(desc); 3961 return desc->gdev->can_sleep; 3962 } 3963 EXPORT_SYMBOL_GPL(gpiod_cansleep); 3964 3965 /** 3966 * gpiod_set_consumer_name() - set the consumer name for the descriptor 3967 * @desc: gpio to set the consumer name on 3968 * @name: the new consumer name 3969 * 3970 * Returns: 3971 * 0 on success, or negative errno on failure. 3972 */ 3973 int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name) 3974 { 3975 int ret; 3976 3977 VALIDATE_DESC(desc); 3978 3979 ret = desc_set_label(desc, name); 3980 if (ret == 0) 3981 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG); 3982 3983 return ret; 3984 } 3985 EXPORT_SYMBOL_GPL(gpiod_set_consumer_name); 3986 3987 /** 3988 * gpiod_to_irq() - return the IRQ corresponding to a GPIO 3989 * @desc: gpio whose IRQ will be returned (already requested) 3990 * 3991 * Returns: 3992 * The IRQ corresponding to the passed GPIO, or an error code in case of error. 3993 */ 3994 int gpiod_to_irq(const struct gpio_desc *desc) 3995 { 3996 struct gpio_device *gdev; 3997 struct gpio_chip *gc; 3998 int offset; 3999 int ret; 4000 4001 ret = validate_desc(desc, __func__); 4002 if (ret <= 0) 4003 return -EINVAL; 4004 4005 gdev = desc->gdev; 4006 /* FIXME Cannot use gpio_chip_guard due to const desc. */ 4007 guard(srcu)(&gdev->srcu); 4008 gc = srcu_dereference(gdev->chip, &gdev->srcu); 4009 if (!gc) 4010 return -ENODEV; 4011 4012 offset = gpio_chip_hwgpio(desc); 4013 if (gc->to_irq) { 4014 ret = gc->to_irq(gc, offset); 4015 if (ret) 4016 return ret; 4017 4018 /* Zero means NO_IRQ */ 4019 return -ENXIO; 4020 } 4021 #ifdef CONFIG_GPIOLIB_IRQCHIP 4022 if (gc->irq.chip) { 4023 /* 4024 * Avoid race condition with other code, which tries to lookup 4025 * an IRQ before the irqchip has been properly registered, 4026 * i.e. while gpiochip is still being brought up. 4027 */ 4028 return -EPROBE_DEFER; 4029 } 4030 #endif 4031 return -ENXIO; 4032 } 4033 EXPORT_SYMBOL_GPL(gpiod_to_irq); 4034 4035 /** 4036 * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ 4037 * @gc: the chip the GPIO to lock belongs to 4038 * @offset: the offset of the GPIO to lock as IRQ 4039 * 4040 * This is used directly by GPIO drivers that want to lock down 4041 * a certain GPIO line to be used for IRQs. 4042 * 4043 * Returns: 4044 * 0 on success, or negative errno on failure. 4045 */ 4046 int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) 4047 { 4048 struct gpio_desc *desc; 4049 4050 desc = gpiochip_get_desc(gc, offset); 4051 if (IS_ERR(desc)) 4052 return PTR_ERR(desc); 4053 4054 /* 4055 * If it's fast: flush the direction setting if something changed 4056 * behind our back 4057 */ 4058 if (!gc->can_sleep && gc->get_direction) { 4059 int dir = gpiod_get_direction(desc); 4060 4061 if (dir < 0) { 4062 chip_err(gc, "%s: cannot get GPIO direction\n", 4063 __func__); 4064 return dir; 4065 } 4066 } 4067 4068 /* To be valid for IRQ the line needs to be input or open drain */ 4069 if (test_bit(FLAG_IS_OUT, &desc->flags) && 4070 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)) { 4071 chip_err(gc, 4072 "%s: tried to flag a GPIO set as output for IRQ\n", 4073 __func__); 4074 return -EIO; 4075 } 4076 4077 set_bit(FLAG_USED_AS_IRQ, &desc->flags); 4078 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 4079 4080 return 0; 4081 } 4082 EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq); 4083 4084 /** 4085 * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ 4086 * @gc: the chip the GPIO to lock belongs to 4087 * @offset: the offset of the GPIO to lock as IRQ 4088 * 4089 * This is used directly by GPIO drivers that want to indicate 4090 * that a certain GPIO is no longer used exclusively for IRQ. 4091 */ 4092 void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset) 4093 { 4094 struct gpio_desc *desc; 4095 4096 desc = gpiochip_get_desc(gc, offset); 4097 if (IS_ERR(desc)) 4098 return; 4099 4100 clear_bit(FLAG_USED_AS_IRQ, &desc->flags); 4101 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 4102 } 4103 EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq); 4104 4105 void gpiochip_disable_irq(struct gpio_chip *gc, unsigned int offset) 4106 { 4107 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 4108 4109 if (!IS_ERR(desc) && 4110 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) 4111 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 4112 } 4113 EXPORT_SYMBOL_GPL(gpiochip_disable_irq); 4114 4115 void gpiochip_enable_irq(struct gpio_chip *gc, unsigned int offset) 4116 { 4117 struct gpio_desc *desc = gpiochip_get_desc(gc, offset); 4118 4119 if (!IS_ERR(desc) && 4120 !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) { 4121 /* 4122 * We must not be output when using IRQ UNLESS we are 4123 * open drain. 4124 */ 4125 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) && 4126 !test_bit(FLAG_OPEN_DRAIN, &desc->flags)); 4127 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags); 4128 } 4129 } 4130 EXPORT_SYMBOL_GPL(gpiochip_enable_irq); 4131 4132 bool gpiochip_line_is_irq(struct gpio_chip *gc, unsigned int offset) 4133 { 4134 if (offset >= gc->ngpio) 4135 return false; 4136 4137 return test_bit(FLAG_USED_AS_IRQ, &gc->gpiodev->descs[offset].flags); 4138 } 4139 EXPORT_SYMBOL_GPL(gpiochip_line_is_irq); 4140 4141 int gpiochip_reqres_irq(struct gpio_chip *gc, unsigned int offset) 4142 { 4143 int ret; 4144 4145 if (!try_module_get(gc->gpiodev->owner)) 4146 return -ENODEV; 4147 4148 ret = gpiochip_lock_as_irq(gc, offset); 4149 if (ret) { 4150 chip_err(gc, "unable to lock HW IRQ %u for IRQ\n", offset); 4151 module_put(gc->gpiodev->owner); 4152 return ret; 4153 } 4154 return 0; 4155 } 4156 EXPORT_SYMBOL_GPL(gpiochip_reqres_irq); 4157 4158 void gpiochip_relres_irq(struct gpio_chip *gc, unsigned int offset) 4159 { 4160 gpiochip_unlock_as_irq(gc, offset); 4161 module_put(gc->gpiodev->owner); 4162 } 4163 EXPORT_SYMBOL_GPL(gpiochip_relres_irq); 4164 4165 bool gpiochip_line_is_open_drain(struct gpio_chip *gc, unsigned int offset) 4166 { 4167 if (offset >= gc->ngpio) 4168 return false; 4169 4170 return test_bit(FLAG_OPEN_DRAIN, &gc->gpiodev->descs[offset].flags); 4171 } 4172 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain); 4173 4174 bool gpiochip_line_is_open_source(struct gpio_chip *gc, unsigned int offset) 4175 { 4176 if (offset >= gc->ngpio) 4177 return false; 4178 4179 return test_bit(FLAG_OPEN_SOURCE, &gc->gpiodev->descs[offset].flags); 4180 } 4181 EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source); 4182 4183 bool gpiochip_line_is_persistent(struct gpio_chip *gc, unsigned int offset) 4184 { 4185 if (offset >= gc->ngpio) 4186 return false; 4187 4188 return !test_bit(FLAG_TRANSITORY, &gc->gpiodev->descs[offset].flags); 4189 } 4190 EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent); 4191 4192 /** 4193 * gpiod_get_raw_value_cansleep() - return a gpio's raw value 4194 * @desc: gpio whose value will be returned 4195 * 4196 * Returns: 4197 * The GPIO's raw value, i.e. the value of the physical line disregarding 4198 * its ACTIVE_LOW status, or negative errno on failure. 4199 * 4200 * This function is to be called from contexts that can sleep. 4201 */ 4202 int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) 4203 { 4204 might_sleep(); 4205 VALIDATE_DESC(desc); 4206 return gpiod_get_raw_value_commit(desc); 4207 } 4208 EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); 4209 4210 /** 4211 * gpiod_get_value_cansleep() - return a gpio's value 4212 * @desc: gpio whose value will be returned 4213 * 4214 * Returns: 4215 * The GPIO's logical value, i.e. taking the ACTIVE_LOW status into 4216 * account, or negative errno on failure. 4217 * 4218 * This function is to be called from contexts that can sleep. 4219 */ 4220 int gpiod_get_value_cansleep(const struct gpio_desc *desc) 4221 { 4222 int value; 4223 4224 might_sleep(); 4225 VALIDATE_DESC(desc); 4226 value = gpiod_get_raw_value_commit(desc); 4227 if (value < 0) 4228 return value; 4229 4230 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 4231 value = !value; 4232 4233 return value; 4234 } 4235 EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); 4236 4237 /** 4238 * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs 4239 * @array_size: number of elements in the descriptor array / value bitmap 4240 * @desc_array: array of GPIO descriptors whose values will be read 4241 * @array_info: information on applicability of fast bitmap processing path 4242 * @value_bitmap: bitmap to store the read values 4243 * 4244 * Read the raw values of the GPIOs, i.e. the values of the physical lines 4245 * without regard for their ACTIVE_LOW status. 4246 * 4247 * This function is to be called from contexts that can sleep. 4248 * 4249 * Returns: 4250 * 0 on success, or negative errno on failure. 4251 */ 4252 int gpiod_get_raw_array_value_cansleep(unsigned int array_size, 4253 struct gpio_desc **desc_array, 4254 struct gpio_array *array_info, 4255 unsigned long *value_bitmap) 4256 { 4257 might_sleep(); 4258 if (!desc_array) 4259 return -EINVAL; 4260 return gpiod_get_array_value_complex(true, true, array_size, 4261 desc_array, array_info, 4262 value_bitmap); 4263 } 4264 EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep); 4265 4266 /** 4267 * gpiod_get_array_value_cansleep() - read values from an array of GPIOs 4268 * @array_size: number of elements in the descriptor array / value bitmap 4269 * @desc_array: array of GPIO descriptors whose values will be read 4270 * @array_info: information on applicability of fast bitmap processing path 4271 * @value_bitmap: bitmap to store the read values 4272 * 4273 * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 4274 * into account. 4275 * 4276 * This function is to be called from contexts that can sleep. 4277 * 4278 * Returns: 4279 * 0 on success, or negative errno on failure. 4280 */ 4281 int gpiod_get_array_value_cansleep(unsigned int array_size, 4282 struct gpio_desc **desc_array, 4283 struct gpio_array *array_info, 4284 unsigned long *value_bitmap) 4285 { 4286 might_sleep(); 4287 if (!desc_array) 4288 return -EINVAL; 4289 return gpiod_get_array_value_complex(false, true, array_size, 4290 desc_array, array_info, 4291 value_bitmap); 4292 } 4293 EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep); 4294 4295 /** 4296 * gpiod_set_raw_value_cansleep() - assign a gpio's raw value 4297 * @desc: gpio whose value will be assigned 4298 * @value: value to assign 4299 * 4300 * Set the raw value of the GPIO, i.e. the value of its physical line without 4301 * regard for its ACTIVE_LOW status. 4302 * 4303 * This function is to be called from contexts that can sleep. 4304 * 4305 * Returns: 4306 * 0 on success, negative error number on failure. 4307 */ 4308 int gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) 4309 { 4310 might_sleep(); 4311 VALIDATE_DESC(desc); 4312 return gpiod_set_raw_value_commit(desc, value); 4313 } 4314 EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); 4315 4316 /** 4317 * gpiod_set_value_cansleep() - assign a gpio's value 4318 * @desc: gpio whose value will be assigned 4319 * @value: value to assign 4320 * 4321 * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into 4322 * account 4323 * 4324 * This function is to be called from contexts that can sleep. 4325 * 4326 * Returns: 4327 * 0 on success, negative error number on failure. 4328 */ 4329 int gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 4330 { 4331 might_sleep(); 4332 VALIDATE_DESC(desc); 4333 return gpiod_set_value_nocheck(desc, value); 4334 } 4335 EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 4336 4337 /** 4338 * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs 4339 * @array_size: number of elements in the descriptor array / value bitmap 4340 * @desc_array: array of GPIO descriptors whose values will be assigned 4341 * @array_info: information on applicability of fast bitmap processing path 4342 * @value_bitmap: bitmap of values to assign 4343 * 4344 * Set the raw values of the GPIOs, i.e. the values of the physical lines 4345 * without regard for their ACTIVE_LOW status. 4346 * 4347 * This function is to be called from contexts that can sleep. 4348 * 4349 * Returns: 4350 * 0 on success, or negative errno on failure. 4351 */ 4352 int gpiod_set_raw_array_value_cansleep(unsigned int array_size, 4353 struct gpio_desc **desc_array, 4354 struct gpio_array *array_info, 4355 unsigned long *value_bitmap) 4356 { 4357 might_sleep(); 4358 if (!desc_array) 4359 return -EINVAL; 4360 return gpiod_set_array_value_complex(true, true, array_size, desc_array, 4361 array_info, value_bitmap); 4362 } 4363 EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep); 4364 4365 /** 4366 * gpiod_add_lookup_tables() - register GPIO device consumers 4367 * @tables: list of tables of consumers to register 4368 * @n: number of tables in the list 4369 */ 4370 void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) 4371 { 4372 unsigned int i; 4373 4374 guard(mutex)(&gpio_lookup_lock); 4375 4376 for (i = 0; i < n; i++) 4377 list_add_tail(&tables[i]->list, &gpio_lookup_list); 4378 } 4379 4380 /** 4381 * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs 4382 * @array_size: number of elements in the descriptor array / value bitmap 4383 * @desc_array: array of GPIO descriptors whose values will be assigned 4384 * @array_info: information on applicability of fast bitmap processing path 4385 * @value_bitmap: bitmap of values to assign 4386 * 4387 * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status 4388 * into account. 4389 * 4390 * This function is to be called from contexts that can sleep. 4391 * 4392 * Returns: 4393 * 0 on success, or negative errno on failure. 4394 */ 4395 int gpiod_set_array_value_cansleep(unsigned int array_size, 4396 struct gpio_desc **desc_array, 4397 struct gpio_array *array_info, 4398 unsigned long *value_bitmap) 4399 { 4400 might_sleep(); 4401 if (!desc_array) 4402 return -EINVAL; 4403 return gpiod_set_array_value_complex(false, true, array_size, 4404 desc_array, array_info, 4405 value_bitmap); 4406 } 4407 EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep); 4408 4409 void gpiod_line_state_notify(struct gpio_desc *desc, unsigned long action) 4410 { 4411 guard(read_lock_irqsave)(&desc->gdev->line_state_lock); 4412 4413 raw_notifier_call_chain(&desc->gdev->line_state_notifier, action, desc); 4414 } 4415 4416 /** 4417 * gpiod_add_lookup_table() - register GPIO device consumers 4418 * @table: table of consumers to register 4419 */ 4420 void gpiod_add_lookup_table(struct gpiod_lookup_table *table) 4421 { 4422 gpiod_add_lookup_tables(&table, 1); 4423 } 4424 EXPORT_SYMBOL_GPL(gpiod_add_lookup_table); 4425 4426 /** 4427 * gpiod_remove_lookup_table() - unregister GPIO device consumers 4428 * @table: table of consumers to unregister 4429 */ 4430 void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) 4431 { 4432 /* Nothing to remove */ 4433 if (!table) 4434 return; 4435 4436 guard(mutex)(&gpio_lookup_lock); 4437 4438 list_del(&table->list); 4439 } 4440 EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table); 4441 4442 /** 4443 * gpiod_add_hogs() - register a set of GPIO hogs from machine code 4444 * @hogs: table of gpio hog entries with a zeroed sentinel at the end 4445 */ 4446 void gpiod_add_hogs(struct gpiod_hog *hogs) 4447 { 4448 struct gpiod_hog *hog; 4449 4450 guard(mutex)(&gpio_machine_hogs_mutex); 4451 4452 for (hog = &hogs[0]; hog->chip_label; hog++) { 4453 list_add_tail(&hog->list, &gpio_machine_hogs); 4454 4455 /* 4456 * The chip may have been registered earlier, so check if it 4457 * exists and, if so, try to hog the line now. 4458 */ 4459 struct gpio_device *gdev __free(gpio_device_put) = 4460 gpio_device_find_by_label(hog->chip_label); 4461 if (gdev) 4462 gpiochip_machine_hog(gpio_device_get_chip(gdev), hog); 4463 } 4464 } 4465 EXPORT_SYMBOL_GPL(gpiod_add_hogs); 4466 4467 void gpiod_remove_hogs(struct gpiod_hog *hogs) 4468 { 4469 struct gpiod_hog *hog; 4470 4471 guard(mutex)(&gpio_machine_hogs_mutex); 4472 4473 for (hog = &hogs[0]; hog->chip_label; hog++) 4474 list_del(&hog->list); 4475 } 4476 EXPORT_SYMBOL_GPL(gpiod_remove_hogs); 4477 4478 static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) 4479 { 4480 const char *dev_id = dev ? dev_name(dev) : NULL; 4481 struct gpiod_lookup_table *table; 4482 4483 list_for_each_entry(table, &gpio_lookup_list, list) { 4484 if (table->dev_id && dev_id) { 4485 /* 4486 * Valid strings on both ends, must be identical to have 4487 * a match 4488 */ 4489 if (!strcmp(table->dev_id, dev_id)) 4490 return table; 4491 } else { 4492 /* 4493 * One of the pointers is NULL, so both must be to have 4494 * a match 4495 */ 4496 if (dev_id == table->dev_id) 4497 return table; 4498 } 4499 } 4500 4501 return NULL; 4502 } 4503 4504 static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 4505 unsigned int idx, unsigned long *flags) 4506 { 4507 struct gpio_desc *desc = ERR_PTR(-ENOENT); 4508 struct gpiod_lookup_table *table; 4509 struct gpiod_lookup *p; 4510 struct gpio_chip *gc; 4511 4512 guard(mutex)(&gpio_lookup_lock); 4513 4514 table = gpiod_find_lookup_table(dev); 4515 if (!table) 4516 return desc; 4517 4518 for (p = &table->table[0]; p->key; p++) { 4519 /* idx must always match exactly */ 4520 if (p->idx != idx) 4521 continue; 4522 4523 /* If the lookup entry has a con_id, require exact match */ 4524 if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) 4525 continue; 4526 4527 if (p->chip_hwnum == U16_MAX) { 4528 desc = gpio_name_to_desc(p->key); 4529 if (desc) { 4530 *flags = p->flags; 4531 return desc; 4532 } 4533 4534 dev_warn(dev, "cannot find GPIO line %s, deferring\n", 4535 p->key); 4536 return ERR_PTR(-EPROBE_DEFER); 4537 } 4538 4539 struct gpio_device *gdev __free(gpio_device_put) = 4540 gpio_device_find_by_label(p->key); 4541 if (!gdev) { 4542 /* 4543 * As the lookup table indicates a chip with 4544 * p->key should exist, assume it may 4545 * still appear later and let the interested 4546 * consumer be probed again or let the Deferred 4547 * Probe infrastructure handle the error. 4548 */ 4549 dev_warn(dev, "cannot find GPIO chip %s, deferring\n", 4550 p->key); 4551 return ERR_PTR(-EPROBE_DEFER); 4552 } 4553 4554 gc = gpio_device_get_chip(gdev); 4555 4556 if (gc->ngpio <= p->chip_hwnum) { 4557 dev_err(dev, 4558 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n", 4559 idx, p->chip_hwnum, gc->ngpio - 1, 4560 gc->label); 4561 return ERR_PTR(-EINVAL); 4562 } 4563 4564 desc = gpio_device_get_desc(gdev, p->chip_hwnum); 4565 *flags = p->flags; 4566 4567 return desc; 4568 } 4569 4570 return desc; 4571 } 4572 4573 static int platform_gpio_count(struct device *dev, const char *con_id) 4574 { 4575 struct gpiod_lookup_table *table; 4576 struct gpiod_lookup *p; 4577 unsigned int count = 0; 4578 4579 scoped_guard(mutex, &gpio_lookup_lock) { 4580 table = gpiod_find_lookup_table(dev); 4581 if (!table) 4582 return -ENOENT; 4583 4584 for (p = &table->table[0]; p->key; p++) { 4585 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || 4586 (!con_id && !p->con_id)) 4587 count++; 4588 } 4589 } 4590 4591 if (!count) 4592 return -ENOENT; 4593 4594 return count; 4595 } 4596 4597 static struct gpio_desc *gpiod_find_by_fwnode(struct fwnode_handle *fwnode, 4598 struct device *consumer, 4599 const char *con_id, 4600 unsigned int idx, 4601 enum gpiod_flags *flags, 4602 unsigned long *lookupflags) 4603 { 4604 const char *name = function_name_or_default(con_id); 4605 struct gpio_desc *desc = ERR_PTR(-ENOENT); 4606 4607 if (is_of_node(fwnode)) { 4608 dev_dbg(consumer, "using DT '%pfw' for '%s' GPIO lookup\n", fwnode, name); 4609 desc = of_find_gpio(to_of_node(fwnode), con_id, idx, lookupflags); 4610 } else if (is_acpi_node(fwnode)) { 4611 dev_dbg(consumer, "using ACPI '%pfw' for '%s' GPIO lookup\n", fwnode, name); 4612 desc = acpi_find_gpio(fwnode, con_id, idx, flags, lookupflags); 4613 } else if (is_software_node(fwnode)) { 4614 dev_dbg(consumer, "using swnode '%pfw' for '%s' GPIO lookup\n", fwnode, name); 4615 desc = swnode_find_gpio(fwnode, con_id, idx, lookupflags); 4616 } 4617 4618 return desc; 4619 } 4620 4621 struct gpio_desc *gpiod_find_and_request(struct device *consumer, 4622 struct fwnode_handle *fwnode, 4623 const char *con_id, 4624 unsigned int idx, 4625 enum gpiod_flags flags, 4626 const char *label, 4627 bool platform_lookup_allowed) 4628 { 4629 unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT; 4630 const char *name = function_name_or_default(con_id); 4631 /* 4632 * scoped_guard() is implemented as a for loop, meaning static 4633 * analyzers will complain about these two not being initialized. 4634 */ 4635 struct gpio_desc *desc = NULL; 4636 int ret = 0; 4637 4638 scoped_guard(srcu, &gpio_devices_srcu) { 4639 desc = gpiod_find_by_fwnode(fwnode, consumer, con_id, idx, 4640 &flags, &lookupflags); 4641 if (gpiod_not_found(desc) && platform_lookup_allowed) { 4642 /* 4643 * Either we are not using DT or ACPI, or their lookup 4644 * did not return a result. In that case, use platform 4645 * lookup as a fallback. 4646 */ 4647 dev_dbg(consumer, 4648 "using lookup tables for GPIO lookup\n"); 4649 desc = gpiod_find(consumer, con_id, idx, &lookupflags); 4650 } 4651 4652 if (IS_ERR(desc)) { 4653 dev_dbg(consumer, "No GPIO consumer %s found\n", name); 4654 return desc; 4655 } 4656 4657 /* 4658 * If a connection label was passed use that, else attempt to use 4659 * the device name as label 4660 */ 4661 ret = gpiod_request(desc, label); 4662 } 4663 if (ret) { 4664 if (!(ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 4665 return ERR_PTR(ret); 4666 4667 /* 4668 * This happens when there are several consumers for 4669 * the same GPIO line: we just return here without 4670 * further initialization. It is a bit of a hack. 4671 * This is necessary to support fixed regulators. 4672 * 4673 * FIXME: Make this more sane and safe. 4674 */ 4675 dev_info(consumer, "nonexclusive access to GPIO for %s\n", name); 4676 return desc; 4677 } 4678 4679 ret = gpiod_configure_flags(desc, con_id, lookupflags, flags); 4680 if (ret < 0) { 4681 gpiod_put(desc); 4682 dev_err(consumer, "setup of GPIO %s failed: %d\n", name, ret); 4683 return ERR_PTR(ret); 4684 } 4685 4686 gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED); 4687 4688 return desc; 4689 } 4690 4691 /** 4692 * fwnode_gpiod_get_index - obtain a GPIO from firmware node 4693 * @fwnode: handle of the firmware node 4694 * @con_id: function within the GPIO consumer 4695 * @index: index of the GPIO to obtain for the consumer 4696 * @flags: GPIO initialization flags 4697 * @label: label to attach to the requested GPIO 4698 * 4699 * This function can be used for drivers that get their configuration 4700 * from opaque firmware. 4701 * 4702 * The function properly finds the corresponding GPIO using whatever is the 4703 * underlying firmware interface and then makes sure that the GPIO 4704 * descriptor is requested before it is returned to the caller. 4705 * 4706 * Returns: 4707 * On successful request the GPIO pin is configured in accordance with 4708 * provided @flags. 4709 * 4710 * In case of error an ERR_PTR() is returned. 4711 */ 4712 struct gpio_desc *fwnode_gpiod_get_index(struct fwnode_handle *fwnode, 4713 const char *con_id, 4714 int index, 4715 enum gpiod_flags flags, 4716 const char *label) 4717 { 4718 return gpiod_find_and_request(NULL, fwnode, con_id, index, flags, label, false); 4719 } 4720 EXPORT_SYMBOL_GPL(fwnode_gpiod_get_index); 4721 4722 /** 4723 * gpiod_count - return the number of GPIOs associated with a device / function 4724 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4725 * @con_id: function within the GPIO consumer 4726 * 4727 * Returns: 4728 * The number of GPIOs associated with a device / function or -ENOENT if no 4729 * GPIO has been assigned to the requested function. 4730 */ 4731 int gpiod_count(struct device *dev, const char *con_id) 4732 { 4733 const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 4734 int count = -ENOENT; 4735 4736 if (is_of_node(fwnode)) 4737 count = of_gpio_count(fwnode, con_id); 4738 else if (is_acpi_node(fwnode)) 4739 count = acpi_gpio_count(fwnode, con_id); 4740 else if (is_software_node(fwnode)) 4741 count = swnode_gpio_count(fwnode, con_id); 4742 4743 if (count < 0) 4744 count = platform_gpio_count(dev, con_id); 4745 4746 return count; 4747 } 4748 EXPORT_SYMBOL_GPL(gpiod_count); 4749 4750 /** 4751 * gpiod_get - obtain a GPIO for a given GPIO function 4752 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4753 * @con_id: function within the GPIO consumer 4754 * @flags: optional GPIO initialization flags 4755 * 4756 * Returns: 4757 * The GPIO descriptor corresponding to the function @con_id of device 4758 * dev, -ENOENT if no GPIO has been assigned to the requested function, or 4759 * another IS_ERR() code if an error occurred while trying to acquire the GPIO. 4760 */ 4761 struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id, 4762 enum gpiod_flags flags) 4763 { 4764 return gpiod_get_index(dev, con_id, 0, flags); 4765 } 4766 EXPORT_SYMBOL_GPL(gpiod_get); 4767 4768 /** 4769 * gpiod_get_optional - obtain an optional GPIO for a given GPIO function 4770 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4771 * @con_id: function within the GPIO consumer 4772 * @flags: optional GPIO initialization flags 4773 * 4774 * This is equivalent to gpiod_get(), except that when no GPIO was assigned to 4775 * the requested function it will return NULL. This is convenient for drivers 4776 * that need to handle optional GPIOs. 4777 * 4778 * Returns: 4779 * The GPIO descriptor corresponding to the function @con_id of device 4780 * dev, NULL if no GPIO has been assigned to the requested function, or 4781 * another IS_ERR() code if an error occurred while trying to acquire the GPIO. 4782 */ 4783 struct gpio_desc *__must_check gpiod_get_optional(struct device *dev, 4784 const char *con_id, 4785 enum gpiod_flags flags) 4786 { 4787 return gpiod_get_index_optional(dev, con_id, 0, flags); 4788 } 4789 EXPORT_SYMBOL_GPL(gpiod_get_optional); 4790 4791 4792 /** 4793 * gpiod_configure_flags - helper function to configure a given GPIO 4794 * @desc: gpio whose value will be assigned 4795 * @con_id: function within the GPIO consumer 4796 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 4797 * of_find_gpio() or of_get_gpio_hog() 4798 * @dflags: gpiod_flags - optional GPIO initialization flags 4799 * 4800 * Returns: 4801 * 0 on success, -ENOENT if no GPIO has been assigned to the 4802 * requested function and/or index, or another IS_ERR() code if an error 4803 * occurred while trying to acquire the GPIO. 4804 */ 4805 int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, 4806 unsigned long lflags, enum gpiod_flags dflags) 4807 { 4808 const char *name = function_name_or_default(con_id); 4809 int ret; 4810 4811 if (lflags & GPIO_ACTIVE_LOW) 4812 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 4813 4814 if (lflags & GPIO_OPEN_DRAIN) 4815 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 4816 else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) { 4817 /* 4818 * This enforces open drain mode from the consumer side. 4819 * This is necessary for some busses like I2C, but the lookup 4820 * should *REALLY* have specified them as open drain in the 4821 * first place, so print a little warning here. 4822 */ 4823 set_bit(FLAG_OPEN_DRAIN, &desc->flags); 4824 gpiod_warn(desc, 4825 "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n"); 4826 } 4827 4828 if (lflags & GPIO_OPEN_SOURCE) 4829 set_bit(FLAG_OPEN_SOURCE, &desc->flags); 4830 4831 if (((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) || 4832 ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DISABLE)) || 4833 ((lflags & GPIO_PULL_DOWN) && (lflags & GPIO_PULL_DISABLE))) { 4834 gpiod_err(desc, 4835 "multiple pull-up, pull-down or pull-disable enabled, invalid configuration\n"); 4836 return -EINVAL; 4837 } 4838 4839 if (lflags & GPIO_PULL_UP) 4840 set_bit(FLAG_PULL_UP, &desc->flags); 4841 else if (lflags & GPIO_PULL_DOWN) 4842 set_bit(FLAG_PULL_DOWN, &desc->flags); 4843 else if (lflags & GPIO_PULL_DISABLE) 4844 set_bit(FLAG_BIAS_DISABLE, &desc->flags); 4845 4846 ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY)); 4847 if (ret < 0) 4848 return ret; 4849 4850 /* No particular flag request, return here... */ 4851 if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) { 4852 gpiod_dbg(desc, "no flags found for GPIO %s\n", name); 4853 return 0; 4854 } 4855 4856 /* Process flags */ 4857 if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) 4858 ret = gpiod_direction_output_nonotify(desc, 4859 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL)); 4860 else 4861 ret = gpiod_direction_input_nonotify(desc); 4862 4863 return ret; 4864 } 4865 4866 /** 4867 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function 4868 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4869 * @con_id: function within the GPIO consumer 4870 * @idx: index of the GPIO to obtain in the consumer 4871 * @flags: optional GPIO initialization flags 4872 * 4873 * This variant of gpiod_get() allows to access GPIOs other than the first 4874 * defined one for functions that define several GPIOs. 4875 * 4876 * Returns: 4877 * A valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the 4878 * requested function and/or index, or another IS_ERR() code if an error 4879 * occurred while trying to acquire the GPIO. 4880 */ 4881 struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 4882 const char *con_id, 4883 unsigned int idx, 4884 enum gpiod_flags flags) 4885 { 4886 struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL; 4887 const char *devname = dev ? dev_name(dev) : "?"; 4888 const char *label = con_id ?: devname; 4889 4890 return gpiod_find_and_request(dev, fwnode, con_id, idx, flags, label, true); 4891 } 4892 EXPORT_SYMBOL_GPL(gpiod_get_index); 4893 4894 /** 4895 * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO 4896 * function 4897 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4898 * @con_id: function within the GPIO consumer 4899 * @index: index of the GPIO to obtain in the consumer 4900 * @flags: optional GPIO initialization flags 4901 * 4902 * This is equivalent to gpiod_get_index(), except that when no GPIO with the 4903 * specified index was assigned to the requested function it will return NULL. 4904 * This is convenient for drivers that need to handle optional GPIOs. 4905 * 4906 * Returns: 4907 * A valid GPIO descriptor, NULL if no GPIO has been assigned to the 4908 * requested function and/or index, or another IS_ERR() code if an error 4909 * occurred while trying to acquire the GPIO. 4910 */ 4911 struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev, 4912 const char *con_id, 4913 unsigned int index, 4914 enum gpiod_flags flags) 4915 { 4916 struct gpio_desc *desc; 4917 4918 desc = gpiod_get_index(dev, con_id, index, flags); 4919 if (gpiod_not_found(desc)) 4920 return NULL; 4921 4922 return desc; 4923 } 4924 EXPORT_SYMBOL_GPL(gpiod_get_index_optional); 4925 4926 /** 4927 * gpiod_hog - Hog the specified GPIO desc given the provided flags 4928 * @desc: gpio whose value will be assigned 4929 * @name: gpio line name 4930 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from 4931 * of_find_gpio() or of_get_gpio_hog() 4932 * @dflags: gpiod_flags - optional GPIO initialization flags 4933 * 4934 * Returns: 4935 * 0 on success, or negative errno on failure. 4936 */ 4937 int gpiod_hog(struct gpio_desc *desc, const char *name, 4938 unsigned long lflags, enum gpiod_flags dflags) 4939 { 4940 struct gpio_device *gdev = desc->gdev; 4941 struct gpio_desc *local_desc; 4942 int hwnum; 4943 int ret; 4944 4945 CLASS(gpio_chip_guard, guard)(desc); 4946 if (!guard.gc) 4947 return -ENODEV; 4948 4949 if (test_and_set_bit(FLAG_IS_HOGGED, &desc->flags)) 4950 return 0; 4951 4952 hwnum = gpio_chip_hwgpio(desc); 4953 4954 local_desc = gpiochip_request_own_desc(guard.gc, hwnum, name, 4955 lflags, dflags); 4956 if (IS_ERR(local_desc)) { 4957 clear_bit(FLAG_IS_HOGGED, &desc->flags); 4958 ret = PTR_ERR(local_desc); 4959 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n", 4960 name, gdev->label, hwnum, ret); 4961 return ret; 4962 } 4963 4964 gpiod_dbg(desc, "hogged as %s/%s\n", 4965 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input", 4966 (dflags & GPIOD_FLAGS_BIT_DIR_OUT) ? 4967 str_high_low(dflags & GPIOD_FLAGS_BIT_DIR_VAL) : "?"); 4968 4969 return 0; 4970 } 4971 4972 /** 4973 * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog 4974 * @gc: gpio chip to act on 4975 */ 4976 static void gpiochip_free_hogs(struct gpio_chip *gc) 4977 { 4978 struct gpio_desc *desc; 4979 4980 for_each_gpio_desc_with_flag(gc, desc, FLAG_IS_HOGGED) 4981 gpiochip_free_own_desc(desc); 4982 } 4983 4984 /** 4985 * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function 4986 * @dev: GPIO consumer, can be NULL for system-global GPIOs 4987 * @con_id: function within the GPIO consumer 4988 * @flags: optional GPIO initialization flags 4989 * 4990 * This function acquires all the GPIOs defined under a given function. 4991 * 4992 * Returns: 4993 * The GPIO descriptors corresponding to the function @con_id of device 4994 * dev, -ENOENT if no GPIO has been assigned to the requested function, 4995 * or another IS_ERR() code if an error occurred while trying to acquire 4996 * the GPIOs. 4997 */ 4998 struct gpio_descs *__must_check gpiod_get_array(struct device *dev, 4999 const char *con_id, 5000 enum gpiod_flags flags) 5001 { 5002 struct gpio_desc *desc; 5003 struct gpio_descs *descs; 5004 struct gpio_device *gdev; 5005 struct gpio_array *array_info = NULL; 5006 int count, bitmap_size; 5007 unsigned long dflags; 5008 size_t descs_size; 5009 5010 count = gpiod_count(dev, con_id); 5011 if (count < 0) 5012 return ERR_PTR(count); 5013 5014 descs_size = struct_size(descs, desc, count); 5015 descs = kzalloc(descs_size, GFP_KERNEL); 5016 if (!descs) 5017 return ERR_PTR(-ENOMEM); 5018 5019 for (descs->ndescs = 0; descs->ndescs < count; descs->ndescs++) { 5020 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags); 5021 if (IS_ERR(desc)) { 5022 gpiod_put_array(descs); 5023 return ERR_CAST(desc); 5024 } 5025 5026 descs->desc[descs->ndescs] = desc; 5027 5028 gdev = gpiod_to_gpio_device(desc); 5029 /* 5030 * If pin hardware number of array member 0 is also 0, select 5031 * its chip as a candidate for fast bitmap processing path. 5032 */ 5033 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) { 5034 struct gpio_descs *array; 5035 5036 bitmap_size = BITS_TO_LONGS(gdev->ngpio > count ? 5037 gdev->ngpio : count); 5038 5039 array = krealloc(descs, descs_size + 5040 struct_size(array_info, invert_mask, 3 * bitmap_size), 5041 GFP_KERNEL | __GFP_ZERO); 5042 if (!array) { 5043 gpiod_put_array(descs); 5044 return ERR_PTR(-ENOMEM); 5045 } 5046 5047 descs = array; 5048 5049 array_info = (void *)descs + descs_size; 5050 array_info->get_mask = array_info->invert_mask + 5051 bitmap_size; 5052 array_info->set_mask = array_info->get_mask + 5053 bitmap_size; 5054 5055 array_info->desc = descs->desc; 5056 array_info->size = count; 5057 array_info->gdev = gdev; 5058 bitmap_set(array_info->get_mask, descs->ndescs, 5059 count - descs->ndescs); 5060 bitmap_set(array_info->set_mask, descs->ndescs, 5061 count - descs->ndescs); 5062 descs->info = array_info; 5063 } 5064 5065 /* If there is no cache for fast bitmap processing path, continue */ 5066 if (!array_info) 5067 continue; 5068 5069 /* Unmark array members which don't belong to the 'fast' chip */ 5070 if (array_info->gdev != gdev) { 5071 __clear_bit(descs->ndescs, array_info->get_mask); 5072 __clear_bit(descs->ndescs, array_info->set_mask); 5073 } 5074 /* 5075 * Detect array members which belong to the 'fast' chip 5076 * but their pins are not in hardware order. 5077 */ 5078 else if (gpio_chip_hwgpio(desc) != descs->ndescs) { 5079 /* 5080 * Don't use fast path if all array members processed so 5081 * far belong to the same chip as this one but its pin 5082 * hardware number is different from its array index. 5083 */ 5084 if (bitmap_full(array_info->get_mask, descs->ndescs)) { 5085 array_info = NULL; 5086 } else { 5087 __clear_bit(descs->ndescs, 5088 array_info->get_mask); 5089 __clear_bit(descs->ndescs, 5090 array_info->set_mask); 5091 } 5092 } else { 5093 dflags = READ_ONCE(desc->flags); 5094 /* Exclude open drain or open source from fast output */ 5095 if (test_bit(FLAG_OPEN_DRAIN, &dflags) || 5096 test_bit(FLAG_OPEN_SOURCE, &dflags)) 5097 __clear_bit(descs->ndescs, 5098 array_info->set_mask); 5099 /* Identify 'fast' pins which require invertion */ 5100 if (gpiod_is_active_low(desc)) 5101 __set_bit(descs->ndescs, 5102 array_info->invert_mask); 5103 } 5104 } 5105 if (array_info) 5106 dev_dbg(dev, 5107 "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n", 5108 array_info->gdev->label, array_info->size, 5109 *array_info->get_mask, *array_info->set_mask, 5110 *array_info->invert_mask); 5111 return descs; 5112 } 5113 EXPORT_SYMBOL_GPL(gpiod_get_array); 5114 5115 /** 5116 * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO 5117 * function 5118 * @dev: GPIO consumer, can be NULL for system-global GPIOs 5119 * @con_id: function within the GPIO consumer 5120 * @flags: optional GPIO initialization flags 5121 * 5122 * This is equivalent to gpiod_get_array(), except that when no GPIO was 5123 * assigned to the requested function it will return NULL. 5124 * 5125 * Returns: 5126 * The GPIO descriptors corresponding to the function @con_id of device 5127 * dev, NULL if no GPIO has been assigned to the requested function, 5128 * or another IS_ERR() code if an error occurred while trying to acquire 5129 * the GPIOs. 5130 */ 5131 struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev, 5132 const char *con_id, 5133 enum gpiod_flags flags) 5134 { 5135 struct gpio_descs *descs; 5136 5137 descs = gpiod_get_array(dev, con_id, flags); 5138 if (gpiod_not_found(descs)) 5139 return NULL; 5140 5141 return descs; 5142 } 5143 EXPORT_SYMBOL_GPL(gpiod_get_array_optional); 5144 5145 /** 5146 * gpiod_put - dispose of a GPIO descriptor 5147 * @desc: GPIO descriptor to dispose of 5148 * 5149 * No descriptor can be used after gpiod_put() has been called on it. 5150 */ 5151 void gpiod_put(struct gpio_desc *desc) 5152 { 5153 gpiod_free(desc); 5154 } 5155 EXPORT_SYMBOL_GPL(gpiod_put); 5156 5157 /** 5158 * gpiod_put_array - dispose of multiple GPIO descriptors 5159 * @descs: struct gpio_descs containing an array of descriptors 5160 */ 5161 void gpiod_put_array(struct gpio_descs *descs) 5162 { 5163 unsigned int i; 5164 5165 for (i = 0; i < descs->ndescs; i++) 5166 gpiod_put(descs->desc[i]); 5167 5168 kfree(descs); 5169 } 5170 EXPORT_SYMBOL_GPL(gpiod_put_array); 5171 5172 static int gpio_stub_drv_probe(struct device *dev) 5173 { 5174 /* 5175 * The DT node of some GPIO chips have a "compatible" property, but 5176 * never have a struct device added and probed by a driver to register 5177 * the GPIO chip with gpiolib. In such cases, fw_devlink=on will cause 5178 * the consumers of the GPIO chip to get probe deferred forever because 5179 * they will be waiting for a device associated with the GPIO chip 5180 * firmware node to get added and bound to a driver. 5181 * 5182 * To allow these consumers to probe, we associate the struct 5183 * gpio_device of the GPIO chip with the firmware node and then simply 5184 * bind it to this stub driver. 5185 */ 5186 return 0; 5187 } 5188 5189 static struct device_driver gpio_stub_drv = { 5190 .name = "gpio_stub_drv", 5191 .bus = &gpio_bus_type, 5192 .probe = gpio_stub_drv_probe, 5193 }; 5194 5195 static int __init gpiolib_dev_init(void) 5196 { 5197 int ret; 5198 5199 /* Register GPIO sysfs bus */ 5200 ret = bus_register(&gpio_bus_type); 5201 if (ret < 0) { 5202 pr_err("gpiolib: could not register GPIO bus type\n"); 5203 return ret; 5204 } 5205 5206 ret = driver_register(&gpio_stub_drv); 5207 if (ret < 0) { 5208 pr_err("gpiolib: could not register GPIO stub driver\n"); 5209 bus_unregister(&gpio_bus_type); 5210 return ret; 5211 } 5212 5213 ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, GPIOCHIP_NAME); 5214 if (ret < 0) { 5215 pr_err("gpiolib: failed to allocate char dev region\n"); 5216 driver_unregister(&gpio_stub_drv); 5217 bus_unregister(&gpio_bus_type); 5218 return ret; 5219 } 5220 5221 gpiolib_initialized = true; 5222 gpiochip_setup_devs(); 5223 5224 #if IS_ENABLED(CONFIG_OF_DYNAMIC) && IS_ENABLED(CONFIG_OF_GPIO) 5225 WARN_ON(of_reconfig_notifier_register(&gpio_of_notifier)); 5226 #endif /* CONFIG_OF_DYNAMIC && CONFIG_OF_GPIO */ 5227 5228 return ret; 5229 } 5230 core_initcall(gpiolib_dev_init); 5231 5232 #ifdef CONFIG_DEBUG_FS 5233 5234 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev) 5235 { 5236 bool active_low, is_irq, is_out; 5237 unsigned int gpio = gdev->base; 5238 struct gpio_desc *desc; 5239 struct gpio_chip *gc; 5240 unsigned long flags; 5241 int value; 5242 5243 guard(srcu)(&gdev->srcu); 5244 5245 gc = srcu_dereference(gdev->chip, &gdev->srcu); 5246 if (!gc) { 5247 seq_puts(s, "Underlying GPIO chip is gone\n"); 5248 return; 5249 } 5250 5251 for_each_gpio_desc(gc, desc) { 5252 guard(srcu)(&desc->gdev->desc_srcu); 5253 flags = READ_ONCE(desc->flags); 5254 is_irq = test_bit(FLAG_USED_AS_IRQ, &flags); 5255 if (is_irq || test_bit(FLAG_REQUESTED, &flags)) { 5256 gpiod_get_direction(desc); 5257 is_out = test_bit(FLAG_IS_OUT, &flags); 5258 value = gpio_chip_get_value(gc, desc); 5259 active_low = test_bit(FLAG_ACTIVE_LOW, &flags); 5260 seq_printf(s, " gpio-%-3u (%-20.20s|%-20.20s) %s %s %s%s\n", 5261 gpio, desc->name ?: "", gpiod_get_label(desc), 5262 is_out ? "out" : "in ", 5263 value >= 0 ? str_hi_lo(value) : "? ", 5264 is_irq ? "IRQ " : "", 5265 active_low ? "ACTIVE LOW" : ""); 5266 } else if (desc->name) { 5267 seq_printf(s, " gpio-%-3u (%-20.20s)\n", gpio, desc->name); 5268 } 5269 5270 gpio++; 5271 } 5272 } 5273 5274 struct gpiolib_seq_priv { 5275 bool newline; 5276 int idx; 5277 }; 5278 5279 static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) 5280 { 5281 struct gpiolib_seq_priv *priv; 5282 struct gpio_device *gdev; 5283 loff_t index = *pos; 5284 5285 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 5286 if (!priv) 5287 return NULL; 5288 5289 s->private = priv; 5290 if (*pos > 0) 5291 priv->newline = true; 5292 priv->idx = srcu_read_lock(&gpio_devices_srcu); 5293 5294 list_for_each_entry_srcu(gdev, &gpio_devices, list, 5295 srcu_read_lock_held(&gpio_devices_srcu)) { 5296 if (index-- == 0) 5297 return gdev; 5298 } 5299 5300 return NULL; 5301 } 5302 5303 static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) 5304 { 5305 struct gpiolib_seq_priv *priv = s->private; 5306 struct gpio_device *gdev = v, *next; 5307 5308 next = list_entry_rcu(gdev->list.next, struct gpio_device, list); 5309 gdev = &next->list == &gpio_devices ? NULL : next; 5310 priv->newline = true; 5311 ++*pos; 5312 5313 return gdev; 5314 } 5315 5316 static void gpiolib_seq_stop(struct seq_file *s, void *v) 5317 { 5318 struct gpiolib_seq_priv *priv = s->private; 5319 5320 srcu_read_unlock(&gpio_devices_srcu, priv->idx); 5321 kfree(priv); 5322 } 5323 5324 static int gpiolib_seq_show(struct seq_file *s, void *v) 5325 { 5326 struct gpiolib_seq_priv *priv = s->private; 5327 struct gpio_device *gdev = v; 5328 struct gpio_chip *gc; 5329 struct device *parent; 5330 5331 if (priv->newline) 5332 seq_putc(s, '\n'); 5333 5334 guard(srcu)(&gdev->srcu); 5335 5336 gc = srcu_dereference(gdev->chip, &gdev->srcu); 5337 if (!gc) { 5338 seq_printf(s, "%s: (dangling chip)\n", dev_name(&gdev->dev)); 5339 return 0; 5340 } 5341 5342 seq_printf(s, "%s: GPIOs %u-%u", dev_name(&gdev->dev), gdev->base, 5343 gdev->base + gdev->ngpio - 1); 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