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/mmc/slot-gpio.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
25 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
27 /* Schedule a card detection after a debounce timeout */
28 mmc_detect_change(dev_id, msecs_to_jiffies(100));
32 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
34 size_t len = strlen(dev_name(host->parent)) + 4;
36 int irq = gpio_to_irq(gpio);
42 ctx = kmalloc(sizeof(*ctx) + len, GFP_KERNEL);
46 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
48 ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
52 ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
53 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
59 host->hotplug.irq = irq;
60 host->hotplug.handler_priv = ctx;
70 EXPORT_SYMBOL(mmc_gpio_request_cd);
72 void mmc_gpio_free_cd(struct mmc_host *host)
74 struct mmc_gpio *ctx = host->hotplug.handler_priv;
79 free_irq(host->hotplug.irq, host);
80 gpio_free(ctx->cd_gpio);
83 EXPORT_SYMBOL(mmc_gpio_free_cd);