mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hanna Hawa <hhhawa@amazon.com>
To: <a.zummo@towertech.it>, <alexandre.belloni@bootlin.com>
Cc: <linux-rtc@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<dwmw@amazon.co.uk>, <benh@amazon.com>, <eduval@amazon.com>,
	<ronenk@amazon.com>, <talel@amazon.com>, <jonnyc@amazon.com>,
	<hanochu@amazon.com>, <farbere@amazon.com>, <hhhawa@amazon.com>
Subject: [RFC PATCH 1/1] rtc: bq32k: update years calculation according century bit
Date: Mon, 7 Feb 2022 09:01:56 +0200	[thread overview]
Message-ID: <20220207070156.19373-1-hhhawa@amazon.com> (raw)

tm_year filed hold the number of years since 1900, in case the century
was changed the driver will return invalid year, as it will not
increment the years field by 200.

This change update the years calculation in bq32k_rtc_read_time() and
bq32k_rtc_write_time(). By increasing the years by 100 always, and only
if the century enable bit (BQ32K_CENT_EN) is set and century bit
(BQ32K_CENT) is cleared will increase again by 100 to represent the next
century.

Signed-off-by: Hanna Hawa <hhhawa@amazon.com>
---
 drivers/rtc/rtc-bq32k.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/rtc/rtc-bq32k.c b/drivers/rtc/rtc-bq32k.c
index 2235c968842d..09795dd2728b 100644
--- a/drivers/rtc/rtc-bq32k.c
+++ b/drivers/rtc/rtc-bq32k.c
@@ -108,8 +108,20 @@ static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_mday = bcd2bin(regs.date);
 	tm->tm_wday = bcd2bin(regs.day) - 1;
 	tm->tm_mon = bcd2bin(regs.month) - 1;
-	tm->tm_year = bcd2bin(regs.years) +
-				((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
+	/*
+	 * tm_year is number of years since 1900. Need to increase the years by
+	 * 100 always assuming we are on 20YY and not 19YY.
+	 */
+	tm->tm_year = bcd2bin(regs.years) + 100;
+
+	/*
+	 * If the century enable bit (BQ32K_CENT_EN) is set, and century bit
+	 * (BQ32K_CENT) is cleared, that means we are on the next century, which
+	 * required to increase by 100.
+	 */
+	if ((regs.cent_hours & BQ32K_CENT_EN) &&
+	    !(regs.cent_hours & BQ32K_CENT))
+		tm->tm_year += 100;
 
 	return 0;
 }
@@ -117,6 +129,7 @@ static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
 static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
 	struct bq32k_regs regs;
+	int year;
 
 	regs.seconds = bin2bcd(tm->tm_sec);
 	regs.minutes = bin2bcd(tm->tm_min);
@@ -125,11 +138,15 @@ static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	regs.date = bin2bcd(tm->tm_mday);
 	regs.month = bin2bcd(tm->tm_mon + 1);
 
-	if (tm->tm_year >= 100) {
+	/* Assume we are on 20YY and not 19YY */
+	year = tm->tm_year - 100;
+
+	if (year < 100) {
 		regs.cent_hours |= BQ32K_CENT;
-		regs.years = bin2bcd(tm->tm_year - 100);
-	} else
-		regs.years = bin2bcd(tm->tm_year);
+		regs.years = bin2bcd(year);
+	} else {
+		regs.years = bin2bcd(year - 100);
+	}
 
 	return bq32k_write(dev, &regs, 0, sizeof(regs));
 }
-- 
2.17.1


             reply	other threads:[~2022-02-07  7:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-07  7:01 Hanna Hawa [this message]
2022-03-09 21:29 ` Alexandre Belloni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220207070156.19373-1-hhhawa@amazon.com \
    --to=hhhawa@amazon.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@bootlin.com \
    --cc=benh@amazon.com \
    --cc=dwmw@amazon.co.uk \
    --cc=eduval@amazon.com \
    --cc=farbere@amazon.com \
    --cc=hanochu@amazon.com \
    --cc=jonnyc@amazon.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=ronenk@amazon.com \
    --cc=talel@amazon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®