1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
14 #define REG_COUNT 0x80
16 static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
21 ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf));
25 time->tm_sec = buf[REG_SEC - REG_SEC];
26 time->tm_min = buf[REG_MIN - REG_SEC];
27 time->tm_hour = buf[REG_HOUR - REG_SEC];
28 time->tm_mday = buf[REG_MDAY - REG_SEC];
29 time->tm_mon = buf[REG_MON - REG_SEC];
30 time->tm_year = buf[REG_YEAR - REG_SEC] + 1900;
31 time->tm_wday = buf[REG_WDAY - REG_SEC];
36 static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
41 buf[REG_SEC - REG_SEC] = time->tm_sec;
42 buf[REG_MIN - REG_SEC] = time->tm_min;
43 buf[REG_HOUR - REG_SEC] = time->tm_hour;
44 buf[REG_MDAY - REG_SEC] = time->tm_mday;
45 buf[REG_MON - REG_SEC] = time->tm_mon;
46 buf[REG_YEAR - REG_SEC] = time->tm_year - 1900;
47 buf[REG_WDAY - REG_SEC] = time->tm_wday;
49 ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf));
56 static int sandbox_rtc_reset(struct udevice *dev)
58 return dm_i2c_reg_write(dev, REG_RESET, 0);
61 static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
63 return dm_i2c_reg_read(dev, reg);
66 static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
68 return dm_i2c_reg_write(dev, reg, val);
71 #if CONFIG_IS_ENABLED(ACPIGEN)
72 static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name)
74 return acpi_copy_name(out_name, "RTCC");
77 struct acpi_ops sandbox_rtc_acpi_ops = {
78 .get_name = sandbox_rtc_get_name,
82 static int sandbox_rtc_bind(struct udevice *dev)
84 #if CONFIG_IS_ENABLED(PLATDATA)
85 struct sandbox_i2c_rtc_plat_data *plat = dev_get_plat(dev);
87 /* Set up the emul_idx for i2c_emul_find() */
88 i2c_emul_set_idx(dev, plat->dtplat.sandbox_emul->idx);
94 static const struct rtc_ops sandbox_rtc_ops = {
95 .get = sandbox_rtc_get,
96 .set = sandbox_rtc_set,
97 .reset = sandbox_rtc_reset,
98 .read8 = sandbox_rtc_read8,
99 .write8 = sandbox_rtc_write8,
102 static const struct udevice_id sandbox_rtc_ids[] = {
103 { .compatible = "sandbox-rtc" },
107 U_BOOT_DRIVER(sandbox_rtc) = {
108 .name = "sandbox_rtc",
110 .of_match = sandbox_rtc_ids,
111 .ops = &sandbox_rtc_ops,
112 .bind = sandbox_rtc_bind,
113 ACPI_OPS_PTR(&sandbox_rtc_acpi_ops)