[PATCH 3/4] regulator: qcom-rpmh: readback voltage/bypass/mode/status set during bootup
From: Kamal Wadhwa
Date: Mon Apr 06 2026 - 18:25:00 EST
Currently, during regulator registration, regulator framework sends an
unnecessary `min-microvolts` request for the rpmh-regulator device. This
happens because in current design, we do not have a way to readback the
voltage settings that was set during the bootloader stage.
Fix this by using the rpmh_read() API to read the regulator voltage
settings done during boot and make it available to regulator framework
from the very first read after the bootup.
Also use this API to read the status/mode/bypass settings as well. This
will provide the regulator framework a sense of the initial settings
done by bootloader and thus preventing any redundents writes for any
setting post bootup incase the same setting was already applied during
bootup.
Signed-off-by: Kamal Wadhwa <kamal.wadhwa@xxxxxxxxxxxxxxxx>
---
drivers/regulator/qcom-rpmh-regulator.c | 178 ++++++++++++++++++++++++++++++++
1 file changed, 178 insertions(+)
diff --git a/drivers/regulator/qcom-rpmh-regulator.c b/drivers/regulator/qcom-rpmh-regulator.c
index 85fbf10f74bb3393071bc65681356312f27b7527..8e1c576b718b595bbbff7f5fa76de84d4e90f3bb 100644
--- a/drivers/regulator/qcom-rpmh-regulator.c
+++ b/drivers/regulator/qcom-rpmh-regulator.c
@@ -61,8 +61,13 @@ static const struct resource_name_formats vreg_rsc_name_lookup[NUM_REGULATOR_TYP
};
#define RPMH_REGULATOR_REG_VRM_VOLTAGE 0x0
+#define RPMH_REGULATOR_VOLTAGE_MASK 0x1FFF
+
#define RPMH_REGULATOR_REG_ENABLE 0x4
+#define RPMH_REGULATOR_ENABLE_MASK 0x1
+
#define RPMH_REGULATOR_REG_VRM_MODE 0x8
+#define RPMH_REGULATOR_MODE_MASK 0x7
#define PMIC4_LDO_MODE_RETENTION 4
#define PMIC4_LDO_MODE_LPM 5
@@ -154,6 +159,7 @@ struct rpmh_vreg_hw_data {
* @voltage_selector: Selector used for get_voltage_sel() and
* set_voltage_sel() callbacks
* @mode: RPMh VRM regulator current framework mode
+ * @status: RPMh VRM regulator current framework status
*/
struct rpmh_vreg {
struct device *dev;
@@ -166,6 +172,7 @@ struct rpmh_vreg {
bool bypassed;
int voltage_selector;
unsigned int mode;
+ unsigned int status;
};
/**
@@ -210,6 +217,28 @@ static int rpmh_regulator_send_request(struct rpmh_vreg *vreg,
return ret;
}
+/**
+ * rpmh_regulator_read_data() - read data from RPMh
+ * @vreg: Pointer to the RPMh regulator
+ * @cmd: Pointer to the RPMh command struct to readback data
+ *
+ * Return: 0 on success, or a negative error number on failure
+ */
+static int rpmh_regulator_read_data(struct rpmh_vreg *vreg, struct tcs_cmd *cmd)
+{
+ return rpmh_read(vreg->dev, cmd);
+}
+
+static void rpmh_vreg_update_status(struct rpmh_vreg *vreg)
+{
+ if (!vreg->enabled)
+ vreg->status = REGULATOR_STATUS_OFF;
+ else if (vreg->bypassed)
+ vreg->status = REGULATOR_STATUS_BYPASS;
+ else
+ vreg->status = regulator_mode_to_status(vreg->mode);
+}
+
static int _rpmh_regulator_vrm_set_voltage_sel(struct regulator_dev *rdev,
unsigned int selector, bool wait_for_ack)
{
@@ -248,9 +277,42 @@ static int rpmh_regulator_vrm_set_voltage_sel(struct regulator_dev *rdev,
selector > vreg->voltage_selector);
}
+static int _rpmh_regulator_vrm_get_voltage(struct regulator_dev *rdev, int *uV)
+{
+ struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
+ struct tcs_cmd cmd = {
+ .addr = vreg->addr + RPMH_REGULATOR_REG_VRM_VOLTAGE,
+ };
+ int min_uV = rdev->constraints->min_uV;
+ int max_uV = rdev->constraints->max_uV;
+ int ret, _uV = 0;
+
+ ret = rpmh_regulator_read_data(vreg, &cmd);
+ if (!ret)
+ _uV = (cmd.data & RPMH_REGULATOR_VOLTAGE_MASK) * 1000;
+ else
+ dev_err(vreg->dev, "failed to read VOLTAGE ret = %d\n", ret);
+
+ if (!_uV || (_uV >= min_uV && _uV <= max_uV))
+ *uV = _uV;
+ else
+ dev_err(vreg->dev, "read voltage %d is out-of-range[%d:%d]\n",
+ _uV, min_uV, max_uV);
+
+ return ret;
+}
+
static int rpmh_regulator_vrm_get_voltage_sel(struct regulator_dev *rdev)
{
struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
+ int ret, uV = 0;
+
+ if (vreg->voltage_selector < 0) {
+ ret = _rpmh_regulator_vrm_get_voltage(rdev, &uV);
+ if (!ret && uV != 0)
+ vreg->voltage_selector = regulator_map_voltage_linear_range(rdev,
+ uV, INT_MAX);
+ }
return vreg->voltage_selector;
}
@@ -284,6 +346,8 @@ static int rpmh_regulator_set_enable_state(struct regulator_dev *rdev,
if (!ret)
vreg->enabled = enable;
+ rpmh_vreg_update_status(vreg);
+
return ret;
}
@@ -333,9 +397,27 @@ static int rpmh_regulator_vrm_set_mode(struct regulator_dev *rdev,
if (!ret)
vreg->mode = mode;
+ rpmh_vreg_update_status(vreg);
+
return ret;
}
+static int rpmh_regulator_vrm_get_pmic_mode(struct rpmh_vreg *vreg, int *pmic_mode)
+{
+ struct tcs_cmd cmd = {
+ .addr = vreg->addr + RPMH_REGULATOR_REG_VRM_MODE,
+ };
+ int ret;
+
+ ret = rpmh_regulator_read_data(vreg, &cmd);
+ if (!ret)
+ *pmic_mode = cmd.data & RPMH_REGULATOR_MODE_MASK;
+ else
+ return -EINVAL;
+
+ return 0;
+}
+
static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
{
struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
@@ -343,6 +425,13 @@ static unsigned int rpmh_regulator_vrm_get_mode(struct regulator_dev *rdev)
return vreg->mode;
}
+static int rpmh_regulator_vrm_get_status(struct regulator_dev *rdev)
+{
+ struct rpmh_vreg *vreg = rdev_get_drvdata(rdev);
+
+ return vreg->status;
+}
+
/**
* rpmh_regulator_vrm_get_optimum_mode() - get the mode based on the load
* @rdev: Regulator device pointer for the rpmh-regulator
@@ -379,6 +468,8 @@ static int rpmh_regulator_vrm_set_bypass(struct regulator_dev *rdev,
if (!ret)
vreg->bypassed = enable;
+ rpmh_vreg_update_status(vreg);
+
return ret;
}
@@ -401,6 +492,7 @@ static const struct regulator_ops rpmh_regulator_vrm_ops = {
.list_voltage = regulator_list_voltage_linear_range,
.set_mode = rpmh_regulator_vrm_set_mode,
.get_mode = rpmh_regulator_vrm_get_mode,
+ .get_status = rpmh_regulator_vrm_get_status,
};
static const struct regulator_ops rpmh_regulator_vrm_drms_ops = {
@@ -412,6 +504,7 @@ static const struct regulator_ops rpmh_regulator_vrm_drms_ops = {
.list_voltage = regulator_list_voltage_linear_range,
.set_mode = rpmh_regulator_vrm_set_mode,
.get_mode = rpmh_regulator_vrm_get_mode,
+ .get_status = rpmh_regulator_vrm_get_status,
.get_optimum_mode = rpmh_regulator_vrm_get_optimum_mode,
};
@@ -424,6 +517,7 @@ static const struct regulator_ops rpmh_regulator_vrm_bypass_ops = {
.list_voltage = regulator_list_voltage_linear_range,
.set_mode = rpmh_regulator_vrm_set_mode,
.get_mode = rpmh_regulator_vrm_get_mode,
+ .get_status = rpmh_regulator_vrm_get_status,
.set_bypass = rpmh_regulator_vrm_set_bypass,
.get_bypass = rpmh_regulator_vrm_get_bypass,
};
@@ -432,6 +526,7 @@ static const struct regulator_ops rpmh_regulator_xob_ops = {
.enable = rpmh_regulator_enable,
.disable = rpmh_regulator_disable,
.is_enabled = rpmh_regulator_is_enabled,
+ .get_status = rpmh_regulator_vrm_get_status,
};
/**
@@ -540,6 +635,83 @@ static int rpmh_regulator_init_vreg(struct rpmh_vreg *vreg, struct device *dev,
return 0;
}
+static int rpmh_regulator_determine_initial_status(struct rpmh_vreg *vreg)
+{
+ struct tcs_cmd cmd = {
+ .addr = vreg->addr + RPMH_REGULATOR_REG_ENABLE,
+ };
+ int ret, pmic_mode, mode;
+ int sts = 0;
+
+ ret = rpmh_regulator_read_data(vreg, &cmd);
+ if (ret) {
+ vreg->status = REGULATOR_STATUS_UNDEFINED;
+ dev_err(vreg->dev, "failed to read ENABLE status ret = %d\n", ret);
+
+ return ret;
+ }
+
+ sts = cmd.data & RPMH_REGULATOR_ENABLE_MASK;
+ if (!sts) {
+ vreg->status = REGULATOR_STATUS_OFF;
+
+ return 0;
+ }
+
+ if (vreg->hw_data->regulator_type == XOB) {
+ vreg->status = sts ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
+
+ return 0;
+ }
+
+ ret = rpmh_regulator_vrm_get_pmic_mode(vreg, &pmic_mode);
+ if (ret < 0) {
+ vreg->mode = REGULATOR_MODE_INVALID;
+ vreg->status = REGULATOR_STATUS_UNDEFINED;
+ dev_err(vreg->dev, "failed to read pmic_mode ret = %d\n", ret);
+
+ return ret;
+ }
+
+ /*
+ * NOTE: Since BOB4 BYPASS_MODE value = 0 we cannot confirm if that BOB
+ * regulator has been sent into bypass mode by bootloader or if bootloader
+ * just has not requested for any mode voting. Due this limitation, we
+ * must check if the read pmic_mode value is non-zero before comparing it
+ * to bypass mode value. This also is needed to avoid setting BYPASS status
+ * for LDOs which dont support bypass mode, and have the pmic_bypass_mode
+ * uninitialized value as zero in the vreg hw data. For such cases assume
+ * lowest mode/status, if pmic_mode is zero, to allow for mode voting.
+ */
+ if (!pmic_mode) {
+ for (mode = REGULATOR_MODE_STANDBY; mode > REGULATOR_MODE_INVALID; mode >>= 1) {
+ if (vreg->hw_data->pmic_mode_map[mode] >= 0) {
+ vreg->mode = mode;
+ break;
+ }
+ }
+
+ vreg->status = regulator_mode_to_status(vreg->mode);
+ return 0;
+ }
+
+ if (vreg->hw_data->pmic_bypass_mode == pmic_mode) {
+ vreg->bypassed = true;
+ vreg->status = REGULATOR_STATUS_BYPASS;
+ return 0;
+ }
+
+ for (mode = REGULATOR_MODE_STANDBY; mode > REGULATOR_MODE_INVALID; mode >>= 1) {
+ if (pmic_mode == vreg->hw_data->pmic_mode_map[mode]) {
+ vreg->mode = mode;
+ vreg->status = regulator_mode_to_status(vreg->mode);
+ break;
+ }
+ }
+
+ return 0;
+}
+
static const int pmic_mode_map_pmic4_ldo[REGULATOR_MODE_STANDBY + 1] = {
[REGULATOR_MODE_INVALID] = -EINVAL,
[REGULATOR_MODE_STANDBY] = PMIC4_LDO_MODE_RETENTION,
@@ -1823,6 +1995,12 @@ static int rpmh_regulator_probe(struct platform_device *pdev)
vreg_data);
if (ret < 0)
return ret;
+
+ ret = rpmh_regulator_determine_initial_status(vreg);
+ if (ret < 0)
+ dev_err(dev, "failed to read initial status for %s\n",
+ vreg->rdesc.name);
+
}
return 0;
--
2.25.1