Skip to content

Commit 0955f5b

Browse files
bebarinobroonie
authored andcommitted
regulator: Avoid grabbing regulator lock during suspend/resume
I see it takes about 5us per regulator to grab the lock, check that this regulator isn't going to do anything for suspend, and then release the lock. When that is combined with PMICs that have dozens of regulators we get into a state where we spend a few miliseconds doing a bunch of locking operations synchronously to figure out that there's nothing to do. Let's reorganize the code here a bit so that we don't grab the lock until we're actually going to do something so that suspend is a little faster. Signed-off-by: Stephen Boyd <swboyd@chromium.org> Reviewed-by: Douglas Anderson <dianders@chromium.org> Cc: Matthias Kaehlcke <mka@chromium.org> Cc: Douglas Anderson <dianders@chromium.org> Link: https://lore.kernel.org/r/20200804070837.1084024-1-swboyd@chromium.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent a16138a commit 0955f5b

1 file changed

Lines changed: 51 additions & 24 deletions

File tree

‎drivers/regulator/core.c‎

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,30 @@ regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
565565
}
566566
}
567567

568+
static const struct regulator_state *
569+
regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state)
570+
{
571+
const struct regulator_state *rstate;
572+
573+
rstate = regulator_get_suspend_state(rdev, state);
574+
if (rstate == NULL)
575+
return NULL;
576+
577+
/* If we have no suspend mode configuration don't set anything;
578+
* only warn if the driver implements set_suspend_voltage or
579+
* set_suspend_mode callback.
580+
*/
581+
if (rstate->enabled != ENABLE_IN_SUSPEND &&
582+
rstate->enabled != DISABLE_IN_SUSPEND) {
583+
if (rdev->desc->ops->set_suspend_voltage ||
584+
rdev->desc->ops->set_suspend_mode)
585+
rdev_warn(rdev, "No configuration\n");
586+
return NULL;
587+
}
588+
589+
return rstate;
590+
}
591+
568592
static ssize_t regulator_uV_show(struct device *dev,
569593
struct device_attribute *attr, char *buf)
570594
{
@@ -982,27 +1006,10 @@ static int drms_uA_update(struct regulator_dev *rdev)
9821006
return err;
9831007
}
9841008

985-
static int suspend_set_state(struct regulator_dev *rdev,
986-
suspend_state_t state)
1009+
static int __suspend_set_state(struct regulator_dev *rdev,
1010+
const struct regulator_state *rstate)
9871011
{
9881012
int ret = 0;
989-
struct regulator_state *rstate;
990-
991-
rstate = regulator_get_suspend_state(rdev, state);
992-
if (rstate == NULL)
993-
return 0;
994-
995-
/* If we have no suspend mode configuration don't set anything;
996-
* only warn if the driver implements set_suspend_voltage or
997-
* set_suspend_mode callback.
998-
*/
999-
if (rstate->enabled != ENABLE_IN_SUSPEND &&
1000-
rstate->enabled != DISABLE_IN_SUSPEND) {
1001-
if (rdev->desc->ops->set_suspend_voltage ||
1002-
rdev->desc->ops->set_suspend_mode)
1003-
rdev_warn(rdev, "No configuration\n");
1004-
return 0;
1005-
}
10061013

10071014
if (rstate->enabled == ENABLE_IN_SUSPEND &&
10081015
rdev->desc->ops->set_suspend_enable)
@@ -1037,6 +1044,18 @@ static int suspend_set_state(struct regulator_dev *rdev,
10371044
return ret;
10381045
}
10391046

1047+
static int suspend_set_initial_state(struct regulator_dev *rdev)
1048+
{
1049+
const struct regulator_state *rstate;
1050+
1051+
rstate = regulator_get_suspend_state_check(rdev,
1052+
rdev->constraints->initial_state);
1053+
if (!rstate)
1054+
return 0;
1055+
1056+
return __suspend_set_state(rdev, rstate);
1057+
}
1058+
10401059
static void print_constraints(struct regulator_dev *rdev)
10411060
{
10421061
struct regulation_constraints *constraints = rdev->constraints;
@@ -1319,7 +1338,7 @@ static int set_machine_constraints(struct regulator_dev *rdev,
13191338

13201339
/* do we need to setup our suspend state */
13211340
if (rdev->constraints->initial_state) {
1322-
ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1341+
ret = suspend_set_initial_state(rdev);
13231342
if (ret < 0) {
13241343
rdev_err(rdev, "failed to set suspend state\n");
13251344
return ret;
@@ -5362,9 +5381,14 @@ static int regulator_suspend(struct device *dev)
53625381
struct regulator_dev *rdev = dev_to_rdev(dev);
53635382
suspend_state_t state = pm_suspend_target_state;
53645383
int ret;
5384+
const struct regulator_state *rstate;
5385+
5386+
rstate = regulator_get_suspend_state_check(rdev, state);
5387+
if (!rstate)
5388+
return 0;
53655389

53665390
regulator_lock(rdev);
5367-
ret = suspend_set_state(rdev, state);
5391+
ret = __suspend_set_state(rdev, rstate);
53685392
regulator_unlock(rdev);
53695393

53705394
return ret;
@@ -5381,11 +5405,14 @@ static int regulator_resume(struct device *dev)
53815405
if (rstate == NULL)
53825406
return 0;
53835407

5408+
/* Avoid grabbing the lock if we don't need to */
5409+
if (!rdev->desc->ops->resume)
5410+
return 0;
5411+
53845412
regulator_lock(rdev);
53855413

5386-
if (rdev->desc->ops->resume &&
5387-
(rstate->enabled == ENABLE_IN_SUSPEND ||
5388-
rstate->enabled == DISABLE_IN_SUSPEND))
5414+
if (rstate->enabled == ENABLE_IN_SUSPEND ||
5415+
rstate->enabled == DISABLE_IN_SUSPEND)
53895416
ret = rdev->desc->ops->resume(rdev);
53905417

53915418
regulator_unlock(rdev);

0 commit comments

Comments
 (0)