rtc: pcf2123: fix negative offset rounding
authorAlexandre Belloni <alexandre.belloni@bootlin.com>
Wed, 19 Jun 2019 13:17:53 +0000 (15:17 +0200)
committerAlexandre Belloni <alexandre.belloni@bootlin.com>
Wed, 19 Jun 2019 14:20:31 +0000 (16:20 +0200)
Using result = (value + divisor/2) / divisor is rounding values up and only
works well for positive values. Instead use DIV_ROUND_CLOSEST which does
the correct thing.

Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
drivers/rtc/rtc-pcf2123.c

index 3b314ce991e5aad4261ef39c2fb896d15bc8c2c3..e8100af789efc2f169834ca96a62f49b5e46d2f0 100644 (file)
@@ -161,7 +161,7 @@ static int pcf2123_set_offset(struct device *dev, long offset)
        else if (offset < OFFSET_STEP * -128)
                reg = -128;
        else
-               reg = (s8)((offset + (OFFSET_STEP >> 1)) / OFFSET_STEP);
+               reg = DIV_ROUND_CLOSEST(offset, OFFSET_STEP);
 
        /* choose fine offset only for odd values in the normal range */
        if (reg & 1 && reg <= 63 && reg >= -64) {