diff mbox series

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

Message ID 20240724224713.2963301-2-jonas@kwiboo.se
State Accepted
Commit 4386ab9118e7b405829fbcb3bbbaf2f47ddf0b44
Delegated to: Jaehoon Chung
Headers show
Series [1/2] power: regulator: Do not call set_suspend_value with -ENODATA | expand

Commit Message

Jonas Karlman July 24, 2024, 10:47 p.m. UTC
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.

Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
---
 drivers/power/regulator/regulator-uclass.c | 24 +++++++++++-----------
 1 file changed, 12 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/drivers/power/regulator/regulator-uclass.c b/drivers/power/regulator/regulator-uclass.c
index 03d39587b315..009229002920 100644
--- a/drivers/power/regulator/regulator-uclass.c
+++ b/drivers/power/regulator/regulator-uclass.c
@@ -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);