2 * Generic GPIO card-detect helper
4 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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.
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>
24 static irqreturn_t
mmc_cd_gpio_irqt(int irq
, void *dev_id
)
26 /* Schedule a card detection after a debounce timeout */
27 mmc_detect_change(dev_id
, msecs_to_jiffies(100));
31 int mmc_cd_gpio_request(struct mmc_host
*host
, unsigned int gpio
)
33 size_t len
= strlen(dev_name(host
->parent
)) + 4;
34 struct mmc_cd_gpio
*cd
;
35 int irq
= gpio_to_irq(gpio
);
41 cd
= kmalloc(sizeof(*cd
) + len
, GFP_KERNEL
);
45 snprintf(cd
->label
, len
, "%s cd", dev_name(host
->parent
));
47 ret
= gpio_request_one(gpio
, GPIOF_DIR_IN
, cd
->label
);
51 ret
= request_threaded_irq(irq
, NULL
, mmc_cd_gpio_irqt
,
52 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
58 host
->hotplug
.irq
= irq
;
59 host
->hotplug
.handler_priv
= cd
;
69 EXPORT_SYMBOL(mmc_cd_gpio_request
);
71 void mmc_cd_gpio_free(struct mmc_host
*host
)
73 struct mmc_cd_gpio
*cd
= host
->hotplug
.handler_priv
;
75 free_irq(host
->hotplug
.irq
, host
);
79 EXPORT_SYMBOL(mmc_cd_gpio_free
);