gpio_wdt.c 6.5 KB

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