• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cd-gpio.h>
16 #include <linux/mmc/host.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 
20 struct mmc_cd_gpio {
21 	unsigned int gpio;
22 	char label[0];
23 };
24 
mmc_cd_gpio_irqt(int irq,void * dev_id)25 static 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 
mmc_cd_gpio_request(struct mmc_host * host,unsigned int gpio)32 int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio)
33 {
34 	size_t len = strlen(dev_name(host->parent)) + 4;
35 	struct mmc_cd_gpio *cd;
36 	int irq = gpio_to_irq(gpio);
37 	int ret;
38 
39 	if (irq < 0)
40 		return irq;
41 
42 	cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL);
43 	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,
53 				   IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
54 				   cd->label, host);
55 	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 
64 eirqreq:
65 	gpio_free(gpio);
66 egpioreq:
67 	kfree(cd);
68 	return ret;
69 }
70 EXPORT_SYMBOL(mmc_cd_gpio_request);
71 
mmc_cd_gpio_free(struct mmc_host * host)72 void mmc_cd_gpio_free(struct mmc_host *host)
73 {
74 	struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
75 
76 	if (!cd)
77 		return;
78 
79 	free_irq(host->hotplug.irq, host);
80 	gpio_free(cd->gpio);
81 	kfree(cd);
82 }
83 EXPORT_SYMBOL(mmc_cd_gpio_free);
84