]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: rtc: sandbox: Add a driver for the sandbox I2C RTC
authorSimon Glass <sjg@chromium.org>
Mon, 20 Apr 2015 18:37:25 +0000 (12:37 -0600)
committerSimon Glass <sjg@chromium.org>
Wed, 6 May 2015 02:58:40 +0000 (20:58 -0600)
Add a driver which communicates with the sandbox I2C emulation RTC device
and permits it to be used in U-Boot. This driver is very simple - it just
reads and writes selected I2C registers in the device.

Signed-off-by: Simon Glass <sjg@chromium.org>
drivers/rtc/Makefile
drivers/rtc/sandbox_rtc.c [new file with mode: 0644]

index aeb705ae9629ca8d1a889a0a9b0f6d228e8f9155..9077bb39f1e13e596237eb93c1967fca6d65b6e6 100644 (file)
@@ -52,4 +52,7 @@ obj-$(CONFIG_RTC_RTC4543) += rtc4543.o
 obj-$(CONFIG_RTC_RV3029) += rv3029.o
 obj-$(CONFIG_RTC_RX8025) += rx8025.o
 obj-$(CONFIG_RTC_S3C24X0) += s3c24x0_rtc.o
+ifdef CONFIG_DM_RTC
+obj-$(CONFIG_SANDBOX) += sandbox_rtc.o
+endif
 obj-$(CONFIG_RTC_X1205) += x1205.o
diff --git a/drivers/rtc/sandbox_rtc.c b/drivers/rtc/sandbox_rtc.c
new file mode 100644 (file)
index 0000000..f292fbe
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * (C) Copyright 2015 Google, Inc
+ * Written by Simon Glass <sjg@chromium.org>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2c.h>
+#include <rtc.h>
+#include <asm/rtc.h>
+
+#define REG_COUNT 0x80
+
+static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
+{
+       time->tm_sec = dm_i2c_reg_read(dev, REG_SEC);
+       if (time->tm_sec < 0)
+               return time->tm_sec;
+       time->tm_min = dm_i2c_reg_read(dev, REG_MIN);
+       if (time->tm_min < 0)
+               return time->tm_min;
+       time->tm_hour = dm_i2c_reg_read(dev, REG_HOUR);
+       if (time->tm_hour < 0)
+               return time->tm_hour;
+       time->tm_mday = dm_i2c_reg_read(dev, REG_MDAY);
+       if (time->tm_mday < 0)
+               return time->tm_mday;
+       time->tm_mon = dm_i2c_reg_read(dev, REG_MON);
+       if (time->tm_mon < 0)
+               return time->tm_mon;
+       time->tm_year = dm_i2c_reg_read(dev, REG_YEAR);
+       if (time->tm_year < 0)
+               return time->tm_year;
+       time->tm_year += 1900;
+       time->tm_wday = dm_i2c_reg_read(dev, REG_WDAY);
+       if (time->tm_wday < 0)
+               return time->tm_wday;
+
+       return 0;
+}
+
+static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
+{
+       int ret;
+
+       ret = dm_i2c_reg_write(dev, REG_SEC, time->tm_sec);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_MIN, time->tm_min);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_HOUR, time->tm_hour);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_MDAY, time->tm_mday);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_MON, time->tm_mon);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_YEAR, time->tm_year - 1900);
+       if (ret < 0)
+               return ret;
+       ret = dm_i2c_reg_write(dev, REG_WDAY, time->tm_wday);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int sandbox_rtc_reset(struct udevice *dev)
+{
+       return dm_i2c_reg_write(dev, REG_RESET, 0);
+}
+
+static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
+{
+       return dm_i2c_reg_read(dev, reg);
+}
+
+static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
+{
+       return dm_i2c_reg_write(dev, reg, val);
+}
+
+static const struct rtc_ops sandbox_rtc_ops = {
+       .get = sandbox_rtc_get,
+       .set = sandbox_rtc_set,
+       .reset = sandbox_rtc_reset,
+       .read8 = sandbox_rtc_read8,
+       .write8 = sandbox_rtc_write8,
+};
+
+static const struct udevice_id sandbox_rtc_ids[] = {
+       { .compatible = "sandbox-rtc" },
+       { }
+};
+
+U_BOOT_DRIVER(rtc_sandbox) = {
+       .name   = "rtc-sandbox",
+       .id     = UCLASS_RTC,
+       .of_match = sandbox_rtc_ids,
+       .ops    = &sandbox_rtc_ops,
+};