1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic GPIO card-detect helper
9 #include <linux/gpio/consumer.h>
10 #include <linux/interrupt.h>
11 #include <linux/jiffies.h>
12 #include <linux/mmc/host.h>
13 #include <linux/mmc/slot-gpio.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include "slot-gpio.h"
20 struct gpio_desc *ro_gpio;
21 struct gpio_desc *cd_gpio;
22 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
25 u32 cd_debounce_delay_ms;
28 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
30 /* Schedule a card detection after a debounce timeout */
31 struct mmc_host *host = dev_id;
32 struct mmc_gpio *ctx = host->slot.handler_priv;
34 host->trigger_card_event = true;
35 mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
40 int mmc_gpio_alloc(struct mmc_host *host)
42 const char *devname = dev_name(host->parent);
45 ctx = devm_kzalloc(host->parent, sizeof(*ctx), GFP_KERNEL);
49 ctx->cd_debounce_delay_ms = 200;
50 ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s cd", devname);
53 ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s ro", devname);
56 host->slot.handler_priv = ctx;
57 host->slot.cd_irq = -EINVAL;
62 int mmc_gpio_get_ro(struct mmc_host *host)
64 struct mmc_gpio *ctx = host->slot.handler_priv;
66 if (!ctx || !ctx->ro_gpio)
69 return gpiod_get_value_cansleep(ctx->ro_gpio);
71 EXPORT_SYMBOL(mmc_gpio_get_ro);
73 int mmc_gpio_get_cd(struct mmc_host *host)
75 struct mmc_gpio *ctx = host->slot.handler_priv;
78 if (!ctx || !ctx->cd_gpio)
81 cansleep = gpiod_cansleep(ctx->cd_gpio);
83 gpiod_get_value_cansleep(ctx->cd_gpio) :
84 gpiod_get_value(ctx->cd_gpio);
86 EXPORT_SYMBOL(mmc_gpio_get_cd);
88 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
90 struct mmc_gpio *ctx = host->slot.handler_priv;
94 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
98 * Do not use IRQ if the platform prefers to poll, e.g., because that
99 * IRQ number is already used by another unit and cannot be shared.
101 if (!(host->caps & MMC_CAP_NEEDS_POLL))
102 irq = gpiod_to_irq(ctx->cd_gpio);
105 if (!ctx->cd_gpio_isr)
106 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
107 ret = devm_request_threaded_irq(host->parent, irq,
108 NULL, ctx->cd_gpio_isr,
109 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
110 ctx->cd_label, host);
115 host->slot.cd_irq = irq;
118 host->caps |= MMC_CAP_NEEDS_POLL;
120 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
122 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
126 if (!(host->caps & MMC_CAP_CD_WAKE) ||
127 host->slot.cd_irq < 0 ||
128 on == host->slot.cd_wake_enabled)
132 ret = enable_irq_wake(host->slot.cd_irq);
133 host->slot.cd_wake_enabled = !ret;
135 disable_irq_wake(host->slot.cd_irq);
136 host->slot.cd_wake_enabled = false;
141 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
143 /* Register an alternate interrupt service routine for
144 * the card-detect GPIO.
146 void mmc_gpio_set_cd_isr(struct mmc_host *host,
147 irqreturn_t (*isr)(int irq, void *dev_id))
149 struct mmc_gpio *ctx = host->slot.handler_priv;
151 WARN_ON(ctx->cd_gpio_isr);
152 ctx->cd_gpio_isr = isr;
154 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
157 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
159 * @con_id: function within the GPIO consumer
160 * @idx: index of the GPIO to obtain in the consumer
161 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
162 * @debounce: debounce time in microseconds
164 * Note that this must be called prior to mmc_add_host()
165 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
167 * Returns zero on success, else an error.
169 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
170 unsigned int idx, bool override_active_level,
171 unsigned int debounce)
173 struct mmc_gpio *ctx = host->slot.handler_priv;
174 struct gpio_desc *desc;
177 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
179 return PTR_ERR(desc);
181 /* Update default label if no con_id provided */
183 gpiod_set_consumer_name(desc, ctx->cd_label);
186 ret = gpiod_set_debounce(desc, debounce);
188 ctx->cd_debounce_delay_ms = debounce / 1000;
191 /* override forces default (active-low) polarity ... */
192 if (override_active_level && !gpiod_is_active_low(desc))
193 gpiod_toggle_active_low(desc);
195 /* ... or active-high */
196 if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
197 gpiod_toggle_active_low(desc);
203 EXPORT_SYMBOL(mmc_gpiod_request_cd);
205 bool mmc_can_gpio_cd(struct mmc_host *host)
207 struct mmc_gpio *ctx = host->slot.handler_priv;
209 return ctx->cd_gpio ? true : false;
211 EXPORT_SYMBOL(mmc_can_gpio_cd);
214 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
216 * @con_id: function within the GPIO consumer
217 * @idx: index of the GPIO to obtain in the consumer
218 * @debounce: debounce time in microseconds
220 * Returns zero on success, else an error.
222 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
223 unsigned int idx, unsigned int debounce)
225 struct mmc_gpio *ctx = host->slot.handler_priv;
226 struct gpio_desc *desc;
229 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
231 return PTR_ERR(desc);
233 /* Update default label if no con_id provided */
235 gpiod_set_consumer_name(desc, ctx->ro_label);
238 ret = gpiod_set_debounce(desc, debounce);
243 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
244 gpiod_toggle_active_low(desc);
250 EXPORT_SYMBOL(mmc_gpiod_request_ro);
252 bool mmc_can_gpio_ro(struct mmc_host *host)
254 struct mmc_gpio *ctx = host->slot.handler_priv;
256 return ctx->ro_gpio ? true : false;
258 EXPORT_SYMBOL(mmc_can_gpio_ro);