wm831x_wdt.c 7.4 KB

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