twl4030_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) Nokia Corporation
  3. *
  4. * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/slab.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/i2c/twl.h>
  30. #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
  31. #define TWL4030_WDT_STATE_OPEN 0x1
  32. #define TWL4030_WDT_STATE_ACTIVE 0x8
  33. static struct platform_device *twl4030_wdt_dev;
  34. struct twl4030_wdt {
  35. struct miscdevice miscdev;
  36. int timer_margin;
  37. unsigned long state;
  38. };
  39. static int nowayout = WATCHDOG_NOWAYOUT;
  40. module_param(nowayout, int, 0);
  41. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  42. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  43. static int twl4030_wdt_write(unsigned char val)
  44. {
  45. return twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, val,
  46. TWL4030_WATCHDOG_CFG_REG_OFFS);
  47. }
  48. static int twl4030_wdt_enable(struct twl4030_wdt *wdt)
  49. {
  50. return twl4030_wdt_write(wdt->timer_margin + 1);
  51. }
  52. static int twl4030_wdt_disable(struct twl4030_wdt *wdt)
  53. {
  54. return twl4030_wdt_write(0);
  55. }
  56. static int twl4030_wdt_set_timeout(struct twl4030_wdt *wdt, int timeout)
  57. {
  58. if (timeout < 0 || timeout > 30) {
  59. dev_warn(wdt->miscdev.parent,
  60. "Timeout can only be in the range [0-30] seconds");
  61. return -EINVAL;
  62. }
  63. wdt->timer_margin = timeout;
  64. return twl4030_wdt_enable(wdt);
  65. }
  66. static ssize_t twl4030_wdt_write_fop(struct file *file,
  67. const char __user *data, size_t len, loff_t *ppos)
  68. {
  69. struct twl4030_wdt *wdt = file->private_data;
  70. if (len)
  71. twl4030_wdt_enable(wdt);
  72. return len;
  73. }
  74. static long twl4030_wdt_ioctl(struct file *file,
  75. unsigned int cmd, unsigned long arg)
  76. {
  77. void __user *argp = (void __user *)arg;
  78. int __user *p = argp;
  79. int new_margin;
  80. struct twl4030_wdt *wdt = file->private_data;
  81. static const struct watchdog_info twl4030_wd_ident = {
  82. .identity = "TWL4030 Watchdog",
  83. .options = WDIOF_SETTIMEOUT,
  84. .firmware_version = 0,
  85. };
  86. switch (cmd) {
  87. case WDIOC_GETSUPPORT:
  88. return copy_to_user(argp, &twl4030_wd_ident,
  89. sizeof(twl4030_wd_ident)) ? -EFAULT : 0;
  90. case WDIOC_GETSTATUS:
  91. case WDIOC_GETBOOTSTATUS:
  92. return put_user(0, p);
  93. case WDIOC_KEEPALIVE:
  94. twl4030_wdt_enable(wdt);
  95. break;
  96. case WDIOC_SETTIMEOUT:
  97. if (get_user(new_margin, p))
  98. return -EFAULT;
  99. if (twl4030_wdt_set_timeout(wdt, new_margin))
  100. return -EINVAL;
  101. return put_user(wdt->timer_margin, p);
  102. case WDIOC_GETTIMEOUT:
  103. return put_user(wdt->timer_margin, p);
  104. default:
  105. return -ENOTTY;
  106. }
  107. return 0;
  108. }
  109. static int twl4030_wdt_open(struct inode *inode, struct file *file)
  110. {
  111. struct twl4030_wdt *wdt = platform_get_drvdata(twl4030_wdt_dev);
  112. /* /dev/watchdog can only be opened once */
  113. if (test_and_set_bit(0, &wdt->state))
  114. return -EBUSY;
  115. wdt->state |= TWL4030_WDT_STATE_ACTIVE;
  116. file->private_data = (void *) wdt;
  117. twl4030_wdt_enable(wdt);
  118. return nonseekable_open(inode, file);
  119. }
  120. static int twl4030_wdt_release(struct inode *inode, struct file *file)
  121. {
  122. struct twl4030_wdt *wdt = file->private_data;
  123. if (nowayout) {
  124. dev_alert(wdt->miscdev.parent,
  125. "Unexpected close, watchdog still running!\n");
  126. twl4030_wdt_enable(wdt);
  127. } else {
  128. if (twl4030_wdt_disable(wdt))
  129. return -EFAULT;
  130. wdt->state &= ~TWL4030_WDT_STATE_ACTIVE;
  131. }
  132. clear_bit(0, &wdt->state);
  133. return 0;
  134. }
  135. static const struct file_operations twl4030_wdt_fops = {
  136. .owner = THIS_MODULE,
  137. .llseek = no_llseek,
  138. .open = twl4030_wdt_open,
  139. .release = twl4030_wdt_release,
  140. .unlocked_ioctl = twl4030_wdt_ioctl,
  141. .write = twl4030_wdt_write_fop,
  142. };
  143. static int __devinit twl4030_wdt_probe(struct platform_device *pdev)
  144. {
  145. int ret = 0;
  146. struct twl4030_wdt *wdt;
  147. wdt = kzalloc(sizeof(struct twl4030_wdt), GFP_KERNEL);
  148. if (!wdt)
  149. return -ENOMEM;
  150. wdt->state = 0;
  151. wdt->timer_margin = 30;
  152. wdt->miscdev.parent = &pdev->dev;
  153. wdt->miscdev.fops = &twl4030_wdt_fops;
  154. wdt->miscdev.minor = WATCHDOG_MINOR;
  155. wdt->miscdev.name = "watchdog";
  156. platform_set_drvdata(pdev, wdt);
  157. twl4030_wdt_dev = pdev;
  158. twl4030_wdt_disable(wdt);
  159. ret = misc_register(&wdt->miscdev);
  160. if (ret) {
  161. dev_err(wdt->miscdev.parent,
  162. "Failed to register misc device\n");
  163. platform_set_drvdata(pdev, NULL);
  164. kfree(wdt);
  165. twl4030_wdt_dev = NULL;
  166. return ret;
  167. }
  168. return 0;
  169. }
  170. static int __devexit twl4030_wdt_remove(struct platform_device *pdev)
  171. {
  172. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  173. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  174. if (twl4030_wdt_disable(wdt))
  175. return -EFAULT;
  176. wdt->state &= ~TWL4030_WDT_STATE_ACTIVE;
  177. misc_deregister(&wdt->miscdev);
  178. platform_set_drvdata(pdev, NULL);
  179. kfree(wdt);
  180. twl4030_wdt_dev = NULL;
  181. return 0;
  182. }
  183. #ifdef CONFIG_PM
  184. static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  185. {
  186. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  187. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  188. return twl4030_wdt_disable(wdt);
  189. return 0;
  190. }
  191. static int twl4030_wdt_resume(struct platform_device *pdev)
  192. {
  193. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  194. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  195. return twl4030_wdt_enable(wdt);
  196. return 0;
  197. }
  198. #else
  199. #define twl4030_wdt_suspend NULL
  200. #define twl4030_wdt_resume NULL
  201. #endif
  202. static struct platform_driver twl4030_wdt_driver = {
  203. .probe = twl4030_wdt_probe,
  204. .remove = __devexit_p(twl4030_wdt_remove),
  205. .suspend = twl4030_wdt_suspend,
  206. .resume = twl4030_wdt_resume,
  207. .driver = {
  208. .owner = THIS_MODULE,
  209. .name = "twl4030_wdt",
  210. },
  211. };
  212. static int __devinit twl4030_wdt_init(void)
  213. {
  214. return platform_driver_register(&twl4030_wdt_driver);
  215. }
  216. module_init(twl4030_wdt_init);
  217. static void __devexit twl4030_wdt_exit(void)
  218. {
  219. platform_driver_unregister(&twl4030_wdt_driver);
  220. }
  221. module_exit(twl4030_wdt_exit);
  222. MODULE_AUTHOR("Nokia Corporation");
  223. MODULE_LICENSE("GPL");
  224. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  225. MODULE_ALIAS("platform:twl4030_wdt");