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/log2.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/rtc.h>
20 #define RV8803_I2C_TRY_COUNT 4
22 #define RV8803_SEC 0x00
23 #define RV8803_MIN 0x01
24 #define RV8803_HOUR 0x02
25 #define RV8803_WEEK 0x03
26 #define RV8803_DAY 0x04
27 #define RV8803_MONTH 0x05
28 #define RV8803_YEAR 0x06
29 #define RV8803_RAM 0x07
30 #define RV8803_ALARM_MIN 0x08
31 #define RV8803_ALARM_HOUR 0x09
32 #define RV8803_ALARM_WEEK_OR_DAY 0x0A
33 #define RV8803_EXT 0x0D
34 #define RV8803_FLAG 0x0E
35 #define RV8803_CTRL 0x0F
37 #define RV8803_EXT_WADA BIT(6)
39 #define RV8803_FLAG_V1F BIT(0)
40 #define RV8803_FLAG_V2F BIT(1)
41 #define RV8803_FLAG_AF BIT(3)
42 #define RV8803_FLAG_TF BIT(4)
43 #define RV8803_FLAG_UF BIT(5)
45 #define RV8803_CTRL_RESET BIT(0)
47 #define RV8803_CTRL_EIE BIT(2)
48 #define RV8803_CTRL_AIE BIT(3)
49 #define RV8803_CTRL_TIE BIT(4)
50 #define RV8803_CTRL_UIE BIT(5)
52 #define RX8900_BACKUP_CTRL 0x18
53 #define RX8900_FLAG_SWOFF BIT(2)
54 #define RX8900_FLAG_VDETOFF BIT(3)
62 struct i2c_client
*client
;
63 struct rtc_device
*rtc
;
64 struct mutex flags_lock
;
66 enum rv8803_type type
;
69 static int rv8803_read_reg(const struct i2c_client
*client
, u8 reg
)
71 int try = RV8803_I2C_TRY_COUNT
;
75 * There is a 61µs window during which the RTC does not acknowledge I2C
76 * transfers. In that case, ensure that there are multiple attempts.
79 ret
= i2c_smbus_read_byte_data(client
, reg
);
80 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
82 dev_err(&client
->dev
, "Unable to read register 0x%02x\n", reg
);
87 static int rv8803_read_regs(const struct i2c_client
*client
,
88 u8 reg
, u8 count
, u8
*values
)
90 int try = RV8803_I2C_TRY_COUNT
;
94 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, count
, values
);
95 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
98 "Unable to read registers 0x%02x..0x%02x\n",
99 reg
, reg
+ count
- 1);
100 return ret
< 0 ? ret
: -EIO
;
106 static int rv8803_write_reg(const struct i2c_client
*client
, u8 reg
, u8 value
)
108 int try = RV8803_I2C_TRY_COUNT
;
112 ret
= i2c_smbus_write_byte_data(client
, reg
, value
);
113 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
115 dev_err(&client
->dev
, "Unable to write register 0x%02x\n", reg
);
120 static int rv8803_write_regs(const struct i2c_client
*client
,
121 u8 reg
, u8 count
, const u8
*values
)
123 int try = RV8803_I2C_TRY_COUNT
;
127 ret
= i2c_smbus_write_i2c_block_data(client
, reg
, count
,
129 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
131 dev_err(&client
->dev
,
132 "Unable to write registers 0x%02x..0x%02x\n",
133 reg
, reg
+ count
- 1);
138 static irqreturn_t
rv8803_handle_irq(int irq
, void *dev_id
)
140 struct i2c_client
*client
= dev_id
;
141 struct rv8803_data
*rv8803
= i2c_get_clientdata(client
);
142 unsigned long events
= 0;
145 mutex_lock(&rv8803
->flags_lock
);
147 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
149 mutex_unlock(&rv8803
->flags_lock
);
153 if (flags
& RV8803_FLAG_V1F
)
154 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
156 if (flags
& RV8803_FLAG_V2F
)
157 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
159 if (flags
& RV8803_FLAG_TF
) {
160 flags
&= ~RV8803_FLAG_TF
;
161 rv8803
->ctrl
&= ~RV8803_CTRL_TIE
;
165 if (flags
& RV8803_FLAG_AF
) {
166 flags
&= ~RV8803_FLAG_AF
;
167 rv8803
->ctrl
&= ~RV8803_CTRL_AIE
;
171 if (flags
& RV8803_FLAG_UF
) {
172 flags
&= ~RV8803_FLAG_UF
;
173 rv8803
->ctrl
&= ~RV8803_CTRL_UIE
;
178 rtc_update_irq(rv8803
->rtc
, 1, events
);
179 rv8803_write_reg(client
, RV8803_FLAG
, flags
);
180 rv8803_write_reg(rv8803
->client
, RV8803_CTRL
, rv8803
->ctrl
);
183 mutex_unlock(&rv8803
->flags_lock
);
188 static int rv8803_get_time(struct device
*dev
, struct rtc_time
*tm
)
190 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
196 flags
= rv8803_read_reg(rv8803
->client
, RV8803_FLAG
);
200 if (flags
& RV8803_FLAG_V2F
) {
201 dev_warn(dev
, "Voltage low, data is invalid.\n");
205 ret
= rv8803_read_regs(rv8803
->client
, RV8803_SEC
, 7, date
);
209 if ((date1
[RV8803_SEC
] & 0x7f) == bin2bcd(59)) {
210 ret
= rv8803_read_regs(rv8803
->client
, RV8803_SEC
, 7, date2
);
214 if ((date2
[RV8803_SEC
] & 0x7f) != bin2bcd(59))
218 tm
->tm_sec
= bcd2bin(date
[RV8803_SEC
] & 0x7f);
219 tm
->tm_min
= bcd2bin(date
[RV8803_MIN
] & 0x7f);
220 tm
->tm_hour
= bcd2bin(date
[RV8803_HOUR
] & 0x3f);
221 tm
->tm_wday
= ilog2(date
[RV8803_WEEK
] & 0x7f);
222 tm
->tm_mday
= bcd2bin(date
[RV8803_DAY
] & 0x3f);
223 tm
->tm_mon
= bcd2bin(date
[RV8803_MONTH
] & 0x1f) - 1;
224 tm
->tm_year
= bcd2bin(date
[RV8803_YEAR
]) + 100;
229 static int rv8803_set_time(struct device
*dev
, struct rtc_time
*tm
)
231 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
233 int ctrl
, flags
, ret
;
235 ctrl
= rv8803_read_reg(rv8803
->client
, RV8803_CTRL
);
240 ret
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
241 ctrl
| RV8803_CTRL_RESET
);
245 date
[RV8803_SEC
] = bin2bcd(tm
->tm_sec
);
246 date
[RV8803_MIN
] = bin2bcd(tm
->tm_min
);
247 date
[RV8803_HOUR
] = bin2bcd(tm
->tm_hour
);
248 date
[RV8803_WEEK
] = 1 << (tm
->tm_wday
);
249 date
[RV8803_DAY
] = bin2bcd(tm
->tm_mday
);
250 date
[RV8803_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
251 date
[RV8803_YEAR
] = bin2bcd(tm
->tm_year
- 100);
253 ret
= rv8803_write_regs(rv8803
->client
, RV8803_SEC
, 7, date
);
257 /* Restart the clock */
258 ret
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
259 ctrl
& ~RV8803_CTRL_RESET
);
263 mutex_lock(&rv8803
->flags_lock
);
265 flags
= rv8803_read_reg(rv8803
->client
, RV8803_FLAG
);
267 mutex_unlock(&rv8803
->flags_lock
);
271 ret
= rv8803_write_reg(rv8803
->client
, RV8803_FLAG
,
272 flags
& ~(RV8803_FLAG_V1F
| RV8803_FLAG_V2F
));
274 mutex_unlock(&rv8803
->flags_lock
);
279 static int rv8803_get_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
281 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
282 struct i2c_client
*client
= rv8803
->client
;
286 ret
= rv8803_read_regs(client
, RV8803_ALARM_MIN
, 3, alarmvals
);
290 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
294 alrm
->time
.tm_sec
= 0;
295 alrm
->time
.tm_min
= bcd2bin(alarmvals
[0] & 0x7f);
296 alrm
->time
.tm_hour
= bcd2bin(alarmvals
[1] & 0x3f);
297 alrm
->time
.tm_mday
= bcd2bin(alarmvals
[2] & 0x3f);
299 alrm
->enabled
= !!(rv8803
->ctrl
& RV8803_CTRL_AIE
);
300 alrm
->pending
= (flags
& RV8803_FLAG_AF
) && alrm
->enabled
;
305 static int rv8803_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
307 struct i2c_client
*client
= to_i2c_client(dev
);
308 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
313 /* The alarm has no seconds, round up to nearest minute */
314 if (alrm
->time
.tm_sec
) {
315 time64_t alarm_time
= rtc_tm_to_time64(&alrm
->time
);
317 alarm_time
+= 60 - alrm
->time
.tm_sec
;
318 rtc_time64_to_tm(alarm_time
, &alrm
->time
);
321 mutex_lock(&rv8803
->flags_lock
);
323 ret
= rv8803_read_regs(client
, RV8803_FLAG
, 2, ctrl
);
325 mutex_unlock(&rv8803
->flags_lock
);
329 alarmvals
[0] = bin2bcd(alrm
->time
.tm_min
);
330 alarmvals
[1] = bin2bcd(alrm
->time
.tm_hour
);
331 alarmvals
[2] = bin2bcd(alrm
->time
.tm_mday
);
333 if (rv8803
->ctrl
& (RV8803_CTRL_AIE
| RV8803_CTRL_UIE
)) {
334 rv8803
->ctrl
&= ~(RV8803_CTRL_AIE
| RV8803_CTRL_UIE
);
335 err
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
338 mutex_unlock(&rv8803
->flags_lock
);
343 ctrl
[1] &= ~RV8803_FLAG_AF
;
344 err
= rv8803_write_reg(rv8803
->client
, RV8803_FLAG
, ctrl
[1]);
345 mutex_unlock(&rv8803
->flags_lock
);
349 err
= rv8803_write_regs(rv8803
->client
, RV8803_ALARM_MIN
, 3, alarmvals
);
354 if (rv8803
->rtc
->uie_rtctimer
.enabled
)
355 rv8803
->ctrl
|= RV8803_CTRL_UIE
;
356 if (rv8803
->rtc
->aie_timer
.enabled
)
357 rv8803
->ctrl
|= RV8803_CTRL_AIE
;
359 err
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
368 static int rv8803_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
370 struct i2c_client
*client
= to_i2c_client(dev
);
371 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
372 int ctrl
, flags
, err
;
377 if (rv8803
->rtc
->uie_rtctimer
.enabled
)
378 ctrl
|= RV8803_CTRL_UIE
;
379 if (rv8803
->rtc
->aie_timer
.enabled
)
380 ctrl
|= RV8803_CTRL_AIE
;
382 if (!rv8803
->rtc
->uie_rtctimer
.enabled
)
383 ctrl
&= ~RV8803_CTRL_UIE
;
384 if (!rv8803
->rtc
->aie_timer
.enabled
)
385 ctrl
&= ~RV8803_CTRL_AIE
;
388 mutex_lock(&rv8803
->flags_lock
);
389 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
391 mutex_unlock(&rv8803
->flags_lock
);
394 flags
&= ~(RV8803_FLAG_AF
| RV8803_FLAG_UF
);
395 err
= rv8803_write_reg(client
, RV8803_FLAG
, flags
);
396 mutex_unlock(&rv8803
->flags_lock
);
400 if (ctrl
!= rv8803
->ctrl
) {
402 err
= rv8803_write_reg(client
, RV8803_CTRL
, rv8803
->ctrl
);
410 static int rv8803_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
412 struct i2c_client
*client
= to_i2c_client(dev
);
413 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
418 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
422 if (flags
& RV8803_FLAG_V1F
)
423 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
425 if (flags
& RV8803_FLAG_V2F
)
426 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
428 flags
&= RV8803_FLAG_V1F
| RV8803_FLAG_V2F
;
430 if (copy_to_user((void __user
*)arg
, &flags
, sizeof(int)))
436 mutex_lock(&rv8803
->flags_lock
);
437 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
439 mutex_unlock(&rv8803
->flags_lock
);
443 flags
&= ~(RV8803_FLAG_V1F
| RV8803_FLAG_V2F
);
444 ret
= rv8803_write_reg(client
, RV8803_FLAG
, flags
);
445 mutex_unlock(&rv8803
->flags_lock
);
456 static int rv8803_nvram_write(void *priv
, unsigned int offset
, void *val
,
461 ret
= rv8803_write_reg(priv
, RV8803_RAM
, *(u8
*)val
);
468 static int rv8803_nvram_read(void *priv
, unsigned int offset
,
469 void *val
, size_t bytes
)
473 ret
= rv8803_read_reg(priv
, RV8803_RAM
);
482 static struct rtc_class_ops rv8803_rtc_ops
= {
483 .read_time
= rv8803_get_time
,
484 .set_time
= rv8803_set_time
,
485 .ioctl
= rv8803_ioctl
,
488 static int rx8900_trickle_charger_init(struct rv8803_data
*rv8803
)
490 struct i2c_client
*client
= rv8803
->client
;
491 struct device_node
*node
= client
->dev
.of_node
;
498 if (rv8803
->type
!= rx_8900
)
501 err
= i2c_smbus_read_byte_data(rv8803
->client
, RX8900_BACKUP_CTRL
);
505 flags
= ~(RX8900_FLAG_VDETOFF
| RX8900_FLAG_SWOFF
) & (u8
)err
;
507 if (of_property_read_bool(node
, "epson,vdet-disable"))
508 flags
|= RX8900_FLAG_VDETOFF
;
510 if (of_property_read_bool(node
, "trickle-diode-disable"))
511 flags
|= RX8900_FLAG_SWOFF
;
513 return i2c_smbus_write_byte_data(rv8803
->client
, RX8900_BACKUP_CTRL
,
517 static int rv8803_probe(struct i2c_client
*client
,
518 const struct i2c_device_id
*id
)
520 struct i2c_adapter
*adapter
= client
->adapter
;
521 struct rv8803_data
*rv8803
;
523 struct nvmem_config nvmem_cfg
= {
524 .name
= "rv8803_nvram",
528 .reg_read
= rv8803_nvram_read
,
529 .reg_write
= rv8803_nvram_write
,
533 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
534 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
535 dev_err(&adapter
->dev
, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
539 rv8803
= devm_kzalloc(&client
->dev
, sizeof(struct rv8803_data
),
544 mutex_init(&rv8803
->flags_lock
);
545 rv8803
->client
= client
;
546 if (client
->dev
.of_node
)
547 rv8803
->type
= (enum rv8803_type
)
548 of_device_get_match_data(&client
->dev
);
550 rv8803
->type
= id
->driver_data
;
551 i2c_set_clientdata(client
, rv8803
);
553 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
557 if (flags
& RV8803_FLAG_V1F
)
558 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
560 if (flags
& RV8803_FLAG_V2F
)
561 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
563 if (flags
& RV8803_FLAG_AF
)
564 dev_warn(&client
->dev
, "An alarm maybe have been missed.\n");
566 rv8803
->rtc
= devm_rtc_allocate_device(&client
->dev
);
567 if (IS_ERR(rv8803
->rtc
))
568 return PTR_ERR(rv8803
->rtc
);
570 if (client
->irq
> 0) {
571 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
572 NULL
, rv8803_handle_irq
,
573 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
576 dev_warn(&client
->dev
, "unable to request IRQ, alarms disabled\n");
579 rv8803_rtc_ops
.read_alarm
= rv8803_get_alarm
;
580 rv8803_rtc_ops
.set_alarm
= rv8803_set_alarm
;
581 rv8803_rtc_ops
.alarm_irq_enable
= rv8803_alarm_irq_enable
;
585 err
= rv8803_write_reg(rv8803
->client
, RV8803_EXT
, RV8803_EXT_WADA
);
589 err
= rx8900_trickle_charger_init(rv8803
);
591 dev_err(&client
->dev
, "failed to init charger\n");
595 rv8803
->rtc
->ops
= &rv8803_rtc_ops
;
596 rv8803
->rtc
->nvram_old_abi
= true;
597 rv8803
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
598 rv8803
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
599 err
= rtc_register_device(rv8803
->rtc
);
603 rtc_nvmem_register(rv8803
->rtc
, &nvmem_cfg
);
605 rv8803
->rtc
->max_user_freq
= 1;
610 static const struct i2c_device_id rv8803_id
[] = {
611 { "rv8803", rv_8803
},
612 { "rx8803", rv_8803
},
613 { "rx8900", rx_8900
},
616 MODULE_DEVICE_TABLE(i2c
, rv8803_id
);
618 static const struct of_device_id rv8803_of_match
[] = {
620 .compatible
= "microcrystal,rv8803",
621 .data
= (void *)rv_8803
624 .compatible
= "epson,rx8803",
625 .data
= (void *)rv_8803
628 .compatible
= "epson,rx8900",
629 .data
= (void *)rx_8900
633 MODULE_DEVICE_TABLE(of
, rv8803_of_match
);
635 static struct i2c_driver rv8803_driver
= {
637 .name
= "rtc-rv8803",
638 .of_match_table
= of_match_ptr(rv8803_of_match
),
640 .probe
= rv8803_probe
,
641 .id_table
= rv8803_id
,
643 module_i2c_driver(rv8803_driver
);
645 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
646 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
647 MODULE_LICENSE("GPL v2");