aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpiolib.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-02-26 12:35:29 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-02-26 12:35:29 -0500
commit4c8c225abf972ce422c241579ce1d4d27eaeb166 (patch)
tree77bc67defdc53c494b20632e66b82ce9be3c06af /drivers/gpio/gpiolib.c
parent3eb05225ee8efb81fe50558f5f9d94e7477ade8f (diff)
parent9170100ee46402af6d318134525c728027318d67 (diff)
Merge tag 'gpio-for-linus' of git://git.secretlab.ca/git/linux
Pull GPIO changes from Grant Likely: "This branch contains the usual set of individual driver improvements and bug fixes, as well as updates to the core code. The more notable changes include: - Internally add new API for referencing GPIOs by gpio_desc instead of number. Eventually this will become a public API - ACPI GPIO binding support" * tag 'gpio-for-linus' of git://git.secretlab.ca/git/linux: (33 commits) arm64: select ARCH_WANT_OPTIONAL_GPIOLIB gpio: em: Use irq_domain_add_simple() to fix runtime error gpio: using common order: let 'static const' instead of 'const static' gpio/vt8500: memory cleanup missing gpiolib: Fix locking on gpio debugfs files gpiolib: let gpio_chip reference its descriptors gpiolib: use descriptors internally gpiolib: use gpio_chips list in gpiochip_find_base gpiolib: use gpio_chips list in sysfs ops gpiolib: use gpio_chips list in gpiochip_find gpiolib: use gpio_chips list in gpiolib_sysfs_init gpiolib: link all gpio_chips using a list gpio/langwell: cleanup driver gpio/langwell: Add Cloverview ids to pci device table gpio/lynxpoint: add chipset gpio driver. gpiolib: add missing braces in gpio_direction_show gpiolib-acpi: Fix error checks in interrupt requesting gpio: mpc8xxx: don't set IRQ_TYPE_NONE when creating irq mapping gpiolib: remove gpiochip_reserve() arm: pxa: tosa: do not use gpiochip_reserve() ...
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c759
1 files changed, 451 insertions, 308 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5359ca78130f..4828fe7c66cb 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -3,6 +3,7 @@
3#include <linux/interrupt.h> 3#include <linux/interrupt.h>
4#include <linux/irq.h> 4#include <linux/irq.h>
5#include <linux/spinlock.h> 5#include <linux/spinlock.h>
6#include <linux/list.h>
6#include <linux/device.h> 7#include <linux/device.h>
7#include <linux/err.h> 8#include <linux/err.h>
8#include <linux/debugfs.h> 9#include <linux/debugfs.h>
@@ -52,14 +53,13 @@ struct gpio_desc {
52/* flag symbols are bit numbers */ 53/* flag symbols are bit numbers */
53#define FLAG_REQUESTED 0 54#define FLAG_REQUESTED 0
54#define FLAG_IS_OUT 1 55#define FLAG_IS_OUT 1
55#define FLAG_RESERVED 2 56#define FLAG_EXPORT 2 /* protected by sysfs_lock */
56#define FLAG_EXPORT 3 /* protected by sysfs_lock */ 57#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
57#define FLAG_SYSFS 4 /* exported via /sys/class/gpio/control */ 58#define FLAG_TRIG_FALL 4 /* trigger on falling edge */
58#define FLAG_TRIG_FALL 5 /* trigger on falling edge */ 59#define FLAG_TRIG_RISE 5 /* trigger on rising edge */
59#define FLAG_TRIG_RISE 6 /* trigger on rising edge */ 60#define FLAG_ACTIVE_LOW 6 /* sysfs value has active low */
60#define FLAG_ACTIVE_LOW 7 /* sysfs value has active low */ 61#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
61#define FLAG_OPEN_DRAIN 8 /* Gpio is open drain type */ 62#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
62#define FLAG_OPEN_SOURCE 9 /* Gpio is open source type */
63 63
64#define ID_SHIFT 16 /* add new flags before this one */ 64#define ID_SHIFT 16 /* add new flags before this one */
65 65
@@ -72,10 +72,36 @@ struct gpio_desc {
72}; 72};
73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; 73static struct gpio_desc gpio_desc[ARCH_NR_GPIOS];
74 74
75#define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio)
76
77static LIST_HEAD(gpio_chips);
78
75#ifdef CONFIG_GPIO_SYSFS 79#ifdef CONFIG_GPIO_SYSFS
76static DEFINE_IDR(dirent_idr); 80static DEFINE_IDR(dirent_idr);
77#endif 81#endif
78 82
83/*
84 * Internal gpiod_* API using descriptors instead of the integer namespace.
85 * Most of this should eventually go public.
86 */
87static int gpiod_request(struct gpio_desc *desc, const char *label);
88static void gpiod_free(struct gpio_desc *desc);
89static int gpiod_direction_input(struct gpio_desc *desc);
90static int gpiod_direction_output(struct gpio_desc *desc, int value);
91static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce);
92static int gpiod_get_value_cansleep(struct gpio_desc *desc);
93static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value);
94static int gpiod_get_value(struct gpio_desc *desc);
95static void gpiod_set_value(struct gpio_desc *desc, int value);
96static int gpiod_cansleep(struct gpio_desc *desc);
97static int gpiod_to_irq(struct gpio_desc *desc);
98static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
99static int gpiod_export_link(struct device *dev, const char *name,
100 struct gpio_desc *desc);
101static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
102static void gpiod_unexport(struct gpio_desc *desc);
103
104
79static inline void desc_set_label(struct gpio_desc *d, const char *label) 105static inline void desc_set_label(struct gpio_desc *d, const char *label)
80{ 106{
81#ifdef CONFIG_DEBUG_FS 107#ifdef CONFIG_DEBUG_FS
@@ -83,6 +109,36 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
83#endif 109#endif
84} 110}
85 111
112/*
113 * Return the GPIO number of the passed descriptor relative to its chip
114 */
115static int gpio_chip_hwgpio(const struct gpio_desc *desc)
116{
117 return desc - &desc->chip->desc[0];
118}
119
120/**
121 * Convert a GPIO number to its descriptor
122 */
123static struct gpio_desc *gpio_to_desc(unsigned gpio)
124{
125 if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio))
126 return NULL;
127 else
128 return &gpio_desc[gpio];
129}
130
131/**
132 * Convert a GPIO descriptor to the integer namespace.
133 * This should disappear in the future but is needed since we still
134 * use GPIO numbers for error messages and sysfs nodes
135 */
136static int desc_to_gpio(const struct gpio_desc *desc)
137{
138 return desc->chip->base + gpio_chip_hwgpio(desc);
139}
140
141
86/* Warn when drivers omit gpio_request() calls -- legal but ill-advised 142/* Warn when drivers omit gpio_request() calls -- legal but ill-advised
87 * when setting direction, and otherwise illegal. Until board setup code 143 * when setting direction, and otherwise illegal. Until board setup code
88 * and drivers use explicit requests everywhere (which won't happen when 144 * and drivers use explicit requests everywhere (which won't happen when
@@ -94,10 +150,10 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
94 * only "legal" in the sense that (old) code using it won't break yet, 150 * only "legal" in the sense that (old) code using it won't break yet,
95 * but instead only triggers a WARN() stack dump. 151 * but instead only triggers a WARN() stack dump.
96 */ 152 */
97static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset) 153static int gpio_ensure_requested(struct gpio_desc *desc)
98{ 154{
99 const struct gpio_chip *chip = desc->chip; 155 const struct gpio_chip *chip = desc->chip;
100 const int gpio = chip->base + offset; 156 const int gpio = desc_to_gpio(desc);
101 157
102 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, 158 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
103 "autorequest GPIO-%d\n", gpio)) { 159 "autorequest GPIO-%d\n", gpio)) {
@@ -116,95 +172,54 @@ static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
116} 172}
117 173
118/* caller holds gpio_lock *OR* gpio is marked as requested */ 174/* caller holds gpio_lock *OR* gpio is marked as requested */
175static struct gpio_chip *gpiod_to_chip(struct gpio_desc *desc)
176{
177 return desc->chip;
178}
179
119struct gpio_chip *gpio_to_chip(unsigned gpio) 180struct gpio_chip *gpio_to_chip(unsigned gpio)
120{ 181{
121 return gpio_desc[gpio].chip; 182 return gpiod_to_chip(gpio_to_desc(gpio));
122} 183}
123 184
124/* dynamic allocation of GPIOs, e.g. on a hotplugged device */ 185/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
125static int gpiochip_find_base(int ngpio) 186static int gpiochip_find_base(int ngpio)
126{ 187{
127 int i; 188 struct gpio_chip *chip;
128 int spare = 0; 189 int base = ARCH_NR_GPIOS - ngpio;
129 int base = -ENOSPC;
130
131 for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
132 struct gpio_desc *desc = &gpio_desc[i];
133 struct gpio_chip *chip = desc->chip;
134 190
135 if (!chip && !test_bit(FLAG_RESERVED, &desc->flags)) { 191 list_for_each_entry_reverse(chip, &gpio_chips, list) {
136 spare++; 192 /* found a free space? */
137 if (spare == ngpio) { 193 if (chip->base + chip->ngpio <= base)
138 base = i; 194 break;
139 break; 195 else
140 } 196 /* nope, check the space right before the chip */
141 } else { 197 base = chip->base - ngpio;
142 spare = 0;
143 if (chip)
144 i -= chip->ngpio - 1;
145 }
146 } 198 }
147 199
148 if (gpio_is_valid(base)) 200 if (gpio_is_valid(base)) {
149 pr_debug("%s: found new base at %d\n", __func__, base); 201 pr_debug("%s: found new base at %d\n", __func__, base);
150 return base; 202 return base;
151} 203 } else {
152 204 pr_err("%s: cannot find free range\n", __func__);
153/** 205 return -ENOSPC;
154 * gpiochip_reserve() - reserve range of gpios to use with platform code only
155 * @start: starting gpio number
156 * @ngpio: number of gpios to reserve
157 * Context: platform init, potentially before irqs or kmalloc will work
158 *
159 * Returns a negative errno if any gpio within the range is already reserved
160 * or registered, else returns zero as a success code. Use this function
161 * to mark a range of gpios as unavailable for dynamic gpio number allocation,
162 * for example because its driver support is not yet loaded.
163 */
164int __init gpiochip_reserve(int start, int ngpio)
165{
166 int ret = 0;
167 unsigned long flags;
168 int i;
169
170 if (!gpio_is_valid(start) || !gpio_is_valid(start + ngpio - 1))
171 return -EINVAL;
172
173 spin_lock_irqsave(&gpio_lock, flags);
174
175 for (i = start; i < start + ngpio; i++) {
176 struct gpio_desc *desc = &gpio_desc[i];
177
178 if (desc->chip || test_bit(FLAG_RESERVED, &desc->flags)) {
179 ret = -EBUSY;
180 goto err;
181 }
182
183 set_bit(FLAG_RESERVED, &desc->flags);
184 } 206 }
185
186 pr_debug("%s: reserved gpios from %d to %d\n",
187 __func__, start, start + ngpio - 1);
188err:
189 spin_unlock_irqrestore(&gpio_lock, flags);
190
191 return ret;
192} 207}
193 208
194/* caller ensures gpio is valid and requested, chip->get_direction may sleep */ 209/* caller ensures gpio is valid and requested, chip->get_direction may sleep */
195static int gpio_get_direction(unsigned gpio) 210static int gpiod_get_direction(struct gpio_desc *desc)
196{ 211{
197 struct gpio_chip *chip; 212 struct gpio_chip *chip;
198 struct gpio_desc *desc = &gpio_desc[gpio]; 213 unsigned offset;
199 int status = -EINVAL; 214 int status = -EINVAL;
200 215
201 chip = gpio_to_chip(gpio); 216 chip = gpiod_to_chip(desc);
202 gpio -= chip->base; 217 offset = gpio_chip_hwgpio(desc);
203 218
204 if (!chip->get_direction) 219 if (!chip->get_direction)
205 return status; 220 return status;
206 221
207 status = chip->get_direction(chip, gpio); 222 status = chip->get_direction(chip, offset);
208 if (status > 0) { 223 if (status > 0) {
209 /* GPIOF_DIR_IN, or other positive */ 224 /* GPIOF_DIR_IN, or other positive */
210 status = 1; 225 status = 1;
@@ -248,19 +263,19 @@ static DEFINE_MUTEX(sysfs_lock);
248static ssize_t gpio_direction_show(struct device *dev, 263static ssize_t gpio_direction_show(struct device *dev,
249 struct device_attribute *attr, char *buf) 264 struct device_attribute *attr, char *buf)
250{ 265{
251 const struct gpio_desc *desc = dev_get_drvdata(dev); 266 struct gpio_desc *desc = dev_get_drvdata(dev);
252 unsigned gpio = desc - gpio_desc;
253 ssize_t status; 267 ssize_t status;
254 268
255 mutex_lock(&sysfs_lock); 269 mutex_lock(&sysfs_lock);
256 270
257 if (!test_bit(FLAG_EXPORT, &desc->flags)) 271 if (!test_bit(FLAG_EXPORT, &desc->flags)) {
258 status = -EIO; 272 status = -EIO;
259 else 273 } else {
260 gpio_get_direction(gpio); 274 gpiod_get_direction(desc);
261 status = sprintf(buf, "%s\n", 275 status = sprintf(buf, "%s\n",
262 test_bit(FLAG_IS_OUT, &desc->flags) 276 test_bit(FLAG_IS_OUT, &desc->flags)
263 ? "out" : "in"); 277 ? "out" : "in");
278 }
264 279
265 mutex_unlock(&sysfs_lock); 280 mutex_unlock(&sysfs_lock);
266 return status; 281 return status;
@@ -269,8 +284,7 @@ static ssize_t gpio_direction_show(struct device *dev,
269static ssize_t gpio_direction_store(struct device *dev, 284static ssize_t gpio_direction_store(struct device *dev,
270 struct device_attribute *attr, const char *buf, size_t size) 285 struct device_attribute *attr, const char *buf, size_t size)
271{ 286{
272 const struct gpio_desc *desc = dev_get_drvdata(dev); 287 struct gpio_desc *desc = dev_get_drvdata(dev);
273 unsigned gpio = desc - gpio_desc;
274 ssize_t status; 288 ssize_t status;
275 289
276 mutex_lock(&sysfs_lock); 290 mutex_lock(&sysfs_lock);
@@ -278,11 +292,11 @@ static ssize_t gpio_direction_store(struct device *dev,
278 if (!test_bit(FLAG_EXPORT, &desc->flags)) 292 if (!test_bit(FLAG_EXPORT, &desc->flags))
279 status = -EIO; 293 status = -EIO;
280 else if (sysfs_streq(buf, "high")) 294 else if (sysfs_streq(buf, "high"))
281 status = gpio_direction_output(gpio, 1); 295 status = gpiod_direction_output(desc, 1);
282 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) 296 else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
283 status = gpio_direction_output(gpio, 0); 297 status = gpiod_direction_output(desc, 0);
284 else if (sysfs_streq(buf, "in")) 298 else if (sysfs_streq(buf, "in"))
285 status = gpio_direction_input(gpio); 299 status = gpiod_direction_input(desc);
286 else 300 else
287 status = -EINVAL; 301 status = -EINVAL;
288 302
@@ -296,8 +310,7 @@ static /* const */ DEVICE_ATTR(direction, 0644,
296static ssize_t gpio_value_show(struct device *dev, 310static ssize_t gpio_value_show(struct device *dev,
297 struct device_attribute *attr, char *buf) 311 struct device_attribute *attr, char *buf)
298{ 312{
299 const struct gpio_desc *desc = dev_get_drvdata(dev); 313 struct gpio_desc *desc = dev_get_drvdata(dev);
300 unsigned gpio = desc - gpio_desc;
301 ssize_t status; 314 ssize_t status;
302 315
303 mutex_lock(&sysfs_lock); 316 mutex_lock(&sysfs_lock);
@@ -307,7 +320,7 @@ static ssize_t gpio_value_show(struct device *dev,
307 } else { 320 } else {
308 int value; 321 int value;
309 322
310 value = !!gpio_get_value_cansleep(gpio); 323 value = !!gpiod_get_value_cansleep(desc);
311 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 324 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
312 value = !value; 325 value = !value;
313 326
@@ -321,8 +334,7 @@ static ssize_t gpio_value_show(struct device *dev,
321static ssize_t gpio_value_store(struct device *dev, 334static ssize_t gpio_value_store(struct device *dev,
322 struct device_attribute *attr, const char *buf, size_t size) 335 struct device_attribute *attr, const char *buf, size_t size)
323{ 336{
324 const struct gpio_desc *desc = dev_get_drvdata(dev); 337 struct gpio_desc *desc = dev_get_drvdata(dev);
325 unsigned gpio = desc - gpio_desc;
326 ssize_t status; 338 ssize_t status;
327 339
328 mutex_lock(&sysfs_lock); 340 mutex_lock(&sysfs_lock);
@@ -338,7 +350,7 @@ static ssize_t gpio_value_store(struct device *dev,
338 if (status == 0) { 350 if (status == 0) {
339 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) 351 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
340 value = !value; 352 value = !value;
341 gpio_set_value_cansleep(gpio, value != 0); 353 gpiod_set_value_cansleep(desc, value != 0);
342 status = size; 354 status = size;
343 } 355 }
344 } 356 }
@@ -368,7 +380,7 @@ static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev,
368 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) 380 if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags)
369 return 0; 381 return 0;
370 382
371 irq = gpio_to_irq(desc - gpio_desc); 383 irq = gpiod_to_irq(desc);
372 if (irq < 0) 384 if (irq < 0)
373 return -EIO; 385 return -EIO;
374 386
@@ -638,29 +650,32 @@ static ssize_t export_store(struct class *class,
638 struct class_attribute *attr, 650 struct class_attribute *attr,
639 const char *buf, size_t len) 651 const char *buf, size_t len)
640{ 652{
641 long gpio; 653 long gpio;
642 int status; 654 struct gpio_desc *desc;
655 int status;
643 656
644 status = strict_strtol(buf, 0, &gpio); 657 status = strict_strtol(buf, 0, &gpio);
645 if (status < 0) 658 if (status < 0)
646 goto done; 659 goto done;
647 660
661 desc = gpio_to_desc(gpio);
662
648 /* No extra locking here; FLAG_SYSFS just signifies that the 663 /* No extra locking here; FLAG_SYSFS just signifies that the
649 * request and export were done by on behalf of userspace, so 664 * request and export were done by on behalf of userspace, so
650 * they may be undone on its behalf too. 665 * they may be undone on its behalf too.
651 */ 666 */
652 667
653 status = gpio_request(gpio, "sysfs"); 668 status = gpiod_request(desc, "sysfs");
654 if (status < 0) { 669 if (status < 0) {
655 if (status == -EPROBE_DEFER) 670 if (status == -EPROBE_DEFER)
656 status = -ENODEV; 671 status = -ENODEV;
657 goto done; 672 goto done;
658 } 673 }
659 status = gpio_export(gpio, true); 674 status = gpiod_export(desc, true);
660 if (status < 0) 675 if (status < 0)
661 gpio_free(gpio); 676 gpiod_free(desc);
662 else 677 else
663 set_bit(FLAG_SYSFS, &gpio_desc[gpio].flags); 678 set_bit(FLAG_SYSFS, &desc->flags);
664 679
665done: 680done:
666 if (status) 681 if (status)
@@ -672,8 +687,9 @@ static ssize_t unexport_store(struct class *class,
672 struct class_attribute *attr, 687 struct class_attribute *attr,
673 const char *buf, size_t len) 688 const char *buf, size_t len)
674{ 689{
675 long gpio; 690 long gpio;
676 int status; 691 struct gpio_desc *desc;
692 int status;
677 693
678 status = strict_strtol(buf, 0, &gpio); 694 status = strict_strtol(buf, 0, &gpio);
679 if (status < 0) 695 if (status < 0)
@@ -681,17 +697,18 @@ static ssize_t unexport_store(struct class *class,
681 697
682 status = -EINVAL; 698 status = -EINVAL;
683 699
700 desc = gpio_to_desc(gpio);
684 /* reject bogus commands (gpio_unexport ignores them) */ 701 /* reject bogus commands (gpio_unexport ignores them) */
685 if (!gpio_is_valid(gpio)) 702 if (!desc)
686 goto done; 703 goto done;
687 704
688 /* No extra locking here; FLAG_SYSFS just signifies that the 705 /* No extra locking here; FLAG_SYSFS just signifies that the
689 * request and export were done by on behalf of userspace, so 706 * request and export were done by on behalf of userspace, so
690 * they may be undone on its behalf too. 707 * they may be undone on its behalf too.
691 */ 708 */
692 if (test_and_clear_bit(FLAG_SYSFS, &gpio_desc[gpio].flags)) { 709 if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
693 status = 0; 710 status = 0;
694 gpio_free(gpio); 711 gpiod_free(desc);
695 } 712 }
696done: 713done:
697 if (status) 714 if (status)
@@ -728,13 +745,13 @@ static struct class gpio_class = {
728 * 745 *
729 * Returns zero on success, else an error. 746 * Returns zero on success, else an error.
730 */ 747 */
731int gpio_export(unsigned gpio, bool direction_may_change) 748static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
732{ 749{
733 unsigned long flags; 750 unsigned long flags;
734 struct gpio_desc *desc;
735 int status; 751 int status;
736 const char *ioname = NULL; 752 const char *ioname = NULL;
737 struct device *dev; 753 struct device *dev;
754 int offset;
738 755
739 /* can't export until sysfs is available ... */ 756 /* can't export until sysfs is available ... */
740 if (!gpio_class.p) { 757 if (!gpio_class.p) {
@@ -742,20 +759,19 @@ int gpio_export(unsigned gpio, bool direction_may_change)
742 return -ENOENT; 759 return -ENOENT;
743 } 760 }
744 761
745 if (!gpio_is_valid(gpio)) { 762 if (!desc) {
746 pr_debug("%s: gpio %d is not valid\n", __func__, gpio); 763 pr_debug("%s: invalid gpio descriptor\n", __func__);
747 return -EINVAL; 764 return -EINVAL;
748 } 765 }
749 766
750 mutex_lock(&sysfs_lock); 767 mutex_lock(&sysfs_lock);
751 768
752 spin_lock_irqsave(&gpio_lock, flags); 769 spin_lock_irqsave(&gpio_lock, flags);
753 desc = &gpio_desc[gpio];
754 if (!test_bit(FLAG_REQUESTED, &desc->flags) || 770 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
755 test_bit(FLAG_EXPORT, &desc->flags)) { 771 test_bit(FLAG_EXPORT, &desc->flags)) {
756 spin_unlock_irqrestore(&gpio_lock, flags); 772 spin_unlock_irqrestore(&gpio_lock, flags);
757 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", 773 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n",
758 __func__, gpio, 774 __func__, desc_to_gpio(desc),
759 test_bit(FLAG_REQUESTED, &desc->flags), 775 test_bit(FLAG_REQUESTED, &desc->flags),
760 test_bit(FLAG_EXPORT, &desc->flags)); 776 test_bit(FLAG_EXPORT, &desc->flags));
761 status = -EPERM; 777 status = -EPERM;
@@ -766,11 +782,13 @@ int gpio_export(unsigned gpio, bool direction_may_change)
766 direction_may_change = false; 782 direction_may_change = false;
767 spin_unlock_irqrestore(&gpio_lock, flags); 783 spin_unlock_irqrestore(&gpio_lock, flags);
768 784
769 if (desc->chip->names && desc->chip->names[gpio - desc->chip->base]) 785 offset = gpio_chip_hwgpio(desc);
770 ioname = desc->chip->names[gpio - desc->chip->base]; 786 if (desc->chip->names && desc->chip->names[offset])
787 ioname = desc->chip->names[offset];
771 788
772 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), 789 dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
773 desc, ioname ? ioname : "gpio%u", gpio); 790 desc, ioname ? ioname : "gpio%u",
791 desc_to_gpio(desc));
774 if (IS_ERR(dev)) { 792 if (IS_ERR(dev)) {
775 status = PTR_ERR(dev); 793 status = PTR_ERR(dev);
776 goto fail_unlock; 794 goto fail_unlock;
@@ -786,7 +804,7 @@ int gpio_export(unsigned gpio, bool direction_may_change)
786 goto fail_unregister_device; 804 goto fail_unregister_device;
787 } 805 }
788 806
789 if (gpio_to_irq(gpio) >= 0 && (direction_may_change || 807 if (gpiod_to_irq(desc) >= 0 && (direction_may_change ||
790 !test_bit(FLAG_IS_OUT, &desc->flags))) { 808 !test_bit(FLAG_IS_OUT, &desc->flags))) {
791 status = device_create_file(dev, &dev_attr_edge); 809 status = device_create_file(dev, &dev_attr_edge);
792 if (status) 810 if (status)
@@ -801,9 +819,15 @@ fail_unregister_device:
801 device_unregister(dev); 819 device_unregister(dev);
802fail_unlock: 820fail_unlock:
803 mutex_unlock(&sysfs_lock); 821 mutex_unlock(&sysfs_lock);
804 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 822 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
823 status);
805 return status; 824 return status;
806} 825}
826
827int gpio_export(unsigned gpio, bool direction_may_change)
828{
829 return gpiod_export(gpio_to_desc(gpio), direction_may_change);
830}
807EXPORT_SYMBOL_GPL(gpio_export); 831EXPORT_SYMBOL_GPL(gpio_export);
808 832
809static int match_export(struct device *dev, const void *data) 833static int match_export(struct device *dev, const void *data)
@@ -822,18 +846,16 @@ static int match_export(struct device *dev, const void *data)
822 * 846 *
823 * Returns zero on success, else an error. 847 * Returns zero on success, else an error.
824 */ 848 */
825int gpio_export_link(struct device *dev, const char *name, unsigned gpio) 849static int gpiod_export_link(struct device *dev, const char *name,
850 struct gpio_desc *desc)
826{ 851{
827 struct gpio_desc *desc;
828 int status = -EINVAL; 852 int status = -EINVAL;
829 853
830 if (!gpio_is_valid(gpio)) 854 if (!desc)
831 goto done; 855 goto done;
832 856
833 mutex_lock(&sysfs_lock); 857 mutex_lock(&sysfs_lock);
834 858
835 desc = &gpio_desc[gpio];
836
837 if (test_bit(FLAG_EXPORT, &desc->flags)) { 859 if (test_bit(FLAG_EXPORT, &desc->flags)) {
838 struct device *tdev; 860 struct device *tdev;
839 861
@@ -850,12 +872,17 @@ int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
850 872
851done: 873done:
852 if (status) 874 if (status)
853 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 875 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
876 status);
854 877
855 return status; 878 return status;
856} 879}
857EXPORT_SYMBOL_GPL(gpio_export_link);
858 880
881int gpio_export_link(struct device *dev, const char *name, unsigned gpio)
882{
883 return gpiod_export_link(dev, name, gpio_to_desc(gpio));
884}
885EXPORT_SYMBOL_GPL(gpio_export_link);
859 886
860/** 887/**
861 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value 888 * gpio_sysfs_set_active_low - set the polarity of gpio sysfs value
@@ -869,19 +896,16 @@ EXPORT_SYMBOL_GPL(gpio_export_link);
869 * 896 *
870 * Returns zero on success, else an error. 897 * Returns zero on success, else an error.
871 */ 898 */
872int gpio_sysfs_set_active_low(unsigned gpio, int value) 899static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
873{ 900{
874 struct gpio_desc *desc;
875 struct device *dev = NULL; 901 struct device *dev = NULL;
876 int status = -EINVAL; 902 int status = -EINVAL;
877 903
878 if (!gpio_is_valid(gpio)) 904 if (!desc)
879 goto done; 905 goto done;
880 906
881 mutex_lock(&sysfs_lock); 907 mutex_lock(&sysfs_lock);
882 908
883 desc = &gpio_desc[gpio];
884
885 if (test_bit(FLAG_EXPORT, &desc->flags)) { 909 if (test_bit(FLAG_EXPORT, &desc->flags)) {
886 dev = class_find_device(&gpio_class, NULL, desc, match_export); 910 dev = class_find_device(&gpio_class, NULL, desc, match_export);
887 if (dev == NULL) { 911 if (dev == NULL) {
@@ -897,10 +921,16 @@ unlock:
897 921
898done: 922done:
899 if (status) 923 if (status)
900 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 924 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
925 status);
901 926
902 return status; 927 return status;
903} 928}
929
930int gpio_sysfs_set_active_low(unsigned gpio, int value)
931{
932 return gpiod_sysfs_set_active_low(gpio_to_desc(gpio), value);
933}
904EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low); 934EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
905 935
906/** 936/**
@@ -909,21 +939,18 @@ EXPORT_SYMBOL_GPL(gpio_sysfs_set_active_low);
909 * 939 *
910 * This is implicit on gpio_free(). 940 * This is implicit on gpio_free().
911 */ 941 */
912void gpio_unexport(unsigned gpio) 942static void gpiod_unexport(struct gpio_desc *desc)
913{ 943{
914 struct gpio_desc *desc;
915 int status = 0; 944 int status = 0;
916 struct device *dev = NULL; 945 struct device *dev = NULL;
917 946
918 if (!gpio_is_valid(gpio)) { 947 if (!desc) {
919 status = -EINVAL; 948 status = -EINVAL;
920 goto done; 949 goto done;
921 } 950 }
922 951
923 mutex_lock(&sysfs_lock); 952 mutex_lock(&sysfs_lock);
924 953
925 desc = &gpio_desc[gpio];
926
927 if (test_bit(FLAG_EXPORT, &desc->flags)) { 954 if (test_bit(FLAG_EXPORT, &desc->flags)) {
928 955
929 dev = class_find_device(&gpio_class, NULL, desc, match_export); 956 dev = class_find_device(&gpio_class, NULL, desc, match_export);
@@ -935,13 +962,20 @@ void gpio_unexport(unsigned gpio)
935 } 962 }
936 963
937 mutex_unlock(&sysfs_lock); 964 mutex_unlock(&sysfs_lock);
965
938 if (dev) { 966 if (dev) {
939 device_unregister(dev); 967 device_unregister(dev);
940 put_device(dev); 968 put_device(dev);
941 } 969 }
942done: 970done:
943 if (status) 971 if (status)
944 pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); 972 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc),
973 status);
974}
975
976void gpio_unexport(unsigned gpio)
977{
978 gpiod_unexport(gpio_to_desc(gpio));
945} 979}
946EXPORT_SYMBOL_GPL(gpio_unexport); 980EXPORT_SYMBOL_GPL(gpio_unexport);
947 981
@@ -975,9 +1009,9 @@ static int gpiochip_export(struct gpio_chip *chip)
975 unsigned gpio; 1009 unsigned gpio;
976 1010
977 spin_lock_irqsave(&gpio_lock, flags); 1011 spin_lock_irqsave(&gpio_lock, flags);
978 gpio = chip->base; 1012 gpio = 0;
979 while (gpio_desc[gpio].chip == chip) 1013 while (gpio < chip->ngpio)
980 gpio_desc[gpio++].chip = NULL; 1014 chip->desc[gpio++].chip = NULL;
981 spin_unlock_irqrestore(&gpio_lock, flags); 1015 spin_unlock_irqrestore(&gpio_lock, flags);
982 1016
983 pr_debug("%s: chip %s status %d\n", __func__, 1017 pr_debug("%s: chip %s status %d\n", __func__,
@@ -1012,7 +1046,7 @@ static int __init gpiolib_sysfs_init(void)
1012{ 1046{
1013 int status; 1047 int status;
1014 unsigned long flags; 1048 unsigned long flags;
1015 unsigned gpio; 1049 struct gpio_chip *chip;
1016 1050
1017 status = class_register(&gpio_class); 1051 status = class_register(&gpio_class);
1018 if (status < 0) 1052 if (status < 0)
@@ -1025,10 +1059,7 @@ static int __init gpiolib_sysfs_init(void)
1025 * registered, and so arch_initcall() can always gpio_export(). 1059 * registered, and so arch_initcall() can always gpio_export().
1026 */ 1060 */
1027 spin_lock_irqsave(&gpio_lock, flags); 1061 spin_lock_irqsave(&gpio_lock, flags);
1028 for (gpio = 0; gpio < ARCH_NR_GPIOS; gpio++) { 1062 list_for_each_entry(chip, &gpio_chips, list) {
1029 struct gpio_chip *chip;
1030
1031 chip = gpio_desc[gpio].chip;
1032 if (!chip || chip->exported) 1063 if (!chip || chip->exported)
1033 continue; 1064 continue;
1034 1065
@@ -1053,8 +1084,66 @@ static inline void gpiochip_unexport(struct gpio_chip *chip)
1053{ 1084{
1054} 1085}
1055 1086
1087static inline int gpiod_export(struct gpio_desc *desc,
1088 bool direction_may_change)
1089{
1090 return -ENOSYS;
1091}
1092
1093static inline int gpiod_export_link(struct device *dev, const char *name,
1094 struct gpio_desc *desc)
1095{
1096 return -ENOSYS;
1097}
1098
1099static inline int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value)
1100{
1101 return -ENOSYS;
1102}
1103
1104static inline void gpiod_unexport(struct gpio_desc *desc)
1105{
1106}
1107
1056#endif /* CONFIG_GPIO_SYSFS */ 1108#endif /* CONFIG_GPIO_SYSFS */
1057 1109
1110/*
1111 * Add a new chip to the global chips list, keeping the list of chips sorted
1112 * by base order.
1113 *
1114 * Return -EBUSY if the new chip overlaps with some other chip's integer
1115 * space.
1116 */
1117static int gpiochip_add_to_list(struct gpio_chip *chip)
1118{
1119 struct list_head *pos = &gpio_chips;
1120 struct gpio_chip *_chip;
1121 int err = 0;
1122
1123 /* find where to insert our chip */
1124 list_for_each(pos, &gpio_chips) {
1125 _chip = list_entry(pos, struct gpio_chip, list);
1126 /* shall we insert before _chip? */
1127 if (_chip->base >= chip->base + chip->ngpio)
1128 break;
1129 }
1130
1131 /* are we stepping on the chip right before? */
1132 if (pos != &gpio_chips && pos->prev != &gpio_chips) {
1133 _chip = list_entry(pos->prev, struct gpio_chip, list);
1134 if (_chip->base + _chip->ngpio > chip->base) {
1135 dev_err(chip->dev,
1136 "GPIO integer space overlap, cannot add chip\n");
1137 err = -EBUSY;
1138 }
1139 }
1140
1141 if (!err)
1142 list_add_tail(&chip->list, pos);
1143
1144 return err;
1145}
1146
1058/** 1147/**
1059 * gpiochip_add() - register a gpio_chip 1148 * gpiochip_add() - register a gpio_chip
1060 * @chip: the chip to register, with chip->base initialized 1149 * @chip: the chip to register, with chip->base initialized
@@ -1096,16 +1185,14 @@ int gpiochip_add(struct gpio_chip *chip)
1096 chip->base = base; 1185 chip->base = base;
1097 } 1186 }
1098 1187
1099 /* these GPIO numbers must not be managed by another gpio_chip */ 1188 status = gpiochip_add_to_list(chip);
1100 for (id = base; id < base + chip->ngpio; id++) { 1189
1101 if (gpio_desc[id].chip != NULL) {
1102 status = -EBUSY;
1103 break;
1104 }
1105 }
1106 if (status == 0) { 1190 if (status == 0) {
1107 for (id = base; id < base + chip->ngpio; id++) { 1191 chip->desc = &gpio_desc[chip->base];
1108 gpio_desc[id].chip = chip; 1192
1193 for (id = 0; id < chip->ngpio; id++) {
1194 struct gpio_desc *desc = &chip->desc[id];
1195 desc->chip = chip;
1109 1196
1110 /* REVISIT: most hardware initializes GPIOs as 1197 /* REVISIT: most hardware initializes GPIOs as
1111 * inputs (often with pullups enabled) so power 1198 * inputs (often with pullups enabled) so power
@@ -1114,7 +1201,7 @@ int gpiochip_add(struct gpio_chip *chip)
1114 * and in case chip->get_direction is not set, 1201 * and in case chip->get_direction is not set,
1115 * we may expose the wrong direction in sysfs. 1202 * we may expose the wrong direction in sysfs.
1116 */ 1203 */
1117 gpio_desc[id].flags = !chip->direction_input 1204 desc->flags = !chip->direction_input
1118 ? (1 << FLAG_IS_OUT) 1205 ? (1 << FLAG_IS_OUT)
1119 : 0; 1206 : 0;
1120 } 1207 }
@@ -1167,15 +1254,17 @@ int gpiochip_remove(struct gpio_chip *chip)
1167 gpiochip_remove_pin_ranges(chip); 1254 gpiochip_remove_pin_ranges(chip);
1168 of_gpiochip_remove(chip); 1255 of_gpiochip_remove(chip);
1169 1256
1170 for (id = chip->base; id < chip->base + chip->ngpio; id++) { 1257 for (id = 0; id < chip->ngpio; id++) {
1171 if (test_bit(FLAG_REQUESTED, &gpio_desc[id].flags)) { 1258 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
1172 status = -EBUSY; 1259 status = -EBUSY;
1173 break; 1260 break;
1174 } 1261 }
1175 } 1262 }
1176 if (status == 0) { 1263 if (status == 0) {
1177 for (id = chip->base; id < chip->base + chip->ngpio; id++) 1264 for (id = 0; id < chip->ngpio; id++)
1178 gpio_desc[id].chip = NULL; 1265 chip->desc[id].chip = NULL;
1266
1267 list_del(&chip->list);
1179 } 1268 }
1180 1269
1181 spin_unlock_irqrestore(&gpio_lock, flags); 1270 spin_unlock_irqrestore(&gpio_lock, flags);
@@ -1202,20 +1291,17 @@ struct gpio_chip *gpiochip_find(void *data,
1202 int (*match)(struct gpio_chip *chip, 1291 int (*match)(struct gpio_chip *chip,
1203 void *data)) 1292 void *data))
1204{ 1293{
1205 struct gpio_chip *chip = NULL; 1294 struct gpio_chip *chip;
1206 unsigned long flags; 1295 unsigned long flags;
1207 int i;
1208 1296
1209 spin_lock_irqsave(&gpio_lock, flags); 1297 spin_lock_irqsave(&gpio_lock, flags);
1210 for (i = 0; i < ARCH_NR_GPIOS; i++) { 1298 list_for_each_entry(chip, &gpio_chips, list)
1211 if (!gpio_desc[i].chip) 1299 if (match(chip, data))
1212 continue;
1213
1214 if (match(gpio_desc[i].chip, data)) {
1215 chip = gpio_desc[i].chip;
1216 break; 1300 break;
1217 } 1301
1218 } 1302 /* No match? */
1303 if (&chip->list == &gpio_chips)
1304 chip = NULL;
1219 spin_unlock_irqrestore(&gpio_lock, flags); 1305 spin_unlock_irqrestore(&gpio_lock, flags);
1220 1306
1221 return chip; 1307 return chip;
@@ -1297,20 +1383,18 @@ EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
1297 * on each other, and help provide better diagnostics in debugfs. 1383 * on each other, and help provide better diagnostics in debugfs.
1298 * They're called even less than the "set direction" calls. 1384 * They're called even less than the "set direction" calls.
1299 */ 1385 */
1300int gpio_request(unsigned gpio, const char *label) 1386static int gpiod_request(struct gpio_desc *desc, const char *label)
1301{ 1387{
1302 struct gpio_desc *desc;
1303 struct gpio_chip *chip; 1388 struct gpio_chip *chip;
1304 int status = -EPROBE_DEFER; 1389 int status = -EPROBE_DEFER;
1305 unsigned long flags; 1390 unsigned long flags;
1306 1391
1307 spin_lock_irqsave(&gpio_lock, flags); 1392 spin_lock_irqsave(&gpio_lock, flags);
1308 1393
1309 if (!gpio_is_valid(gpio)) { 1394 if (!desc) {
1310 status = -EINVAL; 1395 status = -EINVAL;
1311 goto done; 1396 goto done;
1312 } 1397 }
1313 desc = &gpio_desc[gpio];
1314 chip = desc->chip; 1398 chip = desc->chip;
1315 if (chip == NULL) 1399 if (chip == NULL)
1316 goto done; 1400 goto done;
@@ -1334,7 +1418,7 @@ int gpio_request(unsigned gpio, const char *label)
1334 if (chip->request) { 1418 if (chip->request) {
1335 /* chip->request may sleep */ 1419 /* chip->request may sleep */
1336 spin_unlock_irqrestore(&gpio_lock, flags); 1420 spin_unlock_irqrestore(&gpio_lock, flags);
1337 status = chip->request(chip, gpio - chip->base); 1421 status = chip->request(chip, gpio_chip_hwgpio(desc));
1338 spin_lock_irqsave(&gpio_lock, flags); 1422 spin_lock_irqsave(&gpio_lock, flags);
1339 1423
1340 if (status < 0) { 1424 if (status < 0) {
@@ -1347,42 +1431,46 @@ int gpio_request(unsigned gpio, const char *label)
1347 if (chip->get_direction) { 1431 if (chip->get_direction) {
1348 /* chip->get_direction may sleep */ 1432 /* chip->get_direction may sleep */
1349 spin_unlock_irqrestore(&gpio_lock, flags); 1433 spin_unlock_irqrestore(&gpio_lock, flags);
1350 gpio_get_direction(gpio); 1434 gpiod_get_direction(desc);
1351 spin_lock_irqsave(&gpio_lock, flags); 1435 spin_lock_irqsave(&gpio_lock, flags);
1352 } 1436 }
1353done: 1437done:
1354 if (status) 1438 if (status)
1355 pr_debug("gpio_request: gpio-%d (%s) status %d\n", 1439 pr_debug("_gpio_request: gpio-%d (%s) status %d\n",
1356 gpio, label ? : "?", status); 1440 desc ? desc_to_gpio(desc) : -1,
1441 label ? : "?", status);
1357 spin_unlock_irqrestore(&gpio_lock, flags); 1442 spin_unlock_irqrestore(&gpio_lock, flags);
1358 return status; 1443 return status;
1359} 1444}
1445
1446int gpio_request(unsigned gpio, const char *label)
1447{
1448 return gpiod_request(gpio_to_desc(gpio), label);
1449}
1360EXPORT_SYMBOL_GPL(gpio_request); 1450EXPORT_SYMBOL_GPL(gpio_request);
1361 1451
1362void gpio_free(unsigned gpio) 1452static void gpiod_free(struct gpio_desc *desc)
1363{ 1453{
1364 unsigned long flags; 1454 unsigned long flags;
1365 struct gpio_desc *desc;
1366 struct gpio_chip *chip; 1455 struct gpio_chip *chip;
1367 1456
1368 might_sleep(); 1457 might_sleep();
1369 1458
1370 if (!gpio_is_valid(gpio)) { 1459 if (!desc) {
1371 WARN_ON(extra_checks); 1460 WARN_ON(extra_checks);
1372 return; 1461 return;
1373 } 1462 }
1374 1463
1375 gpio_unexport(gpio); 1464 gpiod_unexport(desc);
1376 1465
1377 spin_lock_irqsave(&gpio_lock, flags); 1466 spin_lock_irqsave(&gpio_lock, flags);
1378 1467
1379 desc = &gpio_desc[gpio];
1380 chip = desc->chip; 1468 chip = desc->chip;
1381 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { 1469 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
1382 if (chip->free) { 1470 if (chip->free) {
1383 spin_unlock_irqrestore(&gpio_lock, flags); 1471 spin_unlock_irqrestore(&gpio_lock, flags);
1384 might_sleep_if(chip->can_sleep); 1472 might_sleep_if(chip->can_sleep);
1385 chip->free(chip, gpio - chip->base); 1473 chip->free(chip, gpio_chip_hwgpio(desc));
1386 spin_lock_irqsave(&gpio_lock, flags); 1474 spin_lock_irqsave(&gpio_lock, flags);
1387 } 1475 }
1388 desc_set_label(desc, NULL); 1476 desc_set_label(desc, NULL);
@@ -1396,6 +1484,11 @@ void gpio_free(unsigned gpio)
1396 1484
1397 spin_unlock_irqrestore(&gpio_lock, flags); 1485 spin_unlock_irqrestore(&gpio_lock, flags);
1398} 1486}
1487
1488void gpio_free(unsigned gpio)
1489{
1490 gpiod_free(gpio_to_desc(gpio));
1491}
1399EXPORT_SYMBOL_GPL(gpio_free); 1492EXPORT_SYMBOL_GPL(gpio_free);
1400 1493
1401/** 1494/**
@@ -1406,29 +1499,32 @@ EXPORT_SYMBOL_GPL(gpio_free);
1406 */ 1499 */
1407int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) 1500int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1408{ 1501{
1502 struct gpio_desc *desc;
1409 int err; 1503 int err;
1410 1504
1411 err = gpio_request(gpio, label); 1505 desc = gpio_to_desc(gpio);
1506
1507 err = gpiod_request(desc, label);
1412 if (err) 1508 if (err)
1413 return err; 1509 return err;
1414 1510
1415 if (flags & GPIOF_OPEN_DRAIN) 1511 if (flags & GPIOF_OPEN_DRAIN)
1416 set_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags); 1512 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
1417 1513
1418 if (flags & GPIOF_OPEN_SOURCE) 1514 if (flags & GPIOF_OPEN_SOURCE)
1419 set_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags); 1515 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
1420 1516
1421 if (flags & GPIOF_DIR_IN) 1517 if (flags & GPIOF_DIR_IN)
1422 err = gpio_direction_input(gpio); 1518 err = gpiod_direction_input(desc);
1423 else 1519 else
1424 err = gpio_direction_output(gpio, 1520 err = gpiod_direction_output(desc,
1425 (flags & GPIOF_INIT_HIGH) ? 1 : 0); 1521 (flags & GPIOF_INIT_HIGH) ? 1 : 0);
1426 1522
1427 if (err) 1523 if (err)
1428 goto free_gpio; 1524 goto free_gpio;
1429 1525
1430 if (flags & GPIOF_EXPORT) { 1526 if (flags & GPIOF_EXPORT) {
1431 err = gpio_export(gpio, flags & GPIOF_EXPORT_CHANGEABLE); 1527 err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
1432 if (err) 1528 if (err)
1433 goto free_gpio; 1529 goto free_gpio;
1434 } 1530 }
@@ -1436,7 +1532,7 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
1436 return 0; 1532 return 0;
1437 1533
1438 free_gpio: 1534 free_gpio:
1439 gpio_free(gpio); 1535 gpiod_free(desc);
1440 return err; 1536 return err;
1441} 1537}
1442EXPORT_SYMBOL_GPL(gpio_request_one); 1538EXPORT_SYMBOL_GPL(gpio_request_one);
@@ -1491,14 +1587,17 @@ EXPORT_SYMBOL_GPL(gpio_free_array);
1491 */ 1587 */
1492const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) 1588const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
1493{ 1589{
1494 unsigned gpio = chip->base + offset; 1590 struct gpio_desc *desc;
1495 1591
1496 if (!gpio_is_valid(gpio) || gpio_desc[gpio].chip != chip) 1592 if (!GPIO_OFFSET_VALID(chip, offset))
1497 return NULL; 1593 return NULL;
1498 if (test_bit(FLAG_REQUESTED, &gpio_desc[gpio].flags) == 0) 1594
1595 desc = &chip->desc[offset];
1596
1597 if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
1499 return NULL; 1598 return NULL;
1500#ifdef CONFIG_DEBUG_FS 1599#ifdef CONFIG_DEBUG_FS
1501 return gpio_desc[gpio].label; 1600 return desc->label;
1502#else 1601#else
1503 return "?"; 1602 return "?";
1504#endif 1603#endif
@@ -1515,24 +1614,21 @@ EXPORT_SYMBOL_GPL(gpiochip_is_requested);
1515 * rely on gpio_request() having been called beforehand. 1614 * rely on gpio_request() having been called beforehand.
1516 */ 1615 */
1517 1616
1518int gpio_direction_input(unsigned gpio) 1617static int gpiod_direction_input(struct gpio_desc *desc)
1519{ 1618{
1520 unsigned long flags; 1619 unsigned long flags;
1521 struct gpio_chip *chip; 1620 struct gpio_chip *chip;
1522 struct gpio_desc *desc = &gpio_desc[gpio];
1523 int status = -EINVAL; 1621 int status = -EINVAL;
1622 int offset;
1524 1623
1525 spin_lock_irqsave(&gpio_lock, flags); 1624 spin_lock_irqsave(&gpio_lock, flags);
1526 1625
1527 if (!gpio_is_valid(gpio)) 1626 if (!desc)
1528 goto fail; 1627 goto fail;
1529 chip = desc->chip; 1628 chip = desc->chip;
1530 if (!chip || !chip->get || !chip->direction_input) 1629 if (!chip || !chip->get || !chip->direction_input)
1531 goto fail; 1630 goto fail;
1532 gpio -= chip->base; 1631 status = gpio_ensure_requested(desc);
1533 if (gpio >= chip->ngpio)
1534 goto fail;
1535 status = gpio_ensure_requested(desc, gpio);
1536 if (status < 0) 1632 if (status < 0)
1537 goto fail; 1633 goto fail;
1538 1634
@@ -1542,11 +1638,12 @@ int gpio_direction_input(unsigned gpio)
1542 1638
1543 might_sleep_if(chip->can_sleep); 1639 might_sleep_if(chip->can_sleep);
1544 1640
1641 offset = gpio_chip_hwgpio(desc);
1545 if (status) { 1642 if (status) {
1546 status = chip->request(chip, gpio); 1643 status = chip->request(chip, offset);
1547 if (status < 0) { 1644 if (status < 0) {
1548 pr_debug("GPIO-%d: chip request fail, %d\n", 1645 pr_debug("GPIO-%d: chip request fail, %d\n",
1549 chip->base + gpio, status); 1646 desc_to_gpio(desc), status);
1550 /* and it's not available to anyone else ... 1647 /* and it's not available to anyone else ...
1551 * gpio_request() is the fully clean solution. 1648 * gpio_request() is the fully clean solution.
1552 */ 1649 */
@@ -1554,48 +1651,54 @@ int gpio_direction_input(unsigned gpio)
1554 } 1651 }
1555 } 1652 }
1556 1653
1557 status = chip->direction_input(chip, gpio); 1654 status = chip->direction_input(chip, offset);
1558 if (status == 0) 1655 if (status == 0)
1559 clear_bit(FLAG_IS_OUT, &desc->flags); 1656 clear_bit(FLAG_IS_OUT, &desc->flags);
1560 1657
1561 trace_gpio_direction(chip->base + gpio, 1, status); 1658 trace_gpio_direction(desc_to_gpio(desc), 1, status);
1562lose: 1659lose:
1563 return status; 1660 return status;
1564fail: 1661fail:
1565 spin_unlock_irqrestore(&gpio_lock, flags); 1662 spin_unlock_irqrestore(&gpio_lock, flags);
1566 if (status) 1663 if (status) {
1664 int gpio = -1;
1665 if (desc)
1666 gpio = desc_to_gpio(desc);
1567 pr_debug("%s: gpio-%d status %d\n", 1667 pr_debug("%s: gpio-%d status %d\n",
1568 __func__, gpio, status); 1668 __func__, gpio, status);
1669 }
1569 return status; 1670 return status;
1570} 1671}
1672
1673int gpio_direction_input(unsigned gpio)
1674{
1675 return gpiod_direction_input(gpio_to_desc(gpio));
1676}
1571EXPORT_SYMBOL_GPL(gpio_direction_input); 1677EXPORT_SYMBOL_GPL(gpio_direction_input);
1572 1678
1573int gpio_direction_output(unsigned gpio, int value) 1679static int gpiod_direction_output(struct gpio_desc *desc, int value)
1574{ 1680{
1575 unsigned long flags; 1681 unsigned long flags;
1576 struct gpio_chip *chip; 1682 struct gpio_chip *chip;
1577 struct gpio_desc *desc = &gpio_desc[gpio];
1578 int status = -EINVAL; 1683 int status = -EINVAL;
1684 int offset;
1579 1685
1580 /* Open drain pin should not be driven to 1 */ 1686 /* Open drain pin should not be driven to 1 */
1581 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) 1687 if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1582 return gpio_direction_input(gpio); 1688 return gpiod_direction_input(desc);
1583 1689
1584 /* Open source pin should not be driven to 0 */ 1690 /* Open source pin should not be driven to 0 */
1585 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) 1691 if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1586 return gpio_direction_input(gpio); 1692 return gpiod_direction_input(desc);
1587 1693
1588 spin_lock_irqsave(&gpio_lock, flags); 1694 spin_lock_irqsave(&gpio_lock, flags);
1589 1695
1590 if (!gpio_is_valid(gpio)) 1696 if (!desc)
1591 goto fail; 1697 goto fail;
1592 chip = desc->chip; 1698 chip = desc->chip;
1593 if (!chip || !chip->set || !chip->direction_output) 1699 if (!chip || !chip->set || !chip->direction_output)
1594 goto fail; 1700 goto fail;
1595 gpio -= chip->base; 1701 status = gpio_ensure_requested(desc);
1596 if (gpio >= chip->ngpio)
1597 goto fail;
1598 status = gpio_ensure_requested(desc, gpio);
1599 if (status < 0) 1702 if (status < 0)
1600 goto fail; 1703 goto fail;
1601 1704
@@ -1605,11 +1708,12 @@ int gpio_direction_output(unsigned gpio, int value)
1605 1708
1606 might_sleep_if(chip->can_sleep); 1709 might_sleep_if(chip->can_sleep);
1607 1710
1711 offset = gpio_chip_hwgpio(desc);
1608 if (status) { 1712 if (status) {
1609 status = chip->request(chip, gpio); 1713 status = chip->request(chip, offset);
1610 if (status < 0) { 1714 if (status < 0) {
1611 pr_debug("GPIO-%d: chip request fail, %d\n", 1715 pr_debug("GPIO-%d: chip request fail, %d\n",
1612 chip->base + gpio, status); 1716 desc_to_gpio(desc), status);
1613 /* and it's not available to anyone else ... 1717 /* and it's not available to anyone else ...
1614 * gpio_request() is the fully clean solution. 1718 * gpio_request() is the fully clean solution.
1615 */ 1719 */
@@ -1617,20 +1721,29 @@ int gpio_direction_output(unsigned gpio, int value)
1617 } 1721 }
1618 } 1722 }
1619 1723
1620 status = chip->direction_output(chip, gpio, value); 1724 status = chip->direction_output(chip, offset, value);
1621 if (status == 0) 1725 if (status == 0)
1622 set_bit(FLAG_IS_OUT, &desc->flags); 1726 set_bit(FLAG_IS_OUT, &desc->flags);
1623 trace_gpio_value(chip->base + gpio, 0, value); 1727 trace_gpio_value(desc_to_gpio(desc), 0, value);
1624 trace_gpio_direction(chip->base + gpio, 0, status); 1728 trace_gpio_direction(desc_to_gpio(desc), 0, status);
1625lose: 1729lose:
1626 return status; 1730 return status;
1627fail: 1731fail:
1628 spin_unlock_irqrestore(&gpio_lock, flags); 1732 spin_unlock_irqrestore(&gpio_lock, flags);
1629 if (status) 1733 if (status) {
1734 int gpio = -1;
1735 if (desc)
1736 gpio = desc_to_gpio(desc);
1630 pr_debug("%s: gpio-%d status %d\n", 1737 pr_debug("%s: gpio-%d status %d\n",
1631 __func__, gpio, status); 1738 __func__, gpio, status);
1739 }
1632 return status; 1740 return status;
1633} 1741}
1742
1743int gpio_direction_output(unsigned gpio, int value)
1744{
1745 return gpiod_direction_output(gpio_to_desc(gpio), value);
1746}
1634EXPORT_SYMBOL_GPL(gpio_direction_output); 1747EXPORT_SYMBOL_GPL(gpio_direction_output);
1635 1748
1636/** 1749/**
@@ -1638,24 +1751,22 @@ EXPORT_SYMBOL_GPL(gpio_direction_output);
1638 * @gpio: the gpio to set debounce time 1751 * @gpio: the gpio to set debounce time
1639 * @debounce: debounce time is microseconds 1752 * @debounce: debounce time is microseconds
1640 */ 1753 */
1641int gpio_set_debounce(unsigned gpio, unsigned debounce) 1754static int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
1642{ 1755{
1643 unsigned long flags; 1756 unsigned long flags;
1644 struct gpio_chip *chip; 1757 struct gpio_chip *chip;
1645 struct gpio_desc *desc = &gpio_desc[gpio];
1646 int status = -EINVAL; 1758 int status = -EINVAL;
1759 int offset;
1647 1760
1648 spin_lock_irqsave(&gpio_lock, flags); 1761 spin_lock_irqsave(&gpio_lock, flags);
1649 1762
1650 if (!gpio_is_valid(gpio)) 1763 if (!desc)
1651 goto fail; 1764 goto fail;
1652 chip = desc->chip; 1765 chip = desc->chip;
1653 if (!chip || !chip->set || !chip->set_debounce) 1766 if (!chip || !chip->set || !chip->set_debounce)
1654 goto fail; 1767 goto fail;
1655 gpio -= chip->base; 1768
1656 if (gpio >= chip->ngpio) 1769 status = gpio_ensure_requested(desc);
1657 goto fail;
1658 status = gpio_ensure_requested(desc, gpio);
1659 if (status < 0) 1770 if (status < 0)
1660 goto fail; 1771 goto fail;
1661 1772
@@ -1665,16 +1776,26 @@ int gpio_set_debounce(unsigned gpio, unsigned debounce)
1665 1776
1666 might_sleep_if(chip->can_sleep); 1777 might_sleep_if(chip->can_sleep);
1667 1778
1668 return chip->set_debounce(chip, gpio, debounce); 1779 offset = gpio_chip_hwgpio(desc);
1780 return chip->set_debounce(chip, offset, debounce);
1669 1781
1670fail: 1782fail:
1671 spin_unlock_irqrestore(&gpio_lock, flags); 1783 spin_unlock_irqrestore(&gpio_lock, flags);
1672 if (status) 1784 if (status) {
1785 int gpio = -1;
1786 if (desc)
1787 gpio = desc_to_gpio(desc);
1673 pr_debug("%s: gpio-%d status %d\n", 1788 pr_debug("%s: gpio-%d status %d\n",
1674 __func__, gpio, status); 1789 __func__, gpio, status);
1790 }
1675 1791
1676 return status; 1792 return status;
1677} 1793}
1794
1795int gpio_set_debounce(unsigned gpio, unsigned debounce)
1796{
1797 return gpiod_set_debounce(gpio_to_desc(gpio), debounce);
1798}
1678EXPORT_SYMBOL_GPL(gpio_set_debounce); 1799EXPORT_SYMBOL_GPL(gpio_set_debounce);
1679 1800
1680/* I/O calls are only valid after configuration completed; the relevant 1801/* I/O calls are only valid after configuration completed; the relevant
@@ -1708,18 +1829,25 @@ EXPORT_SYMBOL_GPL(gpio_set_debounce);
1708 * It returns the zero or nonzero value provided by the associated 1829 * It returns the zero or nonzero value provided by the associated
1709 * gpio_chip.get() method; or zero if no such method is provided. 1830 * gpio_chip.get() method; or zero if no such method is provided.
1710 */ 1831 */
1711int __gpio_get_value(unsigned gpio) 1832static int gpiod_get_value(struct gpio_desc *desc)
1712{ 1833{
1713 struct gpio_chip *chip; 1834 struct gpio_chip *chip;
1714 int value; 1835 int value;
1836 int offset;
1715 1837
1716 chip = gpio_to_chip(gpio); 1838 chip = desc->chip;
1839 offset = gpio_chip_hwgpio(desc);
1717 /* Should be using gpio_get_value_cansleep() */ 1840 /* Should be using gpio_get_value_cansleep() */
1718 WARN_ON(chip->can_sleep); 1841 WARN_ON(chip->can_sleep);
1719 value = chip->get ? chip->get(chip, gpio - chip->base) : 0; 1842 value = chip->get ? chip->get(chip, offset) : 0;
1720 trace_gpio_value(gpio, 1, value); 1843 trace_gpio_value(desc_to_gpio(desc), 1, value);
1721 return value; 1844 return value;
1722} 1845}
1846
1847int __gpio_get_value(unsigned gpio)
1848{
1849 return gpiod_get_value(gpio_to_desc(gpio));
1850}
1723EXPORT_SYMBOL_GPL(__gpio_get_value); 1851EXPORT_SYMBOL_GPL(__gpio_get_value);
1724 1852
1725/* 1853/*
@@ -1728,23 +1856,25 @@ EXPORT_SYMBOL_GPL(__gpio_get_value);
1728 * @chip: Gpio chip. 1856 * @chip: Gpio chip.
1729 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1857 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1730 */ 1858 */
1731static void _gpio_set_open_drain_value(unsigned gpio, 1859static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value)
1732 struct gpio_chip *chip, int value)
1733{ 1860{
1734 int err = 0; 1861 int err = 0;
1862 struct gpio_chip *chip = desc->chip;
1863 int offset = gpio_chip_hwgpio(desc);
1864
1735 if (value) { 1865 if (value) {
1736 err = chip->direction_input(chip, gpio - chip->base); 1866 err = chip->direction_input(chip, offset);
1737 if (!err) 1867 if (!err)
1738 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1868 clear_bit(FLAG_IS_OUT, &desc->flags);
1739 } else { 1869 } else {
1740 err = chip->direction_output(chip, gpio - chip->base, 0); 1870 err = chip->direction_output(chip, offset, 0);
1741 if (!err) 1871 if (!err)
1742 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1872 set_bit(FLAG_IS_OUT, &desc->flags);
1743 } 1873 }
1744 trace_gpio_direction(gpio, value, err); 1874 trace_gpio_direction(desc_to_gpio(desc), value, err);
1745 if (err < 0) 1875 if (err < 0)
1746 pr_err("%s: Error in set_value for open drain gpio%d err %d\n", 1876 pr_err("%s: Error in set_value for open drain gpio%d err %d\n",
1747 __func__, gpio, err); 1877 __func__, desc_to_gpio(desc), err);
1748} 1878}
1749 1879
1750/* 1880/*
@@ -1753,26 +1883,27 @@ static void _gpio_set_open_drain_value(unsigned gpio,
1753 * @chip: Gpio chip. 1883 * @chip: Gpio chip.
1754 * @value: Non-zero for setting it HIGH otherise it will set to LOW. 1884 * @value: Non-zero for setting it HIGH otherise it will set to LOW.
1755 */ 1885 */
1756static void _gpio_set_open_source_value(unsigned gpio, 1886static void _gpio_set_open_source_value(struct gpio_desc *desc, int value)
1757 struct gpio_chip *chip, int value)
1758{ 1887{
1759 int err = 0; 1888 int err = 0;
1889 struct gpio_chip *chip = desc->chip;
1890 int offset = gpio_chip_hwgpio(desc);
1891
1760 if (value) { 1892 if (value) {
1761 err = chip->direction_output(chip, gpio - chip->base, 1); 1893 err = chip->direction_output(chip, offset, 1);
1762 if (!err) 1894 if (!err)
1763 set_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1895 set_bit(FLAG_IS_OUT, &desc->flags);
1764 } else { 1896 } else {
1765 err = chip->direction_input(chip, gpio - chip->base); 1897 err = chip->direction_input(chip, offset);
1766 if (!err) 1898 if (!err)
1767 clear_bit(FLAG_IS_OUT, &gpio_desc[gpio].flags); 1899 clear_bit(FLAG_IS_OUT, &desc->flags);
1768 } 1900 }
1769 trace_gpio_direction(gpio, !value, err); 1901 trace_gpio_direction(desc_to_gpio(desc), !value, err);
1770 if (err < 0) 1902 if (err < 0)
1771 pr_err("%s: Error in set_value for open source gpio%d err %d\n", 1903 pr_err("%s: Error in set_value for open source gpio%d err %d\n",
1772 __func__, gpio, err); 1904 __func__, desc_to_gpio(desc), err);
1773} 1905}
1774 1906
1775
1776/** 1907/**
1777 * __gpio_set_value() - assign a gpio's value 1908 * __gpio_set_value() - assign a gpio's value
1778 * @gpio: gpio whose value will be assigned 1909 * @gpio: gpio whose value will be assigned
@@ -1782,20 +1913,25 @@ static void _gpio_set_open_source_value(unsigned gpio,
1782 * This is used directly or indirectly to implement gpio_set_value(). 1913 * This is used directly or indirectly to implement gpio_set_value().
1783 * It invokes the associated gpio_chip.set() method. 1914 * It invokes the associated gpio_chip.set() method.
1784 */ 1915 */
1785void __gpio_set_value(unsigned gpio, int value) 1916static void gpiod_set_value(struct gpio_desc *desc, int value)
1786{ 1917{
1787 struct gpio_chip *chip; 1918 struct gpio_chip *chip;
1788 1919
1789 chip = gpio_to_chip(gpio); 1920 chip = desc->chip;
1790 /* Should be using gpio_set_value_cansleep() */ 1921 /* Should be using gpio_set_value_cansleep() */
1791 WARN_ON(chip->can_sleep); 1922 WARN_ON(chip->can_sleep);
1792 trace_gpio_value(gpio, 0, value); 1923 trace_gpio_value(desc_to_gpio(desc), 0, value);
1793 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) 1924 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1794 _gpio_set_open_drain_value(gpio, chip, value); 1925 _gpio_set_open_drain_value(desc, value);
1795 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) 1926 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1796 _gpio_set_open_source_value(gpio, chip, value); 1927 _gpio_set_open_source_value(desc, value);
1797 else 1928 else
1798 chip->set(chip, gpio - chip->base, value); 1929 chip->set(chip, gpio_chip_hwgpio(desc), value);
1930}
1931
1932void __gpio_set_value(unsigned gpio, int value)
1933{
1934 return gpiod_set_value(gpio_to_desc(gpio), value);
1799} 1935}
1800EXPORT_SYMBOL_GPL(__gpio_set_value); 1936EXPORT_SYMBOL_GPL(__gpio_set_value);
1801 1937
@@ -1807,14 +1943,15 @@ EXPORT_SYMBOL_GPL(__gpio_set_value);
1807 * This is used directly or indirectly to implement gpio_cansleep(). It 1943 * This is used directly or indirectly to implement gpio_cansleep(). It
1808 * returns nonzero if access reading or writing the GPIO value can sleep. 1944 * returns nonzero if access reading or writing the GPIO value can sleep.
1809 */ 1945 */
1810int __gpio_cansleep(unsigned gpio) 1946static int gpiod_cansleep(struct gpio_desc *desc)
1811{ 1947{
1812 struct gpio_chip *chip;
1813
1814 /* only call this on GPIOs that are valid! */ 1948 /* only call this on GPIOs that are valid! */
1815 chip = gpio_to_chip(gpio); 1949 return desc->chip->can_sleep;
1950}
1816 1951
1817 return chip->can_sleep; 1952int __gpio_cansleep(unsigned gpio)
1953{
1954 return gpiod_cansleep(gpio_to_desc(gpio));
1818} 1955}
1819EXPORT_SYMBOL_GPL(__gpio_cansleep); 1956EXPORT_SYMBOL_GPL(__gpio_cansleep);
1820 1957
@@ -1827,50 +1964,67 @@ EXPORT_SYMBOL_GPL(__gpio_cansleep);
1827 * It returns the number of the IRQ signaled by this (input) GPIO, 1964 * It returns the number of the IRQ signaled by this (input) GPIO,
1828 * or a negative errno. 1965 * or a negative errno.
1829 */ 1966 */
1830int __gpio_to_irq(unsigned gpio) 1967static int gpiod_to_irq(struct gpio_desc *desc)
1831{ 1968{
1832 struct gpio_chip *chip; 1969 struct gpio_chip *chip;
1970 int offset;
1833 1971
1834 chip = gpio_to_chip(gpio); 1972 chip = desc->chip;
1835 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO; 1973 offset = gpio_chip_hwgpio(desc);
1974 return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO;
1836} 1975}
1837EXPORT_SYMBOL_GPL(__gpio_to_irq);
1838 1976
1977int __gpio_to_irq(unsigned gpio)
1978{
1979 return gpiod_to_irq(gpio_to_desc(gpio));
1980}
1981EXPORT_SYMBOL_GPL(__gpio_to_irq);
1839 1982
1840 1983
1841/* There's no value in making it easy to inline GPIO calls that may sleep. 1984/* There's no value in making it easy to inline GPIO calls that may sleep.
1842 * Common examples include ones connected to I2C or SPI chips. 1985 * Common examples include ones connected to I2C or SPI chips.
1843 */ 1986 */
1844 1987
1845int gpio_get_value_cansleep(unsigned gpio) 1988static int gpiod_get_value_cansleep(struct gpio_desc *desc)
1846{ 1989{
1847 struct gpio_chip *chip; 1990 struct gpio_chip *chip;
1848 int value; 1991 int value;
1992 int offset;
1849 1993
1850 might_sleep_if(extra_checks); 1994 might_sleep_if(extra_checks);
1851 chip = gpio_to_chip(gpio); 1995 chip = desc->chip;
1852 value = chip->get ? chip->get(chip, gpio - chip->base) : 0; 1996 offset = gpio_chip_hwgpio(desc);
1853 trace_gpio_value(gpio, 1, value); 1997 value = chip->get ? chip->get(chip, offset) : 0;
1998 trace_gpio_value(desc_to_gpio(desc), 1, value);
1854 return value; 1999 return value;
1855} 2000}
2001
2002int gpio_get_value_cansleep(unsigned gpio)
2003{
2004 return gpiod_get_value_cansleep(gpio_to_desc(gpio));
2005}
1856EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 2006EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1857 2007
1858void gpio_set_value_cansleep(unsigned gpio, int value) 2008static void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
1859{ 2009{
1860 struct gpio_chip *chip; 2010 struct gpio_chip *chip;
1861 2011
1862 might_sleep_if(extra_checks); 2012 might_sleep_if(extra_checks);
1863 chip = gpio_to_chip(gpio); 2013 chip = desc->chip;
1864 trace_gpio_value(gpio, 0, value); 2014 trace_gpio_value(desc_to_gpio(desc), 0, value);
1865 if (test_bit(FLAG_OPEN_DRAIN, &gpio_desc[gpio].flags)) 2015 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
1866 _gpio_set_open_drain_value(gpio, chip, value); 2016 _gpio_set_open_drain_value(desc, value);
1867 else if (test_bit(FLAG_OPEN_SOURCE, &gpio_desc[gpio].flags)) 2017 else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
1868 _gpio_set_open_source_value(gpio, chip, value); 2018 _gpio_set_open_source_value(desc, value);
1869 else 2019 else
1870 chip->set(chip, gpio - chip->base, value); 2020 chip->set(chip, gpio_chip_hwgpio(desc), value);
1871} 2021}
1872EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1873 2022
2023void gpio_set_value_cansleep(unsigned gpio, int value)
2024{
2025 return gpiod_set_value_cansleep(gpio_to_desc(gpio), value);
2026}
2027EXPORT_SYMBOL_GPL(gpio_set_value_cansleep);
1874 2028
1875#ifdef CONFIG_DEBUG_FS 2029#ifdef CONFIG_DEBUG_FS
1876 2030
@@ -1878,14 +2032,14 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1878{ 2032{
1879 unsigned i; 2033 unsigned i;
1880 unsigned gpio = chip->base; 2034 unsigned gpio = chip->base;
1881 struct gpio_desc *gdesc = &gpio_desc[gpio]; 2035 struct gpio_desc *gdesc = &chip->desc[0];
1882 int is_out; 2036 int is_out;
1883 2037
1884 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { 2038 for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
1885 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) 2039 if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
1886 continue; 2040 continue;
1887 2041
1888 gpio_get_direction(gpio); 2042 gpiod_get_direction(gdesc);
1889 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); 2043 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
1890 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s", 2044 seq_printf(s, " gpio-%-3d (%-20.20s) %s %s",
1891 gpio, gdesc->label, 2045 gpio, gdesc->label,
@@ -1899,46 +2053,35 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1899 2053
1900static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) 2054static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
1901{ 2055{
2056 unsigned long flags;
1902 struct gpio_chip *chip = NULL; 2057 struct gpio_chip *chip = NULL;
1903 unsigned int gpio; 2058 loff_t index = *pos;
1904 void *ret = NULL;
1905 loff_t index = 0;
1906
1907 /* REVISIT this isn't locked against gpio_chip removal ... */
1908 2059
1909 for (gpio = 0; gpio_is_valid(gpio); gpio++) { 2060 s->private = "";
1910 if (gpio_desc[gpio].chip == chip)
1911 continue;
1912
1913 chip = gpio_desc[gpio].chip;
1914 if (!chip)
1915 continue;
1916 2061
1917 if (index++ >= *pos) { 2062 spin_lock_irqsave(&gpio_lock, flags);
1918 ret = chip; 2063 list_for_each_entry(chip, &gpio_chips, list)
1919 break; 2064 if (index-- == 0) {
2065 spin_unlock_irqrestore(&gpio_lock, flags);
2066 return chip;
1920 } 2067 }
1921 } 2068 spin_unlock_irqrestore(&gpio_lock, flags);
1922
1923 s->private = "";
1924 2069
1925 return ret; 2070 return NULL;
1926} 2071}
1927 2072
1928static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) 2073static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
1929{ 2074{
2075 unsigned long flags;
1930 struct gpio_chip *chip = v; 2076 struct gpio_chip *chip = v;
1931 unsigned int gpio;
1932 void *ret = NULL; 2077 void *ret = NULL;
1933 2078
1934 /* skip GPIOs provided by the current chip */ 2079 spin_lock_irqsave(&gpio_lock, flags);
1935 for (gpio = chip->base + chip->ngpio; gpio_is_valid(gpio); gpio++) { 2080 if (list_is_last(&chip->list, &gpio_chips))
1936 chip = gpio_desc[gpio].chip; 2081 ret = NULL;
1937 if (chip) { 2082 else
1938 ret = chip; 2083 ret = list_entry(chip->list.next, struct gpio_chip, list);
1939 break; 2084 spin_unlock_irqrestore(&gpio_lock, flags);
1940 }
1941 }
1942 2085
1943 s->private = "\n"; 2086 s->private = "\n";
1944 ++*pos; 2087 ++*pos;