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