Re: [U-Boot] [PATCH v2] rtc: add Microcrystal RV-8803 driver

2019-07-14 Thread Tom Rini
On Wed, May 29, 2019 at 01:29:58AM +0200, Michael Walle wrote:

> Signed-off-by: Michael Walle 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature
___
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot


[U-Boot] [PATCH v2] rtc: add Microcrystal RV-8803 driver

2019-05-28 Thread Michael Walle
Signed-off-by: Michael Walle 
---

Btw if anybody notices the compatible string difference to the rv3029
driver, ie mc,rv3029 vs microcrystal,rv8803. The "mc" prefix is actually
the legacy one.

changes since v1:
 - enable driver in sandbox_defconfig and sandbox64_defconfig

 configs/sandbox64_defconfig |   1 +
 configs/sandbox_defconfig   |   1 +
 drivers/rtc/Kconfig |  10 +++
 drivers/rtc/Makefile|   1 +
 drivers/rtc/rv8803.c| 167 
 5 files changed, 180 insertions(+)
 create mode 100644 drivers/rtc/rv8803.c

diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index 61175e8063..9b63fb0066 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -163,6 +163,7 @@ CONFIG_REMOTEPROC_SANDBOX=y
 CONFIG_DM_RESET=y
 CONFIG_SANDBOX_RESET=y
 CONFIG_DM_RTC=y
+CONFIG_RTC_RV8803=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SMEM=y
 CONFIG_SANDBOX_SMEM=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index ff01315bd8..d57b95a989 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -179,6 +179,7 @@ CONFIG_REMOTEPROC_SANDBOX=y
 CONFIG_DM_RESET=y
 CONFIG_SANDBOX_RESET=y
 CONFIG_DM_RTC=y
+CONFIG_RTC_RV8803=y
 CONFIG_DEBUG_UART_SANDBOX=y
 CONFIG_SANDBOX_SERIAL=y
 CONFIG_SMEM=y
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index fd0009b2e2..532e94d337 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -70,6 +70,16 @@ config RTC_RV3029
  This driver supports reading and writing the RTC/calendar and the
  battery-baced SRAM section.
 
+config RTC_RV8803
+   bool "Enable RV8803 driver"
+   depends on DM_RTC
+   help
+ The Micro Crystal RV8803 is a high accuracy, ultra-low power I2C
+ Real Time Clock (RTC) with temperature compensation.
+
+ This driver supports reading and writing the RTC/calendar and
+ detects total power failures.
+
 config RTC_RX8010SJ
bool "Enable RX8010SJ driver"
depends on DM_RTC
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 1724602f1c..915adb87fe 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -46,6 +46,7 @@ obj-$(CONFIG_RTC_PL031) += pl031.o
 obj-$(CONFIG_RTC_PT7C4338) += pt7c4338.o
 obj-$(CONFIG_RTC_RS5C372A) += rs5c372.o
 obj-$(CONFIG_RTC_RV3029) += rv3029.o
+obj-$(CONFIG_RTC_RV8803) += rv8803.o
 obj-$(CONFIG_RTC_RX8025) += rx8025.o
 obj-$(CONFIG_RTC_RX8010SJ) += rx8010sj.o
 obj-$(CONFIG_RTC_S3C24X0) += s3c24x0_rtc.o
diff --git a/drivers/rtc/rv8803.c b/drivers/rtc/rv8803.c
new file mode 100644
index 00..2ab40f0833
--- /dev/null
+++ b/drivers/rtc/rv8803.c
@@ -0,0 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Date & Time support for Micro Crystal RV-8803-C7.
+ *
+ * based on ds1307.c which is
+ *   (C) Copyright 2001, 2002, 2003
+ *   Wolfgang Denk, DENX Software Engineering, w...@denx.de.
+ *   Keith Outwater, keith_outwa...@mvis.com`
+ *   Steven Scholz, steven.sch...@imc-berlin.de
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * RTC register addresses
+ */
+#define RTC_SEC_REG_ADDR   0x00
+#define RTC_MIN_REG_ADDR   0x01
+#define RTC_HR_REG_ADDR0x02
+#define RTC_DAY_REG_ADDR   0x03
+#define RTC_DATE_REG_ADDR  0x04
+#define RTC_MON_REG_ADDR   0x05
+#define RTC_YR_REG_ADDR0x06
+
+#define RTC_FLAG_REG_ADDR  0x0E
+#define RTC_FLAG_BIT_V1F   BIT(0)
+#define RTC_FLAG_BIT_V2F   BIT(1)
+
+#define RTC_CTL_REG_ADDR   0x0F
+#define RTC_CTL_BIT_RSTBIT(0)
+
+static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm)
+{
+   int ret;
+   u8 buf[7];
+
+   debug("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
+ tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+   if (tm->tm_year < 2000 || tm->tm_year > 2099)
+   printf("WARNING: year should be between 2000 and 2099!\n");
+
+   buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
+   buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon);
+   buf[RTC_DAY_REG_ADDR] = 1 << (tm->tm_wday & 0x7);
+   buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
+   buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
+   buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
+   buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
+
+   ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
+   if (ret < 0)
+   return ret;
+
+   return 0;
+}
+
+static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm)
+{
+   int ret;
+   u8 buf[7];
+   int flags;
+
+   flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR);
+   if (flags < 0)
+   return flags;
+   debug("%s: flags=%Xh\n", __func__, flags);
+
+   if (flags & RTC_FLAG_BIT_V1F)
+   printf("### Warning: temperature compensation has stopped\n");
+
+   if (flags &