da9055_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. struct kref kref;
  34. };
  35. static const struct {
  36. u8 reg_val;
  37. int user_time; /* In seconds */
  38. } da9055_wdt_maps[] = {
  39. { 0, 0 },
  40. { 1, 2 },
  41. { 2, 4 },
  42. { 3, 8 },
  43. { 4, 16 },
  44. { 5, 32 },
  45. { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
  46. { 6, 65 },
  47. { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
  48. { 7, 131 },
  49. };
  50. static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
  51. unsigned int timeout)
  52. {
  53. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  54. struct da9055 *da9055 = driver_data->da9055;
  55. int ret, i;
  56. for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
  57. if (da9055_wdt_maps[i].user_time == timeout)
  58. break;
  59. if (i == ARRAY_SIZE(da9055_wdt_maps))
  60. ret = -EINVAL;
  61. else
  62. ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
  63. DA9055_TWDSCALE_MASK,
  64. da9055_wdt_maps[i].reg_val <<
  65. DA9055_TWDSCALE_SHIFT);
  66. if (ret < 0) {
  67. dev_err(da9055->dev,
  68. "Failed to update timescale bit, %d\n", ret);
  69. return ret;
  70. }
  71. wdt_dev->timeout = timeout;
  72. return 0;
  73. }
  74. static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
  75. {
  76. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  77. struct da9055 *da9055 = driver_data->da9055;
  78. /*
  79. * We have a minimum time for watchdog window called TWDMIN. A write
  80. * to the watchdog before this elapsed time will cause an error.
  81. */
  82. mdelay(DA9055_TWDMIN);
  83. /* Reset the watchdog timer */
  84. return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
  85. DA9055_WATCHDOG_MASK, 1);
  86. }
  87. static void da9055_wdt_release_resources(struct kref *r)
  88. {
  89. }
  90. static void da9055_wdt_ref(struct watchdog_device *wdt_dev)
  91. {
  92. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  93. kref_get(&driver_data->kref);
  94. }
  95. static void da9055_wdt_unref(struct watchdog_device *wdt_dev)
  96. {
  97. struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  98. kref_put(&driver_data->kref, da9055_wdt_release_resources);
  99. }
  100. static int da9055_wdt_start(struct watchdog_device *wdt_dev)
  101. {
  102. return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  103. }
  104. static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
  105. {
  106. return da9055_wdt_set_timeout(wdt_dev, 0);
  107. }
  108. static struct watchdog_info da9055_wdt_info = {
  109. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  110. .identity = "DA9055 Watchdog",
  111. };
  112. static const struct watchdog_ops da9055_wdt_ops = {
  113. .owner = THIS_MODULE,
  114. .start = da9055_wdt_start,
  115. .stop = da9055_wdt_stop,
  116. .ping = da9055_wdt_ping,
  117. .set_timeout = da9055_wdt_set_timeout,
  118. .ref = da9055_wdt_ref,
  119. .unref = da9055_wdt_unref,
  120. };
  121. static int da9055_wdt_probe(struct platform_device *pdev)
  122. {
  123. struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
  124. struct da9055_wdt_data *driver_data;
  125. struct watchdog_device *da9055_wdt;
  126. int ret;
  127. driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
  128. GFP_KERNEL);
  129. if (!driver_data)
  130. return -ENOMEM;
  131. driver_data->da9055 = da9055;
  132. da9055_wdt = &driver_data->wdt;
  133. da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
  134. da9055_wdt->info = &da9055_wdt_info;
  135. da9055_wdt->ops = &da9055_wdt_ops;
  136. da9055_wdt->parent = &pdev->dev;
  137. watchdog_set_nowayout(da9055_wdt, nowayout);
  138. watchdog_set_drvdata(da9055_wdt, driver_data);
  139. kref_init(&driver_data->kref);
  140. ret = da9055_wdt_stop(da9055_wdt);
  141. if (ret < 0) {
  142. dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret);
  143. goto err;
  144. }
  145. platform_set_drvdata(pdev, driver_data);
  146. ret = watchdog_register_device(&driver_data->wdt);
  147. if (ret != 0)
  148. dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
  149. ret);
  150. err:
  151. return ret;
  152. }
  153. static int da9055_wdt_remove(struct platform_device *pdev)
  154. {
  155. struct da9055_wdt_data *driver_data = platform_get_drvdata(pdev);
  156. watchdog_unregister_device(&driver_data->wdt);
  157. kref_put(&driver_data->kref, da9055_wdt_release_resources);
  158. return 0;
  159. }
  160. static struct platform_driver da9055_wdt_driver = {
  161. .probe = da9055_wdt_probe,
  162. .remove = da9055_wdt_remove,
  163. .driver = {
  164. .name = "da9055-watchdog",
  165. },
  166. };
  167. module_platform_driver(da9055_wdt_driver);
  168. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  169. MODULE_DESCRIPTION("DA9055 watchdog");
  170. MODULE_LICENSE("GPL");
  171. MODULE_ALIAS("platform:da9055-watchdog");