da9063_wdt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Return 0 if watchdog is disabled, else non zero.
  44. */
  45. static unsigned int da9063_wdt_is_running(struct da9063 *da9063)
  46. {
  47. unsigned int val;
  48. regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
  49. return val & DA9063_TWDSCALE_MASK;
  50. }
  51. static int da9063_wdt_disable_timer(struct da9063 *da9063)
  52. {
  53. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  54. DA9063_TWDSCALE_MASK,
  55. DA9063_TWDSCALE_DISABLE);
  56. }
  57. static int
  58. da9063_wdt_update_timeout(struct da9063 *da9063, unsigned int timeout)
  59. {
  60. unsigned int regval;
  61. int ret;
  62. /*
  63. * The watchdog triggers a reboot if a timeout value is already
  64. * programmed because the timeout value combines two functions
  65. * in one: indicating the counter limit and starting the watchdog.
  66. * The watchdog must be disabled to be able to change the timeout
  67. * value if the watchdog is already running. Then we can set the
  68. * new timeout value which enables the watchdog again.
  69. */
  70. ret = da9063_wdt_disable_timer(da9063);
  71. if (ret)
  72. return ret;
  73. usleep_range(150, 300);
  74. regval = da9063_wdt_timeout_to_sel(timeout);
  75. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  76. DA9063_TWDSCALE_MASK, regval);
  77. }
  78. static int da9063_wdt_start(struct watchdog_device *wdd)
  79. {
  80. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  81. int ret;
  82. ret = da9063_wdt_update_timeout(da9063, wdd->timeout);
  83. if (ret)
  84. dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
  85. ret);
  86. return ret;
  87. }
  88. static int da9063_wdt_stop(struct watchdog_device *wdd)
  89. {
  90. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  91. int ret;
  92. ret = da9063_wdt_disable_timer(da9063);
  93. if (ret)
  94. dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n",
  95. ret);
  96. return ret;
  97. }
  98. static int da9063_wdt_ping(struct watchdog_device *wdd)
  99. {
  100. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  101. int ret;
  102. ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
  103. DA9063_WATCHDOG);
  104. if (ret)
  105. dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  106. ret);
  107. return ret;
  108. }
  109. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  110. unsigned int timeout)
  111. {
  112. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  113. int ret = 0;
  114. /*
  115. * There are two cases when a set_timeout() will be called:
  116. * 1. The watchdog is off and someone wants to set the timeout for the
  117. * further use.
  118. * 2. The watchdog is already running and a new timeout value should be
  119. * set.
  120. *
  121. * The watchdog can't store a timeout value not equal zero without
  122. * enabling the watchdog, so the timeout must be buffered by the driver.
  123. */
  124. if (watchdog_active(wdd))
  125. ret = da9063_wdt_update_timeout(da9063, timeout);
  126. if (ret)
  127. dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  128. ret);
  129. else
  130. wdd->timeout = wdt_timeout[da9063_wdt_timeout_to_sel(timeout)];
  131. return ret;
  132. }
  133. static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  134. void *data)
  135. {
  136. struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  137. int ret;
  138. ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
  139. DA9063_SHUTDOWN);
  140. if (ret)
  141. dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n",
  142. ret);
  143. return ret;
  144. }
  145. static const struct watchdog_info da9063_watchdog_info = {
  146. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  147. .identity = "DA9063 Watchdog",
  148. };
  149. static const struct watchdog_ops da9063_watchdog_ops = {
  150. .owner = THIS_MODULE,
  151. .start = da9063_wdt_start,
  152. .stop = da9063_wdt_stop,
  153. .ping = da9063_wdt_ping,
  154. .set_timeout = da9063_wdt_set_timeout,
  155. .restart = da9063_wdt_restart,
  156. };
  157. static int da9063_wdt_probe(struct platform_device *pdev)
  158. {
  159. struct da9063 *da9063;
  160. struct watchdog_device *wdd;
  161. if (!pdev->dev.parent)
  162. return -EINVAL;
  163. da9063 = dev_get_drvdata(pdev->dev.parent);
  164. if (!da9063)
  165. return -EINVAL;
  166. wdd = devm_kzalloc(&pdev->dev, sizeof(*wdd), GFP_KERNEL);
  167. if (!wdd)
  168. return -ENOMEM;
  169. wdd->info = &da9063_watchdog_info;
  170. wdd->ops = &da9063_watchdog_ops;
  171. wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT;
  172. wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT;
  173. wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS;
  174. wdd->timeout = DA9063_WDG_TIMEOUT;
  175. wdd->parent = &pdev->dev;
  176. wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  177. watchdog_set_restart_priority(wdd, 128);
  178. watchdog_set_drvdata(wdd, da9063);
  179. /* Change the timeout to the default value if the watchdog is running */
  180. if (da9063_wdt_is_running(da9063)) {
  181. da9063_wdt_update_timeout(da9063, DA9063_WDG_TIMEOUT);
  182. set_bit(WDOG_HW_RUNNING, &wdd->status);
  183. }
  184. return devm_watchdog_register_device(&pdev->dev, wdd);
  185. }
  186. static struct platform_driver da9063_wdt_driver = {
  187. .probe = da9063_wdt_probe,
  188. .driver = {
  189. .name = DA9063_DRVNAME_WATCHDOG,
  190. },
  191. };
  192. module_platform_driver(da9063_wdt_driver);
  193. MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
  194. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  195. MODULE_LICENSE("GPL");
  196. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);