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/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/rtc.h>
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)
53 struct i2c_client *client;
54 struct rtc_device *rtc;
55 struct mutex flags_lock;
59 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
61 struct i2c_client *client = dev_id;
62 struct rv8803_data *rv8803 = i2c_get_clientdata(client);
63 unsigned long events = 0;
66 mutex_lock(&rv8803->flags_lock);
69 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
71 } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
73 mutex_unlock(&rv8803->flags_lock);
77 if (flags & RV8803_FLAG_V1F)
78 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
80 if (flags & RV8803_FLAG_V2F)
81 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
83 if (flags & RV8803_FLAG_TF) {
84 flags &= ~RV8803_FLAG_TF;
85 rv8803->ctrl &= ~RV8803_CTRL_TIE;
89 if (flags & RV8803_FLAG_AF) {
90 flags &= ~RV8803_FLAG_AF;
91 rv8803->ctrl &= ~RV8803_CTRL_AIE;
95 if (flags & RV8803_FLAG_UF) {
96 flags &= ~RV8803_FLAG_UF;
97 rv8803->ctrl &= ~RV8803_CTRL_UIE;
102 rtc_update_irq(rv8803->rtc, 1, events);
103 i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
104 i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
108 mutex_unlock(&rv8803->flags_lock);
113 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
115 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
121 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
125 if (flags & RV8803_FLAG_V2F) {
126 dev_warn(dev, "Voltage low, data is invalid.\n");
130 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
133 return ret < 0 ? ret : -EIO;
135 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
136 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
139 return ret < 0 ? ret : -EIO;
141 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
145 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
146 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
147 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
148 tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
149 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
150 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
151 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
153 return rtc_valid_tm(tm);
156 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
158 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
162 if ((tm->tm_year < 100) || (tm->tm_year > 199))
165 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
166 date[RV8803_MIN] = bin2bcd(tm->tm_min);
167 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
168 date[RV8803_WEEK] = 1 << (tm->tm_wday);
169 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
170 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
171 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
173 ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
178 mutex_lock(&rv8803->flags_lock);
180 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
182 mutex_unlock(&rv8803->flags_lock);
186 ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
187 flags & ~RV8803_FLAG_V2F);
189 mutex_unlock(&rv8803->flags_lock);
194 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
197 struct i2c_client *client = rv8803->client;
201 ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
204 return ret < 0 ? ret : -EIO;
206 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
210 alrm->time.tm_sec = 0;
211 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
212 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
213 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
215 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
216 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
221 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
223 struct i2c_client *client = to_i2c_client(dev);
224 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
229 /* The alarm has no seconds, round up to nearest minute */
230 if (alrm->time.tm_sec) {
231 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
233 alarm_time += 60 - alrm->time.tm_sec;
234 rtc_time64_to_tm(alarm_time, &alrm->time);
237 mutex_lock(&rv8803->flags_lock);
239 ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
241 mutex_unlock(&rv8803->flags_lock);
242 return ret < 0 ? ret : -EIO;
245 alarmvals[0] = bin2bcd(alrm->time.tm_min);
246 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
247 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
249 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
250 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
251 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
254 mutex_unlock(&rv8803->flags_lock);
259 ctrl[1] &= ~RV8803_FLAG_AF;
260 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
261 mutex_unlock(&rv8803->flags_lock);
265 err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
271 if (rv8803->rtc->uie_rtctimer.enabled)
272 rv8803->ctrl |= RV8803_CTRL_UIE;
273 if (rv8803->rtc->aie_timer.enabled)
274 rv8803->ctrl |= RV8803_CTRL_AIE;
276 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
285 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
287 struct i2c_client *client = to_i2c_client(dev);
288 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
289 int ctrl, flags, err;
294 if (rv8803->rtc->uie_rtctimer.enabled)
295 ctrl |= RV8803_CTRL_UIE;
296 if (rv8803->rtc->aie_timer.enabled)
297 ctrl |= RV8803_CTRL_AIE;
299 if (!rv8803->rtc->uie_rtctimer.enabled)
300 ctrl &= ~RV8803_CTRL_UIE;
301 if (!rv8803->rtc->aie_timer.enabled)
302 ctrl &= ~RV8803_CTRL_AIE;
305 mutex_lock(&rv8803->flags_lock);
306 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
308 mutex_unlock(&rv8803->flags_lock);
311 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
312 err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
313 mutex_unlock(&rv8803->flags_lock);
317 if (ctrl != rv8803->ctrl) {
319 err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
328 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
330 struct i2c_client *client = to_i2c_client(dev);
331 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
336 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
340 if (flags & RV8803_FLAG_V1F)
341 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
343 if (flags & RV8803_FLAG_V2F)
344 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
346 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
348 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
354 mutex_lock(&rv8803->flags_lock);
355 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
357 mutex_unlock(&rv8803->flags_lock);
361 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
362 ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
363 mutex_unlock(&rv8803->flags_lock);
374 static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
375 struct bin_attribute *attr,
376 char *buf, loff_t off, size_t count)
378 struct device *dev = kobj_to_dev(kobj);
379 struct i2c_client *client = to_i2c_client(dev);
382 ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
389 static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
390 struct bin_attribute *attr,
391 char *buf, loff_t off, size_t count)
393 struct device *dev = kobj_to_dev(kobj);
394 struct i2c_client *client = to_i2c_client(dev);
397 ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
406 static struct bin_attribute rv8803_nvram_attr = {
409 .mode = S_IRUGO | S_IWUSR,
412 .read = rv8803_nvram_read,
413 .write = rv8803_nvram_write,
416 static struct rtc_class_ops rv8803_rtc_ops = {
417 .read_time = rv8803_get_time,
418 .set_time = rv8803_set_time,
419 .ioctl = rv8803_ioctl,
422 static int rv8803_probe(struct i2c_client *client,
423 const struct i2c_device_id *id)
425 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
426 struct rv8803_data *rv8803;
427 int err, flags, try = 0;
429 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
430 I2C_FUNC_SMBUS_I2C_BLOCK)) {
431 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
435 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
440 mutex_init(&rv8803->flags_lock);
441 rv8803->client = client;
442 i2c_set_clientdata(client, rv8803);
445 * There is a 60µs window where the RTC may not reply on the i2c bus in
446 * that case, the transfer is not ACKed. In that case, ensure there are
450 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
452 } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4));
457 if (flags & RV8803_FLAG_V1F)
458 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
460 if (flags & RV8803_FLAG_V2F)
461 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
463 if (flags & RV8803_FLAG_AF)
464 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
466 if (client->irq > 0) {
467 err = devm_request_threaded_irq(&client->dev, client->irq,
468 NULL, rv8803_handle_irq,
469 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
472 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
475 rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
476 rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
477 rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
481 rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
482 &rv8803_rtc_ops, THIS_MODULE);
483 if (IS_ERR(rv8803->rtc)) {
484 dev_err(&client->dev, "unable to register the class device\n");
485 return PTR_ERR(rv8803->rtc);
490 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
493 } while (((err == -ENXIO) || (flags == -EIO)) && (try < 4));
497 err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
501 rv8803->rtc->max_user_freq = 1;
506 static int rv8803_remove(struct i2c_client *client)
508 device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
513 static const struct i2c_device_id rv8803_id[] = {
518 MODULE_DEVICE_TABLE(i2c, rv8803_id);
520 static struct i2c_driver rv8803_driver = {
522 .name = "rtc-rv8803",
524 .probe = rv8803_probe,
525 .remove = rv8803_remove,
526 .id_table = rv8803_id,
528 module_i2c_driver(rv8803_driver);
530 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
531 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
532 MODULE_LICENSE("GPL v2");