rtc: add Microcrystal RV-8803 driver
authorMichael Walle <michael@walle.cc>
Tue, 28 May 2019 23:29:58 +0000 (01:29 +0200)
committerTom Rini <trini@konsulko.com>
Sat, 13 Jul 2019 15:11:28 +0000 (11:11 -0400)
Signed-off-by: Michael Walle <michael@walle.cc>
configs/sandbox64_defconfig
configs/sandbox_defconfig
drivers/rtc/Kconfig
drivers/rtc/Makefile
drivers/rtc/rv8803.c [new file with mode: 0644]

index 29a9df8ab0a576561e82b863bc4f5b0c0f3b616f..827b8761cdd87cc519adf015217d77745cb0bd51 100644 (file)
@@ -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
index 6894262b89f9fe65beaad0ca0b88eec69d062d9d..61391a7acd4464561759686643d7ed99dcf00bb9 100644 (file)
@@ -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
index fd0009b2e2d18593017f8f326950d6330807b32f..532e94d337b638e1d3a3c6ae4e98e948f4e5cf76 100644 (file)
@@ -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
index 1724602f1cb6ef8385aa6edf5dbc0090c311c0e1..915adb87fe258e9fe5dc266e6f99e3253b9b5568 100644 (file)
@@ -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 (file)
index 0000000..2ab40f0
--- /dev/null
@@ -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, wd@denx.de.
+ *   Keith Outwater, keith_outwater@mvis.com`
+ *   Steven Scholz, steven.scholz@imc-berlin.de
+ *
+ */
+
+#include <common.h>
+#include <command.h>
+#include <dm.h>
+#include <rtc.h>
+#include <i2c.h>
+
+/*
+ * RTC register addresses
+ */
+#define RTC_SEC_REG_ADDR       0x00
+#define RTC_MIN_REG_ADDR       0x01
+#define RTC_HR_REG_ADDR                0x02
+#define RTC_DAY_REG_ADDR       0x03
+#define RTC_DATE_REG_ADDR      0x04
+#define RTC_MON_REG_ADDR       0x05
+#define RTC_YR_REG_ADDR                0x06
+
+#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_RST                BIT(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 & RTC_FLAG_BIT_V2F) {
+               printf("### Warning: Voltage low, data is invalid\n");
+               return -1;
+       }
+
+       ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
+       if (ret < 0)
+               return ret;
+
+       tm->tm_sec  = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
+       tm->tm_min  = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
+       tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
+       tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
+       tm->tm_mon  = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
+       tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000;
+       tm->tm_wday = fls(buf[RTC_DAY_REG_ADDR] & 0x7F) - 1;
+       tm->tm_yday = 0;
+       tm->tm_isdst = 0;
+
+       debug("Get 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);
+
+       return 0;
+}
+
+static int rv8803_rtc_reset(struct udevice *dev)
+{
+       int ret;
+       struct rtc_time tmp = {
+               .tm_year = 2000,
+               .tm_mon = 1,
+               .tm_mday = 1,
+               .tm_hour = 0,
+               .tm_min = 0,
+               .tm_sec = 0,
+       };
+
+       /* assert reset */
+       ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST);
+       if (ret < 0)
+               return ret;
+
+       /* clear all flags */
+       ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0);
+       if (ret < 0)
+               return ret;
+
+       ret = rv8803_rtc_set(dev, &tmp);
+       if (ret < 0)
+               return ret;
+
+       /* clear reset */
+       ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0);
+       if (ret < 0)
+               return ret;
+
+       debug("RTC:   %4d-%02d-%02d %2d:%02d:%02d UTC\n",
+             tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
+             tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
+
+       return 0;
+}
+
+static int rv8803_probe(struct udevice *dev)
+{
+       i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
+                          DM_I2C_CHIP_WR_ADDRESS);
+
+       return 0;
+}
+
+static const struct rtc_ops rv8803_rtc_ops = {
+       .get = rv8803_rtc_get,
+       .set = rv8803_rtc_set,
+       .reset = rv8803_rtc_reset,
+};
+
+static const struct udevice_id rv8803_rtc_ids[] = {
+       { .compatible = "microcrystal,rv8803", },
+       { }
+};
+
+U_BOOT_DRIVER(rtc_rv8803) = {
+       .name   = "rtc-rv8803",
+       .id     = UCLASS_RTC,
+       .probe  = rv8803_probe,
+       .of_match = rv8803_rtc_ids,
+       .ops    = &rv8803_rtc_ops,
+};