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/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>
27 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
29 /* Schedule a card detection after a debounce timeout */
30 mmc_detect_change(dev_id, msecs_to_jiffies(100));
34 static int mmc_gpio_alloc(struct mmc_host *host)
36 size_t len = strlen(dev_name(host->parent)) + 4;
39 mutex_lock(&host->slot.lock);
41 ctx = host->slot.handler_priv;
44 * devm_kzalloc() can be called after device_initialize(), even
45 * before device_add(), i.e., between mmc_alloc_host() and
48 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
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 ctx->cd_gpio = -EINVAL;
55 ctx->ro_gpio = -EINVAL;
56 host->slot.handler_priv = ctx;
60 mutex_unlock(&host->slot.lock);
62 return ctx ? 0 : -ENOMEM;
65 int mmc_gpio_get_ro(struct mmc_host *host)
67 struct mmc_gpio *ctx = host->slot.handler_priv;
69 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
72 return !gpio_get_value_cansleep(ctx->ro_gpio) ^
73 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
75 EXPORT_SYMBOL(mmc_gpio_get_ro);
77 int mmc_gpio_get_cd(struct mmc_host *host)
79 struct mmc_gpio *ctx = host->slot.handler_priv;
81 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
84 return !gpio_get_value_cansleep(ctx->cd_gpio) ^
85 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
87 EXPORT_SYMBOL(mmc_gpio_get_cd);
89 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
94 if (!gpio_is_valid(gpio))
97 ret = mmc_gpio_alloc(host);
101 ctx = host->slot.handler_priv;
103 return gpio_request_one(gpio, GPIOF_DIR_IN, ctx->ro_label);
105 EXPORT_SYMBOL(mmc_gpio_request_ro);
107 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
109 struct mmc_gpio *ctx;
110 int irq = gpio_to_irq(gpio);
113 ret = mmc_gpio_alloc(host);
117 ctx = host->slot.handler_priv;
119 ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
122 * don't bother freeing memory. It might still get used by other
123 * slot functions, in any case it will be freed, when the device
129 * Even if gpio_to_irq() returns a valid IRQ number, the platform might
130 * still prefer to poll, e.g., because that IRQ number is already used
131 * by another unit and cannot be shared.
133 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
137 ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
138 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
139 ctx->cd_label, host);
144 host->slot.cd_irq = irq;
147 host->caps |= MMC_CAP_NEEDS_POLL;
153 EXPORT_SYMBOL(mmc_gpio_request_cd);
155 void mmc_gpio_free_ro(struct mmc_host *host)
157 struct mmc_gpio *ctx = host->slot.handler_priv;
160 if (!ctx || !gpio_is_valid(ctx->ro_gpio))
164 ctx->ro_gpio = -EINVAL;
168 EXPORT_SYMBOL(mmc_gpio_free_ro);
170 void mmc_gpio_free_cd(struct mmc_host *host)
172 struct mmc_gpio *ctx = host->slot.handler_priv;
175 if (!ctx || !gpio_is_valid(ctx->cd_gpio))
178 if (host->slot.cd_irq >= 0) {
179 free_irq(host->slot.cd_irq, host);
180 host->slot.cd_irq = -EINVAL;
184 ctx->cd_gpio = -EINVAL;
188 EXPORT_SYMBOL(mmc_gpio_free_cd);