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