aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/rtc-ds1374.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/rtc-ds1374.c')
-rw-r--r--drivers/rtc/rtc-ds1374.c285
1 files changed, 285 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-ds1374.c b/drivers/rtc/rtc-ds1374.c
index 9e6e14fb53d7..8605fde394b2 100644
--- a/drivers/rtc/rtc-ds1374.c
+++ b/drivers/rtc/rtc-ds1374.c
@@ -4,6 +4,7 @@
4 * Based on code by Randy Vinson <rvinson@mvista.com>, 4 * Based on code by Randy Vinson <rvinson@mvista.com>,
5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>. 5 * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
6 * 6 *
7 * Copyright (C) 2014 Rose Technology
7 * Copyright (C) 2006-2007 Freescale Semiconductor 8 * Copyright (C) 2006-2007 Freescale Semiconductor
8 * 9 *
9 * 2005 (c) MontaVista Software, Inc. This file is licensed under 10 * 2005 (c) MontaVista Software, Inc. This file is licensed under
@@ -26,6 +27,13 @@
26#include <linux/workqueue.h> 27#include <linux/workqueue.h>
27#include <linux/slab.h> 28#include <linux/slab.h>
28#include <linux/pm.h> 29#include <linux/pm.h>
30#ifdef CONFIG_RTC_DRV_DS1374_WDT
31#include <linux/fs.h>
32#include <linux/ioctl.h>
33#include <linux/miscdevice.h>
34#include <linux/reboot.h>
35#include <linux/watchdog.h>
36#endif
29 37
30#define DS1374_REG_TOD0 0x00 /* Time of Day */ 38#define DS1374_REG_TOD0 0x00 /* Time of Day */
31#define DS1374_REG_TOD1 0x01 39#define DS1374_REG_TOD1 0x01
@@ -49,6 +57,14 @@ static const struct i2c_device_id ds1374_id[] = {
49}; 57};
50MODULE_DEVICE_TABLE(i2c, ds1374_id); 58MODULE_DEVICE_TABLE(i2c, ds1374_id);
51 59
60#ifdef CONFIG_OF
61static const struct of_device_id ds1374_of_match[] = {
62 { .compatible = "dallas,ds1374" },
63 { }
64};
65MODULE_DEVICE_TABLE(of, ds1374_of_match);
66#endif
67
52struct ds1374 { 68struct ds1374 {
53 struct i2c_client *client; 69 struct i2c_client *client;
54 struct rtc_device *rtc; 70 struct rtc_device *rtc;
@@ -162,6 +178,7 @@ static int ds1374_set_time(struct device *dev, struct rtc_time *time)
162 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4); 178 return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
163} 179}
164 180
181#ifndef CONFIG_RTC_DRV_DS1374_WDT
165/* The ds1374 has a decrementer for an alarm, rather than a comparator. 182/* The ds1374 has a decrementer for an alarm, rather than a comparator.
166 * If the time of day is changed, then the alarm will need to be 183 * If the time of day is changed, then the alarm will need to be
167 * reset. 184 * reset.
@@ -263,6 +280,7 @@ out:
263 mutex_unlock(&ds1374->mutex); 280 mutex_unlock(&ds1374->mutex);
264 return ret; 281 return ret;
265} 282}
283#endif
266 284
267static irqreturn_t ds1374_irq(int irq, void *dev_id) 285static irqreturn_t ds1374_irq(int irq, void *dev_id)
268{ 286{
@@ -307,6 +325,7 @@ unlock:
307 mutex_unlock(&ds1374->mutex); 325 mutex_unlock(&ds1374->mutex);
308} 326}
309 327
328#ifndef CONFIG_RTC_DRV_DS1374_WDT
310static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled) 329static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
311{ 330{
312 struct i2c_client *client = to_i2c_client(dev); 331 struct i2c_client *client = to_i2c_client(dev);
@@ -331,15 +350,260 @@ out:
331 mutex_unlock(&ds1374->mutex); 350 mutex_unlock(&ds1374->mutex);
332 return ret; 351 return ret;
333} 352}
353#endif
334 354
335static const struct rtc_class_ops ds1374_rtc_ops = { 355static const struct rtc_class_ops ds1374_rtc_ops = {
336 .read_time = ds1374_read_time, 356 .read_time = ds1374_read_time,
337 .set_time = ds1374_set_time, 357 .set_time = ds1374_set_time,
358#ifndef CONFIG_RTC_DRV_DS1374_WDT
338 .read_alarm = ds1374_read_alarm, 359 .read_alarm = ds1374_read_alarm,
339 .set_alarm = ds1374_set_alarm, 360 .set_alarm = ds1374_set_alarm,
340 .alarm_irq_enable = ds1374_alarm_irq_enable, 361 .alarm_irq_enable = ds1374_alarm_irq_enable,
362#endif
363};
364
365#ifdef CONFIG_RTC_DRV_DS1374_WDT
366/*
367 *****************************************************************************
368 *
369 * Watchdog Driver
370 *
371 *****************************************************************************
372 */
373static struct i2c_client *save_client;
374/* Default margin */
375#define WD_TIMO 131762
376
377#define DRV_NAME "DS1374 Watchdog"
378
379static int wdt_margin = WD_TIMO;
380static unsigned long wdt_is_open;
381module_param(wdt_margin, int, 0);
382MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
383
384static const struct watchdog_info ds1374_wdt_info = {
385 .identity = "DS1374 WTD",
386 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
387 WDIOF_MAGICCLOSE,
341}; 388};
342 389
390static int ds1374_wdt_settimeout(unsigned int timeout)
391{
392 int ret = -ENOIOCTLCMD;
393 int cr;
394
395 ret = cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
396 if (ret < 0)
397 goto out;
398
399 /* Disable any existing watchdog/alarm before setting the new one */
400 cr &= ~DS1374_REG_CR_WACE;
401
402 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
403 if (ret < 0)
404 goto out;
405
406 /* Set new watchdog time */
407 ret = ds1374_write_rtc(save_client, timeout, DS1374_REG_WDALM0, 3);
408 if (ret) {
409 pr_info("rtc-ds1374 - couldn't set new watchdog time\n");
410 goto out;
411 }
412
413 /* Enable watchdog timer */
414 cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
415 cr &= ~DS1374_REG_CR_AIE;
416
417 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
418 if (ret < 0)
419 goto out;
420
421 return 0;
422out:
423 return ret;
424}
425
426
427/*
428 * Reload the watchdog timer. (ie, pat the watchdog)
429 */
430static void ds1374_wdt_ping(void)
431{
432 u32 val;
433 int ret = 0;
434
435 ret = ds1374_read_rtc(save_client, &val, DS1374_REG_WDALM0, 3);
436 if (ret)
437 pr_info("WD TICK FAIL!!!!!!!!!! %i\n", ret);
438}
439
440static void ds1374_wdt_disable(void)
441{
442 int ret = -ENOIOCTLCMD;
443 int cr;
444
445 cr = i2c_smbus_read_byte_data(save_client, DS1374_REG_CR);
446 /* Disable watchdog timer */
447 cr &= ~DS1374_REG_CR_WACE;
448
449 ret = i2c_smbus_write_byte_data(save_client, DS1374_REG_CR, cr);
450}
451
452/*
453 * Watchdog device is opened, and watchdog starts running.
454 */
455static int ds1374_wdt_open(struct inode *inode, struct file *file)
456{
457 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
458
459 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
460 mutex_lock(&ds1374->mutex);
461 if (test_and_set_bit(0, &wdt_is_open)) {
462 mutex_unlock(&ds1374->mutex);
463 return -EBUSY;
464 }
465 /*
466 * Activate
467 */
468 wdt_is_open = 1;
469 mutex_unlock(&ds1374->mutex);
470 return nonseekable_open(inode, file);
471 }
472 return -ENODEV;
473}
474
475/*
476 * Close the watchdog device.
477 */
478static int ds1374_wdt_release(struct inode *inode, struct file *file)
479{
480 if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
481 clear_bit(0, &wdt_is_open);
482
483 return 0;
484}
485
486/*
487 * Pat the watchdog whenever device is written to.
488 */
489static ssize_t ds1374_wdt_write(struct file *file, const char __user *data,
490 size_t len, loff_t *ppos)
491{
492 if (len) {
493 ds1374_wdt_ping();
494 return 1;
495 }
496 return 0;
497}
498
499static ssize_t ds1374_wdt_read(struct file *file, char __user *data,
500 size_t len, loff_t *ppos)
501{
502 return 0;
503}
504
505/*
506 * Handle commands from user-space.
507 */
508static long ds1374_wdt_ioctl(struct file *file, unsigned int cmd,
509 unsigned long arg)
510{
511 int new_margin, options;
512
513 switch (cmd) {
514 case WDIOC_GETSUPPORT:
515 return copy_to_user((struct watchdog_info __user *)arg,
516 &ds1374_wdt_info, sizeof(ds1374_wdt_info)) ? -EFAULT : 0;
517
518 case WDIOC_GETSTATUS:
519 case WDIOC_GETBOOTSTATUS:
520 return put_user(0, (int __user *)arg);
521 case WDIOC_KEEPALIVE:
522 ds1374_wdt_ping();
523 return 0;
524 case WDIOC_SETTIMEOUT:
525 if (get_user(new_margin, (int __user *)arg))
526 return -EFAULT;
527
528 if (new_margin < 1 || new_margin > 16777216)
529 return -EINVAL;
530
531 wdt_margin = new_margin;
532 ds1374_wdt_settimeout(new_margin);
533 ds1374_wdt_ping();
534 /* fallthrough */
535 case WDIOC_GETTIMEOUT:
536 return put_user(wdt_margin, (int __user *)arg);
537 case WDIOC_SETOPTIONS:
538 if (copy_from_user(&options, (int __user *)arg, sizeof(int)))
539 return -EFAULT;
540
541 if (options & WDIOS_DISABLECARD) {
542 pr_info("rtc-ds1374: disable watchdog\n");
543 ds1374_wdt_disable();
544 }
545
546 if (options & WDIOS_ENABLECARD) {
547 pr_info("rtc-ds1374: enable watchdog\n");
548 ds1374_wdt_settimeout(wdt_margin);
549 ds1374_wdt_ping();
550 }
551
552 return -EINVAL;
553 }
554 return -ENOTTY;
555}
556
557static long ds1374_wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
558 unsigned long arg)
559{
560 int ret;
561 struct ds1374 *ds1374 = i2c_get_clientdata(save_client);
562
563 mutex_lock(&ds1374->mutex);
564 ret = ds1374_wdt_ioctl(file, cmd, arg);
565 mutex_unlock(&ds1374->mutex);
566
567 return ret;
568}
569
570static int ds1374_wdt_notify_sys(struct notifier_block *this,
571 unsigned long code, void *unused)
572{
573 if (code == SYS_DOWN || code == SYS_HALT)
574 /* Disable Watchdog */
575 ds1374_wdt_disable();
576 return NOTIFY_DONE;
577}
578
579static const struct file_operations ds1374_wdt_fops = {
580 .owner = THIS_MODULE,
581 .read = ds1374_wdt_read,
582 .unlocked_ioctl = ds1374_wdt_unlocked_ioctl,
583 .write = ds1374_wdt_write,
584 .open = ds1374_wdt_open,
585 .release = ds1374_wdt_release,
586 .llseek = no_llseek,
587};
588
589static struct miscdevice ds1374_miscdev = {
590 .minor = WATCHDOG_MINOR,
591 .name = "watchdog",
592 .fops = &ds1374_wdt_fops,
593};
594
595static struct notifier_block ds1374_wdt_notifier = {
596 .notifier_call = ds1374_wdt_notify_sys,
597};
598
599#endif /*CONFIG_RTC_DRV_DS1374_WDT*/
600/*
601 *****************************************************************************
602 *
603 * Driver Interface
604 *
605 *****************************************************************************
606 */
343static int ds1374_probe(struct i2c_client *client, 607static int ds1374_probe(struct i2c_client *client,
344 const struct i2c_device_id *id) 608 const struct i2c_device_id *id)
345{ 609{
@@ -378,12 +642,33 @@ static int ds1374_probe(struct i2c_client *client,
378 return PTR_ERR(ds1374->rtc); 642 return PTR_ERR(ds1374->rtc);
379 } 643 }
380 644
645#ifdef CONFIG_RTC_DRV_DS1374_WDT
646 save_client = client;
647 ret = misc_register(&ds1374_miscdev);
648 if (ret)
649 return ret;
650 ret = register_reboot_notifier(&ds1374_wdt_notifier);
651 if (ret) {
652 misc_deregister(&ds1374_miscdev);
653 return ret;
654 }
655 ds1374_wdt_settimeout(131072);
656#endif
657
381 return 0; 658 return 0;
382} 659}
383 660
384static int ds1374_remove(struct i2c_client *client) 661static int ds1374_remove(struct i2c_client *client)
385{ 662{
386 struct ds1374 *ds1374 = i2c_get_clientdata(client); 663 struct ds1374 *ds1374 = i2c_get_clientdata(client);
664#ifdef CONFIG_RTC_DRV_DS1374_WDT
665 int res;
666
667 res = misc_deregister(&ds1374_miscdev);
668 if (!res)
669 ds1374_miscdev.parent = NULL;
670 unregister_reboot_notifier(&ds1374_wdt_notifier);
671#endif
387 672
388 if (client->irq > 0) { 673 if (client->irq > 0) {
389 mutex_lock(&ds1374->mutex); 674 mutex_lock(&ds1374->mutex);