[PATCH v5 13/24] scsi: ufs: mediatek: Use the common PHY framework

Nicolas Frattaroli
2026-01-08T10:51:49Z

There is no need to reinvent the PHY framework, especially not its OF
parsing.

Change the code to simply use the PHY framework to acquire the device's
PHY in the ufshcd init, so that it's device linked to the right device.

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
---
drivers/ufs/host/ufs-mediatek.c | 133 ++++++++++++----------------------------
drivers/ufs/host/ufs-mediatek.h | 1 -
2 files changed, 40 insertions(+), 94 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 45e088f6e92e..3250c27cb91f 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -293,44 +293,6 @@ static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
return 0;
}

-static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
-{
- struct ufs_mtk_host *host = ufshcd_get_variant(hba);
- struct device *dev = hba->dev;
- struct device_node *np = dev->of_node;
- int err = 0;
-
- host->mphy = devm_of_phy_get_by_index(dev, np, 0);
-
- if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
- /*
- * UFS driver might be probed before the phy driver does.
- * In that case we would like to return EPROBE_DEFER code.
- */
- err = -EPROBE_DEFER;
- dev_info(dev,
- "%s: required phy hasn't probed yet. err = %d\n",
- __func__, err);
- } else if (IS_ERR(host->mphy)) {
- err = PTR_ERR(host->mphy);
- if (err != -ENODEV) {
- dev_info(dev, "%s: PHY get failed %d\n", __func__,
- err);
- }
- }
-
- if (err)
- host->mphy = NULL;
- /*
- * Allow unbound mphy because not every platform needs specific
- * mphy control.
- */
- if (err == -ENODEV)
- err = 0;
-
- return err;
-}
-
static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
{
struct ufs_mtk_host *host = ufshcd_get_variant(hba);
@@ -1185,13 +1147,21 @@ static int ufs_mtk_init(struct ufs_hba *hba)

ufs_mtk_init_mcq_irq(hba);

- err = ufs_mtk_bind_mphy(hba);
- if (err)
+ host->mphy = devm_phy_get(dev, NULL);
+ if (IS_ERR(host->mphy)) {
+ err = dev_err_probe(dev, PTR_ERR(host->mphy), "Failed to get PHY\n");
+ goto out_variant_clear;
+ }
+
+ err = phy_init(host->mphy);
+ if (err) {
+ dev_err_probe(dev, err, "Failed to initialize PHY\n");
goto out_variant_clear;
+ }

err = ufs_mtk_init_reset(hba);
if (err)
- goto out_variant_clear;
+ goto out_phy_exit;

/* Enable runtime autosuspend */
hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
@@ -1230,7 +1200,7 @@ static int ufs_mtk_init(struct ufs_hba *hba)

err = ufs_mtk_get_supplies(host);
if (err)
- goto out_variant_clear;
+ goto out_phy_exit;

/*
* ufshcd_vops_init() is invoked after
@@ -1255,11 +1225,22 @@ static int ufs_mtk_init(struct ufs_hba *hba)

return 0;

+out_phy_exit:
+ phy_exit(host->mphy);
out_variant_clear:
ufshcd_set_variant(hba, NULL);
return err;
}

+static void ufs_mtk_exit(struct ufs_hba *hba)
+{
+ struct ufs_mtk_host *host = ufshcd_get_variant(hba);
+
+ ufs_mtk_mphy_power_on(hba, false);
+
+ phy_exit(host->mphy);
+}
+
static bool ufs_mtk_pmc_via_fastauto(struct ufs_hba *hba,
struct ufs_pa_layer_attr *dev_req_params)
{
@@ -2255,6 +2236,7 @@ static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
.name = "mediatek.ufshci",
.max_num_rtt = MTK_MAX_NUM_RTT,
.init = ufs_mtk_init,
+ .exit = ufs_mtk_exit,
.get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
.setup_clocks = ufs_mtk_setup_clocks,
.hce_enable_notify = ufs_mtk_hce_enable_notify,
@@ -2313,50 +2295,17 @@ MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
*/
static int ufs_mtk_probe(struct platform_device *pdev)
{
- struct platform_device *phy_pdev;
struct device *dev = &pdev->dev;
- struct device_node *phy_node;
- struct ufs_mtk_host *host;
- struct device *phy_dev;
struct ufs_hba *hba;
- int err;
-
- /* find phy node */
- phy_node = of_parse_phandle(dev->of_node, "phys", 0);
- if (!phy_node)
- return dev_err_probe(dev, -ENOENT, "No PHY node found\n");
-
- phy_pdev = of_find_device_by_node(phy_node);
- of_node_put(phy_node);
- if (!phy_pdev)
- return dev_err_probe(dev, -ENODEV, "No PHY device found\n");
-
- phy_dev = &phy_pdev->dev;
-
- err = pm_runtime_set_active(phy_dev);
- if (err) {
- dev_err_probe(dev, err, "Failed to activate PHY RPM\n");
- goto err_put_phy;
- }
- pm_runtime_enable(phy_dev);
- err = pm_runtime_get_sync(phy_dev);
- if (err) {
- dev_err_probe(dev, err, "Failed to power on PHY\n");
- goto err_put_phy;
- }
+ int ret;

/* perform generic probe */
- err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
- if (err) {
- dev_err_probe(dev, err, "Generic platform probe failed\n");
- goto err_put_phy;
- }
+ ret = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
+ if (ret)
+ return dev_err_probe(dev, ret, "Generic platform probe failed\n");

hba = platform_get_drvdata(pdev);

- host = ufshcd_get_variant(hba);
- host->phy_dev = phy_dev;
-
/*
* Because the default power setting of VSx (the upper layer of
* VCCQ/VCCQ2) is HWLP, we need to prevent VCCQ/VCCQ2 from
@@ -2365,18 +2314,11 @@ static int ufs_mtk_probe(struct platform_device *pdev)
ufs_mtk_dev_vreg_set_lpm(hba, false);

return 0;
-
-err_put_phy:
- put_device(phy_dev);
-
- return err;
}

/**
* ufs_mtk_remove - set driver_data of the device to NULL
* @pdev: pointer to platform device handle
- *
- * Always return 0
*/
static void ufs_mtk_remove(struct platform_device *pdev)
{
@@ -2436,9 +2378,8 @@ static int ufs_mtk_system_resume(struct device *dev)
static int ufs_mtk_runtime_suspend(struct device *dev)
{
struct ufs_hba *hba = dev_get_drvdata(dev);
- struct ufs_mtk_host *host = ufshcd_get_variant(hba);
struct arm_smccc_res res;
- int ret = 0;
+ int ret;

ret = ufshcd_runtime_suspend(dev);
if (ret)
@@ -2449,8 +2390,11 @@ static int ufs_mtk_runtime_suspend(struct device *dev)
if (ufs_mtk_is_rtff_mtcmos(hba))
ufs_mtk_mtcmos_ctrl(false, res);

- if (host->phy_dev)
- pm_runtime_put_sync(host->phy_dev);
+ ret = ufs_mtk_mphy_power_on(hba, false);
+ if (ret) {
+ dev_err(dev, "Failed to power off PHY: %pe\n", ERR_PTR(ret));
+ return ret;
+ }

return 0;
}
@@ -2458,14 +2402,17 @@ static int ufs_mtk_runtime_suspend(struct device *dev)
static int ufs_mtk_runtime_resume(struct device *dev)
{
struct ufs_hba *hba = dev_get_drvdata(dev);
- struct ufs_mtk_host *host = ufshcd_get_variant(hba);
struct arm_smccc_res res;
+ int ret;

if (ufs_mtk_is_rtff_mtcmos(hba))
ufs_mtk_mtcmos_ctrl(true, res);

- if (host->phy_dev)
- pm_runtime_get_sync(host->phy_dev);
+ ret = ufs_mtk_mphy_power_on(hba, true);
+ if (ret) {
+ dev_err(dev, "Failed to power on PHY: %pe\n", ERR_PTR(ret));
+ return ret;
+ }

ufs_mtk_dev_vreg_set_lpm(hba, false);

diff --git a/drivers/ufs/host/ufs-mediatek.h b/drivers/ufs/host/ufs-mediatek.h
index 24c8941f6b86..4e6a34f4ac39 100644
--- a/drivers/ufs/host/ufs-mediatek.h
+++ b/drivers/ufs/host/ufs-mediatek.h
@@ -195,7 +195,6 @@ struct ufs_mtk_host {
bool is_mcq_intr_enabled;
int mcq_nr_intr;
struct ufs_mtk_mcq_intr_info mcq_intr_info[UFSHCD_MAX_Q_NR];
- struct device *phy_dev;
};

/* MTK delay of autosuspend: 500 ms */

--
2.52.0