Commit 0c4e81e0eb6bec3aab1f4500f8fb347202c5b601
Committed by
Tom Rini
1 parent
f91fb7242a
Exists in
smarc_8mq_lf_v2020.04
and in
9 other branches
rtc, rx8025: add DM support
add DM support for this RTC driver. Signed-off-by: Heiko Schocher <hs@denx.de>
Showing 1 changed file with 135 additions and 19 deletions Side-by-side Diff
drivers/rtc/rx8025.c
... | ... | @@ -10,8 +10,9 @@ |
10 | 10 | |
11 | 11 | #include <common.h> |
12 | 12 | #include <command.h> |
13 | -#include <rtc.h> | |
13 | +#include <dm.h> | |
14 | 14 | #include <i2c.h> |
15 | +#include <rtc.h> | |
15 | 16 | |
16 | 17 | /*---------------------------------------------------------------------*/ |
17 | 18 | #undef DEBUG_RTC |
... | ... | @@ -27,6 +28,18 @@ |
27 | 28 | # define CONFIG_SYS_I2C_RTC_ADDR 0x32 |
28 | 29 | #endif |
29 | 30 | |
31 | +#ifdef CONFIG_DM_RTC | |
32 | +#define DEV_TYPE struct udevice | |
33 | +#else | |
34 | +/* Local udevice */ | |
35 | +struct ludevice { | |
36 | + u8 chip; | |
37 | +}; | |
38 | + | |
39 | +#define DEV_TYPE struct ludevice | |
40 | + | |
41 | +#endif | |
42 | + | |
30 | 43 | /* |
31 | 44 | * RTC register addresses |
32 | 45 | */ |
33 | 46 | |
34 | 47 | |
35 | 48 | |
36 | 49 | |
37 | 50 | |
... | ... | @@ -68,21 +81,35 @@ |
68 | 81 | */ |
69 | 82 | |
70 | 83 | /* static uchar rtc_read (uchar reg); */ |
84 | +#ifdef CONFIG_DM_RTC | |
85 | +/* | |
86 | + * on mpc85xx based board with DM and offset len 1 | |
87 | + * accessing rtc works fine. May we can drop this ? | |
88 | + */ | |
89 | +#define rtc_read(reg) buf[(reg) & 0xf] | |
90 | +#else | |
71 | 91 | #define rtc_read(reg) buf[((reg) + 1) & 0xf] |
92 | +#endif | |
72 | 93 | |
73 | -static void rtc_write(uchar reg, uchar val); | |
94 | +static int rtc_write(DEV_TYPE *dev, uchar reg, uchar val); | |
74 | 95 | |
75 | 96 | /* |
76 | 97 | * Get the current time from the RTC |
77 | 98 | */ |
78 | -int rtc_get(struct rtc_time *tmp) | |
99 | +static int rx8025_rtc_get(DEV_TYPE *dev, struct rtc_time *tmp) | |
79 | 100 | { |
80 | 101 | int rel = 0; |
81 | 102 | uchar sec, min, hour, mday, wday, mon, year, ctl2; |
82 | 103 | uchar buf[16]; |
83 | 104 | |
84 | - if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16)) | |
105 | +#ifdef CONFIG_DM_RTC | |
106 | + if (dm_i2c_read(dev, 0, buf, sizeof(buf))) { | |
107 | +#else | |
108 | + if (i2c_read(dev->chip, 0, 0, buf, 16)) { | |
109 | +#endif | |
85 | 110 | printf("Error reading from RTC\n"); |
111 | + return -EIO; | |
112 | + } | |
86 | 113 | |
87 | 114 | sec = rtc_read(RTC_SEC_REG_ADDR); |
88 | 115 | min = rtc_read(RTC_MIN_REG_ADDR); |
... | ... | @@ -138,7 +165,7 @@ |
138 | 165 | /* |
139 | 166 | * Set the RTC |
140 | 167 | */ |
141 | -int rtc_set(struct rtc_time *tmp) | |
168 | +static int rx8025_rtc_set(DEV_TYPE *dev, const struct rtc_time *tmp) | |
142 | 169 | { |
143 | 170 | DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", |
144 | 171 | tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday, |
145 | 172 | |
146 | 173 | |
147 | 174 | |
148 | 175 | |
149 | 176 | |
150 | 177 | |
151 | 178 | |
152 | 179 | |
153 | 180 | |
154 | 181 | |
... | ... | @@ -147,45 +174,134 @@ |
147 | 174 | if (tmp->tm_year < 1970 || tmp->tm_year > 2069) |
148 | 175 | printf("WARNING: year should be between 1970 and 2069!\n"); |
149 | 176 | |
150 | - rtc_write(RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100)); | |
151 | - rtc_write(RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon)); | |
152 | - rtc_write(RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday)); | |
153 | - rtc_write(RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday)); | |
154 | - rtc_write(RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour)); | |
155 | - rtc_write(RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min)); | |
156 | - rtc_write(RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec)); | |
177 | + if (rtc_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100))) | |
178 | + return -EIO; | |
157 | 179 | |
158 | - rtc_write(RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412); | |
180 | + if (rtc_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon))) | |
181 | + return -EIO; | |
159 | 182 | |
160 | - return 0; | |
183 | + if (rtc_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday))) | |
184 | + return -EIO; | |
185 | + | |
186 | + if (rtc_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday))) | |
187 | + return -EIO; | |
188 | + | |
189 | + if (rtc_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour))) | |
190 | + return -EIO; | |
191 | + | |
192 | + if (rtc_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min))) | |
193 | + return -EIO; | |
194 | + | |
195 | + if (rtc_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec))) | |
196 | + return -EIO; | |
197 | + | |
198 | + return rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412); | |
161 | 199 | } |
162 | 200 | |
163 | 201 | /* |
164 | 202 | * Reset the RTC |
165 | 203 | */ |
166 | -void rtc_reset(void) | |
204 | +static int rx8025_rtc_reset(DEV_TYPE *dev) | |
167 | 205 | { |
168 | 206 | uchar buf[16]; |
169 | 207 | uchar ctl2; |
170 | 208 | |
171 | - if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16)) | |
209 | +#ifdef CONFIG_DM_RTC | |
210 | + if (dm_i2c_read(dev, 0, buf, sizeof(buf))) { | |
211 | +#else | |
212 | + if (i2c_read(dev->chip, 0, 0, buf, 16)) { | |
213 | +#endif | |
172 | 214 | printf("Error reading from RTC\n"); |
215 | + return -EIO; | |
216 | + } | |
173 | 217 | |
174 | 218 | ctl2 = rtc_read(RTC_CTL2_REG_ADDR); |
175 | 219 | ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET); |
176 | 220 | ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL; |
177 | - rtc_write(RTC_CTL2_REG_ADDR, ctl2); | |
221 | + | |
222 | + return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2); | |
178 | 223 | } |
179 | 224 | |
180 | 225 | /* |
181 | 226 | * Helper functions |
182 | 227 | */ |
183 | -static void rtc_write(uchar reg, uchar val) | |
228 | +static int rtc_write(DEV_TYPE *dev, uchar reg, uchar val) | |
184 | 229 | { |
185 | 230 | uchar buf[2]; |
186 | 231 | buf[0] = reg << 4; |
187 | 232 | buf[1] = val; |
188 | - if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 2) != 0) | |
233 | + | |
234 | +#ifdef CONFIG_DM_RTC | |
235 | + if (dm_i2c_write(dev, 0, buf, 2)) { | |
236 | +#else | |
237 | + if (i2c_write(dev->chip, 0, 0, buf, 2) != 0) { | |
238 | +#endif | |
189 | 239 | printf("Error writing to RTC\n"); |
240 | + return -EIO; | |
241 | + } | |
242 | + | |
243 | + return 0; | |
190 | 244 | } |
245 | + | |
246 | +#ifdef CONFIG_DM_RTC | |
247 | +static int rx8025_probe(struct udevice *dev) | |
248 | +{ | |
249 | + uchar buf[16]; | |
250 | + int ret = 0; | |
251 | + | |
252 | + if (i2c_get_chip_offset_len(dev) != 1) | |
253 | + ret = i2c_set_chip_offset_len(dev, 1); | |
254 | + | |
255 | + if (ret) | |
256 | + return ret; | |
257 | + | |
258 | + return dm_i2c_read(dev, 0, buf, sizeof(buf)); | |
259 | +} | |
260 | + | |
261 | +static const struct rtc_ops rx8025_rtc_ops = { | |
262 | + .get = rx8025_rtc_get, | |
263 | + .set = rx8025_rtc_set, | |
264 | + .reset = rx8025_rtc_reset, | |
265 | +}; | |
266 | + | |
267 | +static const struct udevice_id rx8025_rtc_ids[] = { | |
268 | + { .compatible = "epson,rx8025" }, | |
269 | + { } | |
270 | +}; | |
271 | + | |
272 | +U_BOOT_DRIVER(rx8010sj_rtc) = { | |
273 | + .name = "rx8025_rtc", | |
274 | + .id = UCLASS_RTC, | |
275 | + .probe = rx8025_probe, | |
276 | + .of_match = rx8025_rtc_ids, | |
277 | + .ops = &rx8025_rtc_ops, | |
278 | +}; | |
279 | +#else | |
280 | +int rtc_get(struct rtc_time *tm) | |
281 | +{ | |
282 | + struct ludevice dev = { | |
283 | + .chip = CONFIG_SYS_I2C_RTC_ADDR, | |
284 | + }; | |
285 | + | |
286 | + return rx8025_rtc_get(&dev, tm); | |
287 | +} | |
288 | + | |
289 | +int rtc_set(struct rtc_time *tm) | |
290 | +{ | |
291 | + struct ludevice dev = { | |
292 | + .chip = CONFIG_SYS_I2C_RTC_ADDR, | |
293 | + }; | |
294 | + | |
295 | + return rx8025_rtc_set(&dev, tm); | |
296 | +} | |
297 | + | |
298 | +void rtc_reset(void) | |
299 | +{ | |
300 | + struct ludevice dev = { | |
301 | + .chip = CONFIG_SYS_I2C_RTC_ADDR, | |
302 | + }; | |
303 | + | |
304 | + rx8025_rtc_reset(&dev); | |
305 | +} | |
306 | +#endif |