regulator: s5m8767: Use DIV_ROUND_UP to calculate selector
authorAxel Lin <axel.lin@gmail.com>
Fri, 13 Apr 2012 01:30:05 +0000 (09:30 +0800)
committerMark Brown <broonie@opensource.wolfsonmicro.com>
Mon, 23 Apr 2012 12:18:10 +0000 (13:18 +0100)
Integer division may truncate the result.
Use DIV_ROUND_UP to ensure new voltage setting falls within specified range.

Also properly handle the case min_vol < desc->min to ensure we don't return
negative value for selector.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
drivers/regulator/s5m8767.c

index cb53187a60d3f89b4e1a5a4cb6480f86bd0d9b06..ebe72d071b44b5354c9b19ee466cbdd3041af20a 100644 (file)
@@ -347,7 +347,10 @@ static int s5m8767_convert_voltage_to_sel(
        if (max_vol < desc->min || min_vol > desc->max)
                return -EINVAL;
 
-       selector = (min_vol - desc->min) / desc->step;
+       if (min_vol < desc->min)
+               min_vol = desc->min;
+
+       selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
 
        if (desc->min + desc->step * selector > max_vol)
                return -EINVAL;