Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-ti-pipe3.c
index 465de2c800f228d7fa4961083e243349f5fbcd33..95c88f929f27f7d4113961e90215f3e769fe75f5 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/delay.h>
 #include <linux/phy/omap_control_phy.h>
 #include <linux/of_platform.h>
+#include <linux/spinlock.h>
 
 #define        PLL_STATUS              0x00000004
 #define        PLL_GO                  0x00000008
@@ -82,6 +83,10 @@ struct ti_pipe3 {
        struct clk              *refclk;
        struct clk              *div_clk;
        struct pipe3_dpll_map   *dpll_map;
+       bool                    enabled;
+       spinlock_t              lock;   /* serialize clock enable/disable */
+       /* the below flag is needed specifically for SATA */
+       bool                    refclk_enabled;
 };
 
 static struct pipe3_dpll_map dpll_map_usb[] = {
@@ -307,6 +312,7 @@ static int ti_pipe3_probe(struct platform_device *pdev)
                return -ENOMEM;
 
        phy->dev                = &pdev->dev;
+       spin_lock_init(&phy->lock);
 
        if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
                match = of_match_device(of_match_ptr(ti_pipe3_id_table),
@@ -333,21 +339,24 @@ static int ti_pipe3_probe(struct platform_device *pdev)
                }
        }
 
+       phy->refclk = devm_clk_get(phy->dev, "refclk");
+       if (IS_ERR(phy->refclk)) {
+               dev_err(&pdev->dev, "unable to get refclk\n");
+               /* older DTBs have missing refclk in SATA PHY
+                * so don't bail out in case of SATA PHY.
+                */
+               if (!of_device_is_compatible(node, "ti,phy-pipe3-sata"))
+                       return PTR_ERR(phy->refclk);
+       }
+
        if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
                phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
                if (IS_ERR(phy->wkupclk)) {
                        dev_err(&pdev->dev, "unable to get wkupclk\n");
                        return PTR_ERR(phy->wkupclk);
                }
-
-               phy->refclk = devm_clk_get(phy->dev, "refclk");
-               if (IS_ERR(phy->refclk)) {
-                       dev_err(&pdev->dev, "unable to get refclk\n");
-                       return PTR_ERR(phy->refclk);
-               }
        } else {
                phy->wkupclk = ERR_PTR(-ENODEV);
-               phy->refclk = ERR_PTR(-ENODEV);
        }
 
        if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
@@ -426,33 +435,42 @@ static int ti_pipe3_remove(struct platform_device *pdev)
 }
 
 #ifdef CONFIG_PM
-
-static int ti_pipe3_runtime_suspend(struct device *dev)
+static int ti_pipe3_enable_refclk(struct ti_pipe3 *phy)
 {
-       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+       if (!IS_ERR(phy->refclk) && !phy->refclk_enabled) {
+               int ret;
 
-       if (!IS_ERR(phy->wkupclk))
-               clk_disable_unprepare(phy->wkupclk);
+               ret = clk_prepare_enable(phy->refclk);
+               if (ret) {
+                       dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
+                       return ret;
+               }
+               phy->refclk_enabled = true;
+       }
+
+       return 0;
+}
+
+static void ti_pipe3_disable_refclk(struct ti_pipe3 *phy)
+{
        if (!IS_ERR(phy->refclk))
                clk_disable_unprepare(phy->refclk);
-       if (!IS_ERR(phy->div_clk))
-               clk_disable_unprepare(phy->div_clk);
 
-       return 0;
+       phy->refclk_enabled = false;
 }
 
-static int ti_pipe3_runtime_resume(struct device *dev)
+static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
 {
-       u32 ret = 0;
-       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+       int ret = 0;
+       unsigned long flags;
 
-       if (!IS_ERR(phy->refclk)) {
-               ret = clk_prepare_enable(phy->refclk);
-               if (ret) {
-                       dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
-                       goto err1;
-               }
-       }
+       spin_lock_irqsave(&phy->lock, flags);
+       if (phy->enabled)
+               goto err1;
+
+       ret = ti_pipe3_enable_refclk(phy);
+       if (ret)
+               goto err1;
 
        if (!IS_ERR(phy->wkupclk)) {
                ret = clk_prepare_enable(phy->wkupclk);
@@ -469,6 +487,9 @@ static int ti_pipe3_runtime_resume(struct device *dev)
                        goto err3;
                }
        }
+
+       phy->enabled = true;
+       spin_unlock_irqrestore(&phy->lock, flags);
        return 0;
 
 err3:
@@ -479,20 +500,80 @@ err2:
        if (!IS_ERR(phy->refclk))
                clk_disable_unprepare(phy->refclk);
 
+       ti_pipe3_disable_refclk(phy);
 err1:
+       spin_unlock_irqrestore(&phy->lock, flags);
        return ret;
 }
 
+static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&phy->lock, flags);
+       if (!phy->enabled) {
+               spin_unlock_irqrestore(&phy->lock, flags);
+               return;
+       }
+
+       if (!IS_ERR(phy->wkupclk))
+               clk_disable_unprepare(phy->wkupclk);
+       /* Don't disable refclk for SATA PHY due to Errata i783 */
+       if (!of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata"))
+               ti_pipe3_disable_refclk(phy);
+       if (!IS_ERR(phy->div_clk))
+               clk_disable_unprepare(phy->div_clk);
+       phy->enabled = false;
+       spin_unlock_irqrestore(&phy->lock, flags);
+}
+
+static int ti_pipe3_runtime_suspend(struct device *dev)
+{
+       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+
+       ti_pipe3_disable_clocks(phy);
+       return 0;
+}
+
+static int ti_pipe3_runtime_resume(struct device *dev)
+{
+       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+       int ret = 0;
+
+       ret = ti_pipe3_enable_clocks(phy);
+       return ret;
+}
+
+static int ti_pipe3_suspend(struct device *dev)
+{
+       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+
+       ti_pipe3_disable_clocks(phy);
+       return 0;
+}
+
+static int ti_pipe3_resume(struct device *dev)
+{
+       struct ti_pipe3 *phy = dev_get_drvdata(dev);
+       int ret;
+
+       ret = ti_pipe3_enable_clocks(phy);
+       if (ret)
+               return ret;
+
+       pm_runtime_disable(dev);
+       pm_runtime_set_active(dev);
+       pm_runtime_enable(dev);
+       return 0;
+}
+#endif
+
 static const struct dev_pm_ops ti_pipe3_pm_ops = {
        SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend,
                           ti_pipe3_runtime_resume, NULL)
+       SET_SYSTEM_SLEEP_PM_OPS(ti_pipe3_suspend, ti_pipe3_resume)
 };
 
-#define DEV_PM_OPS     (&ti_pipe3_pm_ops)
-#else
-#define DEV_PM_OPS     NULL
-#endif
-
 #ifdef CONFIG_OF
 static const struct of_device_id ti_pipe3_id_table[] = {
        {
@@ -520,7 +601,7 @@ static struct platform_driver ti_pipe3_driver = {
        .remove         = ti_pipe3_remove,
        .driver         = {
                .name   = "ti-pipe3",
-               .pm     = DEV_PM_OPS,
+               .pm     = &ti_pipe3_pm_ops,
                .of_match_table = of_match_ptr(ti_pipe3_id_table),
        },
 };