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.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/slot-gpio.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
22 struct gpio_desc
*ro_gpio
;
23 struct gpio_desc
*cd_gpio
;
24 bool override_ro_active_level
;
25 bool override_cd_active_level
;
30 static irqreturn_t
mmc_gpio_cd_irqt(int irq
, void *dev_id
)
32 /* Schedule a card detection after a debounce timeout */
33 struct mmc_host
*host
= dev_id
;
35 host
->trigger_card_event
= true;
36 mmc_detect_change(host
, msecs_to_jiffies(200));
41 static int mmc_gpio_alloc(struct mmc_host
*host
)
43 size_t len
= strlen(dev_name(host
->parent
)) + 4;
46 mutex_lock(&host
->slot
.lock
);
48 ctx
= host
->slot
.handler_priv
;
51 * devm_kzalloc() can be called after device_initialize(), even
52 * before device_add(), i.e., between mmc_alloc_host() and
55 ctx
= devm_kzalloc(&host
->class_dev
, sizeof(*ctx
) + 2 * len
,
58 ctx
->ro_label
= ctx
->cd_label
+ len
;
59 snprintf(ctx
->cd_label
, len
, "%s cd", dev_name(host
->parent
));
60 snprintf(ctx
->ro_label
, len
, "%s ro", dev_name(host
->parent
));
61 host
->slot
.handler_priv
= ctx
;
65 mutex_unlock(&host
->slot
.lock
);
67 return ctx
? 0 : -ENOMEM
;
70 int mmc_gpio_get_ro(struct mmc_host
*host
)
72 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
74 if (!ctx
|| !ctx
->ro_gpio
)
77 if (ctx
->override_ro_active_level
)
78 return !gpiod_get_raw_value_cansleep(ctx
->ro_gpio
) ^
79 !!(host
->caps2
& MMC_CAP2_RO_ACTIVE_HIGH
);
81 return gpiod_get_value_cansleep(ctx
->ro_gpio
);
83 EXPORT_SYMBOL(mmc_gpio_get_ro
);
85 int mmc_gpio_get_cd(struct mmc_host
*host
)
87 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
89 if (!ctx
|| !ctx
->cd_gpio
)
92 if (ctx
->override_cd_active_level
)
93 return !gpiod_get_raw_value_cansleep(ctx
->cd_gpio
) ^
94 !!(host
->caps2
& MMC_CAP2_CD_ACTIVE_HIGH
);
96 return gpiod_get_value_cansleep(ctx
->cd_gpio
);
98 EXPORT_SYMBOL(mmc_gpio_get_cd
);
101 * mmc_gpio_request_ro - request a gpio for write-protection
103 * @gpio: gpio number requested
105 * As devm_* managed functions are used in mmc_gpio_request_ro(), client
106 * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
107 * if the requesting and freeing are only needed at probing and unbinding time
108 * for once. However, if client drivers do something special like runtime
109 * switching for write-protection, they are responsible for calling
110 * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
112 * Returns zero on success, else an error.
114 int mmc_gpio_request_ro(struct mmc_host
*host
, unsigned int gpio
)
116 struct mmc_gpio
*ctx
;
119 if (!gpio_is_valid(gpio
))
122 ret
= mmc_gpio_alloc(host
);
126 ctx
= host
->slot
.handler_priv
;
128 ret
= devm_gpio_request_one(&host
->class_dev
, gpio
, GPIOF_DIR_IN
,
133 ctx
->override_ro_active_level
= true;
134 ctx
->ro_gpio
= gpio_to_desc(gpio
);
138 EXPORT_SYMBOL(mmc_gpio_request_ro
);
140 void mmc_gpiod_request_cd_irq(struct mmc_host
*host
)
142 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
145 if (host
->slot
.cd_irq
>= 0 || !ctx
|| !ctx
->cd_gpio
)
148 irq
= gpiod_to_irq(ctx
->cd_gpio
);
151 * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
152 * still prefer to poll, e.g., because that IRQ number is already used
153 * by another unit and cannot be shared.
155 if (irq
>= 0 && host
->caps
& MMC_CAP_NEEDS_POLL
)
159 ret
= devm_request_threaded_irq(&host
->class_dev
, irq
,
160 NULL
, mmc_gpio_cd_irqt
,
161 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
162 ctx
->cd_label
, host
);
167 host
->slot
.cd_irq
= irq
;
170 host
->caps
|= MMC_CAP_NEEDS_POLL
;
172 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq
);
175 * mmc_gpio_request_cd - request a gpio for card-detection
177 * @gpio: gpio number requested
178 * @debounce: debounce time in microseconds
180 * As devm_* managed functions are used in mmc_gpio_request_cd(), client
181 * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
182 * if the requesting and freeing are only needed at probing and unbinding time
183 * for once. However, if client drivers do something special like runtime
184 * switching for card-detection, they are responsible for calling
185 * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
187 * If GPIO debouncing is desired, set the debounce parameter to a non-zero
188 * value. The caller is responsible for ensuring that the GPIO driver associated
189 * with the GPIO supports debouncing, otherwise an error will be returned.
191 * Returns zero on success, else an error.
193 int mmc_gpio_request_cd(struct mmc_host
*host
, unsigned int gpio
,
194 unsigned int debounce
)
196 struct mmc_gpio
*ctx
;
199 ret
= mmc_gpio_alloc(host
);
203 ctx
= host
->slot
.handler_priv
;
205 ret
= devm_gpio_request_one(&host
->class_dev
, gpio
, GPIOF_DIR_IN
,
209 * don't bother freeing memory. It might still get used by other
210 * slot functions, in any case it will be freed, when the device
216 ret
= gpio_set_debounce(gpio
, debounce
);
221 ctx
->override_cd_active_level
= true;
222 ctx
->cd_gpio
= gpio_to_desc(gpio
);
226 EXPORT_SYMBOL(mmc_gpio_request_cd
);
229 * mmc_gpio_free_ro - free the write-protection gpio
232 * It's provided only for cases that client drivers need to manually free
233 * up the write-protection gpio requested by mmc_gpio_request_ro().
235 void mmc_gpio_free_ro(struct mmc_host
*host
)
237 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
240 if (!ctx
|| !ctx
->ro_gpio
)
243 gpio
= desc_to_gpio(ctx
->ro_gpio
);
246 devm_gpio_free(&host
->class_dev
, gpio
);
248 EXPORT_SYMBOL(mmc_gpio_free_ro
);
251 * mmc_gpio_free_cd - free the card-detection gpio
254 * It's provided only for cases that client drivers need to manually free
255 * up the card-detection gpio requested by mmc_gpio_request_cd().
257 void mmc_gpio_free_cd(struct mmc_host
*host
)
259 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
262 if (!ctx
|| !ctx
->cd_gpio
)
265 if (host
->slot
.cd_irq
>= 0) {
266 devm_free_irq(&host
->class_dev
, host
->slot
.cd_irq
, host
);
267 host
->slot
.cd_irq
= -EINVAL
;
270 gpio
= desc_to_gpio(ctx
->cd_gpio
);
273 devm_gpio_free(&host
->class_dev
, gpio
);
275 EXPORT_SYMBOL(mmc_gpio_free_cd
);
278 * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
280 * @con_id: function within the GPIO consumer
281 * @idx: index of the GPIO to obtain in the consumer
282 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
283 * @debounce: debounce time in microseconds
284 * @gpio_invert: will return whether the GPIO line is inverted or not, set
287 * Use this function in place of mmc_gpio_request_cd() to use the GPIO
288 * descriptor API. Note that it is paired with mmc_gpiod_free_cd() not
289 * mmc_gpio_free_cd(). Note also that it must be called prior to mmc_add_host()
290 * otherwise the caller must also call mmc_gpiod_request_cd_irq().
292 * Returns zero on success, else an error.
294 int mmc_gpiod_request_cd(struct mmc_host
*host
, const char *con_id
,
295 unsigned int idx
, bool override_active_level
,
296 unsigned int debounce
, bool *gpio_invert
)
298 struct mmc_gpio
*ctx
;
299 struct gpio_desc
*desc
;
302 ret
= mmc_gpio_alloc(host
);
306 ctx
= host
->slot
.handler_priv
;
309 con_id
= ctx
->cd_label
;
311 desc
= devm_gpiod_get_index(host
->parent
, con_id
, idx
, GPIOD_IN
);
313 return PTR_ERR(desc
);
316 ret
= gpiod_set_debounce(desc
, debounce
);
322 *gpio_invert
= !gpiod_is_active_low(desc
);
324 ctx
->override_cd_active_level
= override_active_level
;
329 EXPORT_SYMBOL(mmc_gpiod_request_cd
);
332 * mmc_gpiod_request_ro - request a gpio descriptor for write protection
334 * @con_id: function within the GPIO consumer
335 * @idx: index of the GPIO to obtain in the consumer
336 * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
337 * @debounce: debounce time in microseconds
338 * @gpio_invert: will return whether the GPIO line is inverted or not,
339 * set to NULL to ignore
341 * Use this function in place of mmc_gpio_request_ro() to use the GPIO
342 * descriptor API. Note that it is paired with mmc_gpiod_free_ro() not
343 * mmc_gpio_free_ro().
345 * Returns zero on success, else an error.
347 int mmc_gpiod_request_ro(struct mmc_host
*host
, const char *con_id
,
348 unsigned int idx
, bool override_active_level
,
349 unsigned int debounce
, bool *gpio_invert
)
351 struct mmc_gpio
*ctx
;
352 struct gpio_desc
*desc
;
355 ret
= mmc_gpio_alloc(host
);
359 ctx
= host
->slot
.handler_priv
;
362 con_id
= ctx
->ro_label
;
364 desc
= devm_gpiod_get_index(host
->parent
, con_id
, idx
, GPIOD_IN
);
366 return PTR_ERR(desc
);
369 ret
= gpiod_set_debounce(desc
, debounce
);
375 *gpio_invert
= !gpiod_is_active_low(desc
);
377 ctx
->override_ro_active_level
= override_active_level
;
382 EXPORT_SYMBOL(mmc_gpiod_request_ro
);
385 * mmc_gpiod_free_cd - free the card-detection gpio descriptor
388 * It's provided only for cases that client drivers need to manually free
389 * up the card-detection gpio requested by mmc_gpiod_request_cd().
391 void mmc_gpiod_free_cd(struct mmc_host
*host
)
393 struct mmc_gpio
*ctx
= host
->slot
.handler_priv
;
395 if (!ctx
|| !ctx
->cd_gpio
)
398 if (host
->slot
.cd_irq
>= 0) {
399 devm_free_irq(&host
->class_dev
, host
->slot
.cd_irq
, host
);
400 host
->slot
.cd_irq
= -EINVAL
;
403 devm_gpiod_put(host
->parent
, ctx
->cd_gpio
);
407 EXPORT_SYMBOL(mmc_gpiod_free_cd
);