bd9576_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2020 ROHM Semiconductors
  4. *
  5. * ROHM BD9576MUF and BD9573MUF Watchdog driver
  6. */
  7. #include <linux/err.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/mfd/rohm-bd957x.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. #include <linux/regmap.h>
  14. #include <linux/watchdog.h>
  15. static bool nowayout;
  16. module_param(nowayout, bool, 0);
  17. MODULE_PARM_DESC(nowayout,
  18. "Watchdog cannot be stopped once started (default=\"false\")");
  19. #define HW_MARGIN_MIN 2
  20. #define HW_MARGIN_MAX 4416
  21. #define BD957X_WDT_DEFAULT_MARGIN 4416
  22. #define WATCHDOG_TIMEOUT 30
  23. struct bd9576_wdt_priv {
  24. struct gpio_desc *gpiod_ping;
  25. struct gpio_desc *gpiod_en;
  26. struct device *dev;
  27. struct regmap *regmap;
  28. bool always_running;
  29. struct watchdog_device wdd;
  30. };
  31. static void bd9576_wdt_disable(struct bd9576_wdt_priv *priv)
  32. {
  33. gpiod_set_value_cansleep(priv->gpiod_en, 0);
  34. }
  35. static int bd9576_wdt_ping(struct watchdog_device *wdd)
  36. {
  37. struct bd9576_wdt_priv *priv = watchdog_get_drvdata(wdd);
  38. /* Pulse */
  39. gpiod_set_value_cansleep(priv->gpiod_ping, 1);
  40. gpiod_set_value_cansleep(priv->gpiod_ping, 0);
  41. return 0;
  42. }
  43. static int bd9576_wdt_start(struct watchdog_device *wdd)
  44. {
  45. struct bd9576_wdt_priv *priv = watchdog_get_drvdata(wdd);
  46. gpiod_set_value_cansleep(priv->gpiod_en, 1);
  47. return bd9576_wdt_ping(wdd);
  48. }
  49. static int bd9576_wdt_stop(struct watchdog_device *wdd)
  50. {
  51. struct bd9576_wdt_priv *priv = watchdog_get_drvdata(wdd);
  52. if (!priv->always_running)
  53. bd9576_wdt_disable(priv);
  54. else
  55. set_bit(WDOG_HW_RUNNING, &wdd->status);
  56. return 0;
  57. }
  58. static const struct watchdog_info bd957x_wdt_ident = {
  59. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
  60. WDIOF_SETTIMEOUT,
  61. .identity = "BD957x Watchdog",
  62. };
  63. static const struct watchdog_ops bd957x_wdt_ops = {
  64. .owner = THIS_MODULE,
  65. .start = bd9576_wdt_start,
  66. .stop = bd9576_wdt_stop,
  67. .ping = bd9576_wdt_ping,
  68. };
  69. /* Unit is hundreds of uS */
  70. #define FASTNG_MIN 23
  71. static int find_closest_fast(int target, int *sel, int *val)
  72. {
  73. int i;
  74. int window = FASTNG_MIN;
  75. for (i = 0; i < 8 && window < target; i++)
  76. window <<= 1;
  77. *val = window;
  78. *sel = i;
  79. if (i == 8)
  80. return -EINVAL;
  81. return 0;
  82. }
  83. static int find_closest_slow_by_fast(int fast_val, int target, int *slowsel)
  84. {
  85. int sel;
  86. static const int multipliers[] = {2, 3, 7, 15};
  87. for (sel = 0; sel < ARRAY_SIZE(multipliers) &&
  88. multipliers[sel] * fast_val < target; sel++)
  89. ;
  90. if (sel == ARRAY_SIZE(multipliers))
  91. return -EINVAL;
  92. *slowsel = sel;
  93. return 0;
  94. }
  95. static int find_closest_slow(int target, int *slow_sel, int *fast_sel)
  96. {
  97. static const int multipliers[] = {2, 3, 7, 15};
  98. int i, j;
  99. int val = 0;
  100. int window = FASTNG_MIN;
  101. for (i = 0; i < 8; i++) {
  102. for (j = 0; j < ARRAY_SIZE(multipliers); j++) {
  103. int slow;
  104. slow = window * multipliers[j];
  105. if (slow >= target && (!val || slow < val)) {
  106. val = slow;
  107. *fast_sel = i;
  108. *slow_sel = j;
  109. }
  110. }
  111. window <<= 1;
  112. }
  113. if (!val)
  114. return -EINVAL;
  115. return 0;
  116. }
  117. #define BD957X_WDG_TYPE_WINDOW BIT(5)
  118. #define BD957X_WDG_TYPE_SLOW 0
  119. #define BD957X_WDG_TYPE_MASK BIT(5)
  120. #define BD957X_WDG_NG_RATIO_MASK 0x18
  121. #define BD957X_WDG_FASTNG_MASK 0x7
  122. static int bd957x_set_wdt_mode(struct bd9576_wdt_priv *priv, int hw_margin,
  123. int hw_margin_min)
  124. {
  125. int ret, fastng, slowng, type, reg, mask;
  126. struct device *dev = priv->dev;
  127. /* convert to 100uS */
  128. hw_margin *= 10;
  129. hw_margin_min *= 10;
  130. if (hw_margin_min) {
  131. int min;
  132. type = BD957X_WDG_TYPE_WINDOW;
  133. dev_dbg(dev, "Setting type WINDOW 0x%x\n", type);
  134. ret = find_closest_fast(hw_margin_min, &fastng, &min);
  135. if (ret) {
  136. dev_err(dev, "bad WDT window for fast timeout\n");
  137. return ret;
  138. }
  139. ret = find_closest_slow_by_fast(min, hw_margin, &slowng);
  140. if (ret) {
  141. dev_err(dev, "bad WDT window\n");
  142. return ret;
  143. }
  144. } else {
  145. type = BD957X_WDG_TYPE_SLOW;
  146. dev_dbg(dev, "Setting type SLOW 0x%x\n", type);
  147. ret = find_closest_slow(hw_margin, &slowng, &fastng);
  148. if (ret) {
  149. dev_err(dev, "bad WDT window\n");
  150. return ret;
  151. }
  152. }
  153. slowng <<= ffs(BD957X_WDG_NG_RATIO_MASK) - 1;
  154. reg = type | slowng | fastng;
  155. mask = BD957X_WDG_TYPE_MASK | BD957X_WDG_NG_RATIO_MASK |
  156. BD957X_WDG_FASTNG_MASK;
  157. ret = regmap_update_bits(priv->regmap, BD957X_REG_WDT_CONF,
  158. mask, reg);
  159. return ret;
  160. }
  161. static int bd9576_wdt_probe(struct platform_device *pdev)
  162. {
  163. struct device *dev = &pdev->dev;
  164. struct bd9576_wdt_priv *priv;
  165. u32 hw_margin[2];
  166. u32 hw_margin_max = BD957X_WDT_DEFAULT_MARGIN, hw_margin_min = 0;
  167. int count;
  168. int ret;
  169. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  170. if (!priv)
  171. return -ENOMEM;
  172. platform_set_drvdata(pdev, priv);
  173. priv->dev = dev;
  174. priv->regmap = dev_get_regmap(dev->parent, NULL);
  175. if (!priv->regmap) {
  176. dev_err(dev, "No regmap found\n");
  177. return -ENODEV;
  178. }
  179. priv->gpiod_en = devm_fwnode_gpiod_get(dev, dev_fwnode(dev->parent),
  180. "rohm,watchdog-enable",
  181. GPIOD_OUT_LOW,
  182. "watchdog-enable");
  183. if (IS_ERR(priv->gpiod_en))
  184. return dev_err_probe(dev, PTR_ERR(priv->gpiod_en),
  185. "getting watchdog-enable GPIO failed\n");
  186. priv->gpiod_ping = devm_fwnode_gpiod_get(dev, dev_fwnode(dev->parent),
  187. "rohm,watchdog-ping",
  188. GPIOD_OUT_LOW,
  189. "watchdog-ping");
  190. if (IS_ERR(priv->gpiod_ping))
  191. return dev_err_probe(dev, PTR_ERR(priv->gpiod_ping),
  192. "getting watchdog-ping GPIO failed\n");
  193. count = device_property_count_u32(dev->parent, "rohm,hw-timeout-ms");
  194. if (count < 0 && count != -EINVAL)
  195. return count;
  196. if (count > 0) {
  197. if (count > ARRAY_SIZE(hw_margin))
  198. return -EINVAL;
  199. ret = device_property_read_u32_array(dev->parent,
  200. "rohm,hw-timeout-ms",
  201. hw_margin, count);
  202. if (ret < 0)
  203. return ret;
  204. if (count == 1)
  205. hw_margin_max = hw_margin[0];
  206. if (count == 2) {
  207. hw_margin_max = hw_margin[1];
  208. hw_margin_min = hw_margin[0];
  209. }
  210. }
  211. ret = bd957x_set_wdt_mode(priv, hw_margin_max, hw_margin_min);
  212. if (ret)
  213. return ret;
  214. priv->always_running = device_property_read_bool(dev->parent,
  215. "always-running");
  216. watchdog_set_drvdata(&priv->wdd, priv);
  217. priv->wdd.info = &bd957x_wdt_ident;
  218. priv->wdd.ops = &bd957x_wdt_ops;
  219. priv->wdd.min_hw_heartbeat_ms = hw_margin_min;
  220. priv->wdd.max_hw_heartbeat_ms = hw_margin_max;
  221. priv->wdd.parent = dev;
  222. priv->wdd.timeout = WATCHDOG_TIMEOUT;
  223. watchdog_init_timeout(&priv->wdd, 0, dev);
  224. watchdog_set_nowayout(&priv->wdd, nowayout);
  225. watchdog_stop_on_reboot(&priv->wdd);
  226. if (priv->always_running)
  227. bd9576_wdt_start(&priv->wdd);
  228. return devm_watchdog_register_device(dev, &priv->wdd);
  229. }
  230. static struct platform_driver bd9576_wdt_driver = {
  231. .driver = {
  232. .name = "bd9576-wdt",
  233. },
  234. .probe = bd9576_wdt_probe,
  235. };
  236. module_platform_driver(bd9576_wdt_driver);
  237. MODULE_AUTHOR("Matti Vaittinen <[email protected]>");
  238. MODULE_DESCRIPTION("ROHM BD9576/BD9573 Watchdog driver");
  239. MODULE_LICENSE("GPL");
  240. MODULE_ALIAS("platform:bd9576-wdt");