1 // SPDX-License-Identifier: GPL-2.0
3 * RTC driver for the Micro Crystal RV3028
5 * Copyright (C) 2019 Micro Crystal SA
7 * Alexandre Belloni <alexandre.belloni@bootlin.com>
11 #include <linux/bcd.h>
12 #include <linux/bitops.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/log2.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/rtc.h>
22 #define RV3028_SEC 0x00
23 #define RV3028_MIN 0x01
24 #define RV3028_HOUR 0x02
25 #define RV3028_WDAY 0x03
26 #define RV3028_DAY 0x04
27 #define RV3028_MONTH 0x05
28 #define RV3028_YEAR 0x06
29 #define RV3028_ALARM_MIN 0x07
30 #define RV3028_ALARM_HOUR 0x08
31 #define RV3028_ALARM_DAY 0x09
32 #define RV3028_STATUS 0x0E
33 #define RV3028_CTRL1 0x0F
34 #define RV3028_CTRL2 0x10
35 #define RV3028_EVT_CTRL 0x13
36 #define RV3028_TS_COUNT 0x14
37 #define RV3028_TS_SEC 0x15
38 #define RV3028_RAM1 0x1F
39 #define RV3028_EEPROM_ADDR 0x25
40 #define RV3028_EEPROM_DATA 0x26
41 #define RV3028_EEPROM_CMD 0x27
42 #define RV3028_CLKOUT 0x35
43 #define RV3028_OFFSET 0x36
44 #define RV3028_BACKUP 0x37
46 #define RV3028_STATUS_PORF BIT(0)
47 #define RV3028_STATUS_EVF BIT(1)
48 #define RV3028_STATUS_AF BIT(2)
49 #define RV3028_STATUS_TF BIT(3)
50 #define RV3028_STATUS_UF BIT(4)
51 #define RV3028_STATUS_BSF BIT(5)
52 #define RV3028_STATUS_CLKF BIT(6)
53 #define RV3028_STATUS_EEBUSY BIT(7)
55 #define RV3028_CTRL1_EERD BIT(3)
56 #define RV3028_CTRL1_WADA BIT(5)
58 #define RV3028_CTRL2_RESET BIT(0)
59 #define RV3028_CTRL2_12_24 BIT(1)
60 #define RV3028_CTRL2_EIE BIT(2)
61 #define RV3028_CTRL2_AIE BIT(3)
62 #define RV3028_CTRL2_TIE BIT(4)
63 #define RV3028_CTRL2_UIE BIT(5)
64 #define RV3028_CTRL2_TSE BIT(7)
66 #define RV3028_EVT_CTRL_TSR BIT(2)
68 #define RV3028_EEPROM_CMD_WRITE 0x21
69 #define RV3028_EEPROM_CMD_READ 0x22
71 #define RV3028_EEBUSY_POLL 10000
72 #define RV3028_EEBUSY_TIMEOUT 100000
74 #define RV3028_BACKUP_TCE BIT(5)
75 #define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
77 #define OFFSET_STEP_PPT 953674
84 struct regmap
*regmap
;
85 struct rtc_device
*rtc
;
86 enum rv3028_type type
;
89 static u16 rv3028_trickle_resistors
[] = {1000, 3000, 6000, 11000};
91 static ssize_t
timestamp0_store(struct device
*dev
,
92 struct device_attribute
*attr
,
93 const char *buf
, size_t count
)
95 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
97 regmap_update_bits(rv3028
->regmap
, RV3028_EVT_CTRL
, RV3028_EVT_CTRL_TSR
,
103 static ssize_t
timestamp0_show(struct device
*dev
,
104 struct device_attribute
*attr
, char *buf
)
106 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
111 ret
= regmap_read(rv3028
->regmap
, RV3028_TS_COUNT
, &count
);
118 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_TS_SEC
, date
,
123 tm
.tm_sec
= bcd2bin(date
[0]);
124 tm
.tm_min
= bcd2bin(date
[1]);
125 tm
.tm_hour
= bcd2bin(date
[2]);
126 tm
.tm_mday
= bcd2bin(date
[3]);
127 tm
.tm_mon
= bcd2bin(date
[4]) - 1;
128 tm
.tm_year
= bcd2bin(date
[5]) + 100;
130 ret
= rtc_valid_tm(&tm
);
134 return sprintf(buf
, "%llu\n",
135 (unsigned long long)rtc_tm_to_time64(&tm
));
138 static DEVICE_ATTR_RW(timestamp0
);
140 static ssize_t
timestamp0_count_show(struct device
*dev
,
141 struct device_attribute
*attr
, char *buf
)
143 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
146 ret
= regmap_read(rv3028
->regmap
, RV3028_TS_COUNT
, &count
);
150 return sprintf(buf
, "%u\n", count
);
153 static DEVICE_ATTR_RO(timestamp0_count
);
155 static struct attribute
*rv3028_attrs
[] = {
156 &dev_attr_timestamp0
.attr
,
157 &dev_attr_timestamp0_count
.attr
,
161 static const struct attribute_group rv3028_attr_group
= {
162 .attrs
= rv3028_attrs
,
165 static irqreturn_t
rv3028_handle_irq(int irq
, void *dev_id
)
167 struct rv3028_data
*rv3028
= dev_id
;
168 unsigned long events
= 0;
169 u32 status
= 0, ctrl
= 0;
171 if (regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
) < 0 ||
176 if (status
& RV3028_STATUS_PORF
)
177 dev_warn(&rv3028
->rtc
->dev
, "Voltage low, data loss detected.\n");
179 if (status
& RV3028_STATUS_TF
) {
180 status
|= RV3028_STATUS_TF
;
181 ctrl
|= RV3028_CTRL2_TIE
;
185 if (status
& RV3028_STATUS_AF
) {
186 status
|= RV3028_STATUS_AF
;
187 ctrl
|= RV3028_CTRL2_AIE
;
191 if (status
& RV3028_STATUS_UF
) {
192 status
|= RV3028_STATUS_UF
;
193 ctrl
|= RV3028_CTRL2_UIE
;
198 rtc_update_irq(rv3028
->rtc
, 1, events
);
199 regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
, status
, 0);
200 regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
, ctrl
, 0);
203 if (status
& RV3028_STATUS_EVF
) {
204 sysfs_notify(&rv3028
->rtc
->dev
.kobj
, NULL
,
205 dev_attr_timestamp0
.attr
.name
);
206 dev_warn(&rv3028
->rtc
->dev
, "event detected");
212 static int rv3028_get_time(struct device
*dev
, struct rtc_time
*tm
)
214 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
218 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
222 if (status
& RV3028_STATUS_PORF
) {
223 dev_warn(dev
, "Voltage low, data is invalid.\n");
227 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_SEC
, date
, sizeof(date
));
231 tm
->tm_sec
= bcd2bin(date
[RV3028_SEC
] & 0x7f);
232 tm
->tm_min
= bcd2bin(date
[RV3028_MIN
] & 0x7f);
233 tm
->tm_hour
= bcd2bin(date
[RV3028_HOUR
] & 0x3f);
234 tm
->tm_wday
= ilog2(date
[RV3028_WDAY
] & 0x7f);
235 tm
->tm_mday
= bcd2bin(date
[RV3028_DAY
] & 0x3f);
236 tm
->tm_mon
= bcd2bin(date
[RV3028_MONTH
] & 0x1f) - 1;
237 tm
->tm_year
= bcd2bin(date
[RV3028_YEAR
]) + 100;
242 static int rv3028_set_time(struct device
*dev
, struct rtc_time
*tm
)
244 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
248 date
[RV3028_SEC
] = bin2bcd(tm
->tm_sec
);
249 date
[RV3028_MIN
] = bin2bcd(tm
->tm_min
);
250 date
[RV3028_HOUR
] = bin2bcd(tm
->tm_hour
);
251 date
[RV3028_WDAY
] = 1 << (tm
->tm_wday
);
252 date
[RV3028_DAY
] = bin2bcd(tm
->tm_mday
);
253 date
[RV3028_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
254 date
[RV3028_YEAR
] = bin2bcd(tm
->tm_year
- 100);
257 * Writing to the Seconds register has the same effect as setting RESET
260 ret
= regmap_bulk_write(rv3028
->regmap
, RV3028_SEC
, date
,
265 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
266 RV3028_STATUS_PORF
, 0);
271 static int rv3028_get_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
273 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
275 int status
, ctrl
, ret
;
277 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_ALARM_MIN
, alarmvals
,
282 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
286 ret
= regmap_read(rv3028
->regmap
, RV3028_CTRL2
, &ctrl
);
290 alrm
->time
.tm_sec
= 0;
291 alrm
->time
.tm_min
= bcd2bin(alarmvals
[0] & 0x7f);
292 alrm
->time
.tm_hour
= bcd2bin(alarmvals
[1] & 0x3f);
293 alrm
->time
.tm_mday
= bcd2bin(alarmvals
[2] & 0x3f);
295 alrm
->enabled
= !!(ctrl
& RV3028_CTRL2_AIE
);
296 alrm
->pending
= (status
& RV3028_STATUS_AF
) && alrm
->enabled
;
301 static int rv3028_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
303 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
308 /* The alarm has no seconds, round up to nearest minute */
309 if (alrm
->time
.tm_sec
) {
310 time64_t alarm_time
= rtc_tm_to_time64(&alrm
->time
);
312 alarm_time
+= 60 - alrm
->time
.tm_sec
;
313 rtc_time64_to_tm(alarm_time
, &alrm
->time
);
316 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
317 RV3028_CTRL2_AIE
| RV3028_CTRL2_UIE
, 0);
321 alarmvals
[0] = bin2bcd(alrm
->time
.tm_min
);
322 alarmvals
[1] = bin2bcd(alrm
->time
.tm_hour
);
323 alarmvals
[2] = bin2bcd(alrm
->time
.tm_mday
);
325 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
326 RV3028_STATUS_AF
, 0);
330 ret
= regmap_bulk_write(rv3028
->regmap
, RV3028_ALARM_MIN
, alarmvals
,
336 if (rv3028
->rtc
->uie_rtctimer
.enabled
)
337 ctrl
|= RV3028_CTRL2_UIE
;
338 if (rv3028
->rtc
->aie_timer
.enabled
)
339 ctrl
|= RV3028_CTRL2_AIE
;
342 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
343 RV3028_CTRL2_UIE
| RV3028_CTRL2_AIE
, ctrl
);
348 static int rv3028_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
350 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
354 if (rv3028
->rtc
->uie_rtctimer
.enabled
)
355 ctrl
|= RV3028_CTRL2_UIE
;
356 if (rv3028
->rtc
->aie_timer
.enabled
)
357 ctrl
|= RV3028_CTRL2_AIE
;
360 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
361 RV3028_STATUS_AF
| RV3028_STATUS_UF
, 0);
365 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
366 RV3028_CTRL2_UIE
| RV3028_CTRL2_AIE
, ctrl
);
373 static int rv3028_read_offset(struct device
*dev
, long *offset
)
375 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
376 int ret
, value
, steps
;
378 ret
= regmap_read(rv3028
->regmap
, RV3028_OFFSET
, &value
);
382 steps
= sign_extend32(value
<< 1, 8);
384 ret
= regmap_read(rv3028
->regmap
, RV3028_BACKUP
, &value
);
390 *offset
= DIV_ROUND_CLOSEST(steps
* OFFSET_STEP_PPT
, 1000);
395 static int rv3028_set_offset(struct device
*dev
, long offset
)
397 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
400 offset
= clamp(offset
, -244141L, 243187L) * 1000;
401 offset
= DIV_ROUND_CLOSEST(offset
, OFFSET_STEP_PPT
);
403 ret
= regmap_write(rv3028
->regmap
, RV3028_OFFSET
, offset
>> 1);
407 return regmap_update_bits(rv3028
->regmap
, RV3028_BACKUP
, BIT(7),
411 static int rv3028_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
413 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
418 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
422 if (status
& RV3028_STATUS_PORF
)
423 dev_warn(&rv3028
->rtc
->dev
, "Voltage low, data loss detected.\n");
425 status
&= RV3028_STATUS_PORF
;
427 if (copy_to_user((void __user
*)arg
, &status
, sizeof(int)))
433 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
434 RV3028_STATUS_PORF
, 0);
443 static int rv3028_nvram_write(void *priv
, unsigned int offset
, void *val
,
446 return regmap_bulk_write(priv
, RV3028_RAM1
+ offset
, val
, bytes
);
449 static int rv3028_nvram_read(void *priv
, unsigned int offset
, void *val
,
452 return regmap_bulk_read(priv
, RV3028_RAM1
+ offset
, val
, bytes
);
455 static int rv3028_eeprom_write(void *priv
, unsigned int offset
, void *val
,
462 ret
= regmap_read(priv
, RV3028_CTRL1
, &ctrl1
);
466 if (!(ctrl1
& RV3028_CTRL1_EERD
)) {
467 ret
= regmap_update_bits(priv
, RV3028_CTRL1
,
468 RV3028_CTRL1_EERD
, RV3028_CTRL1_EERD
);
472 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
473 !(status
& RV3028_STATUS_EEBUSY
),
475 RV3028_EEBUSY_TIMEOUT
);
480 for (i
= 0; i
< bytes
; i
++) {
481 ret
= regmap_write(priv
, RV3028_EEPROM_ADDR
, offset
+ i
);
485 ret
= regmap_write(priv
, RV3028_EEPROM_DATA
, buf
[i
]);
489 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
, 0x0);
493 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
,
494 RV3028_EEPROM_CMD_WRITE
);
498 usleep_range(RV3028_EEBUSY_POLL
, RV3028_EEBUSY_TIMEOUT
);
500 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
501 !(status
& RV3028_STATUS_EEBUSY
),
503 RV3028_EEBUSY_TIMEOUT
);
509 if (!(ctrl1
& RV3028_CTRL1_EERD
))
511 err
= regmap_update_bits(priv
, RV3028_CTRL1
, RV3028_CTRL1_EERD
,
520 static int rv3028_eeprom_read(void *priv
, unsigned int offset
, void *val
,
523 u32 status
, ctrl1
, data
;
527 ret
= regmap_read(priv
, RV3028_CTRL1
, &ctrl1
);
531 if (!(ctrl1
& RV3028_CTRL1_EERD
)) {
532 ret
= regmap_update_bits(priv
, RV3028_CTRL1
,
533 RV3028_CTRL1_EERD
, RV3028_CTRL1_EERD
);
537 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
538 !(status
& RV3028_STATUS_EEBUSY
),
540 RV3028_EEBUSY_TIMEOUT
);
545 for (i
= 0; i
< bytes
; i
++) {
546 ret
= regmap_write(priv
, RV3028_EEPROM_ADDR
, offset
+ i
);
550 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
, 0x0);
554 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
,
555 RV3028_EEPROM_CMD_READ
);
559 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
560 !(status
& RV3028_STATUS_EEBUSY
),
562 RV3028_EEBUSY_TIMEOUT
);
566 ret
= regmap_read(priv
, RV3028_EEPROM_DATA
, &data
);
573 if (!(ctrl1
& RV3028_CTRL1_EERD
))
575 err
= regmap_update_bits(priv
, RV3028_CTRL1
, RV3028_CTRL1_EERD
,
584 static struct rtc_class_ops rv3028_rtc_ops
= {
585 .read_time
= rv3028_get_time
,
586 .set_time
= rv3028_set_time
,
587 .read_offset
= rv3028_read_offset
,
588 .set_offset
= rv3028_set_offset
,
589 .ioctl
= rv3028_ioctl
,
592 static const struct regmap_config regmap_config
= {
595 .max_register
= 0x37,
598 static int rv3028_probe(struct i2c_client
*client
)
600 struct rv3028_data
*rv3028
;
603 struct nvmem_config nvmem_cfg
= {
604 .name
= "rv3028_nvram",
608 .type
= NVMEM_TYPE_BATTERY_BACKED
,
609 .reg_read
= rv3028_nvram_read
,
610 .reg_write
= rv3028_nvram_write
,
612 struct nvmem_config eeprom_cfg
= {
613 .name
= "rv3028_eeprom",
617 .type
= NVMEM_TYPE_EEPROM
,
618 .reg_read
= rv3028_eeprom_read
,
619 .reg_write
= rv3028_eeprom_write
,
622 rv3028
= devm_kzalloc(&client
->dev
, sizeof(struct rv3028_data
),
627 rv3028
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
629 i2c_set_clientdata(client
, rv3028
);
631 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
635 if (status
& RV3028_STATUS_PORF
)
636 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
638 if (status
& RV3028_STATUS_AF
)
639 dev_warn(&client
->dev
, "An alarm may have been missed.\n");
641 rv3028
->rtc
= devm_rtc_allocate_device(&client
->dev
);
642 if (IS_ERR(rv3028
->rtc
)) {
643 return PTR_ERR(rv3028
->rtc
);
646 if (client
->irq
> 0) {
647 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
648 NULL
, rv3028_handle_irq
,
649 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
652 dev_warn(&client
->dev
, "unable to request IRQ, alarms disabled\n");
655 rv3028_rtc_ops
.read_alarm
= rv3028_get_alarm
;
656 rv3028_rtc_ops
.set_alarm
= rv3028_set_alarm
;
657 rv3028_rtc_ops
.alarm_irq_enable
= rv3028_alarm_irq_enable
;
661 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL1
,
662 RV3028_CTRL1_WADA
, RV3028_CTRL1_WADA
);
666 /* setup timestamping */
667 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
668 RV3028_CTRL2_EIE
| RV3028_CTRL2_TSE
,
669 RV3028_CTRL2_EIE
| RV3028_CTRL2_TSE
);
673 /* setup trickle charger */
674 if (!device_property_read_u32(&client
->dev
, "trickle-resistor-ohms",
678 for (i
= 0; i
< ARRAY_SIZE(rv3028_trickle_resistors
); i
++)
679 if (ohms
== rv3028_trickle_resistors
[i
])
682 if (i
< ARRAY_SIZE(rv3028_trickle_resistors
)) {
683 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_BACKUP
,
685 RV3028_BACKUP_TCR_MASK
,
686 RV3028_BACKUP_TCE
| i
);
690 dev_warn(&client
->dev
, "invalid trickle resistor value\n");
694 ret
= rtc_add_group(rv3028
->rtc
, &rv3028_attr_group
);
698 rv3028
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
699 rv3028
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
700 rv3028
->rtc
->ops
= &rv3028_rtc_ops
;
701 ret
= rtc_register_device(rv3028
->rtc
);
705 nvmem_cfg
.priv
= rv3028
->regmap
;
706 rtc_nvmem_register(rv3028
->rtc
, &nvmem_cfg
);
707 eeprom_cfg
.priv
= rv3028
->regmap
;
708 rtc_nvmem_register(rv3028
->rtc
, &eeprom_cfg
);
710 rv3028
->rtc
->max_user_freq
= 1;
715 static const struct of_device_id rv3028_of_match
[] = {
716 { .compatible
= "microcrystal,rv3028", },
719 MODULE_DEVICE_TABLE(of
, rv3028_of_match
);
721 static struct i2c_driver rv3028_driver
= {
723 .name
= "rtc-rv3028",
724 .of_match_table
= of_match_ptr(rv3028_of_match
),
726 .probe_new
= rv3028_probe
,
728 module_i2c_driver(rv3028_driver
);
730 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
731 MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
732 MODULE_LICENSE("GPL v2");