blob: f9a0c6e8001ecf10183aad226a885762dcaf873e [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
Markus Mayerfa372a52014-04-08 15:19:43 -070035 host->trigger_card_event = true;
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010036 mmc_detect_change(host, msecs_to_jiffies(200));
37
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020038 return IRQ_HANDLED;
39}
40
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020041static int mmc_gpio_alloc(struct mmc_host *host)
42{
43 size_t len = strlen(dev_name(host->parent)) + 4;
44 struct mmc_gpio *ctx;
45
46 mutex_lock(&host->slot.lock);
47
48 ctx = host->slot.handler_priv;
49 if (!ctx) {
50 /*
51 * devm_kzalloc() can be called after device_initialize(), even
52 * before device_add(), i.e., between mmc_alloc_host() and
53 * mmc_add_host()
54 */
Ulf Hanssonb4cc5802014-12-18 15:44:33 +010055 ctx = devm_kzalloc(host->parent, sizeof(*ctx) + 2 * len,
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020056 GFP_KERNEL);
57 if (ctx) {
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020058 ctx->ro_label = ctx->cd_label + len;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020059 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020060 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020061 host->slot.handler_priv = ctx;
62 }
63 }
64
65 mutex_unlock(&host->slot.lock);
66
67 return ctx ? 0 : -ENOMEM;
68}
69
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020070int mmc_gpio_get_ro(struct mmc_host *host)
71{
72 struct mmc_gpio *ctx = host->slot.handler_priv;
73
Adrian Hunter842f4bd2014-03-10 15:02:39 +020074 if (!ctx || !ctx->ro_gpio)
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020075 return -ENOSYS;
76
Adrian Hunter842f4bd2014-03-10 15:02:39 +020077 if (ctx->override_ro_active_level)
78 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
80
81 return gpiod_get_value_cansleep(ctx->ro_gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020082}
83EXPORT_SYMBOL(mmc_gpio_get_ro);
84
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020085int mmc_gpio_get_cd(struct mmc_host *host)
86{
87 struct mmc_gpio *ctx = host->slot.handler_priv;
88
Adrian Hunter842f4bd2014-03-10 15:02:39 +020089 if (!ctx || !ctx->cd_gpio)
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020090 return -ENOSYS;
91
Adrian Hunter842f4bd2014-03-10 15:02:39 +020092 if (ctx->override_cd_active_level)
93 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
94 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
95
96 return gpiod_get_value_cansleep(ctx->cd_gpio);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020097}
98EXPORT_SYMBOL(mmc_gpio_get_cd);
99
Shawn Guod65b5ae2012-12-11 22:32:18 +0800100/**
101 * mmc_gpio_request_ro - request a gpio for write-protection
102 * @host: mmc host
103 * @gpio: gpio number requested
104 *
105 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100106 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800107 *
108 * Returns zero on success, else an error.
109 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200110int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
111{
112 struct mmc_gpio *ctx;
113 int ret;
114
115 if (!gpio_is_valid(gpio))
116 return -EINVAL;
117
118 ret = mmc_gpio_alloc(host);
119 if (ret < 0)
120 return ret;
121
122 ctx = host->slot.handler_priv;
123
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100124 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800125 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400126 if (ret < 0)
127 return ret;
128
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200129 ctx->override_ro_active_level = true;
130 ctx->ro_gpio = gpio_to_desc(gpio);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400131
132 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200133}
134EXPORT_SYMBOL(mmc_gpio_request_ro);
135
Adrian Hunter740a2212014-03-10 15:02:41 +0200136void mmc_gpiod_request_cd_irq(struct mmc_host *host)
Adrian Hunter26652672014-03-10 15:02:40 +0200137{
138 struct mmc_gpio *ctx = host->slot.handler_priv;
139 int ret, irq;
140
141 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
142 return;
143
144 irq = gpiod_to_irq(ctx->cd_gpio);
145
146 /*
147 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
148 * still prefer to poll, e.g., because that IRQ number is already used
149 * by another unit and cannot be shared.
150 */
151 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
152 irq = -EINVAL;
153
154 if (irq >= 0) {
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100155 ret = devm_request_threaded_irq(host->parent, irq,
Adrian Hunter26652672014-03-10 15:02:40 +0200156 NULL, mmc_gpio_cd_irqt,
157 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
158 ctx->cd_label, host);
159 if (ret < 0)
160 irq = ret;
161 }
162
163 host->slot.cd_irq = irq;
164
165 if (irq < 0)
166 host->caps |= MMC_CAP_NEEDS_POLL;
167}
Adrian Hunter740a2212014-03-10 15:02:41 +0200168EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
Adrian Hunter26652672014-03-10 15:02:40 +0200169
Shawn Guod65b5ae2012-12-11 22:32:18 +0800170/**
171 * mmc_gpio_request_cd - request a gpio for card-detection
172 * @host: mmc host
173 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200174 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800175 *
176 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100177 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800178 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200179 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
180 * value. The caller is responsible for ensuring that the GPIO driver associated
181 * with the GPIO supports debouncing, otherwise an error will be returned.
182 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800183 * Returns zero on success, else an error.
184 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200185int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
186 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200187{
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200188 struct mmc_gpio *ctx;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200189 int ret;
190
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200191 ret = mmc_gpio_alloc(host);
192 if (ret < 0)
193 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200194
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200195 ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200196
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100197 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800198 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200199 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200200 /*
201 * don't bother freeing memory. It might still get used by other
202 * slot functions, in any case it will be freed, when the device
203 * is destroyed.
204 */
205 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200206
Laurent Pinchart214fc302013-08-08 12:38:31 +0200207 if (debounce) {
208 ret = gpio_set_debounce(gpio, debounce);
209 if (ret < 0)
210 return ret;
211 }
212
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200213 ctx->override_cd_active_level = true;
214 ctx->cd_gpio = gpio_to_desc(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200215
216 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200217}
218EXPORT_SYMBOL(mmc_gpio_request_cd);
219
Shawn Guod65b5ae2012-12-11 22:32:18 +0800220/**
Adrian Hunter740a2212014-03-10 15:02:41 +0200221 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
222 * @host: mmc host
223 * @con_id: function within the GPIO consumer
224 * @idx: index of the GPIO to obtain in the consumer
225 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
226 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200227 * @gpio_invert: will return whether the GPIO line is inverted or not, set
228 * to NULL to ignore
Adrian Hunter740a2212014-03-10 15:02:41 +0200229 *
230 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100231 * descriptor API. Note that it must be called prior to mmc_add_host()
Adrian Hunter740a2212014-03-10 15:02:41 +0200232 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
233 *
234 * Returns zero on success, else an error.
235 */
236int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
237 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200238 unsigned int debounce, bool *gpio_invert)
Adrian Hunter740a2212014-03-10 15:02:41 +0200239{
240 struct mmc_gpio *ctx;
241 struct gpio_desc *desc;
242 int ret;
243
244 ret = mmc_gpio_alloc(host);
245 if (ret < 0)
246 return ret;
247
248 ctx = host->slot.handler_priv;
249
250 if (!con_id)
251 con_id = ctx->cd_label;
252
Linus Walleij9fbc6952014-08-27 13:00:50 +0200253 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
Adrian Hunter740a2212014-03-10 15:02:41 +0200254 if (IS_ERR(desc))
255 return PTR_ERR(desc);
256
Adrian Hunter740a2212014-03-10 15:02:41 +0200257 if (debounce) {
258 ret = gpiod_set_debounce(desc, debounce);
259 if (ret < 0)
260 return ret;
261 }
262
Linus Walleij89168b42014-10-02 09:08:46 +0200263 if (gpio_invert)
264 *gpio_invert = !gpiod_is_active_low(desc);
265
Adrian Hunter740a2212014-03-10 15:02:41 +0200266 ctx->override_cd_active_level = override_active_level;
267 ctx->cd_gpio = desc;
268
269 return 0;
270}
271EXPORT_SYMBOL(mmc_gpiod_request_cd);
272
273/**
Linus Walleij9d2fa242014-08-27 13:00:51 +0200274 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
275 * @host: mmc host
276 * @con_id: function within the GPIO consumer
277 * @idx: index of the GPIO to obtain in the consumer
278 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
279 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200280 * @gpio_invert: will return whether the GPIO line is inverted or not,
281 * set to NULL to ignore
Linus Walleij9d2fa242014-08-27 13:00:51 +0200282 *
283 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100284 * descriptor API.
Linus Walleij9d2fa242014-08-27 13:00:51 +0200285 *
286 * Returns zero on success, else an error.
287 */
288int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
289 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200290 unsigned int debounce, bool *gpio_invert)
Linus Walleij9d2fa242014-08-27 13:00:51 +0200291{
292 struct mmc_gpio *ctx;
293 struct gpio_desc *desc;
294 int ret;
295
296 ret = mmc_gpio_alloc(host);
297 if (ret < 0)
298 return ret;
299
300 ctx = host->slot.handler_priv;
301
302 if (!con_id)
303 con_id = ctx->ro_label;
304
305 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
306 if (IS_ERR(desc))
307 return PTR_ERR(desc);
308
309 if (debounce) {
310 ret = gpiod_set_debounce(desc, debounce);
311 if (ret < 0)
312 return ret;
313 }
314
Linus Walleij89168b42014-10-02 09:08:46 +0200315 if (gpio_invert)
316 *gpio_invert = !gpiod_is_active_low(desc);
317
Linus Walleij9d2fa242014-08-27 13:00:51 +0200318 ctx->override_ro_active_level = override_active_level;
319 ctx->ro_gpio = desc;
320
321 return 0;
322}
323EXPORT_SYMBOL(mmc_gpiod_request_ro);