]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/mmc/core/slot-gpio.c
mmc: slot-gpio: Add a function to enable/disable card detect IRQ wakeup
[linux.git] / drivers / mmc / core / slot-gpio.c
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
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
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
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
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  */
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
121 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
122 {
123         struct mmc_gpio *ctx = host->slot.handler_priv;
124         int irq = -EINVAL;
125         int ret;
126
127         if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
128                 return;
129
130         /*
131          * 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.
133          */
134         if (!(host->caps & MMC_CAP_NEEDS_POLL))
135                 irq = gpiod_to_irq(ctx->cd_gpio);
136
137         if (irq >= 0) {
138                 if (!ctx->cd_gpio_isr)
139                         ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
140                 ret = devm_request_threaded_irq(host->parent, irq,
141                         NULL, ctx->cd_gpio_isr,
142                         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         else if ((host->caps & MMC_CAP_CD_WAKE) && !enable_irq_wake(irq))
153                 host->slot.cd_wake_enabled = true;
154 }
155 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
156
157 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
158 {
159         int ret = 0;
160
161         if (!(host->caps & MMC_CAP_CD_WAKE) ||
162             host->slot.cd_irq < 0 ||
163             on == host->slot.cd_wake_enabled)
164                 return 0;
165
166         if (on) {
167                 ret = enable_irq_wake(host->slot.cd_irq);
168                 host->slot.cd_wake_enabled = !ret;
169         } else {
170                 disable_irq_wake(host->slot.cd_irq);
171                 host->slot.cd_wake_enabled = false;
172         }
173
174         return ret;
175 }
176 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
177
178 /* Register an alternate interrupt service routine for
179  * the card-detect GPIO.
180  */
181 void mmc_gpio_set_cd_isr(struct mmc_host *host,
182                          irqreturn_t (*isr)(int irq, void *dev_id))
183 {
184         struct mmc_gpio *ctx = host->slot.handler_priv;
185
186         WARN_ON(ctx->cd_gpio_isr);
187         ctx->cd_gpio_isr = isr;
188 }
189 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
190
191 /**
192  * mmc_gpio_request_cd - request a gpio for card-detection
193  * @host: mmc host
194  * @gpio: gpio number requested
195  * @debounce: debounce time in microseconds
196  *
197  * As devm_* managed functions are used in mmc_gpio_request_cd(), client
198  * drivers do not need to worry about freeing up memory.
199  *
200  * If GPIO debouncing is desired, set the debounce parameter to a non-zero
201  * value. The caller is responsible for ensuring that the GPIO driver associated
202  * with the GPIO supports debouncing, otherwise an error will be returned.
203  *
204  * Returns zero on success, else an error.
205  */
206 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
207                         unsigned int debounce)
208 {
209         struct mmc_gpio *ctx = host->slot.handler_priv;
210         int ret;
211
212         ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
213                                     ctx->cd_label);
214         if (ret < 0)
215                 /*
216                  * don't bother freeing memory. It might still get used by other
217                  * slot functions, in any case it will be freed, when the device
218                  * is destroyed.
219                  */
220                 return ret;
221
222         if (debounce) {
223                 ret = gpio_set_debounce(gpio, debounce);
224                 if (ret < 0)
225                         return ret;
226         }
227
228         ctx->override_cd_active_level = true;
229         ctx->cd_gpio = gpio_to_desc(gpio);
230
231         return 0;
232 }
233 EXPORT_SYMBOL(mmc_gpio_request_cd);
234
235 /**
236  * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
237  * @host: mmc host
238  * @con_id: function within the GPIO consumer
239  * @idx: index of the GPIO to obtain in the consumer
240  * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
241  * @debounce: debounce time in microseconds
242  * @gpio_invert: will return whether the GPIO line is inverted or not, set
243  * to NULL to ignore
244  *
245  * Use this function in place of mmc_gpio_request_cd() to use the GPIO
246  * descriptor API.  Note that it must be called prior to mmc_add_host()
247  * otherwise the caller must also call mmc_gpiod_request_cd_irq().
248  *
249  * Returns zero on success, else an error.
250  */
251 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
252                          unsigned int idx, bool override_active_level,
253                          unsigned int debounce, bool *gpio_invert)
254 {
255         struct mmc_gpio *ctx = host->slot.handler_priv;
256         struct gpio_desc *desc;
257         int ret;
258
259         desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
260         if (IS_ERR(desc))
261                 return PTR_ERR(desc);
262
263         if (debounce) {
264                 ret = gpiod_set_debounce(desc, debounce);
265                 if (ret < 0)
266                         return ret;
267         }
268
269         if (gpio_invert)
270                 *gpio_invert = !gpiod_is_active_low(desc);
271
272         ctx->override_cd_active_level = override_active_level;
273         ctx->cd_gpio = desc;
274
275         return 0;
276 }
277 EXPORT_SYMBOL(mmc_gpiod_request_cd);
278
279 bool mmc_can_gpio_cd(struct mmc_host *host)
280 {
281         struct mmc_gpio *ctx = host->slot.handler_priv;
282
283         return ctx->cd_gpio ? true : false;
284 }
285 EXPORT_SYMBOL(mmc_can_gpio_cd);
286
287 /**
288  * mmc_gpiod_request_ro - request a gpio descriptor for write protection
289  * @host: mmc host
290  * @con_id: function within the GPIO consumer
291  * @idx: index of the GPIO to obtain in the consumer
292  * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
293  * @debounce: debounce time in microseconds
294  * @gpio_invert: will return whether the GPIO line is inverted or not,
295  * set to NULL to ignore
296  *
297  * Use this function in place of mmc_gpio_request_ro() to use the GPIO
298  * descriptor API.
299  *
300  * Returns zero on success, else an error.
301  */
302 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
303                          unsigned int idx, bool override_active_level,
304                          unsigned int debounce, bool *gpio_invert)
305 {
306         struct mmc_gpio *ctx = host->slot.handler_priv;
307         struct gpio_desc *desc;
308         int ret;
309
310         desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
311         if (IS_ERR(desc))
312                 return PTR_ERR(desc);
313
314         if (debounce) {
315                 ret = gpiod_set_debounce(desc, debounce);
316                 if (ret < 0)
317                         return ret;
318         }
319
320         if (gpio_invert)
321                 *gpio_invert = !gpiod_is_active_low(desc);
322
323         ctx->override_ro_active_level = override_active_level;
324         ctx->ro_gpio = desc;
325
326         return 0;
327 }
328 EXPORT_SYMBOL(mmc_gpiod_request_ro);
329
330 bool mmc_can_gpio_ro(struct mmc_host *host)
331 {
332         struct mmc_gpio *ctx = host->slot.handler_priv;
333
334         return ctx->ro_gpio ? true : false;
335 }
336 EXPORT_SYMBOL(mmc_can_gpio_ro);