gpio_wdt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for watchdog device controlled through GPIO-line
  4. *
  5. * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. static bool nowayout = WATCHDOG_NOWAYOUT;
  15. module_param(nowayout, bool, 0);
  16. MODULE_PARM_DESC(nowayout,
  17. "Watchdog cannot be stopped once started (default="
  18. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  19. #define SOFT_TIMEOUT_MIN 1
  20. #define SOFT_TIMEOUT_DEF 60
  21. enum {
  22. HW_ALGO_TOGGLE,
  23. HW_ALGO_LEVEL,
  24. };
  25. struct gpio_wdt_priv {
  26. struct gpio_desc *gpiod;
  27. bool state;
  28. bool always_running;
  29. unsigned int hw_algo;
  30. struct watchdog_device wdd;
  31. };
  32. static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
  33. {
  34. /* Eternal ping */
  35. gpiod_set_value_cansleep(priv->gpiod, 1);
  36. /* Put GPIO back to tristate */
  37. if (priv->hw_algo == HW_ALGO_TOGGLE)
  38. gpiod_direction_input(priv->gpiod);
  39. }
  40. static int gpio_wdt_ping(struct watchdog_device *wdd)
  41. {
  42. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  43. switch (priv->hw_algo) {
  44. case HW_ALGO_TOGGLE:
  45. /* Toggle output pin */
  46. priv->state = !priv->state;
  47. gpiod_set_value_cansleep(priv->gpiod, priv->state);
  48. break;
  49. case HW_ALGO_LEVEL:
  50. /* Pulse */
  51. gpiod_set_value_cansleep(priv->gpiod, 1);
  52. udelay(1);
  53. gpiod_set_value_cansleep(priv->gpiod, 0);
  54. break;
  55. }
  56. return 0;
  57. }
  58. static int gpio_wdt_start(struct watchdog_device *wdd)
  59. {
  60. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  61. priv->state = 0;
  62. gpiod_direction_output(priv->gpiod, priv->state);
  63. set_bit(WDOG_HW_RUNNING, &wdd->status);
  64. return gpio_wdt_ping(wdd);
  65. }
  66. static int gpio_wdt_stop(struct watchdog_device *wdd)
  67. {
  68. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  69. if (!priv->always_running) {
  70. gpio_wdt_disable(priv);
  71. } else {
  72. set_bit(WDOG_HW_RUNNING, &wdd->status);
  73. }
  74. return 0;
  75. }
  76. static const struct watchdog_info gpio_wdt_ident = {
  77. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
  78. WDIOF_SETTIMEOUT,
  79. .identity = "GPIO Watchdog",
  80. };
  81. static const struct watchdog_ops gpio_wdt_ops = {
  82. .owner = THIS_MODULE,
  83. .start = gpio_wdt_start,
  84. .stop = gpio_wdt_stop,
  85. .ping = gpio_wdt_ping,
  86. };
  87. static int gpio_wdt_probe(struct platform_device *pdev)
  88. {
  89. struct device *dev = &pdev->dev;
  90. struct device_node *np = dev->of_node;
  91. struct gpio_wdt_priv *priv;
  92. enum gpiod_flags gflags;
  93. unsigned int hw_margin;
  94. const char *algo;
  95. int ret;
  96. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  97. if (!priv)
  98. return -ENOMEM;
  99. platform_set_drvdata(pdev, priv);
  100. ret = of_property_read_string(np, "hw_algo", &algo);
  101. if (ret)
  102. return ret;
  103. if (!strcmp(algo, "toggle")) {
  104. priv->hw_algo = HW_ALGO_TOGGLE;
  105. gflags = GPIOD_IN;
  106. } else if (!strcmp(algo, "level")) {
  107. priv->hw_algo = HW_ALGO_LEVEL;
  108. gflags = GPIOD_OUT_LOW;
  109. } else {
  110. return -EINVAL;
  111. }
  112. priv->gpiod = devm_gpiod_get(dev, NULL, gflags);
  113. if (IS_ERR(priv->gpiod))
  114. return PTR_ERR(priv->gpiod);
  115. ret = of_property_read_u32(np,
  116. "hw_margin_ms", &hw_margin);
  117. if (ret)
  118. return ret;
  119. /* Disallow values lower than 2 and higher than 65535 ms */
  120. if (hw_margin < 2 || hw_margin > 65535)
  121. return -EINVAL;
  122. priv->always_running = of_property_read_bool(np,
  123. "always-running");
  124. watchdog_set_drvdata(&priv->wdd, priv);
  125. priv->wdd.info = &gpio_wdt_ident;
  126. priv->wdd.ops = &gpio_wdt_ops;
  127. priv->wdd.min_timeout = SOFT_TIMEOUT_MIN;
  128. priv->wdd.max_hw_heartbeat_ms = hw_margin;
  129. priv->wdd.parent = dev;
  130. priv->wdd.timeout = SOFT_TIMEOUT_DEF;
  131. watchdog_init_timeout(&priv->wdd, 0, dev);
  132. watchdog_set_nowayout(&priv->wdd, nowayout);
  133. watchdog_stop_on_reboot(&priv->wdd);
  134. if (priv->always_running)
  135. gpio_wdt_start(&priv->wdd);
  136. return devm_watchdog_register_device(dev, &priv->wdd);
  137. }
  138. static const struct of_device_id gpio_wdt_dt_ids[] = {
  139. { .compatible = "linux,wdt-gpio", },
  140. { }
  141. };
  142. MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
  143. static struct platform_driver gpio_wdt_driver = {
  144. .driver = {
  145. .name = "gpio-wdt",
  146. .of_match_table = gpio_wdt_dt_ids,
  147. },
  148. .probe = gpio_wdt_probe,
  149. };
  150. #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
  151. static int __init gpio_wdt_init(void)
  152. {
  153. return platform_driver_register(&gpio_wdt_driver);
  154. }
  155. arch_initcall(gpio_wdt_init);
  156. #else
  157. module_platform_driver(gpio_wdt_driver);
  158. #endif
  159. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  160. MODULE_DESCRIPTION("GPIO Watchdog");
  161. MODULE_LICENSE("GPL");