Merge branches 'msm-fixes' and 'msm-video' of git://codeaurora.org/quic/kernel/dwalke...
[pandora-kernel.git] / drivers / mmc / core / sdio_bus.c
index 4a890dc..2716c7a 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/slab.h>
+#include <linux/pm_runtime.h>
 
 #include <linux/mmc/card.h>
 #include <linux/mmc/sdio_func.h>
@@ -125,21 +126,46 @@ static int sdio_bus_probe(struct device *dev)
        if (!id)
                return -ENODEV;
 
+       /* Unbound SDIO functions are always suspended.
+        * During probe, the function is set active and the usage count
+        * is incremented.  If the driver supports runtime PM,
+        * it should call pm_runtime_put_noidle() in its probe routine and
+        * pm_runtime_get_noresume() in its remove routine.
+        */
+       ret = pm_runtime_get_sync(dev);
+       if (ret < 0)
+               goto out;
+
        /* Set the default block size so the driver is sure it's something
         * sensible. */
        sdio_claim_host(func);
        ret = sdio_set_block_size(func, 0);
        sdio_release_host(func);
        if (ret)
-               return ret;
+               goto disable_runtimepm;
+
+       ret = drv->probe(func, id);
+       if (ret)
+               goto disable_runtimepm;
 
-       return drv->probe(func, id);
+       return 0;
+
+disable_runtimepm:
+       pm_runtime_put_noidle(dev);
+out:
+       return ret;
 }
 
 static int sdio_bus_remove(struct device *dev)
 {
        struct sdio_driver *drv = to_sdio_driver(dev->driver);
        struct sdio_func *func = dev_to_sdio_func(dev);
+       int ret;
+
+       /* Make sure card is powered before invoking ->remove() */
+       ret = pm_runtime_get_sync(dev);
+       if (ret < 0)
+               goto out;
 
        drv->remove(func);
 
@@ -151,9 +177,63 @@ static int sdio_bus_remove(struct device *dev)
                sdio_release_host(func);
        }
 
+       /* First, undo the increment made directly above */
+       pm_runtime_put_noidle(dev);
+
+       /* Then undo the runtime PM settings in sdio_bus_probe() */
+       pm_runtime_put_noidle(dev);
+
+out:
+       return ret;
+}
+
+#ifdef CONFIG_PM_RUNTIME
+
+static int sdio_bus_pm_prepare(struct device *dev)
+{
+       /*
+        * Resume an SDIO device which was suspended at run time at this
+        * point, in order to allow standard SDIO suspend/resume paths
+        * to keep working as usual.
+        *
+        * Ultimately, the SDIO driver itself will decide (in its
+        * suspend handler, or lack thereof) whether the card should be
+        * removed or kept, and if kept, at what power state.
+        *
+        * At this point, PM core have increased our use count, so it's
+        * safe to directly resume the device. After system is resumed
+        * again, PM core will drop back its runtime PM use count, and if
+        * needed device will be suspended again.
+        *
+        * The end result is guaranteed to be a power state that is
+        * coherent with the device's runtime PM use count.
+        *
+        * The return value of pm_runtime_resume is deliberately unchecked
+        * since there is little point in failing system suspend if a
+        * device can't be resumed.
+        */
+       pm_runtime_resume(dev);
+
        return 0;
 }
 
+static const struct dev_pm_ops sdio_bus_pm_ops = {
+       SET_RUNTIME_PM_OPS(
+               pm_generic_runtime_suspend,
+               pm_generic_runtime_resume,
+               pm_generic_runtime_idle
+       )
+       .prepare = sdio_bus_pm_prepare,
+};
+
+#define SDIO_PM_OPS_PTR        (&sdio_bus_pm_ops)
+
+#else /* !CONFIG_PM_RUNTIME */
+
+#define SDIO_PM_OPS_PTR        NULL
+
+#endif /* !CONFIG_PM_RUNTIME */
+
 static struct bus_type sdio_bus_type = {
        .name           = "sdio",
        .dev_attrs      = sdio_dev_attrs,
@@ -161,6 +241,7 @@ static struct bus_type sdio_bus_type = {
        .uevent         = sdio_bus_uevent,
        .probe          = sdio_bus_probe,
        .remove         = sdio_bus_remove,
+       .pm             = SDIO_PM_OPS_PTR,
 };
 
 int sdio_register_bus(void)