twl4030_wdt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) Nokia Corporation
  4. *
  5. * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/watchdog.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/twl.h>
  15. #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
  16. static bool nowayout = WATCHDOG_NOWAYOUT;
  17. module_param(nowayout, bool, 0);
  18. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  19. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  20. static int twl4030_wdt_write(unsigned char val)
  21. {
  22. return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER, val,
  23. TWL4030_WATCHDOG_CFG_REG_OFFS);
  24. }
  25. static int twl4030_wdt_start(struct watchdog_device *wdt)
  26. {
  27. return twl4030_wdt_write(wdt->timeout + 1);
  28. }
  29. static int twl4030_wdt_stop(struct watchdog_device *wdt)
  30. {
  31. return twl4030_wdt_write(0);
  32. }
  33. static int twl4030_wdt_set_timeout(struct watchdog_device *wdt,
  34. unsigned int timeout)
  35. {
  36. wdt->timeout = timeout;
  37. return 0;
  38. }
  39. static const struct watchdog_info twl4030_wdt_info = {
  40. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  41. .identity = "TWL4030 Watchdog",
  42. };
  43. static const struct watchdog_ops twl4030_wdt_ops = {
  44. .owner = THIS_MODULE,
  45. .start = twl4030_wdt_start,
  46. .stop = twl4030_wdt_stop,
  47. .set_timeout = twl4030_wdt_set_timeout,
  48. };
  49. static int twl4030_wdt_probe(struct platform_device *pdev)
  50. {
  51. struct device *dev = &pdev->dev;
  52. struct watchdog_device *wdt;
  53. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  54. if (!wdt)
  55. return -ENOMEM;
  56. wdt->info = &twl4030_wdt_info;
  57. wdt->ops = &twl4030_wdt_ops;
  58. wdt->status = 0;
  59. wdt->timeout = 30;
  60. wdt->min_timeout = 1;
  61. wdt->max_timeout = 30;
  62. wdt->parent = dev;
  63. watchdog_set_nowayout(wdt, nowayout);
  64. platform_set_drvdata(pdev, wdt);
  65. twl4030_wdt_stop(wdt);
  66. return devm_watchdog_register_device(dev, wdt);
  67. }
  68. #ifdef CONFIG_PM
  69. static int twl4030_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  70. {
  71. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  72. if (watchdog_active(wdt))
  73. return twl4030_wdt_stop(wdt);
  74. return 0;
  75. }
  76. static int twl4030_wdt_resume(struct platform_device *pdev)
  77. {
  78. struct watchdog_device *wdt = platform_get_drvdata(pdev);
  79. if (watchdog_active(wdt))
  80. return twl4030_wdt_start(wdt);
  81. return 0;
  82. }
  83. #else
  84. #define twl4030_wdt_suspend NULL
  85. #define twl4030_wdt_resume NULL
  86. #endif
  87. static const struct of_device_id twl_wdt_of_match[] = {
  88. { .compatible = "ti,twl4030-wdt", },
  89. { },
  90. };
  91. MODULE_DEVICE_TABLE(of, twl_wdt_of_match);
  92. static struct platform_driver twl4030_wdt_driver = {
  93. .probe = twl4030_wdt_probe,
  94. .suspend = twl4030_wdt_suspend,
  95. .resume = twl4030_wdt_resume,
  96. .driver = {
  97. .name = "twl4030_wdt",
  98. .of_match_table = twl_wdt_of_match,
  99. },
  100. };
  101. module_platform_driver(twl4030_wdt_driver);
  102. MODULE_AUTHOR("Nokia Corporation");
  103. MODULE_LICENSE("GPL");
  104. MODULE_ALIAS("platform:twl4030_wdt");