Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[linux-drm-fsl-dcu.git] / drivers / mmc / core / sd.c
index 5e8823dc3ef613b70e24ff3d6e16c143da2976a2..6f42050b7ccc6d829d6b58ece04b1177e73cbe80 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <linux/stat.h>
+#include <linux/pm_runtime.h>
 
 #include <linux/mmc/host.h>
 #include <linux/mmc/card.h>
@@ -721,6 +722,7 @@ int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
        int err;
        u32 max_current;
        int retries = 10;
+       u32 pocr = ocr;
 
 try_again:
        if (!retries) {
@@ -773,7 +775,8 @@ try_again:
         */
        if (!mmc_host_is_spi(host) && rocr &&
           ((*rocr & 0x41000000) == 0x41000000)) {
-               err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
+               err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
+                                       pocr);
                if (err == -EAGAIN) {
                        retries--;
                        goto try_again;
@@ -935,6 +938,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
                if (IS_ERR(card))
                        return PTR_ERR(card);
 
+               card->ocr = ocr;
                card->type = MMC_TYPE_SD;
                memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
        }
@@ -1064,10 +1068,7 @@ static void mmc_sd_detect(struct mmc_host *host)
        }
 }
 
-/*
- * Suspend callback from host.
- */
-static int mmc_sd_suspend(struct mmc_host *host)
+static int _mmc_sd_suspend(struct mmc_host *host)
 {
        int err = 0;
 
@@ -1075,34 +1076,77 @@ static int mmc_sd_suspend(struct mmc_host *host)
        BUG_ON(!host->card);
 
        mmc_claim_host(host);
+
+       if (mmc_card_suspended(host->card))
+               goto out;
+
        if (!mmc_host_is_spi(host))
                err = mmc_deselect_cards(host);
        host->card->state &= ~MMC_STATE_HIGHSPEED;
-       if (!err)
+       if (!err) {
                mmc_power_off(host);
+               mmc_card_set_suspended(host->card);
+       }
+
+out:
        mmc_release_host(host);
+       return err;
+}
+
+/*
+ * Callback for suspend
+ */
+static int mmc_sd_suspend(struct mmc_host *host)
+{
+       int err;
+
+       err = _mmc_sd_suspend(host);
+       if (!err) {
+               pm_runtime_disable(&host->card->dev);
+               pm_runtime_set_suspended(&host->card->dev);
+       }
 
        return err;
 }
 
 /*
- * Resume callback from host.
- *
  * This function tries to determine if the same card is still present
  * and, if so, restore all state to it.
  */
-static int mmc_sd_resume(struct mmc_host *host)
+static int _mmc_sd_resume(struct mmc_host *host)
 {
-       int err;
+       int err = 0;
 
        BUG_ON(!host);
        BUG_ON(!host->card);
 
        mmc_claim_host(host);
-       mmc_power_up(host);
-       mmc_select_voltage(host, host->ocr);
-       err = mmc_sd_init_card(host, host->ocr, host->card);
+
+       if (!mmc_card_suspended(host->card))
+               goto out;
+
+       mmc_power_up(host, host->card->ocr);
+       err = mmc_sd_init_card(host, host->card->ocr, host->card);
+       mmc_card_clr_suspended(host->card);
+
+out:
        mmc_release_host(host);
+       return err;
+}
+
+/*
+ * Callback for resume
+ */
+static int mmc_sd_resume(struct mmc_host *host)
+{
+       int err = 0;
+
+       if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
+               err = _mmc_sd_resume(host);
+               pm_runtime_set_active(&host->card->dev);
+               pm_runtime_mark_last_busy(&host->card->dev);
+       }
+       pm_runtime_enable(&host->card->dev);
 
        return err;
 }
@@ -1117,18 +1161,11 @@ static int mmc_sd_runtime_suspend(struct mmc_host *host)
        if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
                return 0;
 
-       mmc_claim_host(host);
-
-       err = mmc_sd_suspend(host);
-       if (err) {
+       err = _mmc_sd_suspend(host);
+       if (err)
                pr_err("%s: error %d doing aggessive suspend\n",
                        mmc_hostname(host), err);
-               goto out;
-       }
-       mmc_power_off(host);
 
-out:
-       mmc_release_host(host);
        return err;
 }
 
@@ -1139,18 +1176,14 @@ static int mmc_sd_runtime_resume(struct mmc_host *host)
 {
        int err;
 
-       if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
+       if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
                return 0;
 
-       mmc_claim_host(host);
-
-       mmc_power_up(host);
-       err = mmc_sd_resume(host);
+       err = _mmc_sd_resume(host);
        if (err)
                pr_err("%s: error %d doing aggessive resume\n",
                        mmc_hostname(host), err);
 
-       mmc_release_host(host);
        return 0;
 }
 
@@ -1160,7 +1193,7 @@ static int mmc_sd_power_restore(struct mmc_host *host)
 
        host->card->state &= ~MMC_STATE_HIGHSPEED;
        mmc_claim_host(host);
-       ret = mmc_sd_init_card(host, host->ocr, host->card);
+       ret = mmc_sd_init_card(host, host->card->ocr, host->card);
        mmc_release_host(host);
 
        return ret;
@@ -1205,7 +1238,7 @@ static void mmc_sd_attach_bus_ops(struct mmc_host *host)
 int mmc_attach_sd(struct mmc_host *host)
 {
        int err;
-       u32 ocr;
+       u32 ocr, rocr;
 
        BUG_ON(!host);
        WARN_ON(!host->claimed);
@@ -1229,31 +1262,12 @@ int mmc_attach_sd(struct mmc_host *host)
                        goto err;
        }
 
-       /*
-        * Sanity check the voltages that the card claims to
-        * support.
-        */
-       if (ocr & 0x7F) {
-               pr_warning("%s: card claims to support voltages "
-                      "below the defined range. These will be ignored.\n",
-                      mmc_hostname(host));
-               ocr &= ~0x7F;
-       }
-
-       if ((ocr & MMC_VDD_165_195) &&
-           !(host->ocr_avail_sd & MMC_VDD_165_195)) {
-               pr_warning("%s: SD card claims to support the "
-                      "incompletely defined 'low voltage range'. This "
-                      "will be ignored.\n", mmc_hostname(host));
-               ocr &= ~MMC_VDD_165_195;
-       }
-
-       host->ocr = mmc_select_voltage(host, ocr);
+       rocr = mmc_select_voltage(host, ocr);
 
        /*
         * Can we support the voltage(s) of the card(s)?
         */
-       if (!host->ocr) {
+       if (!rocr) {
                err = -EINVAL;
                goto err;
        }
@@ -1261,7 +1275,7 @@ int mmc_attach_sd(struct mmc_host *host)
        /*
         * Detect and init the card.
         */
-       err = mmc_sd_init_card(host, host->ocr, NULL);
+       err = mmc_sd_init_card(host, rocr, NULL);
        if (err)
                goto err;