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 irq_handler_t cd_gpio_isr;
25 u32 cd_debounce_delay_ms;
29 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
31 /* Schedule a card detection after a debounce timeout */
32 struct mmc_host *host = dev_id;
33 struct mmc_gpio *ctx = host->slot.handler_priv;
35 host->trigger_card_event = true;
36 mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
41 int mmc_gpio_alloc(struct mmc_host *host)
43 const char *devname = dev_name(host->parent);
46 ctx = devm_kzalloc(host->parent, sizeof(*ctx), GFP_KERNEL);
50 ctx->cd_debounce_delay_ms = 200;
51 ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s cd", devname);
54 ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s ro", devname);
57 ctx->cd_irq = -EINVAL;
58 host->slot.handler_priv = ctx;
59 host->slot.cd_irq = -EINVAL;
64 void mmc_gpio_set_cd_irq(struct mmc_host *host, int irq)
66 struct mmc_gpio *ctx = host->slot.handler_priv;
73 EXPORT_SYMBOL(mmc_gpio_set_cd_irq);
75 int mmc_gpio_get_ro(struct mmc_host *host)
77 struct mmc_gpio *ctx = host->slot.handler_priv;
80 if (!ctx || !ctx->ro_gpio)
83 cansleep = gpiod_cansleep(ctx->ro_gpio);
85 gpiod_get_value_cansleep(ctx->ro_gpio) :
86 gpiod_get_value(ctx->ro_gpio);
88 EXPORT_SYMBOL(mmc_gpio_get_ro);
90 int mmc_gpio_get_cd(struct mmc_host *host)
92 struct mmc_gpio *ctx = host->slot.handler_priv;
95 if (!ctx || !ctx->cd_gpio)
98 cansleep = gpiod_cansleep(ctx->cd_gpio);
100 gpiod_get_value_cansleep(ctx->cd_gpio) :
101 gpiod_get_value(ctx->cd_gpio);
103 EXPORT_SYMBOL(mmc_gpio_get_cd);
105 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
107 struct mmc_gpio *ctx = host->slot.handler_priv;
111 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
115 * Do not use IRQ if the platform prefers to poll, e.g., because that
116 * IRQ number is already used by another unit and cannot be shared.
118 if (ctx->cd_irq >= 0)
120 else if (!(host->caps & MMC_CAP_NEEDS_POLL))
121 irq = gpiod_to_irq(ctx->cd_gpio);
124 if (!ctx->cd_gpio_isr)
125 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
126 ret = devm_request_threaded_irq(host->parent, irq,
127 NULL, ctx->cd_gpio_isr,
128 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
129 ctx->cd_label, host);
134 host->slot.cd_irq = irq;
137 host->caps |= MMC_CAP_NEEDS_POLL;
139 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
141 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
145 if (!(host->caps & MMC_CAP_CD_WAKE) ||
146 host->slot.cd_irq < 0 ||
147 on == host->slot.cd_wake_enabled)
151 ret = enable_irq_wake(host->slot.cd_irq);
152 host->slot.cd_wake_enabled = !ret;
154 disable_irq_wake(host->slot.cd_irq);
155 host->slot.cd_wake_enabled = false;
160 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
162 /* Register an alternate interrupt service routine for
163 * the card-detect GPIO.
165 void mmc_gpio_set_cd_isr(struct mmc_host *host, irq_handler_t isr)
167 struct mmc_gpio *ctx = host->slot.handler_priv;
169 WARN_ON(ctx->cd_gpio_isr);
170 ctx->cd_gpio_isr = isr;
172 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
175 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
177 * @con_id: function within the GPIO consumer
178 * @idx: index of the GPIO to obtain in the consumer
179 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
180 * @debounce: debounce time in microseconds
182 * Note that this must be called prior to mmc_add_host()
183 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
185 * Returns zero on success, else an error.
187 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
188 unsigned int idx, bool override_active_level,
189 unsigned int debounce)
191 struct mmc_gpio *ctx = host->slot.handler_priv;
192 struct gpio_desc *desc;
195 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
197 return PTR_ERR(desc);
199 /* Update default label if no con_id provided */
201 gpiod_set_consumer_name(desc, ctx->cd_label);
204 ret = gpiod_set_debounce(desc, debounce);
206 ctx->cd_debounce_delay_ms = debounce / 1000;
209 /* override forces default (active-low) polarity ... */
210 if (override_active_level && !gpiod_is_active_low(desc))
211 gpiod_toggle_active_low(desc);
213 /* ... or active-high */
214 if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
215 gpiod_toggle_active_low(desc);
221 EXPORT_SYMBOL(mmc_gpiod_request_cd);
224 * mmc_gpiod_set_cd_config - set config for card-detection GPIO
226 * @config: Generic pinconf config (from pinconf_to_config_packed())
228 * This can be used by mmc host drivers to fixup a card-detection GPIO's config
229 * (e.g. set PIN_CONFIG_BIAS_PULL_UP) after acquiring the GPIO descriptor
230 * through mmc_gpiod_request_cd().
233 * 0 on success, or a negative errno value on error.
235 int mmc_gpiod_set_cd_config(struct mmc_host *host, unsigned long config)
237 struct mmc_gpio *ctx = host->slot.handler_priv;
239 return gpiod_set_config(ctx->cd_gpio, config);
241 EXPORT_SYMBOL(mmc_gpiod_set_cd_config);
243 bool mmc_can_gpio_cd(struct mmc_host *host)
245 struct mmc_gpio *ctx = host->slot.handler_priv;
247 return ctx->cd_gpio ? true : false;
249 EXPORT_SYMBOL(mmc_can_gpio_cd);
252 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
254 * @con_id: function within the GPIO consumer
255 * @idx: index of the GPIO to obtain in the consumer
256 * @debounce: debounce time in microseconds
258 * Returns zero on success, else an error.
260 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
261 unsigned int idx, unsigned int debounce)
263 struct mmc_gpio *ctx = host->slot.handler_priv;
264 struct gpio_desc *desc;
267 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
269 return PTR_ERR(desc);
271 /* Update default label if no con_id provided */
273 gpiod_set_consumer_name(desc, ctx->ro_label);
276 ret = gpiod_set_debounce(desc, debounce);
281 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
282 gpiod_toggle_active_low(desc);
288 EXPORT_SYMBOL(mmc_gpiod_request_ro);
290 bool mmc_can_gpio_ro(struct mmc_host *host)
292 struct mmc_gpio *ctx = host->slot.handler_priv;
294 return ctx->ro_gpio ? true : false;
296 EXPORT_SYMBOL(mmc_can_gpio_ro);