rtc: pcf8523: fix for stop bit
authorpaulmn <paulmn@axis.com>
Mon, 29 Aug 2022 12:46:39 +0000 (14:46 +0200)
committerAlexandre Belloni <alexandre.belloni@bootlin.com>
Wed, 16 Nov 2022 22:20:30 +0000 (23:20 +0100)
Bugfix for an issue detected when a goldcap capacitor gets
fully discharged due to a long absence of the power supply,
and then recharges again. The RTC failed to continue to keep
the real-time clock.

This was caused by the incorrect handling of the STOP bit in
the RTC internal register.  This fix solves the problem.

Signed-off-by: paulmn <paulmn@axis.com>
Link: https://lore.kernel.org/r/20220829124639.10906-1-paulmn@axis.com
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
drivers/rtc/rtc-pcf8523.c

index 6174b3fd4b98560dea3e65cc5dfd0dc1dd4fd1f3..92de99f11a7a5a64dcbe8aa97e8c8cde6c7ffc06 100644 (file)
@@ -99,24 +99,24 @@ static irqreturn_t pcf8523_irq(int irq, void *dev_id)
 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
        struct pcf8523 *pcf8523 = dev_get_drvdata(dev);
-       u8 regs[7];
+       u8 regs[10];
        int err;
 
-       err = regmap_bulk_read(pcf8523->regmap, PCF8523_REG_SECONDS, regs,
+       err = regmap_bulk_read(pcf8523->regmap, PCF8523_REG_CONTROL1, regs,
                               sizeof(regs));
        if (err < 0)
                return err;
 
-       if (regs[0] & PCF8523_SECONDS_OS)
+       if ((regs[0] & PCF8523_CONTROL1_STOP) || (regs[3] & PCF8523_SECONDS_OS))
                return -EINVAL;
 
-       tm->tm_sec = bcd2bin(regs[0] & 0x7f);
-       tm->tm_min = bcd2bin(regs[1] & 0x7f);
-       tm->tm_hour = bcd2bin(regs[2] & 0x3f);
-       tm->tm_mday = bcd2bin(regs[3] & 0x3f);
-       tm->tm_wday = regs[4] & 0x7;
-       tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1;
-       tm->tm_year = bcd2bin(regs[6]) + 100;
+       tm->tm_sec = bcd2bin(regs[3] & 0x7f);
+       tm->tm_min = bcd2bin(regs[4] & 0x7f);
+       tm->tm_hour = bcd2bin(regs[5] & 0x3f);
+       tm->tm_mday = bcd2bin(regs[6] & 0x3f);
+       tm->tm_wday = regs[7] & 0x7;
+       tm->tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
+       tm->tm_year = bcd2bin(regs[9]) + 100;
 
        return 0;
 }