diff mbox series

[3/3] rtc: sh: set range

Message ID 20190320113010.8955-3-alexandre.belloni@bootlin.com (mailing list archive)
State Accepted
Delegated to: Geert Uytterhoeven
Headers show
Series [1/3] rtc: sh: stop resetting time to epoch | expand

Commit Message

Alexandre Belloni March 20, 2019, 11:30 a.m. UTC
The SH RTC is a BCD RTC with some version having 4 digits for the year.

The range for the RTCs with only 2 digits for the year was unfortunately
shifted to handle 1999 to 2098.

Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
---
 drivers/rtc/rtc-sh.c | 8 ++++++++
 include/linux/rtc.h  | 1 +
 2 files changed, 9 insertions(+)

Comments

Geert Uytterhoeven March 21, 2019, 10:22 a.m. UTC | #1
On Wed, Mar 20, 2019 at 12:30 PM Alexandre Belloni
<alexandre.belloni@bootlin.com> wrote:
> The SH RTC is a BCD RTC with some version having 4 digits for the year.
>
> The range for the RTCs with only 2 digits for the year was unfortunately
> shifted to handle 1999 to 2098.
>
> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

> --- a/include/linux/rtc.h
> +++ b/include/linux/rtc.h
> @@ -165,6 +165,7 @@ struct rtc_device {
>  #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */

mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??

Is this due to leap seconds, and mktime64() is valid for 1970 and later only?

Gr{oetje,eeting}s,

                        Geert
Alexandre Belloni March 22, 2019, 6:12 a.m. UTC | #2
On 21/03/2019 11:22:26+0100, Geert Uytterhoeven wrote:
> On Wed, Mar 20, 2019 at 12:30 PM Alexandre Belloni
> <alexandre.belloni@bootlin.com> wrote:
> > The SH RTC is a BCD RTC with some version having 4 digits for the year.
> >
> > The range for the RTCs with only 2 digits for the year was unfortunately
> > shifted to handle 1999 to 2098.
> >
> > Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
> 
> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
> 
> > --- a/include/linux/rtc.h
> > +++ b/include/linux/rtc.h
> > @@ -165,6 +165,7 @@ struct rtc_device {
> >  #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */
> 
> mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??
> 
> Is this due to leap seconds, and mktime64() is valid for 1970 and later only?
> 

That's a bug, it seems I didn't use the correct timezone when doing the
calculation. Thanks for spotting that, you can send a patch to correct
it or I can do it.
Geert Uytterhoeven March 22, 2019, 7:26 a.m. UTC | #3
Hi Alexander,

On Fri, Mar 22, 2019 at 7:12 AM Alexandre Belloni
<alexandre.belloni@bootlin.com> wrote:
> On 21/03/2019 11:22:26+0100, Geert Uytterhoeven wrote:
> > On Wed, Mar 20, 2019 at 12:30 PM Alexandre Belloni
> > <alexandre.belloni@bootlin.com> wrote:
> > > The SH RTC is a BCD RTC with some version having 4 digits for the year.
> > >
> > > The range for the RTCs with only 2 digits for the year was unfortunately
> > > shifted to handle 1999 to 2098.
> > >
> > > Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
> >
> > Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
> >
> > > --- a/include/linux/rtc.h
> > > +++ b/include/linux/rtc.h
> > > @@ -165,6 +165,7 @@ struct rtc_device {
> > >  #define RTC_TIMESTAMP_BEGIN_1900       -2208989361LL /* 1900-01-01 00:00:00 */
> >
> > mktime64(1900, 1, 1, 0, 0, 0) = -2208988800 ??
> >
> > Is this due to leap seconds, and mktime64() is valid for 1970 and later only?
> >
>
> That's a bug, it seems I didn't use the correct timezone when doing the
> calculation. Thanks for spotting that, you can send a patch to correct
> it or I can do it.

Thanks for confirming, done.

Gr{oetje,eeting}s,

                        Geert
diff mbox series

Patch

diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c
index b582af10ddb5..37964a867de7 100644
--- a/drivers/rtc/rtc-sh.c
+++ b/drivers/rtc/rtc-sh.c
@@ -601,6 +601,14 @@  static int __init sh_rtc_probe(struct platform_device *pdev)
 	rtc->rtc_dev->ops = &sh_rtc_ops;
 	rtc->rtc_dev->max_user_freq = 256;
 
+	if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
+		rtc->rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_1900;
+		rtc->rtc_dev->range_max = RTC_TIMESTAMP_END_9999;
+	} else {
+		rtc->rtc_dev->range_min = mktime64(1999, 1, 1, 0, 0, 0);
+		rtc->rtc_dev->range_max = mktime64(2098, 12, 31, 23, 59, 59);
+	}
+
 	ret = rtc_register_device(rtc->rtc_dev);
 	if (ret)
 		goto err_unmap;
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index f89bfbb54902..588120ba372c 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -165,6 +165,7 @@  struct rtc_device {
 #define RTC_TIMESTAMP_BEGIN_1900	-2208989361LL /* 1900-01-01 00:00:00 */
 #define RTC_TIMESTAMP_BEGIN_2000	946684800LL /* 2000-01-01 00:00:00 */
 #define RTC_TIMESTAMP_END_2099		4102444799LL /* 2099-12-31 23:59:59 */
+#define RTC_TIMESTAMP_END_9999		253402300799LL /* 9999-12-31 23:59:59 */
 
 extern struct rtc_device *devm_rtc_device_register(struct device *dev,
 					const char *name,