1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * RTC driver for the Micro Crystal RV8803 |
4 | * |
5 | * Copyright (C) 2015 Micro Crystal SA |
6 | * Alexandre Belloni <alexandre.belloni@bootlin.com> |
7 | * |
8 | */ |
9 | |
10 | #include <linux/bcd.h> |
11 | #include <linux/bitops.h> |
12 | #include <linux/bitfield.h> |
13 | #include <linux/log2.h> |
14 | #include <linux/i2c.h> |
15 | #include <linux/interrupt.h> |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> |
18 | #include <linux/of.h> |
19 | #include <linux/rtc.h> |
20 | |
21 | #define RV8803_I2C_TRY_COUNT 4 |
22 | |
23 | #define RV8803_SEC 0x00 |
24 | #define RV8803_MIN 0x01 |
25 | #define RV8803_HOUR 0x02 |
26 | #define RV8803_WEEK 0x03 |
27 | #define RV8803_DAY 0x04 |
28 | #define RV8803_MONTH 0x05 |
29 | #define RV8803_YEAR 0x06 |
30 | #define RV8803_RAM 0x07 |
31 | #define RV8803_ALARM_MIN 0x08 |
32 | #define RV8803_ALARM_HOUR 0x09 |
33 | #define RV8803_ALARM_WEEK_OR_DAY 0x0A |
34 | #define RV8803_EXT 0x0D |
35 | #define RV8803_FLAG 0x0E |
36 | #define RV8803_CTRL 0x0F |
37 | #define RV8803_OSC_OFFSET 0x2C |
38 | |
39 | #define RV8803_EXT_WADA BIT(6) |
40 | |
41 | #define RV8803_FLAG_V1F BIT(0) |
42 | #define RV8803_FLAG_V2F BIT(1) |
43 | #define RV8803_FLAG_AF BIT(3) |
44 | #define RV8803_FLAG_TF BIT(4) |
45 | #define RV8803_FLAG_UF BIT(5) |
46 | |
47 | #define RV8803_CTRL_RESET BIT(0) |
48 | |
49 | #define RV8803_CTRL_EIE BIT(2) |
50 | #define RV8803_CTRL_AIE BIT(3) |
51 | #define RV8803_CTRL_TIE BIT(4) |
52 | #define RV8803_CTRL_UIE BIT(5) |
53 | |
54 | #define RX8803_CTRL_CSEL GENMASK(7, 6) |
55 | |
56 | #define RX8900_BACKUP_CTRL 0x18 |
57 | #define RX8900_FLAG_SWOFF BIT(2) |
58 | #define RX8900_FLAG_VDETOFF BIT(3) |
59 | |
60 | enum rv8803_type { |
61 | rv_8803, |
62 | rx_8803, |
63 | rx_8804, |
64 | rx_8900 |
65 | }; |
66 | |
67 | struct rv8803_data { |
68 | struct i2c_client *client; |
69 | struct rtc_device *rtc; |
70 | struct mutex flags_lock; |
71 | u8 ctrl; |
72 | u8 backup; |
73 | u8 alarm_invalid:1; |
74 | enum rv8803_type type; |
75 | }; |
76 | |
77 | static int rv8803_read_reg(const struct i2c_client *client, u8 reg) |
78 | { |
79 | int try = RV8803_I2C_TRY_COUNT; |
80 | s32 ret; |
81 | |
82 | /* |
83 | * There is a 61µs window during which the RTC does not acknowledge I2C |
84 | * transfers. In that case, ensure that there are multiple attempts. |
85 | */ |
86 | do |
87 | ret = i2c_smbus_read_byte_data(client, command: reg); |
88 | while ((ret == -ENXIO || ret == -EIO) && --try); |
89 | if (ret < 0) |
90 | dev_err(&client->dev, "Unable to read register 0x%02x\n" , reg); |
91 | |
92 | return ret; |
93 | } |
94 | |
95 | static int rv8803_read_regs(const struct i2c_client *client, |
96 | u8 reg, u8 count, u8 *values) |
97 | { |
98 | int try = RV8803_I2C_TRY_COUNT; |
99 | s32 ret; |
100 | |
101 | do |
102 | ret = i2c_smbus_read_i2c_block_data(client, command: reg, length: count, values); |
103 | while ((ret == -ENXIO || ret == -EIO) && --try); |
104 | if (ret != count) { |
105 | dev_err(&client->dev, |
106 | "Unable to read registers 0x%02x..0x%02x\n" , |
107 | reg, reg + count - 1); |
108 | return ret < 0 ? ret : -EIO; |
109 | } |
110 | |
111 | return 0; |
112 | } |
113 | |
114 | static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) |
115 | { |
116 | int try = RV8803_I2C_TRY_COUNT; |
117 | s32 ret; |
118 | |
119 | do |
120 | ret = i2c_smbus_write_byte_data(client, command: reg, value); |
121 | while ((ret == -ENXIO || ret == -EIO) && --try); |
122 | if (ret) |
123 | dev_err(&client->dev, "Unable to write register 0x%02x\n" , reg); |
124 | |
125 | return ret; |
126 | } |
127 | |
128 | static int rv8803_write_regs(const struct i2c_client *client, |
129 | u8 reg, u8 count, const u8 *values) |
130 | { |
131 | int try = RV8803_I2C_TRY_COUNT; |
132 | s32 ret; |
133 | |
134 | do |
135 | ret = i2c_smbus_write_i2c_block_data(client, command: reg, length: count, |
136 | values); |
137 | while ((ret == -ENXIO || ret == -EIO) && --try); |
138 | if (ret) |
139 | dev_err(&client->dev, |
140 | "Unable to write registers 0x%02x..0x%02x\n" , |
141 | reg, reg + count - 1); |
142 | |
143 | return ret; |
144 | } |
145 | |
146 | static int rv8803_regs_init(struct rv8803_data *rv8803) |
147 | { |
148 | int ret; |
149 | |
150 | ret = rv8803_write_reg(client: rv8803->client, RV8803_OSC_OFFSET, value: 0x00); |
151 | if (ret) |
152 | return ret; |
153 | |
154 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
155 | FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */ |
156 | if (ret) |
157 | return ret; |
158 | |
159 | ret = rv8803_write_regs(client: rv8803->client, RV8803_ALARM_MIN, count: 3, |
160 | values: (u8[]){ 0, 0, 0 }); |
161 | if (ret) |
162 | return ret; |
163 | |
164 | return rv8803_write_reg(client: rv8803->client, RV8803_RAM, value: 0x00); |
165 | } |
166 | |
167 | static int rv8803_regs_configure(struct rv8803_data *rv8803); |
168 | |
169 | static int rv8803_regs_reset(struct rv8803_data *rv8803, bool full) |
170 | { |
171 | /* |
172 | * The RV-8803 resets all registers to POR defaults after voltage-loss, |
173 | * the Epson RTCs don't, so we manually reset the remainder here. |
174 | */ |
175 | if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900) { |
176 | int ret = rv8803_regs_init(rv8803); |
177 | if (ret) |
178 | return ret; |
179 | } |
180 | |
181 | return rv8803_regs_configure(rv8803); |
182 | } |
183 | |
184 | static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) |
185 | { |
186 | struct i2c_client *client = dev_id; |
187 | struct rv8803_data *rv8803 = i2c_get_clientdata(client); |
188 | unsigned long events = 0; |
189 | int flags; |
190 | |
191 | mutex_lock(&rv8803->flags_lock); |
192 | |
193 | flags = rv8803_read_reg(client, RV8803_FLAG); |
194 | if (flags <= 0) { |
195 | mutex_unlock(lock: &rv8803->flags_lock); |
196 | return IRQ_NONE; |
197 | } |
198 | |
199 | if (flags & RV8803_FLAG_V1F) |
200 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
201 | |
202 | if (flags & RV8803_FLAG_V2F) |
203 | dev_warn(&client->dev, "Voltage low, data loss detected.\n" ); |
204 | |
205 | if (flags & RV8803_FLAG_TF) { |
206 | flags &= ~RV8803_FLAG_TF; |
207 | rv8803->ctrl &= ~RV8803_CTRL_TIE; |
208 | events |= RTC_PF; |
209 | } |
210 | |
211 | if (flags & RV8803_FLAG_AF) { |
212 | flags &= ~RV8803_FLAG_AF; |
213 | rv8803->ctrl &= ~RV8803_CTRL_AIE; |
214 | events |= RTC_AF; |
215 | } |
216 | |
217 | if (flags & RV8803_FLAG_UF) { |
218 | flags &= ~RV8803_FLAG_UF; |
219 | rv8803->ctrl &= ~RV8803_CTRL_UIE; |
220 | events |= RTC_UF; |
221 | } |
222 | |
223 | if (events) { |
224 | rtc_update_irq(rtc: rv8803->rtc, num: 1, events); |
225 | rv8803_write_reg(client, RV8803_FLAG, value: flags); |
226 | rv8803_write_reg(client: rv8803->client, RV8803_CTRL, value: rv8803->ctrl); |
227 | } |
228 | |
229 | mutex_unlock(lock: &rv8803->flags_lock); |
230 | |
231 | return IRQ_HANDLED; |
232 | } |
233 | |
234 | static int rv8803_get_time(struct device *dev, struct rtc_time *tm) |
235 | { |
236 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
237 | u8 date1[7]; |
238 | u8 date2[7]; |
239 | u8 *date = date1; |
240 | int ret, flags; |
241 | |
242 | if (rv8803->alarm_invalid) { |
243 | dev_warn(dev, "Corruption detected, data may be invalid.\n" ); |
244 | return -EINVAL; |
245 | } |
246 | |
247 | flags = rv8803_read_reg(client: rv8803->client, RV8803_FLAG); |
248 | if (flags < 0) |
249 | return flags; |
250 | |
251 | if (flags & RV8803_FLAG_V2F) { |
252 | dev_warn(dev, "Voltage low, data is invalid.\n" ); |
253 | return -EINVAL; |
254 | } |
255 | |
256 | ret = rv8803_read_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date); |
257 | if (ret) |
258 | return ret; |
259 | |
260 | if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { |
261 | ret = rv8803_read_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date2); |
262 | if (ret) |
263 | return ret; |
264 | |
265 | if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) |
266 | date = date2; |
267 | } |
268 | |
269 | tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); |
270 | tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); |
271 | tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); |
272 | tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); |
273 | tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); |
274 | tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; |
275 | tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; |
276 | |
277 | return 0; |
278 | } |
279 | |
280 | static int rv8803_set_time(struct device *dev, struct rtc_time *tm) |
281 | { |
282 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
283 | u8 date[7]; |
284 | int ctrl, flags, ret; |
285 | |
286 | ctrl = rv8803_read_reg(client: rv8803->client, RV8803_CTRL); |
287 | if (ctrl < 0) |
288 | return ctrl; |
289 | |
290 | /* Stop the clock */ |
291 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
292 | value: ctrl | RV8803_CTRL_RESET); |
293 | if (ret) |
294 | return ret; |
295 | |
296 | date[RV8803_SEC] = bin2bcd(tm->tm_sec); |
297 | date[RV8803_MIN] = bin2bcd(tm->tm_min); |
298 | date[RV8803_HOUR] = bin2bcd(tm->tm_hour); |
299 | date[RV8803_WEEK] = 1 << (tm->tm_wday); |
300 | date[RV8803_DAY] = bin2bcd(tm->tm_mday); |
301 | date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); |
302 | date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); |
303 | |
304 | ret = rv8803_write_regs(client: rv8803->client, RV8803_SEC, count: 7, values: date); |
305 | if (ret) |
306 | return ret; |
307 | |
308 | /* Restart the clock */ |
309 | ret = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
310 | value: ctrl & ~RV8803_CTRL_RESET); |
311 | if (ret) |
312 | return ret; |
313 | |
314 | mutex_lock(&rv8803->flags_lock); |
315 | |
316 | flags = rv8803_read_reg(client: rv8803->client, RV8803_FLAG); |
317 | if (flags < 0) { |
318 | mutex_unlock(lock: &rv8803->flags_lock); |
319 | return flags; |
320 | } |
321 | |
322 | if ((flags & RV8803_FLAG_V2F) || rv8803->alarm_invalid) { |
323 | /* |
324 | * If we sense corruption in the alarm registers, but see no |
325 | * voltage loss flag, we can't rely on other registers having |
326 | * sensible values. Reset them fully. |
327 | */ |
328 | ret = rv8803_regs_reset(rv8803, full: rv8803->alarm_invalid); |
329 | if (ret) { |
330 | mutex_unlock(lock: &rv8803->flags_lock); |
331 | return ret; |
332 | } |
333 | |
334 | rv8803->alarm_invalid = false; |
335 | } |
336 | |
337 | ret = rv8803_write_reg(client: rv8803->client, RV8803_FLAG, |
338 | value: flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); |
339 | |
340 | mutex_unlock(lock: &rv8803->flags_lock); |
341 | |
342 | return ret; |
343 | } |
344 | |
345 | static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
346 | { |
347 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
348 | struct i2c_client *client = rv8803->client; |
349 | u8 alarmvals[3]; |
350 | int flags, ret; |
351 | |
352 | ret = rv8803_read_regs(client, RV8803_ALARM_MIN, count: 3, values: alarmvals); |
353 | if (ret) |
354 | return ret; |
355 | |
356 | flags = rv8803_read_reg(client, RV8803_FLAG); |
357 | if (flags < 0) |
358 | return flags; |
359 | |
360 | alarmvals[0] &= 0x7f; |
361 | alarmvals[1] &= 0x3f; |
362 | alarmvals[2] &= 0x3f; |
363 | |
364 | if (!bcd_is_valid(alarmvals[0]) || |
365 | !bcd_is_valid(alarmvals[1]) || |
366 | !bcd_is_valid(alarmvals[2])) |
367 | goto err_invalid; |
368 | |
369 | alrm->time.tm_sec = 0; |
370 | alrm->time.tm_min = bcd2bin(alarmvals[0]); |
371 | alrm->time.tm_hour = bcd2bin(alarmvals[1]); |
372 | alrm->time.tm_mday = bcd2bin(alarmvals[2]); |
373 | |
374 | alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); |
375 | alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; |
376 | |
377 | if ((unsigned int)alrm->time.tm_mday > 31 || |
378 | (unsigned int)alrm->time.tm_hour >= 24 || |
379 | (unsigned int)alrm->time.tm_min >= 60) |
380 | goto err_invalid; |
381 | |
382 | return 0; |
383 | |
384 | err_invalid: |
385 | rv8803->alarm_invalid = true; |
386 | return -EINVAL; |
387 | } |
388 | |
389 | static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
390 | { |
391 | struct i2c_client *client = to_i2c_client(dev); |
392 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
393 | u8 alarmvals[3]; |
394 | u8 ctrl[2]; |
395 | int ret, err; |
396 | |
397 | /* The alarm has no seconds, round up to nearest minute */ |
398 | if (alrm->time.tm_sec) { |
399 | time64_t alarm_time = rtc_tm_to_time64(tm: &alrm->time); |
400 | |
401 | alarm_time += 60 - alrm->time.tm_sec; |
402 | rtc_time64_to_tm(time: alarm_time, tm: &alrm->time); |
403 | } |
404 | |
405 | mutex_lock(&rv8803->flags_lock); |
406 | |
407 | ret = rv8803_read_regs(client, RV8803_FLAG, count: 2, values: ctrl); |
408 | if (ret) { |
409 | mutex_unlock(lock: &rv8803->flags_lock); |
410 | return ret; |
411 | } |
412 | |
413 | alarmvals[0] = bin2bcd(alrm->time.tm_min); |
414 | alarmvals[1] = bin2bcd(alrm->time.tm_hour); |
415 | alarmvals[2] = bin2bcd(alrm->time.tm_mday); |
416 | |
417 | if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { |
418 | rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); |
419 | err = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
420 | value: rv8803->ctrl); |
421 | if (err) { |
422 | mutex_unlock(lock: &rv8803->flags_lock); |
423 | return err; |
424 | } |
425 | } |
426 | |
427 | ctrl[0] &= ~RV8803_FLAG_AF; |
428 | err = rv8803_write_reg(client: rv8803->client, RV8803_FLAG, value: ctrl[0]); |
429 | mutex_unlock(lock: &rv8803->flags_lock); |
430 | if (err) |
431 | return err; |
432 | |
433 | err = rv8803_write_regs(client: rv8803->client, RV8803_ALARM_MIN, count: 3, values: alarmvals); |
434 | if (err) |
435 | return err; |
436 | |
437 | if (alrm->enabled) { |
438 | if (rv8803->rtc->uie_rtctimer.enabled) |
439 | rv8803->ctrl |= RV8803_CTRL_UIE; |
440 | if (rv8803->rtc->aie_timer.enabled) |
441 | rv8803->ctrl |= RV8803_CTRL_AIE; |
442 | |
443 | err = rv8803_write_reg(client: rv8803->client, RV8803_CTRL, |
444 | value: rv8803->ctrl); |
445 | if (err) |
446 | return err; |
447 | } |
448 | |
449 | return 0; |
450 | } |
451 | |
452 | static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) |
453 | { |
454 | struct i2c_client *client = to_i2c_client(dev); |
455 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
456 | int ctrl, flags, err; |
457 | |
458 | ctrl = rv8803->ctrl; |
459 | |
460 | if (enabled) { |
461 | if (rv8803->rtc->uie_rtctimer.enabled) |
462 | ctrl |= RV8803_CTRL_UIE; |
463 | if (rv8803->rtc->aie_timer.enabled) |
464 | ctrl |= RV8803_CTRL_AIE; |
465 | } else { |
466 | if (!rv8803->rtc->uie_rtctimer.enabled) |
467 | ctrl &= ~RV8803_CTRL_UIE; |
468 | if (!rv8803->rtc->aie_timer.enabled) |
469 | ctrl &= ~RV8803_CTRL_AIE; |
470 | } |
471 | |
472 | mutex_lock(&rv8803->flags_lock); |
473 | flags = rv8803_read_reg(client, RV8803_FLAG); |
474 | if (flags < 0) { |
475 | mutex_unlock(lock: &rv8803->flags_lock); |
476 | return flags; |
477 | } |
478 | flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); |
479 | err = rv8803_write_reg(client, RV8803_FLAG, value: flags); |
480 | mutex_unlock(lock: &rv8803->flags_lock); |
481 | if (err) |
482 | return err; |
483 | |
484 | if (ctrl != rv8803->ctrl) { |
485 | rv8803->ctrl = ctrl; |
486 | err = rv8803_write_reg(client, RV8803_CTRL, value: rv8803->ctrl); |
487 | if (err) |
488 | return err; |
489 | } |
490 | |
491 | return 0; |
492 | } |
493 | |
494 | static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
495 | { |
496 | struct i2c_client *client = to_i2c_client(dev); |
497 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
498 | unsigned int vl = 0; |
499 | int flags, ret = 0; |
500 | |
501 | switch (cmd) { |
502 | case RTC_VL_READ: |
503 | flags = rv8803_read_reg(client, RV8803_FLAG); |
504 | if (flags < 0) |
505 | return flags; |
506 | |
507 | if (flags & RV8803_FLAG_V1F) { |
508 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
509 | vl = RTC_VL_ACCURACY_LOW; |
510 | } |
511 | |
512 | if (flags & RV8803_FLAG_V2F) |
513 | vl |= RTC_VL_DATA_INVALID; |
514 | |
515 | return put_user(vl, (unsigned int __user *)arg); |
516 | |
517 | case RTC_VL_CLR: |
518 | mutex_lock(&rv8803->flags_lock); |
519 | flags = rv8803_read_reg(client, RV8803_FLAG); |
520 | if (flags < 0) { |
521 | mutex_unlock(lock: &rv8803->flags_lock); |
522 | return flags; |
523 | } |
524 | |
525 | flags &= ~RV8803_FLAG_V1F; |
526 | ret = rv8803_write_reg(client, RV8803_FLAG, value: flags); |
527 | mutex_unlock(lock: &rv8803->flags_lock); |
528 | if (ret) |
529 | return ret; |
530 | |
531 | return 0; |
532 | |
533 | default: |
534 | return -ENOIOCTLCMD; |
535 | } |
536 | } |
537 | |
538 | static int rv8803_nvram_write(void *priv, unsigned int offset, void *val, |
539 | size_t bytes) |
540 | { |
541 | return rv8803_write_reg(client: priv, RV8803_RAM, value: *(u8 *)val); |
542 | } |
543 | |
544 | static int rv8803_nvram_read(void *priv, unsigned int offset, |
545 | void *val, size_t bytes) |
546 | { |
547 | int ret; |
548 | |
549 | ret = rv8803_read_reg(client: priv, RV8803_RAM); |
550 | if (ret < 0) |
551 | return ret; |
552 | |
553 | *(u8 *)val = ret; |
554 | |
555 | return 0; |
556 | } |
557 | |
558 | static const struct rtc_class_ops rv8803_rtc_ops = { |
559 | .read_time = rv8803_get_time, |
560 | .set_time = rv8803_set_time, |
561 | .ioctl = rv8803_ioctl, |
562 | .read_alarm = rv8803_get_alarm, |
563 | .set_alarm = rv8803_set_alarm, |
564 | .alarm_irq_enable = rv8803_alarm_irq_enable, |
565 | }; |
566 | |
567 | static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) |
568 | { |
569 | struct i2c_client *client = rv8803->client; |
570 | struct device_node *node = client->dev.of_node; |
571 | int err; |
572 | u8 flags; |
573 | |
574 | if (!node) |
575 | return 0; |
576 | |
577 | if (rv8803->type != rx_8900) |
578 | return 0; |
579 | |
580 | err = i2c_smbus_read_byte_data(client: rv8803->client, RX8900_BACKUP_CTRL); |
581 | if (err < 0) |
582 | return err; |
583 | |
584 | flags = (u8)err; |
585 | flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF); |
586 | flags |= rv8803->backup; |
587 | |
588 | return i2c_smbus_write_byte_data(client: rv8803->client, RX8900_BACKUP_CTRL, |
589 | value: flags); |
590 | } |
591 | |
592 | /* configure registers with values different than the Power-On reset defaults */ |
593 | static int rv8803_regs_configure(struct rv8803_data *rv8803) |
594 | { |
595 | int err; |
596 | |
597 | err = rv8803_write_reg(client: rv8803->client, RV8803_EXT, RV8803_EXT_WADA); |
598 | if (err) |
599 | return err; |
600 | |
601 | err = rx8900_trickle_charger_init(rv8803); |
602 | if (err) { |
603 | dev_err(&rv8803->client->dev, "failed to init charger\n" ); |
604 | return err; |
605 | } |
606 | |
607 | return 0; |
608 | } |
609 | |
610 | static const struct i2c_device_id rv8803_id[] = { |
611 | { "rv8803" , rv_8803 }, |
612 | { "rv8804" , rx_8804 }, |
613 | { "rx8803" , rx_8803 }, |
614 | { "rx8900" , rx_8900 }, |
615 | { } |
616 | }; |
617 | MODULE_DEVICE_TABLE(i2c, rv8803_id); |
618 | |
619 | static int rv8803_probe(struct i2c_client *client) |
620 | { |
621 | struct i2c_adapter *adapter = client->adapter; |
622 | struct rv8803_data *rv8803; |
623 | int err, flags; |
624 | struct nvmem_config nvmem_cfg = { |
625 | .name = "rv8803_nvram" , |
626 | .word_size = 1, |
627 | .stride = 1, |
628 | .size = 1, |
629 | .reg_read = rv8803_nvram_read, |
630 | .reg_write = rv8803_nvram_write, |
631 | .priv = client, |
632 | }; |
633 | |
634 | if (!i2c_check_functionality(adap: adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
635 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
636 | dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n" ); |
637 | return -EIO; |
638 | } |
639 | |
640 | rv8803 = devm_kzalloc(dev: &client->dev, size: sizeof(struct rv8803_data), |
641 | GFP_KERNEL); |
642 | if (!rv8803) |
643 | return -ENOMEM; |
644 | |
645 | mutex_init(&rv8803->flags_lock); |
646 | rv8803->client = client; |
647 | if (client->dev.of_node) { |
648 | rv8803->type = (uintptr_t)of_device_get_match_data(dev: &client->dev); |
649 | } else { |
650 | const struct i2c_device_id *id = i2c_match_id(id: rv8803_id, client); |
651 | |
652 | rv8803->type = id->driver_data; |
653 | } |
654 | i2c_set_clientdata(client, data: rv8803); |
655 | |
656 | flags = rv8803_read_reg(client, RV8803_FLAG); |
657 | if (flags < 0) |
658 | return flags; |
659 | |
660 | if (flags & RV8803_FLAG_V1F) |
661 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n" ); |
662 | |
663 | if (flags & RV8803_FLAG_V2F) |
664 | dev_warn(&client->dev, "Voltage low, data loss detected.\n" ); |
665 | |
666 | if (flags & RV8803_FLAG_AF) |
667 | dev_warn(&client->dev, "An alarm maybe have been missed.\n" ); |
668 | |
669 | rv8803->rtc = devm_rtc_allocate_device(dev: &client->dev); |
670 | if (IS_ERR(ptr: rv8803->rtc)) |
671 | return PTR_ERR(ptr: rv8803->rtc); |
672 | |
673 | if (client->irq > 0) { |
674 | unsigned long irqflags = IRQF_TRIGGER_LOW; |
675 | |
676 | if (dev_fwnode(&client->dev)) |
677 | irqflags = 0; |
678 | |
679 | err = devm_request_threaded_irq(dev: &client->dev, irq: client->irq, |
680 | NULL, thread_fn: rv8803_handle_irq, |
681 | irqflags: irqflags | IRQF_ONESHOT, |
682 | devname: "rv8803" , dev_id: client); |
683 | if (err) { |
684 | dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n" ); |
685 | client->irq = 0; |
686 | } |
687 | } |
688 | if (!client->irq) |
689 | clear_bit(RTC_FEATURE_ALARM, addr: rv8803->rtc->features); |
690 | |
691 | if (of_property_read_bool(np: client->dev.of_node, propname: "epson,vdet-disable" )) |
692 | rv8803->backup |= RX8900_FLAG_VDETOFF; |
693 | |
694 | if (of_property_read_bool(np: client->dev.of_node, propname: "trickle-diode-disable" )) |
695 | rv8803->backup |= RX8900_FLAG_SWOFF; |
696 | |
697 | err = rv8803_regs_configure(rv8803); |
698 | if (err) |
699 | return err; |
700 | |
701 | rv8803->rtc->ops = &rv8803_rtc_ops; |
702 | rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
703 | rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099; |
704 | err = devm_rtc_register_device(rv8803->rtc); |
705 | if (err) |
706 | return err; |
707 | |
708 | devm_rtc_nvmem_register(rtc: rv8803->rtc, nvmem_config: &nvmem_cfg); |
709 | |
710 | rv8803->rtc->max_user_freq = 1; |
711 | |
712 | return 0; |
713 | } |
714 | |
715 | static const __maybe_unused struct of_device_id rv8803_of_match[] = { |
716 | { |
717 | .compatible = "microcrystal,rv8803" , |
718 | .data = (void *)rv_8803 |
719 | }, |
720 | { |
721 | .compatible = "epson,rx8803" , |
722 | .data = (void *)rx_8803 |
723 | }, |
724 | { |
725 | .compatible = "epson,rx8804" , |
726 | .data = (void *)rx_8804 |
727 | }, |
728 | { |
729 | .compatible = "epson,rx8900" , |
730 | .data = (void *)rx_8900 |
731 | }, |
732 | { } |
733 | }; |
734 | MODULE_DEVICE_TABLE(of, rv8803_of_match); |
735 | |
736 | static struct i2c_driver rv8803_driver = { |
737 | .driver = { |
738 | .name = "rtc-rv8803" , |
739 | .of_match_table = of_match_ptr(rv8803_of_match), |
740 | }, |
741 | .probe = rv8803_probe, |
742 | .id_table = rv8803_id, |
743 | }; |
744 | module_i2c_driver(rv8803_driver); |
745 | |
746 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>" ); |
747 | MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver" ); |
748 | MODULE_LICENSE("GPL v2" ); |
749 | |