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