1 /*
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/gpio/consumer.h>
14 #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
21 #include "slot-gpio.h"
22
23 struct mmc_gpio {
24 struct gpio_desc *ro_gpio;
25 struct gpio_desc *cd_gpio;
26 bool override_ro_active_level;
27 bool override_cd_active_level;
28 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
29 char *ro_label;
30 char cd_label[0];
31 };
32
mmc_gpio_cd_irqt(int irq,void * dev_id)33 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
34 {
35 /* Schedule a card detection after a debounce timeout */
36 struct mmc_host *host = dev_id;
37
38 host->trigger_card_event = true;
39 mmc_detect_change(host, msecs_to_jiffies(200));
40
41 return IRQ_HANDLED;
42 }
43
mmc_gpio_alloc(struct mmc_host * host)44 int mmc_gpio_alloc(struct mmc_host *host)
45 {
46 size_t len = strlen(dev_name(host->parent)) + 4;
47 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
48 sizeof(*ctx) + 2 * len, GFP_KERNEL);
49
50 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;
56 }
57
58 return ctx ? 0 : -ENOMEM;
59 }
60
mmc_gpio_get_ro(struct mmc_host * host)61 int mmc_gpio_get_ro(struct mmc_host *host)
62 {
63 struct mmc_gpio *ctx = host->slot.handler_priv;
64
65 if (!ctx || !ctx->ro_gpio)
66 return -ENOSYS;
67
68 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);
73 }
74 EXPORT_SYMBOL(mmc_gpio_get_ro);
75
mmc_gpio_get_cd(struct mmc_host * host)76 int mmc_gpio_get_cd(struct mmc_host *host)
77 {
78 struct mmc_gpio *ctx = host->slot.handler_priv;
79
80 if (!ctx || !ctx->cd_gpio)
81 return -ENOSYS;
82
83 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);
88 }
89 EXPORT_SYMBOL(mmc_gpio_get_cd);
90
91 /**
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
97 * drivers do not need to worry about freeing up memory.
98 *
99 * Returns zero on success, else an error.
100 */
mmc_gpio_request_ro(struct mmc_host * host,unsigned int gpio)101 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
102 {
103 struct mmc_gpio *ctx = host->slot.handler_priv;
104 int ret;
105
106 if (!gpio_is_valid(gpio))
107 return -EINVAL;
108
109 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
110 ctx->ro_label);
111 if (ret < 0)
112 return ret;
113
114 ctx->override_ro_active_level = true;
115 ctx->ro_gpio = gpio_to_desc(gpio);
116
117 return 0;
118 }
119 EXPORT_SYMBOL(mmc_gpio_request_ro);
120
mmc_gpiod_request_cd_irq(struct mmc_host * host)121 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
122 {
123 struct mmc_gpio *ctx = host->slot.handler_priv;
124 int ret, irq;
125
126 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
127 return;
128
129 irq = gpiod_to_irq(ctx->cd_gpio);
130
131 /*
132 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
133 * still prefer to poll, e.g., because that IRQ number is already used
134 * by another unit and cannot be shared.
135 */
136 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
137 irq = -EINVAL;
138
139 if (irq >= 0) {
140 if (!ctx->cd_gpio_isr)
141 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
142 ret = devm_request_threaded_irq(host->parent, irq,
143 NULL, ctx->cd_gpio_isr,
144 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
145 ctx->cd_label, host);
146 if (ret < 0)
147 irq = ret;
148 }
149
150 host->slot.cd_irq = irq;
151
152 if (irq < 0)
153 host->caps |= MMC_CAP_NEEDS_POLL;
154 else if ((host->caps & MMC_CAP_CD_WAKE) && !enable_irq_wake(irq))
155 host->slot.cd_wake_enabled = true;
156 }
157 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
158
159 /* Register an alternate interrupt service routine for
160 * the card-detect GPIO.
161 */
mmc_gpio_set_cd_isr(struct mmc_host * host,irqreturn_t (* isr)(int irq,void * dev_id))162 void mmc_gpio_set_cd_isr(struct mmc_host *host,
163 irqreturn_t (*isr)(int irq, void *dev_id))
164 {
165 struct mmc_gpio *ctx = host->slot.handler_priv;
166
167 WARN_ON(ctx->cd_gpio_isr);
168 ctx->cd_gpio_isr = isr;
169 }
170 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
171
172 /**
173 * mmc_gpio_request_cd - request a gpio for card-detection
174 * @host: mmc host
175 * @gpio: gpio number requested
176 * @debounce: debounce time in microseconds
177 *
178 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
179 * drivers do not need to worry about freeing up memory.
180 *
181 * 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 *
185 * Returns zero on success, else an error.
186 */
mmc_gpio_request_cd(struct mmc_host * host,unsigned int gpio,unsigned int debounce)187 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
188 unsigned int debounce)
189 {
190 struct mmc_gpio *ctx = host->slot.handler_priv;
191 int ret;
192
193 ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
194 ctx->cd_label);
195 if (ret < 0)
196 /*
197 * don't bother freeing memory. It might still get used by other
198 * slot functions, in any case it will be freed, when the device
199 * is destroyed.
200 */
201 return ret;
202
203 if (debounce) {
204 ret = gpio_set_debounce(gpio, debounce);
205 if (ret < 0)
206 return ret;
207 }
208
209 ctx->override_cd_active_level = true;
210 ctx->cd_gpio = gpio_to_desc(gpio);
211
212 return 0;
213 }
214 EXPORT_SYMBOL(mmc_gpio_request_cd);
215
216 /**
217 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
218 * @host: mmc host
219 * @con_id: function within the GPIO consumer
220 * @idx: index of the GPIO to obtain in the consumer
221 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
222 * @debounce: debounce time in microseconds
223 * @gpio_invert: will return whether the GPIO line is inverted or not, set
224 * to NULL to ignore
225 *
226 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
227 * descriptor API. Note that it must be called prior to mmc_add_host()
228 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
229 *
230 * Returns zero on success, else an error.
231 */
mmc_gpiod_request_cd(struct mmc_host * host,const char * con_id,unsigned int idx,bool override_active_level,unsigned int debounce,bool * gpio_invert)232 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
233 unsigned int idx, bool override_active_level,
234 unsigned int debounce, bool *gpio_invert)
235 {
236 struct mmc_gpio *ctx = host->slot.handler_priv;
237 struct gpio_desc *desc;
238 int ret;
239
240 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
241 if (IS_ERR(desc))
242 return PTR_ERR(desc);
243
244 if (debounce) {
245 ret = gpiod_set_debounce(desc, debounce);
246 if (ret < 0)
247 return ret;
248 }
249
250 if (gpio_invert)
251 *gpio_invert = !gpiod_is_active_low(desc);
252
253 ctx->override_cd_active_level = override_active_level;
254 ctx->cd_gpio = desc;
255
256 return 0;
257 }
258 EXPORT_SYMBOL(mmc_gpiod_request_cd);
259
mmc_can_gpio_cd(struct mmc_host * host)260 bool mmc_can_gpio_cd(struct mmc_host *host)
261 {
262 struct mmc_gpio *ctx = host->slot.handler_priv;
263
264 return ctx->cd_gpio ? true : false;
265 }
266 EXPORT_SYMBOL(mmc_can_gpio_cd);
267
268 /**
269 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
270 * @host: mmc host
271 * @con_id: function within the GPIO consumer
272 * @idx: index of the GPIO to obtain in the consumer
273 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
274 * @debounce: debounce time in microseconds
275 * @gpio_invert: will return whether the GPIO line is inverted or not,
276 * set to NULL to ignore
277 *
278 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
279 * descriptor API.
280 *
281 * Returns zero on success, else an error.
282 */
mmc_gpiod_request_ro(struct mmc_host * host,const char * con_id,unsigned int idx,bool override_active_level,unsigned int debounce,bool * gpio_invert)283 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
284 unsigned int idx, bool override_active_level,
285 unsigned int debounce, bool *gpio_invert)
286 {
287 struct mmc_gpio *ctx = host->slot.handler_priv;
288 struct gpio_desc *desc;
289 int ret;
290
291 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
292 if (IS_ERR(desc))
293 return PTR_ERR(desc);
294
295 if (debounce) {
296 ret = gpiod_set_debounce(desc, debounce);
297 if (ret < 0)
298 return ret;
299 }
300
301 if (gpio_invert)
302 *gpio_invert = !gpiod_is_active_low(desc);
303
304 ctx->override_ro_active_level = override_active_level;
305 ctx->ro_gpio = desc;
306
307 return 0;
308 }
309 EXPORT_SYMBOL(mmc_gpiod_request_ro);
310