da9063_wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/delay.h>
  17. #include <linux/mfd/da9063/registers.h>
  18. #include <linux/mfd/da9063/core.h>
  19. #include <linux/regmap.h>
  20. /*
  21. * Watchdog selector to timeout in seconds.
  22. * 0: WDT disabled;
  23. * others: timeout = 2048 ms * 2^(TWDSCALE-1).
  24. */
  25. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  26. #define DA9063_TWDSCALE_DISABLE 0
  27. #define DA9063_TWDSCALE_MIN 1
  28. #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  29. #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN]
  30. #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX]
  31. #define DA9063_WDG_TIMEOUT wdt_timeout[3]
  32. #define DA9063_RESET_PROTECTION_MS 256
  33. static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  34. {
  35. unsigned int i;
  36. for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
  37. if (wdt_timeout[i] >= secs)
  38. return i;
  39. }
  40. return DA9063_TWDSCALE_MAX;
  41. }
  42. /*
  43. * Read the currently active timeout.
  44. * Zero means the watchdog is disabled.
  45. */
  46. static unsigned int da9063_wdt_read_timeout(struct da9063 *da9063)
  47. {
  48. unsigned int val;
  49. regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
  50. return wdt_timeout[val & DA9063_TWDSCALE_MASK];
  51. }
  52. static int da9063_wdt_disable_timer(struct da9063 *da9063)
  53. {
  54. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  55. DA9063_TWDSCALE_MASK,
  56. DA9063_TWDSCALE_DISABLE);
  57. }
  58. static int
  59. da9063_wdt_update_timeout(struct da9063 *da9063, unsigned int timeout)
  60. {
  61. unsigned int regval;
  62. int ret;
  63. /*
  64. * The watchdog triggers a reboot if a timeout value is already
  65. * programmed because the timeout value combines two functions
  66. * in one: indicating the counter limit and starting the watchdog.
  67. * The watchdog must be disabled to be able to change the timeout
  68. * value if the watchdog is already running. Then we can set the
  69. * new timeout value which enables the watchdog again.
  70. */
  71. ret = da9063_wdt_disable_timer(da9063);
  72. if (ret)
  73. return ret;
  74. usleep_range(150, 300);
  75. regval = da9063_wdt_timeout_to_sel(timeout);
  76. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  77. DA9063_TWDSCALE_MASK, regval);
  78. }
  79. static int da9063_wdt_start(struct watchdog_device *wdd)
  80. {
  81. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  82. int ret;
  83. ret = da9063_wdt_update_timeout(da9063, wdd->timeout);
  84. if (ret)
  85. dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
  86. ret);
  87. return ret;
  88. }
  89. static int da9063_wdt_stop(struct watchdog_device *wdd)
  90. {
  91. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  92. int ret;
  93. ret = da9063_wdt_disable_timer(da9063);
  94. if (ret)
  95. dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n",
  96. ret);
  97. return ret;
  98. }
  99. static int da9063_wdt_ping(struct watchdog_device *wdd)
  100. {
  101. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  102. int ret;
  103. ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
  104. DA9063_WATCHDOG);
  105. if (ret)
  106. dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  107. ret);
  108. return ret;
  109. }
  110. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  111. unsigned int timeout)
  112. {
  113. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  114. int ret = 0;
  115. /*
  116. * There are two cases when a set_timeout() will be called:
  117. * 1. The watchdog is off and someone wants to set the timeout for the
  118. * further use.
  119. * 2. The watchdog is already running and a new timeout value should be
  120. * set.
  121. *
  122. * The watchdog can't store a timeout value not equal zero without
  123. * enabling the watchdog, so the timeout must be buffered by the driver.
  124. */
  125. if (watchdog_active(wdd))
  126. ret = da9063_wdt_update_timeout(da9063, timeout);
  127. if (ret)
  128. dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  129. ret);
  130. else
  131. wdd->timeout = wdt_timeout[da9063_wdt_timeout_to_sel(timeout)];
  132. return ret;
  133. }
  134. static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  135. void *data)
  136. {
  137. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  138. int ret;
  139. ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
  140. DA9063_SHUTDOWN);
  141. if (ret)
  142. dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n",
  143. ret);
  144. return ret;
  145. }
  146. static const struct watchdog_info da9063_watchdog_info = {
  147. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  148. .identity = "DA9063 Watchdog",
  149. };
  150. static const struct watchdog_ops da9063_watchdog_ops = {
  151. .owner = THIS_MODULE,
  152. .start = da9063_wdt_start,
  153. .stop = da9063_wdt_stop,
  154. .ping = da9063_wdt_ping,
  155. .set_timeout = da9063_wdt_set_timeout,
  156. .restart = da9063_wdt_restart,
  157. };
  158. static int da9063_wdt_probe(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. struct da9063 *da9063;
  162. struct watchdog_device *wdd;
  163. unsigned int timeout;
  164. if (!dev->parent)
  165. return -EINVAL;
  166. da9063 = dev_get_drvdata(dev->parent);
  167. if (!da9063)
  168. return -EINVAL;
  169. wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
  170. if (!wdd)
  171. return -ENOMEM;
  172. wdd->info = &da9063_watchdog_info;
  173. wdd->ops = &da9063_watchdog_ops;
  174. wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT;
  175. wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT;
  176. wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS;
  177. wdd->parent = dev;
  178. wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  179. watchdog_set_restart_priority(wdd, 128);
  180. watchdog_set_drvdata(wdd, da9063);
  181. wdd->timeout = DA9063_WDG_TIMEOUT;
  182. /* Use pre-configured timeout if watchdog is already running. */
  183. timeout = da9063_wdt_read_timeout(da9063);
  184. if (timeout)
  185. wdd->timeout = timeout;
  186. /* Set timeout, maybe override it with DT value, scale it */
  187. watchdog_init_timeout(wdd, 0, dev);
  188. da9063_wdt_set_timeout(wdd, wdd->timeout);
  189. /* Update timeout if the watchdog is already running. */
  190. if (timeout) {
  191. da9063_wdt_update_timeout(da9063, wdd->timeout);
  192. set_bit(WDOG_HW_RUNNING, &wdd->status);
  193. }
  194. return devm_watchdog_register_device(dev, wdd);
  195. }
  196. static struct platform_driver da9063_wdt_driver = {
  197. .probe = da9063_wdt_probe,
  198. .driver = {
  199. .name = DA9063_DRVNAME_WATCHDOG,
  200. },
  201. };
  202. module_platform_driver(da9063_wdt_driver);
  203. MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
  204. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  205. MODULE_LICENSE("GPL");
  206. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);