| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Generic GPIO card-detect helper |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 10 | 7 | |
|---|
| 11 | 8 | #include <linux/err.h> |
|---|
| 12 | | -#include <linux/gpio.h> |
|---|
| 13 | 9 | #include <linux/gpio/consumer.h> |
|---|
| 14 | 10 | #include <linux/interrupt.h> |
|---|
| 15 | 11 | #include <linux/jiffies.h> |
|---|
| .. | .. |
|---|
| 18 | 14 | #include <linux/module.h> |
|---|
| 19 | 15 | #include <linux/slab.h> |
|---|
| 20 | 16 | |
|---|
| 17 | +#include <trace/hooks/mmc_core.h> |
|---|
| 18 | + |
|---|
| 21 | 19 | #include "slot-gpio.h" |
|---|
| 22 | 20 | |
|---|
| 23 | 21 | struct mmc_gpio { |
|---|
| 24 | 22 | struct gpio_desc *ro_gpio; |
|---|
| 25 | 23 | struct gpio_desc *cd_gpio; |
|---|
| 26 | | - bool override_ro_active_level; |
|---|
| 27 | | - bool override_cd_active_level; |
|---|
| 28 | 24 | irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id); |
|---|
| 29 | 25 | char *ro_label; |
|---|
| 26 | + char *cd_label; |
|---|
| 30 | 27 | u32 cd_debounce_delay_ms; |
|---|
| 31 | | - char cd_label[]; |
|---|
| 32 | 28 | }; |
|---|
| 33 | 29 | |
|---|
| 34 | 30 | static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) |
|---|
| .. | .. |
|---|
| 36 | 32 | /* Schedule a card detection after a debounce timeout */ |
|---|
| 37 | 33 | struct mmc_host *host = dev_id; |
|---|
| 38 | 34 | struct mmc_gpio *ctx = host->slot.handler_priv; |
|---|
| 35 | + bool allow = true; |
|---|
| 36 | + |
|---|
| 37 | + trace_android_vh_mmc_gpio_cd_irqt(host, &allow); |
|---|
| 38 | + if (!allow) |
|---|
| 39 | + return IRQ_HANDLED; |
|---|
| 39 | 40 | |
|---|
| 40 | 41 | host->trigger_card_event = true; |
|---|
| 41 | 42 | mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms)); |
|---|
| .. | .. |
|---|
| 45 | 46 | |
|---|
| 46 | 47 | int mmc_gpio_alloc(struct mmc_host *host) |
|---|
| 47 | 48 | { |
|---|
| 48 | | - size_t len = strlen(dev_name(host->parent)) + 4; |
|---|
| 49 | 49 | struct mmc_gpio *ctx = devm_kzalloc(host->parent, |
|---|
| 50 | | - sizeof(*ctx) + 2 * len, GFP_KERNEL); |
|---|
| 50 | + sizeof(*ctx), GFP_KERNEL); |
|---|
| 51 | 51 | |
|---|
| 52 | 52 | if (ctx) { |
|---|
| 53 | | - ctx->ro_label = ctx->cd_label + len; |
|---|
| 54 | 53 | ctx->cd_debounce_delay_ms = 200; |
|---|
| 55 | | - snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent)); |
|---|
| 56 | | - snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent)); |
|---|
| 54 | + ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, |
|---|
| 55 | + "%s cd", dev_name(host->parent)); |
|---|
| 56 | + if (!ctx->cd_label) |
|---|
| 57 | + return -ENOMEM; |
|---|
| 58 | + ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, |
|---|
| 59 | + "%s ro", dev_name(host->parent)); |
|---|
| 60 | + if (!ctx->ro_label) |
|---|
| 61 | + return -ENOMEM; |
|---|
| 57 | 62 | host->slot.handler_priv = ctx; |
|---|
| 58 | 63 | host->slot.cd_irq = -EINVAL; |
|---|
| 59 | 64 | } |
|---|
| .. | .. |
|---|
| 68 | 73 | if (!ctx || !ctx->ro_gpio) |
|---|
| 69 | 74 | return -ENOSYS; |
|---|
| 70 | 75 | |
|---|
| 71 | | - if (ctx->override_ro_active_level) |
|---|
| 72 | | - return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^ |
|---|
| 73 | | - !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH); |
|---|
| 74 | | - |
|---|
| 75 | 76 | return gpiod_get_value_cansleep(ctx->ro_gpio); |
|---|
| 76 | 77 | } |
|---|
| 77 | 78 | EXPORT_SYMBOL(mmc_gpio_get_ro); |
|---|
| .. | .. |
|---|
| 85 | 86 | return -ENOSYS; |
|---|
| 86 | 87 | |
|---|
| 87 | 88 | cansleep = gpiod_cansleep(ctx->cd_gpio); |
|---|
| 88 | | - if (ctx->override_cd_active_level) { |
|---|
| 89 | | - int value = cansleep ? |
|---|
| 90 | | - gpiod_get_raw_value_cansleep(ctx->cd_gpio) : |
|---|
| 91 | | - gpiod_get_raw_value(ctx->cd_gpio); |
|---|
| 92 | | - return !value ^ !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH); |
|---|
| 93 | | - } |
|---|
| 94 | | - |
|---|
| 95 | 89 | return cansleep ? |
|---|
| 96 | 90 | gpiod_get_value_cansleep(ctx->cd_gpio) : |
|---|
| 97 | 91 | gpiod_get_value(ctx->cd_gpio); |
|---|
| 98 | 92 | } |
|---|
| 99 | 93 | EXPORT_SYMBOL(mmc_gpio_get_cd); |
|---|
| 100 | | - |
|---|
| 101 | | -/** |
|---|
| 102 | | - * mmc_gpio_request_ro - request a gpio for write-protection |
|---|
| 103 | | - * @host: mmc host |
|---|
| 104 | | - * @gpio: gpio number requested |
|---|
| 105 | | - * |
|---|
| 106 | | - * As devm_* managed functions are used in mmc_gpio_request_ro(), client |
|---|
| 107 | | - * drivers do not need to worry about freeing up memory. |
|---|
| 108 | | - * |
|---|
| 109 | | - * Returns zero on success, else an error. |
|---|
| 110 | | - */ |
|---|
| 111 | | -int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio) |
|---|
| 112 | | -{ |
|---|
| 113 | | - struct mmc_gpio *ctx = host->slot.handler_priv; |
|---|
| 114 | | - int ret; |
|---|
| 115 | | - |
|---|
| 116 | | - if (!gpio_is_valid(gpio)) |
|---|
| 117 | | - return -EINVAL; |
|---|
| 118 | | - |
|---|
| 119 | | - ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN, |
|---|
| 120 | | - ctx->ro_label); |
|---|
| 121 | | - if (ret < 0) |
|---|
| 122 | | - return ret; |
|---|
| 123 | | - |
|---|
| 124 | | - ctx->override_ro_active_level = true; |
|---|
| 125 | | - ctx->ro_gpio = gpio_to_desc(gpio); |
|---|
| 126 | | - |
|---|
| 127 | | - return 0; |
|---|
| 128 | | -} |
|---|
| 129 | | -EXPORT_SYMBOL(mmc_gpio_request_ro); |
|---|
| 130 | 94 | |
|---|
| 131 | 95 | void mmc_gpiod_request_cd_irq(struct mmc_host *host) |
|---|
| 132 | 96 | { |
|---|
| .. | .. |
|---|
| 197 | 161 | EXPORT_SYMBOL(mmc_gpio_set_cd_isr); |
|---|
| 198 | 162 | |
|---|
| 199 | 163 | /** |
|---|
| 200 | | - * mmc_gpio_request_cd - request a gpio for card-detection |
|---|
| 201 | | - * @host: mmc host |
|---|
| 202 | | - * @gpio: gpio number requested |
|---|
| 203 | | - * @debounce: debounce time in microseconds |
|---|
| 204 | | - * |
|---|
| 205 | | - * As devm_* managed functions are used in mmc_gpio_request_cd(), client |
|---|
| 206 | | - * drivers do not need to worry about freeing up memory. |
|---|
| 207 | | - * |
|---|
| 208 | | - * If GPIO debouncing is desired, set the debounce parameter to a non-zero |
|---|
| 209 | | - * value. The caller is responsible for ensuring that the GPIO driver associated |
|---|
| 210 | | - * with the GPIO supports debouncing, otherwise an error will be returned. |
|---|
| 211 | | - * |
|---|
| 212 | | - * Returns zero on success, else an error. |
|---|
| 213 | | - */ |
|---|
| 214 | | -int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio, |
|---|
| 215 | | - unsigned int debounce) |
|---|
| 216 | | -{ |
|---|
| 217 | | - struct mmc_gpio *ctx = host->slot.handler_priv; |
|---|
| 218 | | - int ret; |
|---|
| 219 | | - |
|---|
| 220 | | - ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN, |
|---|
| 221 | | - ctx->cd_label); |
|---|
| 222 | | - if (ret < 0) |
|---|
| 223 | | - /* |
|---|
| 224 | | - * don't bother freeing memory. It might still get used by other |
|---|
| 225 | | - * slot functions, in any case it will be freed, when the device |
|---|
| 226 | | - * is destroyed. |
|---|
| 227 | | - */ |
|---|
| 228 | | - return ret; |
|---|
| 229 | | - |
|---|
| 230 | | - if (debounce) { |
|---|
| 231 | | - ret = gpio_set_debounce(gpio, debounce); |
|---|
| 232 | | - if (ret < 0) |
|---|
| 233 | | - return ret; |
|---|
| 234 | | - } |
|---|
| 235 | | - |
|---|
| 236 | | - ctx->override_cd_active_level = true; |
|---|
| 237 | | - ctx->cd_gpio = gpio_to_desc(gpio); |
|---|
| 238 | | - |
|---|
| 239 | | - return 0; |
|---|
| 240 | | -} |
|---|
| 241 | | -EXPORT_SYMBOL(mmc_gpio_request_cd); |
|---|
| 242 | | - |
|---|
| 243 | | -/** |
|---|
| 244 | 164 | * mmc_gpiod_request_cd - request a gpio descriptor for card-detection |
|---|
| 245 | 165 | * @host: mmc host |
|---|
| 246 | 166 | * @con_id: function within the GPIO consumer |
|---|
| 247 | 167 | * @idx: index of the GPIO to obtain in the consumer |
|---|
| 248 | 168 | * @override_active_level: ignore %GPIO_ACTIVE_LOW flag |
|---|
| 249 | 169 | * @debounce: debounce time in microseconds |
|---|
| 250 | | - * @gpio_invert: will return whether the GPIO line is inverted or not, set |
|---|
| 251 | | - * to NULL to ignore |
|---|
| 252 | 170 | * |
|---|
| 253 | | - * Use this function in place of mmc_gpio_request_cd() to use the GPIO |
|---|
| 254 | | - * descriptor API. Note that it must be called prior to mmc_add_host() |
|---|
| 171 | + * Note that this must be called prior to mmc_add_host() |
|---|
| 255 | 172 | * otherwise the caller must also call mmc_gpiod_request_cd_irq(). |
|---|
| 256 | 173 | * |
|---|
| 257 | 174 | * Returns zero on success, else an error. |
|---|
| 258 | 175 | */ |
|---|
| 259 | 176 | int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id, |
|---|
| 260 | 177 | unsigned int idx, bool override_active_level, |
|---|
| 261 | | - unsigned int debounce, bool *gpio_invert) |
|---|
| 178 | + unsigned int debounce) |
|---|
| 262 | 179 | { |
|---|
| 263 | 180 | struct mmc_gpio *ctx = host->slot.handler_priv; |
|---|
| 264 | 181 | struct gpio_desc *desc; |
|---|
| .. | .. |
|---|
| 274 | 191 | ctx->cd_debounce_delay_ms = debounce / 1000; |
|---|
| 275 | 192 | } |
|---|
| 276 | 193 | |
|---|
| 277 | | - if (gpio_invert) |
|---|
| 278 | | - *gpio_invert = !gpiod_is_active_low(desc); |
|---|
| 194 | + /* override forces default (active-low) polarity ... */ |
|---|
| 195 | + if (override_active_level && !gpiod_is_active_low(desc)) |
|---|
| 196 | + gpiod_toggle_active_low(desc); |
|---|
| 279 | 197 | |
|---|
| 280 | | - ctx->override_cd_active_level = override_active_level; |
|---|
| 198 | + /* ... or active-high */ |
|---|
| 199 | + if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) |
|---|
| 200 | + gpiod_toggle_active_low(desc); |
|---|
| 201 | + |
|---|
| 281 | 202 | ctx->cd_gpio = desc; |
|---|
| 282 | 203 | |
|---|
| 283 | 204 | return 0; |
|---|
| .. | .. |
|---|
| 297 | 218 | * @host: mmc host |
|---|
| 298 | 219 | * @con_id: function within the GPIO consumer |
|---|
| 299 | 220 | * @idx: index of the GPIO to obtain in the consumer |
|---|
| 300 | | - * @override_active_level: ignore %GPIO_ACTIVE_LOW flag |
|---|
| 301 | 221 | * @debounce: debounce time in microseconds |
|---|
| 302 | | - * @gpio_invert: will return whether the GPIO line is inverted or not, |
|---|
| 303 | | - * set to NULL to ignore |
|---|
| 304 | | - * |
|---|
| 305 | | - * Use this function in place of mmc_gpio_request_ro() to use the GPIO |
|---|
| 306 | | - * descriptor API. |
|---|
| 307 | 222 | * |
|---|
| 308 | 223 | * Returns zero on success, else an error. |
|---|
| 309 | 224 | */ |
|---|
| 310 | 225 | int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id, |
|---|
| 311 | | - unsigned int idx, bool override_active_level, |
|---|
| 312 | | - unsigned int debounce, bool *gpio_invert) |
|---|
| 226 | + unsigned int idx, unsigned int debounce) |
|---|
| 313 | 227 | { |
|---|
| 314 | 228 | struct mmc_gpio *ctx = host->slot.handler_priv; |
|---|
| 315 | 229 | struct gpio_desc *desc; |
|---|
| .. | .. |
|---|
| 325 | 239 | return ret; |
|---|
| 326 | 240 | } |
|---|
| 327 | 241 | |
|---|
| 328 | | - if (gpio_invert) |
|---|
| 329 | | - *gpio_invert = !gpiod_is_active_low(desc); |
|---|
| 242 | + if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH) |
|---|
| 243 | + gpiod_toggle_active_low(desc); |
|---|
| 330 | 244 | |
|---|
| 331 | | - ctx->override_ro_active_level = override_active_level; |
|---|
| 332 | 245 | ctx->ro_gpio = desc; |
|---|
| 333 | 246 | |
|---|
| 334 | 247 | return 0; |
|---|