1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
5 * Based on a the Linux rtc-rv3029c2.c driver written by:
17 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
21 #define RTC_RV3029_PAGE_LEN 7
24 #define RV3029_ONOFF_CTRL 0x00
25 #define RV3029_ONOFF_CTRL_WE BIT(0)
26 #define RV3029_ONOFF_CTRL_TE BIT(1)
27 #define RV3029_ONOFF_CTRL_TAR BIT(2)
28 #define RV3029_ONOFF_CTRL_EERE BIT(3)
29 #define RV3029_ONOFF_CTRL_SRON BIT(4)
30 #define RV3029_ONOFF_CTRL_TD0 BIT(5)
31 #define RV3029_ONOFF_CTRL_TD1 BIT(6)
32 #define RV3029_ONOFF_CTRL_CLKINT BIT(7)
33 #define RV3029_IRQ_CTRL 0x01
34 #define RV3029_IRQ_CTRL_AIE BIT(0)
35 #define RV3029_IRQ_CTRL_TIE BIT(1)
36 #define RV3029_IRQ_CTRL_V1IE BIT(2)
37 #define RV3029_IRQ_CTRL_V2IE BIT(3)
38 #define RV3029_IRQ_CTRL_SRIE BIT(4)
39 #define RV3029_IRQ_FLAGS 0x02
40 #define RV3029_IRQ_FLAGS_AF BIT(0)
41 #define RV3029_IRQ_FLAGS_TF BIT(1)
42 #define RV3029_IRQ_FLAGS_V1IF BIT(2)
43 #define RV3029_IRQ_FLAGS_V2IF BIT(3)
44 #define RV3029_IRQ_FLAGS_SRF BIT(4)
45 #define RV3029_STATUS 0x03
46 #define RV3029_STATUS_VLOW1 BIT(2)
47 #define RV3029_STATUS_VLOW2 BIT(3)
48 #define RV3029_STATUS_SR BIT(4)
49 #define RV3029_STATUS_PON BIT(5)
50 #define RV3029_STATUS_EEBUSY BIT(7)
51 #define RV3029_RST_CTRL 0x04
52 #define RV3029_RST_CTRL_SYSR BIT(4)
53 #define RV3029_CONTROL_SECTION_LEN 0x05
56 #define RV3029_W_SEC 0x08
57 #define RV3029_W_MINUTES 0x09
58 #define RV3029_W_HOURS 0x0A
59 #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
60 #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
61 #define RV3029_W_DATE 0x0B
62 #define RV3029_W_DAYS 0x0C
63 #define RV3029_W_MONTHS 0x0D
64 #define RV3029_W_YEARS 0x0E
66 /* eeprom control section */
67 #define RV3029_CONTROL_E2P_EECTRL 0x30
68 #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
69 #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
70 #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
71 #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
72 #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
76 #define RV3029_TRICKLE_SHIFT 4
79 static int rv3029_rtc_get(struct udevice *dev, struct rtc_time *tm)
81 u8 regs[RTC_RV3029_PAGE_LEN];
84 ret = dm_i2c_read(dev, RV3029_W_SEC, regs, sizeof(regs));
86 printf("%s: error reading RTC: %x\n", __func__, ret);
90 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
91 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
93 /* HR field has a more complex interpretation */
95 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
97 if (_hr & RV3029_REG_HR_12_24) {
99 tm->tm_hour = bcd2bin(_hr & 0x1f);
100 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
104 tm->tm_hour = bcd2bin(_hr & 0x3f);
108 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
109 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
110 /* RTC supports only years > 1999 */
111 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 2000;
112 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
117 debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
118 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
119 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
124 static int rv3029_rtc_set(struct udevice *dev, const struct rtc_time *tm)
126 u8 regs[RTC_RV3029_PAGE_LEN];
128 debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
129 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
130 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
133 if (tm->tm_year < 2000) {
134 printf("%s: year %d (before 2000) not supported\n",
135 __func__, tm->tm_year);
139 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
140 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
141 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
142 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
143 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
144 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
145 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 2000);
147 return dm_i2c_write(dev, RV3029_W_SEC, regs, sizeof(regs));
150 static int rv3029_rtc_reset(struct udevice *dev)
152 u8 ctrl = RV3029_RST_CTRL_SYSR;
154 const unsigned long timeout_ms = 10000;
157 /* trigger the system-reset */
158 ret = dm_i2c_write(dev, RV3029_RST_CTRL, &ctrl, 1);
162 /* wait for the system-reset to complete */
163 start = get_timer(0);
165 if (get_timer(start) > timeout_ms)
168 ret = dm_i2c_read(dev, RV3029_RST_CTRL, &ctrl, 1);
171 } while (ctrl & RV3029_RST_CTRL_SYSR);
176 static int rv3029_rtc_read8(struct udevice *dev, unsigned int reg)
181 ret = dm_i2c_read(dev, reg, &data, sizeof(data));
182 return ret < 0 ? ret : data;
185 static int rv3029_rtc_write8(struct udevice *dev, unsigned int reg, int val)
189 return dm_i2c_write(dev, reg, &data, 1);
192 #if defined(OF_CONTROL)
193 static int rv3029_get_sr(struct udevice *dev, u8 *buf)
195 int ret = dm_i2c_read(dev, RV3029_STATUS, buf, 1);
200 dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
204 static int rv3029_set_sr(struct udevice *dev, u8 val)
208 ret = dm_i2c_read(dev, RV3029_STATUS, &val, 1);
212 dev_dbg(dev, "status = 0x%.2x (%d)\n", val, val);
216 static int rv3029_eeprom_busywait(struct udevice *dev)
221 for (i = 100; i > 0; i--) {
222 ret = rv3029_get_sr(dev, &sr);
225 if (!(sr & RV3029_STATUS_EEBUSY))
230 dev_err(dev, "EEPROM busy wait timeout.\n");
237 static int rv3029_update_bits(struct udevice *dev, u8 reg, u8 mask, u8 set)
242 ret = dm_i2c_read(dev, reg, &buf, 1);
246 if ((buf & mask) == (set && mask))
249 buf = (buf & ~mask) | (set & mask);
250 ret = dm_i2c_read(dev, reg, &buf, 1);
257 static int rv3029_eeprom_exit(struct udevice *dev)
259 /* Re-enable eeprom refresh */
260 return rv3029_update_bits(dev, RV3029_ONOFF_CTRL,
261 RV3029_ONOFF_CTRL_EERE,
262 RV3029_ONOFF_CTRL_EERE);
265 static int rv3029_eeprom_enter(struct udevice *dev)
270 /* Check whether we are in the allowed voltage range. */
271 ret = rv3029_get_sr(dev, &sr);
274 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
275 /* We clear the bits and retry once just in case
276 * we had a brown out in early startup.
278 sr &= ~RV3029_STATUS_VLOW1;
279 sr &= ~RV3029_STATUS_VLOW2;
280 ret = rv3029_set_sr(dev, sr);
284 ret = rv3029_get_sr(dev, &sr);
287 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
288 dev_err(dev, "Supply voltage is too low to safely access the EEPROM.\n");
293 /* Disable eeprom refresh. */
294 ret = rv3029_update_bits(dev,
295 RV3029_ONOFF_CTRL, RV3029_ONOFF_CTRL_EERE, 0);
299 /* Wait for any previous eeprom accesses to finish. */
300 ret = rv3029_eeprom_busywait(dev);
302 rv3029_eeprom_exit(dev);
307 static int rv3029_eeprom_read(struct udevice *dev, u8 reg,
308 u8 buf[], size_t len)
312 err = rv3029_eeprom_enter(dev);
316 ret = dm_i2c_read(dev, reg, buf, len);
318 err = rv3029_eeprom_exit(dev);
325 static int rv3029_eeprom_write(struct udevice *dev, u8 reg,
326 u8 const buf[], size_t len)
332 ret = rv3029_eeprom_enter(dev);
336 for (i = 0; i < len; i++, reg++) {
337 ret = dm_i2c_read(dev, reg, &tmp, 1);
341 ret = dm_i2c_write(dev, reg, &buf[i], 1);
345 ret = rv3029_eeprom_busywait(dev);
350 ret = rv3029_eeprom_exit(dev);
357 static int rv3029_eeprom_update_bits(struct udevice *dev,
358 u8 reg, u8 mask, u8 set)
363 ret = rv3029_eeprom_read(dev, reg, &buf, 1);
368 * If the EEPROM already reads the correct bitpattern, we don't need
371 if ((buf & mask) == (set & mask))
374 buf = (buf & ~mask) | (set & mask);
375 ret = rv3029_eeprom_write(dev, reg, &buf, 1);
382 static void rv3029_trickle_config(struct udevice *dev)
384 static const struct rv3029_trickle_tab_elem {
385 u32 r; /* resistance in ohms */
386 u8 conf; /* trickle config bits */
387 } rv3029_trickle_tab[] = {
390 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
391 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
394 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
398 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
402 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
405 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
409 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
412 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
415 .conf = RV3029_TRICKLE_1K,
418 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
422 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
425 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
428 .conf = RV3029_TRICKLE_5K,
431 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
434 .conf = RV3029_TRICKLE_20K,
437 .conf = RV3029_TRICKLE_80K,
442 u8 trickle_set_bits = 0;
444 /* Configure the trickle charger. */
445 err = dev_read_u32(dev, "trickle-resistor-ohms", &ohms);
448 /* Find trickle-charger config */
449 for (int i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++)
450 if (rv3029_trickle_tab[i].r >= ohms) {
451 dev_dbg(dev, "trickle charger at %d ohms\n",
452 rv3029_trickle_tab[i].r);
453 trickle_set_bits = rv3029_trickle_tab[i].conf;
458 dev_dbg(dev, "trickle charger config 0x%x\n", trickle_set_bits);
459 err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
463 dev_dbg(dev, "failed to update trickle charger\n");
466 static inline void rv3029_trickle_config(struct udevice *dev)
471 static int rv3029_probe(struct udevice *dev)
473 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
474 DM_I2C_CHIP_WR_ADDRESS);
476 rv3029_trickle_config(dev);
480 static const struct rtc_ops rv3029_rtc_ops = {
481 .get = rv3029_rtc_get,
482 .set = rv3029_rtc_set,
483 .read8 = rv3029_rtc_read8,
484 .write8 = rv3029_rtc_write8,
485 .reset = rv3029_rtc_reset,
488 static const struct udevice_id rv3029_rtc_ids[] = {
489 { .compatible = "mc,rv3029" },
490 { .compatible = "mc,rv3029c2" },
494 U_BOOT_DRIVER(rtc_rv3029) = {
495 .name = "rtc-rv3029",
497 .probe = rv3029_probe,
498 .of_match = rv3029_rtc_ids,
499 .ops = &rv3029_rtc_ops,