twl4030_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/kernel.h>
  23. #include <linux/fs.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/i2c/twl.h>
  29. #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
  30. #define TWL4030_WDT_STATE_OPEN 0x1
  31. #define TWL4030_WDT_STATE_ACTIVE 0x8
  32. static struct platform_device *twl4030_wdt_dev;
  33. struct twl4030_wdt {
  34. struct miscdevice miscdev;
  35. int timer_margin;
  36. unsigned long state;
  37. };
  38. static int nowayout = WATCHDOG_NOWAYOUT;
  39. module_param(nowayout, int, 0);
  40. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  41. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  42. static int twl4030_wdt_write(unsigned char val)
  43. {
  44. return twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, val,
  45. TWL4030_WATCHDOG_CFG_REG_OFFS);
  46. }
  47. static int twl4030_wdt_enable(struct twl4030_wdt *wdt)
  48. {
  49. return twl4030_wdt_write(wdt->timer_margin + 1);
  50. }
  51. static int twl4030_wdt_disable(struct twl4030_wdt *wdt)
  52. {
  53. return twl4030_wdt_write(0);
  54. }
  55. static int twl4030_wdt_set_timeout(struct twl4030_wdt *wdt, int timeout)
  56. {
  57. if (timeout < 0 || timeout > 30) {
  58. dev_warn(wdt->miscdev.parent,
  59. "Timeout can only be in the range [0-30] seconds");
  60. return -EINVAL;
  61. }
  62. wdt->timer_margin = timeout;
  63. return twl4030_wdt_enable(wdt);
  64. }
  65. static ssize_t twl4030_wdt_write_fop(struct file *file,
  66. const char __user *data, size_t len, loff_t *ppos)
  67. {
  68. struct twl4030_wdt *wdt = file->private_data;
  69. if (len)
  70. twl4030_wdt_enable(wdt);
  71. return len;
  72. }
  73. static long twl4030_wdt_ioctl(struct file *file,
  74. unsigned int cmd, unsigned long arg)
  75. {
  76. void __user *argp = (void __user *)arg;
  77. int __user *p = argp;
  78. int new_margin;
  79. struct twl4030_wdt *wdt = file->private_data;
  80. static const struct watchdog_info twl4030_wd_ident = {
  81. .identity = "TWL4030 Watchdog",
  82. .options = WDIOF_SETTIMEOUT,
  83. .firmware_version = 0,
  84. };
  85. switch (cmd) {
  86. case WDIOC_GETSUPPORT:
  87. return copy_to_user(argp, &twl4030_wd_ident,
  88. sizeof(twl4030_wd_ident)) ? -EFAULT : 0;
  89. case WDIOC_GETSTATUS:
  90. case WDIOC_GETBOOTSTATUS:
  91. return put_user(0, p);
  92. case WDIOC_KEEPALIVE:
  93. twl4030_wdt_enable(wdt);
  94. break;
  95. case WDIOC_SETTIMEOUT:
  96. if (get_user(new_margin, p))
  97. return -EFAULT;
  98. if (twl4030_wdt_set_timeout(wdt, new_margin))
  99. return -EINVAL;
  100. return put_user(wdt->timer_margin, p);
  101. case WDIOC_GETTIMEOUT:
  102. return put_user(wdt->timer_margin, p);
  103. default:
  104. return -ENOTTY;
  105. }
  106. return 0;
  107. }
  108. static int twl4030_wdt_open(struct inode *inode, struct file *file)
  109. {
  110. struct twl4030_wdt *wdt = platform_get_drvdata(twl4030_wdt_dev);
  111. /* /dev/watchdog can only be opened once */
  112. if (test_and_set_bit(0, &wdt->state))
  113. return -EBUSY;
  114. wdt->state |= TWL4030_WDT_STATE_ACTIVE;
  115. file->private_data = (void *) wdt;
  116. twl4030_wdt_enable(wdt);
  117. return nonseekable_open(inode, file);
  118. }
  119. static int twl4030_wdt_release(struct inode *inode, struct file *file)
  120. {
  121. struct twl4030_wdt *wdt = file->private_data;
  122. if (nowayout) {
  123. dev_alert(wdt->miscdev.parent,
  124. "Unexpected close, watchdog still running!\n");
  125. twl4030_wdt_enable(wdt);
  126. } else {
  127. if (twl4030_wdt_disable(wdt))
  128. return -EFAULT;
  129. wdt->state &= ~TWL4030_WDT_STATE_ACTIVE;
  130. }
  131. clear_bit(0, &wdt->state);
  132. return 0;
  133. }
  134. static const struct file_operations twl4030_wdt_fops = {
  135. .owner = THIS_MODULE,
  136. .llseek = no_llseek,
  137. .open = twl4030_wdt_open,
  138. .release = twl4030_wdt_release,
  139. .unlocked_ioctl = twl4030_wdt_ioctl,
  140. .write = twl4030_wdt_write_fop,
  141. };
  142. static int __devinit twl4030_wdt_probe(struct platform_device *pdev)
  143. {
  144. int ret = 0;
  145. struct twl4030_wdt *wdt;
  146. wdt = kzalloc(sizeof(struct twl4030_wdt), GFP_KERNEL);
  147. if (!wdt)
  148. return -ENOMEM;
  149. wdt->state = 0;
  150. wdt->timer_margin = 30;
  151. wdt->miscdev.parent = &pdev->dev;
  152. wdt->miscdev.fops = &twl4030_wdt_fops;
  153. wdt->miscdev.minor = WATCHDOG_MINOR;
  154. wdt->miscdev.name = "watchdog";
  155. platform_set_drvdata(pdev, wdt);
  156. twl4030_wdt_dev = pdev;
  157. ret = misc_register(&wdt->miscdev);
  158. if (ret) {
  159. dev_err(wdt->miscdev.parent,
  160. "Failed to register misc device\n");
  161. platform_set_drvdata(pdev, NULL);
  162. kfree(wdt);
  163. twl4030_wdt_dev = NULL;
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. static int __devexit twl4030_wdt_remove(struct platform_device *pdev)
  169. {
  170. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  171. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  172. if (twl4030_wdt_disable(wdt))
  173. return -EFAULT;
  174. wdt->state &= ~TWL4030_WDT_STATE_ACTIVE;
  175. misc_deregister(&wdt->miscdev);
  176. platform_set_drvdata(pdev, NULL);
  177. kfree(wdt);
  178. twl4030_wdt_dev = NULL;
  179. return 0;
  180. }
  181. #ifdef CONFIG_PM
  182. static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  183. {
  184. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  185. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  186. return twl4030_wdt_disable(wdt);
  187. return 0;
  188. }
  189. static int twl4030_wdt_resume(struct platform_device *pdev)
  190. {
  191. struct twl4030_wdt *wdt = platform_get_drvdata(pdev);
  192. if (wdt->state & TWL4030_WDT_STATE_ACTIVE)
  193. return twl4030_wdt_enable(wdt);
  194. return 0;
  195. }
  196. #else
  197. #define twl4030_wdt_suspend NULL
  198. #define twl4030_wdt_resume NULL
  199. #endif
  200. static struct platform_driver twl4030_wdt_driver = {
  201. .probe = twl4030_wdt_probe,
  202. .remove = __devexit_p(twl4030_wdt_remove),
  203. .suspend = twl4030_wdt_suspend,
  204. .resume = twl4030_wdt_resume,
  205. .driver = {
  206. .owner = THIS_MODULE,
  207. .name = "twl4030_wdt",
  208. },
  209. };
  210. static int __devinit twl4030_wdt_init(void)
  211. {
  212. return platform_driver_register(&twl4030_wdt_driver);
  213. }
  214. module_init(twl4030_wdt_init);
  215. static void __devexit twl4030_wdt_exit(void)
  216. {
  217. platform_driver_unregister(&twl4030_wdt_driver);
  218. }
  219. module_exit(twl4030_wdt_exit);
  220. MODULE_AUTHOR("Nokia Corporation");
  221. MODULE_LICENSE("GPL");
  222. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  223. MODULE_ALIAS("platform:twl4030_wdt");