Skip to contentSkip to conversation

regulator: palmas: Check return value of palmas_smps_write()

PATCH Superseded Aleksandrova Alyona 2 participants 3 messages Jun 18 – Jul 27

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 regulator: palmas: Check return value of palmas_smps_write()

palmas_set_mode_smps() updates the SMPS mode register when the rail is enabled, but it ignores a possible error from palmas_smps_write().

Since enable_val is updated after the write, ignoring the error may leave the cached enable value inconsistent with the hardware state.

Return the error if the register write fails to avoid reporting success when the mode has not been updated.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 51d3a0c999e1 ("regulator: palmas: preserve modes of rails during enable/disable")
---
 drivers/regulator/palmas-regulator.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/regulator/palmas-regulator.c +5 -2
index 337dd614695e..fa6dd240015e 100644
@@ -469,8 +469,11 @@ static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
 	}
 
 	pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
-	if (rail_enable)
-		palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+	if (rail_enable) {
+		ret = palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+		if (ret)
+			return ret;
+	}
 
 	/* Switch the enable value to ensure this is used for enable */
 	pmic->desc[id].enable_val = pmic->current_reg_mode[id];
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/1 regulator: palmas: Check return value of palmas_smps_write()

palmas_set_mode_smps() updates the SMPS mode register when the rail is enabled, but it ignores a possible error from palmas_smps_write().

If the register write fails, the function still reports success to the caller even though the hardware mode has not been updated.

Return the error if palmas_smps_write() fails. Update current_reg_mode and enable_val only after a successful register write so that the cached driver state remains consistent with the hardware state.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 51d3a0c999e1 ("regulator: palmas: preserve modes of rails during enable/disable")

---
v2:
- Update current_reg_mode and enable_val only after a successful register write to keep the cached state consistent with the hardware state.

 drivers/regulator/palmas-regulator.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/regulator/palmas-regulator.c +11 -4
index 337dd614695e..8095ebabd671 100644
@@ -443,6 +443,7 @@ static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
 	struct palmas_pmic_driver_data *ddata = pmic->palmas->pmic_ddata;
 	struct palmas_regs_info *rinfo = &ddata->palmas_regs_info[id];
 	unsigned int reg;
+	unsigned int new_mode;
 	bool rail_enable = true;
 
 	ret = palmas_smps_read(pmic->palmas, rinfo->ctrl_addr, &reg);
@@ -468,12 +469,18 @@ static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
 		return -EINVAL;
 	}
 
-	pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
-	if (rail_enable)
-		palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+	new_mode = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
+
+	if (rail_enable) {
+		ret = palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+		if (ret)
+			return ret;
+	}
+
+	pmic->current_reg_mode[id] = new_mode;
 
 	/* Switch the enable value to ensure this is used for enable */
-	pmic->desc[id].enable_val = pmic->current_reg_mode[id];
+	pmic->desc[id].enable_val = new_mode;
 
 	return 0;
 }
Signature · 1 line
2.26.2
#2 lore raw
Quoted 3 lines · Aleksandrova Alyona

v2:
- Update current_reg_mode and enable_val only after a successful register write to keep the cached state consistent with the hardware state.

New versions of the patch are generally recommended to be sent in a separate email thread, not In-Reply-To.

Quoted 6 lines
drivers/regulator/palmas-regulator.c +0 -2
@@ -468,12 +469,18 @@ static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
 		return -EINVAL;
 	}
 
-	pmic->current_reg_mode[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
-	if (rail_enable)

drivers/regulator/palmas-regulator.c static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode) · line 480

Quoted 10 lines
drivers/regulator/palmas-regulator.c +9 -1
-		palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+	new_mode = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
+
+	if (rail_enable) {
+		ret = palmas_smps_write(pmic->palmas, rinfo->ctrl_addr, reg);
+		if (ret)
+			return ret;
+	}
+
+	pmic->current_reg_mode[id] = new_mode;

pmic->current_reg_mode[] update can be moved after the register write. Not sure if a new local variable is actually needed. Sometimes this only complicates things for no real value.

Quoted 9 lines
drivers/regulator/palmas-regulator.c +1 -1
 	/* Switch the enable value to ensure this is used for enable */
-	pmic->desc[id].enable_val = pmic->current_reg_mode[id];
+	pmic->desc[id].enable_val = new_mode;
 
 	return 0;
 }
Signature · 1 line
2.26.2