blob: 8f5dc08d65989d8526f8bc59d1ed91426e93108d [file] [log] [blame]
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +01001/*
2 * Generic GPIO card-detect helper
3 *
4 * 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.
9 */
10
11#include <linux/err.h>
12#include <linux/gpio.h>
13#include <linux/interrupt.h>
14#include <linux/jiffies.h>
H Hartley Sweeten5ca65182012-04-17 13:03:38 -070015#include <linux/mmc/cd-gpio.h>
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010016#include <linux/mmc/host.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19
20struct mmc_cd_gpio {
21 unsigned int gpio;
22 char label[0];
23};
24
25static irqreturn_t mmc_cd_gpio_irqt(int irq, void *dev_id)
26{
27 /* Schedule a card detection after a debounce timeout */
28 mmc_detect_change(dev_id, msecs_to_jiffies(100));
29 return IRQ_HANDLED;
30}
31
Guennadi Liakhovetskic9b05462012-02-09 22:57:07 +010032int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio)
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010033{
34 size_t len = strlen(dev_name(host->parent)) + 4;
Guennadi Liakhovetskic9b05462012-02-09 22:57:07 +010035 struct mmc_cd_gpio *cd;
36 int irq = gpio_to_irq(gpio);
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010037 int ret;
38
Guennadi Liakhovetskic9b05462012-02-09 22:57:07 +010039 if (irq < 0)
40 return irq;
41
42 cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL);
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010043 if (!cd)
44 return -ENOMEM;
45
46 snprintf(cd->label, len, "%s cd", dev_name(host->parent));
47
48 ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label);
49 if (ret < 0)
50 goto egpioreq;
51
52 ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt,
Guennadi Liakhovetski623b51f2012-07-10 22:54:11 -040053 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
54 IRQF_ONESHOT, cd->label, host);
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010055 if (ret < 0)
56 goto eirqreq;
57
58 cd->gpio = gpio;
59 host->hotplug.irq = irq;
60 host->hotplug.handler_priv = cd;
61
62 return 0;
63
64eirqreq:
65 gpio_free(gpio);
66egpioreq:
67 kfree(cd);
68 return ret;
69}
70EXPORT_SYMBOL(mmc_cd_gpio_request);
71
72void mmc_cd_gpio_free(struct mmc_host *host)
73{
74 struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
75
Guennadi Liakhovetski0e9f4802012-04-24 17:56:29 +020076 if (!cd)
77 return;
78
Guennadi Liakhovetski349ab522011-12-25 21:36:02 +010079 free_irq(host->hotplug.irq, host);
80 gpio_free(cd->gpio);
81 kfree(cd);
82}
83EXPORT_SYMBOL(mmc_cd_gpio_free);