From 895fe2321efaf62023fdd8239d1846394df68570 Mon Sep 17 00:00:00 2001 From: Mark Brown Date: Mon, 21 Mar 2016 18:17:43 +0000 Subject: [PATCH 1/5] regulator: core: Always flag voltage constraints as appliable Allow the core to always use the voltage constraints to set the voltage on startup. A forthcoming change in that code will ensure that we bring out of constraints voltages into spec with this setting. Signed-off-by: Mark Brown --- drivers/regulator/of_regulator.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c index 6b0aa80b22fd..d2ddefaaddaf 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -43,12 +43,10 @@ static void of_get_regulation_constraints(struct device_node *np, constraints->max_uV = pval; /* Voltage change possible? */ - if (constraints->min_uV != constraints->max_uV) + if (constraints->min_uV != constraints->max_uV) { constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; - /* Only one voltage? Then make sure it's set. */ - if (constraints->min_uV && constraints->max_uV && - constraints->min_uV == constraints->max_uV) constraints->apply_uV = true; + } if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval)) constraints->uV_offset = pval; From fa93fd4ecc9c58475abac6db93a797bff893bc16 Mon Sep 17 00:00:00 2001 From: Mark Brown Date: Mon, 21 Mar 2016 18:12:52 +0000 Subject: [PATCH 2/5] regulator: core: Ensure we are at least in bounds for our constraints Currently we only attempt to set the voltage during constraints application if an exact voltage is specified. Extend this so that if the currently set voltage for the regulator is outside the bounds set in constraints we will move the voltage to the nearest constraint, raising to the minimum or lowering to the maximum as needed. This ensures that drivers can probe without the hardware being driven out of spec. Reported-by: Ivaylo Dimitrov Tested-by: Ivaylo Dimitrov Signed-off-by: Mark Brown --- drivers/regulator/core.c | 32 +++++++++++++++++++++++++------- drivers/regulator/of_regulator.c | 2 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index e0b764284773..881c37e61f75 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -906,7 +906,8 @@ static int machine_constraints_voltage(struct regulator_dev *rdev, /* do we need to apply the constraint voltage */ if (rdev->constraints->apply_uV && - rdev->constraints->min_uV == rdev->constraints->max_uV) { + rdev->constraints->min_uV && rdev->constraints->max_uV) { + int target_min, target_max; int current_uV = _regulator_get_voltage(rdev); if (current_uV < 0) { rdev_err(rdev, @@ -914,15 +915,32 @@ static int machine_constraints_voltage(struct regulator_dev *rdev, current_uV); return current_uV; } - if (current_uV < rdev->constraints->min_uV || - current_uV > rdev->constraints->max_uV) { + + /* + * If we're below the minimum voltage move up to the + * minimum voltage, if we're above the maximum voltage + * then move down to the maximum. + */ + target_min = current_uV; + target_max = current_uV; + + if (current_uV < rdev->constraints->min_uV) { + target_min = rdev->constraints->min_uV; + target_max = rdev->constraints->min_uV; + } + + if (current_uV > rdev->constraints->max_uV) { + target_min = rdev->constraints->max_uV; + target_max = rdev->constraints->max_uV; + } + + if (target_min != current_uV || target_max != current_uV) { ret = _regulator_do_set_voltage( - rdev, rdev->constraints->min_uV, - rdev->constraints->max_uV); + rdev, target_min, target_max); if (ret < 0) { rdev_err(rdev, - "failed to apply %duV constraint(%d)\n", - rdev->constraints->min_uV, ret); + "failed to apply %d-%duV constraint(%d)\n", + target_min, target_max, ret); return ret; } } diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c index d2ddefaaddaf..f45106a44635 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -43,7 +43,7 @@ static void of_get_regulation_constraints(struct device_node *np, constraints->max_uV = pval; /* Voltage change possible? */ - if (constraints->min_uV != constraints->max_uV) { + if (constraints->min_uV && constraints->max_uV) { constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; constraints->apply_uV = true; } From 5e3ca2b349b1e2c80b060b51bbf2af37448fad85 Mon Sep 17 00:00:00 2001 From: Javier Martinez Canillas Date: Wed, 23 Mar 2016 20:59:34 -0300 Subject: [PATCH 3/5] regulator: Try to resolve regulators supplies on registration Commit 6261b06de565 ("regulator: Defer lookup of supply to regulator_get") moved the regulator supplies lookup logic from the regulators registration to the regulators get time. Unfortunately, that changed the behavior of the regulator core since now a parent supply with a child regulator marked as always-on, won't be enabled unless a client driver attempts to get the child regulator during boot. This patch tries to resolve the parent supply for the already registered regulators each time that a new regulator is registered. So the regulators that have child regulators marked as always on will be enabled regardless if a driver gets the child regulator or not. That was the behavior before the mentioned commit, since parent supplies were looked up at regulator registration time instead of during child get. Since regulator_resolve_supply() checks for rdev->supply, most of the times it will be a no-op. Errors aren't checked to keep the possible out of order dependencies which was the motivation for the mentioned commit. Also, the supply being available will be enforced on regulator get anyways in case the resolve fails on regulators registration. Fixes: 6261b06de565 ("regulator: Defer lookup of supply to regulator_get") Suggested-by: Mark Brown Signed-off-by: Javier Martinez Canillas Signed-off-by: Mark Brown Cc: # 4.1+ --- drivers/regulator/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index e0b764284773..ab1838138877 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -3840,6 +3840,11 @@ static void rdev_init_debugfs(struct regulator_dev *rdev) &rdev->bypass_count); } +static int regulator_register_resolve_supply(struct device *dev, void *data) +{ + return regulator_resolve_supply(dev_to_rdev(dev)); +} + /** * regulator_register - register regulator * @regulator_desc: regulator to register @@ -3986,6 +3991,10 @@ regulator_register(const struct regulator_desc *regulator_desc, } rdev_init_debugfs(rdev); + + /* try to resolve regulators supply since a new one was registered */ + class_for_each_device(®ulator_class, NULL, NULL, + regulator_register_resolve_supply); out: mutex_unlock(®ulator_list_mutex); kfree(config); From 45fa2038cf7820ecfcca8793b81e656ca3caaf0f Mon Sep 17 00:00:00 2001 From: Mark Brown Date: Wed, 30 Mar 2016 08:26:09 -0700 Subject: [PATCH 4/5] regulator: of: Don't flag voltage change as possible for exact voltages Flagging voltage changes as possible for exactly specified voltages appears to be triggering bugs in the SDHCI code (it should be able to handle the case where only one voltage it wants is in the range it is allowed to set) so make sure we only set the flag in cases where there's genuine variability. Signed-off-by: Mark Brown --- drivers/regulator/of_regulator.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c index f45106a44635..cd828dbf9d52 100644 --- a/drivers/regulator/of_regulator.c +++ b/drivers/regulator/of_regulator.c @@ -43,10 +43,12 @@ static void of_get_regulation_constraints(struct device_node *np, constraints->max_uV = pval; /* Voltage change possible? */ - if (constraints->min_uV && constraints->max_uV) { + if (constraints->min_uV != constraints->max_uV) constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; + + /* Do we have a voltage range, if so try to apply it? */ + if (constraints->min_uV && constraints->max_uV) constraints->apply_uV = true; - } if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval)) constraints->uV_offset = pval; From a2151374230820a3a6e654f2998b2a44dbfae4e1 Mon Sep 17 00:00:00 2001 From: Jon Hunter Date: Wed, 30 Mar 2016 17:09:13 +0100 Subject: [PATCH 5/5] regulator: Fix deadlock during regulator registration Commit 5e3ca2b349b1 ("regulator: Try to resolve regulators supplies on registration") added a call to regulator_resolve_supply() within regulator_register() where the regulator_list_mutex is held. This causes a deadlock to occur on the Tegra114 Dalmore board when the palmas PMIC is registered because regulator_register_resolve_supply() calls regulator_dev_lookup() which may try to acquire the regulator_list_mutex again. Fix this by releasing the mutex before calling regulator_register_resolve_supply() and update the error exit path to ensure the mutex is released on an error. [Made commit message more legible -- broonie] Signed-off-by: Jon Hunter Signed-off-by: Mark Brown --- drivers/regulator/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index ab1838138877..fd0e4e37f4e1 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -3991,12 +3991,11 @@ regulator_register(const struct regulator_desc *regulator_desc, } rdev_init_debugfs(rdev); + mutex_unlock(®ulator_list_mutex); /* try to resolve regulators supply since a new one was registered */ class_for_each_device(®ulator_class, NULL, NULL, regulator_register_resolve_supply); -out: - mutex_unlock(®ulator_list_mutex); kfree(config); return rdev; @@ -4007,15 +4006,16 @@ scrub: regulator_ena_gpio_free(rdev); device_unregister(&rdev->dev); /* device core frees rdev */ - rdev = ERR_PTR(ret); goto out; wash: regulator_ena_gpio_free(rdev); clean: kfree(rdev); - rdev = ERR_PTR(ret); - goto out; +out: + mutex_unlock(®ulator_list_mutex); + kfree(config); + return ERR_PTR(ret); } EXPORT_SYMBOL_GPL(regulator_register);