blob: 77065e49c76975a356fbbc322b43f1d00bcb7a62 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass58714162015-04-20 12:37:25 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass58714162015-04-20 12:37:25 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <i2c.h>
10#include <rtc.h>
11#include <asm/rtc.h>
12
13#define REG_COUNT 0x80
14
15static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
16{
Rasmus Villemoes9fb6a412020-07-06 22:01:16 +020017 u8 buf[7];
18 int ret;
19
20 ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf));
21 if (ret < 0)
22 return ret;
23
24 time->tm_sec = buf[REG_SEC - REG_SEC];
25 time->tm_min = buf[REG_MIN - REG_SEC];
26 time->tm_hour = buf[REG_HOUR - REG_SEC];
27 time->tm_mday = buf[REG_MDAY - REG_SEC];
28 time->tm_mon = buf[REG_MON - REG_SEC];
29 time->tm_year = buf[REG_YEAR - REG_SEC] + 1900;
30 time->tm_wday = buf[REG_WDAY - REG_SEC];
Simon Glass58714162015-04-20 12:37:25 -060031
32 return 0;
33}
34
35static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
36{
Rasmus Villemoes9fb6a412020-07-06 22:01:16 +020037 u8 buf[7];
Simon Glass58714162015-04-20 12:37:25 -060038 int ret;
39
Rasmus Villemoes9fb6a412020-07-06 22:01:16 +020040 buf[REG_SEC - REG_SEC] = time->tm_sec;
41 buf[REG_MIN - REG_SEC] = time->tm_min;
42 buf[REG_HOUR - REG_SEC] = time->tm_hour;
43 buf[REG_MDAY - REG_SEC] = time->tm_mday;
44 buf[REG_MON - REG_SEC] = time->tm_mon;
45 buf[REG_YEAR - REG_SEC] = time->tm_year - 1900;
46 buf[REG_WDAY - REG_SEC] = time->tm_wday;
47
48 ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf));
Simon Glass58714162015-04-20 12:37:25 -060049 if (ret < 0)
50 return ret;
51
52 return 0;
53}
54
55static int sandbox_rtc_reset(struct udevice *dev)
56{
57 return dm_i2c_reg_write(dev, REG_RESET, 0);
58}
59
60static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
61{
62 return dm_i2c_reg_read(dev, reg);
63}
64
65static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
66{
67 return dm_i2c_reg_write(dev, reg, val);
68}
69
70static const struct rtc_ops sandbox_rtc_ops = {
71 .get = sandbox_rtc_get,
72 .set = sandbox_rtc_set,
73 .reset = sandbox_rtc_reset,
74 .read8 = sandbox_rtc_read8,
75 .write8 = sandbox_rtc_write8,
76};
77
78static const struct udevice_id sandbox_rtc_ids[] = {
79 { .compatible = "sandbox-rtc" },
80 { }
81};
82
83U_BOOT_DRIVER(rtc_sandbox) = {
84 .name = "rtc-sandbox",
85 .id = UCLASS_RTC,
86 .of_match = sandbox_rtc_ids,
87 .ops = &sandbox_rtc_ops,
88};