1
2
3
4
5
6
7
8
9
10
11#include <linux/err.h>
12#include <linux/gpio.h>
13#include <linux/gpio/consumer.h>
14#include <linux/interrupt.h>
15#include <linux/jiffies.h>
16#include <linux/mmc/host.h>
17#include <linux/mmc/slot-gpio.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20
21struct mmc_gpio {
22 struct gpio_desc *ro_gpio;
23 struct gpio_desc *cd_gpio;
24 bool override_ro_active_level;
25 bool override_cd_active_level;
26 char *ro_label;
27 char cd_label[0];
28};
29
30static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
31{
32
33 struct mmc_host *host = dev_id;
34
35 host->trigger_card_event = true;
36 mmc_detect_change(host, msecs_to_jiffies(200));
37
38 return IRQ_HANDLED;
39}
40
41static int mmc_gpio_alloc(struct mmc_host *host)
42{
43 size_t len = strlen(dev_name(host->parent)) + 4;
44 struct mmc_gpio *ctx;
45
46 mutex_lock(&host->slot.lock);
47
48 ctx = host->slot.handler_priv;
49 if (!ctx) {
50
51
52
53
54
55 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
56 GFP_KERNEL);
57 if (ctx) {
58 ctx->ro_label = ctx->cd_label + len;
59 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
60 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
61 host->slot.handler_priv = ctx;
62 }
63 }
64
65 mutex_unlock(&host->slot.lock);
66
67 return ctx ? 0 : -ENOMEM;
68}
69
70int mmc_gpio_get_ro(struct mmc_host *host)
71{
72 struct mmc_gpio *ctx = host->slot.handler_priv;
73
74 if (!ctx || !ctx->ro_gpio)
75 return -ENOSYS;
76
77 if (ctx->override_ro_active_level)
78 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
80
81 return gpiod_get_value_cansleep(ctx->ro_gpio);
82}
83EXPORT_SYMBOL(mmc_gpio_get_ro);
84
85int mmc_gpio_get_cd(struct mmc_host *host)
86{
87 struct mmc_gpio *ctx = host->slot.handler_priv;
88
89 if (!ctx || !ctx->cd_gpio)
90 return -ENOSYS;
91
92 if (ctx->override_cd_active_level)
93 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
94 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
95
96 return gpiod_get_value_cansleep(ctx->cd_gpio);
97}
98EXPORT_SYMBOL(mmc_gpio_get_cd);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
115{
116 struct mmc_gpio *ctx;
117 int ret;
118
119 if (!gpio_is_valid(gpio))
120 return -EINVAL;
121
122 ret = mmc_gpio_alloc(host);
123 if (ret < 0)
124 return ret;
125
126 ctx = host->slot.handler_priv;
127
128 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
129 ctx->ro_label);
130 if (ret < 0)
131 return ret;
132
133 ctx->override_ro_active_level = true;
134 ctx->ro_gpio = gpio_to_desc(gpio);
135
136 return 0;
137}
138EXPORT_SYMBOL(mmc_gpio_request_ro);
139
140void mmc_gpiod_request_cd_irq(struct mmc_host *host)
141{
142 struct mmc_gpio *ctx = host->slot.handler_priv;
143 int ret, irq;
144
145 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
146 return;
147
148 irq = gpiod_to_irq(ctx->cd_gpio);
149
150
151
152
153
154
155 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
156 irq = -EINVAL;
157
158 if (irq >= 0) {
159 ret = devm_request_threaded_irq(&host->class_dev, irq,
160 NULL, mmc_gpio_cd_irqt,
161 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
162 ctx->cd_label, host);
163 if (ret < 0)
164 irq = ret;
165 }
166
167 host->slot.cd_irq = irq;
168
169 if (irq < 0)
170 host->caps |= MMC_CAP_NEEDS_POLL;
171}
172EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
194 unsigned int debounce)
195{
196 struct mmc_gpio *ctx;
197 int ret;
198
199 ret = mmc_gpio_alloc(host);
200 if (ret < 0)
201 return ret;
202
203 ctx = host->slot.handler_priv;
204
205 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
206 ctx->cd_label);
207 if (ret < 0)
208
209
210
211
212
213 return ret;
214
215 if (debounce) {
216 ret = gpio_set_debounce(gpio, debounce);
217 if (ret < 0)
218 return ret;
219 }
220
221 ctx->override_cd_active_level = true;
222 ctx->cd_gpio = gpio_to_desc(gpio);
223
224 return 0;
225}
226EXPORT_SYMBOL(mmc_gpio_request_cd);
227
228
229
230
231
232
233
234
235void mmc_gpio_free_ro(struct mmc_host *host)
236{
237 struct mmc_gpio *ctx = host->slot.handler_priv;
238 int gpio;
239
240 if (!ctx || !ctx->ro_gpio)
241 return;
242
243 gpio = desc_to_gpio(ctx->ro_gpio);
244 ctx->ro_gpio = NULL;
245
246 devm_gpio_free(&host->class_dev, gpio);
247}
248EXPORT_SYMBOL(mmc_gpio_free_ro);
249
250
251
252
253
254
255
256
257void mmc_gpio_free_cd(struct mmc_host *host)
258{
259 struct mmc_gpio *ctx = host->slot.handler_priv;
260 int gpio;
261
262 if (!ctx || !ctx->cd_gpio)
263 return;
264
265 if (host->slot.cd_irq >= 0) {
266 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
267 host->slot.cd_irq = -EINVAL;
268 }
269
270 gpio = desc_to_gpio(ctx->cd_gpio);
271 ctx->cd_gpio = NULL;
272
273 devm_gpio_free(&host->class_dev, gpio);
274}
275EXPORT_SYMBOL(mmc_gpio_free_cd);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
295 unsigned int idx, bool override_active_level,
296 unsigned int debounce, bool *gpio_invert)
297{
298 struct mmc_gpio *ctx;
299 struct gpio_desc *desc;
300 int ret;
301
302 ret = mmc_gpio_alloc(host);
303 if (ret < 0)
304 return ret;
305
306 ctx = host->slot.handler_priv;
307
308 if (!con_id)
309 con_id = ctx->cd_label;
310
311 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
312 if (IS_ERR(desc))
313 return PTR_ERR(desc);
314
315 if (debounce) {
316 ret = gpiod_set_debounce(desc, debounce);
317 if (ret < 0)
318 return ret;
319 }
320
321 if (gpio_invert)
322 *gpio_invert = !gpiod_is_active_low(desc);
323
324 ctx->override_cd_active_level = override_active_level;
325 ctx->cd_gpio = desc;
326
327 return 0;
328}
329EXPORT_SYMBOL(mmc_gpiod_request_cd);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
348 unsigned int idx, bool override_active_level,
349 unsigned int debounce, bool *gpio_invert)
350{
351 struct mmc_gpio *ctx;
352 struct gpio_desc *desc;
353 int ret;
354
355 ret = mmc_gpio_alloc(host);
356 if (ret < 0)
357 return ret;
358
359 ctx = host->slot.handler_priv;
360
361 if (!con_id)
362 con_id = ctx->ro_label;
363
364 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
365 if (IS_ERR(desc))
366 return PTR_ERR(desc);
367
368 if (debounce) {
369 ret = gpiod_set_debounce(desc, debounce);
370 if (ret < 0)
371 return ret;
372 }
373
374 if (gpio_invert)
375 *gpio_invert = !gpiod_is_active_low(desc);
376
377 ctx->override_ro_active_level = override_active_level;
378 ctx->ro_gpio = desc;
379
380 return 0;
381}
382EXPORT_SYMBOL(mmc_gpiod_request_ro);
383
384
385
386
387
388
389
390
391void mmc_gpiod_free_cd(struct mmc_host *host)
392{
393 struct mmc_gpio *ctx = host->slot.handler_priv;
394
395 if (!ctx || !ctx->cd_gpio)
396 return;
397
398 if (host->slot.cd_irq >= 0) {
399 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
400 host->slot.cd_irq = -EINVAL;
401 }
402
403 devm_gpiod_put(host->parent, ctx->cd_gpio);
404
405 ctx->cd_gpio = NULL;
406}
407EXPORT_SYMBOL(mmc_gpiod_free_cd);
408