gpio_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
  128. if (!gpio_is_valid(priv->gpio))
  129. return priv->gpio;
  130. priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
  131. ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
  132. if (ret)
  133. return ret;
  134. if (!strcmp(algo, "toggle")) {
  135. priv->hw_algo = HW_ALGO_TOGGLE;
  136. f = GPIOF_IN;
  137. } else if (!strcmp(algo, "level")) {
  138. priv->hw_algo = HW_ALGO_LEVEL;
  139. f = priv->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  140. } else {
  141. return -EINVAL;
  142. }
  143. ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
  144. dev_name(&pdev->dev));
  145. if (ret)
  146. return ret;
  147. ret = of_property_read_u32(pdev->dev.of_node,
  148. "hw_margin_ms", &hw_margin);
  149. if (ret)
  150. return ret;
  151. /* Disallow values lower than 2 and higher than 65535 ms */
  152. if (hw_margin < 2 || hw_margin > 65535)
  153. return -EINVAL;
  154. /* Use safe value (1/2 of real timeout) */
  155. priv->hw_margin = msecs_to_jiffies(hw_margin / 2);
  156. priv->always_running = of_property_read_bool(pdev->dev.of_node,
  157. "always-running");
  158. watchdog_set_drvdata(&priv->wdd, priv);
  159. priv->wdd.info = &gpio_wdt_ident;
  160. priv->wdd.ops = &gpio_wdt_ops;
  161. priv->wdd.min_timeout = SOFT_TIMEOUT_MIN;
  162. priv->wdd.max_timeout = SOFT_TIMEOUT_MAX;
  163. priv->wdd.parent = &pdev->dev;
  164. if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0)
  165. priv->wdd.timeout = SOFT_TIMEOUT_DEF;
  166. setup_timer(&priv->timer, gpio_wdt_hwping, (unsigned long)&priv->wdd);
  167. watchdog_stop_on_reboot(&priv->wdd);
  168. ret = watchdog_register_device(&priv->wdd);
  169. if (ret)
  170. return ret;
  171. if (priv->always_running)
  172. gpio_wdt_start_impl(priv);
  173. return 0;
  174. }
  175. static int gpio_wdt_remove(struct platform_device *pdev)
  176. {
  177. struct gpio_wdt_priv *priv = platform_get_drvdata(pdev);
  178. del_timer_sync(&priv->timer);
  179. watchdog_unregister_device(&priv->wdd);
  180. return 0;
  181. }
  182. static const struct of_device_id gpio_wdt_dt_ids[] = {
  183. { .compatible = "linux,wdt-gpio", },
  184. { }
  185. };
  186. MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
  187. static struct platform_driver gpio_wdt_driver = {
  188. .driver = {
  189. .name = "gpio-wdt",
  190. .of_match_table = gpio_wdt_dt_ids,
  191. },
  192. .probe = gpio_wdt_probe,
  193. .remove = gpio_wdt_remove,
  194. };
  195. #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
  196. static int __init gpio_wdt_init(void)
  197. {
  198. return platform_driver_register(&gpio_wdt_driver);
  199. }
  200. arch_initcall(gpio_wdt_init);
  201. #else
  202. module_platform_driver(gpio_wdt_driver);
  203. #endif
  204. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  205. MODULE_DESCRIPTION("GPIO Watchdog");
  206. MODULE_LICENSE("GPL");