1 // SPDX-License-Identifier: GPL-2.0+
4 * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
8 * Epson RX8025 RTC driver.
17 /*---------------------------------------------------------------------*/
21 #define DEBUGR(fmt,args...) printf(fmt ,##args)
23 #define DEBUGR(fmt,args...)
25 /*---------------------------------------------------------------------*/
28 * RTC register addresses
30 #define RTC_SEC_REG_ADDR 0x00
31 #define RTC_MIN_REG_ADDR 0x01
32 #define RTC_HR_REG_ADDR 0x02
33 #define RTC_DAY_REG_ADDR 0x03
34 #define RTC_DATE_REG_ADDR 0x04
35 #define RTC_MON_REG_ADDR 0x05
36 #define RTC_YR_REG_ADDR 0x06
38 #define RTC_CTL1_REG_ADDR 0x0e
39 #define RTC_CTL2_REG_ADDR 0x0f
42 * Control register 1 bits
44 #define RTC_CTL1_BIT_2412 0x20
47 * Control register 2 bits
49 #define RTC_CTL2_BIT_PON 0x10
50 #define RTC_CTL2_BIT_VDET 0x40
51 #define RTC_CTL2_BIT_XST 0x20
52 #define RTC_CTL2_BIT_VDSL 0x80
55 * Note: the RX8025 I2C RTC requires register
56 * reads and write to consist of a single bus
57 * cycle. It is not allowed to write the register
58 * address in a first cycle that is terminated by
59 * a STOP condition. The chips needs a 'restart'
60 * sequence (start sequence without a prior stop).
63 #define rtc_read(reg) buf[(reg) & 0xf]
65 static int rtc_write(struct udevice *dev, uchar reg, uchar val);
68 * Get the current time from the RTC
70 static int rx8025_rtc_get(struct udevice *dev, struct rtc_time *tmp)
73 uchar sec, min, hour, mday, wday, mon, year, ctl2;
76 if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
77 printf("Error reading from RTC\n");
81 sec = rtc_read(RTC_SEC_REG_ADDR);
82 min = rtc_read(RTC_MIN_REG_ADDR);
83 hour = rtc_read(RTC_HR_REG_ADDR);
84 wday = rtc_read(RTC_DAY_REG_ADDR);
85 mday = rtc_read(RTC_DATE_REG_ADDR);
86 mon = rtc_read(RTC_MON_REG_ADDR);
87 year = rtc_read(RTC_YR_REG_ADDR);
89 DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
90 "hr: %02x min: %02x sec: %02x\n",
91 year, mon, mday, wday, hour, min, sec);
94 ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
95 if (ctl2 & RTC_CTL2_BIT_PON) {
96 printf("RTC: power-on detected\n");
100 if (ctl2 & RTC_CTL2_BIT_VDET) {
101 printf("RTC: voltage drop detected\n");
105 if (!(ctl2 & RTC_CTL2_BIT_XST)) {
106 printf("RTC: oscillator stop detected\n");
110 tmp->tm_sec = bcd2bin(sec & 0x7F);
111 tmp->tm_min = bcd2bin(min & 0x7F);
112 if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
113 tmp->tm_hour = bcd2bin(hour & 0x3F);
115 tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 +
116 ((hour & 0x20) ? 12 : 0);
118 tmp->tm_mday = bcd2bin (mday & 0x3F);
119 tmp->tm_mon = bcd2bin (mon & 0x1F);
120 tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
121 tmp->tm_wday = bcd2bin (wday & 0x07);
125 DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
126 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
127 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
135 static int rx8025_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
137 DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
138 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
139 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
141 if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
142 printf("WARNING: year should be between 1970 and 2069!\n");
144 if (rtc_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100)))
147 if (rtc_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon)))
150 if (rtc_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday)))
153 if (rtc_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday)))
156 if (rtc_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour)))
159 if (rtc_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min)))
162 if (rtc_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec)))
165 return rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
171 static int rx8025_rtc_reset(struct udevice *dev)
176 if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
177 printf("Error reading from RTC\n");
181 ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
182 ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
183 ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
185 return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2);
191 static int rtc_write(struct udevice *dev, uchar reg, uchar val)
197 if (dm_i2c_write(dev, 0, buf, 2)) {
198 printf("Error writing to RTC\n");
205 static int rx8025_probe(struct udevice *dev)
210 if (i2c_get_chip_offset_len(dev) != 1)
211 ret = i2c_set_chip_offset_len(dev, 1);
216 return dm_i2c_read(dev, 0, buf, sizeof(buf));
219 static const struct rtc_ops rx8025_rtc_ops = {
220 .get = rx8025_rtc_get,
221 .set = rx8025_rtc_set,
222 .reset = rx8025_rtc_reset,
225 static const struct udevice_id rx8025_rtc_ids[] = {
226 { .compatible = "epson,rx8025" },
230 U_BOOT_DRIVER(rx8010sj_rtc) = {
231 .name = "rx8025_rtc",
233 .probe = rx8025_probe,
234 .of_match = rx8025_rtc_ids,
235 .ops = &rx8025_rtc_ops,