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.
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>
22 struct gpio_desc *ro_gpio;
23 struct gpio_desc *cd_gpio;
24 bool override_ro_active_level;
25 bool override_cd_active_level;
30 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
32 /* Schedule a card detection after a debounce timeout */
33 struct mmc_host *host = dev_id;
35 if (host->ops->card_event)
36 host->ops->card_event(host);
38 mmc_detect_change(host, msecs_to_jiffies(200));
43 static int mmc_gpio_alloc(struct mmc_host *host)
45 size_t len = strlen(dev_name(host->parent)) + 4;
48 mutex_lock(&host->slot.lock);
50 ctx = host->slot.handler_priv;
53 * devm_kzalloc() can be called after device_initialize(), even
54 * before device_add(), i.e., between mmc_alloc_host() and
57 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
60 ctx->ro_label = ctx->cd_label + len;
61 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
62 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
63 host->slot.handler_priv = ctx;
67 mutex_unlock(&host->slot.lock);
69 return ctx ? 0 : -ENOMEM;
72 int mmc_gpio_get_ro(struct mmc_host *host)
74 struct mmc_gpio *ctx = host->slot.handler_priv;
76 if (!ctx || !ctx->ro_gpio)
79 if (ctx->override_ro_active_level)
80 return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
81 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
83 return gpiod_get_value_cansleep(ctx->ro_gpio);
85 EXPORT_SYMBOL(mmc_gpio_get_ro);
87 int mmc_gpio_get_cd(struct mmc_host *host)
89 struct mmc_gpio *ctx = host->slot.handler_priv;
91 if (!ctx || !ctx->cd_gpio)
94 if (ctx->override_cd_active_level)
95 return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
96 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
98 return gpiod_get_value_cansleep(ctx->cd_gpio);
100 EXPORT_SYMBOL(mmc_gpio_get_cd);
103 * mmc_gpio_request_ro - request a gpio for write-protection
105 * @gpio: gpio number requested
107 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
108 * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
109 * if the requesting and freeing are only needed at probing and unbinding time
110 * for once. However, if client drivers do something special like runtime
111 * switching for write-protection, they are responsible for calling
112 * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
114 * Returns zero on success, else an error.
116 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
118 struct mmc_gpio *ctx;
121 if (!gpio_is_valid(gpio))
124 ret = mmc_gpio_alloc(host);
128 ctx = host->slot.handler_priv;
130 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
135 ctx->override_ro_active_level = true;
136 ctx->ro_gpio = gpio_to_desc(gpio);
140 EXPORT_SYMBOL(mmc_gpio_request_ro);
142 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
144 struct mmc_gpio *ctx = host->slot.handler_priv;
147 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
150 irq = gpiod_to_irq(ctx->cd_gpio);
153 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
154 * still prefer to poll, e.g., because that IRQ number is already used
155 * by another unit and cannot be shared.
157 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
161 ret = devm_request_threaded_irq(&host->class_dev, irq,
162 NULL, mmc_gpio_cd_irqt,
163 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
164 ctx->cd_label, host);
169 host->slot.cd_irq = irq;
172 host->caps |= MMC_CAP_NEEDS_POLL;
174 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
177 * mmc_gpio_request_cd - request a gpio for card-detection
179 * @gpio: gpio number requested
180 * @debounce: debounce time in microseconds
182 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
183 * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
184 * if the requesting and freeing are only needed at probing and unbinding time
185 * for once. However, if client drivers do something special like runtime
186 * switching for card-detection, they are responsible for calling
187 * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
189 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
190 * value. The caller is responsible for ensuring that the GPIO driver associated
191 * with the GPIO supports debouncing, otherwise an error will be returned.
193 * Returns zero on success, else an error.
195 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
196 unsigned int debounce)
198 struct mmc_gpio *ctx;
201 ret = mmc_gpio_alloc(host);
205 ctx = host->slot.handler_priv;
207 ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
211 * don't bother freeing memory. It might still get used by other
212 * slot functions, in any case it will be freed, when the device
218 ret = gpio_set_debounce(gpio, debounce);
223 ctx->override_cd_active_level = true;
224 ctx->cd_gpio = gpio_to_desc(gpio);
226 mmc_gpiod_request_cd_irq(host);
230 EXPORT_SYMBOL(mmc_gpio_request_cd);
233 * mmc_gpio_free_ro - free the write-protection gpio
236 * It's provided only for cases that client drivers need to manually free
237 * up the write-protection gpio requested by mmc_gpio_request_ro().
239 void mmc_gpio_free_ro(struct mmc_host *host)
241 struct mmc_gpio *ctx = host->slot.handler_priv;
244 if (!ctx || !ctx->ro_gpio)
247 gpio = desc_to_gpio(ctx->ro_gpio);
250 devm_gpio_free(&host->class_dev, gpio);
252 EXPORT_SYMBOL(mmc_gpio_free_ro);
255 * mmc_gpio_free_cd - free the card-detection gpio
258 * It's provided only for cases that client drivers need to manually free
259 * up the card-detection gpio requested by mmc_gpio_request_cd().
261 void mmc_gpio_free_cd(struct mmc_host *host)
263 struct mmc_gpio *ctx = host->slot.handler_priv;
266 if (!ctx || !ctx->cd_gpio)
269 if (host->slot.cd_irq >= 0) {
270 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
271 host->slot.cd_irq = -EINVAL;
274 gpio = desc_to_gpio(ctx->cd_gpio);
277 devm_gpio_free(&host->class_dev, gpio);
279 EXPORT_SYMBOL(mmc_gpio_free_cd);
282 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
284 * @con_id: function within the GPIO consumer
285 * @idx: index of the GPIO to obtain in the consumer
286 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
287 * @debounce: debounce time in microseconds
289 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
290 * descriptor API. Note that it is paired with mmc_gpiod_free_cd() not
291 * mmc_gpio_free_cd(). Note also that it must be called prior to mmc_add_host()
292 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
294 * Returns zero on success, else an error.
296 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
297 unsigned int idx, bool override_active_level,
298 unsigned int debounce)
300 struct mmc_gpio *ctx;
301 struct gpio_desc *desc;
304 ret = mmc_gpio_alloc(host);
308 ctx = host->slot.handler_priv;
311 con_id = ctx->cd_label;
313 desc = devm_gpiod_get_index(host->parent, con_id, idx);
315 return PTR_ERR(desc);
317 ret = gpiod_direction_input(desc);
322 ret = gpiod_set_debounce(desc, debounce);
327 ctx->override_cd_active_level = override_active_level;
332 EXPORT_SYMBOL(mmc_gpiod_request_cd);
335 * mmc_gpiod_free_cd - free the card-detection gpio descriptor
338 * It's provided only for cases that client drivers need to manually free
339 * up the card-detection gpio requested by mmc_gpiod_request_cd().
341 void mmc_gpiod_free_cd(struct mmc_host *host)
343 struct mmc_gpio *ctx = host->slot.handler_priv;
345 if (!ctx || !ctx->cd_gpio)
348 if (host->slot.cd_irq >= 0) {
349 devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
350 host->slot.cd_irq = -EINVAL;
353 devm_gpiod_put(&host->class_dev, ctx->cd_gpio);
357 EXPORT_SYMBOL(mmc_gpiod_free_cd);