da9062_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Watchdog device driver for DA9062 and DA9061 PMICs
  3. * Copyright (C) 2015 Dialog Semiconductor Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/watchdog.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/mfd/da9062/registers.h>
  24. #include <linux/mfd/da9062/core.h>
  25. #include <linux/regmap.h>
  26. #include <linux/of.h>
  27. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  28. #define DA9062_TWDSCALE_DISABLE 0
  29. #define DA9062_TWDSCALE_MIN 1
  30. #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  31. #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN]
  32. #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX]
  33. #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1]
  34. #define DA9062_RESET_PROTECTION_MS 300
  35. struct da9062_watchdog {
  36. struct da9062 *hw;
  37. struct watchdog_device wdtdev;
  38. unsigned long j_time_stamp;
  39. };
  40. static void da9062_set_window_start(struct da9062_watchdog *wdt)
  41. {
  42. wdt->j_time_stamp = jiffies;
  43. }
  44. static void da9062_apply_window_protection(struct da9062_watchdog *wdt)
  45. {
  46. unsigned long delay = msecs_to_jiffies(DA9062_RESET_PROTECTION_MS);
  47. unsigned long timeout = wdt->j_time_stamp + delay;
  48. unsigned long now = jiffies;
  49. unsigned int diff_ms;
  50. /* if time-limit has not elapsed then wait for remainder */
  51. if (time_before(now, timeout)) {
  52. diff_ms = jiffies_to_msecs(timeout-now);
  53. dev_dbg(wdt->hw->dev,
  54. "Kicked too quickly. Delaying %u msecs\n", diff_ms);
  55. msleep(diff_ms);
  56. }
  57. }
  58. static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
  59. {
  60. unsigned int i;
  61. for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
  62. if (wdt_timeout[i] >= secs)
  63. return i;
  64. }
  65. return DA9062_TWDSCALE_MAX;
  66. }
  67. static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
  68. {
  69. int ret;
  70. da9062_apply_window_protection(wdt);
  71. ret = regmap_update_bits(wdt->hw->regmap,
  72. DA9062AA_CONTROL_F,
  73. DA9062AA_WATCHDOG_MASK,
  74. DA9062AA_WATCHDOG_MASK);
  75. da9062_set_window_start(wdt);
  76. return ret;
  77. }
  78. static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
  79. unsigned int regval)
  80. {
  81. struct da9062 *chip = wdt->hw;
  82. return regmap_update_bits(chip->regmap,
  83. DA9062AA_CONTROL_D,
  84. DA9062AA_TWDSCALE_MASK,
  85. regval);
  86. }
  87. static int da9062_wdt_start(struct watchdog_device *wdd)
  88. {
  89. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  90. unsigned int selector;
  91. int ret;
  92. selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  93. ret = da9062_wdt_update_timeout_register(wdt, selector);
  94. if (ret)
  95. dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
  96. ret);
  97. return ret;
  98. }
  99. static int da9062_wdt_stop(struct watchdog_device *wdd)
  100. {
  101. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  102. int ret;
  103. ret = regmap_update_bits(wdt->hw->regmap,
  104. DA9062AA_CONTROL_D,
  105. DA9062AA_TWDSCALE_MASK,
  106. DA9062_TWDSCALE_DISABLE);
  107. if (ret)
  108. dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
  109. ret);
  110. return ret;
  111. }
  112. static int da9062_wdt_ping(struct watchdog_device *wdd)
  113. {
  114. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  115. int ret;
  116. ret = da9062_reset_watchdog_timer(wdt);
  117. if (ret)
  118. dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
  119. ret);
  120. return ret;
  121. }
  122. static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
  123. unsigned int timeout)
  124. {
  125. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  126. unsigned int selector;
  127. int ret;
  128. selector = da9062_wdt_timeout_to_sel(timeout);
  129. ret = da9062_wdt_update_timeout_register(wdt, selector);
  130. if (ret)
  131. dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
  132. ret);
  133. else
  134. wdd->timeout = wdt_timeout[selector];
  135. return ret;
  136. }
  137. static const struct watchdog_info da9062_watchdog_info = {
  138. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  139. .identity = "DA9062 WDT",
  140. };
  141. static const struct watchdog_ops da9062_watchdog_ops = {
  142. .owner = THIS_MODULE,
  143. .start = da9062_wdt_start,
  144. .stop = da9062_wdt_stop,
  145. .ping = da9062_wdt_ping,
  146. .set_timeout = da9062_wdt_set_timeout,
  147. };
  148. static const struct of_device_id da9062_compatible_id_table[] = {
  149. { .compatible = "dlg,da9062-watchdog", },
  150. { },
  151. };
  152. MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
  153. static int da9062_wdt_probe(struct platform_device *pdev)
  154. {
  155. int ret;
  156. struct da9062 *chip;
  157. struct da9062_watchdog *wdt;
  158. chip = dev_get_drvdata(pdev->dev.parent);
  159. if (!chip)
  160. return -EINVAL;
  161. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  162. if (!wdt)
  163. return -ENOMEM;
  164. wdt->hw = chip;
  165. wdt->wdtdev.info = &da9062_watchdog_info;
  166. wdt->wdtdev.ops = &da9062_watchdog_ops;
  167. wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
  168. wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
  169. wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
  170. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  171. wdt->wdtdev.parent = &pdev->dev;
  172. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  173. ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdtdev);
  174. if (ret < 0) {
  175. dev_err(wdt->hw->dev,
  176. "watchdog registration failed (%d)\n", ret);
  177. return ret;
  178. }
  179. da9062_set_window_start(wdt);
  180. return da9062_wdt_ping(&wdt->wdtdev);
  181. }
  182. static struct platform_driver da9062_wdt_driver = {
  183. .probe = da9062_wdt_probe,
  184. .driver = {
  185. .name = "da9062-watchdog",
  186. .of_match_table = da9062_compatible_id_table,
  187. },
  188. };
  189. module_platform_driver(da9062_wdt_driver);
  190. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  191. MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
  192. MODULE_LICENSE("GPL");
  193. MODULE_ALIAS("platform:da9062-watchdog");