Skip to content

Commit fa93fd4

Browse files
committed
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 <[email protected]> Tested-by: Ivaylo Dimitrov <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 895fe23 commit fa93fd4

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

drivers/regulator/core.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -906,23 +906,41 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
906906

907907
/* do we need to apply the constraint voltage */
908908
if (rdev->constraints->apply_uV &&
909-
rdev->constraints->min_uV == rdev->constraints->max_uV) {
909+
rdev->constraints->min_uV && rdev->constraints->max_uV) {
910+
int target_min, target_max;
910911
int current_uV = _regulator_get_voltage(rdev);
911912
if (current_uV < 0) {
912913
rdev_err(rdev,
913914
"failed to get the current voltage(%d)\n",
914915
current_uV);
915916
return current_uV;
916917
}
917-
if (current_uV < rdev->constraints->min_uV ||
918-
current_uV > rdev->constraints->max_uV) {
918+
919+
/*
920+
* If we're below the minimum voltage move up to the
921+
* minimum voltage, if we're above the maximum voltage
922+
* then move down to the maximum.
923+
*/
924+
target_min = current_uV;
925+
target_max = current_uV;
926+
927+
if (current_uV < rdev->constraints->min_uV) {
928+
target_min = rdev->constraints->min_uV;
929+
target_max = rdev->constraints->min_uV;
930+
}
931+
932+
if (current_uV > rdev->constraints->max_uV) {
933+
target_min = rdev->constraints->max_uV;
934+
target_max = rdev->constraints->max_uV;
935+
}
936+
937+
if (target_min != current_uV || target_max != current_uV) {
919938
ret = _regulator_do_set_voltage(
920-
rdev, rdev->constraints->min_uV,
921-
rdev->constraints->max_uV);
939+
rdev, target_min, target_max);
922940
if (ret < 0) {
923941
rdev_err(rdev,
924-
"failed to apply %duV constraint(%d)\n",
925-
rdev->constraints->min_uV, ret);
942+
"failed to apply %d-%duV constraint(%d)\n",
943+
target_min, target_max, ret);
926944
return ret;
927945
}
928946
}

drivers/regulator/of_regulator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void of_get_regulation_constraints(struct device_node *np,
4343
constraints->max_uV = pval;
4444

4545
/* Voltage change possible? */
46-
if (constraints->min_uV != constraints->max_uV) {
46+
if (constraints->min_uV && constraints->max_uV) {
4747
constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
4848
constraints->apply_uV = true;
4949
}

0 commit comments

Comments
 (0)