ts4800_wdt.c 5.0 KB

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