Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic GPIO card-detect helper |
| 3 | * |
| 4 | * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de> |
| 5 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/gpio.h> |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 13 | #include <linux/gpio/consumer.h> |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 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> |
| 20 | |
Ulf Hansson | 7f133de | 2014-12-18 15:44:34 +0100 | [diff] [blame] | 21 | #include "slot-gpio.h" |
| 22 | |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 23 | struct mmc_gpio { |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 24 | struct gpio_desc *ro_gpio; |
| 25 | struct gpio_desc *cd_gpio; |
| 26 | bool override_ro_active_level; |
| 27 | bool override_cd_active_level; |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame^] | 28 | irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id); |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 29 | char *ro_label; |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 30 | char cd_label[0]; |
| 31 | }; |
| 32 | |
| 33 | static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) |
| 34 | { |
| 35 | /* Schedule a card detection after a debounce timeout */ |
Guennadi Liakhovetski | 451c895 | 2012-12-04 16:51:36 +0100 | [diff] [blame] | 36 | struct mmc_host *host = dev_id; |
| 37 | |
Markus Mayer | fa372a5 | 2014-04-08 15:19:43 -0700 | [diff] [blame] | 38 | host->trigger_card_event = true; |
Guennadi Liakhovetski | 451c895 | 2012-12-04 16:51:36 +0100 | [diff] [blame] | 39 | mmc_detect_change(host, msecs_to_jiffies(200)); |
| 40 | |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 41 | return IRQ_HANDLED; |
| 42 | } |
| 43 | |
Ulf Hansson | 7f133de | 2014-12-18 15:44:34 +0100 | [diff] [blame] | 44 | int mmc_gpio_alloc(struct mmc_host *host) |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 45 | { |
| 46 | size_t len = strlen(dev_name(host->parent)) + 4; |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 47 | struct mmc_gpio *ctx = devm_kzalloc(host->parent, |
| 48 | sizeof(*ctx) + 2 * len, GFP_KERNEL); |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 49 | |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 50 | if (ctx) { |
| 51 | ctx->ro_label = ctx->cd_label + len; |
| 52 | snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent)); |
| 53 | snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent)); |
| 54 | host->slot.handler_priv = ctx; |
| 55 | host->slot.cd_irq = -EINVAL; |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 58 | return ctx ? 0 : -ENOMEM; |
| 59 | } |
| 60 | |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 61 | int mmc_gpio_get_ro(struct mmc_host *host) |
| 62 | { |
| 63 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 64 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 65 | if (!ctx || !ctx->ro_gpio) |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 66 | return -ENOSYS; |
| 67 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 68 | if (ctx->override_ro_active_level) |
| 69 | return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^ |
| 70 | !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH); |
| 71 | |
| 72 | return gpiod_get_value_cansleep(ctx->ro_gpio); |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL(mmc_gpio_get_ro); |
| 75 | |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 76 | int mmc_gpio_get_cd(struct mmc_host *host) |
| 77 | { |
| 78 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 79 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 80 | if (!ctx || !ctx->cd_gpio) |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 81 | return -ENOSYS; |
| 82 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 83 | if (ctx->override_cd_active_level) |
| 84 | return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^ |
| 85 | !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH); |
| 86 | |
| 87 | return gpiod_get_value_cansleep(ctx->cd_gpio); |
Guennadi Liakhovetski | befe404 | 2012-05-01 16:27:25 +0200 | [diff] [blame] | 88 | } |
| 89 | EXPORT_SYMBOL(mmc_gpio_get_cd); |
| 90 | |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 91 | /** |
| 92 | * mmc_gpio_request_ro - request a gpio for write-protection |
| 93 | * @host: mmc host |
| 94 | * @gpio: gpio number requested |
| 95 | * |
| 96 | * As devm_* managed functions are used in mmc_gpio_request_ro(), client |
Ulf Hansson | eddbc3a | 2014-12-18 15:44:32 +0100 | [diff] [blame] | 97 | * drivers do not need to worry about freeing up memory. |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 98 | * |
| 99 | * Returns zero on success, else an error. |
| 100 | */ |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 101 | int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio) |
| 102 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 103 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | if (!gpio_is_valid(gpio)) |
| 107 | return -EINVAL; |
| 108 | |
Ulf Hansson | b4cc580 | 2014-12-18 15:44:33 +0100 | [diff] [blame] | 109 | ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN, |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 110 | ctx->ro_label); |
Chris Ball | 15e8a8e | 2012-09-09 22:56:48 -0400 | [diff] [blame] | 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 114 | ctx->override_ro_active_level = true; |
| 115 | ctx->ro_gpio = gpio_to_desc(gpio); |
Chris Ball | 15e8a8e | 2012-09-09 22:56:48 -0400 | [diff] [blame] | 116 | |
| 117 | return 0; |
Guennadi Liakhovetski | 5aa7dad | 2012-05-01 16:59:38 +0200 | [diff] [blame] | 118 | } |
| 119 | EXPORT_SYMBOL(mmc_gpio_request_ro); |
| 120 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 121 | void mmc_gpiod_request_cd_irq(struct mmc_host *host) |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 122 | { |
| 123 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 124 | int ret, irq; |
| 125 | |
| 126 | if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio) |
| 127 | return; |
| 128 | |
| 129 | irq = gpiod_to_irq(ctx->cd_gpio); |
| 130 | |
| 131 | /* |
| 132 | * Even if gpiod_to_irq() returns a valid IRQ number, the platform might |
| 133 | * still prefer to poll, e.g., because that IRQ number is already used |
| 134 | * by another unit and cannot be shared. |
| 135 | */ |
| 136 | if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL) |
| 137 | irq = -EINVAL; |
| 138 | |
| 139 | if (irq >= 0) { |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame^] | 140 | if (!ctx->cd_gpio_isr) |
| 141 | ctx->cd_gpio_isr = mmc_gpio_cd_irqt; |
Ulf Hansson | b4cc580 | 2014-12-18 15:44:33 +0100 | [diff] [blame] | 142 | ret = devm_request_threaded_irq(host->parent, irq, |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame^] | 143 | NULL, ctx->cd_gpio_isr, |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 144 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 145 | ctx->cd_label, host); |
| 146 | if (ret < 0) |
| 147 | irq = ret; |
| 148 | } |
| 149 | |
| 150 | host->slot.cd_irq = irq; |
| 151 | |
| 152 | if (irq < 0) |
| 153 | host->caps |= MMC_CAP_NEEDS_POLL; |
| 154 | } |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 155 | EXPORT_SYMBOL(mmc_gpiod_request_cd_irq); |
Adrian Hunter | 2665267 | 2014-03-10 15:02:40 +0200 | [diff] [blame] | 156 | |
NeilBrown | c7ea834 | 2015-01-13 08:23:18 +1300 | [diff] [blame^] | 157 | /* Register an alternate interrupt service routine for |
| 158 | * the card-detect GPIO. |
| 159 | */ |
| 160 | void mmc_gpio_set_cd_isr(struct mmc_host *host, |
| 161 | irqreturn_t (*isr)(int irq, void *dev_id)) |
| 162 | { |
| 163 | struct mmc_gpio *ctx = host->slot.handler_priv; |
| 164 | |
| 165 | WARN_ON(ctx->cd_gpio_isr); |
| 166 | ctx->cd_gpio_isr = isr; |
| 167 | } |
| 168 | EXPORT_SYMBOL(mmc_gpio_set_cd_isr); |
| 169 | |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 170 | /** |
| 171 | * mmc_gpio_request_cd - request a gpio for card-detection |
| 172 | * @host: mmc host |
| 173 | * @gpio: gpio number requested |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 174 | * @debounce: debounce time in microseconds |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 175 | * |
| 176 | * As devm_* managed functions are used in mmc_gpio_request_cd(), client |
Ulf Hansson | eddbc3a | 2014-12-18 15:44:32 +0100 | [diff] [blame] | 177 | * drivers do not need to worry about freeing up memory. |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 178 | * |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 179 | * If GPIO debouncing is desired, set the debounce parameter to a non-zero |
| 180 | * value. The caller is responsible for ensuring that the GPIO driver associated |
| 181 | * with the GPIO supports debouncing, otherwise an error will be returned. |
| 182 | * |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 183 | * Returns zero on success, else an error. |
| 184 | */ |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 185 | int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio, |
| 186 | unsigned int debounce) |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 187 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 188 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 189 | int ret; |
| 190 | |
Ulf Hansson | b4cc580 | 2014-12-18 15:44:33 +0100 | [diff] [blame] | 191 | ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN, |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 192 | ctx->cd_label); |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 193 | if (ret < 0) |
Guennadi Liakhovetski | a7d1a1e | 2012-05-01 16:51:38 +0200 | [diff] [blame] | 194 | /* |
| 195 | * don't bother freeing memory. It might still get used by other |
| 196 | * slot functions, in any case it will be freed, when the device |
| 197 | * is destroyed. |
| 198 | */ |
| 199 | return ret; |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 200 | |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 201 | if (debounce) { |
| 202 | ret = gpio_set_debounce(gpio, debounce); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | } |
| 206 | |
Adrian Hunter | 842f4bd | 2014-03-10 15:02:39 +0200 | [diff] [blame] | 207 | ctx->override_cd_active_level = true; |
| 208 | ctx->cd_gpio = gpio_to_desc(gpio); |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 209 | |
| 210 | return 0; |
Guennadi Liakhovetski | fd0ea65 | 2012-04-30 23:31:57 +0200 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL(mmc_gpio_request_cd); |
| 213 | |
Shawn Guo | d65b5ae | 2012-12-11 22:32:18 +0800 | [diff] [blame] | 214 | /** |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 215 | * mmc_gpiod_request_cd - request a gpio descriptor for card-detection |
| 216 | * @host: mmc host |
| 217 | * @con_id: function within the GPIO consumer |
| 218 | * @idx: index of the GPIO to obtain in the consumer |
| 219 | * @override_active_level: ignore %GPIO_ACTIVE_LOW flag |
| 220 | * @debounce: debounce time in microseconds |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 221 | * @gpio_invert: will return whether the GPIO line is inverted or not, set |
| 222 | * to NULL to ignore |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 223 | * |
| 224 | * Use this function in place of mmc_gpio_request_cd() to use the GPIO |
Ulf Hansson | eddbc3a | 2014-12-18 15:44:32 +0100 | [diff] [blame] | 225 | * descriptor API. Note that it must be called prior to mmc_add_host() |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 226 | * otherwise the caller must also call mmc_gpiod_request_cd_irq(). |
| 227 | * |
| 228 | * Returns zero on success, else an error. |
| 229 | */ |
| 230 | int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id, |
| 231 | unsigned int idx, bool override_active_level, |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 232 | unsigned int debounce, bool *gpio_invert) |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 233 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 234 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 235 | struct gpio_desc *desc; |
| 236 | int ret; |
| 237 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 238 | if (!con_id) |
| 239 | con_id = ctx->cd_label; |
| 240 | |
Linus Walleij | 9fbc695 | 2014-08-27 13:00:50 +0200 | [diff] [blame] | 241 | desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN); |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 242 | if (IS_ERR(desc)) |
| 243 | return PTR_ERR(desc); |
| 244 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 245 | if (debounce) { |
| 246 | ret = gpiod_set_debounce(desc, debounce); |
| 247 | if (ret < 0) |
| 248 | return ret; |
| 249 | } |
| 250 | |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 251 | if (gpio_invert) |
| 252 | *gpio_invert = !gpiod_is_active_low(desc); |
| 253 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 254 | ctx->override_cd_active_level = override_active_level; |
| 255 | ctx->cd_gpio = desc; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | EXPORT_SYMBOL(mmc_gpiod_request_cd); |
| 260 | |
| 261 | /** |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 262 | * mmc_gpiod_request_ro - request a gpio descriptor for write protection |
| 263 | * @host: mmc host |
| 264 | * @con_id: function within the GPIO consumer |
| 265 | * @idx: index of the GPIO to obtain in the consumer |
| 266 | * @override_active_level: ignore %GPIO_ACTIVE_LOW flag |
| 267 | * @debounce: debounce time in microseconds |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 268 | * @gpio_invert: will return whether the GPIO line is inverted or not, |
| 269 | * set to NULL to ignore |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 270 | * |
| 271 | * Use this function in place of mmc_gpio_request_ro() to use the GPIO |
Ulf Hansson | eddbc3a | 2014-12-18 15:44:32 +0100 | [diff] [blame] | 272 | * descriptor API. |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 273 | * |
| 274 | * Returns zero on success, else an error. |
| 275 | */ |
| 276 | int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id, |
| 277 | unsigned int idx, bool override_active_level, |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 278 | unsigned int debounce, bool *gpio_invert) |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 279 | { |
Ulf Hansson | df8aca1 | 2014-12-18 15:44:36 +0100 | [diff] [blame] | 280 | struct mmc_gpio *ctx = host->slot.handler_priv; |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 281 | struct gpio_desc *desc; |
| 282 | int ret; |
| 283 | |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 284 | if (!con_id) |
| 285 | con_id = ctx->ro_label; |
| 286 | |
| 287 | desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN); |
| 288 | if (IS_ERR(desc)) |
| 289 | return PTR_ERR(desc); |
| 290 | |
| 291 | if (debounce) { |
| 292 | ret = gpiod_set_debounce(desc, debounce); |
| 293 | if (ret < 0) |
| 294 | return ret; |
| 295 | } |
| 296 | |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 297 | if (gpio_invert) |
| 298 | *gpio_invert = !gpiod_is_active_low(desc); |
| 299 | |
Linus Walleij | 9d2fa24 | 2014-08-27 13:00:51 +0200 | [diff] [blame] | 300 | ctx->override_ro_active_level = override_active_level; |
| 301 | ctx->ro_gpio = desc; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | EXPORT_SYMBOL(mmc_gpiod_request_ro); |