Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Generic GPIO card-detect helper |
| 4 | * |
| 5 | * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de> |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/err.h> |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 9 | #include <linux/gpio/consumer.h> |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 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> |
| 16 | |
Ulf Hansson | 7f133de | 2014-12-18 15:44:34 +0100 | [diff] [blame] | 17 | #include "slot-gpio.h" |
| 18 | |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 19 | struct mmc_gpio { |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 20 | struct gpio_desc *ro_gpio; |
| 21 | struct gpio_desc *cd_gpio; |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame] | 22 | irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id); |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 23 | char *ro_label; |
Linus Walleij | ec5af09 | 2018-11-12 15:12:30 +0100 | [diff] [blame] | 24 | char *cd_label; |
Shawn Lin | bfd694d | 2018-04-24 08:42:57 +0800 | [diff] [blame] | 25 | u32 cd_debounce_delay_ms; |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) |
| 29 | { |
| 30 | /* Schedule a card detection after a debounce timeout */ |
Guennadi Liakhovetski | 451c895 | 2012-12-04 16:51:36 +0100 | [diff] [blame] | 31 | struct mmc_host *host = dev_id; |
Shawn Lin | bfd694d | 2018-04-24 08:42:57 +0800 | [diff] [blame] | 32 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Guennadi Liakhovetski | 451c895 | 2012-12-04 16:51:36 +0100 | [diff] [blame] | 33 | |
Markus Mayer | fa372a5 | 2014-04-08 15:19:43 -0700 | [diff] [blame] | 34 | host->trigger_card_event = true; |
Shawn Lin | bfd694d | 2018-04-24 08:42:57 +0800 | [diff] [blame] | 35 | mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms)); |
Guennadi Liakhovetski | 451c895 | 2012-12-04 16:51:36 +0100 | [diff] [blame] | 36 | |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 37 | return IRQ_HANDLED; |
| 38 | } |
| 39 | |
Ulf Hansson | 7f133de | 2014-12-18 15:44:34 +0100 | [diff] [blame] | 40 | int mmc_gpio_alloc(struct mmc_host *host) |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 41 | { |
Andy Shevchenko | 4877b81 | 2021-09-29 14:17:56 +0300 | [diff] [blame] | 42 | const char *devname = dev_name(host->parent); |
| 43 | struct mmc_gpio *ctx; |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 44 | |
Andy Shevchenko | 4877b81 | 2021-09-29 14:17:56 +0300 | [diff] [blame] | 45 | ctx = devm_kzalloc(host->parent, sizeof(*ctx), GFP_KERNEL); |
| 46 | if (!ctx) |
| 47 | return -ENOMEM; |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 48 | |
Andy Shevchenko | 4877b81 | 2021-09-29 14:17:56 +0300 | [diff] [blame] | 49 | ctx->cd_debounce_delay_ms = 200; |
| 50 | ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s cd", devname); |
| 51 | if (!ctx->cd_label) |
| 52 | return -ENOMEM; |
| 53 | ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s ro", devname); |
| 54 | if (!ctx->ro_label) |
| 55 | return -ENOMEM; |
| 56 | host->slot.handler_priv = ctx; |
| 57 | host->slot.cd_irq = -EINVAL; |
| 58 | |
| 59 | return 0; |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 62 | int mmc_gpio_get_ro(struct mmc_host *host) |
| 63 | { |
| 64 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 65 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 66 | if (!ctx || !ctx->ro_gpio) |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 67 | return -ENOSYS; |
| 68 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 69 | return gpiod_get_value_cansleep(ctx->ro_gpio); |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 70 | } |
| 71 | EXPORT_SYMBOL(mmc_gpio_get_ro); |
| 72 | |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 73 | int mmc_gpio_get_cd(struct mmc_host *host) |
| 74 | { |
| 75 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Evan Green | 52af318 | 2018-05-25 12:25:23 -0700 | [diff] [blame] | 76 | int cansleep; |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 77 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 78 | if (!ctx || !ctx->cd_gpio) |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 79 | return -ENOSYS; |
| 80 | |
Evan Green | 52af318 | 2018-05-25 12:25:23 -0700 | [diff] [blame] | 81 | cansleep = gpiod_cansleep(ctx->cd_gpio); |
Evan Green | 52af318 | 2018-05-25 12:25:23 -0700 | [diff] [blame] | 82 | return cansleep ? |
| 83 | gpiod_get_value_cansleep(ctx->cd_gpio) : |
| 84 | gpiod_get_value(ctx->cd_gpio); |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 85 | } |
| 86 | EXPORT_SYMBOL(mmc_gpio_get_cd); |
| 87 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 88 | void mmc_gpiod_request_cd_irq(struct mmc_host *host) |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 89 | { |
| 90 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Masahiro Yamada | 5f3a860 | 2017-11-08 15:34:54 +0900 | [diff] [blame] | 91 | int irq = -EINVAL; |
| 92 | int ret; |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 93 | |
| 94 | if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio) |
| 95 | return; |
| 96 | |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 97 | /* |
Masahiro Yamada | 5f3a860 | 2017-11-08 15:34:54 +0900 | [diff] [blame] | 98 | * Do not use IRQ if the platform prefers to poll, e.g., because that |
| 99 | * IRQ number is already used by another unit and cannot be shared. |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 100 | */ |
Masahiro Yamada | 5f3a860 | 2017-11-08 15:34:54 +0900 | [diff] [blame] | 101 | if (!(host->caps & MMC_CAP_NEEDS_POLL)) |
| 102 | irq = gpiod_to_irq(ctx->cd_gpio); |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 103 | |
| 104 | if (irq >= 0) { |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame] | 105 | if (!ctx->cd_gpio_isr) |
| 106 | ctx->cd_gpio_isr = mmc_gpio_cd_irqt; |
Ulf Hansson | b4cc580 | 2014-12-18 15:44:33 +0100 | [diff] [blame] | 107 | ret = devm_request_threaded_irq(host->parent, irq, |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame] | 108 | NULL, ctx->cd_gpio_isr, |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 109 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 110 | ctx->cd_label, host); |
| 111 | if (ret < 0) |
| 112 | irq = ret; |
| 113 | } |
| 114 | |
| 115 | host->slot.cd_irq = irq; |
| 116 | |
| 117 | if (irq < 0) |
| 118 | host->caps |= MMC_CAP_NEEDS_POLL; |
| 119 | } |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 120 | EXPORT_SYMBOL(mmc_gpiod_request_cd_irq); |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 121 | |
Adrian Hunter | 36f1d7e | 2018-02-27 14:51:25 +0200 | [diff] [blame] | 122 | int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on) |
| 123 | { |
| 124 | int ret = 0; |
| 125 | |
| 126 | if (!(host->caps & MMC_CAP_CD_WAKE) || |
| 127 | host->slot.cd_irq < 0 || |
| 128 | on == host->slot.cd_wake_enabled) |
| 129 | return 0; |
| 130 | |
| 131 | if (on) { |
| 132 | ret = enable_irq_wake(host->slot.cd_irq); |
| 133 | host->slot.cd_wake_enabled = !ret; |
| 134 | } else { |
| 135 | disable_irq_wake(host->slot.cd_irq); |
| 136 | host->slot.cd_wake_enabled = false; |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | EXPORT_SYMBOL(mmc_gpio_set_cd_wake); |
| 142 | |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame] | 143 | /* Register an alternate interrupt service routine for |
| 144 | * the card-detect GPIO. |
| 145 | */ |
| 146 | void mmc_gpio_set_cd_isr(struct mmc_host *host, |
| 147 | irqreturn_t (*isr)(int irq, void *dev_id)) |
| 148 | { |
| 149 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 150 | |
| 151 | WARN_ON(ctx->cd_gpio_isr); |
| 152 | ctx->cd_gpio_isr = isr; |
| 153 | } |
| 154 | EXPORT_SYMBOL(mmc_gpio_set_cd_isr); |
| 155 | |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 156 | /** |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 157 | * mmc_gpiod_request_cd - request a gpio descriptor for card-detection |
| 158 | * @host: mmc host |
| 159 | * @con_id: function within the GPIO consumer |
| 160 | * @idx: index of the GPIO to obtain in the consumer |
| 161 | * @override_active_level: ignore %GPIO_ACTIVE_LOW flag |
| 162 | * @debounce: debounce time in microseconds |
| 163 | * |
Linus Walleij | a622bb0 | 2018-12-02 09:43:28 +0100 | [diff] [blame] | 164 | * Note that this must be called prior to mmc_add_host() |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 165 | * otherwise the caller must also call mmc_gpiod_request_cd_irq(). |
| 166 | * |
| 167 | * Returns zero on success, else an error. |
| 168 | */ |
| 169 | int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id, |
| 170 | unsigned int idx, bool override_active_level, |
Michał Mirosław | d0052ad | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 171 | unsigned int debounce) |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 172 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 173 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 174 | struct gpio_desc *desc; |
| 175 | int ret; |
| 176 | |
Linus Walleij | 9fbc695 | 2014-08-27 13:00:50 +0200 | [diff] [blame] | 177 | desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN); |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 178 | if (IS_ERR(desc)) |
| 179 | return PTR_ERR(desc); |
| 180 | |
Andy Shevchenko | 8792b0a | 2021-09-29 14:17:57 +0300 | [diff] [blame] | 181 | /* Update default label if no con_id provided */ |
| 182 | if (!con_id) |
| 183 | gpiod_set_consumer_name(desc, ctx->cd_label); |
| 184 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 185 | if (debounce) { |
| 186 | ret = gpiod_set_debounce(desc, debounce); |
| 187 | if (ret < 0) |
Marek Szyprowski | 1b09d9c | 2018-09-28 14:20:40 +0200 | [diff] [blame] | 188 | ctx->cd_debounce_delay_ms = debounce / 1000; |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Michał Mirosław | 0f7c815 | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 191 | /* override forces default (active-low) polarity ... */ |
| 192 | if (override_active_level && !gpiod_is_active_low(desc)) |
| 193 | gpiod_toggle_active_low(desc); |
| 194 | |
| 195 | /* ... or active-high */ |
| 196 | if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) |
| 197 | gpiod_toggle_active_low(desc); |
| 198 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 199 | ctx->cd_gpio = desc; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | EXPORT_SYMBOL(mmc_gpiod_request_cd); |
| 204 | |
Shawn Lin | 50fcbbb | 2016-10-12 10:50:37 +0800 | [diff] [blame] | 205 | bool mmc_can_gpio_cd(struct mmc_host *host) |
| 206 | { |
| 207 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 208 | |
| 209 | return ctx->cd_gpio ? true : false; |
| 210 | } |
| 211 | EXPORT_SYMBOL(mmc_can_gpio_cd); |
| 212 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 213 | /** |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 214 | * mmc_gpiod_request_ro - request a gpio descriptor for write protection |
| 215 | * @host: mmc host |
| 216 | * @con_id: function within the GPIO consumer |
| 217 | * @idx: index of the GPIO to obtain in the consumer |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 218 | * @debounce: debounce time in microseconds |
| 219 | * |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 220 | * Returns zero on success, else an error. |
| 221 | */ |
| 222 | int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id, |
Michał Mirosław | d0052ad | 2019-12-11 03:40:56 +0100 | [diff] [blame] | 223 | unsigned int idx, unsigned int debounce) |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 224 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 225 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 226 | struct gpio_desc *desc; |
| 227 | int ret; |
| 228 | |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 229 | desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN); |
| 230 | if (IS_ERR(desc)) |
| 231 | return PTR_ERR(desc); |
| 232 | |
Andy Shevchenko | 8792b0a | 2021-09-29 14:17:57 +0300 | [diff] [blame] | 233 | /* Update default label if no con_id provided */ |
| 234 | if (!con_id) |
| 235 | gpiod_set_consumer_name(desc, ctx->ro_label); |
| 236 | |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 237 | if (debounce) { |
| 238 | ret = gpiod_set_debounce(desc, debounce); |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | } |
| 242 | |
Michał Mirosław | 9073d10 | 2019-12-11 03:40:55 +0100 | [diff] [blame] | 243 | if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH) |
| 244 | gpiod_toggle_active_low(desc); |
| 245 | |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 246 | ctx->ro_gpio = desc; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | EXPORT_SYMBOL(mmc_gpiod_request_ro); |
Masahiro Yamada | 85f9ef8 | 2018-01-18 01:28:05 +0900 | [diff] [blame] | 251 | |
| 252 | bool mmc_can_gpio_ro(struct mmc_host *host) |
| 253 | { |
| 254 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 255 | |
| 256 | return ctx->ro_gpio ? true : false; |
| 257 | } |
| 258 | EXPORT_SYMBOL(mmc_can_gpio_ro); |