diff options
author | Alexandre Courbot <acourbot@nvidia.com> | 2014-07-22 03:17:43 -0400 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2014-07-23 11:46:00 -0400 |
commit | d82da79722400c63cc70f4c9c2493e31561ea607 (patch) | |
tree | 90d2bf33e888414e8ab8e987012abf71803fb8f5 | |
parent | d74be6dfea1b96cfb4bd79d9254fa9d21ed5f131 (diff) |
gpio: move gpio_ensure_requested() into legacy C file
gpio_ensure_requested() only makes sense when using the integer-based
GPIO API, so make sure it is called from there instead of the gpiod
API which we know cannot be called with a non-requested GPIO anyway.
The uses of gpio_ensure_requested() in the gpiod API were kind of
out-of-place anyway, so putting them in gpio-legacy.c helps clearing the
code.
Actually, considering the time this ensure_requested mechanism has been
around, maybe we should just turn this patch into "remove
gpio_ensure_requested()" if we know for sure that no user depend on it
anymore?
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r-- | drivers/gpio/gpiolib-legacy.c | 106 | ||||
-rw-r--r-- | drivers/gpio/gpiolib.c | 129 | ||||
-rw-r--r-- | include/asm-generic/gpio.h | 15 |
3 files changed, 113 insertions, 137 deletions
diff --git a/drivers/gpio/gpiolib-legacy.c b/drivers/gpio/gpiolib-legacy.c index 078ae6c2df79..0f9429b2522a 100644 --- a/drivers/gpio/gpiolib-legacy.c +++ b/drivers/gpio/gpiolib-legacy.c | |||
@@ -5,6 +5,64 @@ | |||
5 | 5 | ||
6 | #include "gpiolib.h" | 6 | #include "gpiolib.h" |
7 | 7 | ||
8 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | ||
9 | * when setting direction, and otherwise illegal. Until board setup code | ||
10 | * and drivers use explicit requests everywhere (which won't happen when | ||
11 | * those calls have no teeth) we can't avoid autorequesting. This nag | ||
12 | * message should motivate switching to explicit requests... so should | ||
13 | * the weaker cleanup after faults, compared to gpio_request(). | ||
14 | * | ||
15 | * NOTE: the autorequest mechanism is going away; at this point it's | ||
16 | * only "legal" in the sense that (old) code using it won't break yet, | ||
17 | * but instead only triggers a WARN() stack dump. | ||
18 | */ | ||
19 | static int gpio_ensure_requested(struct gpio_desc *desc) | ||
20 | { | ||
21 | struct gpio_chip *chip = desc->chip; | ||
22 | unsigned long flags; | ||
23 | bool request = false; | ||
24 | int err = 0; | ||
25 | |||
26 | spin_lock_irqsave(&gpio_lock, flags); | ||
27 | |||
28 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, | ||
29 | "autorequest GPIO-%d\n", desc_to_gpio(desc))) { | ||
30 | if (!try_module_get(chip->owner)) { | ||
31 | gpiod_err(desc, "%s: module can't be gotten\n", | ||
32 | __func__); | ||
33 | clear_bit(FLAG_REQUESTED, &desc->flags); | ||
34 | /* lose */ | ||
35 | err = -EIO; | ||
36 | goto end; | ||
37 | } | ||
38 | desc->label = "[auto]"; | ||
39 | /* caller must chip->request() w/o spinlock */ | ||
40 | if (chip->request) | ||
41 | request = true; | ||
42 | } | ||
43 | |||
44 | end: | ||
45 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
46 | |||
47 | if (request) { | ||
48 | might_sleep_if(chip->can_sleep); | ||
49 | err = chip->request(chip, gpio_chip_hwgpio(desc)); | ||
50 | |||
51 | if (err < 0) { | ||
52 | gpiod_dbg(desc, "%s: chip request fail, %d\n", | ||
53 | __func__, err); | ||
54 | spin_lock_irqsave(&gpio_lock, flags); | ||
55 | |||
56 | desc->label = NULL; | ||
57 | clear_bit(FLAG_REQUESTED, &desc->flags); | ||
58 | |||
59 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | return err; | ||
64 | } | ||
65 | |||
8 | void gpio_free(unsigned gpio) | 66 | void gpio_free(unsigned gpio) |
9 | { | 67 | { |
10 | gpiod_free(gpio_to_desc(gpio)); | 68 | gpiod_free(gpio_to_desc(gpio)); |
@@ -100,3 +158,51 @@ void gpio_free_array(const struct gpio *array, size_t num) | |||
100 | gpio_free((array++)->gpio); | 158 | gpio_free((array++)->gpio); |
101 | } | 159 | } |
102 | EXPORT_SYMBOL_GPL(gpio_free_array); | 160 | EXPORT_SYMBOL_GPL(gpio_free_array); |
161 | |||
162 | int gpio_direction_input(unsigned gpio) | ||
163 | { | ||
164 | struct gpio_desc *desc = gpio_to_desc(gpio); | ||
165 | int err; | ||
166 | |||
167 | if (!desc) | ||
168 | return -EINVAL; | ||
169 | |||
170 | err = gpio_ensure_requested(desc); | ||
171 | if (err < 0) | ||
172 | return err; | ||
173 | |||
174 | return gpiod_direction_input(desc); | ||
175 | } | ||
176 | EXPORT_SYMBOL_GPL(gpio_direction_input); | ||
177 | |||
178 | int gpio_direction_output(unsigned gpio, int value) | ||
179 | { | ||
180 | struct gpio_desc *desc = gpio_to_desc(gpio); | ||
181 | int err; | ||
182 | |||
183 | if (!desc) | ||
184 | return -EINVAL; | ||
185 | |||
186 | err = gpio_ensure_requested(desc); | ||
187 | if (err < 0) | ||
188 | return err; | ||
189 | |||
190 | return gpiod_direction_output_raw(desc, value); | ||
191 | } | ||
192 | EXPORT_SYMBOL_GPL(gpio_direction_output); | ||
193 | |||
194 | int gpio_set_debounce(unsigned gpio, unsigned debounce) | ||
195 | { | ||
196 | struct gpio_desc *desc = gpio_to_desc(gpio); | ||
197 | int err; | ||
198 | |||
199 | if (!desc) | ||
200 | return -EINVAL; | ||
201 | |||
202 | err = gpio_ensure_requested(desc); | ||
203 | if (err < 0) | ||
204 | return err; | ||
205 | |||
206 | return gpiod_set_debounce(desc, debounce); | ||
207 | } | ||
208 | EXPORT_SYMBOL_GPL(gpio_set_debounce); | ||
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 7582207c92e7..412d64e93cfb 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c | |||
@@ -95,39 +95,6 @@ int desc_to_gpio(const struct gpio_desc *desc) | |||
95 | EXPORT_SYMBOL_GPL(desc_to_gpio); | 95 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
96 | 96 | ||
97 | 97 | ||
98 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised | ||
99 | * when setting direction, and otherwise illegal. Until board setup code | ||
100 | * and drivers use explicit requests everywhere (which won't happen when | ||
101 | * those calls have no teeth) we can't avoid autorequesting. This nag | ||
102 | * message should motivate switching to explicit requests... so should | ||
103 | * the weaker cleanup after faults, compared to gpio_request(). | ||
104 | * | ||
105 | * NOTE: the autorequest mechanism is going away; at this point it's | ||
106 | * only "legal" in the sense that (old) code using it won't break yet, | ||
107 | * but instead only triggers a WARN() stack dump. | ||
108 | */ | ||
109 | static int gpio_ensure_requested(struct gpio_desc *desc) | ||
110 | { | ||
111 | const struct gpio_chip *chip = desc->chip; | ||
112 | const int gpio = desc_to_gpio(desc); | ||
113 | |||
114 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, | ||
115 | "autorequest GPIO-%d\n", gpio)) { | ||
116 | if (!try_module_get(chip->owner)) { | ||
117 | gpiod_err(desc, "%s: module can't be gotten\n", | ||
118 | __func__); | ||
119 | clear_bit(FLAG_REQUESTED, &desc->flags); | ||
120 | /* lose */ | ||
121 | return -EIO; | ||
122 | } | ||
123 | desc_set_label(desc, "[auto]"); | ||
124 | /* caller must chip->request() w/o spinlock */ | ||
125 | if (chip->request) | ||
126 | return 1; | ||
127 | } | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | /** | 98 | /** |
132 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | 99 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs |
133 | * @desc: descriptor to return the chip of | 100 | * @desc: descriptor to return the chip of |
@@ -964,10 +931,8 @@ void gpiochip_free_own_desc(struct gpio_desc *desc) | |||
964 | */ | 931 | */ |
965 | int gpiod_direction_input(struct gpio_desc *desc) | 932 | int gpiod_direction_input(struct gpio_desc *desc) |
966 | { | 933 | { |
967 | unsigned long flags; | ||
968 | struct gpio_chip *chip; | 934 | struct gpio_chip *chip; |
969 | int status = -EINVAL; | 935 | int status = -EINVAL; |
970 | int offset; | ||
971 | 936 | ||
972 | if (!desc || !desc->chip) { | 937 | if (!desc || !desc->chip) { |
973 | pr_warn("%s: invalid GPIO\n", __func__); | 938 | pr_warn("%s: invalid GPIO\n", __func__); |
@@ -982,52 +947,20 @@ int gpiod_direction_input(struct gpio_desc *desc) | |||
982 | return -EIO; | 947 | return -EIO; |
983 | } | 948 | } |
984 | 949 | ||
985 | spin_lock_irqsave(&gpio_lock, flags); | 950 | status = chip->direction_input(chip, gpio_chip_hwgpio(desc)); |
986 | |||
987 | status = gpio_ensure_requested(desc); | ||
988 | if (status < 0) | ||
989 | goto fail; | ||
990 | |||
991 | /* now we know the gpio is valid and chip won't vanish */ | ||
992 | |||
993 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
994 | |||
995 | might_sleep_if(chip->can_sleep); | ||
996 | |||
997 | offset = gpio_chip_hwgpio(desc); | ||
998 | if (status) { | ||
999 | status = chip->request(chip, offset); | ||
1000 | if (status < 0) { | ||
1001 | gpiod_dbg(desc, "%s: chip request fail, %d\n", | ||
1002 | __func__, status); | ||
1003 | /* and it's not available to anyone else ... | ||
1004 | * gpio_request() is the fully clean solution. | ||
1005 | */ | ||
1006 | goto lose; | ||
1007 | } | ||
1008 | } | ||
1009 | |||
1010 | status = chip->direction_input(chip, offset); | ||
1011 | if (status == 0) | 951 | if (status == 0) |
1012 | clear_bit(FLAG_IS_OUT, &desc->flags); | 952 | clear_bit(FLAG_IS_OUT, &desc->flags); |
1013 | 953 | ||
1014 | trace_gpio_direction(desc_to_gpio(desc), 1, status); | 954 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
1015 | lose: | 955 | |
1016 | return status; | ||
1017 | fail: | ||
1018 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1019 | if (status) | ||
1020 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
1021 | return status; | 956 | return status; |
1022 | } | 957 | } |
1023 | EXPORT_SYMBOL_GPL(gpiod_direction_input); | 958 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
1024 | 959 | ||
1025 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) | 960 | static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) |
1026 | { | 961 | { |
1027 | unsigned long flags; | ||
1028 | struct gpio_chip *chip; | 962 | struct gpio_chip *chip; |
1029 | int status = -EINVAL; | 963 | int status = -EINVAL; |
1030 | int offset; | ||
1031 | 964 | ||
1032 | /* GPIOs used for IRQs shall not be set as output */ | 965 | /* GPIOs used for IRQs shall not be set as output */ |
1033 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | 966 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { |
@@ -1053,42 +986,11 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) | |||
1053 | return -EIO; | 986 | return -EIO; |
1054 | } | 987 | } |
1055 | 988 | ||
1056 | spin_lock_irqsave(&gpio_lock, flags); | 989 | status = chip->direction_output(chip, gpio_chip_hwgpio(desc), value); |
1057 | |||
1058 | status = gpio_ensure_requested(desc); | ||
1059 | if (status < 0) | ||
1060 | goto fail; | ||
1061 | |||
1062 | /* now we know the gpio is valid and chip won't vanish */ | ||
1063 | |||
1064 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1065 | |||
1066 | might_sleep_if(chip->can_sleep); | ||
1067 | |||
1068 | offset = gpio_chip_hwgpio(desc); | ||
1069 | if (status) { | ||
1070 | status = chip->request(chip, offset); | ||
1071 | if (status < 0) { | ||
1072 | gpiod_dbg(desc, "%s: chip request fail, %d\n", | ||
1073 | __func__, status); | ||
1074 | /* and it's not available to anyone else ... | ||
1075 | * gpio_request() is the fully clean solution. | ||
1076 | */ | ||
1077 | goto lose; | ||
1078 | } | ||
1079 | } | ||
1080 | |||
1081 | status = chip->direction_output(chip, offset, value); | ||
1082 | if (status == 0) | 990 | if (status == 0) |
1083 | set_bit(FLAG_IS_OUT, &desc->flags); | 991 | set_bit(FLAG_IS_OUT, &desc->flags); |
1084 | trace_gpio_value(desc_to_gpio(desc), 0, value); | 992 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1085 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | 993 | trace_gpio_direction(desc_to_gpio(desc), 0, status); |
1086 | lose: | ||
1087 | return status; | ||
1088 | fail: | ||
1089 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1090 | if (status) | ||
1091 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); | ||
1092 | return status; | 994 | return status; |
1093 | } | 995 | } |
1094 | 996 | ||
@@ -1147,10 +1049,7 @@ EXPORT_SYMBOL_GPL(gpiod_direction_output); | |||
1147 | */ | 1049 | */ |
1148 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | 1050 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
1149 | { | 1051 | { |
1150 | unsigned long flags; | ||
1151 | struct gpio_chip *chip; | 1052 | struct gpio_chip *chip; |
1152 | int status = -EINVAL; | ||
1153 | int offset; | ||
1154 | 1053 | ||
1155 | if (!desc || !desc->chip) { | 1054 | if (!desc || !desc->chip) { |
1156 | pr_warn("%s: invalid GPIO\n", __func__); | 1055 | pr_warn("%s: invalid GPIO\n", __func__); |
@@ -1165,27 +1064,7 @@ int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) | |||
1165 | return -ENOTSUPP; | 1064 | return -ENOTSUPP; |
1166 | } | 1065 | } |
1167 | 1066 | ||
1168 | spin_lock_irqsave(&gpio_lock, flags); | 1067 | return chip->set_debounce(chip, gpio_chip_hwgpio(desc), debounce); |
1169 | |||
1170 | status = gpio_ensure_requested(desc); | ||
1171 | if (status < 0) | ||
1172 | goto fail; | ||
1173 | |||
1174 | /* now we know the gpio is valid and chip won't vanish */ | ||
1175 | |||
1176 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1177 | |||
1178 | might_sleep_if(chip->can_sleep); | ||
1179 | |||
1180 | offset = gpio_chip_hwgpio(desc); | ||
1181 | return chip->set_debounce(chip, offset, debounce); | ||
1182 | |||
1183 | fail: | ||
1184 | spin_unlock_irqrestore(&gpio_lock, flags); | ||
1185 | if (status) | ||
1186 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); | ||
1187 | |||
1188 | return status; | ||
1189 | } | 1068 | } |
1190 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); | 1069 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
1191 | 1070 | ||
diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index c1d4105e1c1d..39a1d06950d9 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h | |||
@@ -63,19 +63,10 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio) | |||
63 | extern int gpio_request(unsigned gpio, const char *label); | 63 | extern int gpio_request(unsigned gpio, const char *label); |
64 | extern void gpio_free(unsigned gpio); | 64 | extern void gpio_free(unsigned gpio); |
65 | 65 | ||
66 | static inline int gpio_direction_input(unsigned gpio) | 66 | extern int gpio_direction_input(unsigned gpio); |
67 | { | 67 | extern int gpio_direction_output(unsigned gpio, int value); |
68 | return gpiod_direction_input(gpio_to_desc(gpio)); | ||
69 | } | ||
70 | static inline int gpio_direction_output(unsigned gpio, int value) | ||
71 | { | ||
72 | return gpiod_direction_output_raw(gpio_to_desc(gpio), value); | ||
73 | } | ||
74 | 68 | ||
75 | static inline int gpio_set_debounce(unsigned gpio, unsigned debounce) | 69 | extern int gpio_set_debounce(unsigned gpio, unsigned debounce); |
76 | { | ||
77 | return gpiod_set_debounce(gpio_to_desc(gpio), debounce); | ||
78 | } | ||
79 | 70 | ||
80 | static inline int gpio_get_value_cansleep(unsigned gpio) | 71 | static inline int gpio_get_value_cansleep(unsigned gpio) |
81 | { | 72 | { |