Skip to contentSkip to conversation

rtc: pcf2123: fix negative offset rounding

PATCH New Alexandre Belloni 1 participant 1 message Jun 19, 2019 Real time clock (RTC)

lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/1 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.

---
 drivers/rtc/rtc-pcf2123.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rtc/rtc-pcf2123.c +1 -1
index 39da8b214275..80a53d2dc813 100644
@@ -244,7 +244,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) {
Signature · 1 line
2.21.0