Commit 372e722ea4dd4ca11c3d04845e11cbc15f32144c

Authored by Alexandre Courbot
Committed by Grant Likely
1 parent 83cabe33eb

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>

Showing 1 changed file with 338 additions and 176 deletions Side-by-side Diff

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