da9062_wdt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * da9062_wdt.c - WDT device driver for DA9062
  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. int ret;
  83. ret = da9062_reset_watchdog_timer(wdt);
  84. if (ret)
  85. return ret;
  86. return regmap_update_bits(chip->regmap,
  87. DA9062AA_CONTROL_D,
  88. DA9062AA_TWDSCALE_MASK,
  89. regval);
  90. }
  91. static int da9062_wdt_start(struct watchdog_device *wdd)
  92. {
  93. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  94. unsigned int selector;
  95. int ret;
  96. selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  97. ret = da9062_wdt_update_timeout_register(wdt, selector);
  98. if (ret)
  99. dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
  100. ret);
  101. return ret;
  102. }
  103. static int da9062_wdt_stop(struct watchdog_device *wdd)
  104. {
  105. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  106. int ret;
  107. ret = da9062_reset_watchdog_timer(wdt);
  108. if (ret) {
  109. dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
  110. ret);
  111. return ret;
  112. }
  113. ret = regmap_update_bits(wdt->hw->regmap,
  114. DA9062AA_CONTROL_D,
  115. DA9062AA_TWDSCALE_MASK,
  116. DA9062_TWDSCALE_DISABLE);
  117. if (ret)
  118. dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
  119. ret);
  120. return ret;
  121. }
  122. static int da9062_wdt_ping(struct watchdog_device *wdd)
  123. {
  124. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  125. int ret;
  126. ret = da9062_reset_watchdog_timer(wdt);
  127. if (ret)
  128. dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
  129. ret);
  130. return ret;
  131. }
  132. static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
  133. unsigned int timeout)
  134. {
  135. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  136. unsigned int selector;
  137. int ret;
  138. selector = da9062_wdt_timeout_to_sel(timeout);
  139. ret = da9062_wdt_update_timeout_register(wdt, selector);
  140. if (ret)
  141. dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
  142. ret);
  143. else
  144. wdd->timeout = wdt_timeout[selector];
  145. return ret;
  146. }
  147. static const struct watchdog_info da9062_watchdog_info = {
  148. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  149. .identity = "DA9062 WDT",
  150. };
  151. static const struct watchdog_ops da9062_watchdog_ops = {
  152. .owner = THIS_MODULE,
  153. .start = da9062_wdt_start,
  154. .stop = da9062_wdt_stop,
  155. .ping = da9062_wdt_ping,
  156. .set_timeout = da9062_wdt_set_timeout,
  157. };
  158. static int da9062_wdt_probe(struct platform_device *pdev)
  159. {
  160. int ret;
  161. struct da9062 *chip;
  162. struct da9062_watchdog *wdt;
  163. chip = dev_get_drvdata(pdev->dev.parent);
  164. if (!chip)
  165. return -EINVAL;
  166. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  167. if (!wdt)
  168. return -ENOMEM;
  169. wdt->hw = chip;
  170. wdt->wdtdev.info = &da9062_watchdog_info;
  171. wdt->wdtdev.ops = &da9062_watchdog_ops;
  172. wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
  173. wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
  174. wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
  175. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  176. wdt->wdtdev.parent = &pdev->dev;
  177. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  178. dev_set_drvdata(&pdev->dev, wdt);
  179. ret = watchdog_register_device(&wdt->wdtdev);
  180. if (ret < 0) {
  181. dev_err(wdt->hw->dev,
  182. "watchdog registration failed (%d)\n", ret);
  183. return ret;
  184. }
  185. da9062_set_window_start(wdt);
  186. ret = da9062_wdt_ping(&wdt->wdtdev);
  187. if (ret < 0)
  188. watchdog_unregister_device(&wdt->wdtdev);
  189. return ret;
  190. }
  191. static int da9062_wdt_remove(struct platform_device *pdev)
  192. {
  193. struct da9062_watchdog *wdt = dev_get_drvdata(&pdev->dev);
  194. watchdog_unregister_device(&wdt->wdtdev);
  195. return 0;
  196. }
  197. static struct platform_driver da9062_wdt_driver = {
  198. .probe = da9062_wdt_probe,
  199. .remove = da9062_wdt_remove,
  200. .driver = {
  201. .name = "da9062-watchdog",
  202. },
  203. };
  204. module_platform_driver(da9062_wdt_driver);
  205. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  206. MODULE_DESCRIPTION("WDT device driver for Dialog DA9062");
  207. MODULE_LICENSE("GPL");
  208. MODULE_ALIAS("platform:da9062-watchdog");