blob: b14d2a246ffcf9eaac02d145844e36bb680bc8ab [file] [log] [blame]
Heiko Schochereac52242021-03-10 08:15:00 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * RTC driver for the Micro Crystal RV3028
4 *
5 * based on linux driver from
6 * Copyright (C) 2019 Micro Crystal SA
7 *
8 * Alexandre Belloni <alexandre.belloni@bootlin.com>
9 *
10 */
11
12#include <dm.h>
13#include <i2c.h>
14#include <rtc.h>
Arturo Buzarra55b21642024-09-23 10:25:18 +020015#include <dm/device_compat.h>
16#include <linux/delay.h>
17#include <power/regulator.h>
Heiko Schochereac52242021-03-10 08:15:00 +010018
19#define RV3028_SEC 0x00
20#define RV3028_MIN 0x01
21#define RV3028_HOUR 0x02
22#define RV3028_WDAY 0x03
23#define RV3028_DAY 0x04
24#define RV3028_MONTH 0x05
25#define RV3028_YEAR 0x06
26#define RV3028_ALARM_MIN 0x07
27#define RV3028_ALARM_HOUR 0x08
28#define RV3028_ALARM_DAY 0x09
29#define RV3028_STATUS 0x0E
30#define RV3028_CTRL1 0x0F
31#define RV3028_CTRL2 0x10
32#define RV3028_EVT_CTRL 0x13
33#define RV3028_TS_COUNT 0x14
34#define RV3028_TS_SEC 0x15
35#define RV3028_RAM1 0x1F
36#define RV3028_EEPROM_ADDR 0x25
37#define RV3028_EEPROM_DATA 0x26
38#define RV3028_EEPROM_CMD 0x27
39#define RV3028_CLKOUT 0x35
40#define RV3028_OFFSET 0x36
41#define RV3028_BACKUP 0x37
42
43#define RV3028_STATUS_PORF BIT(0)
44#define RV3028_STATUS_EVF BIT(1)
45#define RV3028_STATUS_AF BIT(2)
46#define RV3028_STATUS_TF BIT(3)
47#define RV3028_STATUS_UF BIT(4)
48#define RV3028_STATUS_BSF BIT(5)
49#define RV3028_STATUS_CLKF BIT(6)
50#define RV3028_STATUS_EEBUSY BIT(7)
51
52#define RV3028_CLKOUT_FD_MASK GENMASK(2, 0)
53#define RV3028_CLKOUT_PORIE BIT(3)
54#define RV3028_CLKOUT_CLKSY BIT(6)
55#define RV3028_CLKOUT_CLKOE BIT(7)
56
57#define RV3028_CTRL1_EERD BIT(3)
58#define RV3028_CTRL1_WADA BIT(5)
59
60#define RV3028_CTRL2_RESET BIT(0)
61#define RV3028_CTRL2_12_24 BIT(1)
62#define RV3028_CTRL2_EIE BIT(2)
63#define RV3028_CTRL2_AIE BIT(3)
64#define RV3028_CTRL2_TIE BIT(4)
65#define RV3028_CTRL2_UIE BIT(5)
66#define RV3028_CTRL2_TSE BIT(7)
67
68#define RV3028_EVT_CTRL_TSR BIT(2)
69
70#define RV3028_EEPROM_CMD_UPDATE 0x11
71#define RV3028_EEPROM_CMD_WRITE 0x21
72#define RV3028_EEPROM_CMD_READ 0x22
73
74#define RV3028_EEBUSY_POLL 10000
75#define RV3028_EEBUSY_TIMEOUT 100000
76
77#define RV3028_BACKUP_TCE BIT(5)
78#define RV3028_BACKUP_TCR_MASK GENMASK(1, 0)
79
80#define OFFSET_STEP_PPT 953674
81
82#define RTC_RV3028_LEN 7
83
Arturo Buzarra55b21642024-09-23 10:25:18 +020084#define VDD_START_TIME_US 200000
85
86struct rv3028_priv {
87 struct udevice *vdd;
88};
89
Heiko Schochereac52242021-03-10 08:15:00 +010090static int rv3028_rtc_get(struct udevice *dev, struct rtc_time *tm)
91{
92 u8 regs[RTC_RV3028_LEN];
93 u8 status;
94 int ret;
95
96 ret = dm_i2c_read(dev, RV3028_STATUS, &status, 1);
97 if (ret < 0) {
98 printf("%s: error reading RTC status: %x\n", __func__, ret);
99 return -EIO;
100 }
101
102 if (status & RV3028_STATUS_PORF) {
103 printf("Voltage low, data is invalid.\n");
104 return -EINVAL;
105 }
106
107 ret = dm_i2c_read(dev, RV3028_SEC, regs, sizeof(regs));
108 if (ret < 0) {
109 printf("%s: error reading RTC: %x\n", __func__, ret);
110 return -EIO;
111 }
112
113 tm->tm_sec = bcd2bin(regs[RV3028_SEC] & 0x7f);
114 tm->tm_min = bcd2bin(regs[RV3028_MIN] & 0x7f);
115 tm->tm_hour = bcd2bin(regs[RV3028_HOUR] & 0x3f);
116 tm->tm_wday = regs[RV3028_WDAY] & 0x7;
117 tm->tm_mday = bcd2bin(regs[RV3028_DAY] & 0x3f);
118 tm->tm_mon = bcd2bin(regs[RV3028_MONTH] & 0x1f);
119 tm->tm_year = bcd2bin(regs[RV3028_YEAR]) + 2000;
120 tm->tm_yday = 0;
121 tm->tm_isdst = 0;
122
123 debug("%s: %4d-%02d-%02d (wday=%d) %2d:%02d:%02d\n",
124 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
125 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
126
127 return 0;
128}
129
130static int rv3028_rtc_set(struct udevice *dev, const struct rtc_time *tm)
131{
132 u8 regs[RTC_RV3028_LEN];
133 u8 status;
134 int ret;
135
136 debug("%s: %4d-%02d-%02d (wday=%d( %2d:%02d:%02d\n",
137 __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
138 tm->tm_wday, tm->tm_hour, tm->tm_min, tm->tm_sec);
139
140 if (tm->tm_year < 2000) {
141 printf("%s: year %d (before 2000) not supported\n",
142 __func__, tm->tm_year);
143 return -EINVAL;
144 }
145
146 regs[RV3028_SEC] = bin2bcd(tm->tm_sec);
147 regs[RV3028_MIN] = bin2bcd(tm->tm_min);
148 regs[RV3028_HOUR] = bin2bcd(tm->tm_hour);
149 regs[RV3028_WDAY] = tm->tm_wday;
150 regs[RV3028_DAY] = bin2bcd(tm->tm_mday);
151 regs[RV3028_MONTH] = bin2bcd(tm->tm_mon);
152 regs[RV3028_YEAR] = bin2bcd(tm->tm_year - 2000);
153
154 ret = dm_i2c_write(dev, RV3028_SEC, regs, sizeof(regs));
155 if (ret) {
156 printf("%s: set rtc error: %d\n", __func__, ret);
157 return ret;
158 }
159
160 ret = dm_i2c_read(dev, RV3028_STATUS, &status, 1);
161 if (ret < 0) {
162 printf("%s: error reading RTC status: %x\n", __func__, ret);
163 return -EIO;
164 }
165 status |= RV3028_STATUS_PORF;
166 return dm_i2c_write(dev, RV3028_STATUS, &status, 1);
167}
168
169static int rv3028_rtc_reset(struct udevice *dev)
170{
171 return 0;
172}
173
174static int rv3028_rtc_read8(struct udevice *dev, unsigned int reg)
175{
176 u8 data;
177 int ret;
178
179 ret = dm_i2c_read(dev, reg, &data, sizeof(data));
180 return ret < 0 ? ret : data;
181}
182
183static int rv3028_rtc_write8(struct udevice *dev, unsigned int reg, int val)
184{
185 u8 data = val;
186
187 return dm_i2c_write(dev, reg, &data, 1);
188}
189
190static int rv3028_probe(struct udevice *dev)
191{
Arturo Buzarra55b21642024-09-23 10:25:18 +0200192 struct rv3028_priv *priv = dev_get_priv(dev);
193 int ret;
194
195 if (IS_ENABLED(CONFIG_DM_REGULATOR)) {
196 ret = device_get_supply_regulator(dev, "vdd-supply", &priv->vdd);
197 if (ret && ret != -ENOENT) {
198 dev_err(dev, "Warning: cannot get VDD supply\n");
199 return ret;
200 }
201
202 if (priv->vdd) {
203 ret = regulator_set_enable_if_allowed(priv->vdd, true);
204 if (ret) {
205 dev_err(dev, "failed to enable vdd-supply\n");
206 return ret;
207 }
208
209 /* We must wait Tstart = 0.2s before access to I2C */
210 udelay(VDD_START_TIME_US);
211 }
212 }
213
Heiko Schochereac52242021-03-10 08:15:00 +0100214 i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS |
215 DM_I2C_CHIP_WR_ADDRESS);
216
217 return 0;
218}
219
220static const struct rtc_ops rv3028_rtc_ops = {
221 .get = rv3028_rtc_get,
222 .set = rv3028_rtc_set,
223 .read8 = rv3028_rtc_read8,
224 .write8 = rv3028_rtc_write8,
225 .reset = rv3028_rtc_reset,
226};
227
228static const struct udevice_id rv3028_rtc_ids[] = {
229 { .compatible = "microcrystal,rv3028" },
230 { }
231};
232
233U_BOOT_DRIVER(rtc_rv3028) = {
234 .name = "rtc-rv3028",
235 .id = UCLASS_RTC,
236 .probe = rv3028_probe,
237 .of_match = rv3028_rtc_ids,
238 .ops = &rv3028_rtc_ops,
Arturo Buzarra55b21642024-09-23 10:25:18 +0200239 .priv_auto = sizeof(struct rv3028_priv),
Heiko Schochereac52242021-03-10 08:15:00 +0100240};