blob: ec918c27e77f1b97dec2987be31fcbfb11fd8711 [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
Ulf Hansson7f133de2014-12-18 15:44:34 +010021#include "slot-gpio.h"
22
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020023struct mmc_gpio {
Adrian Hunter842f4bd2014-03-10 15:02:39 +020024 struct gpio_desc *ro_gpio;
25 struct gpio_desc *cd_gpio;
26 bool override_ro_active_level;
27 bool override_cd_active_level;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020028 char *ro_label;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020029 char cd_label[0];
30};
31
32static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
33{
34 /* Schedule a card detection after a debounce timeout */
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010035 struct mmc_host *host = dev_id;
36
Markus Mayerfa372a52014-04-08 15:19:43 -070037 host->trigger_card_event = true;
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010038 mmc_detect_change(host, msecs_to_jiffies(200));
39
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020040 return IRQ_HANDLED;
41}
42
Ulf Hansson7f133de2014-12-18 15:44:34 +010043int mmc_gpio_alloc(struct mmc_host *host)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020044{
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 */
Ulf Hanssonb4cc5802014-12-18 15:44:33 +010057 ctx = devm_kzalloc(host->parent, 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
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100108 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800109 *
110 * Returns zero on success, else an error.
111 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200112int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
113{
114 struct mmc_gpio *ctx;
115 int ret;
116
117 if (!gpio_is_valid(gpio))
118 return -EINVAL;
119
120 ret = mmc_gpio_alloc(host);
121 if (ret < 0)
122 return ret;
123
124 ctx = host->slot.handler_priv;
125
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100126 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800127 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400128 if (ret < 0)
129 return ret;
130
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200131 ctx->override_ro_active_level = true;
132 ctx->ro_gpio = gpio_to_desc(gpio);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400133
134 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200135}
136EXPORT_SYMBOL(mmc_gpio_request_ro);
137
Adrian Hunter740a2212014-03-10 15:02:41 +0200138void mmc_gpiod_request_cd_irq(struct mmc_host *host)
Adrian Hunter26652672014-03-10 15:02:40 +0200139{
140 struct mmc_gpio *ctx = host->slot.handler_priv;
141 int ret, irq;
142
143 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
144 return;
145
146 irq = gpiod_to_irq(ctx->cd_gpio);
147
148 /*
149 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
150 * still prefer to poll, e.g., because that IRQ number is already used
151 * by another unit and cannot be shared.
152 */
153 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
154 irq = -EINVAL;
155
156 if (irq >= 0) {
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100157 ret = devm_request_threaded_irq(host->parent, irq,
Adrian Hunter26652672014-03-10 15:02:40 +0200158 NULL, mmc_gpio_cd_irqt,
159 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
160 ctx->cd_label, host);
161 if (ret < 0)
162 irq = ret;
163 }
164
165 host->slot.cd_irq = irq;
166
167 if (irq < 0)
168 host->caps |= MMC_CAP_NEEDS_POLL;
169}
Adrian Hunter740a2212014-03-10 15:02:41 +0200170EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
Adrian Hunter26652672014-03-10 15:02:40 +0200171
Shawn Guod65b5ae2012-12-11 22:32:18 +0800172/**
173 * mmc_gpio_request_cd - request a gpio for card-detection
174 * @host: mmc host
175 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200176 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800177 *
178 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100179 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800180 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200181 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
182 * value. The caller is responsible for ensuring that the GPIO driver associated
183 * with the GPIO supports debouncing, otherwise an error will be returned.
184 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800185 * Returns zero on success, else an error.
186 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200187int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
188 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200189{
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200190 struct mmc_gpio *ctx;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200191 int ret;
192
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200193 ret = mmc_gpio_alloc(host);
194 if (ret < 0)
195 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200196
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200197 ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200198
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100199 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800200 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200201 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200202 /*
203 * don't bother freeing memory. It might still get used by other
204 * slot functions, in any case it will be freed, when the device
205 * is destroyed.
206 */
207 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200208
Laurent Pinchart214fc302013-08-08 12:38:31 +0200209 if (debounce) {
210 ret = gpio_set_debounce(gpio, debounce);
211 if (ret < 0)
212 return ret;
213 }
214
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200215 ctx->override_cd_active_level = true;
216 ctx->cd_gpio = gpio_to_desc(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200217
218 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200219}
220EXPORT_SYMBOL(mmc_gpio_request_cd);
221
Shawn Guod65b5ae2012-12-11 22:32:18 +0800222/**
Adrian Hunter740a2212014-03-10 15:02:41 +0200223 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
224 * @host: mmc host
225 * @con_id: function within the GPIO consumer
226 * @idx: index of the GPIO to obtain in the consumer
227 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
228 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200229 * @gpio_invert: will return whether the GPIO line is inverted or not, set
230 * to NULL to ignore
Adrian Hunter740a2212014-03-10 15:02:41 +0200231 *
232 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100233 * descriptor API. Note that it must be called prior to mmc_add_host()
Adrian Hunter740a2212014-03-10 15:02:41 +0200234 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
235 *
236 * Returns zero on success, else an error.
237 */
238int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
239 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200240 unsigned int debounce, bool *gpio_invert)
Adrian Hunter740a2212014-03-10 15:02:41 +0200241{
242 struct mmc_gpio *ctx;
243 struct gpio_desc *desc;
244 int ret;
245
246 ret = mmc_gpio_alloc(host);
247 if (ret < 0)
248 return ret;
249
250 ctx = host->slot.handler_priv;
251
252 if (!con_id)
253 con_id = ctx->cd_label;
254
Linus Walleij9fbc6952014-08-27 13:00:50 +0200255 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
Adrian Hunter740a2212014-03-10 15:02:41 +0200256 if (IS_ERR(desc))
257 return PTR_ERR(desc);
258
Adrian Hunter740a2212014-03-10 15:02:41 +0200259 if (debounce) {
260 ret = gpiod_set_debounce(desc, debounce);
261 if (ret < 0)
262 return ret;
263 }
264
Linus Walleij89168b42014-10-02 09:08:46 +0200265 if (gpio_invert)
266 *gpio_invert = !gpiod_is_active_low(desc);
267
Adrian Hunter740a2212014-03-10 15:02:41 +0200268 ctx->override_cd_active_level = override_active_level;
269 ctx->cd_gpio = desc;
270
271 return 0;
272}
273EXPORT_SYMBOL(mmc_gpiod_request_cd);
274
275/**
Linus Walleij9d2fa242014-08-27 13:00:51 +0200276 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
277 * @host: mmc host
278 * @con_id: function within the GPIO consumer
279 * @idx: index of the GPIO to obtain in the consumer
280 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
281 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200282 * @gpio_invert: will return whether the GPIO line is inverted or not,
283 * set to NULL to ignore
Linus Walleij9d2fa242014-08-27 13:00:51 +0200284 *
285 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100286 * descriptor API.
Linus Walleij9d2fa242014-08-27 13:00:51 +0200287 *
288 * Returns zero on success, else an error.
289 */
290int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
291 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200292 unsigned int debounce, bool *gpio_invert)
Linus Walleij9d2fa242014-08-27 13:00:51 +0200293{
294 struct mmc_gpio *ctx;
295 struct gpio_desc *desc;
296 int ret;
297
298 ret = mmc_gpio_alloc(host);
299 if (ret < 0)
300 return ret;
301
302 ctx = host->slot.handler_priv;
303
304 if (!con_id)
305 con_id = ctx->ro_label;
306
307 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
308 if (IS_ERR(desc))
309 return PTR_ERR(desc);
310
311 if (debounce) {
312 ret = gpiod_set_debounce(desc, debounce);
313 if (ret < 0)
314 return ret;
315 }
316
Linus Walleij89168b42014-10-02 09:08:46 +0200317 if (gpio_invert)
318 *gpio_invert = !gpiod_is_active_low(desc);
319
Linus Walleij9d2fa242014-08-27 13:00:51 +0200320 ctx->override_ro_active_level = override_active_level;
321 ctx->ro_gpio = desc;
322
323 return 0;
324}
325EXPORT_SYMBOL(mmc_gpiod_request_ro);