forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/mmc/core/slot-gpio.c
....@@ -1,15 +1,11 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Generic GPIO card-detect helper
34 *
45 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
96 */
107
118 #include <linux/err.h>
12
-#include <linux/gpio.h>
139 #include <linux/gpio/consumer.h>
1410 #include <linux/interrupt.h>
1511 #include <linux/jiffies.h>
....@@ -18,17 +14,17 @@
1814 #include <linux/module.h>
1915 #include <linux/slab.h>
2016
17
+#include <trace/hooks/mmc_core.h>
18
+
2119 #include "slot-gpio.h"
2220
2321 struct mmc_gpio {
2422 struct gpio_desc *ro_gpio;
2523 struct gpio_desc *cd_gpio;
26
- bool override_ro_active_level;
27
- bool override_cd_active_level;
2824 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
2925 char *ro_label;
26
+ char *cd_label;
3027 u32 cd_debounce_delay_ms;
31
- char cd_label[];
3228 };
3329
3430 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
....@@ -36,6 +32,11 @@
3632 /* Schedule a card detection after a debounce timeout */
3733 struct mmc_host *host = dev_id;
3834 struct mmc_gpio *ctx = host->slot.handler_priv;
35
+ bool allow = true;
36
+
37
+ trace_android_vh_mmc_gpio_cd_irqt(host, &allow);
38
+ if (!allow)
39
+ return IRQ_HANDLED;
3940
4041 host->trigger_card_event = true;
4142 mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
....@@ -45,15 +46,19 @@
4546
4647 int mmc_gpio_alloc(struct mmc_host *host)
4748 {
48
- size_t len = strlen(dev_name(host->parent)) + 4;
4949 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
50
- sizeof(*ctx) + 2 * len, GFP_KERNEL);
50
+ sizeof(*ctx), GFP_KERNEL);
5151
5252 if (ctx) {
53
- ctx->ro_label = ctx->cd_label + len;
5453 ctx->cd_debounce_delay_ms = 200;
55
- snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
56
- snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
54
+ ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL,
55
+ "%s cd", dev_name(host->parent));
56
+ if (!ctx->cd_label)
57
+ return -ENOMEM;
58
+ ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL,
59
+ "%s ro", dev_name(host->parent));
60
+ if (!ctx->ro_label)
61
+ return -ENOMEM;
5762 host->slot.handler_priv = ctx;
5863 host->slot.cd_irq = -EINVAL;
5964 }
....@@ -68,10 +73,6 @@
6873 if (!ctx || !ctx->ro_gpio)
6974 return -ENOSYS;
7075
71
- if (ctx->override_ro_active_level)
72
- return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
73
- !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
74
-
7576 return gpiod_get_value_cansleep(ctx->ro_gpio);
7677 }
7778 EXPORT_SYMBOL(mmc_gpio_get_ro);
....@@ -85,48 +86,11 @@
8586 return -ENOSYS;
8687
8788 cansleep = gpiod_cansleep(ctx->cd_gpio);
88
- if (ctx->override_cd_active_level) {
89
- int value = cansleep ?
90
- gpiod_get_raw_value_cansleep(ctx->cd_gpio) :
91
- gpiod_get_raw_value(ctx->cd_gpio);
92
- return !value ^ !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
93
- }
94
-
9589 return cansleep ?
9690 gpiod_get_value_cansleep(ctx->cd_gpio) :
9791 gpiod_get_value(ctx->cd_gpio);
9892 }
9993 EXPORT_SYMBOL(mmc_gpio_get_cd);
100
-
101
-/**
102
- * mmc_gpio_request_ro - request a gpio for write-protection
103
- * @host: mmc host
104
- * @gpio: gpio number requested
105
- *
106
- * As devm_* managed functions are used in mmc_gpio_request_ro(), client
107
- * drivers do not need to worry about freeing up memory.
108
- *
109
- * Returns zero on success, else an error.
110
- */
111
-int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
112
-{
113
- struct mmc_gpio *ctx = host->slot.handler_priv;
114
- int ret;
115
-
116
- if (!gpio_is_valid(gpio))
117
- return -EINVAL;
118
-
119
- ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
120
- ctx->ro_label);
121
- if (ret < 0)
122
- return ret;
123
-
124
- ctx->override_ro_active_level = true;
125
- ctx->ro_gpio = gpio_to_desc(gpio);
126
-
127
- return 0;
128
-}
129
-EXPORT_SYMBOL(mmc_gpio_request_ro);
13094
13195 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
13296 {
....@@ -197,68 +161,21 @@
197161 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
198162
199163 /**
200
- * mmc_gpio_request_cd - request a gpio for card-detection
201
- * @host: mmc host
202
- * @gpio: gpio number requested
203
- * @debounce: debounce time in microseconds
204
- *
205
- * As devm_* managed functions are used in mmc_gpio_request_cd(), client
206
- * drivers do not need to worry about freeing up memory.
207
- *
208
- * If GPIO debouncing is desired, set the debounce parameter to a non-zero
209
- * value. The caller is responsible for ensuring that the GPIO driver associated
210
- * with the GPIO supports debouncing, otherwise an error will be returned.
211
- *
212
- * Returns zero on success, else an error.
213
- */
214
-int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
215
- unsigned int debounce)
216
-{
217
- struct mmc_gpio *ctx = host->slot.handler_priv;
218
- int ret;
219
-
220
- ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
221
- ctx->cd_label);
222
- if (ret < 0)
223
- /*
224
- * don't bother freeing memory. It might still get used by other
225
- * slot functions, in any case it will be freed, when the device
226
- * is destroyed.
227
- */
228
- return ret;
229
-
230
- if (debounce) {
231
- ret = gpio_set_debounce(gpio, debounce);
232
- if (ret < 0)
233
- return ret;
234
- }
235
-
236
- ctx->override_cd_active_level = true;
237
- ctx->cd_gpio = gpio_to_desc(gpio);
238
-
239
- return 0;
240
-}
241
-EXPORT_SYMBOL(mmc_gpio_request_cd);
242
-
243
-/**
244164 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
245165 * @host: mmc host
246166 * @con_id: function within the GPIO consumer
247167 * @idx: index of the GPIO to obtain in the consumer
248168 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
249169 * @debounce: debounce time in microseconds
250
- * @gpio_invert: will return whether the GPIO line is inverted or not, set
251
- * to NULL to ignore
252170 *
253
- * Use this function in place of mmc_gpio_request_cd() to use the GPIO
254
- * descriptor API. Note that it must be called prior to mmc_add_host()
171
+ * Note that this must be called prior to mmc_add_host()
255172 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
256173 *
257174 * Returns zero on success, else an error.
258175 */
259176 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
260177 unsigned int idx, bool override_active_level,
261
- unsigned int debounce, bool *gpio_invert)
178
+ unsigned int debounce)
262179 {
263180 struct mmc_gpio *ctx = host->slot.handler_priv;
264181 struct gpio_desc *desc;
....@@ -274,10 +191,14 @@
274191 ctx->cd_debounce_delay_ms = debounce / 1000;
275192 }
276193
277
- if (gpio_invert)
278
- *gpio_invert = !gpiod_is_active_low(desc);
194
+ /* override forces default (active-low) polarity ... */
195
+ if (override_active_level && !gpiod_is_active_low(desc))
196
+ gpiod_toggle_active_low(desc);
279197
280
- ctx->override_cd_active_level = override_active_level;
198
+ /* ... or active-high */
199
+ if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
200
+ gpiod_toggle_active_low(desc);
201
+
281202 ctx->cd_gpio = desc;
282203
283204 return 0;
....@@ -297,19 +218,12 @@
297218 * @host: mmc host
298219 * @con_id: function within the GPIO consumer
299220 * @idx: index of the GPIO to obtain in the consumer
300
- * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
301221 * @debounce: debounce time in microseconds
302
- * @gpio_invert: will return whether the GPIO line is inverted or not,
303
- * set to NULL to ignore
304
- *
305
- * Use this function in place of mmc_gpio_request_ro() to use the GPIO
306
- * descriptor API.
307222 *
308223 * Returns zero on success, else an error.
309224 */
310225 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
311
- unsigned int idx, bool override_active_level,
312
- unsigned int debounce, bool *gpio_invert)
226
+ unsigned int idx, unsigned int debounce)
313227 {
314228 struct mmc_gpio *ctx = host->slot.handler_priv;
315229 struct gpio_desc *desc;
....@@ -325,10 +239,9 @@
325239 return ret;
326240 }
327241
328
- if (gpio_invert)
329
- *gpio_invert = !gpiod_is_active_low(desc);
242
+ if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
243
+ gpiod_toggle_active_low(desc);
330244
331
- ctx->override_ro_active_level = override_active_level;
332245 ctx->ro_gpio = desc;
333246
334247 return 0;