blob: 31f7dbb1566873a4e05ae336c3c602fcf0f53c53 [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;
NeilBrownc7ea8342015-01-13 08:23:18 +130028 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020029 char *ro_label;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020030 char cd_label[0];
31};
32
33static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
34{
35 /* Schedule a card detection after a debounce timeout */
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010036 struct mmc_host *host = dev_id;
37
Markus Mayerfa372a52014-04-08 15:19:43 -070038 host->trigger_card_event = true;
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010039 mmc_detect_change(host, msecs_to_jiffies(200));
40
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020041 return IRQ_HANDLED;
42}
43
Ulf Hansson7f133de2014-12-18 15:44:34 +010044int mmc_gpio_alloc(struct mmc_host *host)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020045{
46 size_t len = strlen(dev_name(host->parent)) + 4;
Ulf Hanssondf8aca12014-12-18 15:44:36 +010047 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
48 sizeof(*ctx) + 2 * len, GFP_KERNEL);
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020049
Ulf Hanssondf8aca12014-12-18 15:44:36 +010050 if (ctx) {
51 ctx->ro_label = ctx->cd_label + len;
52 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
53 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
54 host->slot.handler_priv = ctx;
55 host->slot.cd_irq = -EINVAL;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020056 }
57
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020058 return ctx ? 0 : -ENOMEM;
59}
60
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020061int mmc_gpio_get_ro(struct mmc_host *host)
62{
63 struct mmc_gpio *ctx = host->slot.handler_priv;
64
Adrian Hunter842f4bd2014-03-10 15:02:39 +020065 if (!ctx || !ctx->ro_gpio)
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020066 return -ENOSYS;
67
Adrian Hunter842f4bd2014-03-10 15:02:39 +020068 if (ctx->override_ro_active_level)
69 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
70 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
71
72 return gpiod_get_value_cansleep(ctx->ro_gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020073}
74EXPORT_SYMBOL(mmc_gpio_get_ro);
75
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020076int mmc_gpio_get_cd(struct mmc_host *host)
77{
78 struct mmc_gpio *ctx = host->slot.handler_priv;
79
Adrian Hunter842f4bd2014-03-10 15:02:39 +020080 if (!ctx || !ctx->cd_gpio)
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020081 return -ENOSYS;
82
Adrian Hunter842f4bd2014-03-10 15:02:39 +020083 if (ctx->override_cd_active_level)
84 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
85 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
86
87 return gpiod_get_value_cansleep(ctx->cd_gpio);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020088}
89EXPORT_SYMBOL(mmc_gpio_get_cd);
90
Shawn Guod65b5ae2012-12-11 22:32:18 +080091/**
92 * mmc_gpio_request_ro - request a gpio for write-protection
93 * @host: mmc host
94 * @gpio: gpio number requested
95 *
96 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +010097 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +080098 *
99 * Returns zero on success, else an error.
100 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200101int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
102{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100103 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200104 int ret;
105
106 if (!gpio_is_valid(gpio))
107 return -EINVAL;
108
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100109 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800110 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400111 if (ret < 0)
112 return ret;
113
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200114 ctx->override_ro_active_level = true;
115 ctx->ro_gpio = gpio_to_desc(gpio);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400116
117 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200118}
119EXPORT_SYMBOL(mmc_gpio_request_ro);
120
Adrian Hunter740a2212014-03-10 15:02:41 +0200121void mmc_gpiod_request_cd_irq(struct mmc_host *host)
Adrian Hunter26652672014-03-10 15:02:40 +0200122{
123 struct mmc_gpio *ctx = host->slot.handler_priv;
Masahiro Yamada5f3a8602017-11-08 15:34:54 +0900124 int irq = -EINVAL;
125 int ret;
Adrian Hunter26652672014-03-10 15:02:40 +0200126
127 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
128 return;
129
Adrian Hunter26652672014-03-10 15:02:40 +0200130 /*
Masahiro Yamada5f3a8602017-11-08 15:34:54 +0900131 * Do not use IRQ if the platform prefers to poll, e.g., because that
132 * IRQ number is already used by another unit and cannot be shared.
Adrian Hunter26652672014-03-10 15:02:40 +0200133 */
Masahiro Yamada5f3a8602017-11-08 15:34:54 +0900134 if (!(host->caps & MMC_CAP_NEEDS_POLL))
135 irq = gpiod_to_irq(ctx->cd_gpio);
Adrian Hunter26652672014-03-10 15:02:40 +0200136
137 if (irq >= 0) {
NeilBrownc7ea8342015-01-13 08:23:18 +1300138 if (!ctx->cd_gpio_isr)
139 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100140 ret = devm_request_threaded_irq(host->parent, irq,
NeilBrownc7ea8342015-01-13 08:23:18 +1300141 NULL, ctx->cd_gpio_isr,
Adrian Hunter26652672014-03-10 15:02:40 +0200142 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
143 ctx->cd_label, host);
144 if (ret < 0)
145 irq = ret;
146 }
147
148 host->slot.cd_irq = irq;
149
150 if (irq < 0)
151 host->caps |= MMC_CAP_NEEDS_POLL;
152}
Adrian Hunter740a2212014-03-10 15:02:41 +0200153EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
Adrian Hunter26652672014-03-10 15:02:40 +0200154
Adrian Hunter36f1d7e2018-02-27 14:51:25 +0200155int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
156{
157 int ret = 0;
158
159 if (!(host->caps & MMC_CAP_CD_WAKE) ||
160 host->slot.cd_irq < 0 ||
161 on == host->slot.cd_wake_enabled)
162 return 0;
163
164 if (on) {
165 ret = enable_irq_wake(host->slot.cd_irq);
166 host->slot.cd_wake_enabled = !ret;
167 } else {
168 disable_irq_wake(host->slot.cd_irq);
169 host->slot.cd_wake_enabled = false;
170 }
171
172 return ret;
173}
174EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
175
NeilBrownc7ea8342015-01-13 08:23:18 +1300176/* Register an alternate interrupt service routine for
177 * the card-detect GPIO.
178 */
179void mmc_gpio_set_cd_isr(struct mmc_host *host,
180 irqreturn_t (*isr)(int irq, void *dev_id))
181{
182 struct mmc_gpio *ctx = host->slot.handler_priv;
183
184 WARN_ON(ctx->cd_gpio_isr);
185 ctx->cd_gpio_isr = isr;
186}
187EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
188
Shawn Guod65b5ae2012-12-11 22:32:18 +0800189/**
190 * mmc_gpio_request_cd - request a gpio for card-detection
191 * @host: mmc host
192 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200193 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800194 *
195 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100196 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800197 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200198 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
199 * value. The caller is responsible for ensuring that the GPIO driver associated
200 * with the GPIO supports debouncing, otherwise an error will be returned.
201 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800202 * Returns zero on success, else an error.
203 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200204int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
205 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200206{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100207 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200208 int ret;
209
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100210 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800211 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200212 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200213 /*
214 * don't bother freeing memory. It might still get used by other
215 * slot functions, in any case it will be freed, when the device
216 * is destroyed.
217 */
218 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200219
Laurent Pinchart214fc302013-08-08 12:38:31 +0200220 if (debounce) {
221 ret = gpio_set_debounce(gpio, debounce);
222 if (ret < 0)
223 return ret;
224 }
225
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200226 ctx->override_cd_active_level = true;
227 ctx->cd_gpio = gpio_to_desc(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200228
229 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200230}
231EXPORT_SYMBOL(mmc_gpio_request_cd);
232
Shawn Guod65b5ae2012-12-11 22:32:18 +0800233/**
Adrian Hunter740a2212014-03-10 15:02:41 +0200234 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
235 * @host: mmc host
236 * @con_id: function within the GPIO consumer
237 * @idx: index of the GPIO to obtain in the consumer
238 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
239 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200240 * @gpio_invert: will return whether the GPIO line is inverted or not, set
241 * to NULL to ignore
Adrian Hunter740a2212014-03-10 15:02:41 +0200242 *
243 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100244 * descriptor API. Note that it must be called prior to mmc_add_host()
Adrian Hunter740a2212014-03-10 15:02:41 +0200245 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
246 *
247 * Returns zero on success, else an error.
248 */
249int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
250 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200251 unsigned int debounce, bool *gpio_invert)
Adrian Hunter740a2212014-03-10 15:02:41 +0200252{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100253 struct mmc_gpio *ctx = host->slot.handler_priv;
Adrian Hunter740a2212014-03-10 15:02:41 +0200254 struct gpio_desc *desc;
255 int ret;
256
Linus Walleij9fbc6952014-08-27 13:00:50 +0200257 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
Adrian Hunter740a2212014-03-10 15:02:41 +0200258 if (IS_ERR(desc))
259 return PTR_ERR(desc);
260
Adrian Hunter740a2212014-03-10 15:02:41 +0200261 if (debounce) {
262 ret = gpiod_set_debounce(desc, debounce);
263 if (ret < 0)
264 return ret;
265 }
266
Linus Walleij89168b42014-10-02 09:08:46 +0200267 if (gpio_invert)
268 *gpio_invert = !gpiod_is_active_low(desc);
269
Adrian Hunter740a2212014-03-10 15:02:41 +0200270 ctx->override_cd_active_level = override_active_level;
271 ctx->cd_gpio = desc;
272
273 return 0;
274}
275EXPORT_SYMBOL(mmc_gpiod_request_cd);
276
Shawn Lin50fcbbb2016-10-12 10:50:37 +0800277bool mmc_can_gpio_cd(struct mmc_host *host)
278{
279 struct mmc_gpio *ctx = host->slot.handler_priv;
280
281 return ctx->cd_gpio ? true : false;
282}
283EXPORT_SYMBOL(mmc_can_gpio_cd);
284
Adrian Hunter740a2212014-03-10 15:02:41 +0200285/**
Linus Walleij9d2fa242014-08-27 13:00:51 +0200286 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
287 * @host: mmc host
288 * @con_id: function within the GPIO consumer
289 * @idx: index of the GPIO to obtain in the consumer
290 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
291 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200292 * @gpio_invert: will return whether the GPIO line is inverted or not,
293 * set to NULL to ignore
Linus Walleij9d2fa242014-08-27 13:00:51 +0200294 *
295 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100296 * descriptor API.
Linus Walleij9d2fa242014-08-27 13:00:51 +0200297 *
298 * Returns zero on success, else an error.
299 */
300int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
301 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200302 unsigned int debounce, bool *gpio_invert)
Linus Walleij9d2fa242014-08-27 13:00:51 +0200303{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100304 struct mmc_gpio *ctx = host->slot.handler_priv;
Linus Walleij9d2fa242014-08-27 13:00:51 +0200305 struct gpio_desc *desc;
306 int ret;
307
Linus Walleij9d2fa242014-08-27 13:00:51 +0200308 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
309 if (IS_ERR(desc))
310 return PTR_ERR(desc);
311
312 if (debounce) {
313 ret = gpiod_set_debounce(desc, debounce);
314 if (ret < 0)
315 return ret;
316 }
317
Linus Walleij89168b42014-10-02 09:08:46 +0200318 if (gpio_invert)
319 *gpio_invert = !gpiod_is_active_low(desc);
320
Linus Walleij9d2fa242014-08-27 13:00:51 +0200321 ctx->override_ro_active_level = override_active_level;
322 ctx->ro_gpio = desc;
323
324 return 0;
325}
326EXPORT_SYMBOL(mmc_gpiod_request_ro);
Masahiro Yamada85f9ef82018-01-18 01:28:05 +0900327
328bool mmc_can_gpio_ro(struct mmc_host *host)
329{
330 struct mmc_gpio *ctx = host->slot.handler_priv;
331
332 return ctx->ro_gpio ? true : false;
333}
334EXPORT_SYMBOL(mmc_can_gpio_ro);