stmp3xxx_rtc_wdt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28
  3. *
  4. * Author: Wolfram Sang <kernel@pengutronix.de>
  5. *
  6. * Copyright (C) 2011-12 Wolfram Sang, Pengutronix
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/stmp3xxx_rtc_wdt.h>
  17. #include <linux/notifier.h>
  18. #include <linux/reboot.h>
  19. #define WDOG_TICK_RATE 1000 /* 1 kHz clock */
  20. #define STMP3XXX_DEFAULT_TIMEOUT 19
  21. #define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
  22. static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
  23. module_param(heartbeat, uint, 0);
  24. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
  25. __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
  26. __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));
  27. static int wdt_start(struct watchdog_device *wdd)
  28. {
  29. struct device *dev = watchdog_get_drvdata(wdd);
  30. struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
  31. pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
  32. return 0;
  33. }
  34. static int wdt_stop(struct watchdog_device *wdd)
  35. {
  36. struct device *dev = watchdog_get_drvdata(wdd);
  37. struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
  38. pdata->wdt_set_timeout(dev->parent, 0);
  39. return 0;
  40. }
  41. static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
  42. {
  43. wdd->timeout = new_timeout;
  44. return wdt_start(wdd);
  45. }
  46. static const struct watchdog_info stmp3xxx_wdt_ident = {
  47. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  48. .identity = "STMP3XXX RTC Watchdog",
  49. };
  50. static const struct watchdog_ops stmp3xxx_wdt_ops = {
  51. .owner = THIS_MODULE,
  52. .start = wdt_start,
  53. .stop = wdt_stop,
  54. .set_timeout = wdt_set_timeout,
  55. };
  56. static struct watchdog_device stmp3xxx_wdd = {
  57. .info = &stmp3xxx_wdt_ident,
  58. .ops = &stmp3xxx_wdt_ops,
  59. .min_timeout = 1,
  60. .max_timeout = STMP3XXX_MAX_TIMEOUT,
  61. .status = WATCHDOG_NOWAYOUT_INIT_STATUS,
  62. };
  63. static int wdt_notify_sys(struct notifier_block *nb, unsigned long code,
  64. void *unused)
  65. {
  66. switch (code) {
  67. case SYS_DOWN: /* keep enabled, system might crash while going down */
  68. break;
  69. case SYS_HALT: /* allow the system to actually halt */
  70. case SYS_POWER_OFF:
  71. wdt_stop(&stmp3xxx_wdd);
  72. break;
  73. }
  74. return NOTIFY_DONE;
  75. }
  76. static struct notifier_block wdt_notifier = {
  77. .notifier_call = wdt_notify_sys,
  78. };
  79. static int stmp3xxx_wdt_probe(struct platform_device *pdev)
  80. {
  81. int ret;
  82. watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);
  83. stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
  84. stmp3xxx_wdd.parent = &pdev->dev;
  85. ret = watchdog_register_device(&stmp3xxx_wdd);
  86. if (ret < 0) {
  87. dev_err(&pdev->dev, "cannot register watchdog device\n");
  88. return ret;
  89. }
  90. if (register_reboot_notifier(&wdt_notifier))
  91. dev_warn(&pdev->dev, "cannot register reboot notifier\n");
  92. dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
  93. stmp3xxx_wdd.timeout);
  94. return 0;
  95. }
  96. static int stmp3xxx_wdt_remove(struct platform_device *pdev)
  97. {
  98. unregister_reboot_notifier(&wdt_notifier);
  99. watchdog_unregister_device(&stmp3xxx_wdd);
  100. return 0;
  101. }
  102. static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
  103. {
  104. struct watchdog_device *wdd = &stmp3xxx_wdd;
  105. if (watchdog_active(wdd))
  106. return wdt_stop(wdd);
  107. return 0;
  108. }
  109. static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
  110. {
  111. struct watchdog_device *wdd = &stmp3xxx_wdd;
  112. if (watchdog_active(wdd))
  113. return wdt_start(wdd);
  114. return 0;
  115. }
  116. static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
  117. stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);
  118. static struct platform_driver stmp3xxx_wdt_driver = {
  119. .driver = {
  120. .name = "stmp3xxx_rtc_wdt",
  121. .pm = &stmp3xxx_wdt_pm_ops,
  122. },
  123. .probe = stmp3xxx_wdt_probe,
  124. .remove = stmp3xxx_wdt_remove,
  125. };
  126. module_platform_driver(stmp3xxx_wdt_driver);
  127. MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
  128. MODULE_LICENSE("GPL v2");
  129. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");