ts4800_wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_node *np = pdev->dev.of_node;
  94. struct device_node *syscon_np;
  95. struct watchdog_device *wdd;
  96. struct ts4800_wdt *wdt;
  97. u32 reg;
  98. int ret;
  99. syscon_np = of_parse_phandle(np, "syscon", 0);
  100. if (!syscon_np) {
  101. dev_err(&pdev->dev, "no syscon property\n");
  102. return -ENODEV;
  103. }
  104. ret = of_property_read_u32_index(np, "syscon", 1, &reg);
  105. if (ret < 0) {
  106. dev_err(&pdev->dev, "no offset in syscon\n");
  107. return ret;
  108. }
  109. /* allocate memory for watchdog struct */
  110. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  111. if (!wdt)
  112. return -ENOMEM;
  113. /* set regmap and offset to know where to write */
  114. wdt->feed_offset = reg;
  115. wdt->regmap = syscon_node_to_regmap(syscon_np);
  116. if (IS_ERR(wdt->regmap)) {
  117. dev_err(&pdev->dev, "cannot get parent's regmap\n");
  118. return PTR_ERR(wdt->regmap);
  119. }
  120. /* Initialize struct watchdog_device */
  121. wdd = &wdt->wdd;
  122. wdd->parent = &pdev->dev;
  123. wdd->info = &ts4800_wdt_info;
  124. wdd->ops = &ts4800_wdt_ops;
  125. wdd->min_timeout = ts4800_wdt_map[0].timeout;
  126. wdd->max_timeout = ts4800_wdt_map[MAX_TIMEOUT_INDEX].timeout;
  127. watchdog_set_drvdata(wdd, wdt);
  128. watchdog_set_nowayout(wdd, nowayout);
  129. watchdog_init_timeout(wdd, 0, &pdev->dev);
  130. /*
  131. * As this watchdog supports only a few values, ts4800_wdt_set_timeout
  132. * must be called to initialize timeout and feed_val with valid values.
  133. * Default to maximum timeout if none, or an invalid one, is provided in
  134. * device tree.
  135. */
  136. if (!wdd->timeout)
  137. wdd->timeout = wdd->max_timeout;
  138. ts4800_wdt_set_timeout(wdd, wdd->timeout);
  139. /*
  140. * The feed register is write-only, so it is not possible to determine
  141. * watchdog's state. Disable it to be in a known state.
  142. */
  143. ts4800_wdt_stop(wdd);
  144. ret = watchdog_register_device(wdd);
  145. if (ret) {
  146. dev_err(&pdev->dev,
  147. "failed to register watchdog device\n");
  148. return ret;
  149. }
  150. platform_set_drvdata(pdev, wdt);
  151. dev_info(&pdev->dev,
  152. "initialized (timeout = %d sec, nowayout = %d)\n",
  153. wdd->timeout, nowayout);
  154. return 0;
  155. }
  156. static int ts4800_wdt_remove(struct platform_device *pdev)
  157. {
  158. struct ts4800_wdt *wdt = platform_get_drvdata(pdev);
  159. watchdog_unregister_device(&wdt->wdd);
  160. return 0;
  161. }
  162. static const struct of_device_id ts4800_wdt_of_match[] = {
  163. { .compatible = "technologic,ts4800-wdt", },
  164. { },
  165. };
  166. MODULE_DEVICE_TABLE(of, ts4800_wdt_of_match);
  167. static struct platform_driver ts4800_wdt_driver = {
  168. .probe = ts4800_wdt_probe,
  169. .remove = ts4800_wdt_remove,
  170. .driver = {
  171. .name = "ts4800_wdt",
  172. .of_match_table = ts4800_wdt_of_match,
  173. },
  174. };
  175. module_platform_driver(ts4800_wdt_driver);
  176. MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
  177. MODULE_LICENSE("GPL v2");
  178. MODULE_ALIAS("platform:ts4800_wdt");