clk: mediatek: pll: Implement error handling in register API
authorChen-Yu Tsai <wenst@chromium.org>
Tue, 8 Feb 2022 12:40:28 +0000 (20:40 +0800)
committerStephen Boyd <sboyd@kernel.org>
Thu, 17 Feb 2022 20:12:24 +0000 (12:12 -0800)
The pll clk type registration function does not stop or return errors
if any clk failed to be registered, nor does it implement an error
handling path. This may result in a partially working device if any
step failed.

Make the register function return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, and unmap the I/O space, is done in the new
error path.

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Reviewed-by: Miles Chen <miles.chen@mediatek.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Link: https://lore.kernel.org/r/20220208124034.414635-26-wenst@chromium.org
Reviewed-by: Chun-Jie Chen <chun-jie.chen@mediatek.com>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
drivers/clk/mediatek/clk-pll.c
drivers/clk/mediatek/clk-pll.h

index 8439d37e354dcff34ca612befabdb322cfac6be4..817a80293bfc25ae84cb4d8a5bbb962d629dcdaa 100644 (file)
@@ -377,8 +377,9 @@ static void mtk_clk_unregister_pll(struct clk *clk)
        kfree(pll);
 }
 
-void mtk_clk_register_plls(struct device_node *node,
-               const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
+int mtk_clk_register_plls(struct device_node *node,
+                         const struct mtk_pll_data *plls, int num_plls,
+                         struct clk_onecell_data *clk_data)
 {
        void __iomem *base;
        int i;
@@ -387,7 +388,7 @@ void mtk_clk_register_plls(struct device_node *node,
        base = of_iomap(node, 0);
        if (!base) {
                pr_err("%s(): ioremap failed\n", __func__);
-               return;
+               return -EINVAL;
        }
 
        for (i = 0; i < num_plls; i++) {
@@ -397,11 +398,25 @@ void mtk_clk_register_plls(struct device_node *node,
 
                if (IS_ERR(clk)) {
                        pr_err("Failed to register clk %s: %pe\n", pll->name, clk);
-                       continue;
+                       goto err;
                }
 
                clk_data->clks[pll->id] = clk;
        }
+
+       return 0;
+
+err:
+       while (--i >= 0) {
+               const struct mtk_pll_data *pll = &plls[i];
+
+               mtk_clk_unregister_pll(clk_data->clks[pll->id]);
+               clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
+       }
+
+       iounmap(base);
+
+       return PTR_ERR(clk);
 }
 EXPORT_SYMBOL_GPL(mtk_clk_register_plls);
 
index a889b1e472e7cdbb41d3aefdfc79d5a2f661449d..bf06e44caef93cec1eff406027aa1cdf60eaa0e0 100644 (file)
@@ -48,9 +48,9 @@ struct mtk_pll_data {
        u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
 };
 
-void mtk_clk_register_plls(struct device_node *node,
-                          const struct mtk_pll_data *plls, int num_plls,
-                          struct clk_onecell_data *clk_data);
+int mtk_clk_register_plls(struct device_node *node,
+                         const struct mtk_pll_data *plls, int num_plls,
+                         struct clk_onecell_data *clk_data);
 void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
                             struct clk_onecell_data *clk_data);