stmp3xxx_rtc_wdt.c 3.9 KB

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