[PATCH 2/2] regulator: as3711: Fix the logic in as3711_sel_check

Axel Lin
2012-11-23T16:53:29Z

Below equation means the "voltage" is the "smallest" voltage within specific
range.

ret = DIV_ROUND_UP(min - bottom) / step;
voltage = ret * step + bottom;

If we do try 1 down when (voltage > max), new voltage is then less than min
voltage. Which means the new voltage is not in the requested voltage range.

This patch also includes below cleanups:
- Use DIV_ROUND_UP
- rename variable 'ret' to 'sel' for better readability because as3711_sel_check
returns the selector.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
---
drivers/regulator/as3711-regulator.c | 23 ++++++++---------------
1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/drivers/regulator/as3711-regulator.c b/drivers/regulator/as3711-regulator.c
index 5e813b9..2f1341d 100644
--- a/drivers/regulator/as3711-regulator.c
+++ b/drivers/regulator/as3711-regulator.c
@@ -93,24 +93,17 @@ static int as3711_bound_check(struct regulator_dev *rdev,

static int as3711_sel_check(int min, int max, int bottom, int step)
{
- int ret, voltage;
+ int sel, voltage;

/* Round up min, when dividing: keeps us within the range */
- ret = (min - bottom + step - 1) / step;
- voltage = ret * step + bottom;
+ sel = DIV_ROUND_UP(min - bottom, step);
+ voltage = sel * step + bottom;
pr_debug("%s(): select %d..%d in %d+N*%d: %d\n", __func__,
- min, max, bottom, step, ret);
- if (voltage > max) {
- /*
- * Try 1 down. It will take us below min, but as long we stay
- * above bottom, we're fine.
- */
- ret--;
- voltage = ret * step + bottom;
- if (voltage < bottom)
- return -EINVAL;
- }
- return ret;
+ min, max, bottom, step, sel);
+ if (voltage > max)
+ return -EINVAL;
+
+ return sel;
}

static int as3711_map_voltage_sd(struct regulator_dev *rdev,
--
1.7.9.5