aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio')
-rw-r--r--drivers/gpio/gpiolib.c115
-rw-r--r--drivers/gpio/max7301.c24
-rw-r--r--drivers/gpio/max732x.c5
-rw-r--r--drivers/gpio/mcp23s08.c5
-rw-r--r--drivers/gpio/pca953x.c5
-rw-r--r--drivers/gpio/pcf857x.c5
6 files changed, 128 insertions, 31 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 572d372899d3..faa1cc66e9cf 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -67,17 +67,28 @@ static inline void desc_set_label(struct gpio_desc *d, const char *label)
67 * when setting direction, and otherwise illegal. Until board setup code 67 * when setting direction, and otherwise illegal. Until board setup code
68 * and drivers use explicit requests everywhere (which won't happen when 68 * and drivers use explicit requests everywhere (which won't happen when
69 * those calls have no teeth) we can't avoid autorequesting. This nag 69 * those calls have no teeth) we can't avoid autorequesting. This nag
70 * message should motivate switching to explicit requests... 70 * message should motivate switching to explicit requests... so should
71 * the weaker cleanup after faults, compared to gpio_request().
71 */ 72 */
72static void gpio_ensure_requested(struct gpio_desc *desc) 73static int gpio_ensure_requested(struct gpio_desc *desc, unsigned offset)
73{ 74{
74 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { 75 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
75 pr_warning("GPIO-%d autorequested\n", (int)(desc - gpio_desc)); 76 struct gpio_chip *chip = desc->chip;
77 int gpio = chip->base + offset;
78
79 if (!try_module_get(chip->owner)) {
80 pr_err("GPIO-%d: module can't be gotten \n", gpio);
81 clear_bit(FLAG_REQUESTED, &desc->flags);
82 /* lose */
83 return -EIO;
84 }
85 pr_warning("GPIO-%d autorequested\n", gpio);
76 desc_set_label(desc, "[auto]"); 86 desc_set_label(desc, "[auto]");
77 if (!try_module_get(desc->chip->owner)) 87 /* caller must chip->request() w/o spinlock */
78 pr_err("GPIO-%d: module can't be gotten \n", 88 if (chip->request)
79 (int)(desc - gpio_desc)); 89 return 1;
80 } 90 }
91 return 0;
81} 92}
82 93
83/* caller holds gpio_lock *OR* gpio is marked as requested */ 94/* caller holds gpio_lock *OR* gpio is marked as requested */
@@ -237,7 +248,7 @@ static ssize_t gpio_value_show(struct device *dev,
237 if (!test_bit(FLAG_EXPORT, &desc->flags)) 248 if (!test_bit(FLAG_EXPORT, &desc->flags))
238 status = -EIO; 249 status = -EIO;
239 else 250 else
240 status = sprintf(buf, "%d\n", gpio_get_value_cansleep(gpio)); 251 status = sprintf(buf, "%d\n", !!gpio_get_value_cansleep(gpio));
241 252
242 mutex_unlock(&sysfs_lock); 253 mutex_unlock(&sysfs_lock);
243 return status; 254 return status;
@@ -752,6 +763,7 @@ EXPORT_SYMBOL_GPL(gpiochip_remove);
752int gpio_request(unsigned gpio, const char *label) 763int gpio_request(unsigned gpio, const char *label)
753{ 764{
754 struct gpio_desc *desc; 765 struct gpio_desc *desc;
766 struct gpio_chip *chip;
755 int status = -EINVAL; 767 int status = -EINVAL;
756 unsigned long flags; 768 unsigned long flags;
757 769
@@ -760,14 +772,15 @@ int gpio_request(unsigned gpio, const char *label)
760 if (!gpio_is_valid(gpio)) 772 if (!gpio_is_valid(gpio))
761 goto done; 773 goto done;
762 desc = &gpio_desc[gpio]; 774 desc = &gpio_desc[gpio];
763 if (desc->chip == NULL) 775 chip = desc->chip;
776 if (chip == NULL)
764 goto done; 777 goto done;
765 778
766 if (!try_module_get(desc->chip->owner)) 779 if (!try_module_get(chip->owner))
767 goto done; 780 goto done;
768 781
769 /* NOTE: gpio_request() can be called in early boot, 782 /* NOTE: gpio_request() can be called in early boot,
770 * before IRQs are enabled. 783 * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
771 */ 784 */
772 785
773 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { 786 if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
@@ -775,7 +788,20 @@ int gpio_request(unsigned gpio, const char *label)
775 status = 0; 788 status = 0;
776 } else { 789 } else {
777 status = -EBUSY; 790 status = -EBUSY;
778 module_put(desc->chip->owner); 791 module_put(chip->owner);
792 }
793
794 if (chip->request) {
795 /* chip->request may sleep */
796 spin_unlock_irqrestore(&gpio_lock, flags);
797 status = chip->request(chip, gpio - chip->base);
798 spin_lock_irqsave(&gpio_lock, flags);
799
800 if (status < 0) {
801 desc_set_label(desc, NULL);
802 module_put(chip->owner);
803 clear_bit(FLAG_REQUESTED, &desc->flags);
804 }
779 } 805 }
780 806
781done: 807done:
@@ -791,6 +817,9 @@ void gpio_free(unsigned gpio)
791{ 817{
792 unsigned long flags; 818 unsigned long flags;
793 struct gpio_desc *desc; 819 struct gpio_desc *desc;
820 struct gpio_chip *chip;
821
822 might_sleep();
794 823
795 if (!gpio_is_valid(gpio)) { 824 if (!gpio_is_valid(gpio)) {
796 WARN_ON(extra_checks); 825 WARN_ON(extra_checks);
@@ -802,9 +831,17 @@ void gpio_free(unsigned gpio)
802 spin_lock_irqsave(&gpio_lock, flags); 831 spin_lock_irqsave(&gpio_lock, flags);
803 832
804 desc = &gpio_desc[gpio]; 833 desc = &gpio_desc[gpio];
805 if (desc->chip && test_and_clear_bit(FLAG_REQUESTED, &desc->flags)) { 834 chip = desc->chip;
835 if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
836 if (chip->free) {
837 spin_unlock_irqrestore(&gpio_lock, flags);
838 might_sleep_if(extra_checks && chip->can_sleep);
839 chip->free(chip, gpio - chip->base);
840 spin_lock_irqsave(&gpio_lock, flags);
841 }
806 desc_set_label(desc, NULL); 842 desc_set_label(desc, NULL);
807 module_put(desc->chip->owner); 843 module_put(desc->chip->owner);
844 clear_bit(FLAG_REQUESTED, &desc->flags);
808 } else 845 } else
809 WARN_ON(extra_checks); 846 WARN_ON(extra_checks);
810 847
@@ -869,7 +906,9 @@ int gpio_direction_input(unsigned gpio)
869 gpio -= chip->base; 906 gpio -= chip->base;
870 if (gpio >= chip->ngpio) 907 if (gpio >= chip->ngpio)
871 goto fail; 908 goto fail;
872 gpio_ensure_requested(desc); 909 status = gpio_ensure_requested(desc, gpio);
910 if (status < 0)
911 goto fail;
873 912
874 /* now we know the gpio is valid and chip won't vanish */ 913 /* now we know the gpio is valid and chip won't vanish */
875 914
@@ -877,9 +916,22 @@ int gpio_direction_input(unsigned gpio)
877 916
878 might_sleep_if(extra_checks && chip->can_sleep); 917 might_sleep_if(extra_checks && chip->can_sleep);
879 918
919 if (status) {
920 status = chip->request(chip, gpio);
921 if (status < 0) {
922 pr_debug("GPIO-%d: chip request fail, %d\n",
923 chip->base + gpio, status);
924 /* and it's not available to anyone else ...
925 * gpio_request() is the fully clean solution.
926 */
927 goto lose;
928 }
929 }
930
880 status = chip->direction_input(chip, gpio); 931 status = chip->direction_input(chip, gpio);
881 if (status == 0) 932 if (status == 0)
882 clear_bit(FLAG_IS_OUT, &desc->flags); 933 clear_bit(FLAG_IS_OUT, &desc->flags);
934lose:
883 return status; 935 return status;
884fail: 936fail:
885 spin_unlock_irqrestore(&gpio_lock, flags); 937 spin_unlock_irqrestore(&gpio_lock, flags);
@@ -907,7 +959,9 @@ int gpio_direction_output(unsigned gpio, int value)
907 gpio -= chip->base; 959 gpio -= chip->base;
908 if (gpio >= chip->ngpio) 960 if (gpio >= chip->ngpio)
909 goto fail; 961 goto fail;
910 gpio_ensure_requested(desc); 962 status = gpio_ensure_requested(desc, gpio);
963 if (status < 0)
964 goto fail;
911 965
912 /* now we know the gpio is valid and chip won't vanish */ 966 /* now we know the gpio is valid and chip won't vanish */
913 967
@@ -915,9 +969,22 @@ int gpio_direction_output(unsigned gpio, int value)
915 969
916 might_sleep_if(extra_checks && chip->can_sleep); 970 might_sleep_if(extra_checks && chip->can_sleep);
917 971
972 if (status) {
973 status = chip->request(chip, gpio);
974 if (status < 0) {
975 pr_debug("GPIO-%d: chip request fail, %d\n",
976 chip->base + gpio, status);
977 /* and it's not available to anyone else ...
978 * gpio_request() is the fully clean solution.
979 */
980 goto lose;
981 }
982 }
983
918 status = chip->direction_output(chip, gpio, value); 984 status = chip->direction_output(chip, gpio, value);
919 if (status == 0) 985 if (status == 0)
920 set_bit(FLAG_IS_OUT, &desc->flags); 986 set_bit(FLAG_IS_OUT, &desc->flags);
987lose:
921 return status; 988 return status;
922fail: 989fail:
923 spin_unlock_irqrestore(&gpio_lock, flags); 990 spin_unlock_irqrestore(&gpio_lock, flags);
@@ -1008,6 +1075,24 @@ int __gpio_cansleep(unsigned gpio)
1008} 1075}
1009EXPORT_SYMBOL_GPL(__gpio_cansleep); 1076EXPORT_SYMBOL_GPL(__gpio_cansleep);
1010 1077
1078/**
1079 * __gpio_to_irq() - return the IRQ corresponding to a GPIO
1080 * @gpio: gpio whose IRQ will be returned (already requested)
1081 * Context: any
1082 *
1083 * This is used directly or indirectly to implement gpio_to_irq().
1084 * It returns the number of the IRQ signaled by this (input) GPIO,
1085 * or a negative errno.
1086 */
1087int __gpio_to_irq(unsigned gpio)
1088{
1089 struct gpio_chip *chip;
1090
1091 chip = gpio_to_chip(gpio);
1092 return chip->to_irq ? chip->to_irq(chip, gpio - chip->base) : -ENXIO;
1093}
1094EXPORT_SYMBOL_GPL(__gpio_to_irq);
1095
1011 1096
1012 1097
1013/* There's no value in making it easy to inline GPIO calls that may sleep. 1098/* There's no value in making it easy to inline GPIO calls that may sleep.
@@ -1020,7 +1105,7 @@ int gpio_get_value_cansleep(unsigned gpio)
1020 1105
1021 might_sleep_if(extra_checks); 1106 might_sleep_if(extra_checks);
1022 chip = gpio_to_chip(gpio); 1107 chip = gpio_to_chip(gpio);
1023 return chip->get(chip, gpio - chip->base); 1108 return chip->get ? chip->get(chip, gpio - chip->base) : 0;
1024} 1109}
1025EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); 1110EXPORT_SYMBOL_GPL(gpio_get_value_cansleep);
1026 1111
diff --git a/drivers/gpio/max7301.c b/drivers/gpio/max7301.c
index 39c795ad8312..8b24d784db93 100644
--- a/drivers/gpio/max7301.c
+++ b/drivers/gpio/max7301.c
@@ -255,10 +255,6 @@ static int __devinit max7301_probe(struct spi_device *spi)
255 ts->chip.dev = &spi->dev; 255 ts->chip.dev = &spi->dev;
256 ts->chip.owner = THIS_MODULE; 256 ts->chip.owner = THIS_MODULE;
257 257
258 ret = gpiochip_add(&ts->chip);
259 if (ret)
260 goto exit_destroy;
261
262 /* 258 /*
263 * tristate all pins in hardware and cache the 259 * tristate all pins in hardware and cache the
264 * register values for later use. 260 * register values for later use.
@@ -269,17 +265,19 @@ static int __devinit max7301_probe(struct spi_device *spi)
269 max7301_write(spi, 0x08 + i, 0xAA); 265 max7301_write(spi, 0x08 + i, 0xAA);
270 ts->port_config[i] = 0xAA; 266 ts->port_config[i] = 0xAA;
271 for (j = 0; j < 4; j++) { 267 for (j = 0; j < 4; j++) {
272 int idx = ts->chip.base + (i - 1) * 4 + j; 268 int offset = (i - 1) * 4 + j;
273 ret = gpio_direction_input(idx); 269 ret = max7301_direction_input(&ts->chip, offset);
274 if (ret) 270 if (ret)
275 goto exit_remove; 271 goto exit_destroy;
276 gpio_free(idx);
277 } 272 }
278 } 273 }
274
275 ret = gpiochip_add(&ts->chip);
276 if (ret)
277 goto exit_destroy;
278
279 return ret; 279 return ret;
280 280
281exit_remove:
282 gpiochip_remove(&ts->chip);
283exit_destroy: 281exit_destroy:
284 dev_set_drvdata(&spi->dev, NULL); 282 dev_set_drvdata(&spi->dev, NULL);
285 mutex_destroy(&ts->lock); 283 mutex_destroy(&ts->lock);
@@ -325,13 +323,15 @@ static int __init max7301_init(void)
325{ 323{
326 return spi_register_driver(&max7301_driver); 324 return spi_register_driver(&max7301_driver);
327} 325}
326/* register after spi postcore initcall and before
327 * subsys initcalls that may rely on these GPIOs
328 */
329subsys_initcall(max7301_init);
328 330
329static void __exit max7301_exit(void) 331static void __exit max7301_exit(void)
330{ 332{
331 spi_unregister_driver(&max7301_driver); 333 spi_unregister_driver(&max7301_driver);
332} 334}
333
334module_init(max7301_init);
335module_exit(max7301_exit); 335module_exit(max7301_exit);
336 336
337MODULE_AUTHOR("Juergen Beisert"); 337MODULE_AUTHOR("Juergen Beisert");
diff --git a/drivers/gpio/max732x.c b/drivers/gpio/max732x.c
index b51c8135ca28..55ae9a41897a 100644
--- a/drivers/gpio/max732x.c
+++ b/drivers/gpio/max732x.c
@@ -372,7 +372,10 @@ static int __init max732x_init(void)
372{ 372{
373 return i2c_add_driver(&max732x_driver); 373 return i2c_add_driver(&max732x_driver);
374} 374}
375module_init(max732x_init); 375/* register after i2c postcore initcall and before
376 * subsys initcalls that may rely on these GPIOs
377 */
378subsys_initcall(max732x_init);
376 379
377static void __exit max732x_exit(void) 380static void __exit max732x_exit(void)
378{ 381{
diff --git a/drivers/gpio/mcp23s08.c b/drivers/gpio/mcp23s08.c
index 8a1b405fefda..89c1d222e9d1 100644
--- a/drivers/gpio/mcp23s08.c
+++ b/drivers/gpio/mcp23s08.c
@@ -419,7 +419,10 @@ static int __init mcp23s08_init(void)
419{ 419{
420 return spi_register_driver(&mcp23s08_driver); 420 return spi_register_driver(&mcp23s08_driver);
421} 421}
422module_init(mcp23s08_init); 422/* register after spi postcore initcall and before
423 * subsys initcalls that may rely on these GPIOs
424 */
425subsys_initcall(mcp23s08_init);
423 426
424static void __exit mcp23s08_exit(void) 427static void __exit mcp23s08_exit(void)
425{ 428{
diff --git a/drivers/gpio/pca953x.c b/drivers/gpio/pca953x.c
index cc8468692ae0..9ceeb89f1325 100644
--- a/drivers/gpio/pca953x.c
+++ b/drivers/gpio/pca953x.c
@@ -289,7 +289,10 @@ static int __init pca953x_init(void)
289{ 289{
290 return i2c_add_driver(&pca953x_driver); 290 return i2c_add_driver(&pca953x_driver);
291} 291}
292module_init(pca953x_init); 292/* register after i2c postcore initcall and before
293 * subsys initcalls that may rely on these GPIOs
294 */
295subsys_initcall(pca953x_init);
293 296
294static void __exit pca953x_exit(void) 297static void __exit pca953x_exit(void)
295{ 298{
diff --git a/drivers/gpio/pcf857x.c b/drivers/gpio/pcf857x.c
index fc9c6ae739ee..4bc2070dd4a1 100644
--- a/drivers/gpio/pcf857x.c
+++ b/drivers/gpio/pcf857x.c
@@ -351,7 +351,10 @@ static int __init pcf857x_init(void)
351{ 351{
352 return i2c_add_driver(&pcf857x_driver); 352 return i2c_add_driver(&pcf857x_driver);
353} 353}
354module_init(pcf857x_init); 354/* register after i2c postcore initcall and before
355 * subsys initcalls that may rely on these GPIOs
356 */
357subsys_initcall(pcf857x_init);
355 358
356static void __exit pcf857x_exit(void) 359static void __exit pcf857x_exit(void)
357{ 360{