drivers: net: davinci_mdio: implement pm runtime auto mode
[linux-2.6-microblaze.git] / drivers / net / ethernet / ti / davinci_mdio.c
index 4e7c9b9..ce3ec42 100644 (file)
@@ -90,19 +90,19 @@ static const struct mdio_platform_data default_pdata = {
 struct davinci_mdio_data {
        struct mdio_platform_data pdata;
        struct davinci_mdio_regs __iomem *regs;
-       spinlock_t      lock;
        struct clk      *clk;
        struct device   *dev;
        struct mii_bus  *bus;
-       bool            suspended;
+       bool            active_in_suspend;
        unsigned long   access_time; /* jiffies */
        /* Indicates that driver shouldn't modify phy_mask in case
         * if MDIO bus is registered from DT.
         */
        bool            skip_scan;
+       u32             clk_div;
 };
 
-static void __davinci_mdio_reset(struct davinci_mdio_data *data)
+static void davinci_mdio_init_clk(struct davinci_mdio_data *data)
 {
        u32 mdio_in, div, mdio_out_khz, access_time;
 
@@ -111,9 +111,7 @@ static void __davinci_mdio_reset(struct davinci_mdio_data *data)
        if (div > CONTROL_MAX_DIV)
                div = CONTROL_MAX_DIV;
 
-       /* set enable and clock divider */
-       __raw_writel(div | CONTROL_ENABLE, &data->regs->control);
-
+       data->clk_div = div;
        /*
         * One mdio transaction consists of:
         *      32 bits of preamble
@@ -134,12 +132,23 @@ static void __davinci_mdio_reset(struct davinci_mdio_data *data)
                data->access_time = 1;
 }
 
+static void davinci_mdio_enable(struct davinci_mdio_data *data)
+{
+       /* set enable and clock divider */
+       __raw_writel(data->clk_div | CONTROL_ENABLE, &data->regs->control);
+}
+
 static int davinci_mdio_reset(struct mii_bus *bus)
 {
        struct davinci_mdio_data *data = bus->priv;
        u32 phy_mask, ver;
+       int ret;
 
-       __davinci_mdio_reset(data);
+       ret = pm_runtime_get_sync(data->dev);
+       if (ret < 0) {
+               pm_runtime_put_noidle(data->dev);
+               return ret;
+       }
 
        /* wait for scan logic to settle */
        msleep(PHY_MAX_ADDR * data->access_time);
@@ -150,7 +159,7 @@ static int davinci_mdio_reset(struct mii_bus *bus)
                 (ver >> 8) & 0xff, ver & 0xff);
 
        if (data->skip_scan)
-               return 0;
+               goto done;
 
        /* get phy mask from the alive register */
        phy_mask = __raw_readl(&data->regs->alive);
@@ -165,6 +174,10 @@ static int davinci_mdio_reset(struct mii_bus *bus)
        }
        data->bus->phy_mask = phy_mask;
 
+done:
+       pm_runtime_mark_last_busy(data->dev);
+       pm_runtime_put_autosuspend(data->dev);
+
        return 0;
 }
 
@@ -190,7 +203,7 @@ static inline int wait_for_user_access(struct davinci_mdio_data *data)
                 * operation
                 */
                dev_warn(data->dev, "resetting idled controller\n");
-               __davinci_mdio_reset(data);
+               davinci_mdio_enable(data);
                return -EAGAIN;
        }
 
@@ -225,11 +238,10 @@ static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
        if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
                return -EINVAL;
 
-       spin_lock(&data->lock);
-
-       if (data->suspended) {
-               spin_unlock(&data->lock);
-               return -ENODEV;
+       ret = pm_runtime_get_sync(data->dev);
+       if (ret < 0) {
+               pm_runtime_put_noidle(data->dev);
+               return ret;
        }
 
        reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
@@ -255,8 +267,8 @@ static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
                break;
        }
 
-       spin_unlock(&data->lock);
-
+       pm_runtime_mark_last_busy(data->dev);
+       pm_runtime_put_autosuspend(data->dev);
        return ret;
 }
 
@@ -270,11 +282,10 @@ static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
        if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
                return -EINVAL;
 
-       spin_lock(&data->lock);
-
-       if (data->suspended) {
-               spin_unlock(&data->lock);
-               return -ENODEV;
+       ret = pm_runtime_get_sync(data->dev);
+       if (ret < 0) {
+               pm_runtime_put_noidle(data->dev);
+               return ret;
        }
 
        reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
@@ -295,9 +306,10 @@ static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
                break;
        }
 
-       spin_unlock(&data->lock);
+       pm_runtime_mark_last_busy(data->dev);
+       pm_runtime_put_autosuspend(data->dev);
 
-       return 0;
+       return ret;
 }
 
 #if IS_ENABLED(CONFIG_OF)
@@ -356,26 +368,25 @@ static int davinci_mdio_probe(struct platform_device *pdev)
        data->bus->parent       = dev;
        data->bus->priv         = data;
 
-       pm_runtime_enable(&pdev->dev);
-       pm_runtime_get_sync(&pdev->dev);
        data->clk = devm_clk_get(dev, "fck");
        if (IS_ERR(data->clk)) {
                dev_err(dev, "failed to get device clock\n");
-               ret = PTR_ERR(data->clk);
-               data->clk = NULL;
-               goto bail_out;
+               return PTR_ERR(data->clk);
        }
 
        dev_set_drvdata(dev, data);
        data->dev = dev;
-       spin_lock_init(&data->lock);
 
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        data->regs = devm_ioremap_resource(dev, res);
-       if (IS_ERR(data->regs)) {
-               ret = PTR_ERR(data->regs);
-               goto bail_out;
-       }
+       if (IS_ERR(data->regs))
+               return PTR_ERR(data->regs);
+
+       davinci_mdio_init_clk(data);
+
+       pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
+       pm_runtime_use_autosuspend(&pdev->dev);
+       pm_runtime_enable(&pdev->dev);
 
        /* register the mii bus
         * Create PHYs from DT only in case if PHY child nodes are explicitly
@@ -404,9 +415,8 @@ static int davinci_mdio_probe(struct platform_device *pdev)
        return 0;
 
 bail_out:
-       pm_runtime_put_sync(&pdev->dev);
+       pm_runtime_dont_use_autosuspend(&pdev->dev);
        pm_runtime_disable(&pdev->dev);
-
        return ret;
 }
 
@@ -417,29 +427,47 @@ static int davinci_mdio_remove(struct platform_device *pdev)
        if (data->bus)
                mdiobus_unregister(data->bus);
 
-       pm_runtime_put_sync(&pdev->dev);
+       pm_runtime_dont_use_autosuspend(&pdev->dev);
        pm_runtime_disable(&pdev->dev);
 
        return 0;
 }
 
-#ifdef CONFIG_PM_SLEEP
-static int davinci_mdio_suspend(struct device *dev)
+#ifdef CONFIG_PM
+static int davinci_mdio_runtime_suspend(struct device *dev)
 {
        struct davinci_mdio_data *data = dev_get_drvdata(dev);
        u32 ctrl;
 
-       spin_lock(&data->lock);
-
        /* shutdown the scan state machine */
        ctrl = __raw_readl(&data->regs->control);
        ctrl &= ~CONTROL_ENABLE;
        __raw_writel(ctrl, &data->regs->control);
        wait_for_idle(data);
 
-       data->suspended = true;
-       spin_unlock(&data->lock);
-       pm_runtime_put_sync(data->dev);
+       return 0;
+}
+
+static int davinci_mdio_runtime_resume(struct device *dev)
+{
+       struct davinci_mdio_data *data = dev_get_drvdata(dev);
+
+       davinci_mdio_enable(data);
+       return 0;
+}
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+static int davinci_mdio_suspend(struct device *dev)
+{
+       struct davinci_mdio_data *data = dev_get_drvdata(dev);
+       int ret = 0;
+
+       data->active_in_suspend = !pm_runtime_status_suspended(dev);
+       if (data->active_in_suspend)
+               ret = pm_runtime_force_suspend(dev);
+       if (ret < 0)
+               return ret;
 
        /* Select sleep pin state */
        pinctrl_pm_select_sleep_state(dev);
@@ -454,20 +482,16 @@ static int davinci_mdio_resume(struct device *dev)
        /* Select default pin state */
        pinctrl_pm_select_default_state(dev);
 
-       pm_runtime_get_sync(data->dev);
-
-       spin_lock(&data->lock);
-       /* restart the scan state machine */
-       __davinci_mdio_reset(data);
-
-       data->suspended = false;
-       spin_unlock(&data->lock);
+       if (data->active_in_suspend)
+               pm_runtime_force_resume(dev);
 
        return 0;
 }
 #endif
 
 static const struct dev_pm_ops davinci_mdio_pm_ops = {
+       SET_RUNTIME_PM_OPS(davinci_mdio_runtime_suspend,
+                          davinci_mdio_runtime_resume, NULL)
        SET_LATE_SYSTEM_SLEEP_PM_OPS(davinci_mdio_suspend, davinci_mdio_resume)
 };