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