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