1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #define REG_COUNT 0x80
15 static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
17 time->tm_sec = dm_i2c_reg_read(dev, REG_SEC);
20 time->tm_min = dm_i2c_reg_read(dev, REG_MIN);
23 time->tm_hour = dm_i2c_reg_read(dev, REG_HOUR);
24 if (time->tm_hour < 0)
26 time->tm_mday = dm_i2c_reg_read(dev, REG_MDAY);
27 if (time->tm_mday < 0)
29 time->tm_mon = dm_i2c_reg_read(dev, REG_MON);
32 time->tm_year = dm_i2c_reg_read(dev, REG_YEAR);
33 if (time->tm_year < 0)
35 time->tm_year += 1900;
36 time->tm_wday = dm_i2c_reg_read(dev, REG_WDAY);
37 if (time->tm_wday < 0)
43 static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
47 ret = dm_i2c_reg_write(dev, REG_SEC, time->tm_sec);
50 ret = dm_i2c_reg_write(dev, REG_MIN, time->tm_min);
53 ret = dm_i2c_reg_write(dev, REG_HOUR, time->tm_hour);
56 ret = dm_i2c_reg_write(dev, REG_MDAY, time->tm_mday);
59 ret = dm_i2c_reg_write(dev, REG_MON, time->tm_mon);
62 ret = dm_i2c_reg_write(dev, REG_YEAR, time->tm_year - 1900);
65 ret = dm_i2c_reg_write(dev, REG_WDAY, time->tm_wday);
72 static int sandbox_rtc_reset(struct udevice *dev)
74 return dm_i2c_reg_write(dev, REG_RESET, 0);
77 static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
79 return dm_i2c_reg_read(dev, reg);
82 static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
84 return dm_i2c_reg_write(dev, reg, val);
87 static const struct rtc_ops sandbox_rtc_ops = {
88 .get = sandbox_rtc_get,
89 .set = sandbox_rtc_set,
90 .reset = sandbox_rtc_reset,
91 .read8 = sandbox_rtc_read8,
92 .write8 = sandbox_rtc_write8,
95 static const struct udevice_id sandbox_rtc_ids[] = {
96 { .compatible = "sandbox-rtc" },
100 U_BOOT_DRIVER(rtc_sandbox) = {
101 .name = "rtc-sandbox",
103 .of_match = sandbox_rtc_ids,
104 .ops = &sandbox_rtc_ops,