PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / mmc / core / slot-gpio.c
blob46596b71a32f49d5934c62d3f5323b26cf6c1658
1 /*
2 * Generic GPIO card-detect helper
4 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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 */
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>
20 struct mmc_gpio {
21 int ro_gpio;
22 int cd_gpio;
23 char *ro_label;
24 char cd_label[0];
27 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
29 /* Schedule a card detection after a debounce timeout */
30 struct mmc_host *host = dev_id;
32 if (host->ops->card_event)
33 host->ops->card_event(host);
35 mmc_detect_change(host, msecs_to_jiffies(200));
37 return IRQ_HANDLED;
40 static int mmc_gpio_alloc(struct mmc_host *host)
42 size_t len = strlen(dev_name(host->parent)) + 4;
43 struct mmc_gpio *ctx;
45 mutex_lock(&host->slot.lock);
47 ctx = host->slot.handler_priv;
48 if (!ctx) {
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()
54 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
55 GFP_KERNEL);
56 if (ctx) {
57 ctx->ro_label = ctx->cd_label + len;
58 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
59 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
60 ctx->cd_gpio = -EINVAL;
61 ctx->ro_gpio = -EINVAL;
62 host->slot.handler_priv = ctx;
66 mutex_unlock(&host->slot.lock);
68 return ctx ? 0 : -ENOMEM;
71 int mmc_gpio_get_ro(struct mmc_host *host)
73 struct mmc_gpio *ctx = host->slot.handler_priv;
75 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
76 return -ENOSYS;
78 return !gpio_get_value_cansleep(ctx->ro_gpio) ^
79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
81 EXPORT_SYMBOL(mmc_gpio_get_ro);
83 int mmc_gpio_get_cd(struct mmc_host *host)
85 struct mmc_gpio *ctx = host->slot.handler_priv;
87 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
88 return -ENOSYS;
90 return !gpio_get_value_cansleep(ctx->cd_gpio) ^
91 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
93 EXPORT_SYMBOL(mmc_gpio_get_cd);
95 /**
96 * mmc_gpio_request_ro - request a gpio for write-protection
97 * @host: mmc host
98 * @gpio: gpio number requested
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.
107 * Returns zero on success, else an error.
109 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
111 struct mmc_gpio *ctx;
112 int ret;
114 if (!gpio_is_valid(gpio))
115 return -EINVAL;
117 ret = mmc_gpio_alloc(host);
118 if (ret < 0)
119 return ret;
121 ctx = host->slot.handler_priv;
123 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
124 ctx->ro_label);
125 if (ret < 0)
126 return ret;
128 ctx->ro_gpio = gpio;
130 return 0;
132 EXPORT_SYMBOL(mmc_gpio_request_ro);
135 * mmc_gpio_request_cd - request a gpio for card-detection
136 * @host: mmc host
137 * @gpio: gpio number requested
138 * @debounce: debounce time in microseconds
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.
147 * 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.
151 * Returns zero on success, else an error.
153 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
154 unsigned int debounce)
156 struct mmc_gpio *ctx;
157 int irq = gpio_to_irq(gpio);
158 int ret;
160 ret = mmc_gpio_alloc(host);
161 if (ret < 0)
162 return ret;
164 ctx = host->slot.handler_priv;
166 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
167 ctx->cd_label);
168 if (ret < 0)
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.
174 return ret;
176 if (debounce) {
177 ret = gpio_set_debounce(gpio, debounce);
178 if (ret < 0)
179 return ret;
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.
187 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
188 irq = -EINVAL;
190 if (irq >= 0) {
191 ret = devm_request_threaded_irq(&host->class_dev, irq,
192 NULL, mmc_gpio_cd_irqt,
193 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
194 ctx->cd_label, host);
195 if (ret < 0)
196 irq = ret;
199 host->slot.cd_irq = irq;
201 if (irq < 0)
202 host->caps |= MMC_CAP_NEEDS_POLL;
204 ctx->cd_gpio = gpio;
206 return 0;
208 EXPORT_SYMBOL(mmc_gpio_request_cd);
211 * mmc_gpio_free_ro - free the write-protection gpio
212 * @host: mmc host
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().
217 void mmc_gpio_free_ro(struct mmc_host *host)
219 struct mmc_gpio *ctx = host->slot.handler_priv;
220 int gpio;
222 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
223 return;
225 gpio = ctx->ro_gpio;
226 ctx->ro_gpio = -EINVAL;
228 devm_gpio_free(&host->class_dev, gpio);
230 EXPORT_SYMBOL(mmc_gpio_free_ro);
233 * mmc_gpio_free_cd - free the card-detection gpio
234 * @host: mmc host
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().
239 void mmc_gpio_free_cd(struct mmc_host *host)
241 struct mmc_gpio *ctx = host->slot.handler_priv;
242 int gpio;
244 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
245 return;
247 if (host->slot.cd_irq >= 0) {
248 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
249 host->slot.cd_irq = -EINVAL;
252 gpio = ctx->cd_gpio;
253 ctx->cd_gpio = -EINVAL;
255 devm_gpio_free(&host->class_dev, gpio);
257 EXPORT_SYMBOL(mmc_gpio_free_cd);