da9055_wdt.c 4.5 KB

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