diff options
author | Guennadi Liakhovetski <g.liakhovetski@gmx.de> | 2011-12-25 15:36:02 -0500 |
---|---|---|
committer | Chris Ball <cjb@laptop.org> | 2012-01-11 23:58:45 -0500 |
commit | 349ab52446772a359bc7e7699cae3880d48fa5c9 (patch) | |
tree | ea51e81f4cd78375172063fac266885d11efad0e /drivers/mmc | |
parent | b67e198073b2d2f16572f5fa77553fec14775f69 (diff) |
mmc: add a generic GPIO card-detect helper
This patch adds a primitive helper to support card hotplug detection on
platforms, where a GPIO, capable of producing interrupts, is used for
detection of card-insertion and -removal events.
Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Signed-off-by: Chris Ball <cjb@laptop.org>
Diffstat (limited to 'drivers/mmc')
-rw-r--r-- | drivers/mmc/core/Makefile | 2 | ||||
-rw-r--r-- | drivers/mmc/core/cd-gpio.c | 74 |
2 files changed, 75 insertions, 1 deletions
diff --git a/drivers/mmc/core/Makefile b/drivers/mmc/core/Makefile index 639501970b41..dca4428380f1 100644 --- a/drivers/mmc/core/Makefile +++ b/drivers/mmc/core/Makefile | |||
@@ -7,6 +7,6 @@ mmc_core-y := core.o bus.o host.o \ | |||
7 | mmc.o mmc_ops.o sd.o sd_ops.o \ | 7 | mmc.o mmc_ops.o sd.o sd_ops.o \ |
8 | sdio.o sdio_ops.o sdio_bus.o \ | 8 | sdio.o sdio_ops.o sdio_bus.o \ |
9 | sdio_cis.o sdio_io.o sdio_irq.o \ | 9 | sdio_cis.o sdio_io.o sdio_irq.o \ |
10 | quirks.o | 10 | quirks.o cd-gpio.o |
11 | 11 | ||
12 | mmc_core-$(CONFIG_DEBUG_FS) += debugfs.o | 12 | mmc_core-$(CONFIG_DEBUG_FS) += debugfs.o |
diff --git a/drivers/mmc/core/cd-gpio.c b/drivers/mmc/core/cd-gpio.c new file mode 100644 index 000000000000..082202ae4a03 --- /dev/null +++ b/drivers/mmc/core/cd-gpio.c | |||
@@ -0,0 +1,74 @@ | |||
1 | /* | ||
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> | ||
15 | #include <linux/mmc/host.h> | ||
16 | #include <linux/module.h> | ||
17 | #include <linux/slab.h> | ||
18 | |||
19 | struct mmc_cd_gpio { | ||
20 | unsigned int gpio; | ||
21 | char label[0]; | ||
22 | }; | ||
23 | |||
24 | static irqreturn_t mmc_cd_gpio_irqt(int irq, void *dev_id) | ||
25 | { | ||
26 | /* Schedule a card detection after a debounce timeout */ | ||
27 | mmc_detect_change(dev_id, msecs_to_jiffies(100)); | ||
28 | return IRQ_HANDLED; | ||
29 | } | ||
30 | |||
31 | int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio, | ||
32 | unsigned int irq, unsigned long flags) | ||
33 | { | ||
34 | size_t len = strlen(dev_name(host->parent)) + 4; | ||
35 | struct mmc_cd_gpio *cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL); | ||
36 | int ret; | ||
37 | |||
38 | if (!cd) | ||
39 | return -ENOMEM; | ||
40 | |||
41 | snprintf(cd->label, len, "%s cd", dev_name(host->parent)); | ||
42 | |||
43 | ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label); | ||
44 | if (ret < 0) | ||
45 | goto egpioreq; | ||
46 | |||
47 | ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt, | ||
48 | flags, cd->label, host); | ||
49 | if (ret < 0) | ||
50 | goto eirqreq; | ||
51 | |||
52 | cd->gpio = gpio; | ||
53 | host->hotplug.irq = irq; | ||
54 | host->hotplug.handler_priv = cd; | ||
55 | |||
56 | return 0; | ||
57 | |||
58 | eirqreq: | ||
59 | gpio_free(gpio); | ||
60 | egpioreq: | ||
61 | kfree(cd); | ||
62 | return ret; | ||
63 | } | ||
64 | EXPORT_SYMBOL(mmc_cd_gpio_request); | ||
65 | |||
66 | void mmc_cd_gpio_free(struct mmc_host *host) | ||
67 | { | ||
68 | struct mmc_cd_gpio *cd = host->hotplug.handler_priv; | ||
69 | |||
70 | free_irq(host->hotplug.irq, host); | ||
71 | gpio_free(cd->gpio); | ||
72 | kfree(cd); | ||
73 | } | ||
74 | EXPORT_SYMBOL(mmc_cd_gpio_free); | ||