da9055_wdt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * System monitoring driver for DA9055 PMICs.
  4. *
  5. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  6. *
  7. * Author: David Dajun Chen <dchen@diasemi.com>
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/delay.h>
  17. #include <linux/mfd/da9055/core.h>
  18. #include <linux/mfd/da9055/reg.h>
  19. static bool nowayout = WATCHDOG_NOWAYOUT;
  20. module_param(nowayout, bool, 0);
  21. MODULE_PARM_DESC(nowayout,
  22. "Watchdog cannot be stopped once started (default="
  23. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  24. #define DA9055_DEF_TIMEOUT 4
  25. #define DA9055_TWDMIN 256
  26. struct da9055_wdt_data {
  27. struct watchdog_device wdt;
  28. struct da9055 *da9055;
  29. };
  30. static const struct {
  31. u8 reg_val;
  32. int user_time; /* In seconds */
  33. } da9055_wdt_maps[] = {
  34. { 0, 0 },
  35. { 1, 2 },
  36. { 2, 4 },
  37. { 3, 8 },
  38. { 4, 16 },
  39. { 5, 32 },
  40. { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
  41. { 6, 65 },
  42. { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
  43. { 7, 131 },
  44. };
  45. static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
  46. unsigned int timeout)
  47. {
  48. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  49. struct da9055 *da9055 = driver_data->da9055;
  50. int ret, i;
  51. for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
  52. if (da9055_wdt_maps[i].user_time == timeout)
  53. break;
  54. if (i == ARRAY_SIZE(da9055_wdt_maps))
  55. ret = -EINVAL;
  56. else
  57. ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
  58. DA9055_TWDSCALE_MASK,
  59. da9055_wdt_maps[i].reg_val <<
  60. DA9055_TWDSCALE_SHIFT);
  61. if (ret < 0) {
  62. dev_err(da9055->dev,
  63. "Failed to update timescale bit, %d\n", ret);
  64. return ret;
  65. }
  66. wdt_dev->timeout = timeout;
  67. return 0;
  68. }
  69. static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
  70. {
  71. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  72. struct da9055 *da9055 = driver_data->da9055;
  73. /*
  74. * We have a minimum time for watchdog window called TWDMIN. A write
  75. * to the watchdog before this elapsed time will cause an error.
  76. */
  77. mdelay(DA9055_TWDMIN);
  78. /* Reset the watchdog timer */
  79. return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
  80. DA9055_WATCHDOG_MASK, 1);
  81. }
  82. static int da9055_wdt_start(struct watchdog_device *wdt_dev)
  83. {
  84. return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  85. }
  86. static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
  87. {
  88. return da9055_wdt_set_timeout(wdt_dev, 0);
  89. }
  90. static const struct watchdog_info da9055_wdt_info = {
  91. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  92. .identity = "DA9055 Watchdog",
  93. };
  94. static const struct watchdog_ops da9055_wdt_ops = {
  95. .owner = THIS_MODULE,
  96. .start = da9055_wdt_start,
  97. .stop = da9055_wdt_stop,
  98. .ping = da9055_wdt_ping,
  99. .set_timeout = da9055_wdt_set_timeout,
  100. };
  101. static int da9055_wdt_probe(struct platform_device *pdev)
  102. {
  103. struct device *dev = &pdev->dev;
  104. struct da9055 *da9055 = dev_get_drvdata(dev->parent);
  105. struct da9055_wdt_data *driver_data;
  106. struct watchdog_device *da9055_wdt;
  107. int ret;
  108. driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
  109. if (!driver_data)
  110. return -ENOMEM;
  111. driver_data->da9055 = da9055;
  112. da9055_wdt = &driver_data->wdt;
  113. da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
  114. da9055_wdt->info = &da9055_wdt_info;
  115. da9055_wdt->ops = &da9055_wdt_ops;
  116. da9055_wdt->parent = dev;
  117. watchdog_set_nowayout(da9055_wdt, nowayout);
  118. watchdog_set_drvdata(da9055_wdt, driver_data);
  119. ret = da9055_wdt_stop(da9055_wdt);
  120. if (ret < 0) {
  121. dev_err(dev, "Failed to stop watchdog, %d\n", ret);
  122. return ret;
  123. }
  124. ret = devm_watchdog_register_device(dev, &driver_data->wdt);
  125. if (ret != 0)
  126. dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
  127. ret);
  128. return ret;
  129. }
  130. static struct platform_driver da9055_wdt_driver = {
  131. .probe = da9055_wdt_probe,
  132. .driver = {
  133. .name = "da9055-watchdog",
  134. },
  135. };
  136. module_platform_driver(da9055_wdt_driver);
  137. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  138. MODULE_DESCRIPTION("DA9055 watchdog");
  139. MODULE_LICENSE("GPL");
  140. MODULE_ALIAS("platform:da9055-watchdog");