Skip to contentSkip to conversation

power: regulator: Do not call set_suspend_value with -ENODATA

PATCH In review Jonas Karlman 2 participants 3 messages Jul 24, 2024 – Sep 25, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 power: regulator: Do not call set_suspend_value with -ENODATA

On some boards a PMIC regulator is flagged with regulator-on-in-suspend and does not define any suspend or max microvolt, e.g. on Radxa ROCK 3A:

  vcc_ddr: DCDC_REG3 {
	regulator-name = "vcc_ddr";
	regulator-always-on;
	regulator-boot-on;
	regulator-initial-mode = <0x2>;

	regulator-state-mem {
		regulator-on-in-suspend;
	};

};

This result in suspend_uV having the value -ENODATA after probe.

This negative voltage, -ENODATA, gets missinterpreted and result in an unexpected voltage being set by autoset.

E.g. on Radxa ROCK 3A the vcc_ddr regulator by default have a normal and suspend voltage value of 0.5v. However, due to this missinterpretation the suspend voltage end up beind set to 0.5625v instead.

Fix this by skip calling regulator_set_suspend_value() in autoset and also protect calling set value ops when input value is -ENODATA.

---
 drivers/power/regulator/regulator-uclass.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/power/regulator/regulator-uclass.c +7 -1
index 88a8525b3c47..03d39587b315 100644
@@ -60,6 +60,8 @@ int regulator_set_value(struct udevice *dev, int uV)
 		return -EINVAL;
 	if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
 		return -EINVAL;
+	if (uV == -ENODATA)
+		return -EINVAL;
 
 	if (!ops || !ops->set_value)
 		return -ENOSYS;
@@ -90,6 +92,8 @@ int regulator_set_suspend_value(struct udevice *dev, int uV)
 		return -EINVAL;
 	if (uc_pdata->max_uV != -ENODATA && uV > uc_pdata->max_uV)
 		return -EINVAL;
+	if (uV == -ENODATA)
+		return -EINVAL;
 
 	if (!ops->set_suspend_value)
 		return -ENOSYS;
@@ -141,6 +145,8 @@ int regulator_set_current(struct udevice *dev, int uA)
 		return -EINVAL;
 	if (uc_pdata->max_uA != -ENODATA && uA > uc_pdata->max_uA)
 		return -EINVAL;
+	if (uA == -ENODATA)
+		return -EINVAL;
 
 	if (!ops || !ops->set_current)
 		return -ENOSYS;
@@ -299,7 +305,7 @@ int regulator_autoset(struct udevice *dev)
 	if (ret == -ENOSYS)
 		ret = 0;
 
-	if (!ret && uc_pdata->suspend_on) {
+	if (!ret && uc_pdata->suspend_on && uc_pdata->suspend_uV != -ENODATA) {
 		ret = regulator_set_suspend_value(dev, uc_pdata->suspend_uV);
 		if (ret == -ENOSYS)
 			ret = 0;
Signature · 1 line
2.45.2
#1 lore raw

PATCH 2/2 power: regulator: Consistently return -ENOSYS when ops is unimplemented

dev_get_driver_ops() may return NULL when the udevice is invalid.

Move the ops check to top of functions to consistently return -ENOSYS when ops is unimplemented and prevent trying to access uclass plat data, also add missing NULL checks to suspend ops.

---
 drivers/power/regulator/regulator-uclass.c | 24 +++++++++++-----------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/power/regulator/regulator-uclass.c +12 -12
index 03d39587b315..009229002920 100644
@@ -55,6 +55,9 @@ int regulator_set_value(struct udevice *dev, int uV)
 	struct dm_regulator_uclass_plat *uc_pdata;
 	int ret, old_uV = uV, is_enabled = 0;
 
+	if (!ops || !ops->set_value)
+		return -ENOSYS;
+
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
 		return -EINVAL;
@@ -63,9 +66,6 @@ int regulator_set_value(struct udevice *dev, int uV)
 	if (uV == -ENODATA)
 		return -EINVAL;
 
-	if (!ops || !ops->set_value)
-		return -ENOSYS;
-
 	if (uc_pdata->ramp_delay) {
 		is_enabled = regulator_get_enable(dev);
 		old_uV = regulator_get_value(dev);
@@ -87,6 +87,9 @@ int regulator_set_suspend_value(struct udevice *dev, int uV)
 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
 	struct dm_regulator_uclass_plat *uc_pdata;
 
+	if (!ops || !ops->set_suspend_value)
+		return -ENOSYS;
+
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (uc_pdata->min_uV != -ENODATA && uV < uc_pdata->min_uV)
 		return -EINVAL;
@@ -95,9 +98,6 @@ int regulator_set_suspend_value(struct udevice *dev, int uV)
 	if (uV == -ENODATA)
 		return -EINVAL;
 
-	if (!ops->set_suspend_value)
-		return -ENOSYS;
-
 	return ops->set_suspend_value(dev, uV);
 }
 
@@ -105,7 +105,7 @@ int regulator_get_suspend_value(struct udevice *dev)
 {
 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
 
-	if (!ops->get_suspend_value)
+	if (!ops || !ops->get_suspend_value)
 		return -ENOSYS;
 
 	return ops->get_suspend_value(dev);
@@ -140,6 +140,9 @@ int regulator_set_current(struct udevice *dev, int uA)
 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
 	struct dm_regulator_uclass_plat *uc_pdata;
 
+	if (!ops || !ops->set_current)
+		return -ENOSYS;
+
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (uc_pdata->min_uA != -ENODATA && uA < uc_pdata->min_uA)
 		return -EINVAL;
@@ -148,9 +151,6 @@ int regulator_set_current(struct udevice *dev, int uA)
 	if (uA == -ENODATA)
 		return -EINVAL;
 
-	if (!ops || !ops->set_current)
-		return -ENOSYS;
-
 	return ops->set_current(dev, uA);
 }
 
@@ -216,7 +216,7 @@ int regulator_set_suspend_enable(struct udevice *dev, bool enable)
 {
 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
 
-	if (!ops->set_suspend_enable)
+	if (!ops || !ops->set_suspend_enable)
 		return -ENOSYS;
 
 	return ops->set_suspend_enable(dev, enable);
@@ -226,7 +226,7 @@ int regulator_get_suspend_enable(struct udevice *dev)
 {
 	const struct dm_regulator_ops *ops = dev_get_driver_ops(dev);
 
-	if (!ops->get_suspend_enable)
+	if (!ops || !ops->get_suspend_enable)
 		return -ENOSYS;
 
 	return ops->get_suspend_enable(dev);
Signature · 1 line
2.45.2
#2 lore raw
In reply to Jonas Karlman
Quoted 10 lines · Jonas Karlman

On some boards a PMIC regulator is flagged with regulator-on-in-suspend and does not define any suspend or max microvolt, e.g. on Radxa ROCK 3A:

  vcc_ddr: DCDC_REG3 {
	regulator-name = "vcc_ddr";
	regulator-always-on;
	regulator-boot-on;
	regulator-initial-mode = <0x2>;

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom