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