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/device.h> 7 #include <linux/err.h> 8 #include <linux/debugfs.h> 9 #include <linux/seq_file.h> 10 #include <linux/gpio.h> 11 #include <linux/idr.h> 12 13 14 /* Optional implementation infrastructure for GPIO interfaces. 15 * 16 * Platforms may want to use this if they tend to use very many GPIOs 17 * that aren't part of a System-On-Chip core; or across I2C/SPI/etc. 18 * 19 * When kernel footprint or instruction count is an issue, simpler 20 * implementations may be preferred. The GPIO programming interface 21 * allows for inlining speed-critical get/set operations for common 22 * cases, so that access to SOC-integrated GPIOs can sometimes cost 23 * only an instruction or two per bit. 24 */ 25 26 27 /* When debugging, extend minimal trust to callers and platform code. 28 * Also emit diagnostic messages that may help initial bringup, when 29 * board setup or driver bugs are most common. 30 * 31 * Otherwise, minimize overhead in what may be bitbanging codepaths. 32 */ 33 #ifdef DEBUG 34 #define extra_checks 1 35 #else 36 #define extra_checks 0 37 #endif 38 39 /* gpio_lock prevents conflicts during gpio_desc[] table updates. 40 * While any GPIO is requested, its gpio_chip is not removable; 41 * each GPIO's "requested" flag serves as a lock and refcount. 42 */ 43 static DEFINE_SPINLOCK(gpio_lock); 44 45 struct gpio_desc { 46 struct gpio_chip *chip; 47 unsigned long flags; 48 /* flag symbols are bit numbers */ 49 #define FLAG_REQUESTED 0 50 #define FLAG_IS_OUT 1 51 #define FLAG_RESERVED 2 52 #define FLAG_EXPORT 3 /* protected by sysfs_lock */ 53 #define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ 54 #define FLAG_TRIG_FALL 5 /* trigger on falling edge */ 55 #define FLAG_TRIG_RISE 6 /* trigger on rising edge */ 56 #define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */ 57 58 #define PDESC_ID_SHIFT 16 /* add new flags before this one */ 59 60 #define GPIO_FLAGS_MASK ((1 << PDESC_ID_SHIFT) - 1) 61 #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) 62 63 #ifdef CONFIG_DEBUG_FS 64 const char *label; 65 #endif 66 }; 67 static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; 68 69 #ifdef CONFIG_GPIO_SYSFS 70 struct poll_desc { 71 struct work_struct work; 72 struct sysfs_dirent *value_sd; 73 }; 74 75 static struct idr pdesc_idr; 76 #endif 77 78 static inline void desc_set_label(struct gpio_desc *d, const char *label) 79 { 80 #ifdef CONFIG_DEBUG_FS 81 d->label = label; 82 #endif 83 } 84 85 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised 86 * when setting direction, and otherwise illegal. Until board setup code 87 * and drivers use explicit requests everywhere (which won't happen when 88 * those calls have no teeth) we can't avoid autorequesting. This nag 89 * message should motivate switching to explicit requests... so should 90 * the weaker cleanup after faults, compared to gpio_request(). 91 * 92 * NOTE: the autorequest mechanism is going away; at this point it's 93 * only "legal" in the sense that (old) code using it won't break yet, 94 * but instead only triggers a WARN() stack dump. 95 */ 96 static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) 97 { 98 const struct gpio_chip *chip = desc->chip; 99 const int gpio = chip->base + offset; 100 101 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, 102 "autorequest GPIO-%d\n", gpio)) { 103 if (!try_module_get(chip->owner)) { 104 pr_err("GPIO-%d: module can't be gotten \n", gpio); 105 clear_bit(FLAG_REQUESTED, &desc->flags); 106 /* lose */ 107 return -EIO; 108 } 109 desc_set_label(desc, "[auto]"); 110 /* caller must chip->request() w/o spinlock */ 111 if (chip->request) 112 return 1; 113 } 114 return 0; 115 } 116 117 /* caller holds gpio_lock *OR* gpio is marked as requested */ 118 static inline struct gpio_chip *gpio_to_chip(unsigned gpio) 119 { 120 return gpio_desc[gpio].chip; 121 } 122 123 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 124 static int gpiochip_find_base(int ngpio) 125 { 126 int i; 127 int spare = 0; 128 int base = -ENOSPC; 129 130 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) { 131 struct gpio_desc *desc = &gpio_desc[i]; 132 struct gpio_chip *chip = desc->chip; 133 134 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) { 135 spare++; 136 if (spare == ngpio) { 137 base = i; 138 break; 139 } 140 } else { 141 spare = 0; 142 if (chip) 143 i -= chip->ngpio - 1; 144 } 145 } 146 147 if (gpio_is_valid(base)) 148 pr_debug("%s: found new base at %d\n", __func__, base); 149 return base; 150 } 151 152 /** 153 * gpiochip_reserve() - reserve range of gpios to use with platform code only 154 * @start: starting gpio number 155 * @ngpio: number of gpios to reserve 156 * Context: platform init, potentially before irqs or kmalloc will work 157 * 158 * Returns a negative errno if any gpio within the range is already reserved 159 * or registered, else returns zero as a success code. Use this function 160 * to mark a range of gpios as unavailable for dynamic gpio number allocation, 161 * for example because its driver support is not yet loaded. 162 */ 163 int __init gpiochip_reserve(int start, int ngpio) 164 { 165 int ret = 0; 166 unsigned long flags; 167 int i; 168 169 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1)) 170 return -EINVAL; 171 172 spin_lock_irqsave(&gpio_lock, flags); 173 174 for (i = start; i < start + ngpio; i++) { 175 struct gpio_desc *desc = &gpio_desc[i]; 176 177 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) { 178 ret = -EBUSY; 179 goto err; 180 } 181 182 set_bit(FLAG_RESERVED, &desc->flags); 183 } 184 185 pr_debug("%s: reserved gpios from %d to %d\n", 186 __func__, start, start + ngpio - 1); 187 err: 188 spin_unlock_irqrestore(&gpio_lock, flags); 189 190 return ret; 191 } 192 193 #ifdef CONFIG_GPIO_SYSFS 194 195 /* lock protects against unexport_gpio() being called while 196 * sysfs files are active. 197 */ 198 static DEFINE_MUTEX(sysfs_lock); 199 200 /* 201 * /sys/class/gpio/gpioN... only for GPIOs that are exported 202 * /direction 203 * * MAY BE OMITTED if kernel won't allow direction changes 204 * * is read/write as "in" or "out" 205 * * may also be written as "high" or "low", initializing 206 * output value as specified ("out" implies "low") 207 * /value 208 * * always readable, subject to hardware behavior 209 * * may be writable, as zero/nonzero 210 * /edge 211 * * configures behavior of poll(2) on /value 212 * * available only if pin can generate IRQs on input 213 * * is read/write as "none", "falling", "rising", or "both" 214 * /active_low 215 * * configures polarity of /value 216 * * is read/write as zero/nonzero 217 * * also affects existing and subsequent "falling" and "rising" 218 * /edge configuration 219 */ 220 221 static ssize_t gpio_direction_show(struct device *dev, 222 struct device_attribute *attr, char *buf) 223 { 224 const struct gpio_desc *desc = dev_get_drvdata(dev); 225 ssize_t status; 226 227 mutex_lock(&sysfs_lock); 228 229 if (!test_bit(FLAG_EXPORT, &desc->flags)) 230 status = -EIO; 231 else 232 status = sprintf(buf, "%s\n", 233 test_bit(FLAG_IS_OUT, &desc->flags) 234 ? "out" : "in"); 235 236 mutex_unlock(&sysfs_lock); 237 return status; 238 } 239 240 static ssize_t gpio_direction_store(struct device *dev, 241 struct device_attribute *attr, const char *buf, size_t size) 242 { 243 const struct gpio_desc *desc = dev_get_drvdata(dev); 244 unsigned gpio = desc - gpio_desc; 245 ssize_t status; 246 247 mutex_lock(&sysfs_lock); 248 249 if (!test_bit(FLAG_EXPORT, &desc->flags)) 250 status = -EIO; 251 else if (sysfs_streq(buf, "high")) 252 status = gpio_direction_output(gpio, 1); 253 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) 254 status = gpio_direction_output(gpio, 0); 255 else if (sysfs_streq(buf, "in")) 256 status = gpio_direction_input(gpio); 257 else 258 status = -EINVAL; 259 260 mutex_unlock(&sysfs_lock); 261 return status ? : size; 262 } 263 264 static /* const */ DEVICE_ATTR(direction, 0644, 265 gpio_direction_show, gpio_direction_store); 266 267 static ssize_t gpio_value_show(struct device *dev, 268 struct device_attribute *attr, char *buf) 269 { 270 const struct gpio_desc *desc = dev_get_drvdata(dev); 271 unsigned gpio = desc - gpio_desc; 272 ssize_t status; 273 274 mutex_lock(&sysfs_lock); 275 276 if (!test_bit(FLAG_EXPORT, &desc->flags)) { 277 status = -EIO; 278 } else { 279 int value; 280 281 value = !!gpio_get_value_cansleep(gpio); 282 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 283 value = !value; 284 285 status = sprintf(buf, "%d\n", value); 286 } 287 288 mutex_unlock(&sysfs_lock); 289 return status; 290 } 291 292 static ssize_t gpio_value_store(struct device *dev, 293 struct device_attribute *attr, const char *buf, size_t size) 294 { 295 const struct gpio_desc *desc = dev_get_drvdata(dev); 296 unsigned gpio = desc - gpio_desc; 297 ssize_t status; 298 299 mutex_lock(&sysfs_lock); 300 301 if (!test_bit(FLAG_EXPORT, &desc->flags)) 302 status = -EIO; 303 else if (!test_bit(FLAG_IS_OUT, &desc->flags)) 304 status = -EPERM; 305 else { 306 long value; 307 308 status = strict_strtol(buf, 0, &value); 309 if (status == 0) { 310 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 311 value = !value; 312 gpio_set_value_cansleep(gpio, value != 0); 313 status = size; 314 } 315 } 316 317 mutex_unlock(&sysfs_lock); 318 return status; 319 } 320 321 static const DEVICE_ATTR(value, 0644, 322 gpio_value_show, gpio_value_store); 323 324 static irqreturn_t gpio_sysfs_irq(int irq, void *priv) 325 { 326 struct work_struct *work = priv; 327 328 schedule_work(work); 329 return IRQ_HANDLED; 330 } 331 332 static void gpio_notify_sysfs(struct work_struct *work) 333 { 334 struct poll_desc *pdesc; 335 336 pdesc = container_of(work, struct poll_desc, work); 337 sysfs_notify_dirent(pdesc->value_sd); 338 } 339 340 static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, 341 unsigned long gpio_flags) 342 { 343 struct poll_desc *pdesc; 344 unsigned long irq_flags; 345 int ret, irq, id; 346 347 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) 348 return 0; 349 350 irq = gpio_to_irq(desc - gpio_desc); 351 if (irq < 0) 352 return -EIO; 353 354 id = desc->flags >> PDESC_ID_SHIFT; 355 pdesc = idr_find(&pdesc_idr, id); 356 if (pdesc) { 357 free_irq(irq, &pdesc->work); 358 cancel_work_sync(&pdesc->work); 359 } 360 361 desc->flags &= ~GPIO_TRIGGER_MASK; 362 363 if (!gpio_flags) { 364 ret = 0; 365 goto free_sd; 366 } 367 368 irq_flags = IRQF_SHARED; 369 if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) 370 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 371 IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; 372 if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) 373 irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? 374 IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; 375 376 if (!pdesc) { 377 pdesc = kmalloc(sizeof(*pdesc), GFP_KERNEL); 378 if (!pdesc) { 379 ret = -ENOMEM; 380 goto err_out; 381 } 382 383 do { 384 ret = -ENOMEM; 385 if (idr_pre_get(&pdesc_idr, GFP_KERNEL)) 386 ret = idr_get_new_above(&pdesc_idr, 387 pdesc, 1, &id); 388 } while (ret == -EAGAIN); 389 390 if (ret) 391 goto free_mem; 392 393 desc->flags &= GPIO_FLAGS_MASK; 394 desc->flags |= (unsigned long)id << PDESC_ID_SHIFT; 395 396 if (desc->flags >> PDESC_ID_SHIFT != id) { 397 ret = -ERANGE; 398 goto free_id; 399 } 400 401 pdesc->value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); 402 if (!pdesc->value_sd) { 403 ret = -ENODEV; 404 goto free_id; 405 } 406 INIT_WORK(&pdesc->work, gpio_notify_sysfs); 407 } 408 409 ret = request_irq(irq, gpio_sysfs_irq, irq_flags, 410 "gpiolib", &pdesc->work); 411 if (ret) 412 goto free_sd; 413 414 desc->flags |= gpio_flags; 415 return 0; 416 417 free_sd: 418 sysfs_put(pdesc->value_sd); 419 free_id: 420 idr_remove(&pdesc_idr, id); 421 desc->flags &= GPIO_FLAGS_MASK; 422 free_mem: 423 kfree(pdesc); 424 err_out: 425 return ret; 426 } 427 428 static const struct { 429 const char *name; 430 unsigned long flags; 431 } trigger_types[] = { 432 { "none", 0 }, 433 { "falling", BIT(FLAG_TRIG_FALL) }, 434 { "rising", BIT(FLAG_TRIG_RISE) }, 435 { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, 436 }; 437 438 static ssize_t gpio_edge_show(struct device *dev, 439 struct device_attribute *attr, char *buf) 440 { 441 const struct gpio_desc *desc = dev_get_drvdata(dev); 442 ssize_t status; 443 444 mutex_lock(&sysfs_lock); 445 446 if (!test_bit(FLAG_EXPORT, &desc->flags)) 447 status = -EIO; 448 else { 449 int i; 450 451 status = 0; 452 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 453 if ((desc->flags & GPIO_TRIGGER_MASK) 454 == trigger_types[i].flags) { 455 status = sprintf(buf, "%s\n", 456 trigger_types[i].name); 457 break; 458 } 459 } 460 461 mutex_unlock(&sysfs_lock); 462 return status; 463 } 464 465 static ssize_t gpio_edge_store(struct device *dev, 466 struct device_attribute *attr, const char *buf, size_t size) 467 { 468 struct gpio_desc *desc = dev_get_drvdata(dev); 469 ssize_t status; 470 int i; 471 472 for (i = 0; i < ARRAY_SIZE(trigger_types); i++) 473 if (sysfs_streq(trigger_types[i].name, buf)) 474 goto found; 475 return -EINVAL; 476 477 found: 478 mutex_lock(&sysfs_lock); 479 480 if (!test_bit(FLAG_EXPORT, &desc->flags)) 481 status = -EIO; 482 else { 483 status = gpio_setup_irq(desc, dev, trigger_types[i].flags); 484 if (!status) 485 status = size; 486 } 487 488 mutex_unlock(&sysfs_lock); 489 490 return status; 491 } 492 493 static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); 494 495 static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, 496 int value) 497 { 498 int status = 0; 499 500 if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) 501 return 0; 502 503 if (value) 504 set_bit(FLAG_ACTIVE_LOW, &desc->flags); 505 else 506 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); 507 508 /* reconfigure poll(2) support if enabled on one edge only */ 509 if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ 510 !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { 511 unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; 512 513 gpio_setup_irq(desc, dev, 0); 514 status = gpio_setup_irq(desc, dev, trigger_flags); 515 } 516 517 return status; 518 } 519 520 static ssize_t gpio_active_low_show(struct device *dev, 521 struct device_attribute *attr, char *buf) 522 { 523 const struct gpio_desc *desc = dev_get_drvdata(dev); 524 ssize_t status; 525 526 mutex_lock(&sysfs_lock); 527 528 if (!test_bit(FLAG_EXPORT, &desc->flags)) 529 status = -EIO; 530 else 531 status = sprintf(buf, "%d\n", 532 !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); 533 534 mutex_unlock(&sysfs_lock); 535 536 return status; 537 } 538 539 static ssize_t gpio_active_low_store(struct device *dev, 540 struct device_attribute *attr, const char *buf, size_t size) 541 { 542 struct gpio_desc *desc = dev_get_drvdata(dev); 543 ssize_t status; 544 545 mutex_lock(&sysfs_lock); 546 547 if (!test_bit(FLAG_EXPORT, &desc->flags)) { 548 status = -EIO; 549 } else { 550 long value; 551 552 status = strict_strtol(buf, 0, &value); 553 if (status == 0) 554 status = sysfs_set_active_low(desc, dev, value != 0); 555 } 556 557 mutex_unlock(&sysfs_lock); 558 559 return status ? : size; 560 } 561 562 static const DEVICE_ATTR(active_low, 0644, 563 gpio_active_low_show, gpio_active_low_store); 564 565 static const struct attribute *gpio_attrs[] = { 566 &dev_attr_value.attr, 567 &dev_attr_active_low.attr, 568 NULL, 569 }; 570 571 static const struct attribute_group gpio_attr_group = { 572 .attrs = (struct attribute **) gpio_attrs, 573 }; 574 575 /* 576 * /sys/class/gpio/gpiochipN/ 577 * /base ... matching gpio_chip.base (N) 578 * /label ... matching gpio_chip.label 579 * /ngpio ... matching gpio_chip.ngpio 580 */ 581 582 static ssize_t chip_base_show(struct device *dev, 583 struct device_attribute *attr, char *buf) 584 { 585 const struct gpio_chip *chip = dev_get_drvdata(dev); 586 587 return sprintf(buf, "%d\n", chip->base); 588 } 589 static DEVICE_ATTR(base, 0444, chip_base_show, NULL); 590 591 static ssize_t chip_label_show(struct device *dev, 592 struct device_attribute *attr, char *buf) 593 { 594 const struct gpio_chip *chip = dev_get_drvdata(dev); 595 596 return sprintf(buf, "%s\n", chip->label ? : ""); 597 } 598 static DEVICE_ATTR(label, 0444, chip_label_show, NULL); 599 600 static ssize_t chip_ngpio_show(struct device *dev, 601 struct device_attribute *attr, char *buf) 602 { 603 const struct gpio_chip *chip = dev_get_drvdata(dev); 604 605 return sprintf(buf, "%u\n", chip->ngpio); 606 } 607 static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); 608 609 static const struct attribute *gpiochip_attrs[] = { 610 &dev_attr_base.attr, 611 &dev_attr_label.attr, 612 &dev_attr_ngpio.attr, 613 NULL, 614 }; 615 616 static const struct attribute_group gpiochip_attr_group = { 617 .attrs = (struct attribute **) gpiochip_attrs, 618 }; 619 620 /* 621 * /sys/class/gpio/export ... write-only 622 * integer N ... number of GPIO to export (full access) 623 * /sys/class/gpio/unexport ... write-only 624 * integer N ... number of GPIO to unexport 625 */ 626 static ssize_t export_store(struct class *class, 627 struct class_attribute *attr, 628 const char *buf, size_t len) 629 { 630 long gpio; 631 int status; 632 633 status = strict_strtol(buf, 0, &gpio); 634 if (status < 0) 635 goto done; 636 637 /* No extra locking here; FLAG_SYSFS just signifies that the 638 * request and export were done by on behalf of userspace, so 639 * they may be undone on its behalf too. 640 */ 641 642 status = gpio_request(gpio, "sysfs"); 643 if (status < 0) 644 goto done; 645 646 status = gpio_export(gpio, true); 647 if (status < 0) 648 gpio_free(gpio); 649 else 650 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); 651 652 done: 653 if (status) 654 pr_debug("%s: status %d\n", __func__, status); 655 return status ? : len; 656 } 657 658 static ssize_t unexport_store(struct class *class, 659 struct class_attribute *attr, 660 const char *buf, size_t len) 661 { 662 long gpio; 663 int status; 664 665 status = strict_strtol(buf, 0, &gpio); 666 if (status < 0) 667 goto done; 668 669 status = -EINVAL; 670 671 /* reject bogus commands (gpio_unexport ignores them) */ 672 if (!gpio_is_valid(gpio)) 673 goto done; 674 675 /* No extra locking here; FLAG_SYSFS just signifies that the 676 * request and export were done by on behalf of userspace, so 677 * they may be undone on its behalf too. 678 */ 679 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { 680 status = 0; 681 gpio_free(gpio); 682 } 683 done: 684 if (status) 685 pr_debug("%s: status %d\n", __func__, status); 686 return status ? : len; 687 } 688 689 static struct class_attribute gpio_class_attrs[] = { 690 __ATTR(export, 0200, NULL, export_store), 691 __ATTR(unexport, 0200, NULL, unexport_store), 692 __ATTR_NULL, 693 }; 694 695 static struct class gpio_class = { 696 .name = "gpio", 697 .owner = THIS_MODULE, 698 699 .class_attrs = gpio_class_attrs, 700 }; 701 702 703 /** 704 * gpio_export - export a GPIO through sysfs 705 * @gpio: gpio to make available, already requested 706 * @direction_may_change: true if userspace may change gpio direction 707 * Context: arch_initcall or later 708 * 709 * When drivers want to make a GPIO accessible to userspace after they 710 * have requested it -- perhaps while debugging, or as part of their 711 * public interface -- they may use this routine. If the GPIO can 712 * change direction (some can't) and the caller allows it, userspace 713 * will see "direction" sysfs attribute which may be used to change 714 * the gpio's direction. A "value" attribute will always be provided. 715 * 716 * Returns zero on success, else an error. 717 */ 718 int gpio_export(unsigned gpio, bool direction_may_change) 719 { 720 unsigned long flags; 721 struct gpio_desc *desc; 722 int status = -EINVAL; 723 char *ioname = NULL; 724 725 /* can't export until sysfs is available ... */ 726 if (!gpio_class.p) { 727 pr_debug("%s: called too early!\n", __func__); 728 return -ENOENT; 729 } 730 731 if (!gpio_is_valid(gpio)) 732 goto done; 733 734 mutex_lock(&sysfs_lock); 735 736 spin_lock_irqsave(&gpio_lock, flags); 737 desc = &gpio_desc[gpio]; 738 if (test_bit(FLAG_REQUESTED, &desc->flags) 739 && !test_bit(FLAG_EXPORT, &desc->flags)) { 740 status = 0; 741 if (!desc->chip->direction_input 742 || !desc->chip->direction_output) 743 direction_may_change = false; 744 } 745 spin_unlock_irqrestore(&gpio_lock, flags); 746 747 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base]) 748 ioname = desc->chip->names[gpio - desc->chip->base]; 749 750 if (status == 0) { 751 struct device *dev; 752 753 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 754 desc, ioname ? ioname : "gpio%d", gpio); 755 if (!IS_ERR(dev)) { 756 status = sysfs_create_group(&dev->kobj, 757 &gpio_attr_group); 758 759 if (!status && direction_may_change) 760 status = device_create_file(dev, 761 &dev_attr_direction); 762 763 if (!status && gpio_to_irq(gpio) >= 0 764 && (direction_may_change 765 || !test_bit(FLAG_IS_OUT, 766 &desc->flags))) 767 status = device_create_file(dev, 768 &dev_attr_edge); 769 770 if (status != 0) 771 device_unregister(dev); 772 } else 773 status = PTR_ERR(dev); 774 if (status == 0) 775 set_bit(FLAG_EXPORT, &desc->flags); 776 } 777 778 mutex_unlock(&sysfs_lock); 779 780 done: 781 if (status) 782 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 783 784 return status; 785 } 786 EXPORT_SYMBOL_GPL(gpio_export); 787 788 static int match_export(struct device *dev, void *data) 789 { 790 return dev_get_drvdata(dev) == data; 791 } 792 793 /** 794 * gpio_export_link - create a sysfs link to an exported GPIO node 795 * @dev: device under which to create symlink 796 * @name: name of the symlink 797 * @gpio: gpio to create symlink to, already exported 798 * 799 * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN 800 * node. Caller is responsible for unlinking. 801 * 802 * Returns zero on success, else an error. 803 */ 804 int gpio_export_link(struct device *dev, const char *name, unsigned gpio) 805 { 806 struct gpio_desc *desc; 807 int status = -EINVAL; 808 809 if (!gpio_is_valid(gpio)) 810 goto done; 811 812 mutex_lock(&sysfs_lock); 813 814 desc = &gpio_desc[gpio]; 815 816 if (test_bit(FLAG_EXPORT, &desc->flags)) { 817 struct device *tdev; 818 819 tdev = class_find_device(&gpio_class, NULL, desc, match_export); 820 if (tdev != NULL) { 821 status = sysfs_create_link(&dev->kobj, &tdev->kobj, 822 name); 823 } else { 824 status = -ENODEV; 825 } 826 } 827 828 mutex_unlock(&sysfs_lock); 829 830 done: 831 if (status) 832 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 833 834 return status; 835 } 836 EXPORT_SYMBOL_GPL(gpio_export_link); 837 838 839 /** 840 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value 841 * @gpio: gpio to change 842 * @value: non-zero to use active low, i.e. inverted values 843 * 844 * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. 845 * The GPIO does not have to be exported yet. If poll(2) support has 846 * been enabled for either rising or falling edge, it will be 847 * reconfigured to follow the new polarity. 848 * 849 * Returns zero on success, else an error. 850 */ 851 int gpio_sysfs_set_active_low(unsigned gpio, int value) 852 { 853 struct gpio_desc *desc; 854 struct device *dev = NULL; 855 int status = -EINVAL; 856 857 if (!gpio_is_valid(gpio)) 858 goto done; 859 860 mutex_lock(&sysfs_lock); 861 862 desc = &gpio_desc[gpio]; 863 864 if (test_bit(FLAG_EXPORT, &desc->flags)) { 865 dev = class_find_device(&gpio_class, NULL, desc, match_export); 866 if (dev == NULL) { 867 status = -ENODEV; 868 goto unlock; 869 } 870 } 871 872 status = sysfs_set_active_low(desc, dev, value); 873 874 unlock: 875 mutex_unlock(&sysfs_lock); 876 877 done: 878 if (status) 879 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 880 881 return status; 882 } 883 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); 884 885 /** 886 * gpio_unexport - reverse effect of gpio_export() 887 * @gpio: gpio to make unavailable 888 * 889 * This is implicit on gpio_free(). 890 */ 891 void gpio_unexport(unsigned gpio) 892 { 893 struct gpio_desc *desc; 894 int status = -EINVAL; 895 896 if (!gpio_is_valid(gpio)) 897 goto done; 898 899 mutex_lock(&sysfs_lock); 900 901 desc = &gpio_desc[gpio]; 902 903 if (test_bit(FLAG_EXPORT, &desc->flags)) { 904 struct device *dev = NULL; 905 906 dev = class_find_device(&gpio_class, NULL, desc, match_export); 907 if (dev) { 908 gpio_setup_irq(desc, dev, 0); 909 clear_bit(FLAG_EXPORT, &desc->flags); 910 put_device(dev); 911 device_unregister(dev); 912 status = 0; 913 } else 914 status = -ENODEV; 915 } 916 917 mutex_unlock(&sysfs_lock); 918 done: 919 if (status) 920 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 921 } 922 EXPORT_SYMBOL_GPL(gpio_unexport); 923 924 static int gpiochip_export(struct gpio_chip *chip) 925 { 926 int status; 927 struct device *dev; 928 929 /* Many systems register gpio chips for SOC support very early, 930 * before driver model support is available. In those cases we 931 * export this later, in gpiolib_sysfs_init() ... here we just 932 * verify that _some_ field of gpio_class got initialized. 933 */ 934 if (!gpio_class.p) 935 return 0; 936 937 /* use chip->base for the ID; it's already known to be unique */ 938 mutex_lock(&sysfs_lock); 939 dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, 940 "gpiochip%d", chip->base); 941 if (!IS_ERR(dev)) { 942 status = sysfs_create_group(&dev->kobj, 943 &gpiochip_attr_group); 944 } else 945 status = PTR_ERR(dev); 946 chip->exported = (status == 0); 947 mutex_unlock(&sysfs_lock); 948 949 if (status) { 950 unsigned long flags; 951 unsigned gpio; 952 953 spin_lock_irqsave(&gpio_lock, flags); 954 gpio = chip->base; 955 while (gpio_desc[gpio].chip == chip) 956 gpio_desc[gpio++].chip = NULL; 957 spin_unlock_irqrestore(&gpio_lock, flags); 958 959 pr_debug("%s: chip %s status %d\n", __func__, 960 chip->label, status); 961 } 962 963 return status; 964 } 965 966 static void gpiochip_unexport(struct gpio_chip *chip) 967 { 968 int status; 969 struct device *dev; 970 971 mutex_lock(&sysfs_lock); 972 dev = class_find_device(&gpio_class, NULL, chip, match_export); 973 if (dev) { 974 put_device(dev); 975 device_unregister(dev); 976 chip->exported = 0; 977 status = 0; 978 } else 979 status = -ENODEV; 980 mutex_unlock(&sysfs_lock); 981 982 if (status) 983 pr_debug("%s: chip %s status %d\n", __func__, 984 chip->label, status); 985 } 986 987 static int __init gpiolib_sysfs_init(void) 988 { 989 int status; 990 unsigned long flags; 991 unsigned gpio; 992 993 idr_init(&pdesc_idr); 994 995 status = class_register(&gpio_class); 996 if (status < 0) 997 return status; 998 999 /* Scan and register the gpio_chips which registered very 1000 * early (e.g. before the class_register above was called). 1001 * 1002 * We run before arch_initcall() so chip->dev nodes can have 1003 * registered, and so arch_initcall() can always gpio_export(). 1004 */ 1005 spin_lock_irqsave(&gpio_lock, flags); 1006 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { 1007 struct gpio_chip *chip; 1008 1009 chip = gpio_desc[gpio].chip; 1010 if (!chip || chip->exported) 1011 continue; 1012 1013 spin_unlock_irqrestore(&gpio_lock, flags); 1014 status = gpiochip_export(chip); 1015 spin_lock_irqsave(&gpio_lock, flags); 1016 } 1017 spin_unlock_irqrestore(&gpio_lock, flags); 1018 1019 1020 return status; 1021 } 1022 postcore_initcall(gpiolib_sysfs_init); 1023 1024 #else 1025 static inline int gpiochip_export(struct gpio_chip *chip) 1026 { 1027 return 0; 1028 } 1029 1030 static inline void gpiochip_unexport(struct gpio_chip *chip) 1031 { 1032 } 1033 1034 #endif /* CONFIG_GPIO_SYSFS */ 1035 1036 /** 1037 * gpiochip_add() - register a gpio_chip 1038 * @chip: the chip to register, with chip->base initialized 1039 * Context: potentially before irqs or kmalloc will work 1040 * 1041 * Returns a negative errno if the chip can't be registered, such as 1042 * because the chip->base is invalid or already associated with a 1043 * different chip. Otherwise it returns zero as a success code. 1044 * 1045 * When gpiochip_add() is called very early during boot, so that GPIOs 1046 * can be freely used, the chip->dev device must be registered before 1047 * the gpio framework's arch_initcall(). Otherwise sysfs initialization 1048 * for GPIOs will fail rudely. 1049 * 1050 * If chip->base is negative, this requests dynamic assignment of 1051 * a range of valid GPIOs. 1052 */ 1053 int gpiochip_add(struct gpio_chip *chip) 1054 { 1055 unsigned long flags; 1056 int status = 0; 1057 unsigned id; 1058 int base = chip->base; 1059 1060 if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) 1061 && base >= 0) { 1062 status = -EINVAL; 1063 goto fail; 1064 } 1065 1066 spin_lock_irqsave(&gpio_lock, flags); 1067 1068 if (base < 0) { 1069 base = gpiochip_find_base(chip->ngpio); 1070 if (base < 0) { 1071 status = base; 1072 goto unlock; 1073 } 1074 chip->base = base; 1075 } 1076 1077 /* these GPIO numbers must not be managed by another gpio_chip */ 1078 for (id = base; id < base + chip->ngpio; id++) { 1079 if (gpio_desc[id].chip != NULL) { 1080 status = -EBUSY; 1081 break; 1082 } 1083 } 1084 if (status == 0) { 1085 for (id = base; id < base + chip->ngpio; id++) { 1086 gpio_desc[id].chip = chip; 1087 1088 /* REVISIT: most hardware initializes GPIOs as 1089 * inputs (often with pullups enabled) so power 1090 * usage is minimized. Linux code should set the 1091 * gpio direction first thing; but until it does, 1092 * we may expose the wrong direction in sysfs. 1093 */ 1094 gpio_desc[id].flags = !chip->direction_input 1095 ? (1 << FLAG_IS_OUT) 1096 : 0; 1097 } 1098 } 1099 1100 unlock: 1101 spin_unlock_irqrestore(&gpio_lock, flags); 1102 if (status == 0) 1103 status = gpiochip_export(chip); 1104 fail: 1105 /* failures here can mean systems won't boot... */ 1106 if (status) 1107 pr_err("gpiochip_add: gpios %d..%d (%s) not registered\n", 1108 chip->base, chip->base + chip->ngpio - 1, 1109 chip->label ? : "generic"); 1110 return status; 1111 } 1112 EXPORT_SYMBOL_GPL(gpiochip_add); 1113 1114 /** 1115 * gpiochip_remove() - unregister a gpio_chip 1116 * @chip: the chip to unregister 1117 * 1118 * A gpio_chip with any GPIOs still requested may not be removed. 1119 */ 1120 int gpiochip_remove(struct gpio_chip *chip) 1121 { 1122 unsigned long flags; 1123 int status = 0; 1124 unsigned id; 1125 1126 spin_lock_irqsave(&gpio_lock, flags); 1127 1128 for (id = chip->base; id < chip->base + chip->ngpio; id++) { 1129 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { 1130 status = -EBUSY; 1131 break; 1132 } 1133 } 1134 if (status == 0) { 1135 for (id = chip->base; id < chip->base + chip->ngpio; id++) 1136 gpio_desc[id].chip = NULL; 1137 } 1138 1139 spin_unlock_irqrestore(&gpio_lock, flags); 1140 1141 if (status == 0) 1142 gpiochip_unexport(chip); 1143 1144 return status; 1145 } 1146 EXPORT_SYMBOL_GPL(gpiochip_remove); 1147 1148 1149 /* These "optional" allocation calls help prevent drivers from stomping 1150 * on each other, and help provide better diagnostics in debugfs. 1151 * They're called even less than the "set direction" calls. 1152 */ 1153 int gpio_request(unsigned gpio, const char *label) 1154 { 1155 struct gpio_desc *desc; 1156 struct gpio_chip *chip; 1157 int status = -EINVAL; 1158 unsigned long flags; 1159 1160 spin_lock_irqsave(&gpio_lock, flags); 1161 1162 if (!gpio_is_valid(gpio)) 1163 goto done; 1164 desc = &gpio_desc[gpio]; 1165 chip = desc->chip; 1166 if (chip == NULL) 1167 goto done; 1168 1169 if (!try_module_get(chip->owner)) 1170 goto done; 1171 1172 /* NOTE: gpio_request() can be called in early boot, 1173 * before IRQs are enabled, for non-sleeping (SOC) GPIOs. 1174 */ 1175 1176 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { 1177 desc_set_label(desc, label ? : "?"); 1178 status = 0; 1179 } else { 1180 status = -EBUSY; 1181 module_put(chip->owner); 1182 goto done; 1183 } 1184 1185 if (chip->request) { 1186 /* chip->request may sleep */ 1187 spin_unlock_irqrestore(&gpio_lock, flags); 1188 status = chip->request(chip, gpio - chip->base); 1189 spin_lock_irqsave(&gpio_lock, flags); 1190 1191 if (status < 0) { 1192 desc_set_label(desc, NULL); 1193 module_put(chip->owner); 1194 clear_bit(FLAG_REQUESTED, &desc->flags); 1195 } 1196 } 1197 1198 done: 1199 if (status) 1200 pr_debug("gpio_request: gpio-%d (%s) status %d\n", 1201 gpio, label ? : "?", status); 1202 spin_unlock_irqrestore(&gpio_lock, flags); 1203 return status; 1204 } 1205 EXPORT_SYMBOL_GPL(gpio_request); 1206 1207 void gpio_free(unsigned gpio) 1208 { 1209 unsigned long flags; 1210 struct gpio_desc *desc; 1211 struct gpio_chip *chip; 1212 1213 might_sleep(); 1214 1215 if (!gpio_is_valid(gpio)) { 1216 WARN_ON(extra_checks); 1217 return; 1218 } 1219 1220 gpio_unexport(gpio); 1221 1222 spin_lock_irqsave(&gpio_lock, flags); 1223 1224 desc = &gpio_desc[gpio]; 1225 chip = desc->chip; 1226 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { 1227 if (chip->free) { 1228 spin_unlock_irqrestore(&gpio_lock, flags); 1229 might_sleep_if(extra_checks && chip->can_sleep); 1230 chip->free(chip, gpio - chip->base); 1231 spin_lock_irqsave(&gpio_lock, flags); 1232 } 1233 desc_set_label(desc, NULL); 1234 module_put(desc->chip->owner); 1235 clear_bit(FLAG_ACTIVE_LOW, &desc->flags); 1236 clear_bit(FLAG_REQUESTED, &desc->flags); 1237 } else 1238 WARN_ON(extra_checks); 1239 1240 spin_unlock_irqrestore(&gpio_lock, flags); 1241 } 1242 EXPORT_SYMBOL_GPL(gpio_free); 1243 1244 /** 1245 * gpio_request_one - request a single GPIO with initial configuration 1246 * @gpio: the GPIO number 1247 * @flags: GPIO configuration as specified by GPIOF_* 1248 * @label: a literal description string of this GPIO 1249 */ 1250 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) 1251 { 1252 int err; 1253 1254 err = gpio_request(gpio, label); 1255 if (err) 1256 return err; 1257 1258 if (flags & GPIOF_DIR_IN) 1259 err = gpio_direction_input(gpio); 1260 else 1261 err = gpio_direction_output(gpio, 1262 (flags & GPIOF_INIT_HIGH) ? 1 : 0); 1263 1264 return err; 1265 } 1266 EXPORT_SYMBOL_GPL(gpio_request_one); 1267 1268 /** 1269 * gpio_request_array - request multiple GPIOs in a single call 1270 * @array: array of the 'struct gpio' 1271 * @num: how many GPIOs in the array 1272 */ 1273 int gpio_request_array(struct gpio *array, size_t num) 1274 { 1275 int i, err; 1276 1277 for (i = 0; i < num; i++, array++) { 1278 err = gpio_request_one(array->gpio, array->flags, array->label); 1279 if (err) 1280 goto err_free; 1281 } 1282 return 0; 1283 1284 err_free: 1285 while (i--) 1286 gpio_free((--array)->gpio); 1287 return err; 1288 } 1289 EXPORT_SYMBOL_GPL(gpio_request_array); 1290 1291 /** 1292 * gpio_free_array - release multiple GPIOs in a single call 1293 * @array: array of the 'struct gpio' 1294 * @num: how many GPIOs in the array 1295 */ 1296 void gpio_free_array(struct gpio *array, size_t num) 1297 { 1298 while (num--) 1299 gpio_free((array++)->gpio); 1300 } 1301 EXPORT_SYMBOL_GPL(gpio_free_array); 1302 1303 /** 1304 * gpiochip_is_requested - return string iff signal was requested 1305 * @chip: controller managing the signal 1306 * @offset: of signal within controller's 0..(ngpio - 1) range 1307 * 1308 * Returns NULL if the GPIO is not currently requested, else a string. 1309 * If debugfs support is enabled, the string returned is the label passed 1310 * to gpio_request(); otherwise it is a meaningless constant. 1311 * 1312 * This function is for use by GPIO controller drivers. The label can 1313 * help with diagnostics, and knowing that the signal is used as a GPIO 1314 * can help avoid accidentally multiplexing it to another controller. 1315 */ 1316 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) 1317 { 1318 unsigned gpio = chip->base + offset; 1319 1320 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) 1321 return NULL; 1322 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) 1323 return NULL; 1324 #ifdef CONFIG_DEBUG_FS 1325 return gpio_desc[gpio].label; 1326 #else 1327 return "?"; 1328 #endif 1329 } 1330 EXPORT_SYMBOL_GPL(gpiochip_is_requested); 1331 1332 1333 /* Drivers MUST set GPIO direction before making get/set calls. In 1334 * some cases this is done in early boot, before IRQs are enabled. 1335 * 1336 * As a rule these aren't called more than once (except for drivers 1337 * using the open-drain emulation idiom) so these are natural places 1338 * to accumulate extra debugging checks. Note that we can't (yet) 1339 * rely on gpio_request() having been called beforehand. 1340 */ 1341 1342 int gpio_direction_input(unsigned gpio) 1343 { 1344 unsigned long flags; 1345 struct gpio_chip *chip; 1346 struct gpio_desc *desc = &gpio_desc[gpio]; 1347 int status = -EINVAL; 1348 1349 spin_lock_irqsave(&gpio_lock, flags); 1350 1351 if (!gpio_is_valid(gpio)) 1352 goto fail; 1353 chip = desc->chip; 1354 if (!chip || !chip->get || !chip->direction_input) 1355 goto fail; 1356 gpio -= chip->base; 1357 if (gpio >= chip->ngpio) 1358 goto fail; 1359 status = gpio_ensure_requested(desc, gpio); 1360 if (status < 0) 1361 goto fail; 1362 1363 /* now we know the gpio is valid and chip won't vanish */ 1364 1365 spin_unlock_irqrestore(&gpio_lock, flags); 1366 1367 might_sleep_if(extra_checks && chip->can_sleep); 1368 1369 if (status) { 1370 status = chip->request(chip, gpio); 1371 if (status < 0) { 1372 pr_debug("GPIO-%d: chip request fail, %d\n", 1373 chip->base + gpio, status); 1374 /* and it's not available to anyone else ... 1375 * gpio_request() is the fully clean solution. 1376 */ 1377 goto lose; 1378 } 1379 } 1380 1381 status = chip->direction_input(chip, gpio); 1382 if (status == 0) 1383 clear_bit(FLAG_IS_OUT, &desc->flags); 1384 lose: 1385 return status; 1386 fail: 1387 spin_unlock_irqrestore(&gpio_lock, flags); 1388 if (status) 1389 pr_debug("%s: gpio-%d status %d\n", 1390 __func__, gpio, status); 1391 return status; 1392 } 1393 EXPORT_SYMBOL_GPL(gpio_direction_input); 1394 1395 int gpio_direction_output(unsigned gpio, int value) 1396 { 1397 unsigned long flags; 1398 struct gpio_chip *chip; 1399 struct gpio_desc *desc = &gpio_desc[gpio]; 1400 int status = -EINVAL; 1401 1402 spin_lock_irqsave(&gpio_lock, flags); 1403 1404 if (!gpio_is_valid(gpio)) 1405 goto fail; 1406 chip = desc->chip; 1407 if (!chip || !chip->set || !chip->direction_output) 1408 goto fail; 1409 gpio -= chip->base; 1410 if (gpio >= chip->ngpio) 1411 goto fail; 1412 status = gpio_ensure_requested(desc, gpio); 1413 if (status < 0) 1414 goto fail; 1415 1416 /* now we know the gpio is valid and chip won't vanish */ 1417 1418 spin_unlock_irqrestore(&gpio_lock, flags); 1419 1420 might_sleep_if(extra_checks && chip->can_sleep); 1421 1422 if (status) { 1423 status = chip->request(chip, gpio); 1424 if (status < 0) { 1425 pr_debug("GPIO-%d: chip request fail, %d\n", 1426 chip->base + gpio, status); 1427 /* and it's not available to anyone else ... 1428 * gpio_request() is the fully clean solution. 1429 */ 1430 goto lose; 1431 } 1432 } 1433 1434 status = chip->direction_output(chip, gpio, value); 1435 if (status == 0) 1436 set_bit(FLAG_IS_OUT, &desc->flags); 1437 lose: 1438 return status; 1439 fail: 1440 spin_unlock_irqrestore(&gpio_lock, flags); 1441 if (status) 1442 pr_debug("%s: gpio-%d status %d\n", 1443 __func__, gpio, status); 1444 return status; 1445 } 1446 EXPORT_SYMBOL_GPL(gpio_direction_output); 1447 1448 1449 /* I/O calls are only valid after configuration completed; the relevant 1450 * "is this a valid GPIO" error checks should already have been done. 1451 * 1452 * "Get" operations are often inlinable as reading a pin value register, 1453 * and masking the relevant bit in that register. 1454 * 1455 * When "set" operations are inlinable, they involve writing that mask to 1456 * one register to set a low value, or a different register to set it high. 1457 * Otherwise locking is needed, so there may be little value to inlining. 1458 * 1459 *------------------------------------------------------------------------ 1460 * 1461 * IMPORTANT!!! The hot paths -- get/set value -- assume that callers 1462 * have requested the GPIO. That can include implicit requesting by 1463 * a direction setting call. Marking a gpio as requested locks its chip 1464 * in memory, guaranteeing that these table lookups need no more locking 1465 * and that gpiochip_remove() will fail. 1466 * 1467 * REVISIT when debugging, consider adding some instrumentation to ensure 1468 * that the GPIO was actually requested. 1469 */ 1470 1471 /** 1472 * __gpio_get_value() - return a gpio's value 1473 * @gpio: gpio whose value will be returned 1474 * Context: any 1475 * 1476 * This is used directly or indirectly to implement gpio_get_value(). 1477 * It returns the zero or nonzero value provided by the associated 1478 * gpio_chip.get() method; or zero if no such method is provided. 1479 */ 1480 int __gpio_get_value(unsigned gpio) 1481 { 1482 struct gpio_chip *chip; 1483 1484 chip = gpio_to_chip(gpio); 1485 WARN_ON(extra_checks && chip->can_sleep); 1486 return chip->get ? chip->get(chip, gpio - chip->base) : 0; 1487 } 1488 EXPORT_SYMBOL_GPL(__gpio_get_value); 1489 1490 /** 1491 * __gpio_set_value() - assign a gpio's value 1492 * @gpio: gpio whose value will be assigned 1493 * @value: value to assign 1494 * Context: any 1495 * 1496 * This is used directly or indirectly to implement gpio_set_value(). 1497 * It invokes the associated gpio_chip.set() method. 1498 */ 1499 void __gpio_set_value(unsigned gpio, int value) 1500 { 1501 struct gpio_chip *chip; 1502 1503 chip = gpio_to_chip(gpio); 1504 WARN_ON(extra_checks && chip->can_sleep); 1505 chip->set(chip, gpio - chip->base, value); 1506 } 1507 EXPORT_SYMBOL_GPL(__gpio_set_value); 1508 1509 /** 1510 * __gpio_cansleep() - report whether gpio value access will sleep 1511 * @gpio: gpio in question 1512 * Context: any 1513 * 1514 * This is used directly or indirectly to implement gpio_cansleep(). It 1515 * returns nonzero if access reading or writing the GPIO value can sleep. 1516 */ 1517 int __gpio_cansleep(unsigned gpio) 1518 { 1519 struct gpio_chip *chip; 1520 1521 /* only call this on GPIOs that are valid! */ 1522 chip = gpio_to_chip(gpio); 1523 1524 return chip->can_sleep; 1525 } 1526 EXPORT_SYMBOL_GPL(__gpio_cansleep); 1527 1528 /** 1529 * __gpio_to_irq() - return the IRQ corresponding to a GPIO 1530 * @gpio: gpio whose IRQ will be returned (already requested) 1531 * Context: any 1532 * 1533 * This is used directly or indirectly to implement gpio_to_irq(). 1534 * It returns the number of the IRQ signaled by this (input) GPIO, 1535 * or a negative errno. 1536 */ 1537 int __gpio_to_irq(unsigned gpio) 1538 { 1539 struct gpio_chip *chip; 1540 1541 chip = gpio_to_chip(gpio); 1542 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; 1543 } 1544 EXPORT_SYMBOL_GPL(__gpio_to_irq); 1545 1546 1547 1548 /* There's no value in making it easy to inline GPIO calls that may sleep. 1549 * Common examples include ones connected to I2C or SPI chips. 1550 */ 1551 1552 int gpio_get_value_cansleep(unsigned gpio) 1553 { 1554 struct gpio_chip *chip; 1555 1556 might_sleep_if(extra_checks); 1557 chip = gpio_to_chip(gpio); 1558 return chip->get ? chip->get(chip, gpio - chip->base) : 0; 1559 } 1560 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 1561 1562 void gpio_set_value_cansleep(unsigned gpio, int value) 1563 { 1564 struct gpio_chip *chip; 1565 1566 might_sleep_if(extra_checks); 1567 chip = gpio_to_chip(gpio); 1568 chip->set(chip, gpio - chip->base, value); 1569 } 1570 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); 1571 1572 1573 #ifdef CONFIG_DEBUG_FS 1574 1575 static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) 1576 { 1577 unsigned i; 1578 unsigned gpio = chip->base; 1579 struct gpio_desc *gdesc = &gpio_desc[gpio]; 1580 int is_out; 1581 1582 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { 1583 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) 1584 continue; 1585 1586 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); 1587 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", 1588 gpio, gdesc->label, 1589 is_out ? "out" : "in ", 1590 chip->get 1591 ? (chip->get(chip, i) ? "hi" : "lo") 1592 : "? "); 1593 1594 if (!is_out) { 1595 int irq = gpio_to_irq(gpio); 1596 struct irq_desc *desc = irq_to_desc(irq); 1597 1598 /* This races with request_irq(), set_irq_type(), 1599 * and set_irq_wake() ... but those are "rare". 1600 * 1601 * More significantly, trigger type flags aren't 1602 * currently maintained by genirq. 1603 */ 1604 if (irq >= 0 && desc->action) { 1605 char *trigger; 1606 1607 switch (desc->status & IRQ_TYPE_SENSE_MASK) { 1608 case IRQ_TYPE_NONE: 1609 trigger = "(default)"; 1610 break; 1611 case IRQ_TYPE_EDGE_FALLING: 1612 trigger = "edge-falling"; 1613 break; 1614 case IRQ_TYPE_EDGE_RISING: 1615 trigger = "edge-rising"; 1616 break; 1617 case IRQ_TYPE_EDGE_BOTH: 1618 trigger = "edge-both"; 1619 break; 1620 case IRQ_TYPE_LEVEL_HIGH: 1621 trigger = "level-high"; 1622 break; 1623 case IRQ_TYPE_LEVEL_LOW: 1624 trigger = "level-low"; 1625 break; 1626 default: 1627 trigger = "?trigger?"; 1628 break; 1629 } 1630 1631 seq_printf(s, " irq-%d %s%s", 1632 irq, trigger, 1633 (desc->status & IRQ_WAKEUP) 1634 ? " wakeup" : ""); 1635 } 1636 } 1637 1638 seq_printf(s, "\n"); 1639 } 1640 } 1641 1642 static int gpiolib_show(struct seq_file *s, void *unused) 1643 { 1644 struct gpio_chip *chip = NULL; 1645 unsigned gpio; 1646 int started = 0; 1647 1648 /* REVISIT this isn't locked against gpio_chip removal ... */ 1649 1650 for (gpio = 0; gpio_is_valid(gpio); gpio++) { 1651 struct device *dev; 1652 1653 if (chip == gpio_desc[gpio].chip) 1654 continue; 1655 chip = gpio_desc[gpio].chip; 1656 if (!chip) 1657 continue; 1658 1659 seq_printf(s, "%sGPIOs %d-%d", 1660 started ? "\n" : "", 1661 chip->base, chip->base + chip->ngpio - 1); 1662 dev = chip->dev; 1663 if (dev) 1664 seq_printf(s, ", %s/%s", 1665 dev->bus ? dev->bus->name : "no-bus", 1666 dev_name(dev)); 1667 if (chip->label) 1668 seq_printf(s, ", %s", chip->label); 1669 if (chip->can_sleep) 1670 seq_printf(s, ", can sleep"); 1671 seq_printf(s, ":\n"); 1672 1673 started = 1; 1674 if (chip->dbg_show) 1675 chip->dbg_show(s, chip); 1676 else 1677 gpiolib_dbg_show(s, chip); 1678 } 1679 return 0; 1680 } 1681 1682 static int gpiolib_open(struct inode *inode, struct file *file) 1683 { 1684 return single_open(file, gpiolib_show, NULL); 1685 } 1686 1687 static const struct file_operations gpiolib_operations = { 1688 .open = gpiolib_open, 1689 .read = seq_read, 1690 .llseek = seq_lseek, 1691 .release = single_release, 1692 }; 1693 1694 static int __init gpiolib_debugfs_init(void) 1695 { 1696 /* /sys/kernel/debug/gpio */ 1697 (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, 1698 NULL, NULL, &gpiolib_operations); 1699 return 0; 1700 } 1701 subsys_initcall(gpiolib_debugfs_init); 1702 1703 #endif /* DEBUG_FS */ 1704