Skip to content

Commit 304f578

Browse files
andredbroonie
authored andcommitted
regulator: core: reresolve unresolved supplies when available
When a regulator A and its supply B are provided by different devices, the driver implementing B might be last to probe (with A still pending resolution of its supply B). While we try to resolve all pending supplies for all regulators (including A) during regulator_register() of B via regulator_register_resolve_supply(), supply resolution will still not work for A as the driver for B hasn't finished binding to the PMIC device corresponding to B at that stage yet. The regulator core explicitly only allows supplies from other devices to be used once the relevant driver has fully bound, mainly to avoid having to deal with cases where B itself might -EPROBE_DEFER. In this case, A's supply will only be resolved as part of the core's regulator_init_complete_work_function(), which currently is scheduled to run after 30s. This was added as a work-around in commit 3827b64 ("regulator: core: Resolve supplies before disabling unused regulators") to cover this situation. There are two problems with that approach: * it potentially runs long after all our consumers have probed * an upcoming change will allow regulator_register() to complete successfully even when required supplies (e.g. due to always-on or boot-on) are missing at register time, deferring full configuration of the regulator (and usability by consumers, i.e. usually consumer probe) until the supply becomes available. Resolving supplies in the late work func can therefore make it impossible for consumers to probe at all, as the driver core will not know to reprobe consumers when supplies have resolved. We could schedule an earlier work to try to resolve supplies sooner, but that'd be racy as consumers of A might try to probe before A's supply gets fully resolved via this extra work. Instead, add a very simple regulator bus and add a dummy device with a corresponding driver to it for each regulator that is missing its supply during regulator_register(). This way, the driver core will call our bus' probe whenever a new (regulator) device was successfully bound, allowing us to retry resolving the supply during (our bus) probe and to bind this dummy device if successful. In turn this means the driver core will see a newly bound device and retry probing of all pending consumers, if any. With that in place, we can avoid walking the full list of all known regulators to try resolve missing supplies during regulator_register(), as the driver core will invoke the bus probe for regulators that are still pending their supplies. We can also drop the code trying to resolve supplies one last time before unused regulators get disabled, as all supplies should have resolved at that point in time, and if they haven't then there's no point in trying again, as the outcome won't change. Note: We can not reuse the existing struct device created for each rail, as a device can not be part of a class and a bus simultaneously. Signed-off-by: André Draszik <andre.draszik@linaro.org> Link: https://patch.msgid.link/20260109-regulators-defer-v2-7-1a25dc968e60@linaro.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent e23c0a5 commit 304f578

2 files changed

Lines changed: 98 additions & 24 deletions

File tree

drivers/regulator/core.c

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static LIST_HEAD(regulator_supply_alias_list);
4444
static LIST_HEAD(regulator_coupler_list);
4545
static bool has_full_constraints;
4646

47+
static const struct bus_type regulator_bus;
48+
4749
static struct dentry *debugfs_root;
4850

4951
/*
@@ -5694,17 +5696,6 @@ static void rdev_init_debugfs(struct regulator_dev *rdev)
56945696
&rdev->bypass_count);
56955697
}
56965698

5697-
static int regulator_register_resolve_supply(struct device *dev, void *data)
5698-
{
5699-
struct regulator_dev *rdev = dev_to_rdev(dev);
5700-
5701-
if (regulator_resolve_supply(rdev))
5702-
rdev_dbg(rdev, "unable to resolve supply '%s'\n",
5703-
rdev->supply_name);
5704-
5705-
return 0;
5706-
}
5707-
57085699
int regulator_coupler_register(struct regulator_coupler *coupler)
57095700
{
57105701
mutex_lock(&regulator_list_mutex);
@@ -5923,6 +5914,7 @@ regulator_register(struct device *dev,
59235914
struct regulator_config *config = NULL;
59245915
static atomic_t regulator_no = ATOMIC_INIT(-1);
59255916
struct regulator_dev *rdev;
5917+
bool tried_supply_resolve = false;
59265918
bool dangling_cfg_gpiod = false;
59275919
bool dangling_of_gpiod = false;
59285920
int ret, i;
@@ -6093,6 +6085,7 @@ regulator_register(struct device *dev,
60936085
else
60946086
rdev_dbg(rdev, "unable to resolve supply early: %pe\n",
60956087
ERR_PTR(ret));
6088+
tried_supply_resolve = true;
60966089
}
60976090
if (ret < 0)
60986091
goto wash;
@@ -6124,19 +6117,51 @@ regulator_register(struct device *dev,
61246117
if (ret != 0)
61256118
goto unset_supplies;
61266119

6120+
if (!tried_supply_resolve) {
6121+
/*
6122+
* As an optimisation, try to resolve our supply (if any) now to
6123+
* avoid adding the bus device. Errors are not fatal at this
6124+
* stage, we'll simply try again later.
6125+
*/
6126+
ret = regulator_resolve_supply(rdev);
6127+
if (ret)
6128+
rdev_dbg(rdev,
6129+
"unable to resolve supply (ignoring): %pe\n",
6130+
ERR_PTR(ret));
6131+
}
6132+
6133+
/*
6134+
* If we have a supply but couldn't resolve it yet, register a device
6135+
* with our bus, so that the bus probe gets called whenever any new
6136+
* driver binds, allowing us to retry matching supplies and which then
6137+
* triggers (re)probe of consumers if successful.
6138+
*/
6139+
if (rdev->supply_name && !rdev->supply) {
6140+
device_initialize(&rdev->bdev);
6141+
rdev->bdev.bus = &regulator_bus;
6142+
rdev->bdev.parent = &rdev->dev;
6143+
device_set_pm_not_required(&rdev->dev);
6144+
dev_set_name(&rdev->bdev, "%s.bdev", dev_name(&rdev->dev));
6145+
6146+
ret = device_add(&rdev->bdev);
6147+
if (ret)
6148+
goto del_cdev_and_bdev;
6149+
}
6150+
61276151
rdev_init_debugfs(rdev);
61286152

61296153
/* try to resolve regulators coupling since a new one was registered */
61306154
mutex_lock(&regulator_list_mutex);
61316155
regulator_resolve_coupling(rdev);
61326156
mutex_unlock(&regulator_list_mutex);
61336157

6134-
/* try to resolve regulators supply since a new one was registered */
6135-
class_for_each_device(&regulator_class, NULL, NULL,
6136-
regulator_register_resolve_supply);
61376158
kfree(config);
61386159
return rdev;
61396160

6161+
del_cdev_and_bdev:
6162+
if (rdev->bdev.bus == &regulator_bus)
6163+
put_device(&rdev->bdev);
6164+
device_del(&rdev->dev);
61406165
unset_supplies:
61416166
mutex_lock(&regulator_list_mutex);
61426167
unset_regulator_supplies(rdev);
@@ -6189,6 +6214,9 @@ void regulator_unregister(struct regulator_dev *rdev)
61896214
unset_regulator_supplies(rdev);
61906215
list_del(&rdev->list);
61916216
regulator_ena_gpio_free(rdev);
6217+
if (rdev->bdev.bus == &regulator_bus)
6218+
/* only if the device was added in the first place */
6219+
device_unregister(&rdev->bdev);
61926220
device_unregister(&rdev->dev);
61936221

61946222
mutex_unlock(&regulator_list_mutex);
@@ -6269,6 +6297,45 @@ const struct class regulator_class = {
62696297
.pm = &regulator_pm_ops,
62706298
#endif
62716299
};
6300+
6301+
#define bdev_to_rdev(__bdev) container_of_const(__bdev, struct regulator_dev, bdev)
6302+
6303+
static int regulator_bus_match(struct device *bdev,
6304+
const struct device_driver *drv)
6305+
{
6306+
/* Match always succeeds, we only have one driver */
6307+
return 1;
6308+
}
6309+
6310+
static int regulator_bus_probe(struct device *bdev)
6311+
{
6312+
struct regulator_dev *rdev = bdev_to_rdev(bdev);
6313+
int ret;
6314+
6315+
ret = regulator_resolve_supply(rdev);
6316+
if (ret)
6317+
rdev_dbg(rdev,
6318+
"unable to resolve supply or constraints '%s': %pe\n",
6319+
rdev->supply_name, ERR_PTR(ret));
6320+
else
6321+
rdev_dbg(rdev, "resolved supply '%s'\n", rdev->supply_name);
6322+
6323+
return ret;
6324+
}
6325+
6326+
static const struct bus_type regulator_bus = {
6327+
.name = "regulator",
6328+
.match = regulator_bus_match,
6329+
.probe = regulator_bus_probe,
6330+
};
6331+
6332+
static struct device_driver regulator_bus_driver = {
6333+
.name = "regulator-bus-drv",
6334+
.bus = &regulator_bus,
6335+
.suppress_bind_attrs = true,
6336+
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
6337+
};
6338+
62726339
/**
62736340
* regulator_has_full_constraints - the system has fully specified constraints
62746341
*
@@ -6602,7 +6669,17 @@ static int __init regulator_init(void)
66026669
{
66036670
int ret;
66046671

6672+
ret = bus_register(&regulator_bus);
6673+
if (ret)
6674+
return ret;
6675+
66056676
ret = class_register(&regulator_class);
6677+
if (ret)
6678+
goto err_class;
6679+
6680+
ret = driver_register(&regulator_bus_driver);
6681+
if (ret)
6682+
goto err_driver;
66066683

66076684
debugfs_root = debugfs_create_dir("regulator", NULL);
66086685
if (IS_ERR(debugfs_root))
@@ -6619,6 +6696,12 @@ static int __init regulator_init(void)
66196696

66206697
regulator_coupler_register(&generic_regulator_coupler);
66216698

6699+
return 0;
6700+
6701+
err_driver:
6702+
class_unregister(&regulator_class);
6703+
err_class:
6704+
bus_unregister(&regulator_bus);
66226705
return ret;
66236706
}
66246707

@@ -6679,16 +6762,6 @@ __setup("regulator_ignore_unused", regulator_ignore_unused_setup);
66796762

66806763
static void regulator_init_complete_work_function(struct work_struct *work)
66816764
{
6682-
/*
6683-
* Regulators may had failed to resolve their input supplies
6684-
* when were registered, either because the input supply was
6685-
* not registered yet or because its parent device was not
6686-
* bound yet. So attempt to resolve the input supplies for
6687-
* pending regulators before trying to disable unused ones.
6688-
*/
6689-
class_for_each_device(&regulator_class, NULL, NULL,
6690-
regulator_register_resolve_supply);
6691-
66926765
/*
66936766
* For debugging purposes, it may be useful to prevent unused
66946767
* regulators from being disabled.

include/linux/regulator/driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ struct regulator_dev {
635635
int ref_cnt;
636636
struct module *owner;
637637
struct device dev;
638+
struct device bdev;
638639
struct regulation_constraints *constraints;
639640
struct regulator *supply; /* for tree */
640641
const char *supply_name;

0 commit comments

Comments
 (0)