183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
25c4fa2eeSHeiko Schocher /*
3a73610d2SPhilipp Tomsich * (C) Copyright 2018 Theobroma Systems Design und Consulting GmbH
4a73610d2SPhilipp Tomsich *
5a73610d2SPhilipp Tomsich * Based on a the Linux rtc-rv3029c2.c driver written by:
6a73610d2SPhilipp Tomsich * Gregory Hermant <gregory.hermant@calao-systems.com>
7a73610d2SPhilipp Tomsich * Michael Buesch <m@bues.ch>
85c4fa2eeSHeiko Schocher */
9a73610d2SPhilipp Tomsich
105c4fa2eeSHeiko Schocher #include <common.h>
115c4fa2eeSHeiko Schocher #include <command.h>
12a73610d2SPhilipp Tomsich #include <dm.h>
13*62465631SSimon Glass #include <eeprom.h>
145c4fa2eeSHeiko Schocher #include <i2c.h>
155c4fa2eeSHeiko Schocher #include <rtc.h>
165c4fa2eeSHeiko Schocher
175c4fa2eeSHeiko Schocher #define RTC_RV3029_PAGE_LEN 7
185c4fa2eeSHeiko Schocher
19a73610d2SPhilipp Tomsich /* control section */
20a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL 0x00
21a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_WE BIT(0)
22a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_TE BIT(1)
23a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_TAR BIT(2)
24a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_EERE BIT(3)
25a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_SRON BIT(4)
26a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_TD0 BIT(5)
27a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_TD1 BIT(6)
28a73610d2SPhilipp Tomsich #define RV3029_ONOFF_CTRL_CLKINT BIT(7)
29a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL 0x01
30a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL_AIE BIT(0)
31a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL_TIE BIT(1)
32a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL_V1IE BIT(2)
33a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL_V2IE BIT(3)
34a73610d2SPhilipp Tomsich #define RV3029_IRQ_CTRL_SRIE BIT(4)
35a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS 0x02
36a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS_AF BIT(0)
37a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS_TF BIT(1)
38a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS_V1IF BIT(2)
39a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS_V2IF BIT(3)
40a73610d2SPhilipp Tomsich #define RV3029_IRQ_FLAGS_SRF BIT(4)
41a73610d2SPhilipp Tomsich #define RV3029_STATUS 0x03
42a73610d2SPhilipp Tomsich #define RV3029_STATUS_VLOW1 BIT(2)
43a73610d2SPhilipp Tomsich #define RV3029_STATUS_VLOW2 BIT(3)
44a73610d2SPhilipp Tomsich #define RV3029_STATUS_SR BIT(4)
45a73610d2SPhilipp Tomsich #define RV3029_STATUS_PON BIT(5)
46a73610d2SPhilipp Tomsich #define RV3029_STATUS_EEBUSY BIT(7)
47a73610d2SPhilipp Tomsich #define RV3029_RST_CTRL 0x04
48a73610d2SPhilipp Tomsich #define RV3029_RST_CTRL_SYSR BIT(4)
49a73610d2SPhilipp Tomsich #define RV3029_CONTROL_SECTION_LEN 0x05
505c4fa2eeSHeiko Schocher
51a73610d2SPhilipp Tomsich /* watch section */
52a73610d2SPhilipp Tomsich #define RV3029_W_SEC 0x08
53a73610d2SPhilipp Tomsich #define RV3029_W_MINUTES 0x09
54a73610d2SPhilipp Tomsich #define RV3029_W_HOURS 0x0A
55a73610d2SPhilipp Tomsich #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
56a73610d2SPhilipp Tomsich #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
57a73610d2SPhilipp Tomsich #define RV3029_W_DATE 0x0B
58a73610d2SPhilipp Tomsich #define RV3029_W_DAYS 0x0C
59a73610d2SPhilipp Tomsich #define RV3029_W_MONTHS 0x0D
60a73610d2SPhilipp Tomsich #define RV3029_W_YEARS 0x0E
615c4fa2eeSHeiko Schocher
62a73610d2SPhilipp Tomsich /* eeprom control section */
63a73610d2SPhilipp Tomsich #define RV3029_CONTROL_E2P_EECTRL 0x30
64a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
65a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
66a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
67a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
68a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
69a73610d2SPhilipp Tomsich RV3029_TRICKLE_5K |\
70a73610d2SPhilipp Tomsich RV3029_TRICKLE_20K |\
71a73610d2SPhilipp Tomsich RV3029_TRICKLE_80K)
72a73610d2SPhilipp Tomsich #define RV3029_TRICKLE_SHIFT 4
7371d19f30SHeiko Schocher
74a73610d2SPhilipp Tomsich
rv3029_rtc_get(struct udevice * dev,struct rtc_time * tm)75a73610d2SPhilipp Tomsich static int rv3029_rtc_get(struct udevice *dev, struct rtc_time *tm)
765c4fa2eeSHeiko Schocher {
77a73610d2SPhilipp Tomsich u8 regs[RTC_RV3029_PAGE_LEN];
785c4fa2eeSHeiko Schocher int ret;
795c4fa2eeSHeiko Schocher
80a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, RV3029_W_SEC, regs, sizeof(regs));
81a73610d2SPhilipp Tomsich if (ret < 0) {
825c4fa2eeSHeiko Schocher printf("%s: error reading RTC: %x\n", __func__, ret);
83a73610d2SPhilipp Tomsich return -EIO;
845c4fa2eeSHeiko Schocher }
85a73610d2SPhilipp Tomsich
86a73610d2SPhilipp Tomsich tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
87a73610d2SPhilipp Tomsich tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
88a73610d2SPhilipp Tomsich
89a73610d2SPhilipp Tomsich /* HR field has a more complex interpretation */
90a73610d2SPhilipp Tomsich {
91a73610d2SPhilipp Tomsich const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
92a73610d2SPhilipp Tomsich
93a73610d2SPhilipp Tomsich if (_hr & RV3029_REG_HR_12_24) {
945c4fa2eeSHeiko Schocher /* 12h format */
95a73610d2SPhilipp Tomsich tm->tm_hour = bcd2bin(_hr & 0x1f);
96a73610d2SPhilipp Tomsich if (_hr & RV3029_REG_HR_PM) /* PM flag set */
97a73610d2SPhilipp Tomsich tm->tm_hour += 12;
98a73610d2SPhilipp Tomsich } else {
99a73610d2SPhilipp Tomsich /* 24h format */
100a73610d2SPhilipp Tomsich tm->tm_hour = bcd2bin(_hr & 0x3f);
101a73610d2SPhilipp Tomsich }
102a73610d2SPhilipp Tomsich }
1035c4fa2eeSHeiko Schocher
104a73610d2SPhilipp Tomsich tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
105a73610d2SPhilipp Tomsich tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
1065c4fa2eeSHeiko Schocher /* RTC supports only years > 1999 */
107a73610d2SPhilipp Tomsich tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 2000;
108a73610d2SPhilipp Tomsich tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
1095c4fa2eeSHeiko Schocher
110a73610d2SPhilipp Tomsich tm->tm_yday = 0;
111a73610d2SPhilipp Tomsich tm->tm_isdst = 0;
112a73610d2SPhilipp Tomsich
113a73610d2SPhilipp Tomsich debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
114a73610d2SPhilipp Tomsich __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
115a73610d2SPhilipp Tomsich tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
1165c4fa2eeSHeiko Schocher
1175c4fa2eeSHeiko Schocher return 0;
1185c4fa2eeSHeiko Schocher }
1195c4fa2eeSHeiko Schocher
rv3029_rtc_set(struct udevice * dev,const struct rtc_time * tm)120a73610d2SPhilipp Tomsich static int rv3029_rtc_set(struct udevice *dev, const struct rtc_time *tm)
121a73610d2SPhilipp Tomsich {
122a73610d2SPhilipp Tomsich u8 regs[RTC_RV3029_PAGE_LEN];
123a73610d2SPhilipp Tomsich
124a73610d2SPhilipp Tomsich debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
125a73610d2SPhilipp Tomsich __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
126a73610d2SPhilipp Tomsich tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
127a73610d2SPhilipp Tomsich
128a73610d2SPhilipp Tomsich
129a73610d2SPhilipp Tomsich if (tm->tm_year < 2000) {
130a73610d2SPhilipp Tomsich printf("%s: year %d (before 2000) not supported\n",
131a73610d2SPhilipp Tomsich __func__, tm->tm_year);
132a73610d2SPhilipp Tomsich return -EINVAL;
133a73610d2SPhilipp Tomsich }
134a73610d2SPhilipp Tomsich
135a73610d2SPhilipp Tomsich regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
136a73610d2SPhilipp Tomsich regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
137a73610d2SPhilipp Tomsich regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
138a73610d2SPhilipp Tomsich regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
139a73610d2SPhilipp Tomsich regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
140a73610d2SPhilipp Tomsich regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
141a73610d2SPhilipp Tomsich regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 2000);
142a73610d2SPhilipp Tomsich
143a73610d2SPhilipp Tomsich return dm_i2c_write(dev, RV3029_W_SEC, regs, sizeof(regs));
144a73610d2SPhilipp Tomsich }
145a73610d2SPhilipp Tomsich
rv3029_rtc_reset(struct udevice * dev)146a73610d2SPhilipp Tomsich static int rv3029_rtc_reset(struct udevice *dev)
147a73610d2SPhilipp Tomsich {
148a73610d2SPhilipp Tomsich u8 ctrl = RV3029_RST_CTRL_SYSR;
149a73610d2SPhilipp Tomsich unsigned long start;
150a73610d2SPhilipp Tomsich const unsigned long timeout_ms = 10000;
151a73610d2SPhilipp Tomsich int ret;
152a73610d2SPhilipp Tomsich
153a73610d2SPhilipp Tomsich /* trigger the system-reset */
154a73610d2SPhilipp Tomsich ret = dm_i2c_write(dev, RV3029_RST_CTRL, &ctrl, 1);
155a73610d2SPhilipp Tomsich if (ret < 0)
156a73610d2SPhilipp Tomsich return -EIO;
157a73610d2SPhilipp Tomsich
158a73610d2SPhilipp Tomsich /* wait for the system-reset to complete */
159a73610d2SPhilipp Tomsich start = get_timer(0);
160a73610d2SPhilipp Tomsich do {
161a73610d2SPhilipp Tomsich if (get_timer(start) > timeout_ms)
162a73610d2SPhilipp Tomsich return -ETIMEDOUT;
163a73610d2SPhilipp Tomsich
164a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, RV3029_RST_CTRL, &ctrl, 1);
165a73610d2SPhilipp Tomsich if (ret < 0)
166a73610d2SPhilipp Tomsich return -EIO;
167a73610d2SPhilipp Tomsich } while (ctrl & RV3029_RST_CTRL_SYSR);
168a73610d2SPhilipp Tomsich
169a73610d2SPhilipp Tomsich return 0;
170a73610d2SPhilipp Tomsich }
171a73610d2SPhilipp Tomsich
rv3029_rtc_read8(struct udevice * dev,unsigned int reg)172a73610d2SPhilipp Tomsich static int rv3029_rtc_read8(struct udevice *dev, unsigned int reg)
173a73610d2SPhilipp Tomsich {
174a73610d2SPhilipp Tomsich u8 data;
175a73610d2SPhilipp Tomsich int ret;
176a73610d2SPhilipp Tomsich
177a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, reg, &data, sizeof(data));
178a73610d2SPhilipp Tomsich return ret < 0 ? ret : data;
179a73610d2SPhilipp Tomsich }
180a73610d2SPhilipp Tomsich
rv3029_rtc_write8(struct udevice * dev,unsigned int reg,int val)181a73610d2SPhilipp Tomsich static int rv3029_rtc_write8(struct udevice *dev, unsigned int reg, int val)
182a73610d2SPhilipp Tomsich {
183a73610d2SPhilipp Tomsich u8 data = val;
184a73610d2SPhilipp Tomsich
185a73610d2SPhilipp Tomsich return dm_i2c_write(dev, reg, &data, 1);
186a73610d2SPhilipp Tomsich }
187a73610d2SPhilipp Tomsich
188a73610d2SPhilipp Tomsich #if defined(OF_CONTROL)
rv3029_get_sr(struct udevice * dev,u8 * buf)189a73610d2SPhilipp Tomsich static int rv3029_get_sr(struct udevice *dev, u8 *buf)
190a73610d2SPhilipp Tomsich {
191a73610d2SPhilipp Tomsich int ret = dm_i2c_read(dev, RV3029_STATUS, buf, 1);
192a73610d2SPhilipp Tomsich
193a73610d2SPhilipp Tomsich if (ret < 0)
194a73610d2SPhilipp Tomsich return -EIO;
195a73610d2SPhilipp Tomsich
196a73610d2SPhilipp Tomsich dev_dbg(dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
197a73610d2SPhilipp Tomsich return 0;
198a73610d2SPhilipp Tomsich }
199a73610d2SPhilipp Tomsich
rv3029_set_sr(struct udevice * dev,u8 val)200a73610d2SPhilipp Tomsich static int rv3029_set_sr(struct udevice *dev, u8 val)
2015c4fa2eeSHeiko Schocher {
2025c4fa2eeSHeiko Schocher int ret;
203b633741bSWolfgang Denk
204a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, RV3029_STATUS, &val, 1);
205a73610d2SPhilipp Tomsich if (ret < 0)
206a73610d2SPhilipp Tomsich return -EIO;
2075c4fa2eeSHeiko Schocher
208a73610d2SPhilipp Tomsich dev_dbg(dev, "status = 0x%.2x (%d)\n", val, val);
209a73610d2SPhilipp Tomsich return 0;
2105c4fa2eeSHeiko Schocher }
2115c4fa2eeSHeiko Schocher
rv3029_eeprom_busywait(struct udevice * dev)212a73610d2SPhilipp Tomsich static int rv3029_eeprom_busywait(struct udevice *dev)
21371d19f30SHeiko Schocher {
214a73610d2SPhilipp Tomsich int i, ret;
215a73610d2SPhilipp Tomsich u8 sr;
21671d19f30SHeiko Schocher
217a73610d2SPhilipp Tomsich for (i = 100; i > 0; i--) {
218a73610d2SPhilipp Tomsich ret = rv3029_get_sr(dev, &sr);
219a73610d2SPhilipp Tomsich if (ret < 0)
220a73610d2SPhilipp Tomsich break;
221a73610d2SPhilipp Tomsich if (!(sr & RV3029_STATUS_EEBUSY))
22271d19f30SHeiko Schocher break;
22371d19f30SHeiko Schocher udelay(10000);
22471d19f30SHeiko Schocher }
225a73610d2SPhilipp Tomsich if (i <= 0) {
226a73610d2SPhilipp Tomsich dev_err(dev, "EEPROM busy wait timeout.\n");
227a73610d2SPhilipp Tomsich return -ETIMEDOUT;
22871d19f30SHeiko Schocher }
22971d19f30SHeiko Schocher
230a73610d2SPhilipp Tomsich return ret;
231a73610d2SPhilipp Tomsich }
232a73610d2SPhilipp Tomsich
rv3029_update_bits(struct udevice * dev,u8 reg,u8 mask,u8 set)233a73610d2SPhilipp Tomsich static int rv3029_update_bits(struct udevice *dev, u8 reg, u8 mask, u8 set)
2345c4fa2eeSHeiko Schocher {
235a73610d2SPhilipp Tomsich u8 buf;
236a73610d2SPhilipp Tomsich int ret;
2375c4fa2eeSHeiko Schocher
238a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, reg, &buf, 1);
239a73610d2SPhilipp Tomsich if (ret < 0)
240a73610d2SPhilipp Tomsich return ret;
24171d19f30SHeiko Schocher
242a73610d2SPhilipp Tomsich if ((buf & mask) == (set && mask))
243a73610d2SPhilipp Tomsich return 0;
24471d19f30SHeiko Schocher
245a73610d2SPhilipp Tomsich buf = (buf & ~mask) | (set & mask);
246a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, reg, &buf, 1);
247a73610d2SPhilipp Tomsich if (ret < 0)
248a73610d2SPhilipp Tomsich return ret;
249a73610d2SPhilipp Tomsich
250a73610d2SPhilipp Tomsich return 0;
251a73610d2SPhilipp Tomsich }
252a73610d2SPhilipp Tomsich
rv3029_eeprom_exit(struct udevice * dev)253a73610d2SPhilipp Tomsich static int rv3029_eeprom_exit(struct udevice *dev)
254a73610d2SPhilipp Tomsich {
255a73610d2SPhilipp Tomsich /* Re-enable eeprom refresh */
256a73610d2SPhilipp Tomsich return rv3029_update_bits(dev, RV3029_ONOFF_CTRL,
257a73610d2SPhilipp Tomsich RV3029_ONOFF_CTRL_EERE,
258a73610d2SPhilipp Tomsich RV3029_ONOFF_CTRL_EERE);
259a73610d2SPhilipp Tomsich }
260a73610d2SPhilipp Tomsich
rv3029_eeprom_enter(struct udevice * dev)261a73610d2SPhilipp Tomsich static int rv3029_eeprom_enter(struct udevice *dev)
262a73610d2SPhilipp Tomsich {
263a73610d2SPhilipp Tomsich int ret;
264a73610d2SPhilipp Tomsich u8 sr;
265a73610d2SPhilipp Tomsich
266a73610d2SPhilipp Tomsich /* Check whether we are in the allowed voltage range. */
267a73610d2SPhilipp Tomsich ret = rv3029_get_sr(dev, &sr);
268a73610d2SPhilipp Tomsich if (ret < 0)
269a73610d2SPhilipp Tomsich return ret;
270a73610d2SPhilipp Tomsich if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
271a73610d2SPhilipp Tomsich /* We clear the bits and retry once just in case
272a73610d2SPhilipp Tomsich * we had a brown out in early startup.
27371d19f30SHeiko Schocher */
274a73610d2SPhilipp Tomsich sr &= ~RV3029_STATUS_VLOW1;
275a73610d2SPhilipp Tomsich sr &= ~RV3029_STATUS_VLOW2;
276a73610d2SPhilipp Tomsich ret = rv3029_set_sr(dev, sr);
277a73610d2SPhilipp Tomsich if (ret < 0)
278a73610d2SPhilipp Tomsich return ret;
27971d19f30SHeiko Schocher udelay(10000);
280a73610d2SPhilipp Tomsich ret = rv3029_get_sr(dev, &sr);
281a73610d2SPhilipp Tomsich if (ret < 0)
282a73610d2SPhilipp Tomsich return ret;
283a73610d2SPhilipp Tomsich if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
284a73610d2SPhilipp Tomsich dev_err(dev, "Supply voltage is too low to safely access the EEPROM.\n");
285a73610d2SPhilipp Tomsich return -ENODEV;
286a73610d2SPhilipp Tomsich }
287a73610d2SPhilipp Tomsich }
288a73610d2SPhilipp Tomsich
289a73610d2SPhilipp Tomsich /* Disable eeprom refresh. */
290a73610d2SPhilipp Tomsich ret = rv3029_update_bits(dev,
291a73610d2SPhilipp Tomsich RV3029_ONOFF_CTRL, RV3029_ONOFF_CTRL_EERE, 0);
292a73610d2SPhilipp Tomsich if (ret < 0)
293a73610d2SPhilipp Tomsich return ret;
294a73610d2SPhilipp Tomsich
295a73610d2SPhilipp Tomsich /* Wait for any previous eeprom accesses to finish. */
296a73610d2SPhilipp Tomsich ret = rv3029_eeprom_busywait(dev);
297a73610d2SPhilipp Tomsich if (ret < 0)
298a73610d2SPhilipp Tomsich rv3029_eeprom_exit(dev);
299a73610d2SPhilipp Tomsich
300a73610d2SPhilipp Tomsich return ret;
301a73610d2SPhilipp Tomsich }
302a73610d2SPhilipp Tomsich
rv3029_eeprom_read(struct udevice * dev,u8 reg,u8 buf[],size_t len)303a73610d2SPhilipp Tomsich static int rv3029_eeprom_read(struct udevice *dev, u8 reg,
304a73610d2SPhilipp Tomsich u8 buf[], size_t len)
305a73610d2SPhilipp Tomsich {
306a73610d2SPhilipp Tomsich int ret, err;
307a73610d2SPhilipp Tomsich
308a73610d2SPhilipp Tomsich err = rv3029_eeprom_enter(dev);
309a73610d2SPhilipp Tomsich if (err < 0)
310a73610d2SPhilipp Tomsich return err;
311a73610d2SPhilipp Tomsich
312a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, reg, buf, len);
313a73610d2SPhilipp Tomsich
314a73610d2SPhilipp Tomsich err = rv3029_eeprom_exit(dev);
315a73610d2SPhilipp Tomsich if (err < 0)
316a73610d2SPhilipp Tomsich return err;
317a73610d2SPhilipp Tomsich
318a73610d2SPhilipp Tomsich return ret;
319a73610d2SPhilipp Tomsich }
320a73610d2SPhilipp Tomsich
rv3029_eeprom_write(struct udevice * dev,u8 reg,u8 const buf[],size_t len)321a73610d2SPhilipp Tomsich static int rv3029_eeprom_write(struct udevice *dev, u8 reg,
322a73610d2SPhilipp Tomsich u8 const buf[], size_t len)
323a73610d2SPhilipp Tomsich {
324a73610d2SPhilipp Tomsich int ret;
325a73610d2SPhilipp Tomsich size_t i;
326a73610d2SPhilipp Tomsich u8 tmp;
327a73610d2SPhilipp Tomsich
328a73610d2SPhilipp Tomsich ret = rv3029_eeprom_enter(dev);
329a73610d2SPhilipp Tomsich if (ret < 0)
330a73610d2SPhilipp Tomsich return ret;
331a73610d2SPhilipp Tomsich
332a73610d2SPhilipp Tomsich for (i = 0; i < len; i++, reg++) {
333a73610d2SPhilipp Tomsich ret = dm_i2c_read(dev, reg, &tmp, 1);
334a73610d2SPhilipp Tomsich if (ret < 0)
335a73610d2SPhilipp Tomsich break;
336a73610d2SPhilipp Tomsich if (tmp != buf[i]) {
337a73610d2SPhilipp Tomsich ret = dm_i2c_write(dev, reg, &buf[i], 1);
338a73610d2SPhilipp Tomsich if (ret < 0)
339a73610d2SPhilipp Tomsich break;
340a73610d2SPhilipp Tomsich }
341a73610d2SPhilipp Tomsich ret = rv3029_eeprom_busywait(dev);
342a73610d2SPhilipp Tomsich if (ret < 0)
343a73610d2SPhilipp Tomsich break;
344a73610d2SPhilipp Tomsich }
345a73610d2SPhilipp Tomsich
346a73610d2SPhilipp Tomsich ret = rv3029_eeprom_exit(dev);
347a73610d2SPhilipp Tomsich if (ret < 0)
348a73610d2SPhilipp Tomsich return ret;
349a73610d2SPhilipp Tomsich
350a73610d2SPhilipp Tomsich return 0;
351a73610d2SPhilipp Tomsich }
352a73610d2SPhilipp Tomsich
rv3029_eeprom_update_bits(struct udevice * dev,u8 reg,u8 mask,u8 set)353a73610d2SPhilipp Tomsich static int rv3029_eeprom_update_bits(struct udevice *dev,
354a73610d2SPhilipp Tomsich u8 reg, u8 mask, u8 set)
355a73610d2SPhilipp Tomsich {
356a73610d2SPhilipp Tomsich u8 buf;
357a73610d2SPhilipp Tomsich int ret;
358a73610d2SPhilipp Tomsich
359a73610d2SPhilipp Tomsich ret = rv3029_eeprom_read(dev, reg, &buf, 1);
360a73610d2SPhilipp Tomsich if (ret < 0)
361a73610d2SPhilipp Tomsich return ret;
362a73610d2SPhilipp Tomsich
363a73610d2SPhilipp Tomsich /*
364a73610d2SPhilipp Tomsich * If the EEPROM already reads the correct bitpattern, we don't need
365a73610d2SPhilipp Tomsich * to update it.
366a73610d2SPhilipp Tomsich */
367a73610d2SPhilipp Tomsich if ((buf & mask) == (set & mask))
368a73610d2SPhilipp Tomsich return 0;
369a73610d2SPhilipp Tomsich
370a73610d2SPhilipp Tomsich buf = (buf & ~mask) | (set & mask);
371a73610d2SPhilipp Tomsich ret = rv3029_eeprom_write(dev, reg, &buf, 1);
372a73610d2SPhilipp Tomsich if (ret < 0)
373a73610d2SPhilipp Tomsich return ret;
374a73610d2SPhilipp Tomsich
375a73610d2SPhilipp Tomsich return 0;
376a73610d2SPhilipp Tomsich }
377a73610d2SPhilipp Tomsich
rv3029_trickle_config(struct udevice * dev)378a73610d2SPhilipp Tomsich static void rv3029_trickle_config(struct udevice *dev)
379a73610d2SPhilipp Tomsich {
380a73610d2SPhilipp Tomsich static const struct rv3029_trickle_tab_elem {
381a73610d2SPhilipp Tomsich u32 r; /* resistance in ohms */
382a73610d2SPhilipp Tomsich u8 conf; /* trickle config bits */
383a73610d2SPhilipp Tomsich } rv3029_trickle_tab[] = {
384a73610d2SPhilipp Tomsich {
385a73610d2SPhilipp Tomsich .r = 1076,
386a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
387a73610d2SPhilipp Tomsich RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
388a73610d2SPhilipp Tomsich }, {
389a73610d2SPhilipp Tomsich .r = 1091,
390a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
391a73610d2SPhilipp Tomsich RV3029_TRICKLE_20K,
392a73610d2SPhilipp Tomsich }, {
393a73610d2SPhilipp Tomsich .r = 1137,
394a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
395a73610d2SPhilipp Tomsich RV3029_TRICKLE_80K,
396a73610d2SPhilipp Tomsich }, {
397a73610d2SPhilipp Tomsich .r = 1154,
398a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
399a73610d2SPhilipp Tomsich }, {
400a73610d2SPhilipp Tomsich .r = 1371,
401a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
402a73610d2SPhilipp Tomsich RV3029_TRICKLE_80K,
403a73610d2SPhilipp Tomsich }, {
404a73610d2SPhilipp Tomsich .r = 1395,
405a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
406a73610d2SPhilipp Tomsich }, {
407a73610d2SPhilipp Tomsich .r = 1472,
408a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
409a73610d2SPhilipp Tomsich }, {
410a73610d2SPhilipp Tomsich .r = 1500,
411a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_1K,
412a73610d2SPhilipp Tomsich }, {
413a73610d2SPhilipp Tomsich .r = 3810,
414a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
415a73610d2SPhilipp Tomsich RV3029_TRICKLE_80K,
416a73610d2SPhilipp Tomsich }, {
417a73610d2SPhilipp Tomsich .r = 4000,
418a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
419a73610d2SPhilipp Tomsich }, {
420a73610d2SPhilipp Tomsich .r = 4706,
421a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
422a73610d2SPhilipp Tomsich }, {
423a73610d2SPhilipp Tomsich .r = 5000,
424a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_5K,
425a73610d2SPhilipp Tomsich }, {
426a73610d2SPhilipp Tomsich .r = 16000,
427a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
428a73610d2SPhilipp Tomsich }, {
429a73610d2SPhilipp Tomsich .r = 20000,
430a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_20K,
431a73610d2SPhilipp Tomsich }, {
432a73610d2SPhilipp Tomsich .r = 80000,
433a73610d2SPhilipp Tomsich .conf = RV3029_TRICKLE_80K,
434a73610d2SPhilipp Tomsich },
435a73610d2SPhilipp Tomsich };
436a73610d2SPhilipp Tomsich int err;
437a73610d2SPhilipp Tomsich u32 ohms;
438a73610d2SPhilipp Tomsich u8 trickle_set_bits = 0;
439a73610d2SPhilipp Tomsich
440a73610d2SPhilipp Tomsich /* Configure the trickle charger. */
441a73610d2SPhilipp Tomsich err = dev_read_u32(dev, "trickle-resistor-ohms", &ohms);
442a73610d2SPhilipp Tomsich
443a73610d2SPhilipp Tomsich if (!err) {
444a73610d2SPhilipp Tomsich /* Find trickle-charger config */
445a73610d2SPhilipp Tomsich for (int i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++)
446a73610d2SPhilipp Tomsich if (rv3029_trickle_tab[i].r >= ohms) {
447a73610d2SPhilipp Tomsich dev_dbg(dev, "trickle charger at %d ohms\n",
448a73610d2SPhilipp Tomsich rv3029_trickle_tab[i].r);
449a73610d2SPhilipp Tomsich trickle_set_bits = rv3029_trickle_tab[i].conf;
450a73610d2SPhilipp Tomsich break;
451a73610d2SPhilipp Tomsich }
452a73610d2SPhilipp Tomsich }
453a73610d2SPhilipp Tomsich
454a73610d2SPhilipp Tomsich dev_dbg(dev, "trickle charger config 0x%x\n", trickle_set_bits);
455a73610d2SPhilipp Tomsich err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL,
456a73610d2SPhilipp Tomsich RV3029_TRICKLE_MASK,
457a73610d2SPhilipp Tomsich trickle_set_bits);
458a73610d2SPhilipp Tomsich if (err < 0)
459a73610d2SPhilipp Tomsich dev_dbg(dev, "failed to update trickle charger\n");
460a73610d2SPhilipp Tomsich }
461a73610d2SPhilipp Tomsich #else
rv3029_trickle_config(struct udevice * dev)462a73610d2SPhilipp Tomsich static inline void rv3029_trickle_config(struct udevice *dev)
463a73610d2SPhilipp Tomsich {
46471d19f30SHeiko Schocher }
46571d19f30SHeiko Schocher #endif
466a73610d2SPhilipp Tomsich
rv3029_probe(struct udevice * dev)467a73610d2SPhilipp Tomsich static int rv3029_probe(struct udevice *dev)
468a73610d2SPhilipp Tomsich {
469a73610d2SPhilipp Tomsich i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
470a73610d2SPhilipp Tomsich DM_I2C_CHIP_WR_ADDRESS);
471a73610d2SPhilipp Tomsich
472a73610d2SPhilipp Tomsich rv3029_trickle_config(dev);
473a73610d2SPhilipp Tomsich return 0;
4745c4fa2eeSHeiko Schocher }
475a73610d2SPhilipp Tomsich
476a73610d2SPhilipp Tomsich static const struct rtc_ops rv3029_rtc_ops = {
477a73610d2SPhilipp Tomsich .get = rv3029_rtc_get,
478a73610d2SPhilipp Tomsich .set = rv3029_rtc_set,
479a73610d2SPhilipp Tomsich .read8 = rv3029_rtc_read8,
480a73610d2SPhilipp Tomsich .write8 = rv3029_rtc_write8,
481a73610d2SPhilipp Tomsich .reset = rv3029_rtc_reset,
482a73610d2SPhilipp Tomsich };
483a73610d2SPhilipp Tomsich
484a73610d2SPhilipp Tomsich static const struct udevice_id rv3029_rtc_ids[] = {
485a73610d2SPhilipp Tomsich { .compatible = "mc,rv3029" },
486a73610d2SPhilipp Tomsich { .compatible = "mc,rv3029c2" },
487a73610d2SPhilipp Tomsich { }
488a73610d2SPhilipp Tomsich };
489a73610d2SPhilipp Tomsich
490a73610d2SPhilipp Tomsich U_BOOT_DRIVER(rtc_rv3029) = {
491a73610d2SPhilipp Tomsich .name = "rtc-rv3029",
492a73610d2SPhilipp Tomsich .id = UCLASS_RTC,
493a73610d2SPhilipp Tomsich .probe = rv3029_probe,
494a73610d2SPhilipp Tomsich .of_match = rv3029_rtc_ids,
495a73610d2SPhilipp Tomsich .ops = &rv3029_rtc_ops,
496a73610d2SPhilipp Tomsich };
497