[PATCH v8 phy-next 13/31] scsi: ufs: qcom: keep separate track of PHY power state

Vladimir Oltean
2026-05-05T10:07:17Z

The Generic PHY API needs the following call order:
phy_init() -> phy_power_on() -> phy_power_off() -> phy_exit()
with a balanced number of phy_init() <-> phy_exit() and
phy_power_on() <-> phy_power_off() calls.

The UFS framework is not exactly great in helping out with obeying these
requirements. For example, the Qualcomm UFS HCD driver insists pairing
the PHY power to the clock setup operations. But during driver removal,
we have:

ufshcd_hba_exit()
-> ufshcd_variant_hba_exit()
-> ufs_qcom_exit()
-> ufshcd_setup_clocks(hba, false)

which means that we will underflow the PHY power_count.

Adding a "bool power_count" to the driver and checking it before each
call helps avoid this issue.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
Cc: Can Guo <quic_cang@quicinc.com>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
Cc: Nitin Rawat <quic_nitirawa@quicinc.com>
Cc: Manivannan Sadhasivam <mani@kernel.org>

v7->v8: patch is new to fix an issue reported by Sashiko
https://sashiko.dev/#/patchset/20260430110652.558622-1-vladimir.oltean@nxp.com
---
drivers/ufs/host/ufs-qcom.c | 53 ++++++++++++++++++++++++++++---------
drivers/ufs/host/ufs-qcom.h | 1 +
2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/drivers/ufs/host/ufs-qcom.c b/drivers/ufs/host/ufs-qcom.c
index e28edcfd13a1..c547d8643480 100644
--- a/drivers/ufs/host/ufs-qcom.c
+++ b/drivers/ufs/host/ufs-qcom.c
@@ -485,6 +485,42 @@ static u32 ufs_qcom_get_hs_gear(struct ufs_hba *hba)
return UFS_HS_G3;
}

+static int ufs_qcom_phy_power_on(struct ufs_qcom_host *host)
+{
+ int ret;
+
+ if (host->phy_powered_on)
+ return 0;
+
+ ret = phy_power_on(host->generic_phy);
+ if (ret) {
+ dev_err(host->hba->dev, "Failed to power on PHY: %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+
+ host->phy_powered_on = true;
+
+ return 0;
+}
+
+static void ufs_qcom_phy_power_off(struct ufs_qcom_host *host)
+{
+ int ret;
+
+ if (!host->phy_powered_on)
+ return;
+
+ ret = phy_power_off(host->generic_phy);
+ if (ret) {
+ dev_warn(host->hba->dev, "Failed to power off PHY: %pe\n",
+ ERR_PTR(ret));
+ return;
+ }
+
+ host->phy_powered_on = false;
+}
+
static int ufs_qcom_phy_change_mode(struct ufs_hba *hba)
{
struct ufs_qcom_host *host = ufshcd_get_variant(hba);
@@ -1390,7 +1426,6 @@ static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
enum ufs_notify_change_status status)
{
struct ufs_qcom_host *host = ufshcd_get_variant(hba);
- struct phy *phy;
int err;

/*
@@ -1401,8 +1436,6 @@ static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
if (!host)
return 0;

- phy = host->generic_phy;
-
switch (status) {
case PRE_CHANGE:
if (on) {
@@ -1420,20 +1453,14 @@ static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
ufs_qcom_dev_ref_clk_ctrl(host, false);
}

- err = phy_power_off(phy);
- if (err) {
- dev_err(hba->dev, "phy power off failed, ret=%d\n", err);
- return err;
- }
+ ufs_qcom_phy_power_off(host);
}
break;
case POST_CHANGE:
if (on) {
- err = phy_power_on(phy);
- if (err) {
- dev_err(hba->dev, "phy power on failed, ret = %d\n", err);
+ err = ufs_qcom_phy_power_on(host);
+ if (err)
return err;
- }

/* enable the device ref clock for HS mode*/
if (ufshcd_is_hs_mode(&hba->pwr_info))
@@ -1629,7 +1656,7 @@ static void ufs_qcom_exit(struct ufs_hba *hba)
struct ufs_qcom_host *host = ufshcd_get_variant(hba);

ufs_qcom_disable_lane_clks(host);
- phy_power_off(host->generic_phy);
+ ufs_qcom_phy_power_off(host);
phy_exit(host->generic_phy);
}

diff --git a/drivers/ufs/host/ufs-qcom.h b/drivers/ufs/host/ufs-qcom.h
index 5d083331a7f4..6eafba3c203b 100644
--- a/drivers/ufs/host/ufs-qcom.h
+++ b/drivers/ufs/host/ufs-qcom.h
@@ -322,6 +322,7 @@ struct ufs_qcom_host {
struct clk_bulk_data *clks;
u32 num_clks;
bool is_lane_clks_enabled;
+ bool phy_powered_on;

struct icc_path *icc_ddr;
struct icc_path *icc_cpu;
--
2.34.1