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>
18 #include <linux/of_device.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;
73 enum rv8803_type type;
76 static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
78 int try = RV8803_I2C_TRY_COUNT;
82 * There is a 61µs window during which the RTC does not acknowledge I2C
83 * transfers. In that case, ensure that there are multiple attempts.
86 ret = i2c_smbus_read_byte_data(client, reg);
87 while ((ret == -ENXIO || ret == -EIO) && --try);
89 dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
94 static int rv8803_read_regs(const struct i2c_client *client,
95 u8 reg, u8 count, u8 *values)
97 int try = RV8803_I2C_TRY_COUNT;
101 ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
102 while ((ret == -ENXIO || ret == -EIO) && --try);
104 dev_err(&client->dev,
105 "Unable to read registers 0x%02x..0x%02x\n",
106 reg, reg + count - 1);
107 return ret < 0 ? ret : -EIO;
113 static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
115 int try = RV8803_I2C_TRY_COUNT;
119 ret = i2c_smbus_write_byte_data(client, reg, value);
120 while ((ret == -ENXIO || ret == -EIO) && --try);
122 dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
127 static int rv8803_write_regs(const struct i2c_client *client,
128 u8 reg, u8 count, const u8 *values)
130 int try = RV8803_I2C_TRY_COUNT;
134 ret = i2c_smbus_write_i2c_block_data(client, reg, count,
136 while ((ret == -ENXIO || ret == -EIO) && --try);
138 dev_err(&client->dev,
139 "Unable to write registers 0x%02x..0x%02x\n",
140 reg, reg + count - 1);
145 static int rv8803_regs_init(struct rv8803_data *rv8803)
149 ret = rv8803_write_reg(rv8803->client, RV8803_OSC_OFFSET, 0x00);
153 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
154 FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */
158 ret = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3,
163 return rv8803_write_reg(rv8803->client, RV8803_RAM, 0x00);
166 static int rv8803_regs_configure(struct rv8803_data *rv8803);
168 static int rv8803_regs_reset(struct rv8803_data *rv8803)
171 * The RV-8803 resets all registers to POR defaults after voltage-loss,
172 * the Epson RTCs don't, so we manually reset the remainder here.
174 if (rv8803->type == rx_8803 || rv8803->type == rx_8900) {
175 int ret = rv8803_regs_init(rv8803);
180 return rv8803_regs_configure(rv8803);
183 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
185 struct i2c_client *client = dev_id;
186 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
187 unsigned long events = 0;
190 mutex_lock(&rv8803->flags_lock);
192 flags = rv8803_read_reg(client, RV8803_FLAG);
194 mutex_unlock(&rv8803->flags_lock);
198 if (flags & RV8803_FLAG_V1F)
199 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
201 if (flags & RV8803_FLAG_V2F)
202 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
204 if (flags & RV8803_FLAG_TF) {
205 flags &= ~RV8803_FLAG_TF;
206 rv8803->ctrl &= ~RV8803_CTRL_TIE;
210 if (flags & RV8803_FLAG_AF) {
211 flags &= ~RV8803_FLAG_AF;
212 rv8803->ctrl &= ~RV8803_CTRL_AIE;
216 if (flags & RV8803_FLAG_UF) {
217 flags &= ~RV8803_FLAG_UF;
218 rv8803->ctrl &= ~RV8803_CTRL_UIE;
223 rtc_update_irq(rv8803->rtc, 1, events);
224 rv8803_write_reg(client, RV8803_FLAG, flags);
225 rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
228 mutex_unlock(&rv8803->flags_lock);
233 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
235 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
241 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
245 if (flags & RV8803_FLAG_V2F) {
246 dev_warn(dev, "Voltage low, data is invalid.\n");
250 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
254 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
255 ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
259 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
263 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
264 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
265 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
266 tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
267 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
268 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
269 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
274 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
276 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
278 int ctrl, flags, ret;
280 ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
285 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
286 ctrl | RV8803_CTRL_RESET);
290 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
291 date[RV8803_MIN] = bin2bcd(tm->tm_min);
292 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
293 date[RV8803_WEEK] = 1 << (tm->tm_wday);
294 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
295 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
296 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
298 ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
302 /* Restart the clock */
303 ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
304 ctrl & ~RV8803_CTRL_RESET);
308 mutex_lock(&rv8803->flags_lock);
310 flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
312 mutex_unlock(&rv8803->flags_lock);
316 if (flags & RV8803_FLAG_V2F) {
317 ret = rv8803_regs_reset(rv8803);
319 mutex_unlock(&rv8803->flags_lock);
324 ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
325 flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
327 mutex_unlock(&rv8803->flags_lock);
332 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
334 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
335 struct i2c_client *client = rv8803->client;
339 ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
343 flags = rv8803_read_reg(client, RV8803_FLAG);
347 alrm->time.tm_sec = 0;
348 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
349 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
350 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
352 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
353 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
358 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
360 struct i2c_client *client = to_i2c_client(dev);
361 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
366 /* The alarm has no seconds, round up to nearest minute */
367 if (alrm->time.tm_sec) {
368 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
370 alarm_time += 60 - alrm->time.tm_sec;
371 rtc_time64_to_tm(alarm_time, &alrm->time);
374 mutex_lock(&rv8803->flags_lock);
376 ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
378 mutex_unlock(&rv8803->flags_lock);
382 alarmvals[0] = bin2bcd(alrm->time.tm_min);
383 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
384 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
386 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
387 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
388 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
391 mutex_unlock(&rv8803->flags_lock);
396 ctrl[0] &= ~RV8803_FLAG_AF;
397 err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]);
398 mutex_unlock(&rv8803->flags_lock);
402 err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
407 if (rv8803->rtc->uie_rtctimer.enabled)
408 rv8803->ctrl |= RV8803_CTRL_UIE;
409 if (rv8803->rtc->aie_timer.enabled)
410 rv8803->ctrl |= RV8803_CTRL_AIE;
412 err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
421 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
423 struct i2c_client *client = to_i2c_client(dev);
424 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
425 int ctrl, flags, err;
430 if (rv8803->rtc->uie_rtctimer.enabled)
431 ctrl |= RV8803_CTRL_UIE;
432 if (rv8803->rtc->aie_timer.enabled)
433 ctrl |= RV8803_CTRL_AIE;
435 if (!rv8803->rtc->uie_rtctimer.enabled)
436 ctrl &= ~RV8803_CTRL_UIE;
437 if (!rv8803->rtc->aie_timer.enabled)
438 ctrl &= ~RV8803_CTRL_AIE;
441 mutex_lock(&rv8803->flags_lock);
442 flags = rv8803_read_reg(client, RV8803_FLAG);
444 mutex_unlock(&rv8803->flags_lock);
447 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
448 err = rv8803_write_reg(client, RV8803_FLAG, flags);
449 mutex_unlock(&rv8803->flags_lock);
453 if (ctrl != rv8803->ctrl) {
455 err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
463 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
465 struct i2c_client *client = to_i2c_client(dev);
466 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
472 flags = rv8803_read_reg(client, RV8803_FLAG);
476 if (flags & RV8803_FLAG_V1F) {
477 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
478 vl = RTC_VL_ACCURACY_LOW;
481 if (flags & RV8803_FLAG_V2F)
482 vl |= RTC_VL_DATA_INVALID;
484 return put_user(vl, (unsigned int __user *)arg);
487 mutex_lock(&rv8803->flags_lock);
488 flags = rv8803_read_reg(client, RV8803_FLAG);
490 mutex_unlock(&rv8803->flags_lock);
494 flags &= ~RV8803_FLAG_V1F;
495 ret = rv8803_write_reg(client, RV8803_FLAG, flags);
496 mutex_unlock(&rv8803->flags_lock);
507 static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
510 return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
513 static int rv8803_nvram_read(void *priv, unsigned int offset,
514 void *val, size_t bytes)
518 ret = rv8803_read_reg(priv, RV8803_RAM);
527 static const struct rtc_class_ops rv8803_rtc_ops = {
528 .read_time = rv8803_get_time,
529 .set_time = rv8803_set_time,
530 .ioctl = rv8803_ioctl,
531 .read_alarm = rv8803_get_alarm,
532 .set_alarm = rv8803_set_alarm,
533 .alarm_irq_enable = rv8803_alarm_irq_enable,
536 static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
538 struct i2c_client *client = rv8803->client;
539 struct device_node *node = client->dev.of_node;
546 if (rv8803->type != rx_8900)
549 err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
554 flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF);
555 flags |= rv8803->backup;
557 return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
561 /* configure registers with values different than the Power-On reset defaults */
562 static int rv8803_regs_configure(struct rv8803_data *rv8803)
566 err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
570 err = rx8900_trickle_charger_init(rv8803);
572 dev_err(&rv8803->client->dev, "failed to init charger\n");
579 static int rv8803_probe(struct i2c_client *client,
580 const struct i2c_device_id *id)
582 struct i2c_adapter *adapter = client->adapter;
583 struct rv8803_data *rv8803;
585 struct nvmem_config nvmem_cfg = {
586 .name = "rv8803_nvram",
590 .reg_read = rv8803_nvram_read,
591 .reg_write = rv8803_nvram_write,
595 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
596 I2C_FUNC_SMBUS_I2C_BLOCK)) {
597 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
601 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
606 mutex_init(&rv8803->flags_lock);
607 rv8803->client = client;
608 if (client->dev.of_node)
609 rv8803->type = (enum rv8803_type)
610 of_device_get_match_data(&client->dev);
612 rv8803->type = id->driver_data;
613 i2c_set_clientdata(client, rv8803);
615 flags = rv8803_read_reg(client, RV8803_FLAG);
619 if (flags & RV8803_FLAG_V1F)
620 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
622 if (flags & RV8803_FLAG_V2F)
623 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
625 if (flags & RV8803_FLAG_AF)
626 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
628 rv8803->rtc = devm_rtc_allocate_device(&client->dev);
629 if (IS_ERR(rv8803->rtc))
630 return PTR_ERR(rv8803->rtc);
632 if (client->irq > 0) {
633 err = devm_request_threaded_irq(&client->dev, client->irq,
634 NULL, rv8803_handle_irq,
635 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
638 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
643 clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features);
645 if (of_property_read_bool(client->dev.of_node, "epson,vdet-disable"))
646 rv8803->backup |= RX8900_FLAG_VDETOFF;
648 if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
649 rv8803->backup |= RX8900_FLAG_SWOFF;
651 err = rv8803_regs_configure(rv8803);
655 rv8803->rtc->ops = &rv8803_rtc_ops;
656 rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
657 rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
658 err = devm_rtc_register_device(rv8803->rtc);
662 devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
664 rv8803->rtc->max_user_freq = 1;
669 static const struct i2c_device_id rv8803_id[] = {
670 { "rv8803", rv_8803 },
671 { "rv8804", rx_8804 },
672 { "rx8803", rx_8803 },
673 { "rx8900", rx_8900 },
676 MODULE_DEVICE_TABLE(i2c, rv8803_id);
678 static const __maybe_unused struct of_device_id rv8803_of_match[] = {
680 .compatible = "microcrystal,rv8803",
681 .data = (void *)rv_8803
684 .compatible = "epson,rx8803",
685 .data = (void *)rx_8803
688 .compatible = "epson,rx8804",
689 .data = (void *)rx_8804
692 .compatible = "epson,rx8900",
693 .data = (void *)rx_8900
697 MODULE_DEVICE_TABLE(of, rv8803_of_match);
699 static struct i2c_driver rv8803_driver = {
701 .name = "rtc-rv8803",
702 .of_match_table = of_match_ptr(rv8803_of_match),
704 .probe = rv8803_probe,
705 .id_table = rv8803_id,
707 module_i2c_driver(rv8803_driver);
709 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
710 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
711 MODULE_LICENSE("GPL v2");