Commit fedc459a authored by Alexandre Belloni's avatar Alexandre Belloni

rtc: pcf2123: fix negative offset rounding

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: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
parent e32e60a2
...@@ -161,7 +161,7 @@ static int pcf2123_set_offset(struct device *dev, long offset) ...@@ -161,7 +161,7 @@ static int pcf2123_set_offset(struct device *dev, long offset)
else if (offset < OFFSET_STEP * -128) else if (offset < OFFSET_STEP * -128)
reg = -128; reg = -128;
else 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 */ /* choose fine offset only for odd values in the normal range */
if (reg & 1 && reg <= 63 && reg >= -64) { if (reg & 1 && reg <= 63 && reg >= -64) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment