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