[PATCH v5 07/24] scsi: ufs: mediatek: Rework 0.9V regulator

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

The mediatek UFS host driver does some pretty bad stuff with regards to
the 0.9V regulator. Instead of just checking for the presence of the
regulator, it adds a cap if it's there, and then checks for the cap. It
also sleeps to stabilise the supply after enabling the regulator, which
is something that should be done by the regulator framework with the
appropriate delay properties in the DTS instead of random sleeps in the
driver code.

Rework this code and rename it to the avdd09 name I've chosen in the
binding for this supply name, instead of the downstream "va09" name that
isn't used by the datasheets for any of these chips.

Reviewed-by: Peter Wang <peter.wang@mediatek.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
---
drivers/ufs/host/ufs-mediatek.c | 153 ++++++++++++++++++++++++++--------------
drivers/ufs/host/ufs-mediatek.h | 3 +-
2 files changed, 101 insertions(+), 55 deletions(-)

diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c
index 5cf5f4c94b8f..7fcf4ceeb56e 100644
--- a/drivers/ufs/host/ufs-mediatek.c
+++ b/drivers/ufs/host/ufs-mediatek.c
@@ -38,6 +38,10 @@ static void _ufs_mtk_clk_scale(struct ufs_hba *hba, bool scale_up);
#define MAX_SUPP_MAC 64
#define MCQ_QUEUE_OFFSET(c) ((((c) >> 16) & 0xFF) * 0x200)

+struct ufs_mtk_soc_data {
+ bool has_avdd09;
+};
+
static const struct ufs_dev_quirk ufs_mtk_dev_fixups[] = {
{ .wmanufacturerid = UFS_ANY_VENDOR,
.model = UFS_ANY_MODEL,
@@ -48,13 +52,6 @@ static const struct ufs_dev_quirk ufs_mtk_dev_fixups[] = {
{}
};

-static const struct of_device_id ufs_mtk_of_match[] = {
- { .compatible = "mediatek,mt8183-ufshci" },
- { .compatible = "mediatek,mt8195-ufshci" },
- {},
-};
-MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
-
/*
* Details of UIC Errors
*/
@@ -106,13 +103,6 @@ static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
return host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
}

-static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
-{
- struct ufs_mtk_host *host = ufshcd_get_variant(hba);
-
- return host->caps & UFS_MTK_CAP_VA09_PWR_CTRL;
-}
-
static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
{
struct ufs_mtk_host *host = ufshcd_get_variant(hba);
@@ -506,44 +496,70 @@ static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
return -ETIMEDOUT;
}

+static int ufs_mtk_09v_off(struct ufs_mtk_host *host)
+{
+ struct arm_smccc_res res;
+ int ret;
+
+ if (!host->reg_avdd09)
+ return 0;
+
+ ufs_mtk_va09_pwr_ctrl(res, 0);
+ ret = regulator_disable(host->reg_avdd09);
+ if (ret) {
+ dev_err(host->hba->dev, "Failed to disable avdd09-supply: %pe\n",
+ ERR_PTR(ret));
+ ufs_mtk_va09_pwr_ctrl(res, 1);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int ufs_mtk_09v_on(struct ufs_mtk_host *host)
+{
+ struct arm_smccc_res res;
+ int ret;
+
+ if (!host->reg_avdd09)
+ return 0;
+
+ ret = regulator_enable(host->reg_avdd09);
+ if (ret) {
+ dev_err(host->hba->dev, "Failed to enable avdd09-supply: %pe\n",
+ ERR_PTR(ret));
+ return ret;
+ }
+
+ ufs_mtk_va09_pwr_ctrl(res, 1);
+
+ return 0;
+}
+
static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
{
struct ufs_mtk_host *host = ufshcd_get_variant(hba);
struct phy *mphy = host->mphy;
- struct arm_smccc_res res;
- int ret = 0;
+ int ret;

- if (!mphy || !(on ^ host->mphy_powered_on))
+ if (!mphy || on == host->mphy_powered_on)
return 0;

if (on) {
- if (ufs_mtk_is_va09_supported(hba)) {
- ret = regulator_enable(host->reg_va09);
- if (ret < 0)
- goto out;
- /* wait 200 us to stablize VA09 */
- usleep_range(200, 210);
- ufs_mtk_va09_pwr_ctrl(res, 1);
- }
+ ret = ufs_mtk_09v_on(host);
+ if (ret)
+ return ret;
phy_power_on(mphy);
} else {
phy_power_off(mphy);
- if (ufs_mtk_is_va09_supported(hba)) {
- ufs_mtk_va09_pwr_ctrl(res, 0);
- ret = regulator_disable(host->reg_va09);
- }
- }
-out:
- if (ret) {
- dev_info(hba->dev,
- "failed to %s va09: %d\n",
- on ? "enable" : "disable",
- ret);
- } else {
- host->mphy_powered_on = on;
+ ret = ufs_mtk_09v_off(host);
+ if (ret)
+ return ret;
}

- return ret;
+ host->mphy_powered_on = on;
+
+ return 0;
}

static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
@@ -678,17 +694,6 @@ static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
return;
}

-static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
-{
- struct ufs_mtk_host *host = ufshcd_get_variant(hba);
-
- host->reg_va09 = regulator_get(hba->dev, "va09");
- if (IS_ERR(host->reg_va09))
- dev_info(hba->dev, "failed to get va09");
- else
- host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
-}
-
static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
{
struct ufs_mtk_host *host = ufshcd_get_variant(hba);
@@ -697,9 +702,6 @@ static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
ufs_mtk_init_boost_crypt(hba);

- if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
- ufs_mtk_init_va09_pwr_ctrl(hba);
-
if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
host->caps |= UFS_MTK_CAP_DISABLE_AH8;

@@ -1205,6 +1207,35 @@ static void ufs_mtk_init_mcq_irq(struct ufs_hba *hba)
host->mcq_nr_intr = 0;
}

+/**
+ * ufs_mtk_get_supplies - acquire variant-specific supplies
+ * @host: pointer to driver's private &struct ufs_mtk_host instance
+ *
+ * Returns 0 on success, negative errno on error.
+ */
+static int ufs_mtk_get_supplies(struct ufs_mtk_host *host)
+{
+ struct device *dev = host->hba->dev;
+ const struct ufs_mtk_soc_data *data = of_device_get_match_data(dev);
+
+ if (!data || !data->has_avdd09)
+ return 0;
+
+ host->reg_avdd09 = devm_regulator_get_optional(dev, "avdd09");
+ if (IS_ERR(host->reg_avdd09)) {
+ if (PTR_ERR(host->reg_avdd09) == -ENODEV) {
+ host->reg_avdd09 = NULL;
+ return 0;
+ }
+
+ dev_err(dev, "Failed to get avdd09 regulator: %pe\n",
+ host->reg_avdd09);
+ return PTR_ERR(host->reg_avdd09);
+ }
+
+ return 0;
+}
+
/**
* ufs_mtk_init - find other essential mmio bases
* @hba: host controller instance
@@ -1288,6 +1319,10 @@ static int ufs_mtk_init(struct ufs_hba *hba)

ufs_mtk_init_clocks(hba);

+ err = ufs_mtk_get_supplies(host);
+ if (err)
+ goto out_variant_clear;
+
/*
* ufshcd_vops_init() is invoked after
* ufshcd_setup_clock(true) in ufshcd_hba_init() thus
@@ -2336,6 +2371,18 @@ static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
.config_scsi_dev = ufs_mtk_config_scsi_dev,
};

+static const struct ufs_mtk_soc_data mt8183_data = {
+ .has_avdd09 = true,
+};
+
+static const struct of_device_id ufs_mtk_of_match[] = {
+ { .compatible = "mediatek,mt8183-ufshci", .data = &mt8183_data },
+ { .compatible = "mediatek,mt8192-ufshci" },
+ { .compatible = "mediatek,mt8195-ufshci" },
+ {},
+};
+MODULE_DEVICE_TABLE(of, ufs_mtk_of_match);
+
/**
* ufs_mtk_probe - probe routine of the driver
* @pdev: pointer to Platform device handle
diff --git a/drivers/ufs/host/ufs-mediatek.h b/drivers/ufs/host/ufs-mediatek.h
index 4fce29d131d1..24c8941f6b86 100644
--- a/drivers/ufs/host/ufs-mediatek.h
+++ b/drivers/ufs/host/ufs-mediatek.h
@@ -125,7 +125,6 @@ enum {
*/
enum ufs_mtk_host_caps {
UFS_MTK_CAP_BOOST_CRYPT_ENGINE = 1 << 0,
- UFS_MTK_CAP_VA09_PWR_CTRL = 1 << 1,
UFS_MTK_CAP_DISABLE_AH8 = 1 << 2,
UFS_MTK_CAP_BROKEN_VCC = 1 << 3,

@@ -176,7 +175,7 @@ struct ufs_mtk_mcq_intr_info {

struct ufs_mtk_host {
struct phy *mphy;
- struct regulator *reg_va09;
+ struct regulator *reg_avdd09;
struct reset_control_bulk_data resets[MTK_UFS_NUM_RESETS];
struct ufs_hba *hba;
struct ufs_mtk_crypt_cfg *crypt;

--
2.52.0