stmp3xxx_rtc_wdt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #define WDOG_TICK_RATE 1000 /* 1 kHz clock */
  18. #define STMP3XXX_DEFAULT_TIMEOUT 19
  19. #define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
  20. static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
  21. module_param(heartbeat, uint, 0);
  22. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
  23. __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
  24. __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));
  25. static int wdt_start(struct watchdog_device *wdd)
  26. {
  27. struct device *dev = watchdog_get_drvdata(wdd);
  28. struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
  29. pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
  30. return 0;
  31. }
  32. static int wdt_stop(struct watchdog_device *wdd)
  33. {
  34. struct device *dev = watchdog_get_drvdata(wdd);
  35. struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
  36. pdata->wdt_set_timeout(dev->parent, 0);
  37. return 0;
  38. }
  39. static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
  40. {
  41. wdd->timeout = new_timeout;
  42. return wdt_start(wdd);
  43. }
  44. static const struct watchdog_info stmp3xxx_wdt_ident = {
  45. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  46. .identity = "STMP3XXX RTC Watchdog",
  47. };
  48. static const struct watchdog_ops stmp3xxx_wdt_ops = {
  49. .owner = THIS_MODULE,
  50. .start = wdt_start,
  51. .stop = wdt_stop,
  52. .set_timeout = wdt_set_timeout,
  53. };
  54. static struct watchdog_device stmp3xxx_wdd = {
  55. .info = &stmp3xxx_wdt_ident,
  56. .ops = &stmp3xxx_wdt_ops,
  57. .min_timeout = 1,
  58. .max_timeout = STMP3XXX_MAX_TIMEOUT,
  59. .status = WATCHDOG_NOWAYOUT_INIT_STATUS,
  60. };
  61. static int stmp3xxx_wdt_probe(struct platform_device *pdev)
  62. {
  63. int ret;
  64. watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);
  65. stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
  66. stmp3xxx_wdd.parent = &pdev->dev;
  67. ret = watchdog_register_device(&stmp3xxx_wdd);
  68. if (ret < 0) {
  69. dev_err(&pdev->dev, "cannot register watchdog device\n");
  70. return ret;
  71. }
  72. dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
  73. stmp3xxx_wdd.timeout);
  74. return 0;
  75. }
  76. static int stmp3xxx_wdt_remove(struct platform_device *pdev)
  77. {
  78. watchdog_unregister_device(&stmp3xxx_wdd);
  79. return 0;
  80. }
  81. static int __maybe_unused stmp3xxx_wdt_suspend(struct device *dev)
  82. {
  83. struct watchdog_device *wdd = &stmp3xxx_wdd;
  84. if (watchdog_active(wdd))
  85. return wdt_stop(wdd);
  86. return 0;
  87. }
  88. static int __maybe_unused stmp3xxx_wdt_resume(struct device *dev)
  89. {
  90. struct watchdog_device *wdd = &stmp3xxx_wdd;
  91. if (watchdog_active(wdd))
  92. return wdt_start(wdd);
  93. return 0;
  94. }
  95. static SIMPLE_DEV_PM_OPS(stmp3xxx_wdt_pm_ops,
  96. stmp3xxx_wdt_suspend, stmp3xxx_wdt_resume);
  97. static struct platform_driver stmp3xxx_wdt_driver = {
  98. .driver = {
  99. .name = "stmp3xxx_rtc_wdt",
  100. .pm = &stmp3xxx_wdt_pm_ops,
  101. },
  102. .probe = stmp3xxx_wdt_probe,
  103. .remove = stmp3xxx_wdt_remove,
  104. };
  105. module_platform_driver(stmp3xxx_wdt_driver);
  106. MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
  107. MODULE_LICENSE("GPL v2");
  108. MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");