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