retu_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Retu watchdog driver
  3. *
  4. * Copyright (C) 2004, 2005 Nokia Corporation
  5. *
  6. * Based on code written by Amit Kucheria and Michael Buesch.
  7. * Rewritten by Aaro Koskinen.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/errno.h>
  20. #include <linux/device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mfd/retu.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/platform_device.h>
  26. /* Watchdog timer values in seconds */
  27. #define RETU_WDT_MAX_TIMER 63
  28. struct retu_wdt_dev {
  29. struct retu_dev *rdev;
  30. struct device *dev;
  31. struct delayed_work ping_work;
  32. };
  33. /*
  34. * Since Retu watchdog cannot be disabled in hardware, we must kick it
  35. * with a timer until userspace watchdog software takes over. If
  36. * CONFIG_WATCHDOG_NOWAYOUT is set, we never start the feeding.
  37. */
  38. static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
  39. {
  40. retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
  41. schedule_delayed_work(&wdev->ping_work,
  42. round_jiffies_relative(RETU_WDT_MAX_TIMER * HZ / 2));
  43. }
  44. static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
  45. {
  46. retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
  47. cancel_delayed_work_sync(&wdev->ping_work);
  48. }
  49. static void retu_wdt_ping_work(struct work_struct *work)
  50. {
  51. struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
  52. struct retu_wdt_dev, ping_work);
  53. retu_wdt_ping_enable(wdev);
  54. }
  55. static int retu_wdt_start(struct watchdog_device *wdog)
  56. {
  57. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  58. retu_wdt_ping_disable(wdev);
  59. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  60. }
  61. static int retu_wdt_stop(struct watchdog_device *wdog)
  62. {
  63. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  64. retu_wdt_ping_enable(wdev);
  65. return 0;
  66. }
  67. static int retu_wdt_ping(struct watchdog_device *wdog)
  68. {
  69. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  70. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  71. }
  72. static int retu_wdt_set_timeout(struct watchdog_device *wdog,
  73. unsigned int timeout)
  74. {
  75. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  76. wdog->timeout = timeout;
  77. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  78. }
  79. static const struct watchdog_info retu_wdt_info = {
  80. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  81. .identity = "Retu watchdog",
  82. };
  83. static const struct watchdog_ops retu_wdt_ops = {
  84. .owner = THIS_MODULE,
  85. .start = retu_wdt_start,
  86. .stop = retu_wdt_stop,
  87. .ping = retu_wdt_ping,
  88. .set_timeout = retu_wdt_set_timeout,
  89. };
  90. static int retu_wdt_probe(struct platform_device *pdev)
  91. {
  92. struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
  93. bool nowayout = WATCHDOG_NOWAYOUT;
  94. struct watchdog_device *retu_wdt;
  95. struct retu_wdt_dev *wdev;
  96. int ret;
  97. retu_wdt = devm_kzalloc(&pdev->dev, sizeof(*retu_wdt), GFP_KERNEL);
  98. if (!retu_wdt)
  99. return -ENOMEM;
  100. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  101. if (!wdev)
  102. return -ENOMEM;
  103. retu_wdt->info = &retu_wdt_info;
  104. retu_wdt->ops = &retu_wdt_ops;
  105. retu_wdt->timeout = RETU_WDT_MAX_TIMER;
  106. retu_wdt->min_timeout = 0;
  107. retu_wdt->max_timeout = RETU_WDT_MAX_TIMER;
  108. retu_wdt->parent = &pdev->dev;
  109. watchdog_set_drvdata(retu_wdt, wdev);
  110. watchdog_set_nowayout(retu_wdt, nowayout);
  111. wdev->rdev = rdev;
  112. wdev->dev = &pdev->dev;
  113. INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
  114. ret = watchdog_register_device(retu_wdt);
  115. if (ret < 0)
  116. return ret;
  117. if (nowayout)
  118. retu_wdt_ping(retu_wdt);
  119. else
  120. retu_wdt_ping_enable(wdev);
  121. platform_set_drvdata(pdev, retu_wdt);
  122. return 0;
  123. }
  124. static int retu_wdt_remove(struct platform_device *pdev)
  125. {
  126. struct watchdog_device *wdog = platform_get_drvdata(pdev);
  127. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  128. watchdog_unregister_device(wdog);
  129. cancel_delayed_work_sync(&wdev->ping_work);
  130. return 0;
  131. }
  132. static struct platform_driver retu_wdt_driver = {
  133. .probe = retu_wdt_probe,
  134. .remove = retu_wdt_remove,
  135. .driver = {
  136. .name = "retu-wdt",
  137. },
  138. };
  139. module_platform_driver(retu_wdt_driver);
  140. MODULE_ALIAS("platform:retu-wdt");
  141. MODULE_DESCRIPTION("Retu watchdog");
  142. MODULE_AUTHOR("Amit Kucheria");
  143. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  144. MODULE_LICENSE("GPL");