diff options
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r-- | drivers/gpio/gpiolib.c | 115 |
1 files changed, 100 insertions, 15 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 | */ |
72 | static void gpio_ensure_requested(struct gpio_desc *desc) | 73 | static 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); | |||
752 | int gpio_request(unsigned gpio, const char *label) | 763 | int 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 | ||
781 | done: | 807 | done: |
@@ -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); |
934 | lose: | ||
883 | return status; | 935 | return status; |
884 | fail: | 936 | fail: |
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); |
987 | lose: | ||
921 | return status; | 988 | return status; |
922 | fail: | 989 | fail: |
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 | } |
1009 | EXPORT_SYMBOL_GPL(__gpio_cansleep); | 1076 | EXPORT_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 | */ | ||
1087 | int __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 | } | ||
1094 | EXPORT_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 | } |
1025 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); | 1110 | EXPORT_SYMBOL_GPL(gpio_get_value_cansleep); |
1026 | 1111 | ||