da9063_wdt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog driver for DA9063 PMICs.
  4. *
  5. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  6. *
  7. * Author: Mariusz Wojtasik <[email protected]>
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/watchdog.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/mfd/da9063/registers.h>
  19. #include <linux/mfd/da9063/core.h>
  20. #include <linux/property.h>
  21. #include <linux/regmap.h>
  22. /*
  23. * Watchdog selector to timeout in seconds.
  24. * 0: WDT disabled;
  25. * others: timeout = 2048 ms * 2^(TWDSCALE-1).
  26. */
  27. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  28. static bool use_sw_pm;
  29. #define DA9063_TWDSCALE_DISABLE 0
  30. #define DA9063_TWDSCALE_MIN 1
  31. #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  32. #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN]
  33. #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX]
  34. #define DA9063_WDG_TIMEOUT wdt_timeout[3]
  35. #define DA9063_RESET_PROTECTION_MS 256
  36. static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  37. {
  38. unsigned int i;
  39. for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
  40. if (wdt_timeout[i] >= secs)
  41. return i;
  42. }
  43. return DA9063_TWDSCALE_MAX;
  44. }
  45. /*
  46. * Read the currently active timeout.
  47. * Zero means the watchdog is disabled.
  48. */
  49. static unsigned int da9063_wdt_read_timeout(struct da9063 *da9063)
  50. {
  51. unsigned int val;
  52. regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
  53. return wdt_timeout[val & DA9063_TWDSCALE_MASK];
  54. }
  55. static int da9063_wdt_disable_timer(struct da9063 *da9063)
  56. {
  57. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  58. DA9063_TWDSCALE_MASK,
  59. DA9063_TWDSCALE_DISABLE);
  60. }
  61. static int
  62. da9063_wdt_update_timeout(struct da9063 *da9063, unsigned int timeout)
  63. {
  64. unsigned int regval;
  65. int ret;
  66. /*
  67. * The watchdog triggers a reboot if a timeout value is already
  68. * programmed because the timeout value combines two functions
  69. * in one: indicating the counter limit and starting the watchdog.
  70. * The watchdog must be disabled to be able to change the timeout
  71. * value if the watchdog is already running. Then we can set the
  72. * new timeout value which enables the watchdog again.
  73. */
  74. ret = da9063_wdt_disable_timer(da9063);
  75. if (ret)
  76. return ret;
  77. usleep_range(150, 300);
  78. regval = da9063_wdt_timeout_to_sel(timeout);
  79. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  80. DA9063_TWDSCALE_MASK, regval);
  81. }
  82. static int da9063_wdt_start(struct watchdog_device *wdd)
  83. {
  84. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  85. int ret;
  86. ret = da9063_wdt_update_timeout(da9063, wdd->timeout);
  87. if (ret)
  88. dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
  89. ret);
  90. return ret;
  91. }
  92. static int da9063_wdt_stop(struct watchdog_device *wdd)
  93. {
  94. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  95. int ret;
  96. ret = da9063_wdt_disable_timer(da9063);
  97. if (ret)
  98. dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n",
  99. ret);
  100. return ret;
  101. }
  102. static int da9063_wdt_ping(struct watchdog_device *wdd)
  103. {
  104. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  105. int ret;
  106. /*
  107. * Prevent pings from occurring late in system poweroff/reboot sequence
  108. * and possibly locking out restart handler from accessing i2c bus.
  109. */
  110. if (system_state > SYSTEM_RUNNING)
  111. return 0;
  112. ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
  113. DA9063_WATCHDOG);
  114. if (ret)
  115. dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  116. ret);
  117. return ret;
  118. }
  119. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  120. unsigned int timeout)
  121. {
  122. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  123. int ret = 0;
  124. /*
  125. * There are two cases when a set_timeout() will be called:
  126. * 1. The watchdog is off and someone wants to set the timeout for the
  127. * further use.
  128. * 2. The watchdog is already running and a new timeout value should be
  129. * set.
  130. *
  131. * The watchdog can't store a timeout value not equal zero without
  132. * enabling the watchdog, so the timeout must be buffered by the driver.
  133. */
  134. if (watchdog_active(wdd))
  135. ret = da9063_wdt_update_timeout(da9063, timeout);
  136. if (ret)
  137. dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  138. ret);
  139. else
  140. wdd->timeout = wdt_timeout[da9063_wdt_timeout_to_sel(timeout)];
  141. return ret;
  142. }
  143. static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  144. void *data)
  145. {
  146. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  147. struct i2c_client *client = to_i2c_client(da9063->dev);
  148. int ret;
  149. /* Don't use regmap because it is not atomic safe */
  150. ret = i2c_smbus_write_byte_data(client, DA9063_REG_CONTROL_F,
  151. DA9063_SHUTDOWN);
  152. if (ret < 0)
  153. dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n",
  154. ret);
  155. /* wait for reset to assert... */
  156. mdelay(500);
  157. return ret;
  158. }
  159. static const struct watchdog_info da9063_watchdog_info = {
  160. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  161. .identity = "DA9063 Watchdog",
  162. };
  163. static const struct watchdog_ops da9063_watchdog_ops = {
  164. .owner = THIS_MODULE,
  165. .start = da9063_wdt_start,
  166. .stop = da9063_wdt_stop,
  167. .ping = da9063_wdt_ping,
  168. .set_timeout = da9063_wdt_set_timeout,
  169. .restart = da9063_wdt_restart,
  170. };
  171. static int da9063_wdt_probe(struct platform_device *pdev)
  172. {
  173. struct device *dev = &pdev->dev;
  174. struct da9063 *da9063;
  175. struct watchdog_device *wdd;
  176. unsigned int timeout;
  177. if (!dev->parent)
  178. return -EINVAL;
  179. da9063 = dev_get_drvdata(dev->parent);
  180. if (!da9063)
  181. return -EINVAL;
  182. wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
  183. if (!wdd)
  184. return -ENOMEM;
  185. use_sw_pm = device_property_present(dev, "dlg,use-sw-pm");
  186. wdd->info = &da9063_watchdog_info;
  187. wdd->ops = &da9063_watchdog_ops;
  188. wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT;
  189. wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT;
  190. wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS;
  191. wdd->parent = dev;
  192. wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  193. watchdog_set_restart_priority(wdd, 128);
  194. watchdog_set_drvdata(wdd, da9063);
  195. dev_set_drvdata(dev, wdd);
  196. wdd->timeout = DA9063_WDG_TIMEOUT;
  197. /* Use pre-configured timeout if watchdog is already running. */
  198. timeout = da9063_wdt_read_timeout(da9063);
  199. if (timeout)
  200. wdd->timeout = timeout;
  201. /* Set timeout, maybe override it with DT value, scale it */
  202. watchdog_init_timeout(wdd, 0, dev);
  203. da9063_wdt_set_timeout(wdd, wdd->timeout);
  204. /* Update timeout if the watchdog is already running. */
  205. if (timeout) {
  206. da9063_wdt_update_timeout(da9063, wdd->timeout);
  207. set_bit(WDOG_HW_RUNNING, &wdd->status);
  208. }
  209. return devm_watchdog_register_device(dev, wdd);
  210. }
  211. static int __maybe_unused da9063_wdt_suspend(struct device *dev)
  212. {
  213. struct watchdog_device *wdd = dev_get_drvdata(dev);
  214. if (!use_sw_pm)
  215. return 0;
  216. if (watchdog_active(wdd))
  217. return da9063_wdt_stop(wdd);
  218. return 0;
  219. }
  220. static int __maybe_unused da9063_wdt_resume(struct device *dev)
  221. {
  222. struct watchdog_device *wdd = dev_get_drvdata(dev);
  223. if (!use_sw_pm)
  224. return 0;
  225. if (watchdog_active(wdd))
  226. return da9063_wdt_start(wdd);
  227. return 0;
  228. }
  229. static SIMPLE_DEV_PM_OPS(da9063_wdt_pm_ops,
  230. da9063_wdt_suspend, da9063_wdt_resume);
  231. static struct platform_driver da9063_wdt_driver = {
  232. .probe = da9063_wdt_probe,
  233. .driver = {
  234. .name = DA9063_DRVNAME_WATCHDOG,
  235. .pm = &da9063_wdt_pm_ops,
  236. },
  237. };
  238. module_platform_driver(da9063_wdt_driver);
  239. MODULE_AUTHOR("Mariusz Wojtasik <[email protected]>");
  240. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  241. MODULE_LICENSE("GPL");
  242. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);