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