xref: /openbmc/u-boot/drivers/rtc/sandbox_rtc.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
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 
15 static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
16 {
17 	time->tm_sec = dm_i2c_reg_read(dev, REG_SEC);
18 	if (time->tm_sec < 0)
19 		return time->tm_sec;
20 	time->tm_min = dm_i2c_reg_read(dev, REG_MIN);
21 	if (time->tm_min < 0)
22 		return time->tm_min;
23 	time->tm_hour = dm_i2c_reg_read(dev, REG_HOUR);
24 	if (time->tm_hour < 0)
25 		return time->tm_hour;
26 	time->tm_mday = dm_i2c_reg_read(dev, REG_MDAY);
27 	if (time->tm_mday < 0)
28 		return time->tm_mday;
29 	time->tm_mon = dm_i2c_reg_read(dev, REG_MON);
30 	if (time->tm_mon < 0)
31 		return time->tm_mon;
32 	time->tm_year = dm_i2c_reg_read(dev, REG_YEAR);
33 	if (time->tm_year < 0)
34 		return time->tm_year;
35 	time->tm_year += 1900;
36 	time->tm_wday = dm_i2c_reg_read(dev, REG_WDAY);
37 	if (time->tm_wday < 0)
38 		return time->tm_wday;
39 
40 	return 0;
41 }
42 
43 static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
44 {
45 	int ret;
46 
47 	ret = dm_i2c_reg_write(dev, REG_SEC, time->tm_sec);
48 	if (ret < 0)
49 		return ret;
50 	ret = dm_i2c_reg_write(dev, REG_MIN, time->tm_min);
51 	if (ret < 0)
52 		return ret;
53 	ret = dm_i2c_reg_write(dev, REG_HOUR, time->tm_hour);
54 	if (ret < 0)
55 		return ret;
56 	ret = dm_i2c_reg_write(dev, REG_MDAY, time->tm_mday);
57 	if (ret < 0)
58 		return ret;
59 	ret = dm_i2c_reg_write(dev, REG_MON, time->tm_mon);
60 	if (ret < 0)
61 		return ret;
62 	ret = dm_i2c_reg_write(dev, REG_YEAR, time->tm_year - 1900);
63 	if (ret < 0)
64 		return ret;
65 	ret = dm_i2c_reg_write(dev, REG_WDAY, time->tm_wday);
66 	if (ret < 0)
67 		return ret;
68 
69 	return 0;
70 }
71 
72 static int sandbox_rtc_reset(struct udevice *dev)
73 {
74 	return dm_i2c_reg_write(dev, REG_RESET, 0);
75 }
76 
77 static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
78 {
79 	return dm_i2c_reg_read(dev, reg);
80 }
81 
82 static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
83 {
84 	return dm_i2c_reg_write(dev, reg, val);
85 }
86 
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,
93 };
94 
95 static const struct udevice_id sandbox_rtc_ids[] = {
96 	{ .compatible = "sandbox-rtc" },
97 	{ }
98 };
99 
100 U_BOOT_DRIVER(rtc_sandbox) = {
101 	.name	= "rtc-sandbox",
102 	.id	= UCLASS_RTC,
103 	.of_match = sandbox_rtc_ids,
104 	.ops	= &sandbox_rtc_ops,
105 };
106