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