[RFC v1 4/4] regulator: core: Move regulator supply resolving to the probe function

From: Saravana Kannan
Date: Sat Feb 18 2023 - 03:33:21 EST


We can simplify the regulator's supply resolving code if we resolve the
supply in the regulator's probe function. This allows us to:

- Consolidate the supply resolution code to one place.
- Avoid the need for recursion by allow driver core to take care of
handling dependencies.
- Avoid races and simplify locking by reusing the guarantees provided by
driver core.
- Avoid last minute/lazy resolving during regulator_get().
- Simplify error handling because we can assume the supply has been
resolved once a regulator is probed.
- Allow driver core to use device links/fw_devlink, where available, to
resolve the regulator supplies in the optimal order.

Signed-off-by: Saravana Kannan <saravanak@xxxxxxxxxx>
---
drivers/regulator/core.c | 27 ++++++---------------------
1 file changed, 6 insertions(+), 21 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index d5f9fdd79c14..f3bf74d1a81d 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1952,7 +1952,7 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
if (node) {
r = of_find_regulator_by_node(node);
of_node_put(node);
- if (r)
+ if (r && r->dev.links.status == DL_DEV_DRIVER_BOUND)
return r;

/*
@@ -1982,11 +1982,11 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
}
mutex_unlock(&regulator_list_mutex);

- if (r)
+ if (r && r->dev.links.status == DL_DEV_DRIVER_BOUND)
return r;

r = regulator_lookup_by_name(supply);
- if (r)
+ if (r && r->dev.links.status == DL_DEV_DRIVER_BOUND)
return r;

return ERR_PTR(-ENODEV);
@@ -2050,13 +2050,6 @@ static int regulator_resolve_supply(struct regulator_dev *rdev)
}
}

- /* Recursively resolve the supply of the supply */
- ret = regulator_resolve_supply(r);
- if (ret < 0) {
- put_device(&r->dev);
- goto out;
- }
-
/*
* Recheck rdev->supply with rdev->mutex lock held to avoid a race
* between rdev->supply null check and setting rdev->supply in
@@ -2178,13 +2171,6 @@ struct regulator *_regulator_get(struct device *dev, const char *id,
return regulator;
}

- ret = regulator_resolve_supply(rdev);
- if (ret < 0) {
- regulator = ERR_PTR(ret);
- put_device(&rdev->dev);
- return regulator;
- }
-
if (!try_module_get(rdev->owner)) {
regulator = ERR_PTR(-EPROBE_DEFER);
put_device(&rdev->dev);
@@ -5649,9 +5635,6 @@ regulator_register(struct device *dev,
regulator_resolve_coupling(rdev);
mutex_unlock(&regulator_list_mutex);

- /* try to resolve regulators supply since a new one was registered */
- bus_for_each_dev(&regulator_bus, NULL, NULL,
- regulator_register_resolve_supply);
kfree(config);
return rdev;

@@ -5782,7 +5765,9 @@ static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {

static int regulator_drv_probe(struct device *dev)
{
- return 0;
+ struct regulator_dev *rdev = dev_to_rdev(dev);
+
+ return regulator_resolve_supply(rdev);
}

static struct device_driver regulator_drv = {
--
2.39.2.637.g21b0678d19-goog