1 // SPDX-License-Identifier: GPL-2.0+
3 * Date & Time support for Micro Crystal RV-8803-C7.
5 * based on ds1307.c which is
6 * (C) Copyright 2001, 2002, 2003
18 #include <linux/bitops.h>
21 * RTC register addresses
23 #define RTC_SEC_REG_ADDR 0x00
24 #define RTC_MIN_REG_ADDR 0x01
25 #define RTC_HR_REG_ADDR 0x02
26 #define RTC_DAY_REG_ADDR 0x03
27 #define RTC_DATE_REG_ADDR 0x04
28 #define RTC_MON_REG_ADDR 0x05
29 #define RTC_YR_REG_ADDR 0x06
31 #define RTC_FLAG_REG_ADDR 0x0E
32 #define RTC_FLAG_BIT_V1F BIT(0)
33 #define RTC_FLAG_BIT_V2F BIT(1)
35 #define RTC_CTL_REG_ADDR 0x0F
36 #define RTC_CTL_BIT_RST BIT(0)
38 static int rv8803_rtc_set(struct udevice *dev, const struct rtc_time *tm)
43 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
44 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
45 tm->tm_hour, tm->tm_min, tm->tm_sec);
47 if (tm->tm_year < 2000 || tm->tm_year > 2099)
48 printf("WARNING: year should be between 2000 and 2099!\n");
50 buf[RTC_YR_REG_ADDR] = bin2bcd(tm->tm_year % 100);
51 buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon + 1);
52 buf[RTC_DAY_REG_ADDR] = 1 << (tm->tm_wday & 0x7);
53 buf[RTC_DATE_REG_ADDR] = bin2bcd(tm->tm_mday);
54 buf[RTC_HR_REG_ADDR] = bin2bcd(tm->tm_hour);
55 buf[RTC_MIN_REG_ADDR] = bin2bcd(tm->tm_min);
56 buf[RTC_SEC_REG_ADDR] = bin2bcd(tm->tm_sec);
58 ret = dm_i2c_write(dev, 0, buf, sizeof(buf));
65 static int rv8803_rtc_get(struct udevice *dev, struct rtc_time *tm)
71 flags = dm_i2c_reg_read(dev, RTC_FLAG_REG_ADDR);
74 debug("%s: flags=%Xh\n", __func__, flags);
76 if (flags & RTC_FLAG_BIT_V1F)
77 printf("### Warning: temperature compensation has stopped\n");
79 if (flags & RTC_FLAG_BIT_V2F) {
80 printf("### Warning: Voltage low, data is invalid\n");
84 ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
88 tm->tm_sec = bcd2bin(buf[RTC_SEC_REG_ADDR] & 0x7F);
89 tm->tm_min = bcd2bin(buf[RTC_MIN_REG_ADDR] & 0x7F);
90 tm->tm_hour = bcd2bin(buf[RTC_HR_REG_ADDR] & 0x3F);
91 tm->tm_mday = bcd2bin(buf[RTC_DATE_REG_ADDR] & 0x3F);
92 tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F) - 1;
93 tm->tm_year = bcd2bin(buf[RTC_YR_REG_ADDR]) + 2000;
94 tm->tm_wday = fls(buf[RTC_DAY_REG_ADDR] & 0x7F) - 1;
98 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
99 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
100 tm->tm_hour, tm->tm_min, tm->tm_sec);
105 static int rv8803_rtc_reset(struct udevice *dev)
108 struct rtc_time tmp = {
118 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, RTC_CTL_BIT_RST);
122 /* clear all flags */
123 ret = dm_i2c_reg_write(dev, RTC_FLAG_REG_ADDR, 0);
127 ret = rv8803_rtc_set(dev, &tmp);
132 ret = dm_i2c_reg_write(dev, RTC_CTL_REG_ADDR, 0);
136 debug("RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
137 tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
138 tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
143 static int rv8803_probe(struct udevice *dev)
145 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
146 DM_I2C_CHIP_WR_ADDRESS);
151 static const struct rtc_ops rv8803_rtc_ops = {
152 .get = rv8803_rtc_get,
153 .set = rv8803_rtc_set,
154 .reset = rv8803_rtc_reset,
157 static const struct udevice_id rv8803_rtc_ids[] = {
158 { .compatible = "microcrystal,rv8803", },
159 { .compatible = "epson,rx8803" },
160 { .compatible = "epson,rx8900" },
164 U_BOOT_DRIVER(rtc_rv8803) = {
165 .name = "rtc-rv8803",
167 .probe = rv8803_probe,
168 .of_match = rv8803_rtc_ids,
169 .ops = &rv8803_rtc_ops,