stmp3xxx_rtc_wdt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. struct device *dev = &pdev->dev;
  79. int ret;
  80. watchdog_set_drvdata(&stmp3xxx_wdd, dev);
  81. stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
  82. stmp3xxx_wdd.parent = dev;
  83. ret = devm_watchdog_register_device(dev, &stmp3xxx_wdd);
  84. if (ret < 0)
  85. return ret;
  86. if (register_reboot_notifier(&wdt_notifier))
  87. dev_warn(dev, "cannot register reboot notifier\n");
  88. dev_info(dev, "initialized watchdog with heartbeat %ds\n",
  89. stmp3xxx_wdd.timeout);
  90. return 0;
  91. }
  92. static int stmp3xxx_wdt_remove(struct platform_device *pdev)
  93. {
  94. unregister_reboot_notifier(&wdt_notifier);
  95. return 0;
  96. }
  97. static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
  98. {
  99. struct watchdog_device *wdd = &stmp3xxx_wdd;
  100. if (watchdog_active(wdd))
  101. return wdt_stop(wdd);
  102. return 0;
  103. }
  104. static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
  105. {
  106. struct watchdog_device *wdd = &stmp3xxx_wdd;
  107. if (watchdog_active(wdd))
  108. return wdt_start(wdd);
  109. return 0;
  110. }
  111. static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
  112. stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);
  113. static struct platform_driver stmp3xxx_wdt_driver = {
  114. .driver = {
  115. .name = "stmp3xxx_rtc_wdt",
  116. .pm = &stmp3xxx_wdt_pm_ops,
  117. },
  118. .probe = stmp3xxx_wdt_probe,
  119. .remove = stmp3xxx_wdt_remove,
  120. };
  121. module_platform_driver(stmp3xxx_wdt_driver);
  122. MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
  123. MODULE_LICENSE("GPL v2");
  124. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");