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 spinlock_t 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 spin_lock(&rv8803->flags_lock);
68 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
70 spin_unlock(&rv8803->flags_lock);
74 if (flags & RV8803_FLAG_V1F)
75 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
77 if (flags & RV8803_FLAG_V2F)
78 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
80 if (flags & RV8803_FLAG_TF) {
81 flags &= ~RV8803_FLAG_TF;
82 rv8803->ctrl &= ~RV8803_CTRL_TIE;
86 if (flags & RV8803_FLAG_AF) {
87 flags &= ~RV8803_FLAG_AF;
88 rv8803->ctrl &= ~RV8803_CTRL_AIE;
92 if (flags & RV8803_FLAG_UF) {
93 flags &= ~RV8803_FLAG_UF;
94 rv8803->ctrl &= ~RV8803_CTRL_UIE;
99 rtc_update_irq(rv8803->rtc, 1, events);
100 i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
101 i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
105 spin_unlock(&rv8803->flags_lock);
110 static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
112 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
118 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
122 if (flags & RV8803_FLAG_V2F) {
123 dev_warn(dev, "Voltage low, data is invalid.\n");
127 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
130 return ret < 0 ? ret : -EIO;
132 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
133 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC,
136 return ret < 0 ? ret : -EIO;
138 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
142 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
143 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
144 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
145 tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f);
146 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
147 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
148 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
150 return rtc_valid_tm(tm);
153 static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
155 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
158 unsigned long irqflags;
160 if ((tm->tm_year < 100) || (tm->tm_year > 199))
163 date[RV8803_SEC] = bin2bcd(tm->tm_sec);
164 date[RV8803_MIN] = bin2bcd(tm->tm_min);
165 date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
166 date[RV8803_WEEK] = 1 << (tm->tm_wday);
167 date[RV8803_DAY] = bin2bcd(tm->tm_mday);
168 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
169 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
171 ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC,
176 spin_lock_irqsave(&rv8803->flags_lock, irqflags);
178 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG);
180 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
184 ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG,
185 flags & ~RV8803_FLAG_V2F);
187 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
192 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
194 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
195 struct i2c_client *client = rv8803->client;
199 ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN,
202 return ret < 0 ? ret : -EIO;
204 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
208 alrm->time.tm_sec = 0;
209 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
210 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
211 alrm->time.tm_wday = -1;
212 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
213 alrm->time.tm_mon = -1;
214 alrm->time.tm_year = -1;
216 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
217 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
222 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
224 struct i2c_client *client = to_i2c_client(dev);
225 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
229 unsigned long irqflags;
231 /* The alarm has no seconds, round up to nearest minute */
232 if (alrm->time.tm_sec) {
233 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
235 alarm_time += 60 - alrm->time.tm_sec;
236 rtc_time64_to_tm(alarm_time, &alrm->time);
239 spin_lock_irqsave(&rv8803->flags_lock, irqflags);
241 ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl);
243 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
244 return ret < 0 ? ret : -EIO;
247 alarmvals[0] = bin2bcd(alrm->time.tm_min);
248 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
249 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
251 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
252 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
253 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
256 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
261 ctrl[1] &= ~RV8803_FLAG_AF;
262 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]);
263 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
267 err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN,
273 if (rv8803->rtc->uie_rtctimer.enabled)
274 rv8803->ctrl |= RV8803_CTRL_UIE;
275 if (rv8803->rtc->aie_timer.enabled)
276 rv8803->ctrl |= RV8803_CTRL_AIE;
278 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL,
287 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
289 struct i2c_client *client = to_i2c_client(dev);
290 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
291 int ctrl, flags, err;
292 unsigned long irqflags;
297 if (rv8803->rtc->uie_rtctimer.enabled)
298 ctrl |= RV8803_CTRL_UIE;
299 if (rv8803->rtc->aie_timer.enabled)
300 ctrl |= RV8803_CTRL_AIE;
302 if (!rv8803->rtc->uie_rtctimer.enabled)
303 ctrl &= ~RV8803_CTRL_UIE;
304 if (!rv8803->rtc->aie_timer.enabled)
305 ctrl &= ~RV8803_CTRL_AIE;
308 spin_lock_irqsave(&rv8803->flags_lock, irqflags);
309 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
311 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
314 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
315 err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
316 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
320 if (ctrl != rv8803->ctrl) {
322 err = i2c_smbus_write_byte_data(client, RV8803_CTRL,
331 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
333 struct i2c_client *client = to_i2c_client(dev);
334 struct rv8803_data *rv8803 = dev_get_drvdata(dev);
336 unsigned long irqflags;
340 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
344 if (flags & RV8803_FLAG_V1F)
345 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
347 if (flags & RV8803_FLAG_V2F)
348 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
350 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F;
352 if (copy_to_user((void __user *)arg, &flags, sizeof(int)))
358 spin_lock_irqsave(&rv8803->flags_lock, irqflags);
359 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
361 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
365 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F);
366 ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags);
367 spin_unlock_irqrestore(&rv8803->flags_lock, irqflags);
378 static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj,
379 struct bin_attribute *attr,
380 char *buf, loff_t off, size_t count)
382 struct device *dev = kobj_to_dev(kobj);
383 struct i2c_client *client = to_i2c_client(dev);
386 ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]);
393 static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj,
394 struct bin_attribute *attr,
395 char *buf, loff_t off, size_t count)
397 struct device *dev = kobj_to_dev(kobj);
398 struct i2c_client *client = to_i2c_client(dev);
401 ret = i2c_smbus_read_byte_data(client, RV8803_RAM);
410 static struct bin_attribute rv8803_nvram_attr = {
413 .mode = S_IRUGO | S_IWUSR,
416 .read = rv8803_nvram_read,
417 .write = rv8803_nvram_write,
420 static struct rtc_class_ops rv8803_rtc_ops = {
421 .read_time = rv8803_get_time,
422 .set_time = rv8803_set_time,
423 .ioctl = rv8803_ioctl,
426 static int rv8803_probe(struct i2c_client *client,
427 const struct i2c_device_id *id)
429 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
430 struct rv8803_data *rv8803;
433 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
434 I2C_FUNC_SMBUS_I2C_BLOCK)) {
435 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
439 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
444 rv8803->client = client;
445 i2c_set_clientdata(client, rv8803);
447 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG);
451 if (flags & RV8803_FLAG_V1F)
452 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
454 if (flags & RV8803_FLAG_V2F)
455 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
457 if (flags & RV8803_FLAG_AF)
458 dev_warn(&client->dev, "An alarm maybe have been missed.\n");
460 if (client->irq > 0) {
461 err = devm_request_threaded_irq(&client->dev, client->irq,
462 NULL, rv8803_handle_irq,
463 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
466 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
469 rv8803_rtc_ops.read_alarm = rv8803_get_alarm;
470 rv8803_rtc_ops.set_alarm = rv8803_set_alarm;
471 rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable;
475 rv8803->rtc = devm_rtc_device_register(&client->dev, client->name,
476 &rv8803_rtc_ops, THIS_MODULE);
477 if (IS_ERR(rv8803->rtc)) {
478 dev_err(&client->dev, "unable to register the class device\n");
479 return PTR_ERR(rv8803->rtc);
482 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT,
487 err = device_create_bin_file(&client->dev, &rv8803_nvram_attr);
491 rv8803->rtc->max_user_freq = 1;
496 static int rv8803_remove(struct i2c_client *client)
498 device_remove_bin_file(&client->dev, &rv8803_nvram_attr);
503 static const struct i2c_device_id rv8803_id[] = {
508 MODULE_DEVICE_TABLE(i2c, rv8803_id);
510 static struct i2c_driver rv8803_driver = {
512 .name = "rtc-rv8803",
514 .probe = rv8803_probe,
515 .remove = rv8803_remove,
516 .id_table = rv8803_id,
518 module_i2c_driver(rv8803_driver);
520 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
521 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
522 MODULE_LICENSE("GPL v2");