123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /*
- * Generic GPIO card-detect helper
- *
- * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #include <linux/err.h>
- #include <linux/gpio.h>
- #include <linux/interrupt.h>
- #include <linux/jiffies.h>
- #include <linux/mmc/cd-gpio.h>
- #include <linux/mmc/host.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- struct mmc_cd_gpio {
- unsigned int gpio;
- bool status;
- char label[0];
- };
- static int mmc_cd_get_status(struct mmc_host *host)
- {
- int ret = -ENOSYS;
- struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
- if (!cd || !gpio_is_valid(cd->gpio))
- goto out;
- ret = !gpio_get_value_cansleep(cd->gpio) ^
- !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
- out:
- return ret;
- }
- static irqreturn_t mmc_cd_gpio_irqt(int irq, void *dev_id)
- {
- struct mmc_host *host = dev_id;
- struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
- int status;
- status = mmc_cd_get_status(host);
- if (unlikely(status < 0))
- goto out;
- if (status ^ cd->status) {
- pr_info("%s: slot status change detected (%d -> %d), GPIO_ACTIVE_%s\n",
- mmc_hostname(host), cd->status, status,
- (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) ?
- "HIGH" : "LOW");
- cd->status = status;
- /* Schedule a card detection after a debounce timeout */
- mmc_detect_change(host, msecs_to_jiffies(100));
- }
- out:
- return IRQ_HANDLED;
- }
- int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio)
- {
- size_t len = strlen(dev_name(host->parent)) + 4;
- struct mmc_cd_gpio *cd;
- int irq = gpio_to_irq(gpio);
- int ret;
- if (irq < 0)
- return irq;
- cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL);
- if (!cd) {
- host->hotplug.handler_priv = NULL;
- host->hotplug.irq = 0;
- return -ENOMEM;
- }
- snprintf(cd->label, len, "%s cd", dev_name(host->parent));
- ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label);
- if (ret < 0)
- goto egpioreq;
- cd->gpio = gpio;
- host->hotplug.irq = irq;
- host->hotplug.handler_priv = cd;
- ret = mmc_cd_get_status(host);
- if (ret < 0)
- goto eirqreq;
- cd->status = ret;
- ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt,
- IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
- cd->label, host);
- if (ret < 0)
- goto eirqreq;
- return 0;
- eirqreq:
- host->hotplug.handler_priv = NULL;
- host->hotplug.irq = 0;
- gpio_free(gpio);
- egpioreq:
- kfree(cd);
- return ret;
- }
- EXPORT_SYMBOL(mmc_cd_gpio_request);
- void mmc_cd_gpio_free(struct mmc_host *host)
- {
- struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
- if (!cd || !gpio_is_valid(cd->gpio))
- return;
- free_irq(host->hotplug.irq, host);
- gpio_free(cd->gpio);
- cd->gpio = -EINVAL;
- kfree(cd);
- host->hotplug.handler_priv = NULL;
-
- }
- EXPORT_SYMBOL(mmc_cd_gpio_free);
|