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. wdt->parent = &pdev->dev;
  75. watchdog_set_nowayout(wdt, nowayout);
  76. platform_set_drvdata(pdev, wdt);
  77. twl4030_wdt_stop(wdt);
  78. ret = watchdog_register_device(wdt);
  79. if (ret)
  80. return ret;
  81. return 0;
  82. }
  83. static int twl4030_wdt_remove(struct platform_device *pdev)
  84. {
  85. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  86. watchdog_unregister_device(wdt);
  87. return 0;
  88. }
  89. #ifdef CONFIG_PM
  90. static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  91. {
  92. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  93. if (watchdog_active(wdt))
  94. return twl4030_wdt_stop(wdt);
  95. return 0;
  96. }
  97. static int twl4030_wdt_resume(struct platform_device *pdev)
  98. {
  99. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  100. if (watchdog_active(wdt))
  101. return twl4030_wdt_start(wdt);
  102. return 0;
  103. }
  104. #else
  105. #define twl4030_wdt_suspend NULL
  106. #define twl4030_wdt_resume NULL
  107. #endif
  108. static const struct of_device_id twl_wdt_of_match[] = {
  109. { .compatible = "ti,twl4030-wdt", },
  110. { },
  111. };
  112. MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
  113. static struct platform_driver twl4030_wdt_driver = {
  114. .probe = twl4030_wdt_probe,
  115. .remove = twl4030_wdt_remove,
  116. .suspend = twl4030_wdt_suspend,
  117. .resume = twl4030_wdt_resume,
  118. .driver = {
  119. .name = "twl4030_wdt",
  120. .of_match_table = twl_wdt_of_match,
  121. },
  122. };
  123. module_platform_driver(twl4030_wdt_driver);
  124. MODULE_AUTHOR("Nokia Corporation");
  125. MODULE_LICENSE("GPL");
  126. MODULE_ALIAS("platform:twl4030_wdt");