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