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