retu_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include <linux/device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/mfd/retu.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/platform_device.h>
  27. /* Watchdog timer values in seconds */
  28. #define RETU_WDT_MAX_TIMER 63
  29. struct retu_wdt_dev {
  30. struct retu_dev *rdev;
  31. struct device *dev;
  32. struct delayed_work ping_work;
  33. };
  34. /*
  35. * Since Retu watchdog cannot be disabled in hardware, we must kick it
  36. * with a timer until userspace watchdog software takes over. If
  37. * CONFIG_WATCHDOG_NOWAYOUT is set, we never start the feeding.
  38. */
  39. static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
  40. {
  41. retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
  42. schedule_delayed_work(&wdev->ping_work,
  43. round_jiffies_relative(RETU_WDT_MAX_TIMER * HZ / 2));
  44. }
  45. static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
  46. {
  47. retu_write(wdev->rdev, RETU_REG_WATCHDOG, RETU_WDT_MAX_TIMER);
  48. cancel_delayed_work_sync(&wdev->ping_work);
  49. }
  50. static void retu_wdt_ping_work(struct work_struct *work)
  51. {
  52. struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
  53. struct retu_wdt_dev, ping_work);
  54. retu_wdt_ping_enable(wdev);
  55. }
  56. static int retu_wdt_start(struct watchdog_device *wdog)
  57. {
  58. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  59. retu_wdt_ping_disable(wdev);
  60. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  61. }
  62. static int retu_wdt_stop(struct watchdog_device *wdog)
  63. {
  64. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  65. retu_wdt_ping_enable(wdev);
  66. return 0;
  67. }
  68. static int retu_wdt_ping(struct watchdog_device *wdog)
  69. {
  70. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  71. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  72. }
  73. static int retu_wdt_set_timeout(struct watchdog_device *wdog,
  74. unsigned int timeout)
  75. {
  76. struct retu_wdt_dev *wdev = watchdog_get_drvdata(wdog);
  77. wdog->timeout = timeout;
  78. return retu_write(wdev->rdev, RETU_REG_WATCHDOG, wdog->timeout);
  79. }
  80. static const struct watchdog_info retu_wdt_info = {
  81. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  82. .identity = "Retu watchdog",
  83. };
  84. static const struct watchdog_ops retu_wdt_ops = {
  85. .owner = THIS_MODULE,
  86. .start = retu_wdt_start,
  87. .stop = retu_wdt_stop,
  88. .ping = retu_wdt_ping,
  89. .set_timeout = retu_wdt_set_timeout,
  90. };
  91. static int retu_wdt_probe(struct platform_device *pdev)
  92. {
  93. struct retu_dev *rdev = dev_get_drvdata(pdev->dev.parent);
  94. bool nowayout = WATCHDOG_NOWAYOUT;
  95. struct watchdog_device *retu_wdt;
  96. struct retu_wdt_dev *wdev;
  97. int ret;
  98. retu_wdt = devm_kzalloc(&pdev->dev, sizeof(*retu_wdt), GFP_KERNEL);
  99. if (!retu_wdt)
  100. return -ENOMEM;
  101. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  102. if (!wdev)
  103. return -ENOMEM;
  104. retu_wdt->info = &retu_wdt_info;
  105. retu_wdt->ops = &retu_wdt_ops;
  106. retu_wdt->timeout = RETU_WDT_MAX_TIMER;
  107. retu_wdt->min_timeout = 0;
  108. retu_wdt->max_timeout = RETU_WDT_MAX_TIMER;
  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");