ts4800_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for TS-4800 based boards
  4. *
  5. * Copyright (c) 2015 - Savoir-faire Linux
  6. *
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/watchdog.h>
  15. static bool nowayout = WATCHDOG_NOWAYOUT;
  16. module_param(nowayout, bool, 0);
  17. MODULE_PARM_DESC(nowayout,
  18. "Watchdog cannot be stopped once started (default="
  19. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  20. /* possible feed values */
  21. #define TS4800_WDT_FEED_2S 0x1
  22. #define TS4800_WDT_FEED_10S 0x2
  23. #define TS4800_WDT_DISABLE 0x3
  24. struct ts4800_wdt {
  25. struct watchdog_device wdd;
  26. struct regmap *regmap;
  27. u32 feed_offset;
  28. u32 feed_val;
  29. };
  30. /*
  31. * TS-4800 supports the following timeout values:
  32. *
  33. * value desc
  34. * ---------------------
  35. * 0 feed for 338ms
  36. * 1 feed for 2.706s
  37. * 2 feed for 10.824s
  38. * 3 disable watchdog
  39. *
  40. * Keep the regmap/timeout map ordered by timeout
  41. */
  42. static const struct {
  43. const int timeout;
  44. const int regval;
  45. } ts4800_wdt_map[] = {
  46. { 2, TS4800_WDT_FEED_2S },
  47. { 10, TS4800_WDT_FEED_10S },
  48. };
  49. #define MAX_TIMEOUT_INDEX (ARRAY_SIZE(ts4800_wdt_map) - 1)
  50. static void ts4800_write_feed(struct ts4800_wdt *wdt, u32 val)
  51. {
  52. regmap_write(wdt->regmap, wdt->feed_offset, val);
  53. }
  54. static int ts4800_wdt_start(struct watchdog_device *wdd)
  55. {
  56. struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
  57. ts4800_write_feed(wdt, wdt->feed_val);
  58. return 0;
  59. }
  60. static int ts4800_wdt_stop(struct watchdog_device *wdd)
  61. {
  62. struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
  63. ts4800_write_feed(wdt, TS4800_WDT_DISABLE);
  64. return 0;
  65. }
  66. static int ts4800_wdt_set_timeout(struct watchdog_device *wdd,
  67. unsigned int timeout)
  68. {
  69. struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
  70. int i;
  71. for (i = 0; i < MAX_TIMEOUT_INDEX; i++) {
  72. if (ts4800_wdt_map[i].timeout >= timeout)
  73. break;
  74. }
  75. wdd->timeout = ts4800_wdt_map[i].timeout;
  76. wdt->feed_val = ts4800_wdt_map[i].regval;
  77. return 0;
  78. }
  79. static const struct watchdog_ops ts4800_wdt_ops = {
  80. .owner = THIS_MODULE,
  81. .start = ts4800_wdt_start,
  82. .stop = ts4800_wdt_stop,
  83. .set_timeout = ts4800_wdt_set_timeout,
  84. };
  85. static const struct watchdog_info ts4800_wdt_info = {
  86. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  87. .identity = "TS-4800 Watchdog",
  88. };
  89. static int ts4800_wdt_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct device_node *np = dev->of_node;
  93. struct device_node *syscon_np;
  94. struct watchdog_device *wdd;
  95. struct ts4800_wdt *wdt;
  96. u32 reg;
  97. int ret;
  98. syscon_np = of_parse_phandle(np, "syscon", 0);
  99. if (!syscon_np) {
  100. dev_err(dev, "no syscon property\n");
  101. return -ENODEV;
  102. }
  103. ret = of_property_read_u32_index(np, "syscon", 1, &reg);
  104. if (ret < 0) {
  105. dev_err(dev, "no offset in syscon\n");
  106. of_node_put(syscon_np);
  107. return ret;
  108. }
  109. /* allocate memory for watchdog struct */
  110. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  111. if (!wdt) {
  112. of_node_put(syscon_np);
  113. return -ENOMEM;
  114. }
  115. /* set regmap and offset to know where to write */
  116. wdt->feed_offset = reg;
  117. wdt->regmap = syscon_node_to_regmap(syscon_np);
  118. of_node_put(syscon_np);
  119. if (IS_ERR(wdt->regmap)) {
  120. dev_err(dev, "cannot get parent's regmap\n");
  121. return PTR_ERR(wdt->regmap);
  122. }
  123. /* Initialize struct watchdog_device */
  124. wdd = &wdt->wdd;
  125. wdd->parent = dev;
  126. wdd->info = &ts4800_wdt_info;
  127. wdd->ops = &ts4800_wdt_ops;
  128. wdd->min_timeout = ts4800_wdt_map[0].timeout;
  129. wdd->max_timeout = ts4800_wdt_map[MAX_TIMEOUT_INDEX].timeout;
  130. watchdog_set_drvdata(wdd, wdt);
  131. watchdog_set_nowayout(wdd, nowayout);
  132. watchdog_init_timeout(wdd, 0, dev);
  133. /*
  134. * As this watchdog supports only a few values, ts4800_wdt_set_timeout
  135. * must be called to initialize timeout and feed_val with valid values.
  136. * Default to maximum timeout if none, or an invalid one, is provided in
  137. * device tree.
  138. */
  139. if (!wdd->timeout)
  140. wdd->timeout = wdd->max_timeout;
  141. ts4800_wdt_set_timeout(wdd, wdd->timeout);
  142. /*
  143. * The feed register is write-only, so it is not possible to determine
  144. * watchdog's state. Disable it to be in a known state.
  145. */
  146. ts4800_wdt_stop(wdd);
  147. ret = devm_watchdog_register_device(dev, wdd);
  148. if (ret)
  149. return ret;
  150. platform_set_drvdata(pdev, wdt);
  151. dev_info(dev, "initialized (timeout = %d sec, nowayout = %d)\n",
  152. wdd->timeout, nowayout);
  153. return 0;
  154. }
  155. static const struct of_device_id ts4800_wdt_of_match[] = {
  156. { .compatible = "technologic,ts4800-wdt", },
  157. { },
  158. };
  159. MODULE_DEVICE_TABLE(of, ts4800_wdt_of_match);
  160. static struct platform_driver ts4800_wdt_driver = {
  161. .probe = ts4800_wdt_probe,
  162. .driver = {
  163. .name = "ts4800_wdt",
  164. .of_match_table = ts4800_wdt_of_match,
  165. },
  166. };
  167. module_platform_driver(ts4800_wdt_driver);
  168. MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
  169. MODULE_DESCRIPTION("Watchdog driver for TS-4800 based boards");
  170. MODULE_LICENSE("GPL v2");
  171. MODULE_ALIAS("platform:ts4800_wdt");