blob: 41689da14b8d66a698f74a4178791ae220856e01 [file] [log] [blame]
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +02001/*
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/mmc/slot-gpio.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19
20struct mmc_gpio {
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020021 int cd_gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020022 char cd_label[0];
23};
24
25static irqreturn_t mmc_gpio_cd_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 Liakhovetskia7d1a1e2012-05-01 16:51:38 +020032static int mmc_gpio_alloc(struct mmc_host *host)
33{
34 size_t len = strlen(dev_name(host->parent)) + 4;
35 struct mmc_gpio *ctx;
36
37 mutex_lock(&host->slot.lock);
38
39 ctx = host->slot.handler_priv;
40 if (!ctx) {
41 /*
42 * devm_kzalloc() can be called after device_initialize(), even
43 * before device_add(), i.e., between mmc_alloc_host() and
44 * mmc_add_host()
45 */
46 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + len,
47 GFP_KERNEL);
48 if (ctx) {
49 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
50 ctx->cd_gpio = -EINVAL;
51 host->slot.handler_priv = ctx;
52 }
53 }
54
55 mutex_unlock(&host->slot.lock);
56
57 return ctx ? 0 : -ENOMEM;
58}
59
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020060int mmc_gpio_get_cd(struct mmc_host *host)
61{
62 struct mmc_gpio *ctx = host->slot.handler_priv;
63
64 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
65 return -ENOSYS;
66
67 return !gpio_get_value_cansleep(ctx->cd_gpio) ^
68 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
69}
70EXPORT_SYMBOL(mmc_gpio_get_cd);
71
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020072int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
73{
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020074 struct mmc_gpio *ctx;
75 int irq = gpio_to_irq(gpio);
76 int ret;
77
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020078 ret = mmc_gpio_alloc(host);
79 if (ret < 0)
80 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020081
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020082 ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020083
84 ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
85 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020086 /*
87 * don't bother freeing memory. It might still get used by other
88 * slot functions, in any case it will be freed, when the device
89 * is destroyed.
90 */
91 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020092
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020093 /*
94 * Even if gpio_to_irq() returns a valid IRQ number, the platform might
95 * still prefer to poll, e.g., because that IRQ number is already used
96 * by another unit and cannot be shared.
97 */
98 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
99 irq = -EINVAL;
100
101 if (irq >= 0) {
102 ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200103 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
104 ctx->cd_label, host);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200105 if (ret < 0)
106 irq = ret;
107 }
108
109 host->slot.cd_irq = irq;
110
111 if (irq < 0)
112 host->caps |= MMC_CAP_NEEDS_POLL;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200113
114 ctx->cd_gpio = gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200115
116 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200117}
118EXPORT_SYMBOL(mmc_gpio_request_cd);
119
120void mmc_gpio_free_cd(struct mmc_host *host)
121{
Guennadi Liakhovetski27410ee2012-05-01 15:40:15 +0200122 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200123 int gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200124
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200125 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200126 return;
127
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200128 if (host->slot.cd_irq >= 0) {
129 free_irq(host->slot.cd_irq, host);
130 host->slot.cd_irq = -EINVAL;
131 }
132
133 gpio = ctx->cd_gpio;
134 ctx->cd_gpio = -EINVAL;
135
136 gpio_free(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200137}
138EXPORT_SYMBOL(mmc_gpio_free_cd);