wm8350_wdt.c 4.0 KB

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