wm831x_wdt.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/fs.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/watchdog.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/gpio.h>
  20. #include <linux/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/pdata.h>
  22. #include <linux/mfd/wm831x/watchdog.h>
  23. static int nowayout = WATCHDOG_NOWAYOUT;
  24. module_param(nowayout, int, 0);
  25. MODULE_PARM_DESC(nowayout,
  26. "Watchdog cannot be stopped once started (default="
  27. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  28. static unsigned long wm831x_wdt_users;
  29. static struct miscdevice wm831x_wdt_miscdev;
  30. static int wm831x_wdt_expect_close;
  31. static DEFINE_MUTEX(wdt_mutex);
  32. static struct wm831x *wm831x;
  33. static unsigned int update_gpio;
  34. static unsigned int update_state;
  35. /* We can't use the sub-second values here but they're included
  36. * for completeness. */
  37. static struct {
  38. int time; /* Seconds */
  39. u16 val; /* WDOG_TO value */
  40. } wm831x_wdt_cfgs[] = {
  41. { 1, 2 },
  42. { 2, 3 },
  43. { 4, 4 },
  44. { 8, 5 },
  45. { 16, 6 },
  46. { 32, 7 },
  47. { 33, 7 }, /* Actually 32.768s so include both, others round down */
  48. };
  49. static int wm831x_wdt_set_timeout(struct wm831x *wm831x, u16 value)
  50. {
  51. int ret;
  52. mutex_lock(&wdt_mutex);
  53. ret = wm831x_reg_unlock(wm831x);
  54. if (ret == 0) {
  55. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  56. WM831X_WDOG_TO_MASK, value);
  57. wm831x_reg_lock(wm831x);
  58. } else {
  59. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  60. ret);
  61. }
  62. mutex_unlock(&wdt_mutex);
  63. return ret;
  64. }
  65. static int wm831x_wdt_start(struct wm831x *wm831x)
  66. {
  67. int ret;
  68. mutex_lock(&wdt_mutex);
  69. ret = wm831x_reg_unlock(wm831x);
  70. if (ret == 0) {
  71. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  72. WM831X_WDOG_ENA, WM831X_WDOG_ENA);
  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(&wdt_mutex);
  79. return ret;
  80. }
  81. static int wm831x_wdt_stop(struct wm831x *wm831x)
  82. {
  83. int ret;
  84. mutex_lock(&wdt_mutex);
  85. ret = wm831x_reg_unlock(wm831x);
  86. if (ret == 0) {
  87. ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
  88. WM831X_WDOG_ENA, 0);
  89. wm831x_reg_lock(wm831x);
  90. } else {
  91. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  92. ret);
  93. }
  94. mutex_unlock(&wdt_mutex);
  95. return ret;
  96. }
  97. static int wm831x_wdt_kick(struct wm831x *wm831x)
  98. {
  99. int ret;
  100. u16 reg;
  101. mutex_lock(&wdt_mutex);
  102. if (update_gpio) {
  103. gpio_set_value_cansleep(update_gpio, update_state);
  104. update_state = !update_state;
  105. ret = 0;
  106. goto out;
  107. }
  108. reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  109. if (!(reg & WM831X_WDOG_RST_SRC)) {
  110. dev_err(wm831x->dev, "Hardware watchdog update unsupported\n");
  111. ret = -EINVAL;
  112. goto out;
  113. }
  114. reg |= WM831X_WDOG_RESET;
  115. ret = wm831x_reg_unlock(wm831x);
  116. if (ret == 0) {
  117. ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
  118. wm831x_reg_lock(wm831x);
  119. } else {
  120. dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
  121. ret);
  122. }
  123. out:
  124. mutex_unlock(&wdt_mutex);
  125. return ret;
  126. }
  127. static int wm831x_wdt_open(struct inode *inode, struct file *file)
  128. {
  129. int ret;
  130. if (!wm831x)
  131. return -ENODEV;
  132. if (test_and_set_bit(0, &wm831x_wdt_users))
  133. return -EBUSY;
  134. ret = wm831x_wdt_start(wm831x);
  135. if (ret != 0)
  136. return ret;
  137. return nonseekable_open(inode, file);
  138. }
  139. static int wm831x_wdt_release(struct inode *inode, struct file *file)
  140. {
  141. if (wm831x_wdt_expect_close)
  142. wm831x_wdt_stop(wm831x);
  143. else {
  144. dev_warn(wm831x->dev, "Watchdog device closed uncleanly\n");
  145. wm831x_wdt_kick(wm831x);
  146. }
  147. clear_bit(0, &wm831x_wdt_users);
  148. return 0;
  149. }
  150. static ssize_t wm831x_wdt_write(struct file *file,
  151. const char __user *data, size_t count,
  152. loff_t *ppos)
  153. {
  154. size_t i;
  155. if (count) {
  156. wm831x_wdt_kick(wm831x);
  157. if (!nowayout) {
  158. /* In case it was set long ago */
  159. wm831x_wdt_expect_close = 0;
  160. /* scan to see whether or not we got the magic
  161. character */
  162. for (i = 0; i != count; i++) {
  163. char c;
  164. if (get_user(c, data + i))
  165. return -EFAULT;
  166. if (c == 'V')
  167. wm831x_wdt_expect_close = 42;
  168. }
  169. }
  170. }
  171. return count;
  172. }
  173. static const struct watchdog_info ident = {
  174. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  175. .identity = "WM831x Watchdog",
  176. };
  177. static long wm831x_wdt_ioctl(struct file *file, unsigned int cmd,
  178. unsigned long arg)
  179. {
  180. int ret = -ENOTTY, time, i;
  181. void __user *argp = (void __user *)arg;
  182. int __user *p = argp;
  183. u16 reg;
  184. switch (cmd) {
  185. case WDIOC_GETSUPPORT:
  186. ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  187. break;
  188. case WDIOC_GETSTATUS:
  189. case WDIOC_GETBOOTSTATUS:
  190. ret = put_user(0, p);
  191. break;
  192. case WDIOC_SETOPTIONS:
  193. {
  194. int options;
  195. if (get_user(options, p))
  196. return -EFAULT;
  197. ret = -EINVAL;
  198. /* Setting both simultaneously means at least one must fail */
  199. if (options == WDIOS_DISABLECARD)
  200. ret = wm831x_wdt_start(wm831x);
  201. if (options == WDIOS_ENABLECARD)
  202. ret = wm831x_wdt_stop(wm831x);
  203. break;
  204. }
  205. case WDIOC_KEEPALIVE:
  206. ret = wm831x_wdt_kick(wm831x);
  207. break;
  208. case WDIOC_SETTIMEOUT:
  209. ret = get_user(time, p);
  210. if (ret)
  211. break;
  212. if (time == 0) {
  213. if (nowayout)
  214. ret = -EINVAL;
  215. else
  216. wm831x_wdt_stop(wm831x);
  217. break;
  218. }
  219. for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
  220. if (wm831x_wdt_cfgs[i].time == time)
  221. break;
  222. if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
  223. ret = -EINVAL;
  224. else
  225. ret = wm831x_wdt_set_timeout(wm831x,
  226. wm831x_wdt_cfgs[i].val);
  227. break;
  228. case WDIOC_GETTIMEOUT:
  229. reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  230. reg &= WM831X_WDOG_TO_MASK;
  231. for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
  232. if (wm831x_wdt_cfgs[i].val == reg)
  233. break;
  234. if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) {
  235. dev_warn(wm831x->dev,
  236. "Unknown watchdog configuration: %x\n", reg);
  237. ret = -EINVAL;
  238. } else
  239. ret = put_user(wm831x_wdt_cfgs[i].time, p);
  240. }
  241. return ret;
  242. }
  243. static const struct file_operations wm831x_wdt_fops = {
  244. .owner = THIS_MODULE,
  245. .llseek = no_llseek,
  246. .write = wm831x_wdt_write,
  247. .unlocked_ioctl = wm831x_wdt_ioctl,
  248. .open = wm831x_wdt_open,
  249. .release = wm831x_wdt_release,
  250. };
  251. static struct miscdevice wm831x_wdt_miscdev = {
  252. .minor = WATCHDOG_MINOR,
  253. .name = "watchdog",
  254. .fops = &wm831x_wdt_fops,
  255. };
  256. static int __devinit wm831x_wdt_probe(struct platform_device *pdev)
  257. {
  258. struct wm831x_pdata *chip_pdata;
  259. struct wm831x_watchdog_pdata *pdata;
  260. int reg, ret;
  261. if (wm831x) {
  262. dev_err(&pdev->dev, "wm831x watchdog already registered\n");
  263. return -EBUSY;
  264. }
  265. wm831x = dev_get_drvdata(pdev->dev.parent);
  266. ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
  267. if (ret < 0) {
  268. dev_err(wm831x->dev, "Failed to read watchdog status: %d\n",
  269. ret);
  270. goto err;
  271. }
  272. reg = ret;
  273. if (reg & WM831X_WDOG_DEBUG)
  274. dev_warn(wm831x->dev, "Watchdog is paused\n");
  275. /* Apply any configuration */
  276. if (pdev->dev.parent->platform_data) {
  277. chip_pdata = pdev->dev.parent->platform_data;
  278. pdata = chip_pdata->watchdog;
  279. } else {
  280. pdata = NULL;
  281. }
  282. if (pdata) {
  283. reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
  284. WM831X_WDOG_RST_SRC);
  285. reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT;
  286. reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT;
  287. reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT;
  288. if (pdata->update_gpio) {
  289. ret = gpio_request(pdata->update_gpio,
  290. "Watchdog update");
  291. if (ret < 0) {
  292. dev_err(wm831x->dev,
  293. "Failed to request update GPIO: %d\n",
  294. ret);
  295. goto err;
  296. }
  297. ret = gpio_direction_output(pdata->update_gpio, 0);
  298. if (ret != 0) {
  299. dev_err(wm831x->dev,
  300. "gpio_direction_output returned: %d\n",
  301. ret);
  302. goto err_gpio;
  303. }
  304. update_gpio = pdata->update_gpio;
  305. /* Make sure the watchdog takes hardware updates */
  306. reg |= WM831X_WDOG_RST_SRC;
  307. }
  308. ret = wm831x_reg_unlock(wm831x);
  309. if (ret == 0) {
  310. ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
  311. wm831x_reg_lock(wm831x);
  312. } else {
  313. dev_err(wm831x->dev,
  314. "Failed to unlock security key: %d\n", ret);
  315. goto err_gpio;
  316. }
  317. }
  318. wm831x_wdt_miscdev.parent = &pdev->dev;
  319. ret = misc_register(&wm831x_wdt_miscdev);
  320. if (ret != 0) {
  321. dev_err(wm831x->dev, "Failed to register miscdev: %d\n", ret);
  322. goto err_gpio;
  323. }
  324. return 0;
  325. err_gpio:
  326. if (update_gpio) {
  327. gpio_free(update_gpio);
  328. update_gpio = 0;
  329. }
  330. err:
  331. return ret;
  332. }
  333. static int __devexit wm831x_wdt_remove(struct platform_device *pdev)
  334. {
  335. if (update_gpio) {
  336. gpio_free(update_gpio);
  337. update_gpio = 0;
  338. }
  339. misc_deregister(&wm831x_wdt_miscdev);
  340. return 0;
  341. }
  342. static struct platform_driver wm831x_wdt_driver = {
  343. .probe = wm831x_wdt_probe,
  344. .remove = __devexit_p(wm831x_wdt_remove),
  345. .driver = {
  346. .name = "wm831x-watchdog",
  347. },
  348. };
  349. static int __init wm831x_wdt_init(void)
  350. {
  351. return platform_driver_register(&wm831x_wdt_driver);
  352. }
  353. module_init(wm831x_wdt_init);
  354. static void __exit wm831x_wdt_exit(void)
  355. {
  356. platform_driver_unregister(&wm831x_wdt_driver);
  357. }
  358. module_exit(wm831x_wdt_exit);
  359. MODULE_AUTHOR("Mark Brown");
  360. MODULE_DESCRIPTION("WM831x Watchdog");
  361. MODULE_LICENSE("GPL");
  362. MODULE_ALIAS("platform:wm831x-watchdog");