gpio_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Driver for watchdog device controlled through GPIO-line
  3. *
  4. * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/of_gpio.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/watchdog.h>
  17. #define SOFT_TIMEOUT_MIN 1
  18. #define SOFT_TIMEOUT_DEF 60
  19. #define SOFT_TIMEOUT_MAX 0xffff
  20. enum {
  21. HW_ALGO_TOGGLE,
  22. HW_ALGO_LEVEL,
  23. };
  24. struct gpio_wdt_priv {
  25. int gpio;
  26. bool active_low;
  27. bool state;
  28. bool always_running;
  29. bool armed;
  30. unsigned int hw_algo;
  31. unsigned int hw_margin;
  32. unsigned long last_jiffies;
  33. struct timer_list timer;
  34. struct watchdog_device wdd;
  35. };
  36. static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
  37. {
  38. gpio_set_value_cansleep(priv->gpio, !priv->active_low);
  39. /* Put GPIO back to tristate */
  40. if (priv->hw_algo == HW_ALGO_TOGGLE)
  41. gpio_direction_input(priv->gpio);
  42. }
  43. static void gpio_wdt_hwping(unsigned long data)
  44. {
  45. struct watchdog_device *wdd = (struct watchdog_device *)data;
  46. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  47. if (priv->armed && time_after(jiffies, priv->last_jiffies +
  48. msecs_to_jiffies(wdd->timeout * 1000))) {
  49. dev_crit(wdd->parent,
  50. "Timer expired. System will reboot soon!\n");
  51. return;
  52. }
  53. /* Restart timer */
  54. mod_timer(&priv->timer, jiffies + priv->hw_margin);
  55. switch (priv->hw_algo) {
  56. case HW_ALGO_TOGGLE:
  57. /* Toggle output pin */
  58. priv->state = !priv->state;
  59. gpio_set_value_cansleep(priv->gpio, priv->state);
  60. break;
  61. case HW_ALGO_LEVEL:
  62. /* Pulse */
  63. gpio_set_value_cansleep(priv->gpio, !priv->active_low);
  64. udelay(1);
  65. gpio_set_value_cansleep(priv->gpio, priv->active_low);
  66. break;
  67. }
  68. }
  69. static void gpio_wdt_start_impl(struct gpio_wdt_priv *priv)
  70. {
  71. priv->state = priv->active_low;
  72. gpio_direction_output(priv->gpio, priv->state);
  73. priv->last_jiffies = jiffies;
  74. gpio_wdt_hwping((unsigned long)&priv->wdd);
  75. }
  76. static int gpio_wdt_start(struct watchdog_device *wdd)
  77. {
  78. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  79. gpio_wdt_start_impl(priv);
  80. priv->armed = true;
  81. return 0;
  82. }
  83. static int gpio_wdt_stop(struct watchdog_device *wdd)
  84. {
  85. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  86. priv->armed = false;
  87. if (!priv->always_running) {
  88. mod_timer(&priv->timer, 0);
  89. gpio_wdt_disable(priv);
  90. }
  91. return 0;
  92. }
  93. static int gpio_wdt_ping(struct watchdog_device *wdd)
  94. {
  95. struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
  96. priv->last_jiffies = jiffies;
  97. return 0;
  98. }
  99. static int gpio_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  100. {
  101. wdd->timeout = t;
  102. return gpio_wdt_ping(wdd);
  103. }
  104. static const struct watchdog_info gpio_wdt_ident = {
  105. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
  106. WDIOF_SETTIMEOUT,
  107. .identity = "GPIO Watchdog",
  108. };
  109. static const struct watchdog_ops gpio_wdt_ops = {
  110. .owner = THIS_MODULE,
  111. .start = gpio_wdt_start,
  112. .stop = gpio_wdt_stop,
  113. .ping = gpio_wdt_ping,
  114. .set_timeout = gpio_wdt_set_timeout,
  115. };
  116. static int gpio_wdt_probe(struct platform_device *pdev)
  117. {
  118. struct gpio_wdt_priv *priv;
  119. enum of_gpio_flags flags;
  120. unsigned int hw_margin;
  121. unsigned long f = 0;
  122. const char *algo;
  123. int ret;
  124. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, priv);
  128. priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
  129. if (!gpio_is_valid(priv->gpio))
  130. return priv->gpio;
  131. priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
  132. ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
  133. if (ret)
  134. return ret;
  135. if (!strcmp(algo, "toggle")) {
  136. priv->hw_algo = HW_ALGO_TOGGLE;
  137. f = GPIOF_IN;
  138. } else if (!strcmp(algo, "level")) {
  139. priv->hw_algo = HW_ALGO_LEVEL;
  140. f = priv->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  141. } else {
  142. return -EINVAL;
  143. }
  144. ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
  145. dev_name(&pdev->dev));
  146. if (ret)
  147. return ret;
  148. ret = of_property_read_u32(pdev->dev.of_node,
  149. "hw_margin_ms", &hw_margin);
  150. if (ret)
  151. return ret;
  152. /* Disallow values lower than 2 and higher than 65535 ms */
  153. if (hw_margin < 2 || hw_margin > 65535)
  154. return -EINVAL;
  155. /* Use safe value (1/2 of real timeout) */
  156. priv->hw_margin = msecs_to_jiffies(hw_margin / 2);
  157. priv->always_running = of_property_read_bool(pdev->dev.of_node,
  158. "always-running");
  159. watchdog_set_drvdata(&priv->wdd, priv);
  160. priv->wdd.info = &gpio_wdt_ident;
  161. priv->wdd.ops = &gpio_wdt_ops;
  162. priv->wdd.min_timeout = SOFT_TIMEOUT_MIN;
  163. priv->wdd.max_timeout = SOFT_TIMEOUT_MAX;
  164. priv->wdd.parent = &pdev->dev;
  165. if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0)
  166. priv->wdd.timeout = SOFT_TIMEOUT_DEF;
  167. setup_timer(&priv->timer, gpio_wdt_hwping, (unsigned long)&priv->wdd);
  168. watchdog_stop_on_reboot(&priv->wdd);
  169. ret = watchdog_register_device(&priv->wdd);
  170. if (ret)
  171. return ret;
  172. if (priv->always_running)
  173. gpio_wdt_start_impl(priv);
  174. return 0;
  175. }
  176. static int gpio_wdt_remove(struct platform_device *pdev)
  177. {
  178. struct gpio_wdt_priv *priv = platform_get_drvdata(pdev);
  179. del_timer_sync(&priv->timer);
  180. watchdog_unregister_device(&priv->wdd);
  181. return 0;
  182. }
  183. static const struct of_device_id gpio_wdt_dt_ids[] = {
  184. { .compatible = "linux,wdt-gpio", },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
  188. static struct platform_driver gpio_wdt_driver = {
  189. .driver = {
  190. .name = "gpio-wdt",
  191. .of_match_table = gpio_wdt_dt_ids,
  192. },
  193. .probe = gpio_wdt_probe,
  194. .remove = gpio_wdt_remove,
  195. };
  196. #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
  197. static int __init gpio_wdt_init(void)
  198. {
  199. return platform_driver_register(&gpio_wdt_driver);
  200. }
  201. arch_initcall(gpio_wdt_init);
  202. #else
  203. module_platform_driver(gpio_wdt_driver);
  204. #endif
  205. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  206. MODULE_DESCRIPTION("GPIO Watchdog");
  207. MODULE_LICENSE("GPL");