blob: 46596b71a32f49d5934c62d3f5323b26cf6c1658 [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>
13#include <linux/interrupt.h>
14#include <linux/jiffies.h>
15#include <linux/mmc/host.h>
16#include <linux/mmc/slot-gpio.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19
20struct mmc_gpio {
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020021 int ro_gpio;
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020022 int cd_gpio;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020023 char *ro_label;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020024 char cd_label[0];
25};
26
27static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
28{
29 /* Schedule a card detection after a debounce timeout */
Guennadi Liakhovetski451c8952012-12-04 16:51:36 +010030 struct mmc_host *host = dev_id;
31
32 if (host->ops->card_event)
33 host->ops->card_event(host);
34
35 mmc_detect_change(host, msecs_to_jiffies(200));
36
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +020037 return IRQ_HANDLED;
38}
39
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020040static int mmc_gpio_alloc(struct mmc_host *host)
41{
42 size_t len = strlen(dev_name(host->parent)) + 4;
43 struct mmc_gpio *ctx;
44
45 mutex_lock(&host->slot.lock);
46
47 ctx = host->slot.handler_priv;
48 if (!ctx) {
49 /*
50 * devm_kzalloc() can be called after device_initialize(), even
51 * before device_add(), i.e., between mmc_alloc_host() and
52 * mmc_add_host()
53 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020054 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020055 GFP_KERNEL);
56 if (ctx) {
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020057 ctx->ro_label = ctx->cd_label + len;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020058 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020059 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020060 ctx->cd_gpio = -EINVAL;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020061 ctx->ro_gpio = -EINVAL;
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +020062 host->slot.handler_priv = ctx;
63 }
64 }
65
66 mutex_unlock(&host->slot.lock);
67
68 return ctx ? 0 : -ENOMEM;
69}
70
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +020071int mmc_gpio_get_ro(struct mmc_host *host)
72{
73 struct mmc_gpio *ctx = host->slot.handler_priv;
74
75 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
76 return -ENOSYS;
77
78 return !gpio_get_value_cansleep(ctx->ro_gpio) ^
79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
80}
81EXPORT_SYMBOL(mmc_gpio_get_ro);
82
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +020083int mmc_gpio_get_cd(struct mmc_host *host)
84{
85 struct mmc_gpio *ctx = host->slot.handler_priv;
86
87 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
88 return -ENOSYS;
89
90 return !gpio_get_value_cansleep(ctx->cd_gpio) ^
91 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
92}
93EXPORT_SYMBOL(mmc_gpio_get_cd);
94
Shawn Guod65b5ae2012-12-11 22:32:18 +080095/**
96 * mmc_gpio_request_ro - request a gpio for write-protection
97 * @host: mmc host
98 * @gpio: gpio number requested
99 *
100 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
101 * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
102 * if the requesting and freeing are only needed at probing and unbinding time
103 * for once. However, if client drivers do something special like runtime
104 * switching for write-protection, they are responsible for calling
105 * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
106 *
107 * Returns zero on success, else an error.
108 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200109int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
110{
111 struct mmc_gpio *ctx;
112 int ret;
113
114 if (!gpio_is_valid(gpio))
115 return -EINVAL;
116
117 ret = mmc_gpio_alloc(host);
118 if (ret < 0)
119 return ret;
120
121 ctx = host->slot.handler_priv;
122
Shawn Guod65b5ae2012-12-11 22:32:18 +0800123 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
124 ctx->ro_label);
Chris Ball15e8a8e2012-09-09 22:56:48 -0400125 if (ret < 0)
126 return ret;
127
128 ctx->ro_gpio = gpio;
129
130 return 0;
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200131}
132EXPORT_SYMBOL(mmc_gpio_request_ro);
133
Shawn Guod65b5ae2012-12-11 22:32:18 +0800134/**
135 * mmc_gpio_request_cd - request a gpio for card-detection
136 * @host: mmc host
137 * @gpio: gpio number requested
Laurent Pinchart214fc302013-08-08 12:38:31 +0200138 * @debounce: debounce time in microseconds
Shawn Guod65b5ae2012-12-11 22:32:18 +0800139 *
140 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
141 * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
142 * if the requesting and freeing are only needed at probing and unbinding time
143 * for once. However, if client drivers do something special like runtime
144 * switching for card-detection, they are responsible for calling
145 * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
146 *
Laurent Pinchart214fc302013-08-08 12:38:31 +0200147 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
148 * value. The caller is responsible for ensuring that the GPIO driver associated
149 * with the GPIO supports debouncing, otherwise an error will be returned.
150 *
Shawn Guod65b5ae2012-12-11 22:32:18 +0800151 * Returns zero on success, else an error.
152 */
Laurent Pinchart214fc302013-08-08 12:38:31 +0200153int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
154 unsigned int debounce)
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200155{
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200156 struct mmc_gpio *ctx;
157 int irq = gpio_to_irq(gpio);
158 int ret;
159
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200160 ret = mmc_gpio_alloc(host);
161 if (ret < 0)
162 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200163
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200164 ctx = host->slot.handler_priv;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200165
Shawn Guod65b5ae2012-12-11 22:32:18 +0800166 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
167 ctx->cd_label);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200168 if (ret < 0)
Guennadi Liakhovetskia7d1a1e2012-05-01 16:51:38 +0200169 /*
170 * don't bother freeing memory. It might still get used by other
171 * slot functions, in any case it will be freed, when the device
172 * is destroyed.
173 */
174 return ret;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200175
Laurent Pinchart214fc302013-08-08 12:38:31 +0200176 if (debounce) {
177 ret = gpio_set_debounce(gpio, debounce);
178 if (ret < 0)
179 return ret;
180 }
181
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200182 /*
183 * Even if gpio_to_irq() returns a valid IRQ number, the platform might
184 * still prefer to poll, e.g., because that IRQ number is already used
185 * by another unit and cannot be shared.
186 */
187 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
188 irq = -EINVAL;
189
190 if (irq >= 0) {
Shawn Guod65b5ae2012-12-11 22:32:18 +0800191 ret = devm_request_threaded_irq(&host->class_dev, irq,
192 NULL, mmc_gpio_cd_irqt,
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200193 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
194 ctx->cd_label, host);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200195 if (ret < 0)
196 irq = ret;
197 }
198
199 host->slot.cd_irq = irq;
200
201 if (irq < 0)
202 host->caps |= MMC_CAP_NEEDS_POLL;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200203
204 ctx->cd_gpio = gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200205
206 return 0;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200207}
208EXPORT_SYMBOL(mmc_gpio_request_cd);
209
Shawn Guod65b5ae2012-12-11 22:32:18 +0800210/**
211 * mmc_gpio_free_ro - free the write-protection gpio
212 * @host: mmc host
213 *
214 * It's provided only for cases that client drivers need to manually free
215 * up the write-protection gpio requested by mmc_gpio_request_ro().
216 */
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200217void mmc_gpio_free_ro(struct mmc_host *host)
218{
219 struct mmc_gpio *ctx = host->slot.handler_priv;
220 int gpio;
221
222 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
223 return;
224
225 gpio = ctx->ro_gpio;
226 ctx->ro_gpio = -EINVAL;
227
Shawn Guod65b5ae2012-12-11 22:32:18 +0800228 devm_gpio_free(&host->class_dev, gpio);
Guennadi Liakhovetski5aa7dad2012-05-01 16:59:38 +0200229}
230EXPORT_SYMBOL(mmc_gpio_free_ro);
231
Shawn Guod65b5ae2012-12-11 22:32:18 +0800232/**
233 * mmc_gpio_free_cd - free the card-detection gpio
234 * @host: mmc host
235 *
236 * It's provided only for cases that client drivers need to manually free
237 * up the card-detection gpio requested by mmc_gpio_request_cd().
238 */
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200239void mmc_gpio_free_cd(struct mmc_host *host)
240{
Guennadi Liakhovetski27410ee2012-05-01 15:40:15 +0200241 struct mmc_gpio *ctx = host->slot.handler_priv;
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200242 int gpio;
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200243
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200244 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200245 return;
246
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200247 if (host->slot.cd_irq >= 0) {
Shawn Guod65b5ae2012-12-11 22:32:18 +0800248 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
Guennadi Liakhovetskibefe4042012-05-01 16:27:25 +0200249 host->slot.cd_irq = -EINVAL;
250 }
251
252 gpio = ctx->cd_gpio;
253 ctx->cd_gpio = -EINVAL;
254
Shawn Guod65b5ae2012-12-11 22:32:18 +0800255 devm_gpio_free(&host->class_dev, gpio);
Guennadi Liakhovetskifd0ea652012-04-30 23:31:57 +0200256}
257EXPORT_SYMBOL(mmc_gpio_free_cd);