2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 #define REG_COUNT 0x80
16 static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
18 time->tm_sec = dm_i2c_reg_read(dev, REG_SEC);
21 time->tm_min = dm_i2c_reg_read(dev, REG_MIN);
24 time->tm_hour = dm_i2c_reg_read(dev, REG_HOUR);
25 if (time->tm_hour < 0)
27 time->tm_mday = dm_i2c_reg_read(dev, REG_MDAY);
28 if (time->tm_mday < 0)
30 time->tm_mon = dm_i2c_reg_read(dev, REG_MON);
33 time->tm_year = dm_i2c_reg_read(dev, REG_YEAR);
34 if (time->tm_year < 0)
36 time->tm_year += 1900;
37 time->tm_wday = dm_i2c_reg_read(dev, REG_WDAY);
38 if (time->tm_wday < 0)
44 static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
48 ret = dm_i2c_reg_write(dev, REG_SEC, time->tm_sec);
51 ret = dm_i2c_reg_write(dev, REG_MIN, time->tm_min);
54 ret = dm_i2c_reg_write(dev, REG_HOUR, time->tm_hour);
57 ret = dm_i2c_reg_write(dev, REG_MDAY, time->tm_mday);
60 ret = dm_i2c_reg_write(dev, REG_MON, time->tm_mon);
63 ret = dm_i2c_reg_write(dev, REG_YEAR, time->tm_year - 1900);
66 ret = dm_i2c_reg_write(dev, REG_WDAY, time->tm_wday);
73 static int sandbox_rtc_reset(struct udevice *dev)
75 return dm_i2c_reg_write(dev, REG_RESET, 0);
78 static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
80 return dm_i2c_reg_read(dev, reg);
83 static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
85 return dm_i2c_reg_write(dev, reg, val);
88 static const struct rtc_ops sandbox_rtc_ops = {
89 .get = sandbox_rtc_get,
90 .set = sandbox_rtc_set,
91 .reset = sandbox_rtc_reset,
92 .read8 = sandbox_rtc_read8,
93 .write8 = sandbox_rtc_write8,
96 static const struct udevice_id sandbox_rtc_ids[] = {
97 { .compatible = "sandbox-rtc" },
101 U_BOOT_DRIVER(rtc_sandbox) = {
102 .name = "rtc-sandbox",
104 .of_match = sandbox_rtc_ids,
105 .ops = &sandbox_rtc_ops,