da9063_wdt.c 7.9 KB

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