1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic GPIO card-detect helper
5 * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
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 struct mmc_gpio *ctx = devm_kzalloc(host->parent,
43 sizeof(*ctx), GFP_KERNEL);
46 ctx->cd_debounce_delay_ms = 200;
47 ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL,
48 "%s cd", dev_name(host->parent));
51 ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL,
52 "%s ro", dev_name(host->parent));
55 host->slot.handler_priv = ctx;
56 host->slot.cd_irq = -EINVAL;
59 return ctx ? 0 : -ENOMEM;
62 int mmc_gpio_get_ro(struct mmc_host *host)
64 struct mmc_gpio *ctx = host->slot.handler_priv;
67 if (!ctx || !ctx->ro_gpio)
70 cansleep = gpiod_cansleep(ctx->ro_gpio);
72 gpiod_get_value_cansleep(ctx->ro_gpio) :
73 gpiod_get_value(ctx->ro_gpio);
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;
82 if (!ctx || !ctx->cd_gpio)
85 cansleep = gpiod_cansleep(ctx->cd_gpio);
87 gpiod_get_value_cansleep(ctx->cd_gpio) :
88 gpiod_get_value(ctx->cd_gpio);
90 EXPORT_SYMBOL(mmc_gpio_get_cd);
92 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
94 struct mmc_gpio *ctx = host->slot.handler_priv;
98 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
102 * Do not use IRQ if the platform prefers to poll, e.g., because that
103 * IRQ number is already used by another unit and cannot be shared.
105 if (!(host->caps & MMC_CAP_NEEDS_POLL))
106 irq = gpiod_to_irq(ctx->cd_gpio);
109 if (!ctx->cd_gpio_isr)
110 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
111 ret = devm_request_threaded_irq(host->parent, irq,
112 NULL, ctx->cd_gpio_isr,
113 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
114 ctx->cd_label, host);
119 host->slot.cd_irq = irq;
122 host->caps |= MMC_CAP_NEEDS_POLL;
124 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
126 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
130 if (!(host->caps & MMC_CAP_CD_WAKE) ||
131 host->slot.cd_irq < 0 ||
132 on == host->slot.cd_wake_enabled)
136 ret = enable_irq_wake(host->slot.cd_irq);
137 host->slot.cd_wake_enabled = !ret;
139 disable_irq_wake(host->slot.cd_irq);
140 host->slot.cd_wake_enabled = false;
145 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
147 /* Register an alternate interrupt service routine for
148 * the card-detect GPIO.
150 void mmc_gpio_set_cd_isr(struct mmc_host *host,
151 irqreturn_t (*isr)(int irq, void *dev_id))
153 struct mmc_gpio *ctx = host->slot.handler_priv;
155 WARN_ON(ctx->cd_gpio_isr);
156 ctx->cd_gpio_isr = isr;
158 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
161 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
163 * @con_id: function within the GPIO consumer
164 * @idx: index of the GPIO to obtain in the consumer
165 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
166 * @debounce: debounce time in microseconds
168 * Note that this must be called prior to mmc_add_host()
169 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
171 * Returns zero on success, else an error.
173 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
174 unsigned int idx, bool override_active_level,
175 unsigned int debounce)
177 struct mmc_gpio *ctx = host->slot.handler_priv;
178 struct gpio_desc *desc;
181 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
183 return PTR_ERR(desc);
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);
234 ret = gpiod_set_debounce(desc, debounce);
239 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
240 gpiod_toggle_active_low(desc);
246 EXPORT_SYMBOL(mmc_gpiod_request_ro);
248 bool mmc_can_gpio_ro(struct mmc_host *host)
250 struct mmc_gpio *ctx = host->slot.handler_priv;
252 return ctx->ro_gpio ? true : false;
254 EXPORT_SYMBOL(mmc_can_gpio_ro);