0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/mmc/host.h>
0013 #include <linux/mmc/slot-gpio.h>
0014 #include <linux/module.h>
0015 #include <linux/slab.h>
0016
0017 #include "slot-gpio.h"
0018
0019 struct mmc_gpio {
0020 struct gpio_desc *ro_gpio;
0021 struct gpio_desc *cd_gpio;
0022 irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
0023 char *ro_label;
0024 char *cd_label;
0025 u32 cd_debounce_delay_ms;
0026 };
0027
0028 static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
0029 {
0030
0031 struct mmc_host *host = dev_id;
0032 struct mmc_gpio *ctx = host->slot.handler_priv;
0033
0034 host->trigger_card_event = true;
0035 mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
0036
0037 return IRQ_HANDLED;
0038 }
0039
0040 int mmc_gpio_alloc(struct mmc_host *host)
0041 {
0042 const char *devname = dev_name(host->parent);
0043 struct mmc_gpio *ctx;
0044
0045 ctx = devm_kzalloc(host->parent, sizeof(*ctx), GFP_KERNEL);
0046 if (!ctx)
0047 return -ENOMEM;
0048
0049 ctx->cd_debounce_delay_ms = 200;
0050 ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s cd", devname);
0051 if (!ctx->cd_label)
0052 return -ENOMEM;
0053 ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s ro", devname);
0054 if (!ctx->ro_label)
0055 return -ENOMEM;
0056 host->slot.handler_priv = ctx;
0057 host->slot.cd_irq = -EINVAL;
0058
0059 return 0;
0060 }
0061
0062 int mmc_gpio_get_ro(struct mmc_host *host)
0063 {
0064 struct mmc_gpio *ctx = host->slot.handler_priv;
0065
0066 if (!ctx || !ctx->ro_gpio)
0067 return -ENOSYS;
0068
0069 return gpiod_get_value_cansleep(ctx->ro_gpio);
0070 }
0071 EXPORT_SYMBOL(mmc_gpio_get_ro);
0072
0073 int mmc_gpio_get_cd(struct mmc_host *host)
0074 {
0075 struct mmc_gpio *ctx = host->slot.handler_priv;
0076 int cansleep;
0077
0078 if (!ctx || !ctx->cd_gpio)
0079 return -ENOSYS;
0080
0081 cansleep = gpiod_cansleep(ctx->cd_gpio);
0082 return cansleep ?
0083 gpiod_get_value_cansleep(ctx->cd_gpio) :
0084 gpiod_get_value(ctx->cd_gpio);
0085 }
0086 EXPORT_SYMBOL(mmc_gpio_get_cd);
0087
0088 void mmc_gpiod_request_cd_irq(struct mmc_host *host)
0089 {
0090 struct mmc_gpio *ctx = host->slot.handler_priv;
0091 int irq = -EINVAL;
0092 int ret;
0093
0094 if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
0095 return;
0096
0097
0098
0099
0100
0101 if (!(host->caps & MMC_CAP_NEEDS_POLL))
0102 irq = gpiod_to_irq(ctx->cd_gpio);
0103
0104 if (irq >= 0) {
0105 if (!ctx->cd_gpio_isr)
0106 ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
0107 ret = devm_request_threaded_irq(host->parent, irq,
0108 NULL, ctx->cd_gpio_isr,
0109 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0110 ctx->cd_label, host);
0111 if (ret < 0)
0112 irq = ret;
0113 }
0114
0115 host->slot.cd_irq = irq;
0116
0117 if (irq < 0)
0118 host->caps |= MMC_CAP_NEEDS_POLL;
0119 }
0120 EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
0121
0122 int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
0123 {
0124 int ret = 0;
0125
0126 if (!(host->caps & MMC_CAP_CD_WAKE) ||
0127 host->slot.cd_irq < 0 ||
0128 on == host->slot.cd_wake_enabled)
0129 return 0;
0130
0131 if (on) {
0132 ret = enable_irq_wake(host->slot.cd_irq);
0133 host->slot.cd_wake_enabled = !ret;
0134 } else {
0135 disable_irq_wake(host->slot.cd_irq);
0136 host->slot.cd_wake_enabled = false;
0137 }
0138
0139 return ret;
0140 }
0141 EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
0142
0143
0144
0145
0146 void mmc_gpio_set_cd_isr(struct mmc_host *host,
0147 irqreturn_t (*isr)(int irq, void *dev_id))
0148 {
0149 struct mmc_gpio *ctx = host->slot.handler_priv;
0150
0151 WARN_ON(ctx->cd_gpio_isr);
0152 ctx->cd_gpio_isr = isr;
0153 }
0154 EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
0170 unsigned int idx, bool override_active_level,
0171 unsigned int debounce)
0172 {
0173 struct mmc_gpio *ctx = host->slot.handler_priv;
0174 struct gpio_desc *desc;
0175 int ret;
0176
0177 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
0178 if (IS_ERR(desc))
0179 return PTR_ERR(desc);
0180
0181
0182 if (!con_id)
0183 gpiod_set_consumer_name(desc, ctx->cd_label);
0184
0185 if (debounce) {
0186 ret = gpiod_set_debounce(desc, debounce);
0187 if (ret < 0)
0188 ctx->cd_debounce_delay_ms = debounce / 1000;
0189 }
0190
0191
0192 if (override_active_level && !gpiod_is_active_low(desc))
0193 gpiod_toggle_active_low(desc);
0194
0195
0196 if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
0197 gpiod_toggle_active_low(desc);
0198
0199 ctx->cd_gpio = desc;
0200
0201 return 0;
0202 }
0203 EXPORT_SYMBOL(mmc_gpiod_request_cd);
0204
0205 bool mmc_can_gpio_cd(struct mmc_host *host)
0206 {
0207 struct mmc_gpio *ctx = host->slot.handler_priv;
0208
0209 return ctx->cd_gpio ? true : false;
0210 }
0211 EXPORT_SYMBOL(mmc_can_gpio_cd);
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
0223 unsigned int idx, unsigned int debounce)
0224 {
0225 struct mmc_gpio *ctx = host->slot.handler_priv;
0226 struct gpio_desc *desc;
0227 int ret;
0228
0229 desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
0230 if (IS_ERR(desc))
0231 return PTR_ERR(desc);
0232
0233
0234 if (!con_id)
0235 gpiod_set_consumer_name(desc, ctx->ro_label);
0236
0237 if (debounce) {
0238 ret = gpiod_set_debounce(desc, debounce);
0239 if (ret < 0)
0240 return ret;
0241 }
0242
0243 if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
0244 gpiod_toggle_active_low(desc);
0245
0246 ctx->ro_gpio = desc;
0247
0248 return 0;
0249 }
0250 EXPORT_SYMBOL(mmc_gpiod_request_ro);
0251
0252 bool mmc_can_gpio_ro(struct mmc_host *host)
0253 {
0254 struct mmc_gpio *ctx = host->slot.handler_priv;
0255
0256 return ctx->ro_gpio ? true : false;
0257 }
0258 EXPORT_SYMBOL(mmc_can_gpio_ro);