2 * RTC driver for the Micro Crystal RV8803
4 * Copyright (C) 2015 Micro Crystal SA
6 * Alexandre Belloni <alexandre.belloni@free-electrons.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/bcd.h>
15 #include <linux/bitops.h>
16 #include <linux/log2.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/rtc.h>
23 #define RV8803_I2C_TRY_COUNT 4
25 #define RV8803_SEC 0x00
26 #define RV8803_MIN 0x01
27 #define RV8803_HOUR 0x02
28 #define RV8803_WEEK 0x03
29 #define RV8803_DAY 0x04
30 #define RV8803_MONTH 0x05
31 #define RV8803_YEAR 0x06
32 #define RV8803_RAM 0x07
33 #define RV8803_ALARM_MIN 0x08
34 #define RV8803_ALARM_HOUR 0x09
35 #define RV8803_ALARM_WEEK_OR_DAY 0x0A
36 #define RV8803_EXT 0x0D
37 #define RV8803_FLAG 0x0E
38 #define RV8803_CTRL 0x0F
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)
56 struct i2c_client
*client
;
57 struct rtc_device
*rtc
;
58 struct mutex flags_lock
;
62 static int rv8803_read_reg(const struct i2c_client
*client
, u8 reg
)
64 int try = RV8803_I2C_TRY_COUNT
;
68 * There is a 61µs window during which the RTC does not acknowledge I2C
69 * transfers. In that case, ensure that there are multiple attempts.
72 ret
= i2c_smbus_read_byte_data(client
, reg
);
73 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
75 dev_err(&client
->dev
, "Unable to read register 0x%02x\n", reg
);
80 static int rv8803_read_regs(const struct i2c_client
*client
,
81 u8 reg
, u8 count
, u8
*values
)
83 int try = RV8803_I2C_TRY_COUNT
;
87 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, count
, values
);
88 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
91 "Unable to read registers 0x%02x..0x%02x\n",
92 reg
, reg
+ count
- 1);
93 return ret
< 0 ? ret
: -EIO
;
99 static int rv8803_write_reg(const struct i2c_client
*client
, u8 reg
, u8 value
)
101 int try = RV8803_I2C_TRY_COUNT
;
105 ret
= i2c_smbus_write_byte_data(client
, reg
, value
);
106 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
108 dev_err(&client
->dev
, "Unable to write register 0x%02x\n", reg
);
113 static int rv8803_write_regs(const struct i2c_client
*client
,
114 u8 reg
, u8 count
, const u8
*values
)
116 int try = RV8803_I2C_TRY_COUNT
;
120 ret
= i2c_smbus_write_i2c_block_data(client
, reg
, count
,
122 while ((ret
== -ENXIO
|| ret
== -EIO
) && --try);
124 dev_err(&client
->dev
,
125 "Unable to write registers 0x%02x..0x%02x\n",
126 reg
, reg
+ count
- 1);
131 static irqreturn_t
rv8803_handle_irq(int irq
, void *dev_id
)
133 struct i2c_client
*client
= dev_id
;
134 struct rv8803_data
*rv8803
= i2c_get_clientdata(client
);
135 unsigned long events
= 0;
138 mutex_lock(&rv8803
->flags_lock
);
140 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
142 mutex_unlock(&rv8803
->flags_lock
);
146 if (flags
& RV8803_FLAG_V1F
)
147 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
149 if (flags
& RV8803_FLAG_V2F
)
150 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
152 if (flags
& RV8803_FLAG_TF
) {
153 flags
&= ~RV8803_FLAG_TF
;
154 rv8803
->ctrl
&= ~RV8803_CTRL_TIE
;
158 if (flags
& RV8803_FLAG_AF
) {
159 flags
&= ~RV8803_FLAG_AF
;
160 rv8803
->ctrl
&= ~RV8803_CTRL_AIE
;
164 if (flags
& RV8803_FLAG_UF
) {
165 flags
&= ~RV8803_FLAG_UF
;
166 rv8803
->ctrl
&= ~RV8803_CTRL_UIE
;
171 rtc_update_irq(rv8803
->rtc
, 1, events
);
172 rv8803_write_reg(client
, RV8803_FLAG
, flags
);
173 rv8803_write_reg(rv8803
->client
, RV8803_CTRL
, rv8803
->ctrl
);
176 mutex_unlock(&rv8803
->flags_lock
);
181 static int rv8803_get_time(struct device
*dev
, struct rtc_time
*tm
)
183 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
189 flags
= rv8803_read_reg(rv8803
->client
, RV8803_FLAG
);
193 if (flags
& RV8803_FLAG_V2F
) {
194 dev_warn(dev
, "Voltage low, data is invalid.\n");
198 ret
= rv8803_read_regs(rv8803
->client
, RV8803_SEC
, 7, date
);
202 if ((date1
[RV8803_SEC
] & 0x7f) == bin2bcd(59)) {
203 ret
= rv8803_read_regs(rv8803
->client
, RV8803_SEC
, 7, date2
);
207 if ((date2
[RV8803_SEC
] & 0x7f) != bin2bcd(59))
211 tm
->tm_sec
= bcd2bin(date
[RV8803_SEC
] & 0x7f);
212 tm
->tm_min
= bcd2bin(date
[RV8803_MIN
] & 0x7f);
213 tm
->tm_hour
= bcd2bin(date
[RV8803_HOUR
] & 0x3f);
214 tm
->tm_wday
= ilog2(date
[RV8803_WEEK
] & 0x7f);
215 tm
->tm_mday
= bcd2bin(date
[RV8803_DAY
] & 0x3f);
216 tm
->tm_mon
= bcd2bin(date
[RV8803_MONTH
] & 0x1f) - 1;
217 tm
->tm_year
= bcd2bin(date
[RV8803_YEAR
]) + 100;
222 static int rv8803_set_time(struct device
*dev
, struct rtc_time
*tm
)
224 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
226 int ctrl
, flags
, ret
;
228 if ((tm
->tm_year
< 100) || (tm
->tm_year
> 199))
231 ctrl
= rv8803_read_reg(rv8803
->client
, RV8803_CTRL
);
236 ret
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
237 ctrl
| RV8803_CTRL_RESET
);
241 date
[RV8803_SEC
] = bin2bcd(tm
->tm_sec
);
242 date
[RV8803_MIN
] = bin2bcd(tm
->tm_min
);
243 date
[RV8803_HOUR
] = bin2bcd(tm
->tm_hour
);
244 date
[RV8803_WEEK
] = 1 << (tm
->tm_wday
);
245 date
[RV8803_DAY
] = bin2bcd(tm
->tm_mday
);
246 date
[RV8803_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
247 date
[RV8803_YEAR
] = bin2bcd(tm
->tm_year
- 100);
249 ret
= rv8803_write_regs(rv8803
->client
, RV8803_SEC
, 7, date
);
253 /* Restart the clock */
254 ret
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
255 ctrl
& ~RV8803_CTRL_RESET
);
259 mutex_lock(&rv8803
->flags_lock
);
261 flags
= rv8803_read_reg(rv8803
->client
, RV8803_FLAG
);
263 mutex_unlock(&rv8803
->flags_lock
);
267 ret
= rv8803_write_reg(rv8803
->client
, RV8803_FLAG
,
268 flags
& ~(RV8803_FLAG_V1F
| RV8803_FLAG_V2F
));
270 mutex_unlock(&rv8803
->flags_lock
);
275 static int rv8803_get_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
277 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
278 struct i2c_client
*client
= rv8803
->client
;
282 ret
= rv8803_read_regs(client
, RV8803_ALARM_MIN
, 3, alarmvals
);
286 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
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
= !!(rv8803
->ctrl
& RV8803_CTRL_AIE
);
296 alrm
->pending
= (flags
& RV8803_FLAG_AF
) && alrm
->enabled
;
301 static int rv8803_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
303 struct i2c_client
*client
= to_i2c_client(dev
);
304 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
309 /* The alarm has no seconds, round up to nearest minute */
310 if (alrm
->time
.tm_sec
) {
311 time64_t alarm_time
= rtc_tm_to_time64(&alrm
->time
);
313 alarm_time
+= 60 - alrm
->time
.tm_sec
;
314 rtc_time64_to_tm(alarm_time
, &alrm
->time
);
317 mutex_lock(&rv8803
->flags_lock
);
319 ret
= rv8803_read_regs(client
, RV8803_FLAG
, 2, ctrl
);
321 mutex_unlock(&rv8803
->flags_lock
);
325 alarmvals
[0] = bin2bcd(alrm
->time
.tm_min
);
326 alarmvals
[1] = bin2bcd(alrm
->time
.tm_hour
);
327 alarmvals
[2] = bin2bcd(alrm
->time
.tm_mday
);
329 if (rv8803
->ctrl
& (RV8803_CTRL_AIE
| RV8803_CTRL_UIE
)) {
330 rv8803
->ctrl
&= ~(RV8803_CTRL_AIE
| RV8803_CTRL_UIE
);
331 err
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
334 mutex_unlock(&rv8803
->flags_lock
);
339 ctrl
[1] &= ~RV8803_FLAG_AF
;
340 err
= rv8803_write_reg(rv8803
->client
, RV8803_FLAG
, ctrl
[1]);
341 mutex_unlock(&rv8803
->flags_lock
);
345 err
= rv8803_write_regs(rv8803
->client
, RV8803_ALARM_MIN
, 3, alarmvals
);
350 if (rv8803
->rtc
->uie_rtctimer
.enabled
)
351 rv8803
->ctrl
|= RV8803_CTRL_UIE
;
352 if (rv8803
->rtc
->aie_timer
.enabled
)
353 rv8803
->ctrl
|= RV8803_CTRL_AIE
;
355 err
= rv8803_write_reg(rv8803
->client
, RV8803_CTRL
,
364 static int rv8803_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
366 struct i2c_client
*client
= to_i2c_client(dev
);
367 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
368 int ctrl
, flags
, err
;
373 if (rv8803
->rtc
->uie_rtctimer
.enabled
)
374 ctrl
|= RV8803_CTRL_UIE
;
375 if (rv8803
->rtc
->aie_timer
.enabled
)
376 ctrl
|= RV8803_CTRL_AIE
;
378 if (!rv8803
->rtc
->uie_rtctimer
.enabled
)
379 ctrl
&= ~RV8803_CTRL_UIE
;
380 if (!rv8803
->rtc
->aie_timer
.enabled
)
381 ctrl
&= ~RV8803_CTRL_AIE
;
384 mutex_lock(&rv8803
->flags_lock
);
385 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
387 mutex_unlock(&rv8803
->flags_lock
);
390 flags
&= ~(RV8803_FLAG_AF
| RV8803_FLAG_UF
);
391 err
= rv8803_write_reg(client
, RV8803_FLAG
, flags
);
392 mutex_unlock(&rv8803
->flags_lock
);
396 if (ctrl
!= rv8803
->ctrl
) {
398 err
= rv8803_write_reg(client
, RV8803_CTRL
, rv8803
->ctrl
);
406 static int rv8803_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
408 struct i2c_client
*client
= to_i2c_client(dev
);
409 struct rv8803_data
*rv8803
= dev_get_drvdata(dev
);
414 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
418 if (flags
& RV8803_FLAG_V1F
)
419 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
421 if (flags
& RV8803_FLAG_V2F
)
422 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
424 flags
&= RV8803_FLAG_V1F
| RV8803_FLAG_V2F
;
426 if (copy_to_user((void __user
*)arg
, &flags
, sizeof(int)))
432 mutex_lock(&rv8803
->flags_lock
);
433 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
435 mutex_unlock(&rv8803
->flags_lock
);
439 flags
&= ~(RV8803_FLAG_V1F
| RV8803_FLAG_V2F
);
440 ret
= rv8803_write_reg(client
, RV8803_FLAG
, flags
);
441 mutex_unlock(&rv8803
->flags_lock
);
452 static ssize_t
rv8803_nvram_write(struct file
*filp
, struct kobject
*kobj
,
453 struct bin_attribute
*attr
,
454 char *buf
, loff_t off
, size_t count
)
456 struct device
*dev
= kobj_to_dev(kobj
);
457 struct i2c_client
*client
= to_i2c_client(dev
);
460 ret
= rv8803_write_reg(client
, RV8803_RAM
, buf
[0]);
467 static ssize_t
rv8803_nvram_read(struct file
*filp
, struct kobject
*kobj
,
468 struct bin_attribute
*attr
,
469 char *buf
, loff_t off
, size_t count
)
471 struct device
*dev
= kobj_to_dev(kobj
);
472 struct i2c_client
*client
= to_i2c_client(dev
);
475 ret
= rv8803_read_reg(client
, RV8803_RAM
);
484 static struct bin_attribute rv8803_nvram_attr
= {
487 .mode
= S_IRUGO
| S_IWUSR
,
490 .read
= rv8803_nvram_read
,
491 .write
= rv8803_nvram_write
,
494 static struct rtc_class_ops rv8803_rtc_ops
= {
495 .read_time
= rv8803_get_time
,
496 .set_time
= rv8803_set_time
,
497 .ioctl
= rv8803_ioctl
,
500 static int rv8803_probe(struct i2c_client
*client
,
501 const struct i2c_device_id
*id
)
503 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
504 struct rv8803_data
*rv8803
;
507 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
|
508 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
509 dev_err(&adapter
->dev
, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
513 rv8803
= devm_kzalloc(&client
->dev
, sizeof(struct rv8803_data
),
518 mutex_init(&rv8803
->flags_lock
);
519 rv8803
->client
= client
;
520 i2c_set_clientdata(client
, rv8803
);
522 flags
= rv8803_read_reg(client
, RV8803_FLAG
);
526 if (flags
& RV8803_FLAG_V1F
)
527 dev_warn(&client
->dev
, "Voltage low, temperature compensation stopped.\n");
529 if (flags
& RV8803_FLAG_V2F
)
530 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
532 if (flags
& RV8803_FLAG_AF
)
533 dev_warn(&client
->dev
, "An alarm maybe have been missed.\n");
535 if (client
->irq
> 0) {
536 err
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
537 NULL
, rv8803_handle_irq
,
538 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
541 dev_warn(&client
->dev
, "unable to request IRQ, alarms disabled\n");
544 rv8803_rtc_ops
.read_alarm
= rv8803_get_alarm
;
545 rv8803_rtc_ops
.set_alarm
= rv8803_set_alarm
;
546 rv8803_rtc_ops
.alarm_irq_enable
= rv8803_alarm_irq_enable
;
550 rv8803
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
551 &rv8803_rtc_ops
, THIS_MODULE
);
552 if (IS_ERR(rv8803
->rtc
)) {
553 dev_err(&client
->dev
, "unable to register the class device\n");
554 return PTR_ERR(rv8803
->rtc
);
557 err
= rv8803_write_reg(rv8803
->client
, RV8803_EXT
, RV8803_EXT_WADA
);
561 err
= device_create_bin_file(&client
->dev
, &rv8803_nvram_attr
);
565 rv8803
->rtc
->max_user_freq
= 1;
570 static int rv8803_remove(struct i2c_client
*client
)
572 device_remove_bin_file(&client
->dev
, &rv8803_nvram_attr
);
577 static const struct i2c_device_id rv8803_id
[] = {
582 MODULE_DEVICE_TABLE(i2c
, rv8803_id
);
584 static struct i2c_driver rv8803_driver
= {
586 .name
= "rtc-rv8803",
588 .probe
= rv8803_probe
,
589 .remove
= rv8803_remove
,
590 .id_table
= rv8803_id
,
592 module_i2c_driver(rv8803_driver
);
594 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
595 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
596 MODULE_LICENSE("GPL v2");