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