Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpiolib: use descriptors internally

Make sure gpiolib works internally with descriptors and (chip, offset)
pairs instead of using the global integer namespace. This prepares the
ground for the removal of the global gpio_desc[] array and the
introduction of the descriptor-based GPIO API.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
[grant.likely: Squash in fix for link error when CONFIG_SYSFS=n]
Signed-off-by: Grant Likely <grant.likely@secretlab.ca>

authored by

Alexandre Courbot and committed by
Grant Likely
372e722e 83cabe33

+339 -177
+339 -177
drivers/gpio/gpiolib.c
··· 78 78 static DEFINE_IDR(dirent_idr); 79 79 #endif 80 80 81 + /* 82 + * Internal gpiod_* API using descriptors instead of the integer namespace. 83 + * Most of this should eventually go public. 84 + */ 85 + static int gpiod_request(struct gpio_desc *desc, const char *label); 86 + static void gpiod_free(struct gpio_desc *desc); 87 + static int gpiod_direction_input(struct gpio_desc *desc); 88 + static int gpiod_direction_output(struct gpio_desc *desc, int value); 89 + static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce); 90 + static int gpiod_get_value_cansleep(struct gpio_desc *desc); 91 + static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value); 92 + static int gpiod_get_value(struct gpio_desc *desc); 93 + static void gpiod_set_value(struct gpio_desc *desc, int value); 94 + static int gpiod_cansleep(struct gpio_desc *desc); 95 + static int gpiod_to_irq(struct gpio_desc *desc); 96 + static int gpiod_export(struct gpio_desc *desc, bool direction_may_change); 97 + static int gpiod_export_link(struct device *dev, const char *name, 98 + struct gpio_desc *desc); 99 + static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value); 100 + static void gpiod_unexport(struct gpio_desc *desc); 101 + 102 + 81 103 static inline void desc_set_label(struct gpio_desc *d, const char *label) 82 104 { 83 105 #ifdef CONFIG_DEBUG_FS 84 106 d->label = label; 85 107 #endif 86 108 } 109 + 110 + /* 111 + * Return the GPIO number of the passed descriptor relative to its chip 112 + */ 113 + static int gpio_chip_hwgpio(const struct gpio_desc *desc) 114 + { 115 + return (desc - &gpio_desc[0]) - desc->chip->base; 116 + } 117 + 118 + /** 119 + * Convert a GPIO number to its descriptor 120 + */ 121 + static struct gpio_desc *gpio_to_desc(unsigned gpio) 122 + { 123 + if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) 124 + return NULL; 125 + else 126 + return &gpio_desc[gpio]; 127 + } 128 + 129 + /** 130 + * Convert a GPIO descriptor to the integer namespace. 131 + * This should disappear in the future but is needed since we still 132 + * use GPIO numbers for error messages and sysfs nodes 133 + */ 134 + static int desc_to_gpio(const struct gpio_desc *desc) 135 + { 136 + return desc - &gpio_desc[0]; 137 + } 138 + 87 139 88 140 /* Warn when drivers omit gpio_request() calls -- legal but ill-advised 89 141 * when setting direction, and otherwise illegal. Until board setup code ··· 148 96 * only "legal" in the sense that (old) code using it won't break yet, 149 97 * but instead only triggers a WARN() stack dump. 150 98 */ 151 - static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) 99 + static int gpio_ensure_requested(struct gpio_desc *desc) 152 100 { 153 101 const struct gpio_chip *chip = desc->chip; 154 - const int gpio = chip->base + offset; 102 + const int gpio = desc_to_gpio(desc); 155 103 156 104 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, 157 105 "autorequest GPIO-%d\n", gpio)) { ··· 170 118 } 171 119 172 120 /* caller holds gpio_lock *OR* gpio is marked as requested */ 121 + static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc) 122 + { 123 + return desc->chip; 124 + } 125 + 173 126 struct gpio_chip *gpio_to_chip(unsigned gpio) 174 127 { 175 - return gpio_desc[gpio].chip; 128 + return gpiod_to_chip(gpio_to_desc(gpio)); 176 129 } 177 130 178 131 /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ ··· 205 148 } 206 149 207 150 /* caller ensures gpio is valid and requested, chip->get_direction may sleep */ 208 - static int gpio_get_direction(unsigned gpio) 151 + static int gpiod_get_direction(struct gpio_desc *desc) 209 152 { 210 153 struct gpio_chip *chip; 211 - struct gpio_desc *desc = &gpio_desc[gpio]; 154 + unsigned offset; 212 155 int status = -EINVAL; 213 156 214 - chip = gpio_to_chip(gpio); 215 - gpio -= chip->base; 157 + chip = gpiod_to_chip(desc); 158 + offset = gpio_chip_hwgpio(desc); 216 159 217 160 if (!chip->get_direction) 218 161 return status; 219 162 220 - status = chip->get_direction(chip, gpio); 163 + status = chip->get_direction(chip, offset); 221 164 if (status > 0) { 222 165 /* GPIOF_DIR_IN, or other positive */ 223 166 status = 1; ··· 261 204 static ssize_t gpio_direction_show(struct device *dev, 262 205 struct device_attribute *attr, char *buf) 263 206 { 264 - const struct gpio_desc *desc = dev_get_drvdata(dev); 265 - unsigned gpio = desc - gpio_desc; 207 + struct gpio_desc *desc = dev_get_drvdata(dev); 266 208 ssize_t status; 267 209 268 210 mutex_lock(&sysfs_lock); ··· 269 213 if (!test_bit(FLAG_EXPORT, &desc->flags)) { 270 214 status = -EIO; 271 215 } else { 272 - gpio_get_direction(gpio); 216 + gpiod_get_direction(desc); 273 217 status = sprintf(buf, "%s\n", 274 218 test_bit(FLAG_IS_OUT, &desc->flags) 275 219 ? "out" : "in"); ··· 282 226 static ssize_t gpio_direction_store(struct device *dev, 283 227 struct device_attribute *attr, const char *buf, size_t size) 284 228 { 285 - const struct gpio_desc *desc = dev_get_drvdata(dev); 286 - unsigned gpio = desc - gpio_desc; 229 + struct gpio_desc *desc = dev_get_drvdata(dev); 287 230 ssize_t status; 288 231 289 232 mutex_lock(&sysfs_lock); ··· 290 235 if (!test_bit(FLAG_EXPORT, &desc->flags)) 291 236 status = -EIO; 292 237 else if (sysfs_streq(buf, "high")) 293 - status = gpio_direction_output(gpio, 1); 238 + status = gpiod_direction_output(desc, 1); 294 239 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) 295 - status = gpio_direction_output(gpio, 0); 240 + status = gpiod_direction_output(desc, 0); 296 241 else if (sysfs_streq(buf, "in")) 297 - status = gpio_direction_input(gpio); 242 + status = gpiod_direction_input(desc); 298 243 else 299 244 status = -EINVAL; 300 245 ··· 308 253 static ssize_t gpio_value_show(struct device *dev, 309 254 struct device_attribute *attr, char *buf) 310 255 { 311 - const struct gpio_desc *desc = dev_get_drvdata(dev); 312 - unsigned gpio = desc - gpio_desc; 256 + struct gpio_desc *desc = dev_get_drvdata(dev); 313 257 ssize_t status; 314 258 315 259 mutex_lock(&sysfs_lock); ··· 318 264 } else { 319 265 int value; 320 266 321 - value = !!gpio_get_value_cansleep(gpio); 267 + value = !!gpiod_get_value_cansleep(desc); 322 268 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 323 269 value = !value; 324 270 ··· 332 278 static ssize_t gpio_value_store(struct device *dev, 333 279 struct device_attribute *attr, const char *buf, size_t size) 334 280 { 335 - const struct gpio_desc *desc = dev_get_drvdata(dev); 336 - unsigned gpio = desc - gpio_desc; 281 + struct gpio_desc *desc = dev_get_drvdata(dev); 337 282 ssize_t status; 338 283 339 284 mutex_lock(&sysfs_lock); ··· 348 295 if (status == 0) { 349 296 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 350 297 value = !value; 351 - gpio_set_value_cansleep(gpio, value != 0); 298 + gpiod_set_value_cansleep(desc, value != 0); 352 299 status = size; 353 300 } 354 301 } ··· 378 325 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) 379 326 return 0; 380 327 381 - irq = gpio_to_irq(desc - gpio_desc); 328 + irq = gpiod_to_irq(desc); 382 329 if (irq < 0) 383 330 return -EIO; 384 331 ··· 648 595 struct class_attribute *attr, 649 596 const char *buf, size_t len) 650 597 { 651 - long gpio; 652 - int status; 598 + long gpio; 599 + struct gpio_desc *desc; 600 + int status; 653 601 654 602 status = strict_strtol(buf, 0, &gpio); 655 603 if (status < 0) 656 604 goto done; 605 + 606 + desc = gpio_to_desc(gpio); 657 607 658 608 /* No extra locking here; FLAG_SYSFS just signifies that the 659 609 * request and export were done by on behalf of userspace, so 660 610 * they may be undone on its behalf too. 661 611 */ 662 612 663 - status = gpio_request(gpio, "sysfs"); 613 + status = gpiod_request(desc, "sysfs"); 664 614 if (status < 0) { 665 615 if (status == -EPROBE_DEFER) 666 616 status = -ENODEV; 667 617 goto done; 668 618 } 669 - status = gpio_export(gpio, true); 619 + status = gpiod_export(desc, true); 670 620 if (status < 0) 671 - gpio_free(gpio); 621 + gpiod_free(desc); 672 622 else 673 - set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); 623 + set_bit(FLAG_SYSFS, &desc->flags); 674 624 675 625 done: 676 626 if (status) ··· 685 629 struct class_attribute *attr, 686 630 const char *buf, size_t len) 687 631 { 688 - long gpio; 689 - int status; 632 + long gpio; 633 + struct gpio_desc *desc; 634 + int status; 690 635 691 636 status = strict_strtol(buf, 0, &gpio); 692 637 if (status < 0) ··· 695 638 696 639 status = -EINVAL; 697 640 641 + desc = gpio_to_desc(gpio); 698 642 /* reject bogus commands (gpio_unexport ignores them) */ 699 - if (!gpio_is_valid(gpio)) 643 + if (!desc) 700 644 goto done; 701 645 702 646 /* No extra locking here; FLAG_SYSFS just signifies that the 703 647 * request and export were done by on behalf of userspace, so 704 648 * they may be undone on its behalf too. 705 649 */ 706 - if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { 650 + if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { 707 651 status = 0; 708 - gpio_free(gpio); 652 + gpiod_free(desc); 709 653 } 710 654 done: 711 655 if (status) ··· 743 685 * 744 686 * Returns zero on success, else an error. 745 687 */ 746 - int gpio_export(unsigned gpio, bool direction_may_change) 688 + static int gpiod_export(struct gpio_desc *desc, bool direction_may_change) 747 689 { 748 690 unsigned long flags; 749 - struct gpio_desc *desc; 750 691 int status; 751 692 const char *ioname = NULL; 752 693 struct device *dev; 694 + int offset; 753 695 754 696 /* can't export until sysfs is available ... */ 755 697 if (!gpio_class.p) { ··· 757 699 return -ENOENT; 758 700 } 759 701 760 - if (!gpio_is_valid(gpio)) { 761 - pr_debug("%s: gpio %d is not valid\n", __func__, gpio); 702 + if (!desc) { 703 + pr_debug("%s: invalid gpio descriptor\n", __func__); 762 704 return -EINVAL; 763 705 } 764 706 765 707 mutex_lock(&sysfs_lock); 766 708 767 709 spin_lock_irqsave(&gpio_lock, flags); 768 - desc = &gpio_desc[gpio]; 769 710 if (!test_bit(FLAG_REQUESTED, &desc->flags) || 770 711 test_bit(FLAG_EXPORT, &desc->flags)) { 771 712 spin_unlock_irqrestore(&gpio_lock, flags); 772 713 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", 773 - __func__, gpio, 714 + __func__, desc_to_gpio(desc), 774 715 test_bit(FLAG_REQUESTED, &desc->flags), 775 716 test_bit(FLAG_EXPORT, &desc->flags)); 776 717 status = -EPERM; ··· 780 723 direction_may_change = false; 781 724 spin_unlock_irqrestore(&gpio_lock, flags); 782 725 783 - if (desc->chip->names && desc->chip->names[gpio - desc->chip->base]) 784 - ioname = desc->chip->names[gpio - desc->chip->base]; 726 + offset = gpio_chip_hwgpio(desc); 727 + if (desc->chip->names && desc->chip->names[offset]) 728 + ioname = desc->chip->names[offset]; 785 729 786 730 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 787 - desc, ioname ? ioname : "gpio%u", gpio); 731 + desc, ioname ? ioname : "gpio%u", 732 + desc_to_gpio(desc)); 788 733 if (IS_ERR(dev)) { 789 734 status = PTR_ERR(dev); 790 735 goto fail_unlock; ··· 802 743 goto fail_unregister_device; 803 744 } 804 745 805 - if (gpio_to_irq(gpio) >= 0 && (direction_may_change || 746 + if (gpiod_to_irq(desc) >= 0 && (direction_may_change || 806 747 !test_bit(FLAG_IS_OUT, &desc->flags))) { 807 748 status = device_create_file(dev, &dev_attr_edge); 808 749 if (status) ··· 817 758 device_unregister(dev); 818 759 fail_unlock: 819 760 mutex_unlock(&sysfs_lock); 820 - pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 761 + pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 762 + status); 821 763 return status; 764 + } 765 + 766 + int gpio_export(unsigned gpio, bool direction_may_change) 767 + { 768 + return gpiod_export(gpio_to_desc(gpio), direction_may_change); 822 769 } 823 770 EXPORT_SYMBOL_GPL(gpio_export); 824 771 ··· 844 779 * 845 780 * Returns zero on success, else an error. 846 781 */ 847 - int gpio_export_link(struct device *dev, const char *name, unsigned gpio) 782 + static int gpiod_export_link(struct device *dev, const char *name, 783 + struct gpio_desc *desc) 848 784 { 849 - struct gpio_desc *desc; 850 785 int status = -EINVAL; 851 786 852 - if (!gpio_is_valid(gpio)) 787 + if (!desc) 853 788 goto done; 854 789 855 790 mutex_lock(&sysfs_lock); 856 - 857 - desc = &gpio_desc[gpio]; 858 791 859 792 if (test_bit(FLAG_EXPORT, &desc->flags)) { 860 793 struct device *tdev; ··· 870 807 871 808 done: 872 809 if (status) 873 - pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 810 + pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 811 + status); 874 812 875 813 return status; 876 814 } 877 - EXPORT_SYMBOL_GPL(gpio_export_link); 878 815 816 + int gpio_export_link(struct device *dev, const char *name, unsigned gpio) 817 + { 818 + return gpiod_export_link(dev, name, gpio_to_desc(gpio)); 819 + } 820 + EXPORT_SYMBOL_GPL(gpio_export_link); 879 821 880 822 /** 881 823 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value ··· 894 826 * 895 827 * Returns zero on success, else an error. 896 828 */ 897 - int gpio_sysfs_set_active_low(unsigned gpio, int value) 829 + static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 898 830 { 899 - struct gpio_desc *desc; 900 831 struct device *dev = NULL; 901 832 int status = -EINVAL; 902 833 903 - if (!gpio_is_valid(gpio)) 834 + if (!desc) 904 835 goto done; 905 836 906 837 mutex_lock(&sysfs_lock); 907 - 908 - desc = &gpio_desc[gpio]; 909 838 910 839 if (test_bit(FLAG_EXPORT, &desc->flags)) { 911 840 dev = class_find_device(&gpio_class, NULL, desc, match_export); ··· 919 854 920 855 done: 921 856 if (status) 922 - pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 857 + pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 858 + status); 923 859 924 860 return status; 861 + } 862 + 863 + int gpio_sysfs_set_active_low(unsigned gpio, int value) 864 + { 865 + return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value); 925 866 } 926 867 EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); 927 868 ··· 937 866 * 938 867 * This is implicit on gpio_free(). 939 868 */ 940 - void gpio_unexport(unsigned gpio) 869 + static void gpiod_unexport(struct gpio_desc *desc) 941 870 { 942 - struct gpio_desc *desc; 943 871 int status = 0; 944 872 struct device *dev = NULL; 945 873 946 - if (!gpio_is_valid(gpio)) { 874 + if (!desc) { 947 875 status = -EINVAL; 948 876 goto done; 949 877 } 950 878 951 879 mutex_lock(&sysfs_lock); 952 - 953 - desc = &gpio_desc[gpio]; 954 880 955 881 if (test_bit(FLAG_EXPORT, &desc->flags)) { 956 882 ··· 960 892 } 961 893 962 894 mutex_unlock(&sysfs_lock); 895 + 963 896 if (dev) { 964 897 device_unregister(dev); 965 898 put_device(dev); 966 899 } 967 900 done: 968 901 if (status) 969 - pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 902 + pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 903 + status); 904 + } 905 + 906 + void gpio_unexport(unsigned gpio) 907 + { 908 + gpiod_unexport(gpio_to_desc(gpio)); 970 909 } 971 910 EXPORT_SYMBOL_GPL(gpio_unexport); 972 911 ··· 1079 1004 } 1080 1005 1081 1006 static inline void gpiochip_unexport(struct gpio_chip *chip) 1007 + { 1008 + } 1009 + 1010 + static inline int gpiod_export(struct gpio_desc *desc, 1011 + bool direction_may_change) 1012 + { 1013 + return -ENOSYS; 1014 + } 1015 + 1016 + static inline int gpiod_export_link(struct device *dev, const char *name, 1017 + struct gpio_desc *desc) 1018 + { 1019 + return -ENOSYS; 1020 + } 1021 + 1022 + static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) 1023 + { 1024 + return -ENOSYS; 1025 + } 1026 + 1027 + static inline void gpiod_unexport(struct gpio_desc *desc) 1082 1028 { 1083 1029 } 1084 1030 ··· 1378 1282 * on each other, and help provide better diagnostics in debugfs. 1379 1283 * They're called even less than the "set direction" calls. 1380 1284 */ 1381 - int gpio_request(unsigned gpio, const char *label) 1285 + static int gpiod_request(struct gpio_desc *desc, const char *label) 1382 1286 { 1383 - struct gpio_desc *desc; 1384 1287 struct gpio_chip *chip; 1385 1288 int status = -EPROBE_DEFER; 1386 1289 unsigned long flags; 1387 1290 1388 1291 spin_lock_irqsave(&gpio_lock, flags); 1389 1292 1390 - if (!gpio_is_valid(gpio)) { 1293 + if (!desc) { 1391 1294 status = -EINVAL; 1392 1295 goto done; 1393 1296 } 1394 - desc = &gpio_desc[gpio]; 1395 1297 chip = desc->chip; 1396 1298 if (chip == NULL) 1397 1299 goto done; ··· 1413 1319 if (chip->request) { 1414 1320 /* chip->request may sleep */ 1415 1321 spin_unlock_irqrestore(&gpio_lock, flags); 1416 - status = chip->request(chip, gpio - chip->base); 1322 + status = chip->request(chip, gpio_chip_hwgpio(desc)); 1417 1323 spin_lock_irqsave(&gpio_lock, flags); 1418 1324 1419 1325 if (status < 0) { ··· 1426 1332 if (chip->get_direction) { 1427 1333 /* chip->get_direction may sleep */ 1428 1334 spin_unlock_irqrestore(&gpio_lock, flags); 1429 - gpio_get_direction(gpio); 1335 + gpiod_get_direction(desc); 1430 1336 spin_lock_irqsave(&gpio_lock, flags); 1431 1337 } 1432 1338 done: 1433 1339 if (status) 1434 - pr_debug("gpio_request: gpio-%d (%s) status %d\n", 1435 - gpio, label ? : "?", status); 1340 + pr_debug("_gpio_request: gpio-%d (%s) status %d\n", 1341 + desc ? desc_to_gpio(desc) : -1, 1342 + label ? : "?", status); 1436 1343 spin_unlock_irqrestore(&gpio_lock, flags); 1437 1344 return status; 1438 1345 } 1346 + 1347 + int gpio_request(unsigned gpio, const char *label) 1348 + { 1349 + return gpiod_request(gpio_to_desc(gpio), label); 1350 + } 1439 1351 EXPORT_SYMBOL_GPL(gpio_request); 1440 1352 1441 - void gpio_free(unsigned gpio) 1353 + static void gpiod_free(struct gpio_desc *desc) 1442 1354 { 1443 1355 unsigned long flags; 1444 - struct gpio_desc *desc; 1445 1356 struct gpio_chip *chip; 1446 1357 1447 1358 might_sleep(); 1448 1359 1449 - if (!gpio_is_valid(gpio)) { 1360 + if (!desc) { 1450 1361 WARN_ON(extra_checks); 1451 1362 return; 1452 1363 } 1453 1364 1454 - gpio_unexport(gpio); 1365 + gpiod_unexport(desc); 1455 1366 1456 1367 spin_lock_irqsave(&gpio_lock, flags); 1457 1368 1458 - desc = &gpio_desc[gpio]; 1459 1369 chip = desc->chip; 1460 1370 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { 1461 1371 if (chip->free) { 1462 1372 spin_unlock_irqrestore(&gpio_lock, flags); 1463 1373 might_sleep_if(chip->can_sleep); 1464 - chip->free(chip, gpio - chip->base); 1374 + chip->free(chip, gpio_chip_hwgpio(desc)); 1465 1375 spin_lock_irqsave(&gpio_lock, flags); 1466 1376 } 1467 1377 desc_set_label(desc, NULL); ··· 1479 1381 1480 1382 spin_unlock_irqrestore(&gpio_lock, flags); 1481 1383 } 1384 + 1385 + void gpio_free(unsigned gpio) 1386 + { 1387 + gpiod_free(gpio_to_desc(gpio)); 1388 + } 1482 1389 EXPORT_SYMBOL_GPL(gpio_free); 1483 1390 1484 1391 /** ··· 1494 1391 */ 1495 1392 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) 1496 1393 { 1394 + struct gpio_desc *desc; 1497 1395 int err; 1498 1396 1499 - err = gpio_request(gpio, label); 1397 + desc = gpio_to_desc(gpio); 1398 + 1399 + err = gpiod_request(desc, label); 1500 1400 if (err) 1501 1401 return err; 1502 1402 1503 1403 if (flags & GPIOF_OPEN_DRAIN) 1504 - set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags); 1404 + set_bit(FLAG_OPEN_DRAIN, &desc->flags); 1505 1405 1506 1406 if (flags & GPIOF_OPEN_SOURCE) 1507 - set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags); 1407 + set_bit(FLAG_OPEN_SOURCE, &desc->flags); 1508 1408 1509 1409 if (flags & GPIOF_DIR_IN) 1510 - err = gpio_direction_input(gpio); 1410 + err = gpiod_direction_input(desc); 1511 1411 else 1512 - err = gpio_direction_output(gpio, 1412 + err = gpiod_direction_output(desc, 1513 1413 (flags & GPIOF_INIT_HIGH) ? 1 : 0); 1514 1414 1515 1415 if (err) 1516 1416 goto free_gpio; 1517 1417 1518 1418 if (flags & GPIOF_EXPORT) { 1519 - err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE); 1419 + err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); 1520 1420 if (err) 1521 1421 goto free_gpio; 1522 1422 } ··· 1527 1421 return 0; 1528 1422 1529 1423 free_gpio: 1530 - gpio_free(gpio); 1424 + gpiod_free(desc); 1531 1425 return err; 1532 1426 } 1533 1427 EXPORT_SYMBOL_GPL(gpio_request_one); ··· 1583 1477 const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) 1584 1478 { 1585 1479 unsigned gpio = chip->base + offset; 1480 + struct gpio_desc *desc = &gpio_desc[gpio]; 1586 1481 1587 - if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) 1482 + if (!gpio_is_valid(gpio) || desc->chip != chip) 1588 1483 return NULL; 1589 - if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) 1484 + if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) 1590 1485 return NULL; 1591 1486 #ifdef CONFIG_DEBUG_FS 1592 - return gpio_desc[gpio].label; 1487 + return desc->label; 1593 1488 #else 1594 1489 return "?"; 1595 1490 #endif ··· 1607 1500 * rely on gpio_request() having been called beforehand. 1608 1501 */ 1609 1502 1610 - int gpio_direction_input(unsigned gpio) 1503 + static int gpiod_direction_input(struct gpio_desc *desc) 1611 1504 { 1612 1505 unsigned long flags; 1613 1506 struct gpio_chip *chip; 1614 - struct gpio_desc *desc = &gpio_desc[gpio]; 1615 1507 int status = -EINVAL; 1508 + int offset; 1616 1509 1617 1510 spin_lock_irqsave(&gpio_lock, flags); 1618 1511 1619 - if (!gpio_is_valid(gpio)) 1512 + if (!desc) 1620 1513 goto fail; 1621 1514 chip = desc->chip; 1622 1515 if (!chip || !chip->get || !chip->direction_input) 1623 1516 goto fail; 1624 - gpio -= chip->base; 1625 - if (gpio >= chip->ngpio) 1626 - goto fail; 1627 - status = gpio_ensure_requested(desc, gpio); 1517 + status = gpio_ensure_requested(desc); 1628 1518 if (status < 0) 1629 1519 goto fail; 1630 1520 ··· 1631 1527 1632 1528 might_sleep_if(chip->can_sleep); 1633 1529 1530 + offset = gpio_chip_hwgpio(desc); 1634 1531 if (status) { 1635 - status = chip->request(chip, gpio); 1532 + status = chip->request(chip, offset); 1636 1533 if (status < 0) { 1637 1534 pr_debug("GPIO-%d: chip request fail, %d\n", 1638 - chip->base + gpio, status); 1535 + desc_to_gpio(desc), status); 1639 1536 /* and it's not available to anyone else ... 1640 1537 * gpio_request() is the fully clean solution. 1641 1538 */ ··· 1644 1539 } 1645 1540 } 1646 1541 1647 - status = chip->direction_input(chip, gpio); 1542 + status = chip->direction_input(chip, offset); 1648 1543 if (status == 0) 1649 1544 clear_bit(FLAG_IS_OUT, &desc->flags); 1650 1545 1651 - trace_gpio_direction(chip->base + gpio, 1, status); 1546 + trace_gpio_direction(desc_to_gpio(desc), 1, status); 1652 1547 lose: 1653 1548 return status; 1654 1549 fail: 1655 1550 spin_unlock_irqrestore(&gpio_lock, flags); 1656 - if (status) 1551 + if (status) { 1552 + int gpio = -1; 1553 + if (desc) 1554 + gpio = desc_to_gpio(desc); 1657 1555 pr_debug("%s: gpio-%d status %d\n", 1658 1556 __func__, gpio, status); 1557 + } 1659 1558 return status; 1559 + } 1560 + 1561 + int gpio_direction_input(unsigned gpio) 1562 + { 1563 + return gpiod_direction_input(gpio_to_desc(gpio)); 1660 1564 } 1661 1565 EXPORT_SYMBOL_GPL(gpio_direction_input); 1662 1566 1663 - int gpio_direction_output(unsigned gpio, int value) 1567 + static int gpiod_direction_output(struct gpio_desc *desc, int value) 1664 1568 { 1665 1569 unsigned long flags; 1666 1570 struct gpio_chip *chip; 1667 - struct gpio_desc *desc = &gpio_desc[gpio]; 1668 1571 int status = -EINVAL; 1572 + int offset; 1669 1573 1670 1574 /* Open drain pin should not be driven to 1 */ 1671 1575 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1672 - return gpio_direction_input(gpio); 1576 + return gpiod_direction_input(desc); 1673 1577 1674 1578 /* Open source pin should not be driven to 0 */ 1675 1579 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1676 - return gpio_direction_input(gpio); 1580 + return gpiod_direction_input(desc); 1677 1581 1678 1582 spin_lock_irqsave(&gpio_lock, flags); 1679 1583 1680 - if (!gpio_is_valid(gpio)) 1584 + if (!desc) 1681 1585 goto fail; 1682 1586 chip = desc->chip; 1683 1587 if (!chip || !chip->set || !chip->direction_output) 1684 1588 goto fail; 1685 - gpio -= chip->base; 1686 - if (gpio >= chip->ngpio) 1687 - goto fail; 1688 - status = gpio_ensure_requested(desc, gpio); 1589 + status = gpio_ensure_requested(desc); 1689 1590 if (status < 0) 1690 1591 goto fail; 1691 1592 ··· 1701 1590 1702 1591 might_sleep_if(chip->can_sleep); 1703 1592 1593 + offset = gpio_chip_hwgpio(desc); 1704 1594 if (status) { 1705 - status = chip->request(chip, gpio); 1595 + status = chip->request(chip, offset); 1706 1596 if (status < 0) { 1707 1597 pr_debug("GPIO-%d: chip request fail, %d\n", 1708 - chip->base + gpio, status); 1598 + desc_to_gpio(desc), status); 1709 1599 /* and it's not available to anyone else ... 1710 1600 * gpio_request() is the fully clean solution. 1711 1601 */ ··· 1714 1602 } 1715 1603 } 1716 1604 1717 - status = chip->direction_output(chip, gpio, value); 1605 + status = chip->direction_output(chip, offset, value); 1718 1606 if (status == 0) 1719 1607 set_bit(FLAG_IS_OUT, &desc->flags); 1720 - trace_gpio_value(chip->base + gpio, 0, value); 1721 - trace_gpio_direction(chip->base + gpio, 0, status); 1608 + trace_gpio_value(desc_to_gpio(desc), 0, value); 1609 + trace_gpio_direction(desc_to_gpio(desc), 0, status); 1722 1610 lose: 1723 1611 return status; 1724 1612 fail: 1725 1613 spin_unlock_irqrestore(&gpio_lock, flags); 1726 - if (status) 1614 + if (status) { 1615 + int gpio = -1; 1616 + if (desc) 1617 + gpio = desc_to_gpio(desc); 1727 1618 pr_debug("%s: gpio-%d status %d\n", 1728 1619 __func__, gpio, status); 1620 + } 1729 1621 return status; 1622 + } 1623 + 1624 + int gpio_direction_output(unsigned gpio, int value) 1625 + { 1626 + return gpiod_direction_output(gpio_to_desc(gpio), value); 1730 1627 } 1731 1628 EXPORT_SYMBOL_GPL(gpio_direction_output); 1732 1629 ··· 1744 1623 * @gpio: the gpio to set debounce time 1745 1624 * @debounce: debounce time is microseconds 1746 1625 */ 1747 - int gpio_set_debounce(unsigned gpio, unsigned debounce) 1626 + static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) 1748 1627 { 1749 1628 unsigned long flags; 1750 1629 struct gpio_chip *chip; 1751 - struct gpio_desc *desc = &gpio_desc[gpio]; 1752 1630 int status = -EINVAL; 1631 + int offset; 1753 1632 1754 1633 spin_lock_irqsave(&gpio_lock, flags); 1755 1634 1756 - if (!gpio_is_valid(gpio)) 1635 + if (!desc) 1757 1636 goto fail; 1758 1637 chip = desc->chip; 1759 1638 if (!chip || !chip->set || !chip->set_debounce) 1760 1639 goto fail; 1761 - gpio -= chip->base; 1762 - if (gpio >= chip->ngpio) 1763 - goto fail; 1764 - status = gpio_ensure_requested(desc, gpio); 1640 + 1641 + status = gpio_ensure_requested(desc); 1765 1642 if (status < 0) 1766 1643 goto fail; 1767 1644 ··· 1769 1650 1770 1651 might_sleep_if(chip->can_sleep); 1771 1652 1772 - return chip->set_debounce(chip, gpio, debounce); 1653 + offset = gpio_chip_hwgpio(desc); 1654 + return chip->set_debounce(chip, offset, debounce); 1773 1655 1774 1656 fail: 1775 1657 spin_unlock_irqrestore(&gpio_lock, flags); 1776 - if (status) 1658 + if (status) { 1659 + int gpio = -1; 1660 + if (desc) 1661 + gpio = desc_to_gpio(desc); 1777 1662 pr_debug("%s: gpio-%d status %d\n", 1778 1663 __func__, gpio, status); 1664 + } 1779 1665 1780 1666 return status; 1667 + } 1668 + 1669 + int gpio_set_debounce(unsigned gpio, unsigned debounce) 1670 + { 1671 + return gpiod_set_debounce(gpio_to_desc(gpio), debounce); 1781 1672 } 1782 1673 EXPORT_SYMBOL_GPL(gpio_set_debounce); 1783 1674 ··· 1822 1693 * It returns the zero or nonzero value provided by the associated 1823 1694 * gpio_chip.get() method; or zero if no such method is provided. 1824 1695 */ 1825 - int __gpio_get_value(unsigned gpio) 1696 + static int gpiod_get_value(struct gpio_desc *desc) 1826 1697 { 1827 1698 struct gpio_chip *chip; 1828 1699 int value; 1700 + int offset; 1829 1701 1830 - chip = gpio_to_chip(gpio); 1702 + chip = desc->chip; 1703 + offset = gpio_chip_hwgpio(desc); 1831 1704 /* Should be using gpio_get_value_cansleep() */ 1832 1705 WARN_ON(chip->can_sleep); 1833 - value = chip->get ? chip->get(chip, gpio - chip->base) : 0; 1834 - trace_gpio_value(gpio, 1, value); 1706 + value = chip->get ? chip->get(chip, offset) : 0; 1707 + trace_gpio_value(desc_to_gpio(desc), 1, value); 1835 1708 return value; 1709 + } 1710 + 1711 + int __gpio_get_value(unsigned gpio) 1712 + { 1713 + return gpiod_get_value(gpio_to_desc(gpio)); 1836 1714 } 1837 1715 EXPORT_SYMBOL_GPL(__gpio_get_value); 1838 1716 ··· 1849 1713 * @chip: Gpio chip. 1850 1714 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1851 1715 */ 1852 - static void _gpio_set_open_drain_value(unsigned gpio, 1853 - struct gpio_chip *chip, int value) 1716 + static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) 1854 1717 { 1855 1718 int err = 0; 1719 + struct gpio_chip *chip = desc->chip; 1720 + int offset = gpio_chip_hwgpio(desc); 1721 + 1856 1722 if (value) { 1857 - err = chip->direction_input(chip, gpio - chip->base); 1723 + err = chip->direction_input(chip, offset); 1858 1724 if (!err) 1859 - clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1725 + clear_bit(FLAG_IS_OUT, &desc->flags); 1860 1726 } else { 1861 - err = chip->direction_output(chip, gpio - chip->base, 0); 1727 + err = chip->direction_output(chip, offset, 0); 1862 1728 if (!err) 1863 - set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1729 + set_bit(FLAG_IS_OUT, &desc->flags); 1864 1730 } 1865 - trace_gpio_direction(gpio, value, err); 1731 + trace_gpio_direction(desc_to_gpio(desc), value, err); 1866 1732 if (err < 0) 1867 1733 pr_err("%s: Error in set_value for open drain gpio%d err %d\n", 1868 - __func__, gpio, err); 1734 + __func__, desc_to_gpio(desc), err); 1869 1735 } 1870 1736 1871 1737 /* ··· 1876 1738 * @chip: Gpio chip. 1877 1739 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1878 1740 */ 1879 - static void _gpio_set_open_source_value(unsigned gpio, 1880 - struct gpio_chip *chip, int value) 1741 + static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) 1881 1742 { 1882 1743 int err = 0; 1744 + struct gpio_chip *chip = desc->chip; 1745 + int offset = gpio_chip_hwgpio(desc); 1746 + 1883 1747 if (value) { 1884 - err = chip->direction_output(chip, gpio - chip->base, 1); 1748 + err = chip->direction_output(chip, offset, 1); 1885 1749 if (!err) 1886 - set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1750 + set_bit(FLAG_IS_OUT, &desc->flags); 1887 1751 } else { 1888 - err = chip->direction_input(chip, gpio - chip->base); 1752 + err = chip->direction_input(chip, offset); 1889 1753 if (!err) 1890 - clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1754 + clear_bit(FLAG_IS_OUT, &desc->flags); 1891 1755 } 1892 - trace_gpio_direction(gpio, !value, err); 1756 + trace_gpio_direction(desc_to_gpio(desc), !value, err); 1893 1757 if (err < 0) 1894 1758 pr_err("%s: Error in set_value for open source gpio%d err %d\n", 1895 - __func__, gpio, err); 1759 + __func__, desc_to_gpio(desc), err); 1896 1760 } 1897 - 1898 1761 1899 1762 /** 1900 1763 * __gpio_set_value() - assign a gpio's value ··· 1906 1767 * This is used directly or indirectly to implement gpio_set_value(). 1907 1768 * It invokes the associated gpio_chip.set() method. 1908 1769 */ 1909 - void __gpio_set_value(unsigned gpio, int value) 1770 + static void gpiod_set_value(struct gpio_desc *desc, int value) 1910 1771 { 1911 1772 struct gpio_chip *chip; 1912 1773 1913 - chip = gpio_to_chip(gpio); 1774 + chip = desc->chip; 1914 1775 /* Should be using gpio_set_value_cansleep() */ 1915 1776 WARN_ON(chip->can_sleep); 1916 - trace_gpio_value(gpio, 0, value); 1917 - if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) 1918 - _gpio_set_open_drain_value(gpio, chip, value); 1919 - else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) 1920 - _gpio_set_open_source_value(gpio, chip, value); 1777 + trace_gpio_value(desc_to_gpio(desc), 0, value); 1778 + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1779 + _gpio_set_open_drain_value(desc, value); 1780 + else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1781 + _gpio_set_open_source_value(desc, value); 1921 1782 else 1922 - chip->set(chip, gpio - chip->base, value); 1783 + chip->set(chip, gpio_chip_hwgpio(desc), value); 1784 + } 1785 + 1786 + void __gpio_set_value(unsigned gpio, int value) 1787 + { 1788 + return gpiod_set_value(gpio_to_desc(gpio), value); 1923 1789 } 1924 1790 EXPORT_SYMBOL_GPL(__gpio_set_value); 1925 1791 ··· 1936 1792 * This is used directly or indirectly to implement gpio_cansleep(). It 1937 1793 * returns nonzero if access reading or writing the GPIO value can sleep. 1938 1794 */ 1795 + static int gpiod_cansleep(struct gpio_desc *desc) 1796 + { 1797 + /* only call this on GPIOs that are valid! */ 1798 + return desc->chip->can_sleep; 1799 + } 1800 + 1939 1801 int __gpio_cansleep(unsigned gpio) 1940 1802 { 1941 - struct gpio_chip *chip; 1942 - 1943 - /* only call this on GPIOs that are valid! */ 1944 - chip = gpio_to_chip(gpio); 1945 - 1946 - return chip->can_sleep; 1803 + return gpiod_cansleep(gpio_to_desc(gpio)); 1947 1804 } 1948 1805 EXPORT_SYMBOL_GPL(__gpio_cansleep); 1949 1806 ··· 1957 1812 * It returns the number of the IRQ signaled by this (input) GPIO, 1958 1813 * or a negative errno. 1959 1814 */ 1960 - int __gpio_to_irq(unsigned gpio) 1815 + static int gpiod_to_irq(struct gpio_desc *desc) 1961 1816 { 1962 1817 struct gpio_chip *chip; 1818 + int offset; 1963 1819 1964 - chip = gpio_to_chip(gpio); 1965 - return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; 1820 + chip = desc->chip; 1821 + offset = gpio_chip_hwgpio(desc); 1822 + return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; 1823 + } 1824 + 1825 + int __gpio_to_irq(unsigned gpio) 1826 + { 1827 + return gpiod_to_irq(gpio_to_desc(gpio)); 1966 1828 } 1967 1829 EXPORT_SYMBOL_GPL(__gpio_to_irq); 1968 - 1969 1830 1970 1831 1971 1832 /* There's no value in making it easy to inline GPIO calls that may sleep. 1972 1833 * Common examples include ones connected to I2C or SPI chips. 1973 1834 */ 1974 1835 1975 - int gpio_get_value_cansleep(unsigned gpio) 1836 + static int gpiod_get_value_cansleep(struct gpio_desc *desc) 1976 1837 { 1977 1838 struct gpio_chip *chip; 1978 1839 int value; 1840 + int offset; 1979 1841 1980 1842 might_sleep_if(extra_checks); 1981 - chip = gpio_to_chip(gpio); 1982 - value = chip->get ? chip->get(chip, gpio - chip->base) : 0; 1983 - trace_gpio_value(gpio, 1, value); 1843 + chip = desc->chip; 1844 + offset = gpio_chip_hwgpio(desc); 1845 + value = chip->get ? chip->get(chip, offset) : 0; 1846 + trace_gpio_value(desc_to_gpio(desc), 1, value); 1984 1847 return value; 1848 + } 1849 + 1850 + int gpio_get_value_cansleep(unsigned gpio) 1851 + { 1852 + return gpiod_get_value_cansleep(gpio_to_desc(gpio)); 1985 1853 } 1986 1854 EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 1987 1855 1988 - void gpio_set_value_cansleep(unsigned gpio, int value) 1856 + static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) 1989 1857 { 1990 1858 struct gpio_chip *chip; 1991 1859 1992 1860 might_sleep_if(extra_checks); 1993 - chip = gpio_to_chip(gpio); 1994 - trace_gpio_value(gpio, 0, value); 1995 - if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) 1996 - _gpio_set_open_drain_value(gpio, chip, value); 1997 - else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) 1998 - _gpio_set_open_source_value(gpio, chip, value); 1861 + chip = desc->chip; 1862 + trace_gpio_value(desc_to_gpio(desc), 0, value); 1863 + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1864 + _gpio_set_open_drain_value(desc, value); 1865 + else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1866 + _gpio_set_open_source_value(desc, value); 1999 1867 else 2000 - chip->set(chip, gpio - chip->base, value); 1868 + chip->set(chip, gpio_chip_hwgpio(desc), value); 1869 + } 1870 + 1871 + void gpio_set_value_cansleep(unsigned gpio, int value) 1872 + { 1873 + return gpiod_set_value_cansleep(gpio_to_desc(gpio), value); 2001 1874 } 2002 1875 EXPORT_SYMBOL_GPL(gpio_set_value_cansleep); 2003 - 2004 1876 2005 1877 #ifdef CONFIG_DEBUG_FS 2006 1878 ··· 2032 1870 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) 2033 1871 continue; 2034 1872 2035 - gpio_get_direction(gpio); 1873 + gpiod_get_direction(gdesc); 2036 1874 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); 2037 1875 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", 2038 1876 gpio, gdesc->label,