wm8350_wdt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Watchdog driver for the wm8350
  3. *
  4. * Copyright (C) 2007, 2008 Wolfson Microelectronics <linux@wolfsonmicro.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/mfd/wm8350/core.h>
  19. static bool nowayout = WATCHDOG_NOWAYOUT;
  20. module_param(nowayout, bool, 0);
  21. MODULE_PARM_DESC(nowayout,
  22. "Watchdog cannot be stopped once started (default="
  23. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  24. static DEFINE_MUTEX(wdt_mutex);
  25. static struct {
  26. unsigned int time; /* Seconds */
  27. u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */
  28. } wm8350_wdt_cfgs[] = {
  29. { 1, 0x02 },
  30. { 2, 0x04 },
  31. { 4, 0x05 },
  32. };
  33. static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev,
  34. unsigned int timeout)
  35. {
  36. struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
  37. int ret, i;
  38. u16 reg;
  39. for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
  40. if (wm8350_wdt_cfgs[i].time == timeout)
  41. break;
  42. if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
  43. return -EINVAL;
  44. mutex_lock(&wdt_mutex);
  45. wm8350_reg_unlock(wm8350);
  46. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  47. reg &= ~WM8350_WDOG_TO_MASK;
  48. reg |= wm8350_wdt_cfgs[i].val;
  49. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  50. wm8350_reg_lock(wm8350);
  51. mutex_unlock(&wdt_mutex);
  52. wdt_dev->timeout = timeout;
  53. return ret;
  54. }
  55. static int wm8350_wdt_start(struct watchdog_device *wdt_dev)
  56. {
  57. struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
  58. int ret;
  59. u16 reg;
  60. mutex_lock(&wdt_mutex);
  61. wm8350_reg_unlock(wm8350);
  62. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  63. reg &= ~WM8350_WDOG_MODE_MASK;
  64. reg |= 0x20;
  65. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  66. wm8350_reg_lock(wm8350);
  67. mutex_unlock(&wdt_mutex);
  68. return ret;
  69. }
  70. static int wm8350_wdt_stop(struct watchdog_device *wdt_dev)
  71. {
  72. struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
  73. int ret;
  74. u16 reg;
  75. mutex_lock(&wdt_mutex);
  76. wm8350_reg_unlock(wm8350);
  77. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  78. reg &= ~WM8350_WDOG_MODE_MASK;
  79. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  80. wm8350_reg_lock(wm8350);
  81. mutex_unlock(&wdt_mutex);
  82. return ret;
  83. }
  84. static int wm8350_wdt_ping(struct watchdog_device *wdt_dev)
  85. {
  86. struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
  87. int ret;
  88. u16 reg;
  89. mutex_lock(&wdt_mutex);
  90. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  91. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  92. mutex_unlock(&wdt_mutex);
  93. return ret;
  94. }
  95. static const struct watchdog_info wm8350_wdt_info = {
  96. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  97. .identity = "WM8350 Watchdog",
  98. };
  99. static const struct watchdog_ops wm8350_wdt_ops = {
  100. .owner = THIS_MODULE,
  101. .start = wm8350_wdt_start,
  102. .stop = wm8350_wdt_stop,
  103. .ping = wm8350_wdt_ping,
  104. .set_timeout = wm8350_wdt_set_timeout,
  105. };
  106. static struct watchdog_device wm8350_wdt = {
  107. .info = &wm8350_wdt_info,
  108. .ops = &wm8350_wdt_ops,
  109. .timeout = 4,
  110. .min_timeout = 1,
  111. .max_timeout = 4,
  112. };
  113. static int wm8350_wdt_probe(struct platform_device *pdev)
  114. {
  115. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  116. if (!wm8350) {
  117. pr_err("No driver data supplied\n");
  118. return -ENODEV;
  119. }
  120. watchdog_set_nowayout(&wm8350_wdt, nowayout);
  121. watchdog_set_drvdata(&wm8350_wdt, wm8350);
  122. wm8350_wdt.parent = &pdev->dev;
  123. /* Default to 4s timeout */
  124. wm8350_wdt_set_timeout(&wm8350_wdt, 4);
  125. return watchdog_register_device(&wm8350_wdt);
  126. }
  127. static int wm8350_wdt_remove(struct platform_device *pdev)
  128. {
  129. watchdog_unregister_device(&wm8350_wdt);
  130. return 0;
  131. }
  132. static struct platform_driver wm8350_wdt_driver = {
  133. .probe = wm8350_wdt_probe,
  134. .remove = wm8350_wdt_remove,
  135. .driver = {
  136. .name = "wm8350-wdt",
  137. },
  138. };
  139. module_platform_driver(wm8350_wdt_driver);
  140. MODULE_AUTHOR("Mark Brown");
  141. MODULE_DESCRIPTION("WM8350 Watchdog");
  142. MODULE_LICENSE("GPL");
  143. MODULE_ALIAS("platform:wm8350-wdt");