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