da9062_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/i2c.h>
  14. #include <linux/delay.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/mfd/da9062/registers.h>
  17. #include <linux/mfd/da9062/core.h>
  18. #include <linux/property.h>
  19. #include <linux/regmap.h>
  20. #include <linux/of.h>
  21. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  22. #define DA9062_TWDSCALE_DISABLE 0
  23. #define DA9062_TWDSCALE_MIN 1
  24. #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  25. #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN]
  26. #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX]
  27. #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1]
  28. #define DA9062_RESET_PROTECTION_MS 300
  29. struct da9062_watchdog {
  30. struct da9062 *hw;
  31. struct watchdog_device wdtdev;
  32. bool use_sw_pm;
  33. };
  34. static unsigned int da9062_wdt_read_timeout(struct da9062_watchdog *wdt)
  35. {
  36. unsigned int val;
  37. regmap_read(wdt->hw->regmap, DA9062AA_CONTROL_D, &val);
  38. return wdt_timeout[val & DA9062AA_TWDSCALE_MASK];
  39. }
  40. static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
  41. {
  42. unsigned int i;
  43. for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
  44. if (wdt_timeout[i] >= secs)
  45. return i;
  46. }
  47. return DA9062_TWDSCALE_MAX;
  48. }
  49. static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
  50. {
  51. return regmap_update_bits(wdt->hw->regmap, DA9062AA_CONTROL_F,
  52. DA9062AA_WATCHDOG_MASK,
  53. DA9062AA_WATCHDOG_MASK);
  54. }
  55. static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
  56. unsigned int regval)
  57. {
  58. struct da9062 *chip = wdt->hw;
  59. regmap_update_bits(chip->regmap,
  60. DA9062AA_CONTROL_D,
  61. DA9062AA_TWDSCALE_MASK,
  62. DA9062_TWDSCALE_DISABLE);
  63. usleep_range(150, 300);
  64. return regmap_update_bits(chip->regmap,
  65. DA9062AA_CONTROL_D,
  66. DA9062AA_TWDSCALE_MASK,
  67. regval);
  68. }
  69. static int da9062_wdt_start(struct watchdog_device *wdd)
  70. {
  71. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  72. unsigned int selector;
  73. int ret;
  74. selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  75. ret = da9062_wdt_update_timeout_register(wdt, selector);
  76. if (ret)
  77. dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
  78. ret);
  79. return ret;
  80. }
  81. static int da9062_wdt_stop(struct watchdog_device *wdd)
  82. {
  83. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  84. int ret;
  85. ret = regmap_update_bits(wdt->hw->regmap,
  86. DA9062AA_CONTROL_D,
  87. DA9062AA_TWDSCALE_MASK,
  88. DA9062_TWDSCALE_DISABLE);
  89. if (ret)
  90. dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
  91. ret);
  92. return ret;
  93. }
  94. static int da9062_wdt_ping(struct watchdog_device *wdd)
  95. {
  96. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  97. int ret;
  98. /*
  99. * Prevent pings from occurring late in system poweroff/reboot sequence
  100. * and possibly locking out restart handler from accessing i2c bus.
  101. */
  102. if (system_state > SYSTEM_RUNNING)
  103. return 0;
  104. ret = da9062_reset_watchdog_timer(wdt);
  105. if (ret)
  106. dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
  107. ret);
  108. return ret;
  109. }
  110. static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
  111. unsigned int timeout)
  112. {
  113. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  114. unsigned int selector;
  115. int ret;
  116. selector = da9062_wdt_timeout_to_sel(timeout);
  117. ret = da9062_wdt_update_timeout_register(wdt, selector);
  118. if (ret)
  119. dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
  120. ret);
  121. else
  122. wdd->timeout = wdt_timeout[selector];
  123. return ret;
  124. }
  125. static int da9062_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  126. void *data)
  127. {
  128. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  129. struct i2c_client *client = to_i2c_client(wdt->hw->dev);
  130. int ret;
  131. /* Don't use regmap because it is not atomic safe */
  132. ret = i2c_smbus_write_byte_data(client, DA9062AA_CONTROL_F,
  133. DA9062AA_SHUTDOWN_MASK);
  134. if (ret < 0)
  135. dev_alert(wdt->hw->dev, "Failed to shutdown (err = %d)\n",
  136. ret);
  137. /* wait for reset to assert... */
  138. mdelay(500);
  139. return ret;
  140. }
  141. static const struct watchdog_info da9062_watchdog_info = {
  142. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  143. .identity = "DA9062 WDT",
  144. };
  145. static const struct watchdog_ops da9062_watchdog_ops = {
  146. .owner = THIS_MODULE,
  147. .start = da9062_wdt_start,
  148. .stop = da9062_wdt_stop,
  149. .ping = da9062_wdt_ping,
  150. .set_timeout = da9062_wdt_set_timeout,
  151. .restart = da9062_wdt_restart,
  152. };
  153. static const struct of_device_id da9062_compatible_id_table[] = {
  154. { .compatible = "dlg,da9062-watchdog", },
  155. { },
  156. };
  157. MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
  158. static int da9062_wdt_probe(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. unsigned int timeout;
  162. struct da9062 *chip;
  163. struct da9062_watchdog *wdt;
  164. chip = dev_get_drvdata(dev->parent);
  165. if (!chip)
  166. return -EINVAL;
  167. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  168. if (!wdt)
  169. return -ENOMEM;
  170. wdt->use_sw_pm = device_property_present(dev, "dlg,use-sw-pm");
  171. wdt->hw = chip;
  172. wdt->wdtdev.info = &da9062_watchdog_info;
  173. wdt->wdtdev.ops = &da9062_watchdog_ops;
  174. wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
  175. wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
  176. wdt->wdtdev.min_hw_heartbeat_ms = DA9062_RESET_PROTECTION_MS;
  177. wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
  178. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  179. wdt->wdtdev.parent = dev;
  180. watchdog_set_restart_priority(&wdt->wdtdev, 128);
  181. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  182. dev_set_drvdata(dev, &wdt->wdtdev);
  183. timeout = da9062_wdt_read_timeout(wdt);
  184. if (timeout)
  185. wdt->wdtdev.timeout = timeout;
  186. /* Set timeout from DT value if available */
  187. watchdog_init_timeout(&wdt->wdtdev, 0, dev);
  188. if (timeout) {
  189. da9062_wdt_set_timeout(&wdt->wdtdev, wdt->wdtdev.timeout);
  190. set_bit(WDOG_HW_RUNNING, &wdt->wdtdev.status);
  191. }
  192. return devm_watchdog_register_device(dev, &wdt->wdtdev);
  193. }
  194. static int __maybe_unused da9062_wdt_suspend(struct device *dev)
  195. {
  196. struct watchdog_device *wdd = dev_get_drvdata(dev);
  197. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  198. if (!wdt->use_sw_pm)
  199. return 0;
  200. if (watchdog_active(wdd))
  201. return da9062_wdt_stop(wdd);
  202. return 0;
  203. }
  204. static int __maybe_unused da9062_wdt_resume(struct device *dev)
  205. {
  206. struct watchdog_device *wdd = dev_get_drvdata(dev);
  207. struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
  208. if (!wdt->use_sw_pm)
  209. return 0;
  210. if (watchdog_active(wdd))
  211. return da9062_wdt_start(wdd);
  212. return 0;
  213. }
  214. static SIMPLE_DEV_PM_OPS(da9062_wdt_pm_ops,
  215. da9062_wdt_suspend, da9062_wdt_resume);
  216. static struct platform_driver da9062_wdt_driver = {
  217. .probe = da9062_wdt_probe,
  218. .driver = {
  219. .name = "da9062-watchdog",
  220. .pm = &da9062_wdt_pm_ops,
  221. .of_match_table = da9062_compatible_id_table,
  222. },
  223. };
  224. module_platform_driver(da9062_wdt_driver);
  225. MODULE_AUTHOR("S Twiss <[email protected]>");
  226. MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
  227. MODULE_LICENSE("GPL");
  228. MODULE_ALIAS("platform:da9062-watchdog");