clk: imx: scu: add suspend/resume support
authorDong Aisheng <aisheng.dong@nxp.com>
Wed, 29 Jul 2020 08:00:14 +0000 (16:00 +0800)
committerShawn Guo <shawnguo@kernel.org>
Mon, 26 Oct 2020 02:53:57 +0000 (10:53 +0800)
Clock state will be lost when its power domain is completely off
during system suspend/resume. So we save and restore the state
accordingly in suspend/resume callback.

Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <kernel@pengutronix.de>
Cc: Michael Turquette <mturquette@baylibre.com>
Reviewed-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Dong Aisheng <aisheng.dong@nxp.com>
Signed-off-by: Shawn Guo <shawnguo@kernel.org>
drivers/clk/imx/clk-scu.c

index f5dc6731a2178189280b09466beded58489528d5..229a290ca5b6848f013068da0dabcdcc17f73345 100644 (file)
@@ -45,6 +45,10 @@ struct clk_scu {
        struct clk_hw hw;
        u16 rsrc_id;
        u8 clk_type;
+
+       /* for state save&restore */
+       bool is_enabled;
+       u32 rate;
 };
 
 /*
@@ -430,6 +434,9 @@ struct clk_hw *__imx_clk_scu(struct device *dev, const char *name,
                hw = ERR_PTR(ret);
        }
 
+       if (dev)
+               dev_set_drvdata(dev, clk);
+
        return hw;
 }
 
@@ -486,10 +493,52 @@ static int imx_clk_scu_probe(struct platform_device *pdev)
        return 0;
 }
 
+static int __maybe_unused imx_clk_scu_suspend(struct device *dev)
+{
+       struct clk_scu *clk = dev_get_drvdata(dev);
+
+       clk->rate = clk_hw_get_rate(&clk->hw);
+       clk->is_enabled = clk_hw_is_enabled(&clk->hw);
+
+       if (clk->rate)
+               dev_dbg(dev, "save rate %d\n", clk->rate);
+
+       if (clk->is_enabled)
+               dev_dbg(dev, "save enabled state\n");
+
+       return 0;
+}
+
+static int __maybe_unused imx_clk_scu_resume(struct device *dev)
+{
+       struct clk_scu *clk = dev_get_drvdata(dev);
+       int ret = 0;
+
+       if (clk->rate) {
+               ret = clk_scu_set_rate(&clk->hw, clk->rate, 0);
+               dev_dbg(dev, "restore rate %d %s\n", clk->rate,
+                       !ret ? "success" : "failed");
+       }
+
+       if (clk->is_enabled) {
+               ret = clk_scu_prepare(&clk->hw);
+               dev_dbg(dev, "restore enabled state %s\n",
+                       !ret ? "success" : "failed");
+       }
+
+       return ret;
+}
+
+static const struct dev_pm_ops imx_clk_scu_pm_ops = {
+       SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_clk_scu_suspend,
+                                     imx_clk_scu_resume)
+};
+
 static struct platform_driver imx_clk_scu_driver = {
        .driver = {
                .name = "imx-scu-clk",
                .suppress_bind_attrs = true,
+               .pm = &imx_clk_scu_pm_ops,
        },
        .probe = imx_clk_scu_probe,
 };