2024-06-11 07:24:49

by Csókás Bence

[permalink] [raw]
Subject: [PATCH v2 1/2] rtc: ds1307: Detect oscillator fail on mcp794xx

This patch enables the detection of the oscillator failure on mcp794xx chips.

Co-developed-by: Szentendrei, Tamás <[email protected]>
Signed-off-by: Szentendrei, Tamás <[email protected]>
Signed-off-by: Csókás, Bence <[email protected]>
---
drivers/rtc/rtc-ds1307.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index 506b7d1c2397..bdb7b201a160 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -65,6 +65,7 @@ enum ds_type {
# define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
# define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
#define DS1307_REG_WDAY 0x03 /* 01-07 */
+# define MCP794XX_BIT_OSCRUN BIT(5)
# define MCP794XX_BIT_VBATEN 0x08
#define DS1307_REG_MDAY 0x04 /* 01-31 */
#define DS1307_REG_MONTH 0x05 /* 01-12 */
@@ -242,6 +243,10 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
dev_warn_once(dev, "oscillator failed, set time!\n");
return -EINVAL;
+ } else if (ds1307->type == mcp794xx &&
+ !(regs[DS1307_REG_WDAY] & MCP794XX_BIT_OSCRUN)) {
+ dev_warn_once(dev, "oscillator failed, set time!\n");
+ return -EINVAL;
}

tmp = regs[DS1307_REG_SECS];
--
2.34.1




2024-06-11 07:44:43

by Csókás Bence

[permalink] [raw]
Subject: [PATCH v2 2/2] rtc: ds1307: Clamp year to valid BCD (0-99) in `set_time()`

`tm_year` may go up to 299 if the device supports the
century bit. Therefore, subtracting may not give us
a valid 2-digit number, but modulo does.

Co-developed-by: Szentendrei, Tamás <[email protected]>
Signed-off-by: Szentendrei, Tamás <[email protected]>
Signed-off-by: Csókás, Bence <[email protected]>
---
drivers/rtc/rtc-ds1307.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index bdb7b201a160..872e0b679be4 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -359,7 +359,7 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);

/* assume 20YY not 19YY */
- tmp = t->tm_year - 100;
+ tmp = t->tm_year % 100;
regs[DS1307_REG_YEAR] = bin2bcd(tmp);

if (chip->century_enable_bit)
--
2.34.1