1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic GPIO card-detect helper
4 *
5 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 */
7
8 #include <linux/err.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/jiffies.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/slot-gpio.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16
17 #include <trace/hooks/mmc_core.h>
18
19 #include "slot-gpio.h"
20
21 struct mmc_gpio {
22 struct gpio_desc *ro_gpio;
23 struct gpio_desc *cd_gpio;
24 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
25 char *ro_label;
26 char *cd_label;
27 u32 cd_debounce_delay_ms;
28 };
29
mmc_gpio_cd_irqt(int irq,void * dev_id)30 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
31 {
32 /* Schedule a card detection after a debounce timeout */
33 struct mmc_host *host = dev_id;
34 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;
40
41 host->trigger_card_event = true;
42 mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
43
44 return IRQ_HANDLED;
45 }
46
mmc_gpio_alloc(struct mmc_host * host)47 int mmc_gpio_alloc(struct mmc_host *host)
48 {
49 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
50 sizeof(*ctx), GFP_KERNEL);
51
52 if (ctx) {
53 ctx->cd_debounce_delay_ms = 200;
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;
62 host->slot.handler_priv = ctx;
63 host->slot.cd_irq = -EINVAL;
64 }
65
66 return ctx ? 0 : -ENOMEM;
67 }
68
mmc_gpio_get_ro(struct mmc_host * host)69 int mmc_gpio_get_ro(struct mmc_host *host)
70 {
71 struct mmc_gpio *ctx = host->slot.handler_priv;
72 int cansleep;
73
74 if (!ctx || !ctx->ro_gpio)
75 return -ENOSYS;
76
77 cansleep = gpiod_cansleep(ctx->ro_gpio);
78 return cansleep ?
79 gpiod_get_value_cansleep(ctx->ro_gpio) :
80 gpiod_get_value(ctx->ro_gpio);
81 }
82 EXPORT_SYMBOL(mmc_gpio_get_ro);
83
mmc_gpio_get_cd(struct mmc_host * host)84 int mmc_gpio_get_cd(struct mmc_host *host)
85 {
86 struct mmc_gpio *ctx = host->slot.handler_priv;
87 int cansleep;
88
89 if (!ctx || !ctx->cd_gpio)
90 return -ENOSYS;
91
92 cansleep = gpiod_cansleep(ctx->cd_gpio);
93 return cansleep ?
94 gpiod_get_value_cansleep(ctx->cd_gpio) :
95 gpiod_get_value(ctx->cd_gpio);
96 }
97 EXPORT_SYMBOL(mmc_gpio_get_cd);
98
mmc_gpiod_request_cd_irq(struct mmc_host * host)99 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
100 {
101 struct mmc_gpio *ctx = host->slot.handler_priv;
102 int irq = -EINVAL;
103 int ret;
104
105 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
106 return;
107
108 /*
109 * Do not use IRQ if the platform prefers to poll, e.g., because that
110 * IRQ number is already used by another unit and cannot be shared.
111 */
112 if (!(host->caps & MMC_CAP_NEEDS_POLL))
113 irq = gpiod_to_irq(ctx->cd_gpio);
114
115 if (irq >= 0) {
116 if (!ctx->cd_gpio_isr)
117 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
118 ret = devm_request_threaded_irq(host->parent, irq,
119 NULL, ctx->cd_gpio_isr,
120 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
121 ctx->cd_label, host);
122 if (ret < 0)
123 irq = ret;
124 }
125
126 host->slot.cd_irq = irq;
127
128 if (irq < 0)
129 host->caps |= MMC_CAP_NEEDS_POLL;
130 }
131 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
132
mmc_gpio_set_cd_wake(struct mmc_host * host,bool on)133 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
134 {
135 int ret = 0;
136
137 if (!(host->caps & MMC_CAP_CD_WAKE) ||
138 host->slot.cd_irq < 0 ||
139 on == host->slot.cd_wake_enabled)
140 return 0;
141
142 if (on) {
143 ret = enable_irq_wake(host->slot.cd_irq);
144 host->slot.cd_wake_enabled = !ret;
145 } else {
146 disable_irq_wake(host->slot.cd_irq);
147 host->slot.cd_wake_enabled = false;
148 }
149
150 return ret;
151 }
152 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
153
154 /* Register an alternate interrupt service routine for
155 * the card-detect GPIO.
156 */
mmc_gpio_set_cd_isr(struct mmc_host * host,irqreturn_t (* isr)(int irq,void * dev_id))157 void mmc_gpio_set_cd_isr(struct mmc_host *host,
158 irqreturn_t (*isr)(int irq, void *dev_id))
159 {
160 struct mmc_gpio *ctx = host->slot.handler_priv;
161
162 WARN_ON(ctx->cd_gpio_isr);
163 ctx->cd_gpio_isr = isr;
164 }
165 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
166
167 /**
168 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
169 * @host: mmc host
170 * @con_id: function within the GPIO consumer
171 * @idx: index of the GPIO to obtain in the consumer
172 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
173 * @debounce: debounce time in microseconds
174 *
175 * Note that this must be called prior to mmc_add_host()
176 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
177 *
178 * Returns zero on success, else an error.
179 */
mmc_gpiod_request_cd(struct mmc_host * host,const char * con_id,unsigned int idx,bool override_active_level,unsigned int debounce)180 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
181 unsigned int idx, bool override_active_level,
182 unsigned int debounce)
183 {
184 struct mmc_gpio *ctx = host->slot.handler_priv;
185 struct gpio_desc *desc;
186 int ret;
187
188 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
189 if (IS_ERR(desc))
190 return PTR_ERR(desc);
191
192 if (debounce) {
193 ret = gpiod_set_debounce(desc, debounce);
194 if (ret < 0)
195 ctx->cd_debounce_delay_ms = debounce / 1000;
196 }
197
198 /* override forces default (active-low) polarity ... */
199 if (override_active_level && !gpiod_is_active_low(desc))
200 gpiod_toggle_active_low(desc);
201
202 /* ... or active-high */
203 if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
204 gpiod_toggle_active_low(desc);
205
206 ctx->cd_gpio = desc;
207
208 return 0;
209 }
210 EXPORT_SYMBOL(mmc_gpiod_request_cd);
211
mmc_can_gpio_cd(struct mmc_host * host)212 bool mmc_can_gpio_cd(struct mmc_host *host)
213 {
214 struct mmc_gpio *ctx = host->slot.handler_priv;
215
216 return ctx->cd_gpio ? true : false;
217 }
218 EXPORT_SYMBOL(mmc_can_gpio_cd);
219
220 /**
221 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
222 * @host: mmc host
223 * @con_id: function within the GPIO consumer
224 * @idx: index of the GPIO to obtain in the consumer
225 * @debounce: debounce time in microseconds
226 *
227 * Returns zero on success, else an error.
228 */
mmc_gpiod_request_ro(struct mmc_host * host,const char * con_id,unsigned int idx,unsigned int debounce)229 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
230 unsigned int idx, unsigned int debounce)
231 {
232 struct mmc_gpio *ctx = host->slot.handler_priv;
233 struct gpio_desc *desc;
234 int ret;
235
236 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
237 if (IS_ERR(desc))
238 return PTR_ERR(desc);
239
240 if (debounce) {
241 ret = gpiod_set_debounce(desc, debounce);
242 if (ret < 0)
243 return ret;
244 }
245
246 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
247 gpiod_toggle_active_low(desc);
248
249 ctx->ro_gpio = desc;
250
251 return 0;
252 }
253 EXPORT_SYMBOL(mmc_gpiod_request_ro);
254
mmc_can_gpio_ro(struct mmc_host * host)255 bool mmc_can_gpio_ro(struct mmc_host *host)
256 {
257 struct mmc_gpio *ctx = host->slot.handler_priv;
258
259 return ctx->ro_gpio ? true : false;
260 }
261 EXPORT_SYMBOL(mmc_can_gpio_ro);
262