wm831x_wdt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog driver for the wm831x PMICs
  4. *
  5. * Copyright (C) 2009 Wolfson Microelectronics
  6. */
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/gpio.h>
  16. #include <linux/mfd/wm831x/core.h>
  17. #include <linux/mfd/wm831x/pdata.h>
  18. #include <linux/mfd/wm831x/watchdog.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. struct wm831x_wdt_drvdata {
  25. struct watchdog_device wdt;
  26. struct wm831x *wm831x;
  27. struct mutex lock;
  28. int update_gpio;
  29. int update_state;
  30. };
  31. /* We can't use the sub-second values here but they're included
  32. * for completeness. */
  33. static struct {
  34. unsigned int time; /* Seconds */
  35. u16 val; /* WDOG_TO value */
  36. } wm831x_wdt_cfgs[] = {
  37. { 1, 2 },
  38. { 2, 3 },
  39. { 4, 4 },
  40. { 8, 5 },
  41. { 16, 6 },
  42. { 32, 7 },
  43. { 33, 7 }, /* Actually 32.768s so include both, others round down */
  44. };
  45. static int wm831x_wdt_start(struct watchdog_device *wdt_dev)
  46. {
  47. struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
  48. struct wm831x *wm831x = driver_data->wm831x;
  49. int ret;
  50. mutex_lock(&driver_data->lock);
  51. ret = wm831x_reg_unlock(wm831x);
  52. if (ret == 0) {
  53. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  54. WM831X_WDOG_ENA, WM831X_WDOG_ENA);
  55. wm831x_reg_lock(wm831x);
  56. } else {
  57. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  58. ret);
  59. }
  60. mutex_unlock(&driver_data->lock);
  61. return ret;
  62. }
  63. static int wm831x_wdt_stop(struct watchdog_device *wdt_dev)
  64. {
  65. struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
  66. struct wm831x *wm831x = driver_data->wm831x;
  67. int ret;
  68. mutex_lock(&driver_data->lock);
  69. ret = wm831x_reg_unlock(wm831x);
  70. if (ret == 0) {
  71. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  72. WM831X_WDOG_ENA, 0);
  73. wm831x_reg_lock(wm831x);
  74. } else {
  75. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  76. ret);
  77. }
  78. mutex_unlock(&driver_data->lock);
  79. return ret;
  80. }
  81. static int wm831x_wdt_ping(struct watchdog_device *wdt_dev)
  82. {
  83. struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
  84. struct wm831x *wm831x = driver_data->wm831x;
  85. int ret;
  86. u16 reg;
  87. mutex_lock(&driver_data->lock);
  88. if (driver_data->update_gpio) {
  89. gpio_set_value_cansleep(driver_data->update_gpio,
  90. driver_data->update_state);
  91. driver_data->update_state = !driver_data->update_state;
  92. ret = 0;
  93. goto out;
  94. }
  95. reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  96. if (!(reg & WM831X_WDOG_RST_SRC)) {
  97. dev_err(wm831x->dev, "Hardware watchdog update unsupported\n");
  98. ret = -EINVAL;
  99. goto out;
  100. }
  101. reg |= WM831X_WDOG_RESET;
  102. ret = wm831x_reg_unlock(wm831x);
  103. if (ret == 0) {
  104. ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
  105. wm831x_reg_lock(wm831x);
  106. } else {
  107. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  108. ret);
  109. }
  110. out:
  111. mutex_unlock(&driver_data->lock);
  112. return ret;
  113. }
  114. static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev,
  115. unsigned int timeout)
  116. {
  117. struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
  118. struct wm831x *wm831x = driver_data->wm831x;
  119. int ret, i;
  120. for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
  121. if (wm831x_wdt_cfgs[i].time == timeout)
  122. break;
  123. if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
  124. return -EINVAL;
  125. ret = wm831x_reg_unlock(wm831x);
  126. if (ret == 0) {
  127. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  128. WM831X_WDOG_TO_MASK,
  129. wm831x_wdt_cfgs[i].val);
  130. wm831x_reg_lock(wm831x);
  131. } else {
  132. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  133. ret);
  134. }
  135. wdt_dev->timeout = timeout;
  136. return ret;
  137. }
  138. static const struct watchdog_info wm831x_wdt_info = {
  139. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  140. .identity = "WM831x Watchdog",
  141. };
  142. static const struct watchdog_ops wm831x_wdt_ops = {
  143. .owner = THIS_MODULE,
  144. .start = wm831x_wdt_start,
  145. .stop = wm831x_wdt_stop,
  146. .ping = wm831x_wdt_ping,
  147. .set_timeout = wm831x_wdt_set_timeout,
  148. };
  149. static int wm831x_wdt_probe(struct platform_device *pdev)
  150. {
  151. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  152. struct wm831x_pdata *chip_pdata = dev_get_platdata(pdev->dev.parent);
  153. struct wm831x_watchdog_pdata *pdata;
  154. struct wm831x_wdt_drvdata *driver_data;
  155. struct watchdog_device *wm831x_wdt;
  156. int reg, ret, i;
  157. ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  158. if (ret < 0) {
  159. dev_err(wm831x->dev, "Failed to read watchdog status: %d\n",
  160. ret);
  161. return ret;
  162. }
  163. reg = ret;
  164. if (reg & WM831X_WDOG_DEBUG)
  165. dev_warn(wm831x->dev, "Watchdog is paused\n");
  166. driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
  167. GFP_KERNEL);
  168. if (!driver_data)
  169. return -ENOMEM;
  170. mutex_init(&driver_data->lock);
  171. driver_data->wm831x = wm831x;
  172. wm831x_wdt = &driver_data->wdt;
  173. wm831x_wdt->info = &wm831x_wdt_info;
  174. wm831x_wdt->ops = &wm831x_wdt_ops;
  175. wm831x_wdt->parent = &pdev->dev;
  176. watchdog_set_nowayout(wm831x_wdt, nowayout);
  177. watchdog_set_drvdata(wm831x_wdt, driver_data);
  178. reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  179. reg &= WM831X_WDOG_TO_MASK;
  180. for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
  181. if (wm831x_wdt_cfgs[i].val == reg)
  182. break;
  183. if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
  184. dev_warn(wm831x->dev,
  185. "Unknown watchdog timeout: %x\n", reg);
  186. else
  187. wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time;
  188. /* Apply any configuration */
  189. if (chip_pdata)
  190. pdata = chip_pdata->watchdog;
  191. else
  192. pdata = NULL;
  193. if (pdata) {
  194. reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
  195. WM831X_WDOG_RST_SRC);
  196. reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT;
  197. reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT;
  198. reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT;
  199. if (pdata->update_gpio) {
  200. ret = devm_gpio_request_one(&pdev->dev,
  201. pdata->update_gpio,
  202. GPIOF_OUT_INIT_LOW,
  203. "Watchdog update");
  204. if (ret < 0) {
  205. dev_err(wm831x->dev,
  206. "Failed to request update GPIO: %d\n",
  207. ret);
  208. return ret;
  209. }
  210. driver_data->update_gpio = pdata->update_gpio;
  211. /* Make sure the watchdog takes hardware updates */
  212. reg |= WM831X_WDOG_RST_SRC;
  213. }
  214. ret = wm831x_reg_unlock(wm831x);
  215. if (ret == 0) {
  216. ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
  217. wm831x_reg_lock(wm831x);
  218. } else {
  219. dev_err(wm831x->dev,
  220. "Failed to unlock security key: %d\n", ret);
  221. return ret;
  222. }
  223. }
  224. ret = devm_watchdog_register_device(&pdev->dev, &driver_data->wdt);
  225. if (ret != 0) {
  226. dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n",
  227. ret);
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. static struct platform_driver wm831x_wdt_driver = {
  233. .probe = wm831x_wdt_probe,
  234. .driver = {
  235. .name = "wm831x-watchdog",
  236. },
  237. };
  238. module_platform_driver(wm831x_wdt_driver);
  239. MODULE_AUTHOR("Mark Brown");
  240. MODULE_DESCRIPTION("WM831x Watchdog");
  241. MODULE_LICENSE("GPL");
  242. MODULE_ALIAS("platform:wm831x-watchdog");