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