gpio_wdt.c 4.6 KB

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