da9062_wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog device driver for DA9062 and DA9061 PMICs
  4. * Copyright (C) 2015 Dialog Semiconductor Ltd.
  5. *
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/watchdog.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/mfd/da9062/registers.h>
  16. #include <linux/mfd/da9062/core.h>
  17. #include <linux/regmap.h>
  18. #include <linux/of.h>
  19. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  20. #define DA9062_TWDSCALE_DISABLE 0
  21. #define DA9062_TWDSCALE_MIN 1
  22. #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  23. #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN]
  24. #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX]
  25. #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1]
  26. #define DA9062_RESET_PROTECTION_MS 300
  27. struct da9062_watchdog {
  28. struct da9062 *hw;
  29. struct watchdog_device wdtdev;
  30. };
  31. static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
  32. {
  33. unsigned int i;
  34. for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
  35. if (wdt_timeout[i] >= secs)
  36. return i;
  37. }
  38. return DA9062_TWDSCALE_MAX;
  39. }
  40. static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
  41. {
  42. return regmap_update_bits(wdt->hw->regmap, DA9062AA_CONTROL_F,
  43. DA9062AA_WATCHDOG_MASK,
  44. DA9062AA_WATCHDOG_MASK);
  45. }
  46. static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
  47. unsigned int regval)
  48. {
  49. struct da9062 *chip = wdt->hw;
  50. regmap_update_bits(chip->regmap,
  51. DA9062AA_CONTROL_D,
  52. DA9062AA_TWDSCALE_MASK,
  53. DA9062_TWDSCALE_DISABLE);
  54. usleep_range(150, 300);
  55. return regmap_update_bits(chip->regmap,
  56. DA9062AA_CONTROL_D,
  57. DA9062AA_TWDSCALE_MASK,
  58. regval);
  59. }
  60. static int da9062_wdt_start(struct watchdog_device *wdd)
  61. {
  62. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  63. unsigned int selector;
  64. int ret;
  65. selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  66. ret = da9062_wdt_update_timeout_register(wdt, selector);
  67. if (ret)
  68. dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
  69. ret);
  70. return ret;
  71. }
  72. static int da9062_wdt_stop(struct watchdog_device *wdd)
  73. {
  74. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  75. int ret;
  76. ret = regmap_update_bits(wdt->hw->regmap,
  77. DA9062AA_CONTROL_D,
  78. DA9062AA_TWDSCALE_MASK,
  79. DA9062_TWDSCALE_DISABLE);
  80. if (ret)
  81. dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
  82. ret);
  83. return ret;
  84. }
  85. static int da9062_wdt_ping(struct watchdog_device *wdd)
  86. {
  87. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  88. int ret;
  89. ret = da9062_reset_watchdog_timer(wdt);
  90. if (ret)
  91. dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
  92. ret);
  93. return ret;
  94. }
  95. static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
  96. unsigned int timeout)
  97. {
  98. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  99. unsigned int selector;
  100. int ret;
  101. selector = da9062_wdt_timeout_to_sel(timeout);
  102. ret = da9062_wdt_update_timeout_register(wdt, selector);
  103. if (ret)
  104. dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
  105. ret);
  106. else
  107. wdd->timeout = wdt_timeout[selector];
  108. return ret;
  109. }
  110. static int da9062_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  111. void *data)
  112. {
  113. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  114. int ret;
  115. ret = regmap_write(wdt->hw->regmap,
  116. DA9062AA_CONTROL_F,
  117. DA9062AA_SHUTDOWN_MASK);
  118. if (ret)
  119. dev_alert(wdt->hw->dev, "Failed to shutdown (err = %d)\n",
  120. ret);
  121. /* wait for reset to assert... */
  122. mdelay(500);
  123. return ret;
  124. }
  125. static const struct watchdog_info da9062_watchdog_info = {
  126. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  127. .identity = "DA9062 WDT",
  128. };
  129. static const struct watchdog_ops da9062_watchdog_ops = {
  130. .owner = THIS_MODULE,
  131. .start = da9062_wdt_start,
  132. .stop = da9062_wdt_stop,
  133. .ping = da9062_wdt_ping,
  134. .set_timeout = da9062_wdt_set_timeout,
  135. .restart = da9062_wdt_restart,
  136. };
  137. static const struct of_device_id da9062_compatible_id_table[] = {
  138. { .compatible = "dlg,da9062-watchdog", },
  139. { },
  140. };
  141. MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
  142. static int da9062_wdt_probe(struct platform_device *pdev)
  143. {
  144. struct device *dev = &pdev->dev;
  145. int ret;
  146. struct da9062 *chip;
  147. struct da9062_watchdog *wdt;
  148. chip = dev_get_drvdata(dev->parent);
  149. if (!chip)
  150. return -EINVAL;
  151. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  152. if (!wdt)
  153. return -ENOMEM;
  154. wdt->hw = chip;
  155. wdt->wdtdev.info = &da9062_watchdog_info;
  156. wdt->wdtdev.ops = &da9062_watchdog_ops;
  157. wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
  158. wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
  159. wdt->wdtdev.min_hw_heartbeat_ms = DA9062_RESET_PROTECTION_MS;
  160. wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
  161. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  162. wdt->wdtdev.parent = dev;
  163. watchdog_set_restart_priority(&wdt->wdtdev, 128);
  164. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  165. ret = devm_watchdog_register_device(dev, &wdt->wdtdev);
  166. if (ret < 0)
  167. return ret;
  168. return da9062_wdt_ping(&wdt->wdtdev);
  169. }
  170. static struct platform_driver da9062_wdt_driver = {
  171. .probe = da9062_wdt_probe,
  172. .driver = {
  173. .name = "da9062-watchdog",
  174. .of_match_table = da9062_compatible_id_table,
  175. },
  176. };
  177. module_platform_driver(da9062_wdt_driver);
  178. MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
  179. MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
  180. MODULE_LICENSE("GPL");
  181. MODULE_ALIAS("platform:da9062-watchdog");