twl4030_wdt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/watchdog.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/i2c/twl.h>
  27. #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
  28. static bool nowayout = WATCHDOG_NOWAYOUT;
  29. module_param(nowayout, bool, 0);
  30. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  31. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. static int twl4030_wdt_write(unsigned char val)
  33. {
  34. return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val,
  35. TWL4030_WATCHDOG_CFG_REG_OFFS);
  36. }
  37. static int twl4030_wdt_start(struct watchdog_device *wdt)
  38. {
  39. return twl4030_wdt_write(wdt->timeout + 1);
  40. }
  41. static int twl4030_wdt_stop(struct watchdog_device *wdt)
  42. {
  43. return twl4030_wdt_write(0);
  44. }
  45. static int twl4030_wdt_set_timeout(struct watchdog_device *wdt,
  46. unsigned int timeout)
  47. {
  48. wdt->timeout = timeout;
  49. return 0;
  50. }
  51. static const struct watchdog_info twl4030_wdt_info = {
  52. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  53. .identity = "TWL4030 Watchdog",
  54. };
  55. static const struct watchdog_ops twl4030_wdt_ops = {
  56. .owner = THIS_MODULE,
  57. .start = twl4030_wdt_start,
  58. .stop = twl4030_wdt_stop,
  59. .set_timeout = twl4030_wdt_set_timeout,
  60. };
  61. static int twl4030_wdt_probe(struct platform_device *pdev)
  62. {
  63. int ret = 0;
  64. struct watchdog_device *wdt;
  65. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  66. if (!wdt)
  67. return -ENOMEM;
  68. wdt->info = &twl4030_wdt_info;
  69. wdt->ops = &twl4030_wdt_ops;
  70. wdt->status = 0;
  71. wdt->timeout = 30;
  72. wdt->min_timeout = 1;
  73. wdt->max_timeout = 30;
  74. watchdog_set_nowayout(wdt, nowayout);
  75. platform_set_drvdata(pdev, wdt);
  76. twl4030_wdt_stop(wdt);
  77. ret = watchdog_register_device(wdt);
  78. if (ret)
  79. return ret;
  80. return 0;
  81. }
  82. static int twl4030_wdt_remove(struct platform_device *pdev)
  83. {
  84. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  85. watchdog_unregister_device(wdt);
  86. return 0;
  87. }
  88. #ifdef CONFIG_PM
  89. static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  90. {
  91. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  92. if (watchdog_active(wdt))
  93. return twl4030_wdt_stop(wdt);
  94. return 0;
  95. }
  96. static int twl4030_wdt_resume(struct platform_device *pdev)
  97. {
  98. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  99. if (watchdog_active(wdt))
  100. return twl4030_wdt_start(wdt);
  101. return 0;
  102. }
  103. #else
  104. #define twl4030_wdt_suspend NULL
  105. #define twl4030_wdt_resume NULL
  106. #endif
  107. static const struct of_device_id twl_wdt_of_match[] = {
  108. { .compatible = "ti,twl4030-wdt", },
  109. { },
  110. };
  111. MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
  112. static struct platform_driver twl4030_wdt_driver = {
  113. .probe = twl4030_wdt_probe,
  114. .remove = twl4030_wdt_remove,
  115. .suspend = twl4030_wdt_suspend,
  116. .resume = twl4030_wdt_resume,
  117. .driver = {
  118. .name = "twl4030_wdt",
  119. .of_match_table = twl_wdt_of_match,
  120. },
  121. };
  122. module_platform_driver(twl4030_wdt_driver);
  123. MODULE_AUTHOR("Nokia Corporation");
  124. MODULE_LICENSE("GPL");
  125. MODULE_ALIAS("platform:twl4030_wdt");