diff mbox

rtc: pcf8523: refuse to write dates later than 2099

Message ID 1446827876-31613-1-git-send-email-u.kleine-koenig@pengutronix.de
State Accepted
Headers show

Commit Message

Uwe Kleine-König Nov. 6, 2015, 4:37 p.m. UTC
When the chip increments the YEAR register and it already holds
bin2bcd(99) it reads as 0 afterwards. With this behaviour the last valid
day (without trickery) that has a representation is 2099-12-31 23:59:59.
So refuse to write later dates.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
Hello,

When setting the YEAR register to bin2bcd(100) == 0xa0 it increments
just fine up to bin2bcd(159) == 0xf9 but these values are forbidden in
the manual. But this doesn't help to expand (or shift) the range of
supported dates.

An alternative to this patch would be to save tm_year % 100 because
that's what happens when waiting long enough anyhow. Would that be
better? Do we already care at all for year-2100-problems?

Best regards
Uwe

 drivers/rtc/rtc-pcf8523.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Alexandre Belloni Nov. 24, 2015, 11:35 p.m. UTC | #1
Hi,

On 06/11/2015 at 17:37:56 +0100, Uwe Kleine-König wrote :
> When the chip increments the YEAR register and it already holds
> bin2bcd(99) it reads as 0 afterwards. With this behaviour the last valid
> day (without trickery) that has a representation is 2099-12-31 23:59:59.
> So refuse to write later dates.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
> Hello,
> 
> When setting the YEAR register to bin2bcd(100) == 0xa0 it increments
> just fine up to bin2bcd(159) == 0xf9 but these values are forbidden in
> the manual. But this doesn't help to expand (or shift) the range of
> supported dates.
> 
> An alternative to this patch would be to save tm_year % 100 because
> that's what happens when waiting long enough anyhow. Would that be
> better? Do we already care at all for year-2100-problems?
> 

There is no way to tell whether the date is fine after February
2100 so I will take the patch as is.
diff mbox

Patch

diff --git a/drivers/rtc/rtc-pcf8523.c b/drivers/rtc/rtc-pcf8523.c
index 4cdb64be061b..261d65e67c8b 100644
--- a/drivers/rtc/rtc-pcf8523.c
+++ b/drivers/rtc/rtc-pcf8523.c
@@ -219,6 +219,17 @@  static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	u8 regs[8];
 	int err;
 
+	/*
+	 * The hardware can only store values between 0 and 99 in it's YEAR
+	 * register (with 99 overflowing to 0 on increment).
+	 * After 2100-02-28 we could start interpreting the year to be in the
+	 * interval [2100, 2199], but there is no path to switch in a smooth way
+	 * because the chip handles YEAR=0x00 (and the out-of-spec
+	 * YEAR=0xa0) as a leap year, but 2100 isn't.
+	 */
+	if (tm->tm_year < 100 || tm->tm_year >= 200)
+		return -EINVAL;
+
 	err = pcf8523_stop_rtc(client);
 	if (err < 0)
 		return err;