#include <linux/io.h>
 #include <linux/gpio.h>
 #include <linux/sfi.h>
+#include <linux/pm_runtime.h>
 
 #include "sdhci.h"
 
 
        int                     pci_bar;
        int                     rst_n_gpio;
+       int                     cd_gpio;
+       int                     cd_irq;
 };
 
 struct sdhci_pci_chip {
        return 0;
 }
 
+#ifdef CONFIG_PM_RUNTIME
+
+static irqreturn_t mfd_sd_cd(int irq, void *dev_id)
+{
+       struct sdhci_pci_slot *slot = dev_id;
+       struct sdhci_host *host = slot->host;
+
+       mmc_detect_change(host->mmc, msecs_to_jiffies(200));
+       return IRQ_HANDLED;
+}
+
+#define MFLD_SD_CD_PIN 69
+
+static int mfd_sd_probe_slot(struct sdhci_pci_slot *slot)
+{
+       int err, irq, gpio = MFLD_SD_CD_PIN;
+
+       slot->cd_gpio = -EINVAL;
+       slot->cd_irq = -EINVAL;
+
+       err = gpio_request(gpio, "sd_cd");
+       if (err < 0)
+               goto out;
+
+       err = gpio_direction_input(gpio);
+       if (err < 0)
+               goto out_free;
+
+       irq = gpio_to_irq(gpio);
+       if (irq < 0)
+               goto out_free;
+
+       err = request_irq(irq, mfd_sd_cd, IRQF_TRIGGER_RISING |
+                         IRQF_TRIGGER_FALLING, "sd_cd", slot);
+       if (err)
+               goto out_free;
+
+       slot->cd_gpio = gpio;
+       slot->cd_irq = irq;
+       slot->host->quirks2 |= SDHCI_QUIRK2_OWN_CARD_DETECTION;
+
+       return 0;
+
+out_free:
+       gpio_free(gpio);
+out:
+       dev_warn(&slot->chip->pdev->dev, "failed to setup card detect wake up\n");
+       return 0;
+}
+
+static void mfd_sd_remove_slot(struct sdhci_pci_slot *slot, int dead)
+{
+       if (slot->cd_irq >= 0)
+               free_irq(slot->cd_irq, slot);
+       gpio_free(slot->cd_gpio);
+}
+
+#else
+
+#define mfd_sd_probe_slot      NULL
+#define mfd_sd_remove_slot     NULL
+
+#endif
+
 static int mfd_emmc_probe_slot(struct sdhci_pci_slot *slot)
 {
        const char *name = NULL;
                slot->host->mmc->caps |= MMC_CAP_HW_RESET;
        }
 
-       slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA;
+       slot->host->mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE;
 
        slot->host->mmc->caps2 = MMC_CAP2_BOOTPART_NOACC;
 
 
 static const struct sdhci_pci_fixes sdhci_intel_mfd_sd = {
        .quirks         = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
+       .probe_slot     = mfd_sd_probe_slot,
+       .remove_slot    = mfd_sd_remove_slot,
 };
 
 static const struct sdhci_pci_fixes sdhci_intel_mfd_sdio = {
 
 #endif /* CONFIG_PM */
 
+#ifdef CONFIG_PM_RUNTIME
+
+static int sdhci_pci_runtime_suspend(struct device *dev)
+{
+       struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+       struct sdhci_pci_chip *chip;
+       struct sdhci_pci_slot *slot;
+       pm_message_t state = { .event = PM_EVENT_SUSPEND };
+       int i, ret;
+
+       chip = pci_get_drvdata(pdev);
+       if (!chip)
+               return 0;
+
+       for (i = 0; i < chip->num_slots; i++) {
+               slot = chip->slots[i];
+               if (!slot)
+                       continue;
+
+               ret = sdhci_runtime_suspend_host(slot->host);
+
+               if (ret) {
+                       for (i--; i >= 0; i--)
+                               sdhci_runtime_resume_host(chip->slots[i]->host);
+                       return ret;
+               }
+       }
+
+       if (chip->fixes && chip->fixes->suspend) {
+               ret = chip->fixes->suspend(chip, state);
+               if (ret) {
+                       for (i = chip->num_slots - 1; i >= 0; i--)
+                               sdhci_runtime_resume_host(chip->slots[i]->host);
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
+static int sdhci_pci_runtime_resume(struct device *dev)
+{
+       struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
+       struct sdhci_pci_chip *chip;
+       struct sdhci_pci_slot *slot;
+       int i, ret;
+
+       chip = pci_get_drvdata(pdev);
+       if (!chip)
+               return 0;
+
+       if (chip->fixes && chip->fixes->resume) {
+               ret = chip->fixes->resume(chip);
+               if (ret)
+                       return ret;
+       }
+
+       for (i = 0; i < chip->num_slots; i++) {
+               slot = chip->slots[i];
+               if (!slot)
+                       continue;
+
+               ret = sdhci_runtime_resume_host(slot->host);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+static int sdhci_pci_runtime_idle(struct device *dev)
+{
+       return 0;
+}
+
+#else
+
+#define sdhci_pci_runtime_suspend      NULL
+#define sdhci_pci_runtime_resume       NULL
+#define sdhci_pci_runtime_idle         NULL
+
+#endif
+
+static const struct dev_pm_ops sdhci_pci_pm_ops = {
+       .runtime_suspend = sdhci_pci_runtime_suspend,
+       .runtime_resume = sdhci_pci_runtime_resume,
+       .runtime_idle = sdhci_pci_runtime_idle,
+};
+
 /*****************************************************************************\
  *                                                                           *
  * Device probing/removal                                                    *
        sdhci_free_host(slot->host);
 }
 
+static void __devinit sdhci_pci_runtime_pm_allow(struct device *dev)
+{
+       pm_runtime_put_noidle(dev);
+       pm_runtime_allow(dev);
+       pm_runtime_set_autosuspend_delay(dev, 50);
+       pm_runtime_use_autosuspend(dev);
+       pm_suspend_ignore_children(dev, 1);
+}
+
+static void __devexit sdhci_pci_runtime_pm_forbid(struct device *dev)
+{
+       pm_runtime_forbid(dev);
+       pm_runtime_get_noresume(dev);
+}
+
 static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
                                     const struct pci_device_id *ent)
 {
                chip->slots[i] = slot;
        }
 
+       sdhci_pci_runtime_pm_allow(&pdev->dev);
+
        return 0;
 
 free:
        int i;
        struct sdhci_pci_chip *chip;
 
+       sdhci_pci_runtime_pm_forbid(&pdev->dev);
+
        chip = pci_get_drvdata(pdev);
 
        if (chip) {
        .remove =       __devexit_p(sdhci_pci_remove),
        .suspend =      sdhci_pci_suspend,
        .resume =       sdhci_pci_resume,
+       .driver =       {
+               .pm =   &sdhci_pci_pm_ops
+       },
 };
 
 /*****************************************************************************\
 
 #include <linux/slab.h>
 #include <linux/scatterlist.h>
 #include <linux/regulator/consumer.h>
+#include <linux/pm_runtime.h>
 
 #include <linux/leds.h>
 
 #define MAX_TUNING_LOOP 40
 
 static unsigned int debug_quirks = 0;
+static unsigned int debug_quirks2;
 
 static void sdhci_finish_data(struct sdhci_host *);
 
 static int sdhci_execute_tuning(struct mmc_host *mmc);
 static void sdhci_tuning_timer(unsigned long data);
 
+#ifdef CONFIG_PM_RUNTIME
+static int sdhci_runtime_pm_get(struct sdhci_host *host);
+static int sdhci_runtime_pm_put(struct sdhci_host *host);
+#else
+static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
+{
+       return 0;
+}
+static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
+{
+       return 0;
+}
+#endif
+
 static void sdhci_dumpregs(struct sdhci_host *host)
 {
        printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
        if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
                return;
 
+       if (host->quirks2 & SDHCI_QUIRK2_OWN_CARD_DETECTION)
+               return;
+
        present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
                              SDHCI_CARD_PRESENT;
        irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
 
        spin_lock_irqsave(&host->lock, flags);
 
+       if (host->runtime_suspended)
+               goto out;
+
        if (brightness == LED_OFF)
                sdhci_deactivate_led(host);
        else
                sdhci_activate_led(host);
-
+out:
        spin_unlock_irqrestore(&host->lock, flags);
 }
 #endif
 
        host = mmc_priv(mmc);
 
+       sdhci_runtime_pm_get(host);
+
        spin_lock_irqsave(&host->lock, flags);
 
        WARN_ON(host->mrq != NULL);
        spin_unlock_irqrestore(&host->lock, flags);
 }
 
-static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
 {
-       struct sdhci_host *host;
        unsigned long flags;
        u8 ctrl;
 
-       host = mmc_priv(mmc);
-
        spin_lock_irqsave(&host->lock, flags);
 
        if (host->flags & SDHCI_DEVICE_DEAD)
        spin_unlock_irqrestore(&host->lock, flags);
 }
 
-static int check_ro(struct sdhci_host *host)
+static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
+{
+       struct sdhci_host *host = mmc_priv(mmc);
+
+       sdhci_runtime_pm_get(host);
+       sdhci_do_set_ios(host, ios);
+       sdhci_runtime_pm_put(host);
+}
+
+static int sdhci_check_ro(struct sdhci_host *host)
 {
        unsigned long flags;
        int is_readonly;
 
 #define SAMPLE_COUNT   5
 
-static int sdhci_get_ro(struct mmc_host *mmc)
+static int sdhci_do_get_ro(struct sdhci_host *host)
 {
-       struct sdhci_host *host;
        int i, ro_count;
 
-       host = mmc_priv(mmc);
-
        if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
-               return check_ro(host);
+               return sdhci_check_ro(host);
 
        ro_count = 0;
        for (i = 0; i < SAMPLE_COUNT; i++) {
-               if (check_ro(host)) {
+               if (sdhci_check_ro(host)) {
                        if (++ro_count > SAMPLE_COUNT / 2)
                                return 1;
                }
                host->ops->hw_reset(host);
 }
 
-static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
+static int sdhci_get_ro(struct mmc_host *mmc)
 {
-       struct sdhci_host *host;
-       unsigned long flags;
-
-       host = mmc_priv(mmc);
+       struct sdhci_host *host = mmc_priv(mmc);
+       int ret;
 
-       spin_lock_irqsave(&host->lock, flags);
+       sdhci_runtime_pm_get(host);
+       ret = sdhci_do_get_ro(host);
+       sdhci_runtime_pm_put(host);
+       return ret;
+}
 
+static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
+{
        if (host->flags & SDHCI_DEVICE_DEAD)
                goto out;
 
+       if (enable)
+               host->flags |= SDHCI_SDIO_IRQ_ENABLED;
+       else
+               host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
+
+       /* SDIO IRQ will be enabled as appropriate in runtime resume */
+       if (host->runtime_suspended)
+               goto out;
+
        if (enable)
                sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
        else
                sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
 out:
        mmiowb();
+}
+
+static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
+{
+       struct sdhci_host *host = mmc_priv(mmc);
+       unsigned long flags;
 
+       spin_lock_irqsave(&host->lock, flags);
+       sdhci_enable_sdio_irq_nolock(host, enable);
        spin_unlock_irqrestore(&host->lock, flags);
 }
 
-static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
-       struct mmc_ios *ios)
+static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
+                                               struct mmc_ios *ios)
 {
-       struct sdhci_host *host;
        u8 pwr;
        u16 clk, ctrl;
        u32 present_state;
 
-       host = mmc_priv(mmc);
-
        /*
         * Signal Voltage Switching is only applicable for Host Controllers
         * v3.00 and above.
                return 0;
 }
 
+static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
+       struct mmc_ios *ios)
+{
+       struct sdhci_host *host = mmc_priv(mmc);
+       int err;
+
+       if (host->version < SDHCI_SPEC_300)
+               return 0;
+       sdhci_runtime_pm_get(host);
+       err = sdhci_do_start_signal_voltage_switch(host, ios);
+       sdhci_runtime_pm_put(host);
+       return err;
+}
+
 static int sdhci_execute_tuning(struct mmc_host *mmc)
 {
        struct sdhci_host *host;
 
        host = mmc_priv(mmc);
 
+       sdhci_runtime_pm_get(host);
        disable_irq(host->irq);
        spin_lock(&host->lock);
 
        else {
                spin_unlock(&host->lock);
                enable_irq(host->irq);
+               sdhci_runtime_pm_put(host);
                return 0;
        }
 
        timeout = 150;
        do {
                struct mmc_command cmd = {0};
-               struct mmc_request mrq = {0};
+               struct mmc_request mrq = {NULL};
 
                if (!tuning_loop_counter && !timeout)
                        break;
        sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
        spin_unlock(&host->lock);
        enable_irq(host->irq);
+       sdhci_runtime_pm_put(host);
 
        return err;
 }
 
-static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
+static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
 {
-       struct sdhci_host *host;
        u16 ctrl;
        unsigned long flags;
 
-       host = mmc_priv(mmc);
-
        /* Host Controller v3.00 defines preset value registers */
        if (host->version < SDHCI_SPEC_300)
                return;
        if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
                ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
                sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
+               host->flags |= SDHCI_PV_ENABLED;
        } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
                ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
                sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
+               host->flags &= ~SDHCI_PV_ENABLED;
        }
 
        spin_unlock_irqrestore(&host->lock, flags);
 }
 
+static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
+{
+       struct sdhci_host *host = mmc_priv(mmc);
+
+       sdhci_runtime_pm_get(host);
+       sdhci_do_enable_preset_value(host, enable);
+       sdhci_runtime_pm_put(host);
+}
+
 static const struct mmc_host_ops sdhci_ops = {
        .request        = sdhci_request,
        .set_ios        = sdhci_set_ios,
 
        spin_lock_irqsave(&host->lock, flags);
 
-       if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
-               if (host->mrq) {
-                       printk(KERN_ERR "%s: Card removed during transfer!\n",
-                               mmc_hostname(host->mmc));
-                       printk(KERN_ERR "%s: Resetting controller.\n",
-                               mmc_hostname(host->mmc));
+       /* Check host->mrq first in case we are runtime suspended */
+       if (host->mrq &&
+           !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
+               printk(KERN_ERR "%s: Card removed during transfer!\n",
+                       mmc_hostname(host->mmc));
+               printk(KERN_ERR "%s: Resetting controller.\n",
+                       mmc_hostname(host->mmc));
 
-                       sdhci_reset(host, SDHCI_RESET_CMD);
-                       sdhci_reset(host, SDHCI_RESET_DATA);
+               sdhci_reset(host, SDHCI_RESET_CMD);
+               sdhci_reset(host, SDHCI_RESET_DATA);
 
-                       host->mrq->cmd->error = -ENOMEDIUM;
-                       tasklet_schedule(&host->finish_tasklet);
-               }
+               host->mrq->cmd->error = -ENOMEDIUM;
+               tasklet_schedule(&host->finish_tasklet);
        }
 
        spin_unlock_irqrestore(&host->lock, flags);
 
        host = (struct sdhci_host*)param;
 
+       spin_lock_irqsave(&host->lock, flags);
+
         /*
          * If this tasklet gets rescheduled while running, it will
          * be run again afterwards but without any active request.
          */
-       if (!host->mrq)
+       if (!host->mrq) {
+               spin_unlock_irqrestore(&host->lock, flags);
                return;
-
-       spin_lock_irqsave(&host->lock, flags);
+       }
 
        del_timer(&host->timer);
 
        spin_unlock_irqrestore(&host->lock, flags);
 
        mmc_request_done(host->mmc, mrq);
+       sdhci_runtime_pm_put(host);
 }
 
 static void sdhci_timeout_timer(unsigned long data)
 static irqreturn_t sdhci_irq(int irq, void *dev_id)
 {
        irqreturn_t result;
-       struct sdhci_host* host = dev_id;
+       struct sdhci_host *host = dev_id;
        u32 intmask;
        int cardint = 0;
 
        spin_lock(&host->lock);
 
+       if (host->runtime_suspended) {
+               spin_unlock(&host->lock);
+               printk(KERN_WARNING "%s: got irq while runtime suspended\n",
+                      mmc_hostname(host->mmc));
+               return IRQ_HANDLED;
+       }
+
        intmask = sdhci_readl(host, SDHCI_INT_STATUS);
 
        if (!intmask || intmask == 0xffffffff) {
                        return ret;
        }
 
-
        if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
                if (host->ops->enable_dma)
                        host->ops->enable_dma(host);
 
 #endif /* CONFIG_PM */
 
+#ifdef CONFIG_PM_RUNTIME
+
+static int sdhci_runtime_pm_get(struct sdhci_host *host)
+{
+       return pm_runtime_get_sync(host->mmc->parent);
+}
+
+static int sdhci_runtime_pm_put(struct sdhci_host *host)
+{
+       pm_runtime_mark_last_busy(host->mmc->parent);
+       return pm_runtime_put_autosuspend(host->mmc->parent);
+}
+
+int sdhci_runtime_suspend_host(struct sdhci_host *host)
+{
+       unsigned long flags;
+       int ret = 0;
+
+       /* Disable tuning since we are suspending */
+       if (host->version >= SDHCI_SPEC_300 &&
+           host->tuning_mode == SDHCI_TUNING_MODE_1) {
+               del_timer_sync(&host->tuning_timer);
+               host->flags &= ~SDHCI_NEEDS_RETUNING;
+       }
+
+       spin_lock_irqsave(&host->lock, flags);
+       sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
+       spin_unlock_irqrestore(&host->lock, flags);
+
+       synchronize_irq(host->irq);
+
+       spin_lock_irqsave(&host->lock, flags);
+       host->runtime_suspended = true;
+       spin_unlock_irqrestore(&host->lock, flags);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
+
+int sdhci_runtime_resume_host(struct sdhci_host *host)
+{
+       unsigned long flags;
+       int ret = 0, host_flags = host->flags;
+
+       if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
+               if (host->ops->enable_dma)
+                       host->ops->enable_dma(host);
+       }
+
+       sdhci_init(host, 0);
+
+       /* Force clock and power re-program */
+       host->pwr = 0;
+       host->clock = 0;
+       sdhci_do_set_ios(host, &host->mmc->ios);
+
+       sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
+       if (host_flags & SDHCI_PV_ENABLED)
+               sdhci_do_enable_preset_value(host, true);
+
+       /* Set the re-tuning expiration flag */
+       if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
+           (host->tuning_mode == SDHCI_TUNING_MODE_1))
+               host->flags |= SDHCI_NEEDS_RETUNING;
+
+       spin_lock_irqsave(&host->lock, flags);
+
+       host->runtime_suspended = false;
+
+       /* Enable SDIO IRQ */
+       if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
+               sdhci_enable_sdio_irq_nolock(host, true);
+
+       /* Enable Card Detection */
+       sdhci_enable_card_detection(host);
+
+       spin_unlock_irqrestore(&host->lock, flags);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
+
+#endif
+
 /*****************************************************************************\
  *                                                                           *
  * Device allocation/registration                                            *
 
        if (debug_quirks)
                host->quirks = debug_quirks;
+       if (debug_quirks2)
+               host->quirks2 = debug_quirks2;
 
        sdhci_reset(host, SDHCI_RESET_ALL);
 
 module_exit(sdhci_drv_exit);
 
 module_param(debug_quirks, uint, 0444);
+module_param(debug_quirks2, uint, 0444);
 
 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
 MODULE_LICENSE("GPL");
 
 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
+MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");