mena21_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Watchdog driver for the A21 VME CPU Boards
  4. *
  5. * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
  6. *
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/delay.h>
  18. #include <linux/bitops.h>
  19. #include <linux/of.h>
  20. #define NUM_GPIOS 6
  21. enum a21_wdt_gpios {
  22. GPIO_WD_ENAB,
  23. GPIO_WD_FAST,
  24. GPIO_WD_TRIG,
  25. GPIO_WD_RST0,
  26. GPIO_WD_RST1,
  27. GPIO_WD_RST2,
  28. };
  29. struct a21_wdt_drv {
  30. struct watchdog_device wdt;
  31. struct gpio_desc *gpios[NUM_GPIOS];
  32. };
  33. static bool nowayout = WATCHDOG_NOWAYOUT;
  34. module_param(nowayout, bool, 0);
  35. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  36. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  37. static unsigned int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
  38. {
  39. int reset = 0;
  40. reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
  41. reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
  42. reset |= gpiod_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
  43. return reset;
  44. }
  45. static int a21_wdt_start(struct watchdog_device *wdt)
  46. {
  47. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  48. gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 1);
  49. return 0;
  50. }
  51. static int a21_wdt_stop(struct watchdog_device *wdt)
  52. {
  53. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  54. gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
  55. return 0;
  56. }
  57. static int a21_wdt_ping(struct watchdog_device *wdt)
  58. {
  59. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  60. gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 0);
  61. ndelay(10);
  62. gpiod_set_value(drv->gpios[GPIO_WD_TRIG], 1);
  63. return 0;
  64. }
  65. static int a21_wdt_set_timeout(struct watchdog_device *wdt,
  66. unsigned int timeout)
  67. {
  68. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  69. if (timeout != 1 && timeout != 30) {
  70. dev_err(wdt->parent, "Only 1 and 30 allowed as timeout\n");
  71. return -EINVAL;
  72. }
  73. if (timeout == 30 && wdt->timeout == 1) {
  74. dev_err(wdt->parent,
  75. "Transition from fast to slow mode not allowed\n");
  76. return -EINVAL;
  77. }
  78. if (timeout == 1)
  79. gpiod_set_value(drv->gpios[GPIO_WD_FAST], 1);
  80. else
  81. gpiod_set_value(drv->gpios[GPIO_WD_FAST], 0);
  82. wdt->timeout = timeout;
  83. return 0;
  84. }
  85. static const struct watchdog_info a21_wdt_info = {
  86. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  87. .identity = "MEN A21 Watchdog",
  88. };
  89. static const struct watchdog_ops a21_wdt_ops = {
  90. .owner = THIS_MODULE,
  91. .start = a21_wdt_start,
  92. .stop = a21_wdt_stop,
  93. .ping = a21_wdt_ping,
  94. .set_timeout = a21_wdt_set_timeout,
  95. };
  96. static struct watchdog_device a21_wdt = {
  97. .info = &a21_wdt_info,
  98. .ops = &a21_wdt_ops,
  99. .min_timeout = 1,
  100. .max_timeout = 30,
  101. };
  102. static int a21_wdt_probe(struct platform_device *pdev)
  103. {
  104. struct device *dev = &pdev->dev;
  105. struct a21_wdt_drv *drv;
  106. unsigned int reset = 0;
  107. int num_gpios;
  108. int ret;
  109. int i;
  110. drv = devm_kzalloc(dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
  111. if (!drv)
  112. return -ENOMEM;
  113. num_gpios = gpiod_count(dev, NULL);
  114. if (num_gpios != NUM_GPIOS) {
  115. dev_err(dev, "gpios DT property wrong, got %d want %d",
  116. num_gpios, NUM_GPIOS);
  117. return -ENODEV;
  118. }
  119. /* Request the used GPIOs */
  120. for (i = 0; i < num_gpios; i++) {
  121. enum gpiod_flags gflags;
  122. if (i < GPIO_WD_RST0)
  123. gflags = GPIOD_ASIS;
  124. else
  125. gflags = GPIOD_IN;
  126. drv->gpios[i] = devm_gpiod_get_index(dev, NULL, i, gflags);
  127. if (IS_ERR(drv->gpios[i]))
  128. return PTR_ERR(drv->gpios[i]);
  129. gpiod_set_consumer_name(drv->gpios[i], "MEN A21 Watchdog");
  130. /*
  131. * Retrieve the initial value from the GPIOs that should be
  132. * output, then set up the line as output with that value.
  133. */
  134. if (i < GPIO_WD_RST0) {
  135. int val;
  136. val = gpiod_get_value(drv->gpios[i]);
  137. gpiod_direction_output(drv->gpios[i], val);
  138. }
  139. }
  140. watchdog_init_timeout(&a21_wdt, 30, dev);
  141. watchdog_set_nowayout(&a21_wdt, nowayout);
  142. watchdog_set_drvdata(&a21_wdt, drv);
  143. a21_wdt.parent = dev;
  144. reset = a21_wdt_get_bootstatus(drv);
  145. if (reset == 2)
  146. a21_wdt.bootstatus |= WDIOF_EXTERN1;
  147. else if (reset == 4)
  148. a21_wdt.bootstatus |= WDIOF_CARDRESET;
  149. else if (reset == 5)
  150. a21_wdt.bootstatus |= WDIOF_POWERUNDER;
  151. else if (reset == 7)
  152. a21_wdt.bootstatus |= WDIOF_EXTERN2;
  153. drv->wdt = a21_wdt;
  154. dev_set_drvdata(dev, drv);
  155. ret = devm_watchdog_register_device(dev, &a21_wdt);
  156. if (ret)
  157. return ret;
  158. dev_info(dev, "MEN A21 watchdog timer driver enabled\n");
  159. return 0;
  160. }
  161. static void a21_wdt_shutdown(struct platform_device *pdev)
  162. {
  163. struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
  164. gpiod_set_value(drv->gpios[GPIO_WD_ENAB], 0);
  165. }
  166. static const struct of_device_id a21_wdt_ids[] = {
  167. { .compatible = "men,a021-wdt" },
  168. { },
  169. };
  170. MODULE_DEVICE_TABLE(of, a21_wdt_ids);
  171. static struct platform_driver a21_wdt_driver = {
  172. .probe = a21_wdt_probe,
  173. .shutdown = a21_wdt_shutdown,
  174. .driver = {
  175. .name = "a21-watchdog",
  176. .of_match_table = a21_wdt_ids,
  177. },
  178. };
  179. module_platform_driver(a21_wdt_driver);
  180. MODULE_AUTHOR("MEN Mikro Elektronik");
  181. MODULE_DESCRIPTION("MEN A21 Watchdog");
  182. MODULE_LICENSE("GPL");
  183. MODULE_ALIAS("platform:a21-watchdog");