[PATCH v2 14/22] phy: cadence-torrent: rework ref clock init and probe cleanup

From: Benjamin Robin

Date: Wed Dec 02 2026 - 10:11:02 EST


Rework the reference clock setup and simplify the probe error handling.

Reference clock initialization is factored into tp_ref_clk_init(),
which enables a single ref clock and validates its rate, called twice
from tp_ref_clks_init(). The helper takes the clock name ("main" or
"PLL1") so it can be reported in error messages. A ref_clks_initialized
flag tracks whether the clocks are currently enabled, letting
tp_ref_clks_uninit() disable them only when they were previously set up.

The ref clocks are now initialized only after the phy subnodes have been
parsed successfully, since they are not needed when no subnode is
present. This also sets up a later step, since the set of allowed
reference clock frequencies will depend on the selected standards.

All the probe error paths are folded into a single cleanup label that
calls tp_cleanup_remove(), which asserts the APB reset and uninitializes
the ref clocks. cdns_torrent_phy_remove() reuses the same helper.

Finally, the APB reset is now deasserted before tp_is_already_configured()
reads the registers, so the check runs on a block that is not held in
reset.

Signed-off-by: Benjamin Robin <benjamin.robin@xxxxxxxxxxx>
---
drivers/phy/cadence/phy-cadence-torrent.c | 182 ++++++++++++++----------------
1 file changed, 87 insertions(+), 95 deletions(-)

diff --git a/drivers/phy/cadence/phy-cadence-torrent.c b/drivers/phy/cadence/phy-cadence-torrent.c
index 69e5323b1d68..de4ff3a133ff 100644
--- a/drivers/phy/cadence/phy-cadence-torrent.c
+++ b/drivers/phy/cadence/phy-cadence-torrent.c
@@ -363,11 +363,13 @@ struct tp_data {
struct clk *clk1;
enum cdns_torrent_ref_clk ref_clk_rate;
enum cdns_torrent_ref_clk ref_clk1_rate;
+
+ bool already_configured;
+ bool ref_clks_initialized;
u8 mask_used_lanes;

struct tp_link links[MAX_NUM_LANES];
int nb_links;
- int already_configured;
const struct cdns_torrent_data *init_data;

struct tp_reg_base reg_common_cdb;
@@ -2422,89 +2424,86 @@ static int tp_of_get_clk(struct tp_data *tp_data)
return 0;
}

-static int cdns_torrent_clk(struct tp_data *tp_data)
+static int tp_ref_clk_init(const struct tp_data *tp_data, struct clk *clk, const char *name,
+ enum cdns_torrent_ref_clk *p_ref_clk_rate)
{
- unsigned long ref_clk1_rate;
- unsigned long ref_clk_rate;
+ struct device *dev = tp_data->dev;
+ u32 ref_clk_rate;
int ret;

- ret = clk_prepare_enable(tp_data->clk);
+ ret = clk_prepare_enable(clk);
if (ret) {
- dev_err(tp_data->dev, "Failed to prepare ref clock: %d\n", ret);
+ dev_err(dev, "Failed to prepare %s ref clock: %d\n", name, ret);
return ret;
}

- ref_clk_rate = clk_get_rate(tp_data->clk);
+ ref_clk_rate = clk_get_rate(clk);
if (!ref_clk_rate) {
- dev_err(tp_data->dev, "Failed to get ref clock rate\n");
+ dev_err(dev, "Failed to get %s ref clock rate\n", name);
ret = -EINVAL;
goto disable_clk;
}

switch (ref_clk_rate) {
case REF_CLK_19_2MHZ:
- tp_data->ref_clk_rate = CLK_19_2_MHZ;
+ *p_ref_clk_rate = CLK_19_2_MHZ;
break;
case REF_CLK_25MHZ:
- tp_data->ref_clk_rate = CLK_25_MHZ;
+ *p_ref_clk_rate = CLK_25_MHZ;
break;
case REF_CLK_100MHZ:
- tp_data->ref_clk_rate = CLK_100_MHZ;
+ *p_ref_clk_rate = CLK_100_MHZ;
break;
case REF_CLK_156_25MHZ:
- tp_data->ref_clk_rate = CLK_156_25_MHZ;
+ *p_ref_clk_rate = CLK_156_25_MHZ;
break;
default:
- dev_err(tp_data->dev, "Invalid ref clock rate\n");
+ dev_err(dev, "Invalid ref clock rate: %u Hz\n", ref_clk_rate);
ret = -EINVAL;
goto disable_clk;
}

- if (tp_data->clk1) {
- ret = clk_prepare_enable(tp_data->clk1);
- if (ret) {
- dev_err(tp_data->dev, "Failed to prepare PLL1 ref clock: %d\n", ret);
- goto disable_clk;
- }
-
- ref_clk1_rate = clk_get_rate(tp_data->clk1);
- if (!ref_clk1_rate) {
- dev_err(tp_data->dev, "Failed to get PLL1 ref clock rate\n");
- ret = -EINVAL;
- goto disable_clk1;
- }
-
- switch (ref_clk1_rate) {
- case REF_CLK_19_2MHZ:
- tp_data->ref_clk1_rate = CLK_19_2_MHZ;
- break;
- case REF_CLK_25MHZ:
- tp_data->ref_clk1_rate = CLK_25_MHZ;
- break;
- case REF_CLK_100MHZ:
- tp_data->ref_clk1_rate = CLK_100_MHZ;
- break;
- case REF_CLK_156_25MHZ:
- tp_data->ref_clk1_rate = CLK_156_25_MHZ;
- break;
- default:
- dev_err(tp_data->dev, "Invalid PLL1 ref clock rate\n");
- ret = -EINVAL;
- goto disable_clk1;
- }
- } else {
- tp_data->ref_clk1_rate = tp_data->ref_clk_rate;
- }
-
return 0;

-disable_clk1:
- clk_disable_unprepare(tp_data->clk1);
disable_clk:
- clk_disable_unprepare(tp_data->clk);
+ clk_disable_unprepare(clk);
return ret;
}

+static int tp_ref_clks_init(struct tp_data *tp_data)
+{
+ int ret;
+
+ if (tp_data->ref_clks_initialized)
+ return 0;
+
+ ret = tp_ref_clk_init(tp_data, tp_data->clk, "main", &tp_data->ref_clk_rate);
+ if (ret)
+ return ret;
+
+ if (!tp_data->clk1) {
+ tp_data->ref_clk1_rate = tp_data->ref_clk_rate;
+ } else {
+ ret = tp_ref_clk_init(tp_data, tp_data->clk1, "PLL1", &tp_data->ref_clk1_rate);
+ if (ret) {
+ clk_disable_unprepare(tp_data->clk);
+ return ret;
+ }
+ }
+
+ tp_data->ref_clks_initialized = true;
+ return 0;
+}
+
+static void tp_ref_clks_uninit(struct tp_data *tp_data)
+{
+ if (tp_data->ref_clks_initialized) {
+ clk_disable_unprepare(tp_data->clk1);
+ clk_disable_unprepare(tp_data->clk);
+ tp_data->ref_clks_initialized = false;
+ }
+}
+
static int tp_set_used_lanes(struct tp_data *tp_data, u32 mlane, u32 num_lanes)
{
u8 mask_available_lanes;
@@ -2672,6 +2671,18 @@ static int tp_dp_reg_base_init(struct platform_device *pdev, struct tp_data *tp_
return 0;
}

+static void tp_cleanup_remove(struct tp_data *tp_data)
+{
+ for (u8 i = 0; i < tp_data->nb_links; ++i) {
+ reset_control_assert(tp_data->links[i].lnk_rst);
+ reset_control_put(tp_data->links[i].lnk_rst);
+ }
+
+ reset_control_assert(tp_data->phy_rst);
+ reset_control_assert(tp_data->apb_rst);
+ tp_ref_clks_uninit(tp_data);
+}
+
static bool tp_is_already_configured(const struct tp_data *tp_data)
{
u16 val = tp_reg_read(&tp_data->reg_phy_pma_common_cdb, PHY_PMA_CMN_CTRL1);
@@ -2721,21 +2732,11 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)

ret = tp_of_get_reset(tp_data);
if (ret)
- return ret;
+ goto cleanup;

ret = tp_of_get_clk(tp_data);
if (ret)
- return ret;
-
- tp_data->already_configured = tp_is_already_configured(tp_data);
- if (!tp_data->already_configured) {
- ret = cdns_torrent_clk(tp_data);
- if (ret)
- return ret;
-
- /* Enable APB */
- reset_control_deassert(tp_data->apb_rst);
- }
+ goto cleanup;

for_each_available_child_of_node(dev->of_node, child) {
/* PHY subnode name must be 'phy'. */
@@ -2744,29 +2745,39 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)

ret = tp_add_phy_from_of_subnode(tp_data, child);
if (ret)
- goto put_lnk_rst;
+ goto cleanup;
}
child = NULL;

if (tp_data->nb_links == 0) {
ret = -ENODEV;
- goto put_lnk_rst;
+ goto cleanup;
+ }
+
+ /* Enable APB */
+ reset_control_deassert(tp_data->apb_rst);
+
+ tp_data->already_configured = tp_is_already_configured(tp_data);
+ if (!tp_data->already_configured) {
+ ret = tp_ref_clks_init(tp_data);
+ if (ret)
+ goto cleanup;
}

ret = tp_dp_reg_base_init(pdev, tp_data);
if (ret)
- goto put_lnk_rst;
+ goto cleanup;

if (tp_data->nb_links > 1 && !tp_data->already_configured) {
ret = cdns_torrent_phy_configure_multilink(tp_data);
if (ret)
- goto put_lnk_rst;
+ goto cleanup;
}

phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
if (IS_ERR(phy_provider)) {
ret = PTR_ERR(phy_provider);
- goto put_lnk_rst;
+ goto cleanup;
}

if (tp_data->nb_links > 1)
@@ -2781,33 +2792,19 @@ static int cdns_torrent_phy_probe(struct platform_device *pdev)

return 0;

-put_lnk_rst:
+cleanup:
if (child)
of_node_put(child);

- for (u8 i = 0; i < tp_data->nb_links; i++)
- reset_control_put(tp_data->links[i].lnk_rst);
-
- reset_control_assert(tp_data->apb_rst);
- clk_disable_unprepare(tp_data->clk1);
- clk_disable_unprepare(tp_data->clk);
+ tp_cleanup_remove(tp_data);
return ret;
}

static void cdns_torrent_phy_remove(struct platform_device *pdev)
{
struct tp_data *tp_data = platform_get_drvdata(pdev);
- int i;

- reset_control_assert(tp_data->phy_rst);
- reset_control_assert(tp_data->apb_rst);
- for (i = 0; i < tp_data->nb_links; i++) {
- reset_control_assert(tp_data->links[i].lnk_rst);
- reset_control_put(tp_data->links[i].lnk_rst);
- }
-
- clk_disable_unprepare(tp_data->clk1);
- clk_disable_unprepare(tp_data->clk);
+ tp_cleanup_remove(tp_data);
}

/* Multilink PCIe and USB Same SSC link configuration */
@@ -2984,12 +2981,8 @@ static int tp_suspend_noirq(struct device *dev)
for (i = 0; i < tp_data->nb_links; i++)
reset_control_assert(tp_data->links[i].lnk_rst);

- if (tp_data->already_configured)
- tp_data->already_configured = 0;
- else {
- clk_disable_unprepare(tp_data->clk1);
- clk_disable_unprepare(tp_data->clk);
- }
+ tp_data->already_configured = false;
+ tp_ref_clks_uninit(tp_data);

return 0;
}
@@ -3004,7 +2997,7 @@ static int tp_resume_noirq(struct device *dev)
if (ret)
return ret;

- ret = cdns_torrent_clk(tp_data);
+ ret = tp_ref_clks_init(tp_data);
if (ret)
return ret;

@@ -3024,8 +3017,7 @@ static int tp_resume_noirq(struct device *dev)
reset_control_assert(tp_data->links[i].lnk_rst);
reset_control_assert(tp_data->apb_rst);

- clk_disable_unprepare(tp_data->clk1);
- clk_disable_unprepare(tp_data->clk);
+ tp_ref_clks_uninit(tp_data);

return ret;
}

--
2.56.1