1 // SPDX-License-Identifier: GPL-2.0
3 * RTC driver for the Micro Crystal RV8803
5 * Copyright (C) 2015 Micro Crystal SA
6 * Alexandre Belloni <alexandre.belloni@bootlin.com>
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>
19 #include <linux/rtc.h>
20 #include <linux/pm_wakeirq.h>
22 #define RV8803_I2C_TRY_COUNT 4
24 #define RV8803_SEC 0x00
25 #define RV8803_MIN 0x01
26 #define RV8803_HOUR 0x02
27 #define RV8803_WEEK 0x03
28 #define RV8803_DAY 0x04
29 #define RV8803_MONTH 0x05
30 #define RV8803_YEAR 0x06
31 #define RV8803_RAM 0x07
32 #define RV8803_ALARM_MIN 0x08
33 #define RV8803_ALARM_HOUR 0x09
34 #define RV8803_ALARM_WEEK_OR_DAY 0x0A
35 #define RV8803_EXT 0x0D
36 #define RV8803_FLAG 0x0E
37 #define RV8803_CTRL 0x0F
38 #define RV8803_OSC_OFFSET 0x2C
40 #define RV8803_EXT_WADA BIT(6)
42 #define RV8803_FLAG_V1F BIT(0)
43 #define RV8803_FLAG_V2F BIT(1)
44 #define RV8803_FLAG_AF BIT(3)
45 #define RV8803_FLAG_TF BIT(4)
46 #define RV8803_FLAG_UF BIT(5)
48 #define RV8803_CTRL_RESET BIT(0)
50 #define RV8803_CTRL_EIE BIT(2)
51 #define RV8803_CTRL_AIE BIT(3)
52 #define RV8803_CTRL_TIE BIT(4)
53 #define RV8803_CTRL_UIE BIT(5)
55 #define RX8803_CTRL_CSEL GENMASK(7, 6)
57 #define RX8900_BACKUP_CTRL 0x18
58 #define RX8900_FLAG_SWOFF BIT(2)
59 #define RX8900_FLAG_VDETOFF BIT(3)
69 struct i2c_client *client;
70 struct rtc_device *rtc;
71 struct mutex flags_lock;
75 enum rv8803_type type;
78 static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
80 int try = RV8803_I2C_TRY_COUNT;
84 * There is a 61µs window during which the RTC does not acknowledge I2C
85 * transfers. In that case, ensure that there are multiple attempts.
88 ret = i2c_smbus_read_byte_data(client, reg);
89 while ((ret == -ENXIO || ret == -EIO) && --try);
91 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
96 static int rv8803_read_regs(const struct i2c_client *client,
97 u8 reg, u8 count, u8 *values)
99 int try = RV8803_I2C_TRY_COUNT;
103 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
104 while ((ret == -ENXIO || ret == -EIO) && --try);
106 dev_err(&client->dev,
107 "Unable to read registers 0x%02x..0x%02x\n",
108 reg, reg + count - 1);
109 return ret < 0 ? ret : -EIO;
115 static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
117 int try = RV8803_I2C_TRY_COUNT;
121 ret = i2c_smbus_write_byte_data(client, reg, value);
122 while ((ret == -ENXIO || ret == -EIO) && --try);
124 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
129 static int rv8803_write_regs(const struct i2c_client *client,
130 u8 reg, u8 count, const u8 *values)
132 int try = RV8803_I2C_TRY_COUNT;
136 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
138 while ((ret == -ENXIO || ret == -EIO) && --try);
140 dev_err(&client->dev,
141 "Unable to write registers 0x%02x..0x%02x\n",
142 reg, reg + count - 1);
147 static int rv8803_regs_init(struct rv8803_data *rv8803)
151 ret = rv8803_write_reg(rv8803->client, RV8803_OSC_OFFSET, 0x00);
155 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
156 FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */
160 ret = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3,
165 return rv8803_write_reg(rv8803->client, RV8803_RAM, 0x00);
168 static int rv8803_regs_configure(struct rv8803_data *rv8803);
170 static int rv8803_regs_reset(struct rv8803_data *rv8803, bool full)
173 * The RV-8803 resets all registers to POR defaults after voltage-loss,
174 * the Epson RTCs don't, so we manually reset the remainder here.
176 if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900) {
177 int ret = rv8803_regs_init(rv8803);
182 return rv8803_regs_configure(rv8803);
185 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
187 struct i2c_client *client = dev_id;
188 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
189 unsigned long events = 0;
192 mutex_lock(&rv8803->flags_lock);
194 flags = rv8803_read_reg(client, RV8803_FLAG);
196 mutex_unlock(&rv8803->flags_lock);
200 if (flags & RV8803_FLAG_V1F)
201 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
203 if (flags & RV8803_FLAG_V2F)
204 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
206 if (flags & RV8803_FLAG_TF) {
207 flags &= ~RV8803_FLAG_TF;
208 rv8803->ctrl &= ~RV8803_CTRL_TIE;
212 if (flags & RV8803_FLAG_AF) {
213 flags &= ~RV8803_FLAG_AF;
214 rv8803->ctrl &= ~RV8803_CTRL_AIE;
218 if (flags & RV8803_FLAG_UF) {
219 flags &= ~RV8803_FLAG_UF;
220 rv8803->ctrl &= ~RV8803_CTRL_UIE;
225 rtc_update_irq(rv8803->rtc, 1, events);
226 rv8803_write_reg(client, RV8803_FLAG, flags);
227 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
230 mutex_unlock(&rv8803->flags_lock);
235 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
237 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
243 if (rv8803->alarm_invalid) {
244 dev_warn(dev, "Corruption detected, data may be invalid.\n");
248 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
252 if (flags & RV8803_FLAG_V2F) {
253 dev_warn(dev, "Voltage low, data is invalid.\n");
257 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
261 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
262 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
266 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
270 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
271 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
272 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
273 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
274 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
275 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
276 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
281 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
283 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
285 int ctrl, flags, ret;
287 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
292 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
293 ctrl | RV8803_CTRL_RESET);
297 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
298 date[RV8803_MIN] = bin2bcd(tm->tm_min);
299 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
300 date[RV8803_WEEK] = 1 << (tm->tm_wday);
301 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
302 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
303 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
305 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
309 /* Restart the clock */
310 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
311 ctrl & ~RV8803_CTRL_RESET);
315 mutex_lock(&rv8803->flags_lock);
317 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
319 mutex_unlock(&rv8803->flags_lock);
323 if ((flags & RV8803_FLAG_V2F) || rv8803->alarm_invalid) {
325 * If we sense corruption in the alarm registers, but see no
326 * voltage loss flag, we can't rely on other registers having
327 * sensible values. Reset them fully.
329 ret = rv8803_regs_reset(rv8803, rv8803->alarm_invalid);
331 mutex_unlock(&rv8803->flags_lock);
335 rv8803->alarm_invalid = false;
338 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
339 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
341 mutex_unlock(&rv8803->flags_lock);
346 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
348 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
349 struct i2c_client *client = rv8803->client;
353 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
357 flags = rv8803_read_reg(client, RV8803_FLAG);
361 alarmvals[0] &= 0x7f;
362 alarmvals[1] &= 0x3f;
363 alarmvals[2] &= 0x3f;
365 if (!bcd_is_valid(alarmvals[0]) ||
366 !bcd_is_valid(alarmvals[1]) ||
367 !bcd_is_valid(alarmvals[2]))
370 alrm->time.tm_sec = 0;
371 alrm->time.tm_min = bcd2bin(alarmvals[0]);
372 alrm->time.tm_hour = bcd2bin(alarmvals[1]);
373 alrm->time.tm_mday = bcd2bin(alarmvals[2]);
375 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
376 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
378 if ((unsigned int)alrm->time.tm_mday > 31 ||
379 (unsigned int)alrm->time.tm_hour >= 24 ||
380 (unsigned int)alrm->time.tm_min >= 60)
386 rv8803->alarm_invalid = true;
390 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
392 struct i2c_client *client = to_i2c_client(dev);
393 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
398 /* The alarm has no seconds, round up to nearest minute */
399 if (alrm->time.tm_sec) {
400 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
402 alarm_time += 60 - alrm->time.tm_sec;
403 rtc_time64_to_tm(alarm_time, &alrm->time);
406 mutex_lock(&rv8803->flags_lock);
408 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
410 mutex_unlock(&rv8803->flags_lock);
414 alarmvals[0] = bin2bcd(alrm->time.tm_min);
415 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
416 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
418 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
419 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
420 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
423 mutex_unlock(&rv8803->flags_lock);
428 ctrl[0] &= ~RV8803_FLAG_AF;
429 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]);
430 mutex_unlock(&rv8803->flags_lock);
434 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
439 if (rv8803->rtc->uie_rtctimer.enabled)
440 rv8803->ctrl |= RV8803_CTRL_UIE;
441 if (rv8803->rtc->aie_timer.enabled)
442 rv8803->ctrl |= RV8803_CTRL_AIE;
444 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
453 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
455 struct i2c_client *client = to_i2c_client(dev);
456 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
457 int ctrl, flags, err;
462 if (rv8803->rtc->uie_rtctimer.enabled)
463 ctrl |= RV8803_CTRL_UIE;
464 if (rv8803->rtc->aie_timer.enabled)
465 ctrl |= RV8803_CTRL_AIE;
467 if (!rv8803->rtc->uie_rtctimer.enabled)
468 ctrl &= ~RV8803_CTRL_UIE;
469 if (!rv8803->rtc->aie_timer.enabled)
470 ctrl &= ~RV8803_CTRL_AIE;
473 mutex_lock(&rv8803->flags_lock);
474 flags = rv8803_read_reg(client, RV8803_FLAG);
476 mutex_unlock(&rv8803->flags_lock);
479 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
480 err = rv8803_write_reg(client, RV8803_FLAG, flags);
481 mutex_unlock(&rv8803->flags_lock);
485 if (ctrl != rv8803->ctrl) {
487 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
495 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
497 struct i2c_client *client = to_i2c_client(dev);
498 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
504 flags = rv8803_read_reg(client, RV8803_FLAG);
508 if (flags & RV8803_FLAG_V1F) {
509 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
510 vl = RTC_VL_ACCURACY_LOW;
513 if (flags & RV8803_FLAG_V2F)
514 vl |= RTC_VL_DATA_INVALID;
516 return put_user(vl, (unsigned int __user *)arg);
519 mutex_lock(&rv8803->flags_lock);
520 flags = rv8803_read_reg(client, RV8803_FLAG);
522 mutex_unlock(&rv8803->flags_lock);
526 flags &= ~RV8803_FLAG_V1F;
527 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
528 mutex_unlock(&rv8803->flags_lock);
539 static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
542 return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
545 static int rv8803_nvram_read(void *priv, unsigned int offset,
546 void *val, size_t bytes)
550 ret = rv8803_read_reg(priv, RV8803_RAM);
559 static const struct rtc_class_ops rv8803_rtc_ops = {
560 .read_time = rv8803_get_time,
561 .set_time = rv8803_set_time,
562 .ioctl = rv8803_ioctl,
563 .read_alarm = rv8803_get_alarm,
564 .set_alarm = rv8803_set_alarm,
565 .alarm_irq_enable = rv8803_alarm_irq_enable,
568 static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
570 struct i2c_client *client = rv8803->client;
571 struct device_node *node = client->dev.of_node;
578 if (rv8803->type != rx_8900)
581 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
586 flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF);
587 flags |= rv8803->backup;
589 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
593 /* configure registers with values different than the Power-On reset defaults */
594 static int rv8803_regs_configure(struct rv8803_data *rv8803)
598 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
602 err = rx8900_trickle_charger_init(rv8803);
604 dev_err(&rv8803->client->dev, "failed to init charger\n");
611 static int rv8803_resume(struct device *dev)
613 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
615 if (rv8803->client->irq > 0 && device_may_wakeup(dev))
616 disable_irq_wake(rv8803->client->irq);
621 static int rv8803_suspend(struct device *dev)
623 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
625 if (rv8803->client->irq > 0 && device_may_wakeup(dev))
626 enable_irq_wake(rv8803->client->irq);
631 static DEFINE_SIMPLE_DEV_PM_OPS(rv8803_pm_ops, rv8803_suspend, rv8803_resume);
633 static const struct i2c_device_id rv8803_id[] = {
634 { "rv8803", rv_8803 },
635 { "rv8804", rx_8804 },
636 { "rx8803", rx_8803 },
637 { "rx8900", rx_8900 },
640 MODULE_DEVICE_TABLE(i2c, rv8803_id);
642 static int rv8803_probe(struct i2c_client *client)
644 struct i2c_adapter *adapter = client->adapter;
645 struct rv8803_data *rv8803;
647 struct nvmem_config nvmem_cfg = {
648 .name = "rv8803_nvram",
652 .reg_read = rv8803_nvram_read,
653 .reg_write = rv8803_nvram_write,
657 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
658 I2C_FUNC_SMBUS_I2C_BLOCK)) {
659 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
663 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
668 mutex_init(&rv8803->flags_lock);
669 rv8803->client = client;
670 if (client->dev.of_node) {
671 rv8803->type = (uintptr_t)of_device_get_match_data(&client->dev);
673 const struct i2c_device_id *id = i2c_match_id(rv8803_id, client);
675 rv8803->type = id->driver_data;
677 i2c_set_clientdata(client, rv8803);
679 flags = rv8803_read_reg(client, RV8803_FLAG);
683 if (flags & RV8803_FLAG_V1F)
684 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
686 if (flags & RV8803_FLAG_V2F)
687 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
689 if (flags & RV8803_FLAG_AF)
690 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
692 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
693 if (IS_ERR(rv8803->rtc))
694 return PTR_ERR(rv8803->rtc);
696 if (client->irq > 0) {
697 unsigned long irqflags = IRQF_TRIGGER_LOW;
699 if (dev_fwnode(&client->dev))
702 err = devm_request_threaded_irq(&client->dev, client->irq,
703 NULL, rv8803_handle_irq,
704 irqflags | IRQF_ONESHOT,
707 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
710 device_init_wakeup(&client->dev, true);
711 err = dev_pm_set_wake_irq(&client->dev, client->irq);
713 dev_err(&client->dev, "failed to set wake IRQ\n");
717 clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features);
719 if (of_property_read_bool(client->dev.of_node, "epson,vdet-disable"))
720 rv8803->backup |= RX8900_FLAG_VDETOFF;
722 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
723 rv8803->backup |= RX8900_FLAG_SWOFF;
725 err = rv8803_regs_configure(rv8803);
729 rv8803->rtc->ops = &rv8803_rtc_ops;
730 rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
731 rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
732 err = devm_rtc_register_device(rv8803->rtc);
736 devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
738 rv8803->rtc->max_user_freq = 1;
743 static const __maybe_unused struct of_device_id rv8803_of_match[] = {
745 .compatible = "microcrystal,rv8803",
746 .data = (void *)rv_8803
749 .compatible = "epson,rx8803",
750 .data = (void *)rx_8803
753 .compatible = "epson,rx8804",
754 .data = (void *)rx_8804
757 .compatible = "epson,rx8900",
758 .data = (void *)rx_8900
762 MODULE_DEVICE_TABLE(of, rv8803_of_match);
764 static struct i2c_driver rv8803_driver = {
766 .name = "rtc-rv8803",
767 .of_match_table = of_match_ptr(rv8803_of_match),
768 .pm = &rv8803_pm_ops,
770 .probe = rv8803_probe,
771 .id_table = rv8803_id,
773 module_i2c_driver(rv8803_driver);
775 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
776 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
777 MODULE_LICENSE("GPL v2");