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> 13 #include <linux/interrupt.h> 14 #include <linux/jiffies.h> 15 #include <linux/mmc/host.h> 16 #include <linux/mmc/slot-gpio.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 20 struct mmc_gpio { 21 int ro_gpio; 22 int cd_gpio; 23 char *ro_label; 24 char cd_label[0]; 25 }; 26 27 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id) 28 { 29 /* Schedule a card detection after a debounce timeout */ 30 struct mmc_host *host = dev_id; 31 32 if (host->ops->card_event) 33 host->ops->card_event(host); 34 35 mmc_detect_change(host, msecs_to_jiffies(200)); 36 37 return IRQ_HANDLED; 38 } 39 40 static int mmc_gpio_alloc(struct mmc_host *host) 41 { 42 size_t len = strlen(dev_name(host->parent)) + 4; 43 struct mmc_gpio *ctx; 44 45 mutex_lock(&host->slot.lock); 46 47 ctx = host->slot.handler_priv; 48 if (!ctx) { 49 /* 50 * devm_kzalloc() can be called after device_initialize(), even 51 * before device_add(), i.e., between mmc_alloc_host() and 52 * mmc_add_host() 53 */ 54 ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len, 55 GFP_KERNEL); 56 if (ctx) { 57 ctx->ro_label = ctx->cd_label + len; 58 snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent)); 59 snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent)); 60 ctx->cd_gpio = -EINVAL; 61 ctx->ro_gpio = -EINVAL; 62 host->slot.handler_priv = ctx; 63 } 64 } 65 66 mutex_unlock(&host->slot.lock); 67 68 return ctx ? 0 : -ENOMEM; 69 } 70 71 int mmc_gpio_get_ro(struct mmc_host *host) 72 { 73 struct mmc_gpio *ctx = host->slot.handler_priv; 74 75 if (!ctx || !gpio_is_valid(ctx->ro_gpio)) 76 return -ENOSYS; 77 78 return !gpio_get_value_cansleep(ctx->ro_gpio) ^ 79 !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH); 80 } 81 EXPORT_SYMBOL(mmc_gpio_get_ro); 82 83 int mmc_gpio_get_cd(struct mmc_host *host) 84 { 85 struct mmc_gpio *ctx = host->slot.handler_priv; 86 87 if (!ctx || !gpio_is_valid(ctx->cd_gpio)) 88 return -ENOSYS; 89 90 return !gpio_get_value_cansleep(ctx->cd_gpio) ^ 91 !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH); 92 } 93 EXPORT_SYMBOL(mmc_gpio_get_cd); 94 95 int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio) 96 { 97 struct mmc_gpio *ctx; 98 int ret; 99 100 if (!gpio_is_valid(gpio)) 101 return -EINVAL; 102 103 ret = mmc_gpio_alloc(host); 104 if (ret < 0) 105 return ret; 106 107 ctx = host->slot.handler_priv; 108 109 ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->ro_label); 110 if (ret < 0) 111 return ret; 112 113 ctx->ro_gpio = gpio; 114 115 return 0; 116 } 117 EXPORT_SYMBOL(mmc_gpio_request_ro); 118 119 int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio) 120 { 121 struct mmc_gpio *ctx; 122 int irq = gpio_to_irq(gpio); 123 int ret; 124 125 ret = mmc_gpio_alloc(host); 126 if (ret < 0) 127 return ret; 128 129 ctx = host->slot.handler_priv; 130 131 ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label); 132 if (ret < 0) 133 /* 134 * don't bother freeing memory. It might still get used by other 135 * slot functions, in any case it will be freed, when the device 136 * is destroyed. 137 */ 138 return ret; 139 140 /* 141 * Even if gpio_to_irq() returns a valid IRQ number, the platform might 142 * still prefer to poll, e.g., because that IRQ number is already used 143 * by another unit and cannot be shared. 144 */ 145 if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL) 146 irq = -EINVAL; 147 148 if (irq >= 0) { 149 ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt, 150 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 151 ctx->cd_label, host); 152 if (ret < 0) 153 irq = ret; 154 } 155 156 host->slot.cd_irq = irq; 157 158 if (irq < 0) 159 host->caps |= MMC_CAP_NEEDS_POLL; 160 161 ctx->cd_gpio = gpio; 162 163 return 0; 164 } 165 EXPORT_SYMBOL(mmc_gpio_request_cd); 166 167 void mmc_gpio_free_ro(struct mmc_host *host) 168 { 169 struct mmc_gpio *ctx = host->slot.handler_priv; 170 int gpio; 171 172 if (!ctx || !gpio_is_valid(ctx->ro_gpio)) 173 return; 174 175 gpio = ctx->ro_gpio; 176 ctx->ro_gpio = -EINVAL; 177 178 gpio_free(gpio); 179 } 180 EXPORT_SYMBOL(mmc_gpio_free_ro); 181 182 void mmc_gpio_free_cd(struct mmc_host *host) 183 { 184 struct mmc_gpio *ctx = host->slot.handler_priv; 185 int gpio; 186 187 if (!ctx || !gpio_is_valid(ctx->cd_gpio)) 188 return; 189 190 if (host->slot.cd_irq >= 0) { 191 free_irq(host->slot.cd_irq, host); 192 host->slot.cd_irq = -EINVAL; 193 } 194 195 gpio = ctx->cd_gpio; 196 ctx->cd_gpio = -EINVAL; 197 198 gpio_free(gpio); 199 } 200 EXPORT_SYMBOL(mmc_gpio_free_cd); 201