[PATCH] Fixup regulator_balance_voltage()

From: Dmitry Osipenko
Date: Fri Sep 28 2018 - 14:49:20 EST


---
drivers/regulator/core.c | 39 ++++++++++++++++++++++++---------------
1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 282511508698..4a386fe9f26a 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -3187,7 +3187,8 @@ static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
return ret;
}

-static int regulator_get_optimal_voltage(struct regulator_dev *rdev)
+static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
+ int *min_uV, int *max_uV)
{
struct coupling_desc *c_desc = &rdev->coupling_desc;
struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
@@ -3211,7 +3212,9 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev)
* by consumers.
*/
if (n_coupled == 1) {
- ret = desired_min_uV;
+ *min_uV = desired_min_uV;
+ *max_uV = desired_max_uV;
+ ret = 1;
goto out;
}

@@ -3285,8 +3288,10 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev)
ret = -EINVAL;
goto out;
}
- ret = possible_uV;
+ ret = (possible_uV == desired_min_uV);

+ *min_uV = possible_uV;
+ *max_uV = desired_max_uV;
out:
return ret;
}
@@ -3298,7 +3303,8 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
struct regulator_dev *best_rdev;
struct coupling_desc *c_desc = &rdev->coupling_desc;
int n_coupled;
- int i, best_delta, best_uV, ret = 1;
+ int i, best_delta, best_min_uV, best_max_uV, ret = 1;
+ bool last = false;

c_rdevs = c_desc->coupled_rdevs;
n_coupled = c_desc->n_coupled;
@@ -3314,9 +3320,10 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
* Find the best possible voltage change on each loop. Leave the loop
* if there isn't any possible change.
*/
- while (1) {
+ while (!last) {
best_delta = 0;
- best_uV = 0;
+ best_min_uV = 0;
+ best_max_uV = 0;
best_rdev = NULL;

/*
@@ -3330,24 +3337,26 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
* max_spread constraint in order to balance
* the coupled voltages.
*/
- int optimal_uV, current_uV;
+ int optimal_uV, optimal_max_uV, current_uV;

- optimal_uV = regulator_get_optimal_voltage(c_rdevs[i]);
- if (optimal_uV < 0) {
- ret = optimal_uV;
+ ret = regulator_get_optimal_voltage(c_rdevs[i],
+ &optimal_uV,
+ &optimal_max_uV);
+ if (ret < 0)
goto out;
- }

current_uV = _regulator_get_voltage(c_rdevs[i]);
if (current_uV < 0) {
- ret = optimal_uV;
+ ret = current_uV;
goto out;
}

if (abs(best_delta) < abs(optimal_uV - current_uV)) {
best_delta = optimal_uV - current_uV;
best_rdev = c_rdevs[i];
- best_uV = optimal_uV;
+ best_min_uV = optimal_uV;
+ best_max_uV = optimal_max_uV;
+ last = (best_rdev == rdev && ret > 0);
}
}

@@ -3357,8 +3366,8 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
goto out;
}

- ret = regulator_set_voltage_rdev(best_rdev, best_uV,
- best_uV, state);
+ ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
+ best_max_uV, state);

if (ret < 0)
goto out;
--
2.19.0