drivers: net: davinci_mdio: implement pm runtime auto mode
[linux-2.6-microblaze.git] / drivers / net / ethernet / ti / davinci_mdio.c
index b6d0059..ce3ec42 100644 (file)
@@ -93,14 +93,16 @@ struct davinci_mdio_data {
        struct clk      *clk;
        struct device   *dev;
        struct mii_bus  *bus;
+       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;
 
@@ -109,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
@@ -132,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);
@@ -148,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);
@@ -163,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;
 }
 
@@ -188,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;
        }
 
@@ -223,6 +238,12 @@ 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;
 
+       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) |
               (phy_id << 16));
 
@@ -246,6 +267,8 @@ static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
                break;
        }
 
+       pm_runtime_mark_last_busy(data->dev);
+       pm_runtime_put_autosuspend(data->dev);
        return ret;
 }
 
@@ -259,6 +282,12 @@ 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;
 
+       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) |
                   (phy_id << 16) | (phy_data & USERACCESS_DATA));
 
@@ -277,7 +306,10 @@ static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
                break;
        }
 
-       return 0;
+       pm_runtime_mark_last_busy(data->dev);
+       pm_runtime_put_autosuspend(data->dev);
+
+       return ret;
 }
 
 #if IS_ENABLED(CONFIG_OF)
@@ -350,8 +382,11 @@ static int davinci_mdio_probe(struct platform_device *pdev)
        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);
-       pm_runtime_get_sync(&pdev->dev);
 
        /* register the mii bus
         * Create PHYs from DT only in case if PHY child nodes are explicitly
@@ -380,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;
 }
 
@@ -393,14 +427,14 @@ 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;
@@ -411,6 +445,30 @@ static int davinci_mdio_suspend(struct device *dev)
        __raw_writel(ctrl, &data->regs->control);
        wait_for_idle(data);
 
+       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);
 
@@ -424,14 +482,16 @@ static int davinci_mdio_resume(struct device *dev)
        /* Select default pin state */
        pinctrl_pm_select_default_state(dev);
 
-       /* restart the scan state machine */
-       __davinci_mdio_reset(data);
+       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)
 };