linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xing Zheng <zhengxing@rock-chips.com>
To: heiko@sntech.de
Cc: a.zummo@towertech.it, alexandre.belloni@free-electrons.com,
	rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org,
	zhengxing@rock-chips.com
Subject: [PATCH v2 9/9] rtc: hym8563: make sure hym8563 can be normal work
Date: Thu, 17 Sep 2015 18:39:28 +0800	[thread overview]
Message-ID: <1442486368-1912-1-git-send-email-zhengxing@rock-chips.com> (raw)
In-Reply-To: <1442478540-15068-1-git-send-email-zhengxing@rock-chips.com>

The rtc hym8563 maybe failed to register if first startup or rtc
powerdown:
[    0.988540 ] rtc-hym8563 1-0051: no valid clock/calendar values available
[    0.995642 ] rtc-hym8563 1-0051: rtc core: registered hym8563 as rtc0
[    1.078985 ] rtc-hym8563 1-0051: no valid clock/calendar values available
[    1.085698 ] rtc-hym8563 1-0051: hctosys: unable to read the hardware clock

We can set initial time for rtc and register it:
[    0.995678 ] rtc-hym8563 1-0051: rtc core: registered hym8563 as rtc0
[    1.080313 ] rtc-hym8563 1-0051: setting system clock to 2000-01-01 00:02:00 UTC (946684920)

---

Changes in v2:
Signed-off-by: Xing Zheng <zhengxing@rock-chips.com>

 drivers/rtc/rtc-hym8563.c |   93 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/drivers/rtc/rtc-hym8563.c b/drivers/rtc/rtc-hym8563.c
index 097325d..ea37fdf 100644
--- a/drivers/rtc/rtc-hym8563.c
+++ b/drivers/rtc/rtc-hym8563.c
@@ -83,6 +83,8 @@
 
 #define HYM8563_TMR_CNT		0x0f
 
+#define HYM8563_RTC_SECTION_LEN	0x07
+
 struct hym8563 {
 	struct i2c_client	*client;
 	struct rtc_device	*rtc;
@@ -527,9 +529,77 @@ static int hym8563_resume(struct device *dev)
 
 static SIMPLE_DEV_PM_OPS(hym8563_pm_ops, hym8563_suspend, hym8563_resume);
 
+static int hym8563_set_time(struct i2c_client *client, struct rtc_time *tm)
+{
+	u8 regs[HYM8563_RTC_SECTION_LEN] = {0};
+	u8 mon_day;
+	int ret = 0;
+
+	dev_dbg(&client->dev, "%s -- %4d-%02d-%02d(%d) %02d:%02d:%02d\n", __func__,
+		1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, tm->tm_wday,
+		tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+	mon_day = rtc_month_days((tm->tm_mon), tm->tm_year + 1900);
+
+	if (tm->tm_sec >= 60 || tm->tm_sec < 0)
+		regs[0x00] = bin2bcd(0x00);
+	else
+		regs[0x00] = bin2bcd(tm->tm_sec);
+
+	if (tm->tm_min >= 60 || tm->tm_min < 0)
+		regs[0x01] = bin2bcd(0x00);
+	else
+		regs[0x01] = bin2bcd(tm->tm_min);
+
+	if (tm->tm_hour >= 24 || tm->tm_hour < 0)
+		regs[0x02] = bin2bcd(0x00);
+	else
+		regs[0x02] = bin2bcd(tm->tm_hour);
+
+	if ((tm->tm_mday) > mon_day)
+		regs[0x03] = bin2bcd(mon_day);
+	else if ((tm->tm_mday) > 0)
+		regs[0x03] = bin2bcd(tm->tm_mday);
+	else if ((tm->tm_mday) <= 0)
+		regs[0x03] = bin2bcd(0x01);
+
+	if (tm->tm_year >= 200)
+		regs[0x06] = bin2bcd(99);
+	else if (tm->tm_year >= 100)
+		regs[0x06] = bin2bcd(tm->tm_year - 100);
+	else if (tm->tm_year >= 0) {
+		regs[0x06] = bin2bcd(tm->tm_year);
+		regs[0x05] |= 0x80;
+	} else {
+		regs[0x06] = bin2bcd(0);
+		regs[0x05] |= 0x80;
+	}
+
+	regs[0x04] = bin2bcd(tm->tm_wday);
+	regs[0x05] = (regs[0x05] & 0x80) | (bin2bcd(tm->tm_mon + 1) & 0x7F);
+
+	ret = i2c_smbus_write_byte_data(client, HYM8563_SEC, *regs);
+	if (ret < 0) {
+		dev_err(&client->dev, "%s: error writing i2c data %d\n",
+			__func__, ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int hym8563_probe(struct i2c_client *client,
 			 const struct i2c_device_id *id)
 {
+	struct rtc_time tm_read, tm = {
+		.tm_sec = 0,
+		.tm_min = 0,
+		.tm_hour = 12,
+		.tm_mday = 1,
+		.tm_mon = 0,
+		.tm_year = 111,
+		.tm_wday = 6,
+	};
 	struct hym8563 *hym8563;
 	int ret;
 
@@ -562,6 +632,7 @@ static int hym8563_probe(struct i2c_client *client,
 
 	/* check state of calendar information */
 	ret = i2c_smbus_read_byte_data(client, HYM8563_SEC);
+
 	if (ret < 0)
 		return ret;
 
@@ -569,6 +640,28 @@ static int hym8563_probe(struct i2c_client *client,
 	dev_dbg(&client->dev, "rtc information is %s\n",
 		hym8563->valid ? "valid" : "invalid");
 
+	if (!hym8563->valid) {
+		ret = hym8563_set_time(client, &tm);
+		if (ret < 0) {
+			dev_err(&client->dev, "rtc set time failed, %d\n", ret);
+			return ret;
+		}
+
+		hym8563->valid = true;
+	}
+
+	/* read current time */
+	ret = hym8563_rtc_read_time(&client->dev, &tm_read);
+	if (ret < 0) {
+		dev_err(&client->dev, "rtc read time failed, %d\n", ret);
+		return ret;
+	}
+
+	dev_dbg(&client->dev, "tm_sec = %d, tm_min = %d, tm_hour = %d,tm_mday = %d\n",
+		tm_read.tm_sec, tm_read.tm_min, tm_read.tm_hour, tm_read.tm_mday);
+	dev_dbg(&client->dev, "tm_mon = %d, tm_year = %d, tm_wday = %d\n",
+		tm_read.tm_mon, tm_read.tm_year, tm_read.tm_wday);
+
 	hym8563->rtc = devm_rtc_device_register(&client->dev, client->name,
 						&hym8563_rtc_ops, THIS_MODULE);
 	if (IS_ERR(hym8563->rtc))
-- 
1.7.9.5



  parent reply	other threads:[~2015-09-17 10:39 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-17  8:28 [PATCH v2 0/9] Build and support rk3036 SoC platform Xing Zheng
2015-09-17  8:28 ` [PATCH v2 1/9] ARM: dts: rockchip: add core rk3036 dts Xing Zheng
2015-09-17  9:18   ` Heiko Stübner
2015-09-24  2:18     ` Xing Zheng
2015-09-17  8:28 ` [PATCH v2 2/9] clk: rockchip: add dt-binding header for rk3036 Xing Zheng
2015-09-17  9:25   ` Heiko Stübner
2015-09-24  2:17     ` Xing Zheng
2015-09-17  8:28 ` [PATCH v2 3/9] clk: rockchip: add clock controller " Xing Zheng
2015-09-17  9:47   ` Heiko Stübner
2015-09-24  3:04     ` Xing Zheng
2015-09-24  3:31       ` Xing Zheng
2015-10-07 10:24         ` Heiko Stuebner
2015-09-17  8:28 ` [PATCH v2 4/9] clk: rockchip: add new clock type and " Xing Zheng
2015-09-17  9:54   ` Heiko Stübner
2015-09-22 22:41   ` Stephen Boyd
2015-09-22 22:58     ` Heiko Stübner
2015-09-22 23:19       ` Stephen Boyd
2015-09-30 23:32         ` Heiko Stübner
2015-10-01  0:51           ` Stephen Boyd
2015-09-17  9:59 ` [PATCH v2 0/9] Build and support rk3036 SoC platform Heiko Stübner
2015-09-17 10:32 ` [PATCH v2 5/9] dt-bindings: add documentation of rk3036 clock controller Xing Zheng
2015-09-17 15:09   ` Heiko Stübner
2015-09-24  3:42     ` Xing Zheng
2015-09-17 10:34 ` [PATCH v2 6/9] pinctrl: rockchip: add support for the rk3036 Xing Zheng
2015-09-17 12:47   ` Heiko Stübner
2015-09-17 10:37 ` [PATCH v2 7/9] rockchip: make sure timer5 is enabled on rk3036 platforms Xing Zheng
2015-09-17 15:05   ` Heiko Stübner
2015-09-28 12:25     ` Xing Zheng
2015-09-28 12:44       ` Heiko Stübner
2015-09-28 12:53         ` Xing Zheng
2015-09-17 10:38 ` [PATCH v2 8/9] ARM: rockchip: add support smp for rk3036 Xing Zheng
2015-09-17 20:15   ` Heiko Stübner
2015-09-28 11:50     ` Xing Zheng
2015-09-17 10:39 ` Xing Zheng [this message]
2015-09-17 12:07   ` [PATCH v2 9/9] rtc: hym8563: make sure hym8563 can be normal work Heiko Stübner
2015-09-17 12:31     ` Alexandre Belloni
2015-09-17 12:44       ` Xing Zheng

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=1442486368-1912-1-git-send-email-zhengxing@rock-chips.com \
    --to=zhengxing@rock-chips.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@free-electrons.com \
    --cc=heiko@sntech.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.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;
as well as URLs for NNTP newsgroup(s).