wm8350_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #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/mfd/wm8350/core.h>
  20. static int nowayout = WATCHDOG_NOWAYOUT;
  21. module_param(nowayout, int, 0);
  22. MODULE_PARM_DESC(nowayout,
  23. "Watchdog cannot be stopped once started (default="
  24. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  25. static unsigned long wm8350_wdt_users;
  26. static struct miscdevice wm8350_wdt_miscdev;
  27. static int wm8350_wdt_expect_close;
  28. static DEFINE_MUTEX(wdt_mutex);
  29. static struct {
  30. int time; /* Seconds */
  31. u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */
  32. } wm8350_wdt_cfgs[] = {
  33. { 1, 0x02 },
  34. { 2, 0x04 },
  35. { 4, 0x05 },
  36. };
  37. static struct wm8350 *get_wm8350(void)
  38. {
  39. return dev_get_drvdata(wm8350_wdt_miscdev.parent);
  40. }
  41. static int wm8350_wdt_set_timeout(struct wm8350 *wm8350, u16 value)
  42. {
  43. int ret;
  44. u16 reg;
  45. mutex_lock(&wdt_mutex);
  46. wm8350_reg_unlock(wm8350);
  47. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  48. reg &= ~WM8350_WDOG_TO_MASK;
  49. reg |= value;
  50. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  51. wm8350_reg_lock(wm8350);
  52. mutex_unlock(&wdt_mutex);
  53. return ret;
  54. }
  55. static int wm8350_wdt_start(struct wm8350 *wm8350)
  56. {
  57. int ret;
  58. u16 reg;
  59. mutex_lock(&wdt_mutex);
  60. wm8350_reg_unlock(wm8350);
  61. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  62. reg &= ~WM8350_WDOG_MODE_MASK;
  63. reg |= 0x20;
  64. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  65. wm8350_reg_lock(wm8350);
  66. mutex_unlock(&wdt_mutex);
  67. return ret;
  68. }
  69. static int wm8350_wdt_stop(struct wm8350 *wm8350)
  70. {
  71. int ret;
  72. u16 reg;
  73. mutex_lock(&wdt_mutex);
  74. wm8350_reg_unlock(wm8350);
  75. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  76. reg &= ~WM8350_WDOG_MODE_MASK;
  77. ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
  78. wm8350_reg_lock(wm8350);
  79. mutex_unlock(&wdt_mutex);
  80. return ret;
  81. }
  82. static int wm8350_wdt_kick(struct wm8350 *wm8350)
  83. {
  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 int wm8350_wdt_open(struct inode *inode, struct file *file)
  93. {
  94. struct wm8350 *wm8350 = get_wm8350();
  95. int ret;
  96. if (!wm8350)
  97. return -ENODEV;
  98. if (test_and_set_bit(0, &wm8350_wdt_users))
  99. return -EBUSY;
  100. ret = wm8350_wdt_start(wm8350);
  101. if (ret != 0)
  102. return ret;
  103. return nonseekable_open(inode, file);
  104. }
  105. static int wm8350_wdt_release(struct inode *inode, struct file *file)
  106. {
  107. struct wm8350 *wm8350 = get_wm8350();
  108. if (wm8350_wdt_expect_close)
  109. wm8350_wdt_stop(wm8350);
  110. else {
  111. dev_warn(wm8350->dev, "Watchdog device closed uncleanly\n");
  112. wm8350_wdt_kick(wm8350);
  113. }
  114. clear_bit(0, &wm8350_wdt_users);
  115. return 0;
  116. }
  117. static ssize_t wm8350_wdt_write(struct file *file,
  118. const char __user *data, size_t count,
  119. loff_t *ppos)
  120. {
  121. struct wm8350 *wm8350 = get_wm8350();
  122. size_t i;
  123. if (count) {
  124. wm8350_wdt_kick(wm8350);
  125. if (!nowayout) {
  126. /* In case it was set long ago */
  127. wm8350_wdt_expect_close = 0;
  128. /* scan to see whether or not we got the magic
  129. character */
  130. for (i = 0; i != count; i++) {
  131. char c;
  132. if (get_user(c, data + i))
  133. return -EFAULT;
  134. if (c == 'V')
  135. wm8350_wdt_expect_close = 42;
  136. }
  137. }
  138. }
  139. return count;
  140. }
  141. static const struct watchdog_info ident = {
  142. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  143. .identity = "WM8350 Watchdog",
  144. };
  145. static long wm8350_wdt_ioctl(struct file *file, unsigned int cmd,
  146. unsigned long arg)
  147. {
  148. struct wm8350 *wm8350 = get_wm8350();
  149. int ret = -ENOTTY, time, i;
  150. void __user *argp = (void __user *)arg;
  151. int __user *p = argp;
  152. u16 reg;
  153. switch (cmd) {
  154. case WDIOC_GETSUPPORT:
  155. ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  156. break;
  157. case WDIOC_GETSTATUS:
  158. case WDIOC_GETBOOTSTATUS:
  159. ret = put_user(0, p);
  160. break;
  161. case WDIOC_SETOPTIONS:
  162. {
  163. int options;
  164. if (get_user(options, p))
  165. return -EFAULT;
  166. ret = -EINVAL;
  167. /* Setting both simultaneously means at least one must fail */
  168. if (options == WDIOS_DISABLECARD)
  169. ret = wm8350_wdt_start(wm8350);
  170. if (options == WDIOS_ENABLECARD)
  171. ret = wm8350_wdt_stop(wm8350);
  172. break;
  173. }
  174. case WDIOC_KEEPALIVE:
  175. ret = wm8350_wdt_kick(wm8350);
  176. break;
  177. case WDIOC_SETTIMEOUT:
  178. ret = get_user(time, p);
  179. if (ret)
  180. break;
  181. if (time == 0) {
  182. if (nowayout)
  183. ret = -EINVAL;
  184. else
  185. wm8350_wdt_stop(wm8350);
  186. break;
  187. }
  188. for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
  189. if (wm8350_wdt_cfgs[i].time == time)
  190. break;
  191. if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
  192. ret = -EINVAL;
  193. else
  194. ret = wm8350_wdt_set_timeout(wm8350,
  195. wm8350_wdt_cfgs[i].val);
  196. break;
  197. case WDIOC_GETTIMEOUT:
  198. reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
  199. reg &= WM8350_WDOG_TO_MASK;
  200. for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
  201. if (wm8350_wdt_cfgs[i].val == reg)
  202. break;
  203. if (i == ARRAY_SIZE(wm8350_wdt_cfgs)) {
  204. dev_warn(wm8350->dev,
  205. "Unknown watchdog configuration: %x\n", reg);
  206. ret = -EINVAL;
  207. } else
  208. ret = put_user(wm8350_wdt_cfgs[i].time, p);
  209. }
  210. return ret;
  211. }
  212. static const struct file_operations wm8350_wdt_fops = {
  213. .owner = THIS_MODULE,
  214. .llseek = no_llseek,
  215. .write = wm8350_wdt_write,
  216. .unlocked_ioctl = wm8350_wdt_ioctl,
  217. .open = wm8350_wdt_open,
  218. .release = wm8350_wdt_release,
  219. };
  220. static struct miscdevice wm8350_wdt_miscdev = {
  221. .minor = WATCHDOG_MINOR,
  222. .name = "watchdog",
  223. .fops = &wm8350_wdt_fops,
  224. };
  225. static int __devinit wm8350_wdt_probe(struct platform_device *pdev)
  226. {
  227. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  228. if (!wm8350) {
  229. pr_err("No driver data supplied\n");
  230. return -ENODEV;
  231. }
  232. /* Default to 4s timeout */
  233. wm8350_wdt_set_timeout(wm8350, 0x05);
  234. wm8350_wdt_miscdev.parent = &pdev->dev;
  235. return misc_register(&wm8350_wdt_miscdev);
  236. }
  237. static int __devexit wm8350_wdt_remove(struct platform_device *pdev)
  238. {
  239. misc_deregister(&wm8350_wdt_miscdev);
  240. return 0;
  241. }
  242. static struct platform_driver wm8350_wdt_driver = {
  243. .probe = wm8350_wdt_probe,
  244. .remove = __devexit_p(wm8350_wdt_remove),
  245. .driver = {
  246. .name = "wm8350-wdt",
  247. },
  248. };
  249. static int __init wm8350_wdt_init(void)
  250. {
  251. return platform_driver_register(&wm8350_wdt_driver);
  252. }
  253. module_init(wm8350_wdt_init);
  254. static void __exit wm8350_wdt_exit(void)
  255. {
  256. platform_driver_unregister(&wm8350_wdt_driver);
  257. }
  258. module_exit(wm8350_wdt_exit);
  259. MODULE_AUTHOR("Mark Brown");
  260. MODULE_DESCRIPTION("WM8350 Watchdog");
  261. MODULE_LICENSE("GPL");
  262. MODULE_ALIAS("platform:wm8350-wdt");