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>
21 #define RV8803_I2C_TRY_COUNT 4
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
39 #define RV8803_EXT_WADA BIT(6)
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)
47 #define RV8803_CTRL_RESET BIT(0)
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)
54 #define RX8803_CTRL_CSEL GENMASK(7, 6)
56 #define RX8900_BACKUP_CTRL 0x18
57 #define RX8900_FLAG_SWOFF BIT(2)
58 #define RX8900_FLAG_VDETOFF BIT(3)
68 struct i2c_client *client;
69 struct rtc_device *rtc;
70 struct mutex flags_lock;
74 enum rv8803_type type;
77 static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
79 int try = RV8803_I2C_TRY_COUNT;
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.
87 ret = i2c_smbus_read_byte_data(client, reg);
88 while ((ret == -ENXIO || ret == -EIO) && --try);
90 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
95 static int rv8803_read_regs(const struct i2c_client *client,
96 u8 reg, u8 count, u8 *values)
98 int try = RV8803_I2C_TRY_COUNT;
102 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
103 while ((ret == -ENXIO || ret == -EIO) && --try);
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;
114 static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
116 int try = RV8803_I2C_TRY_COUNT;
120 ret = i2c_smbus_write_byte_data(client, reg, value);
121 while ((ret == -ENXIO || ret == -EIO) && --try);
123 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
128 static int rv8803_write_regs(const struct i2c_client *client,
129 u8 reg, u8 count, const u8 *values)
131 int try = RV8803_I2C_TRY_COUNT;
135 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
137 while ((ret == -ENXIO || ret == -EIO) && --try);
139 dev_err(&client->dev,
140 "Unable to write registers 0x%02x..0x%02x\n",
141 reg, reg + count - 1);
146 static int rv8803_regs_init(struct rv8803_data *rv8803)
150 ret = rv8803_write_reg(rv8803->client, RV8803_OSC_OFFSET, 0x00);
154 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
155 FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */
159 ret = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3,
164 return rv8803_write_reg(rv8803->client, RV8803_RAM, 0x00);
167 static int rv8803_regs_configure(struct rv8803_data *rv8803);
169 static int rv8803_regs_reset(struct rv8803_data *rv8803, bool full)
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.
175 if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900) {
176 int ret = rv8803_regs_init(rv8803);
181 return rv8803_regs_configure(rv8803);
184 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
186 struct i2c_client *client = dev_id;
187 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
188 unsigned long events = 0;
191 mutex_lock(&rv8803->flags_lock);
193 flags = rv8803_read_reg(client, RV8803_FLAG);
195 mutex_unlock(&rv8803->flags_lock);
199 if (flags & RV8803_FLAG_V1F)
200 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
202 if (flags & RV8803_FLAG_V2F)
203 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
205 if (flags & RV8803_FLAG_TF) {
206 flags &= ~RV8803_FLAG_TF;
207 rv8803->ctrl &= ~RV8803_CTRL_TIE;
211 if (flags & RV8803_FLAG_AF) {
212 flags &= ~RV8803_FLAG_AF;
213 rv8803->ctrl &= ~RV8803_CTRL_AIE;
217 if (flags & RV8803_FLAG_UF) {
218 flags &= ~RV8803_FLAG_UF;
219 rv8803->ctrl &= ~RV8803_CTRL_UIE;
224 rtc_update_irq(rv8803->rtc, 1, events);
225 rv8803_write_reg(client, RV8803_FLAG, flags);
226 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
229 mutex_unlock(&rv8803->flags_lock);
234 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
236 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
242 if (rv8803->alarm_invalid) {
243 dev_warn(dev, "Corruption detected, data may be invalid.\n");
247 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
251 if (flags & RV8803_FLAG_V2F) {
252 dev_warn(dev, "Voltage low, data is invalid.\n");
256 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
260 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
261 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
265 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
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;
280 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
282 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
284 int ctrl, flags, ret;
286 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
291 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
292 ctrl | RV8803_CTRL_RESET);
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);
304 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
308 /* Restart the clock */
309 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
310 ctrl & ~RV8803_CTRL_RESET);
314 mutex_lock(&rv8803->flags_lock);
316 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
318 mutex_unlock(&rv8803->flags_lock);
322 if ((flags & RV8803_FLAG_V2F) || rv8803->alarm_invalid) {
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.
328 ret = rv8803_regs_reset(rv8803, rv8803->alarm_invalid);
330 mutex_unlock(&rv8803->flags_lock);
334 rv8803->alarm_invalid = false;
337 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
338 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
340 mutex_unlock(&rv8803->flags_lock);
345 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
347 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
348 struct i2c_client *client = rv8803->client;
352 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
356 flags = rv8803_read_reg(client, RV8803_FLAG);
360 alarmvals[0] &= 0x7f;
361 alarmvals[1] &= 0x3f;
362 alarmvals[2] &= 0x3f;
364 if (!bcd_is_valid(alarmvals[0]) ||
365 !bcd_is_valid(alarmvals[1]) ||
366 !bcd_is_valid(alarmvals[2]))
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]);
374 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
375 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
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)
385 rv8803->alarm_invalid = true;
389 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
391 struct i2c_client *client = to_i2c_client(dev);
392 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
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(&alrm->time);
401 alarm_time += 60 - alrm->time.tm_sec;
402 rtc_time64_to_tm(alarm_time, &alrm->time);
405 mutex_lock(&rv8803->flags_lock);
407 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
409 mutex_unlock(&rv8803->flags_lock);
413 alarmvals[0] = bin2bcd(alrm->time.tm_min);
414 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
415 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
417 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
418 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
419 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
422 mutex_unlock(&rv8803->flags_lock);
427 ctrl[0] &= ~RV8803_FLAG_AF;
428 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]);
429 mutex_unlock(&rv8803->flags_lock);
433 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
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;
443 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
452 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
454 struct i2c_client *client = to_i2c_client(dev);
455 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
456 int ctrl, flags, err;
461 if (rv8803->rtc->uie_rtctimer.enabled)
462 ctrl |= RV8803_CTRL_UIE;
463 if (rv8803->rtc->aie_timer.enabled)
464 ctrl |= RV8803_CTRL_AIE;
466 if (!rv8803->rtc->uie_rtctimer.enabled)
467 ctrl &= ~RV8803_CTRL_UIE;
468 if (!rv8803->rtc->aie_timer.enabled)
469 ctrl &= ~RV8803_CTRL_AIE;
472 mutex_lock(&rv8803->flags_lock);
473 flags = rv8803_read_reg(client, RV8803_FLAG);
475 mutex_unlock(&rv8803->flags_lock);
478 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
479 err = rv8803_write_reg(client, RV8803_FLAG, flags);
480 mutex_unlock(&rv8803->flags_lock);
484 if (ctrl != rv8803->ctrl) {
486 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
494 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
496 struct i2c_client *client = to_i2c_client(dev);
497 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
503 flags = rv8803_read_reg(client, RV8803_FLAG);
507 if (flags & RV8803_FLAG_V1F) {
508 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
509 vl = RTC_VL_ACCURACY_LOW;
512 if (flags & RV8803_FLAG_V2F)
513 vl |= RTC_VL_DATA_INVALID;
515 return put_user(vl, (unsigned int __user *)arg);
518 mutex_lock(&rv8803->flags_lock);
519 flags = rv8803_read_reg(client, RV8803_FLAG);
521 mutex_unlock(&rv8803->flags_lock);
525 flags &= ~RV8803_FLAG_V1F;
526 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
527 mutex_unlock(&rv8803->flags_lock);
538 static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
541 return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
544 static int rv8803_nvram_read(void *priv, unsigned int offset,
545 void *val, size_t bytes)
549 ret = rv8803_read_reg(priv, RV8803_RAM);
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,
567 static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
569 struct i2c_client *client = rv8803->client;
570 struct device_node *node = client->dev.of_node;
577 if (rv8803->type != rx_8900)
580 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
585 flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF);
586 flags |= rv8803->backup;
588 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
592 /* configure registers with values different than the Power-On reset defaults */
593 static int rv8803_regs_configure(struct rv8803_data *rv8803)
597 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
601 err = rx8900_trickle_charger_init(rv8803);
603 dev_err(&rv8803->client->dev, "failed to init charger\n");
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 },
617 MODULE_DEVICE_TABLE(i2c, rv8803_id);
619 static int rv8803_probe(struct i2c_client *client)
621 struct i2c_adapter *adapter = client->adapter;
622 struct rv8803_data *rv8803;
624 struct nvmem_config nvmem_cfg = {
625 .name = "rv8803_nvram",
629 .reg_read = rv8803_nvram_read,
630 .reg_write = rv8803_nvram_write,
634 if (!i2c_check_functionality(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");
640 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
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(&client->dev);
650 const struct i2c_device_id *id = i2c_match_id(rv8803_id, client);
652 rv8803->type = id->driver_data;
654 i2c_set_clientdata(client, rv8803);
656 flags = rv8803_read_reg(client, RV8803_FLAG);
660 if (flags & RV8803_FLAG_V1F)
661 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
663 if (flags & RV8803_FLAG_V2F)
664 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
666 if (flags & RV8803_FLAG_AF)
667 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
669 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
670 if (IS_ERR(rv8803->rtc))
671 return PTR_ERR(rv8803->rtc);
673 if (client->irq > 0) {
674 unsigned long irqflags = IRQF_TRIGGER_LOW;
676 if (dev_fwnode(&client->dev))
679 err = devm_request_threaded_irq(&client->dev, client->irq,
680 NULL, rv8803_handle_irq,
681 irqflags | IRQF_ONESHOT,
684 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
689 clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features);
691 if (of_property_read_bool(client->dev.of_node, "epson,vdet-disable"))
692 rv8803->backup |= RX8900_FLAG_VDETOFF;
694 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
695 rv8803->backup |= RX8900_FLAG_SWOFF;
697 err = rv8803_regs_configure(rv8803);
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);
708 devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
710 rv8803->rtc->max_user_freq = 1;
715 static const __maybe_unused struct of_device_id rv8803_of_match[] = {
717 .compatible = "microcrystal,rv8803",
718 .data = (void *)rv_8803
721 .compatible = "epson,rx8803",
722 .data = (void *)rx_8803
725 .compatible = "epson,rx8804",
726 .data = (void *)rx_8804
729 .compatible = "epson,rx8900",
730 .data = (void *)rx_8900
734 MODULE_DEVICE_TABLE(of, rv8803_of_match);
736 static struct i2c_driver rv8803_driver = {
738 .name = "rtc-rv8803",
739 .of_match_table = of_match_ptr(rv8803_of_match),
741 .probe = rv8803_probe,
742 .id_table = rv8803_id,
744 module_i2c_driver(rv8803_driver);
746 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
747 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
748 MODULE_LICENSE("GPL v2");