wm8350_wdt.c 6.7 KB

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