Skip to contentSkip to conversation

regulator: twl-regulator: Use DIV_ROUND_UP at appropriate places

PATCH New Axel Lin 1 participant 1 message Apr 10, 2012

Subsystems (2)
lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH v3 1/1 regulator: twl-regulator: Use DIV_ROUND_UP at appropriate places

Use DIV_ROUND_UP ensures we set the voltage falls within the specified range. Current code may set the voltage lower than the specified range.

For example:

vsel = (min_uV - 600000) / 125;
if (vsel % 100)

        vsel += 100;

vsel /= 100;
vsel++

In the case min_uV is 712599 uV, the vsel will be 10. Then current code set the voltage to 712500 uV which is lower than the specified range.

---
v3: Fix off-by-one for vsel, the vsel++ after DIV_ROUND_UP is required because
current code does subtract index by 1 in the list_voltage callback.
v2: Remove braces {} for single statement blocks

 drivers/regulator/twl-regulator.c |   20 ++++----------------
 1 files changed, 4 insertions(+), 16 deletions(-)
drivers/regulator/twl-regulator.c +4 -16
index 964946f..5d19b42 100644
@@ -806,10 +806,7 @@ twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
 			vsel = 0;
 		else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
 			int calc_uV;
-			vsel = (min_uV - 600000) / 125;
-			if (vsel % 100)
-				vsel += 100;
-			vsel /= 100;
+			vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
 			vsel++;
 			calc_uV = twl6030smps_list_voltage(rdev, vsel);
 			if (calc_uV > max_uV)
@@ -836,10 +833,7 @@ twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
 			vsel = 0;
 		else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
 			int calc_uV;
-			vsel = (min_uV - 700000) / 125;
-			if (vsel % 100)
-				vsel += 100;
-			vsel /= 100;
+			vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
 			vsel++;
 			calc_uV = twl6030smps_list_voltage(rdev, vsel);
 			if (calc_uV > max_uV)
@@ -865,10 +859,7 @@ twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
 		if (min_uV == 0)
 			vsel = 0;
 		else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
-			vsel = (min_uV - 1852000) / 386;
-			if (vsel % 100)
-				vsel += 100;
-			vsel /= 100;
+			vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
 			vsel++;
 		}
 		break;
@@ -876,10 +867,7 @@ twl6030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
 		if (min_uV == 0)
 			vsel = 0;
 		else if ((min_uV >= 2161000) && (max_uV <= 4321000)) {
-			vsel = (min_uV - 2161000) / 386;
-			if (vsel % 100)
-				vsel += 100;
-			vsel /= 100;
+			vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
 			vsel++;
 		}
 		break;
Signature · 1 line
1.7.5.4