blob: 1a3edbd47719e0b94fa90b2dbce144a4322520b9 [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;
Ulf Hanssondf8aca12014-12-18 15:44:36 +010046 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
47 sizeof(*ctx) + 2 * len, GFP_KERNEL);
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020048
Ulf Hanssondf8aca12014-12-18 15:44:36 +010049 if (ctx) {
50 ctx->ro_label = ctx->cd_label + len;
51 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
52 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
53 host->slot.handler_priv = ctx;
54 host->slot.cd_irq = -EINVAL;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020055 }
56
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020057 return ctx ? 0 : -ENOMEM;
58}
59
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020060int mmc_gpio_get_ro(struct mmc_host *host)
61{
62 struct mmc_gpio *ctx = host->slot.handler_priv;
63
Adrian Hunter842f4bd2014-03-10 15:02:39 +020064 if (!ctx || !ctx->ro_gpio)
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020065 return -ENOSYS;
66
Adrian Hunter842f4bd2014-03-10 15:02:39 +020067 if (ctx->override_ro_active_level)
68 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
69 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
70
71 return gpiod_get_value_cansleep(ctx->ro_gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020072}
73EXPORT_SYMBOL(mmc_gpio_get_ro);
74
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020075int mmc_gpio_get_cd(struct mmc_host *host)
76{
77 struct mmc_gpio *ctx = host->slot.handler_priv;
78
Adrian Hunter842f4bd2014-03-10 15:02:39 +020079 if (!ctx || !ctx->cd_gpio)
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020080 return -ENOSYS;
81
Adrian Hunter842f4bd2014-03-10 15:02:39 +020082 if (ctx->override_cd_active_level)
83 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
84 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
85
86 return gpiod_get_value_cansleep(ctx->cd_gpio);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020087}
88EXPORT_SYMBOL(mmc_gpio_get_cd);
89
Shawn Guod65b5ae2012-12-11 22:32:18 +080090/**
91 * mmc_gpio_request_ro - request a gpio for write-protection
92 * @host: mmc host
93 * @gpio: gpio number requested
94 *
95 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +010096 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +080097 *
98 * Returns zero on success, else an error.
99 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200100int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
101{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100102 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200103 int ret;
104
105 if (!gpio_is_valid(gpio))
106 return -EINVAL;
107
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100108 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800109 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400110 if (ret < 0)
111 return ret;
112
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200113 ctx->override_ro_active_level = true;
114 ctx->ro_gpio = gpio_to_desc(gpio);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400115
116 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200117}
118EXPORT_SYMBOL(mmc_gpio_request_ro);
119
Adrian Hunter740a2212014-03-10 15:02:41 +0200120void mmc_gpiod_request_cd_irq(struct mmc_host *host)
Adrian Hunter26652672014-03-10 15:02:40 +0200121{
122 struct mmc_gpio *ctx = host->slot.handler_priv;
123 int ret, irq;
124
125 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
126 return;
127
128 irq = gpiod_to_irq(ctx->cd_gpio);
129
130 /*
131 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
132 * still prefer to poll, e.g., because that IRQ number is already used
133 * by another unit and cannot be shared.
134 */
135 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
136 irq = -EINVAL;
137
138 if (irq >= 0) {
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100139 ret = devm_request_threaded_irq(host->parent, irq,
Adrian Hunter26652672014-03-10 15:02:40 +0200140 NULL, mmc_gpio_cd_irqt,
141 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
142 ctx->cd_label, host);
143 if (ret < 0)
144 irq = ret;
145 }
146
147 host->slot.cd_irq = irq;
148
149 if (irq < 0)
150 host->caps |= MMC_CAP_NEEDS_POLL;
151}
Adrian Hunter740a2212014-03-10 15:02:41 +0200152EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
Adrian Hunter26652672014-03-10 15:02:40 +0200153
Shawn Guod65b5ae2012-12-11 22:32:18 +0800154/**
155 * mmc_gpio_request_cd - request a gpio for card-detection
156 * @host: mmc host
157 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200158 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800159 *
160 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100161 * drivers do not need to worry about freeing up memory.
Shawn Guod65b5ae2012-12-11 22:32:18 +0800162 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200163 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
164 * value. The caller is responsible for ensuring that the GPIO driver associated
165 * with the GPIO supports debouncing, otherwise an error will be returned.
166 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800167 * Returns zero on success, else an error.
168 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200169int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
170 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200171{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100172 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200173 int ret;
174
Ulf Hanssonb4cc5802014-12-18 15:44:33 +0100175 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
Shawn Guod65b5ae2012-12-11 22:32:18 +0800176 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200177 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200178 /*
179 * don't bother freeing memory. It might still get used by other
180 * slot functions, in any case it will be freed, when the device
181 * is destroyed.
182 */
183 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200184
Laurent Pinchart214fc302013-08-08 12:38:31 +0200185 if (debounce) {
186 ret = gpio_set_debounce(gpio, debounce);
187 if (ret < 0)
188 return ret;
189 }
190
Adrian Hunter842f4bd2014-03-10 15:02:39 +0200191 ctx->override_cd_active_level = true;
192 ctx->cd_gpio = gpio_to_desc(gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200193
194 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200195}
196EXPORT_SYMBOL(mmc_gpio_request_cd);
197
Shawn Guod65b5ae2012-12-11 22:32:18 +0800198/**
Adrian Hunter740a2212014-03-10 15:02:41 +0200199 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
200 * @host: mmc host
201 * @con_id: function within the GPIO consumer
202 * @idx: index of the GPIO to obtain in the consumer
203 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
204 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200205 * @gpio_invert: will return whether the GPIO line is inverted or not, set
206 * to NULL to ignore
Adrian Hunter740a2212014-03-10 15:02:41 +0200207 *
208 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100209 * descriptor API. Note that it must be called prior to mmc_add_host()
Adrian Hunter740a2212014-03-10 15:02:41 +0200210 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
211 *
212 * Returns zero on success, else an error.
213 */
214int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
215 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200216 unsigned int debounce, bool *gpio_invert)
Adrian Hunter740a2212014-03-10 15:02:41 +0200217{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100218 struct mmc_gpio *ctx = host->slot.handler_priv;
Adrian Hunter740a2212014-03-10 15:02:41 +0200219 struct gpio_desc *desc;
220 int ret;
221
Adrian Hunter740a2212014-03-10 15:02:41 +0200222 if (!con_id)
223 con_id = ctx->cd_label;
224
Linus Walleij9fbc6952014-08-27 13:00:50 +0200225 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
Adrian Hunter740a2212014-03-10 15:02:41 +0200226 if (IS_ERR(desc))
227 return PTR_ERR(desc);
228
Adrian Hunter740a2212014-03-10 15:02:41 +0200229 if (debounce) {
230 ret = gpiod_set_debounce(desc, debounce);
231 if (ret < 0)
232 return ret;
233 }
234
Linus Walleij89168b42014-10-02 09:08:46 +0200235 if (gpio_invert)
236 *gpio_invert = !gpiod_is_active_low(desc);
237
Adrian Hunter740a2212014-03-10 15:02:41 +0200238 ctx->override_cd_active_level = override_active_level;
239 ctx->cd_gpio = desc;
240
241 return 0;
242}
243EXPORT_SYMBOL(mmc_gpiod_request_cd);
244
245/**
Linus Walleij9d2fa242014-08-27 13:00:51 +0200246 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
247 * @host: mmc host
248 * @con_id: function within the GPIO consumer
249 * @idx: index of the GPIO to obtain in the consumer
250 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
251 * @debounce: debounce time in microseconds
Linus Walleij89168b42014-10-02 09:08:46 +0200252 * @gpio_invert: will return whether the GPIO line is inverted or not,
253 * set to NULL to ignore
Linus Walleij9d2fa242014-08-27 13:00:51 +0200254 *
255 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
Ulf Hanssoneddbc3a2014-12-18 15:44:32 +0100256 * descriptor API.
Linus Walleij9d2fa242014-08-27 13:00:51 +0200257 *
258 * Returns zero on success, else an error.
259 */
260int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
261 unsigned int idx, bool override_active_level,
Linus Walleij89168b42014-10-02 09:08:46 +0200262 unsigned int debounce, bool *gpio_invert)
Linus Walleij9d2fa242014-08-27 13:00:51 +0200263{
Ulf Hanssondf8aca12014-12-18 15:44:36 +0100264 struct mmc_gpio *ctx = host->slot.handler_priv;
Linus Walleij9d2fa242014-08-27 13:00:51 +0200265 struct gpio_desc *desc;
266 int ret;
267
Linus Walleij9d2fa242014-08-27 13:00:51 +0200268 if (!con_id)
269 con_id = ctx->ro_label;
270
271 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
272 if (IS_ERR(desc))
273 return PTR_ERR(desc);
274
275 if (debounce) {
276 ret = gpiod_set_debounce(desc, debounce);
277 if (ret < 0)
278 return ret;
279 }
280
Linus Walleij89168b42014-10-02 09:08:46 +0200281 if (gpio_invert)
282 *gpio_invert = !gpiod_is_active_low(desc);
283
Linus Walleij9d2fa242014-08-27 13:00:51 +0200284 ctx->override_ro_active_level = override_active_level;
285 ctx->ro_gpio = desc;
286
287 return 0;
288}
289EXPORT_SYMBOL(mmc_gpiod_request_ro);