[PATCH 1/3] dt-bindings: define vendor prefix for whwave, Inc.

2018-12-13 Thread Dianlong Li
Introduce vendor prefix for whwave, Inc.
for SD3078 rtc device.

Signed-off-by: Dianlong Li 
---
 .../devicetree/bindings/vendor-prefixes.txt|1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/vendor-prefixes.txt 
b/Documentation/devicetree/bindings/vendor-prefixes.txt
index 4b1a2a8..89403c7 100644
--- a/Documentation/devicetree/bindings/vendor-prefixes.txt
+++ b/Documentation/devicetree/bindings/vendor-prefixes.txt
@@ -423,6 +423,7 @@ vot Vision Optical Technology Co., Ltd.
 wd Western Digital Corp.
 wetek  WeTek Electronics, limited.
 wexler Wexler
+whwave  Shenzhen whwave Electronics, Inc.
 wi2wi  Wi2Wi, Inc.
 winbond Winbond Electronics corp.
 winstarWinstar Display Corp.
-- 
1.7.9.5




[PATCH 2/3] dt-bindings: rtc: sd3078: add device tree documentation

2018-12-13 Thread Dianlong Li
The devicetree documentation for the SD3078 device tree.

Signed-off-by: Dianlong Li 
---
 Documentation/devicetree/bindings/rtc/rtc.txt |1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/rtc/rtc.txt 
b/Documentation/devicetree/bindings/rtc/rtc.txt
index 7c8da69..e46edab 100644
--- a/Documentation/devicetree/bindings/rtc/rtc.txt
+++ b/Documentation/devicetree/bindings/rtc/rtc.txt
@@ -62,3 +62,4 @@ ricoh,rs5c372bI2C bus SERIAL INTERFACE 
REAL-TIME CLOCK IC
 ricoh,rv5c386  I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
 ricoh,rv5c387a I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
 sii,s35390a2-wire CMOS real-time clock
+whwave,sd3078  I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
-- 
1.7.9.5




[PATCH 3/3] rtc: sd3078: new driver.

2018-12-13 Thread Dianlong Li
The sd3078 is a combination RTC and SRAM device with I2C interface.

Signed-off-by: Dianlong Li 
---
 MAINTAINERS  |6 ++
 drivers/rtc/Kconfig  |9 ++
 drivers/rtc/Makefile |1 +
 drivers/rtc/rtc-sd3078.c |  232 ++
 4 files changed, 248 insertions(+)
 create mode 100644 drivers/rtc/rtc-sd3078.c

diff --git a/MAINTAINERS b/MAINTAINERS
index f485597..2ad8671 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -16125,6 +16125,12 @@ L: linux-g...@vger.kernel.org
 S: Maintained
 F: drivers/gpio/gpio-wcove.c
 
+WHWAVE RTC DRIVER
+M: Dianlong Li 
+L: linux-...@vger.kernel.org
+S: Maintained
+F: drivers/rtc/rtc-sd3078.c
+
 WIIMOTE HID DRIVER
 M: David Herrmann 
 L: linux-in...@vger.kernel.org
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index a819ef0..51a8255b 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -646,6 +646,15 @@ config RTC_DRV_S5M
  This driver can also be built as a module. If so, the module
  will be called rtc-s5m.
 
+config RTC_DRV_SD3078
+tristate "ZXW Crystal SD3078"
+help
+  If you say yes here you get support for the ZXW Crystal
+  SD3078 RTC chips.
+
+  This driver can also be built as a module. If so, the module
+  will be called rtc-sd3078
+
 endif # I2C
 
 comment "SPI RTC drivers"
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 290c173..91a9257 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -148,6 +148,7 @@ obj-$(CONFIG_RTC_DRV_S3C)   += rtc-s3c.o
 obj-$(CONFIG_RTC_DRV_S5M)  += rtc-s5m.o
 obj-$(CONFIG_RTC_DRV_SA1100)   += rtc-sa1100.o
 obj-$(CONFIG_RTC_DRV_SC27XX)   += rtc-sc27xx.o
+obj-$(CONFIG_RTC_DRV_SD3078)   += rtc-sd3078.o
 obj-$(CONFIG_RTC_DRV_SH)   += rtc-sh.o
 obj-$(CONFIG_RTC_DRV_SIRFSOC)  += rtc-sirfsoc.o
 obj-$(CONFIG_RTC_DRV_SNVS) += rtc-snvs.o
diff --git a/drivers/rtc/rtc-sd3078.c b/drivers/rtc/rtc-sd3078.c
new file mode 100644
index 000..a70d7311
--- /dev/null
+++ b/drivers/rtc/rtc-sd3078.c
@@ -0,0 +1,232 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Real Time Clock (RTC) Driver for sd3078
+ * Copyright (C) 2018 Zoro Li
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define SD3078_REG_SC  0x00
+#define SD3078_REG_MN  0x01
+#define SD3078_REG_HR  0x02
+#define SD3078_REG_DW  0x03
+#define SD3078_REG_DM  0x04
+#define SD3078_REG_MO  0x05
+#define SD3078_REG_YR  0x06
+
+#define SD3078_REG_CTRL1   0x0f
+#define SD3078_REG_CTRL2   0x10
+#define SD3078_REG_CTRL3   0x11
+
+#define KEY_WRITE1 0x80
+#define KEY_WRITE2 0x04
+#define KEY_WRITE3 0x80
+
+#define NUM_TIME_REGS   (SD3078_REG_YR - SD3078_REG_SC + 1)
+
+/*
+ * The sd3078 has write protection
+ * and we can choose whether or not to use it.
+ * Write protection is turned off by default.
+ */
+#define WRITE_PROTECT_EN   0
+
+struct sd3078 {
+   struct rtc_device   *rtc;
+   struct regmap   *regmap;
+};
+
+/*
+ * In order to prevent arbitrary modification of the time register,
+ * when modification of the register,
+ * the "write" bit needs to be written in a certain order.
+ * 1. set WRITE1 bit
+ * 2. set WRITE2 bit
+ * 3. set WRITE3 bit
+ */
+static void sd3078_enable_reg_write(struct sd3078 *sd3078)
+{
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
+  KEY_WRITE1, KEY_WRITE1);
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
+  KEY_WRITE2, KEY_WRITE2);
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
+  KEY_WRITE3, KEY_WRITE3);
+}
+
+#if WRITE_PROTECT_EN
+/*
+ * In order to prevent arbitrary modification of the time register,
+ * we should disable the write function.
+ * when disable write,
+ * the "write" bit needs to be clear in a certain order.
+ * 1. clear WRITE2 bit
+ * 2. clear WRITE3 bit
+ * 3. clear WRITE1 bit
+ */
+static void sd3078_disable_reg_write(struct sd3078 *sd3078)
+{
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
+  KEY_WRITE2, 0);
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL1,
+  KEY_WRITE3, 0);
+   regmap_update_bits(sd3078->regmap, SD3078_REG_CTRL2,
+  KEY_WRITE1, 0);
+}
+#endif
+
+static int sd3078_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+   unsigned char hour;
+   unsigned char rtc_data[NUM_TIME_REGS] = {0};
+   struct i2c_client *client = to_i2c_client(dev);
+   struct sd3078 *sd3078 = i2c_get_clientdata(client);
+   int ret;
+
+   ret = regmap_bulk_read(sd3078->regmap, SD3078_REG_SC, rtc_data,
+  NUM_TIME_REGS);