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 bool override_cd_active_level
;
23 irqreturn_t (*cd_gpio_isr
)(int irq
, void *dev_id
);
26 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 struct mmc_gpio
*ctx
= devm_kzalloc(host
->parent
,
44 sizeof(*ctx
), GFP_KERNEL
);
47 ctx
->cd_debounce_delay_ms
= 200;
48 ctx
->cd_label
= devm_kasprintf(host
->parent
, GFP_KERNEL
,
49 "%s cd", dev_name(host
->parent
));
52 ctx
->ro_label
= devm_kasprintf(host
->parent
, GFP_KERNEL
,
53 "%s ro", dev_name(host
->parent
));
56 host
->slot
.handler_priv
= ctx
;
57 host
->slot
.cd_irq
= -EINVAL
;
60 return ctx
? 0 : -ENOMEM
;
63 int mmc_gpio_get_ro(struct mmc_host
*host
)
65 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
67 if (!ctx
|| !ctx
->ro_gpio
)
70 return gpiod_get_value_cansleep(ctx
->ro_gpio
);
72 EXPORT_SYMBOL(mmc_gpio_get_ro
);
74 int mmc_gpio_get_cd(struct mmc_host
*host
)
76 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
79 if (!ctx
|| !ctx
->cd_gpio
)
82 cansleep
= gpiod_cansleep(ctx
->cd_gpio
);
83 if (ctx
->override_cd_active_level
) {
84 int value
= cansleep
?
85 gpiod_get_raw_value_cansleep(ctx
->cd_gpio
) :
86 gpiod_get_raw_value(ctx
->cd_gpio
);
87 return !value
^ !!(host
->caps2
& MMC_CAP2_CD_ACTIVE_HIGH
);
91 gpiod_get_value_cansleep(ctx
->cd_gpio
) :
92 gpiod_get_value(ctx
->cd_gpio
);
94 EXPORT_SYMBOL(mmc_gpio_get_cd
);
96 void mmc_gpiod_request_cd_irq(struct mmc_host
*host
)
98 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
102 if (host
->slot
.cd_irq
>= 0 || !ctx
|| !ctx
->cd_gpio
)
106 * Do not use IRQ if the platform prefers to poll, e.g., because that
107 * IRQ number is already used by another unit and cannot be shared.
109 if (!(host
->caps
& MMC_CAP_NEEDS_POLL
))
110 irq
= gpiod_to_irq(ctx
->cd_gpio
);
113 if (!ctx
->cd_gpio_isr
)
114 ctx
->cd_gpio_isr
= mmc_gpio_cd_irqt
;
115 ret
= devm_request_threaded_irq(host
->parent
, irq
,
116 NULL
, ctx
->cd_gpio_isr
,
117 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
118 ctx
->cd_label
, host
);
123 host
->slot
.cd_irq
= irq
;
126 host
->caps
|= MMC_CAP_NEEDS_POLL
;
128 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq
);
130 int mmc_gpio_set_cd_wake(struct mmc_host
*host
, bool on
)
134 if (!(host
->caps
& MMC_CAP_CD_WAKE
) ||
135 host
->slot
.cd_irq
< 0 ||
136 on
== host
->slot
.cd_wake_enabled
)
140 ret
= enable_irq_wake(host
->slot
.cd_irq
);
141 host
->slot
.cd_wake_enabled
= !ret
;
143 disable_irq_wake(host
->slot
.cd_irq
);
144 host
->slot
.cd_wake_enabled
= false;
149 EXPORT_SYMBOL(mmc_gpio_set_cd_wake
);
151 /* Register an alternate interrupt service routine for
152 * the card-detect GPIO.
154 void mmc_gpio_set_cd_isr(struct mmc_host
*host
,
155 irqreturn_t (*isr
)(int irq
, void *dev_id
))
157 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
159 WARN_ON(ctx
->cd_gpio_isr
);
160 ctx
->cd_gpio_isr
= isr
;
162 EXPORT_SYMBOL(mmc_gpio_set_cd_isr
);
165 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
167 * @con_id: function within the GPIO consumer
168 * @idx: index of the GPIO to obtain in the consumer
169 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
170 * @debounce: debounce time in microseconds
171 * @gpio_invert: will return whether the GPIO line is inverted or not, set
174 * Note that this must be called prior to mmc_add_host()
175 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
177 * Returns zero on success, else an error.
179 int mmc_gpiod_request_cd(struct mmc_host
*host
, const char *con_id
,
180 unsigned int idx
, bool override_active_level
,
181 unsigned int debounce
, bool *gpio_invert
)
183 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
184 struct gpio_desc
*desc
;
187 desc
= devm_gpiod_get_index(host
->parent
, con_id
, idx
, GPIOD_IN
);
189 return PTR_ERR(desc
);
192 ret
= gpiod_set_debounce(desc
, debounce
);
194 ctx
->cd_debounce_delay_ms
= debounce
/ 1000;
198 *gpio_invert
= !gpiod_is_active_low(desc
);
200 ctx
->override_cd_active_level
= override_active_level
;
205 EXPORT_SYMBOL(mmc_gpiod_request_cd
);
207 bool mmc_can_gpio_cd(struct mmc_host
*host
)
209 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
211 return ctx
->cd_gpio
? true : false;
213 EXPORT_SYMBOL(mmc_can_gpio_cd
);
216 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
218 * @con_id: function within the GPIO consumer
219 * @idx: index of the GPIO to obtain in the consumer
220 * @debounce: debounce time in microseconds
221 * @gpio_invert: will return whether the GPIO line is inverted or not,
222 * set to NULL to ignore
224 * Returns zero on success, else an error.
226 int mmc_gpiod_request_ro(struct mmc_host
*host
, const char *con_id
,
228 unsigned int debounce
, bool *gpio_invert
)
230 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
231 struct gpio_desc
*desc
;
234 desc
= devm_gpiod_get_index(host
->parent
, con_id
, idx
, GPIOD_IN
);
236 return PTR_ERR(desc
);
239 ret
= gpiod_set_debounce(desc
, debounce
);
245 *gpio_invert
= !gpiod_is_active_low(desc
);
251 EXPORT_SYMBOL(mmc_gpiod_request_ro
);
253 bool mmc_can_gpio_ro(struct mmc_host
*host
)
255 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
257 return ctx
->ro_gpio
? true : false;
259 EXPORT_SYMBOL(mmc_can_gpio_ro
);