2 * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
7 * Copyright (C) 2014 Rose Technology
8 * Copyright (C) 2006-2007 Freescale Semiconductor
10 * 2005 (c) MontaVista Software, Inc. This file is licensed under
11 * the terms of the GNU General Public License version 2. This program
12 * is licensed "as is" without any warranty of any kind, whether express
16 * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
17 * recommended in .../Documentation/i2c/writing-clients.rst section
18 * "Sending and receiving", using SMBus level communication is preferred.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/i2c.h>
27 #include <linux/rtc.h>
28 #include <linux/bcd.h>
29 #include <linux/workqueue.h>
30 #include <linux/slab.h>
32 #ifdef CONFIG_RTC_DRV_DS1374_WDT
34 #include <linux/ioctl.h>
35 #include <linux/miscdevice.h>
36 #include <linux/reboot.h>
37 #include <linux/watchdog.h>
40 #define DS1374_REG_TOD0 0x00 /* Time of Day */
41 #define DS1374_REG_TOD1 0x01
42 #define DS1374_REG_TOD2 0x02
43 #define DS1374_REG_TOD3 0x03
44 #define DS1374_REG_WDALM0 0x04 /* Watchdog/Alarm */
45 #define DS1374_REG_WDALM1 0x05
46 #define DS1374_REG_WDALM2 0x06
47 #define DS1374_REG_CR 0x07 /* Control */
48 #define DS1374_REG_CR_AIE 0x01 /* Alarm Int. Enable */
49 #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
50 #define DS1374_REG_CR_WACE 0x40 /* WD/Alarm counter enable */
51 #define DS1374_REG_SR 0x08 /* Status */
52 #define DS1374_REG_SR_OSF 0x80 /* Oscillator Stop Flag */
53 #define DS1374_REG_SR_AF 0x01 /* Alarm Flag */
54 #define DS1374_REG_TCR 0x09 /* Trickle Charge */
56 static const struct i2c_device_id ds1374_id[] = {
60 MODULE_DEVICE_TABLE(i2c, ds1374_id);
63 static const struct of_device_id ds1374_of_match[] = {
64 { .compatible = "dallas,ds1374" },
67 MODULE_DEVICE_TABLE(of, ds1374_of_match);
71 struct i2c_client *client;
72 struct rtc_device *rtc;
73 struct work_struct work;
75 /* The mutex protects alarm operations, and prevents a race
76 * between the enable_irq() in the workqueue and the free_irq()
77 * in the remove function.
83 static struct i2c_driver ds1374_driver;
85 static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
92 if (WARN_ON(nbytes > 4))
95 ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
102 for (i = nbytes - 1, *time = 0; i >= 0; i--)
103 *time = (*time << 8) | buf[i];
108 static int ds1374_write_rtc(struct i2c_client *client, u32 time,
119 for (i = 0; i < nbytes; i++) {
120 buf[i] = time & 0xff;
124 return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
127 static int ds1374_check_rtc_status(struct i2c_client *client)
132 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
136 if (stat & DS1374_REG_SR_OSF)
137 dev_warn(&client->dev,
138 "oscillator discontinuity flagged, time unreliable\n");
140 stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
142 ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
146 /* If the alarm is pending, clear it before requesting
147 * the interrupt, so an interrupt event isn't reported
148 * before everything is initialized.
151 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
155 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
156 return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
159 static int ds1374_read_time(struct device *dev, struct rtc_time *time)
161 struct i2c_client *client = to_i2c_client(dev);
165 ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
167 rtc_time64_to_tm(itime, time);
172 static int ds1374_set_time(struct device *dev, struct rtc_time *time)
174 struct i2c_client *client = to_i2c_client(dev);
175 unsigned long itime = rtc_tm_to_time64(time);
177 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
180 #ifndef CONFIG_RTC_DRV_DS1374_WDT
181 /* The ds1374 has a decrementer for an alarm, rather than a comparator.
182 * If the time of day is changed, then the alarm will need to be
185 static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
187 struct i2c_client *client = to_i2c_client(dev);
188 struct ds1374 *ds1374 = i2c_get_clientdata(client);
193 if (client->irq <= 0)
196 mutex_lock(&ds1374->mutex);
198 cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
202 sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
206 ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
210 ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
214 rtc_time64_to_tm(now + cur_alarm, &alarm->time);
215 alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
216 alarm->pending = !!(sr & DS1374_REG_SR_AF);
219 mutex_unlock(&ds1374->mutex);
223 static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
225 struct i2c_client *client = to_i2c_client(dev);
226 struct ds1374 *ds1374 = i2c_get_clientdata(client);
228 unsigned long new_alarm, itime;
232 if (client->irq <= 0)
235 ret = ds1374_read_time(dev, &now);
239 new_alarm = rtc_tm_to_time64(&alarm->time);
240 itime = rtc_tm_to_time64(&now);
242 /* This can happen due to races, in addition to dates that are
243 * truly in the past. To avoid requiring the caller to check for
244 * races, dates in the past are assumed to be in the recent past
245 * (i.e. not something that we'd rather the caller know about via
246 * an error), and the alarm is set to go off as soon as possible.
248 if (time_before_eq(new_alarm, itime))
253 mutex_lock(&ds1374->mutex);
255 ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
259 /* Disable any existing alarm before setting the new one
260 * (or lack thereof). */
261 cr &= ~DS1374_REG_CR_WACE;
263 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
267 ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
271 if (alarm->enabled) {
272 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
273 cr &= ~DS1374_REG_CR_WDALM;
275 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
279 mutex_unlock(&ds1374->mutex);
284 static irqreturn_t ds1374_irq(int irq, void *dev_id)
286 struct i2c_client *client = dev_id;
287 struct ds1374 *ds1374 = i2c_get_clientdata(client);
289 disable_irq_nosync(irq);
290 schedule_work(&ds1374->work);
294 static void ds1374_work(struct work_struct *work)
296 struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
297 struct i2c_client *client = ds1374->client;
300 mutex_lock(&ds1374->mutex);
302 stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
306 if (stat & DS1374_REG_SR_AF) {
307 stat &= ~DS1374_REG_SR_AF;
308 i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
310 control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
314 control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
315 i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
317 rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
321 if (!ds1374->exiting)
322 enable_irq(client->irq);
324 mutex_unlock(&ds1374->mutex);
327 #ifndef CONFIG_RTC_DRV_DS1374_WDT
328 static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
330 struct i2c_client *client = to_i2c_client(dev);
331 struct ds1374 *ds1374 = i2c_get_clientdata(client);
334 mutex_lock(&ds1374->mutex);
336 ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
341 ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
342 ret &= ~DS1374_REG_CR_WDALM;
344 ret &= ~DS1374_REG_CR_WACE;
346 ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
349 mutex_unlock(&ds1374->mutex);
354 static const struct rtc_class_ops ds1374_rtc_ops = {
355 .read_time = ds1374_read_time,
356 .set_time = ds1374_set_time,
357 #ifndef CONFIG_RTC_DRV_DS1374_WDT
358 .read_alarm = ds1374_read_alarm,
359 .set_alarm = ds1374_set_alarm,
360 .alarm_irq_enable = ds1374_alarm_irq_enable,
364 #ifdef CONFIG_RTC_DRV_DS1374_WDT
366 *****************************************************************************
370 *****************************************************************************
372 static struct i2c_client *save_client;
374 #define WD_TIMO 131762
376 #define DRV_NAME "DS1374 Watchdog"
378 static int wdt_margin = WD_TIMO;
379 static unsigned long wdt_is_open;
380 module_param(wdt_margin, int, 0);
381 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
383 static const struct watchdog_info ds1374_wdt_info = {
384 .identity = "DS1374 WTD",
385 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
389 static int ds1374_wdt_settimeout(unsigned int timeout)
391 int ret = -ENOIOCTLCMD;
394 ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
398 /* Disable any existing watchdog/alarm before setting the new one */
399 cr &= ~DS1374_REG_CR_WACE;
401 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
405 /* Set new watchdog time */
406 ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
408 pr_info("couldn't set new watchdog time\n");
412 /* Enable watchdog timer */
413 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
414 cr &= ~DS1374_REG_CR_AIE;
416 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
427 * Reload the watchdog timer. (ie, pat the watchdog)
429 static void ds1374_wdt_ping(void)
434 ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
436 pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
439 static void ds1374_wdt_disable(void)
443 cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
444 /* Disable watchdog timer */
445 cr &= ~DS1374_REG_CR_WACE;
447 i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
451 * Watchdog device is opened, and watchdog starts running.
453 static int ds1374_wdt_open(struct inode *inode, struct file *file)
455 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
457 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
458 mutex_lock(&ds1374->mutex);
459 if (test_and_set_bit(0, &wdt_is_open)) {
460 mutex_unlock(&ds1374->mutex);
467 mutex_unlock(&ds1374->mutex);
468 return stream_open(inode, file);
474 * Close the watchdog device.
476 static int ds1374_wdt_release(struct inode *inode, struct file *file)
478 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
479 clear_bit(0, &wdt_is_open);
485 * Pat the watchdog whenever device is written to.
487 static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
488 size_t len, loff_t *ppos)
497 static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
498 size_t len, loff_t *ppos)
504 * Handle commands from user-space.
506 static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
509 int new_margin, options;
512 case WDIOC_GETSUPPORT:
513 return copy_to_user((struct watchdog_info __user *)arg,
514 &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
516 case WDIOC_GETSTATUS:
517 case WDIOC_GETBOOTSTATUS:
518 return put_user(0, (int __user *)arg);
519 case WDIOC_KEEPALIVE:
522 case WDIOC_SETTIMEOUT:
523 if (get_user(new_margin, (int __user *)arg))
526 /* the hardware's tick rate is 4096 Hz, so
527 * the counter value needs to be scaled accordingly
530 if (new_margin < 1 || new_margin > 16777216)
533 wdt_margin = new_margin;
534 ds1374_wdt_settimeout(new_margin);
537 case WDIOC_GETTIMEOUT:
538 /* when returning ... inverse is true */
539 return put_user((wdt_margin >> 12), (int __user *)arg);
540 case WDIOC_SETOPTIONS:
541 if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
544 if (options & WDIOS_DISABLECARD) {
545 pr_info("disable watchdog\n");
546 ds1374_wdt_disable();
550 if (options & WDIOS_ENABLECARD) {
551 pr_info("enable watchdog\n");
552 ds1374_wdt_settimeout(wdt_margin);
561 static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
565 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
567 mutex_lock(&ds1374->mutex);
568 ret = ds1374_wdt_ioctl(file, cmd, arg);
569 mutex_unlock(&ds1374->mutex);
574 static int ds1374_wdt_notify_sys(struct notifier_block *this,
575 unsigned long code, void *unused)
577 if (code == SYS_DOWN || code == SYS_HALT)
578 /* Disable Watchdog */
579 ds1374_wdt_disable();
583 static const struct file_operations ds1374_wdt_fops = {
584 .owner = THIS_MODULE,
585 .read = ds1374_wdt_read,
586 .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
587 .compat_ioctl = compat_ptr_ioctl,
588 .write = ds1374_wdt_write,
589 .open = ds1374_wdt_open,
590 .release = ds1374_wdt_release,
594 static struct miscdevice ds1374_miscdev = {
595 .minor = WATCHDOG_MINOR,
597 .fops = &ds1374_wdt_fops,
600 static struct notifier_block ds1374_wdt_notifier = {
601 .notifier_call = ds1374_wdt_notify_sys,
604 #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
606 *****************************************************************************
610 *****************************************************************************
612 static int ds1374_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
615 struct ds1374 *ds1374;
618 ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
622 ds1374->rtc = devm_rtc_allocate_device(&client->dev);
623 if (IS_ERR(ds1374->rtc))
624 return PTR_ERR(ds1374->rtc);
626 ds1374->client = client;
627 i2c_set_clientdata(client, ds1374);
629 INIT_WORK(&ds1374->work, ds1374_work);
630 mutex_init(&ds1374->mutex);
632 ret = ds1374_check_rtc_status(client);
636 if (client->irq > 0) {
637 ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
640 dev_err(&client->dev, "unable to request IRQ\n");
644 device_set_wakeup_capable(&client->dev, 1);
647 ds1374->rtc->ops = &ds1374_rtc_ops;
648 ds1374->rtc->range_max = U32_MAX;
650 ret = rtc_register_device(ds1374->rtc);
654 #ifdef CONFIG_RTC_DRV_DS1374_WDT
655 save_client = client;
656 ret = misc_register(&ds1374_miscdev);
659 ret = register_reboot_notifier(&ds1374_wdt_notifier);
661 misc_deregister(&ds1374_miscdev);
664 ds1374_wdt_settimeout(131072);
670 static int ds1374_remove(struct i2c_client *client)
672 struct ds1374 *ds1374 = i2c_get_clientdata(client);
673 #ifdef CONFIG_RTC_DRV_DS1374_WDT
674 misc_deregister(&ds1374_miscdev);
675 ds1374_miscdev.parent = NULL;
676 unregister_reboot_notifier(&ds1374_wdt_notifier);
679 if (client->irq > 0) {
680 mutex_lock(&ds1374->mutex);
682 mutex_unlock(&ds1374->mutex);
684 devm_free_irq(&client->dev, client->irq, client);
685 cancel_work_sync(&ds1374->work);
691 #ifdef CONFIG_PM_SLEEP
692 static int ds1374_suspend(struct device *dev)
694 struct i2c_client *client = to_i2c_client(dev);
696 if (client->irq > 0 && device_may_wakeup(&client->dev))
697 enable_irq_wake(client->irq);
701 static int ds1374_resume(struct device *dev)
703 struct i2c_client *client = to_i2c_client(dev);
705 if (client->irq > 0 && device_may_wakeup(&client->dev))
706 disable_irq_wake(client->irq);
711 static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
713 static struct i2c_driver ds1374_driver = {
715 .name = "rtc-ds1374",
716 .of_match_table = of_match_ptr(ds1374_of_match),
719 .probe = ds1374_probe,
720 .remove = ds1374_remove,
721 .id_table = ds1374_id,
724 module_i2c_driver(ds1374_driver);
727 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
728 MODULE_LICENSE("GPL");