bd9576_wdt.c 6.6 KB

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