Skip to contentSkip to conversation

rtc: em3027: correct month value

PATCH Superseded Ilya Ledvich 3 participants 8 messages Oct 31, 2019 – Nov 5, 2019 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rtc: em3027: correct month value

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +2 -2
index 77cca1392253..ef3792543f93 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_hour	= bcd2bin(buf[2]);
 	tm->tm_mday	= bcd2bin(buf[3]);
 	tm->tm_wday	= bcd2bin(buf[4]);
-	tm->tm_mon	= bcd2bin(buf[5]);
+	tm->tm_mon	= bcd2bin(buf[5]) - 1;
 	tm->tm_year	= bcd2bin(buf[6]) + 100;
 
 	return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
 	buf[3] = bin2bcd(tm->tm_hour);
 	buf[4] = bin2bcd(tm->tm_mday);
 	buf[5] = bin2bcd(tm->tm_wday);
-	buf[6] = bin2bcd(tm->tm_mon);
+	buf[6] = bin2bcd(tm->tm_mon) + 1;
 	buf[7] = bin2bcd(tm->tm_year % 100);
 
 	/* write time/date registers */
Signature · 1 line
2.20.1
#1 lore raw

Hi,

2019年10月31日(木) 16:57 Ilya Ledvich <[email protected]>:

Quoted 23 lines

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +1 -1
index 77cca1392253..ef3792543f93 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
        tm->tm_hour     = bcd2bin(buf[2]);
        tm->tm_mday     = bcd2bin(buf[3]);
        tm->tm_wday     = bcd2bin(buf[4]);
-       tm->tm_mon      = bcd2bin(buf[5]);
+       tm->tm_mon      = bcd2bin(buf[5]) - 1;
        tm->tm_year     = bcd2bin(buf[6]) + 100;
 
        return 0;

drivers/rtc/rtc-em3027.c static int em3027_set_time(struct device *dev, struct rtc_time *tm) · line 97

Quoted 5 lines
drivers/rtc/rtc-em3027.c +1 -1
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
        buf[3] = bin2bcd(tm->tm_hour);
        buf[4] = bin2bcd(tm->tm_mday);
        buf[5] = bin2bcd(tm->tm_wday);
-       buf[6] = bin2bcd(tm->tm_mon);
+       buf[6] = bin2bcd(tm->tm_mon) + 1;

I think '+'1 should be set in bin2bcd().

Quoted 6 lines
drivers/rtc/rtc-em3027.c +0 -1
        buf[7] = bin2bcd(tm->tm_year % 100);
 
        /* write time/date registers */
--

2.20.1

Best regards,
Nobuhiro

Signature · 3 lines
Nobuhiro Iwamatsu
   iwamatsu at {nigauri.org / debian.org}
   GPG ID: 40AD1FA6
#2 lore raw
Quoted 24 lines · Nobuhiro Iwamatsu

Hi,

2019年10月31日(木) 16:57 Ilya Ledvich <[email protected]>:

Quoted 21 lines

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +1 -1
index 77cca1392253..ef3792543f93 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
        tm->tm_hour     = bcd2bin(buf[2]);
        tm->tm_mday     = bcd2bin(buf[3]);
        tm->tm_wday     = bcd2bin(buf[4]);
-       tm->tm_mon      = bcd2bin(buf[5]);
+       tm->tm_mon      = bcd2bin(buf[5]) - 1;
        tm->tm_year     = bcd2bin(buf[6]) + 100;
 
Quoted 10 lines

drivers/rtc/rtc-em3027.c static int em3027_set_time(struct device *dev, struct rtc_time *tm) · line 97

Quoted 7 lines
drivers/rtc/rtc-em3027.c +1 -1
        return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
        buf[3] = bin2bcd(tm->tm_hour);
        buf[4] = bin2bcd(tm->tm_mday);
        buf[5] = bin2bcd(tm->tm_wday);
-       buf[6] = bin2bcd(tm->tm_mon);
+       buf[6] = bin2bcd(tm->tm_mon) + 1;

I think '+'1 should be set in bin2bcd().

This is correct.

Ilya, could you also run
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git/tree/rtc-range.c
on that rtc? I'm interested in the full output. It seems this RTC working range is 2000 to 2079

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#3 lore raw

Hi,

Quoted 33 lines · Alexandre Belloni
Quoted 23 lines · Nobuhiro Iwamatsu

Hi,

2019年10月31日(木) 16:57 Ilya Ledvich <[email protected]>:

Quoted 20 lines

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
  drivers/rtc/rtc-em3027.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +1 -1
index 77cca1392253..ef3792543f93 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
         tm->tm_hour     = bcd2bin(buf[2]);
         tm->tm_mday     = bcd2bin(buf[3]);
         tm->tm_wday     = bcd2bin(buf[4]);
-       tm->tm_mon      = bcd2bin(buf[5]);
+       tm->tm_mon      = bcd2bin(buf[5]) - 1;
         tm->tm_year     = bcd2bin(buf[6]) + 100;
 
Quoted 9 lines

drivers/rtc/rtc-em3027.c static int em3027_set_time(struct device *dev, struct rtc_time *tm) · line 97

Quoted 7 lines
drivers/rtc/rtc-em3027.c +1 -1
         return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
         buf[3] = bin2bcd(tm->tm_hour);
         buf[4] = bin2bcd(tm->tm_mday);
         buf[5] = bin2bcd(tm->tm_wday);
-       buf[6] = bin2bcd(tm->tm_mon);
+       buf[6] = bin2bcd(tm->tm_mon) + 1;

I think '+'1 should be set in bin2bcd().

Quoted 1 line

This is correct.

Thank you for the note. I'll fix it and send v2.

Quoted 5 lines

Ilya, could you also run
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git/tree/rtc-range.c
on that rtc? I'm interested in the full output. It seems this RTC working range is 2000 to 2079

According to the data sheet its range is 2000 to 2079 indeed. Please find the full output below.
---
Testing 2000-02-28 23:59:59.
OK

Testing 2038-01-19 03:14:07.
OK

Testing 2069-12-31 23:59:59.
OK

Testing 2099-12-31 23:59:59.
KO  Read back 2019-12-31 23:59:59.

Testing 2100-02-28 23:59:59.
KO  Read back 2000-02-28 23:59:59.

Testing 2106-02-07 06:28:15.
KO  Read back 2006-02-07 06:28:15.

Testing 2262-04-11 23:47:16.
KO  Read back 2062-04-11 23:47:16.

---

Kind regards,

#4 lore raw
In reply to Alexandre Belloni

Hi,

Quoted 33 lines · Alexandre Belloni
Quoted 23 lines · Nobuhiro Iwamatsu

Hi,

2019年10月31日(木) 16:57 Ilya Ledvich <[email protected]>:

Quoted 20 lines

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
  drivers/rtc/rtc-em3027.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +1 -1
index 77cca1392253..ef3792543f93 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
         tm->tm_hour     = bcd2bin(buf[2]);
         tm->tm_mday     = bcd2bin(buf[3]);
         tm->tm_wday     = bcd2bin(buf[4]);
-       tm->tm_mon      = bcd2bin(buf[5]);
+       tm->tm_mon      = bcd2bin(buf[5]) - 1;
         tm->tm_year     = bcd2bin(buf[6]) + 100;
 
Quoted 9 lines

drivers/rtc/rtc-em3027.c static int em3027_set_time(struct device *dev, struct rtc_time *tm) · line 97

Quoted 7 lines
drivers/rtc/rtc-em3027.c +1 -1
         return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
         buf[3] = bin2bcd(tm->tm_hour);
         buf[4] = bin2bcd(tm->tm_mday);
         buf[5] = bin2bcd(tm->tm_wday);
-       buf[6] = bin2bcd(tm->tm_mon);
+       buf[6] = bin2bcd(tm->tm_mon) + 1;

I think '+'1 should be set in bin2bcd().

Quoted 1 line

This is correct.

Thank you for the note. I'll fix it and send v2.

Quoted 5 lines

Ilya, could you also run
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/rtc-tools.git/tree/rtc-range.c
on that rtc? I'm interested in the full output. It seems this RTC working range is 2000 to 2079

According to the data sheet its range is 2000 to 2079 indeed.

Please find the full output below.

Testing 2000-02-28 23:59:59.
OK

Testing 2038-01-19 03:14:07.
OK

Testing 2069-12-31 23:59:59.
OK

Testing 2099-12-31 23:59:59.
KO  Read back 2019-12-31 23:59:59.

Testing 2100-02-28 23:59:59.
KO  Read back 2000-02-28 23:59:59.

Testing 2106-02-07 06:28:15.
KO  Read back 2006-02-07 06:28:15.

Testing 2262-04-11 23:47:16.
KO  Read back 2062-04-11 23:47:16.

Kind regards,

Ilya

#5 lore raw

PATCH v2 1/1 rtc: em3027: correct month value

In reply to Alexandre Belloni

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +2 -2
index 77cca1392253..9f176bce48ba 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_hour	= bcd2bin(buf[2]);
 	tm->tm_mday	= bcd2bin(buf[3]);
 	tm->tm_wday	= bcd2bin(buf[4]);
-	tm->tm_mon	= bcd2bin(buf[5]);
+	tm->tm_mon	= bcd2bin(buf[5]) - 1;
 	tm->tm_year	= bcd2bin(buf[6]) + 100;
 
 	return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
 	buf[3] = bin2bcd(tm->tm_hour);
 	buf[4] = bin2bcd(tm->tm_mday);
 	buf[5] = bin2bcd(tm->tm_wday);
-	buf[6] = bin2bcd(tm->tm_mon);
+	buf[6] = bin2bcd(tm->tm_mon + 1);
 	buf[7] = bin2bcd(tm->tm_year % 100);
 
 	/* write time/date registers */
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#6 lore raw

Hi,

2019年11月1日(金) 18:54 Ilya Ledvich <[email protected]>:

Quoted 5 lines

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

Best regards,
Nobuhiro

Quoted 27 lines
---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/rtc/rtc-em3027.c +2 -3
index 77cca1392253..9f176bce48ba 100644
@@ -71,7 +71,7 @@ static int em3027_get_time(struct device *dev, struct rtc_time *tm)
        tm->tm_hour     = bcd2bin(buf[2]);
        tm->tm_mday     = bcd2bin(buf[3]);
        tm->tm_wday     = bcd2bin(buf[4]);
-       tm->tm_mon      = bcd2bin(buf[5]);
+       tm->tm_mon      = bcd2bin(buf[5]) - 1;
        tm->tm_year     = bcd2bin(buf[6]) + 100;
 
        return 0;
@@ -94,7 +94,7 @@ static int em3027_set_time(struct device *dev, struct rtc_time *tm)
        buf[3] = bin2bcd(tm->tm_hour);
        buf[4] = bin2bcd(tm->tm_mday);
        buf[5] = bin2bcd(tm->tm_wday);
-       buf[6] = bin2bcd(tm->tm_mon);
+       buf[6] = bin2bcd(tm->tm_mon + 1);
        buf[7] = bin2bcd(tm->tm_year % 100);
 
        /* write time/date registers */
--
Quoted 2 lines

2.20.1

--
Nobuhiro Iwamatsu
iwamatsu at {nigauri.org / debian.org}
GPG ID: 40AD1FA6

maintainer Applied
#7 lore raw
In reply to Ilya Ledvich
Quoted 8 lines · Ilya Ledvich

The RTC month value is 1-indexed, but the kernel assumes it is 0-indexed. This may result in the RTC not rolling over correctly.

---
 drivers/rtc/rtc-em3027.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Applied, thanks.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com