blob: 47fa07e3604dca6a129a2474cb0e7325c21bac8a [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>
Adrian Hunter842f4bd2014-03-10 15:02:39 +020013#include <linux/gpio/consumer.h>
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020014#include <linux/interrupt.h>
15#include <linux/jiffies.h>
16#include <linux/mmc/host.h>
17#include <linux/mmc/slot-gpio.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20
21struct mmc_gpio {
Adrian Hunter842f4bd2014-03-10 15:02:39 +020022 struct gpio_desc *ro_gpio;
23 struct gpio_desc *cd_gpio;
24 bool override_ro_active_level;
25 bool override_cd_active_level;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020026 char *ro_label;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020027 char cd_label[0];
28};
29
30static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
31{
32 /* Schedule a card detection after a debounce timeout */
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010033 struct mmc_host *host = dev_id;
34
35 if (host->ops->card_event)
36 host->ops->card_event(host);
37
38 mmc_detect_change(host, msecs_to_jiffies(200));
39
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020040 return IRQ_HANDLED;
41}
42
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020043static int mmc_gpio_alloc(struct mmc_host *host)
44{
45 size_t len = strlen(dev_name(host->parent)) + 4;
46 struct mmc_gpio *ctx;
47
48 mutex_lock(&host->slot.lock);
49
50 ctx = host->slot.handler_priv;
51 if (!ctx) {
52 /*
53 * devm_kzalloc() can be called after device_initialize(), even
54 * before device_add(), i.e., between mmc_alloc_host() and
55 * mmc_add_host()
56 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020057 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020058 GFP_KERNEL);
59 if (ctx) {
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020060 ctx->ro_label = ctx->cd_label + len;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020061 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020062 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020063 host->slot.handler_priv = ctx;
64 }
65 }
66
67 mutex_unlock(&host->slot.lock);
68
69 return ctx ? 0 : -ENOMEM;
70}
71
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020072int mmc_gpio_get_ro(struct mmc_host *host)
73{
74 struct mmc_gpio *ctx = host->slot.handler_priv;
75
Adrian Hunter842f4bd2014-03-10 15:02:39 +020076 if (!ctx || !ctx->ro_gpio)
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020077 return -ENOSYS;
78
Adrian Hunter842f4bd2014-03-10 15:02:39 +020079 if (ctx->override_ro_active_level)
80 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
81 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
82
83 return gpiod_get_value_cansleep(ctx->ro_gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020084}
85EXPORT_SYMBOL(mmc_gpio_get_ro);
86
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020087int mmc_gpio_get_cd(struct mmc_host *host)
88{
89 struct mmc_gpio *ctx = host->slot.handler_priv;
90
Adrian Hunter842f4bd2014-03-10 15:02:39 +020091 if (!ctx || !ctx->cd_gpio)
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020092 return -ENOSYS;
93
Adrian Hunter842f4bd2014-03-10 15:02:39 +020094 if (ctx->override_cd_active_level)
95 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
96 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
97
98 return gpiod_get_value_cansleep(ctx->cd_gpio);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020099}
100EXPORT_SYMBOL(mmc_gpio_get_cd);
101
Shawn Guod65b5ae2012-12-11 22:32:18 +0800102/**
103 * mmc_gpio_request_ro - request a gpio for write-protection
104 * @host: mmc host
105 * @gpio: gpio number requested
106 *
107 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
108 * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
109 * if the requesting and freeing are only needed at probing and unbinding time
110 * for once. However, if client drivers do something special like runtime
111 * switching for write-protection, they are responsible for calling
112 * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
113 *
114 * Returns zero on success, else an error.
115 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200116int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
117{
118 struct mmc_gpio *ctx;
119 int ret;
120
121 if (!gpio_is_valid(gpio))
122 return -EINVAL;
123
124 ret = mmc_gpio_alloc(host);
125 if (ret < 0)
126 return ret;
127
128 ctx = host->slot.handler_priv;
129
Shawn Guod65b5ae2012-12-11 22:32:18 +0800130 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
131 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400132 if (ret < 0)
133 return ret;
134
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200135 ctx->override_ro_active_level = true;
136 ctx->ro_gpio = gpio_to_desc(gpio);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400137
138 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200139}
140EXPORT_SYMBOL(mmc_gpio_request_ro);
141
Adrian Hunter26652672014-03-10 15:02:40 +0200142static void mmc_gpiod_request_cd_irq(struct mmc_host *host)
143{
144 struct mmc_gpio *ctx = host->slot.handler_priv;
145 int ret, irq;
146
147 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
148 return;
149
150 irq = gpiod_to_irq(ctx->cd_gpio);
151
152 /*
153 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
154 * still prefer to poll, e.g., because that IRQ number is already used
155 * by another unit and cannot be shared.
156 */
157 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
158 irq = -EINVAL;
159
160 if (irq >= 0) {
161 ret = devm_request_threaded_irq(&host->class_dev, irq,
162 NULL, mmc_gpio_cd_irqt,
163 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
164 ctx->cd_label, host);
165 if (ret < 0)
166 irq = ret;
167 }
168
169 host->slot.cd_irq = irq;
170
171 if (irq < 0)
172 host->caps |= MMC_CAP_NEEDS_POLL;
173}
174
Shawn Guod65b5ae2012-12-11 22:32:18 +0800175/**
176 * mmc_gpio_request_cd - request a gpio for card-detection
177 * @host: mmc host
178 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200179 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800180 *
181 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
182 * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
183 * if the requesting and freeing are only needed at probing and unbinding time
184 * for once. However, if client drivers do something special like runtime
185 * switching for card-detection, they are responsible for calling
186 * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
187 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200188 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
189 * value. The caller is responsible for ensuring that the GPIO driver associated
190 * with the GPIO supports debouncing, otherwise an error will be returned.
191 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800192 * Returns zero on success, else an error.
193 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200194int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
195 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200196{
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200197 struct mmc_gpio *ctx;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200198 int ret;
199
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200200 ret = mmc_gpio_alloc(host);
201 if (ret < 0)
202 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200203
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200204 ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200205
Shawn Guod65b5ae2012-12-11 22:32:18 +0800206 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
207 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200208 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200209 /*
210 * don't bother freeing memory. It might still get used by other
211 * slot functions, in any case it will be freed, when the device
212 * is destroyed.
213 */
214 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200215
Laurent Pinchart214fc302013-08-08 12:38:31 +0200216 if (debounce) {
217 ret = gpio_set_debounce(gpio, debounce);
218 if (ret < 0)
219 return ret;
220 }
221
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200222 ctx->override_cd_active_level = true;
223 ctx->cd_gpio = gpio_to_desc(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200224
Adrian Hunter26652672014-03-10 15:02:40 +0200225 mmc_gpiod_request_cd_irq(host);
226
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200227 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200228}
229EXPORT_SYMBOL(mmc_gpio_request_cd);
230
Shawn Guod65b5ae2012-12-11 22:32:18 +0800231/**
232 * mmc_gpio_free_ro - free the write-protection gpio
233 * @host: mmc host
234 *
235 * It's provided only for cases that client drivers need to manually free
236 * up the write-protection gpio requested by mmc_gpio_request_ro().
237 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200238void mmc_gpio_free_ro(struct mmc_host *host)
239{
240 struct mmc_gpio *ctx = host->slot.handler_priv;
241 int gpio;
242
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200243 if (!ctx || !ctx->ro_gpio)
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200244 return;
245
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200246 gpio = desc_to_gpio(ctx->ro_gpio);
247 ctx->ro_gpio = NULL;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200248
Shawn Guod65b5ae2012-12-11 22:32:18 +0800249 devm_gpio_free(&host->class_dev, gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200250}
251EXPORT_SYMBOL(mmc_gpio_free_ro);
252
Shawn Guod65b5ae2012-12-11 22:32:18 +0800253/**
254 * mmc_gpio_free_cd - free the card-detection gpio
255 * @host: mmc host
256 *
257 * It's provided only for cases that client drivers need to manually free
258 * up the card-detection gpio requested by mmc_gpio_request_cd().
259 */
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200260void mmc_gpio_free_cd(struct mmc_host *host)
261{
Guennadi Liakhovetski27410ee2012-05-01 15:40:15 +0200262 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200263 int gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200264
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200265 if (!ctx || !ctx->cd_gpio)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200266 return;
267
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200268 if (host->slot.cd_irq >= 0) {
Shawn Guod65b5ae2012-12-11 22:32:18 +0800269 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200270 host->slot.cd_irq = -EINVAL;
271 }
272
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200273 gpio = desc_to_gpio(ctx->cd_gpio);
274 ctx->cd_gpio = NULL;
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200275
Shawn Guod65b5ae2012-12-11 22:32:18 +0800276 devm_gpio_free(&host->class_dev, gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200277}
278EXPORT_SYMBOL(mmc_gpio_free_cd);