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/consumer.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>
20 #include "slot-gpio.h"
23 struct gpio_desc
*ro_gpio
;
24 struct gpio_desc
*cd_gpio
;
25 bool override_ro_active_level
;
26 bool override_cd_active_level
;
27 irqreturn_t (*cd_gpio_isr
)(int irq
, void *dev_id
);
30 u32 cd_debounce_delay_ms
;
33 static irqreturn_t
mmc_gpio_cd_irqt(int irq
, void *dev_id
)
35 /* Schedule a card detection after a debounce timeout */
36 struct mmc_host
*host
= dev_id
;
37 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
39 host
->trigger_card_event
= true;
40 mmc_detect_change(host
, msecs_to_jiffies(ctx
->cd_debounce_delay_ms
));
45 int mmc_gpio_alloc(struct mmc_host
*host
)
47 struct mmc_gpio
*ctx
= devm_kzalloc(host
->parent
,
48 sizeof(*ctx
), GFP_KERNEL
);
51 ctx
->cd_debounce_delay_ms
= 200;
52 ctx
->cd_label
= devm_kasprintf(host
->parent
, GFP_KERNEL
,
53 "%s cd", dev_name(host
->parent
));
56 ctx
->ro_label
= devm_kasprintf(host
->parent
, GFP_KERNEL
,
57 "%s ro", dev_name(host
->parent
));
60 host
->slot
.handler_priv
= ctx
;
61 host
->slot
.cd_irq
= -EINVAL
;
64 return ctx
? 0 : -ENOMEM
;
67 int mmc_gpio_get_ro(struct mmc_host
*host
)
69 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
71 if (!ctx
|| !ctx
->ro_gpio
)
74 if (ctx
->override_ro_active_level
)
75 return !gpiod_get_raw_value_cansleep(ctx
->ro_gpio
) ^
76 !!(host
->caps2
& MMC_CAP2_RO_ACTIVE_HIGH
);
78 return gpiod_get_value_cansleep(ctx
->ro_gpio
);
80 EXPORT_SYMBOL(mmc_gpio_get_ro
);
82 int mmc_gpio_get_cd(struct mmc_host
*host
)
84 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
87 if (!ctx
|| !ctx
->cd_gpio
)
90 cansleep
= gpiod_cansleep(ctx
->cd_gpio
);
91 if (ctx
->override_cd_active_level
) {
92 int value
= cansleep
?
93 gpiod_get_raw_value_cansleep(ctx
->cd_gpio
) :
94 gpiod_get_raw_value(ctx
->cd_gpio
);
95 return !value
^ !!(host
->caps2
& MMC_CAP2_CD_ACTIVE_HIGH
);
99 gpiod_get_value_cansleep(ctx
->cd_gpio
) :
100 gpiod_get_value(ctx
->cd_gpio
);
102 EXPORT_SYMBOL(mmc_gpio_get_cd
);
104 void mmc_gpiod_request_cd_irq(struct mmc_host
*host
)
106 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
110 if (host
->slot
.cd_irq
>= 0 || !ctx
|| !ctx
->cd_gpio
)
114 * Do not use IRQ if the platform prefers to poll, e.g., because that
115 * IRQ number is already used by another unit and cannot be shared.
117 if (!(host
->caps
& MMC_CAP_NEEDS_POLL
))
118 irq
= gpiod_to_irq(ctx
->cd_gpio
);
121 if (!ctx
->cd_gpio_isr
)
122 ctx
->cd_gpio_isr
= mmc_gpio_cd_irqt
;
123 ret
= devm_request_threaded_irq(host
->parent
, irq
,
124 NULL
, ctx
->cd_gpio_isr
,
125 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
126 ctx
->cd_label
, host
);
131 host
->slot
.cd_irq
= irq
;
134 host
->caps
|= MMC_CAP_NEEDS_POLL
;
136 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq
);
138 int mmc_gpio_set_cd_wake(struct mmc_host
*host
, bool on
)
142 if (!(host
->caps
& MMC_CAP_CD_WAKE
) ||
143 host
->slot
.cd_irq
< 0 ||
144 on
== host
->slot
.cd_wake_enabled
)
148 ret
= enable_irq_wake(host
->slot
.cd_irq
);
149 host
->slot
.cd_wake_enabled
= !ret
;
151 disable_irq_wake(host
->slot
.cd_irq
);
152 host
->slot
.cd_wake_enabled
= false;
157 EXPORT_SYMBOL(mmc_gpio_set_cd_wake
);
159 /* Register an alternate interrupt service routine for
160 * the card-detect GPIO.
162 void mmc_gpio_set_cd_isr(struct mmc_host
*host
,
163 irqreturn_t (*isr
)(int irq
, void *dev_id
))
165 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
167 WARN_ON(ctx
->cd_gpio_isr
);
168 ctx
->cd_gpio_isr
= isr
;
170 EXPORT_SYMBOL(mmc_gpio_set_cd_isr
);
173 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
175 * @con_id: function within the GPIO consumer
176 * @idx: index of the GPIO to obtain in the consumer
177 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
178 * @debounce: debounce time in microseconds
179 * @gpio_invert: will return whether the GPIO line is inverted or not, set
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
, bool *gpio_invert
)
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
);
200 ret
= gpiod_set_debounce(desc
, debounce
);
202 ctx
->cd_debounce_delay_ms
= debounce
/ 1000;
206 *gpio_invert
= !gpiod_is_active_low(desc
);
208 ctx
->override_cd_active_level
= override_active_level
;
213 EXPORT_SYMBOL(mmc_gpiod_request_cd
);
215 bool mmc_can_gpio_cd(struct mmc_host
*host
)
217 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
219 return ctx
->cd_gpio
? true : false;
221 EXPORT_SYMBOL(mmc_can_gpio_cd
);
224 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
226 * @con_id: function within the GPIO consumer
227 * @idx: index of the GPIO to obtain in the consumer
228 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
229 * @debounce: debounce time in microseconds
230 * @gpio_invert: will return whether the GPIO line is inverted or not,
231 * set to NULL to ignore
233 * Returns zero on success, else an error.
235 int mmc_gpiod_request_ro(struct mmc_host
*host
, const char *con_id
,
236 unsigned int idx
, bool override_active_level
,
237 unsigned int debounce
, bool *gpio_invert
)
239 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
240 struct gpio_desc
*desc
;
243 desc
= devm_gpiod_get_index(host
->parent
, con_id
, idx
, GPIOD_IN
);
245 return PTR_ERR(desc
);
248 ret
= gpiod_set_debounce(desc
, debounce
);
254 *gpio_invert
= !gpiod_is_active_low(desc
);
256 ctx
->override_ro_active_level
= override_active_level
;
261 EXPORT_SYMBOL(mmc_gpiod_request_ro
);
263 bool mmc_can_gpio_ro(struct mmc_host
*host
)
265 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
267 return ctx
->ro_gpio
? true : false;
269 EXPORT_SYMBOL(mmc_can_gpio_ro
);