max77620_wdt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Maxim MAX77620 Watchdog Driver
  3. *
  4. * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/mfd/max77620.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include <linux/watchdog.h>
  22. static bool nowayout = WATCHDOG_NOWAYOUT;
  23. struct max77620_wdt {
  24. struct device *dev;
  25. struct regmap *rmap;
  26. struct watchdog_device wdt_dev;
  27. };
  28. static int max77620_wdt_start(struct watchdog_device *wdt_dev)
  29. {
  30. struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  31. return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
  32. MAX77620_WDTEN, MAX77620_WDTEN);
  33. }
  34. static int max77620_wdt_stop(struct watchdog_device *wdt_dev)
  35. {
  36. struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  37. return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
  38. MAX77620_WDTEN, 0);
  39. }
  40. static int max77620_wdt_ping(struct watchdog_device *wdt_dev)
  41. {
  42. struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  43. return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL3,
  44. MAX77620_WDTC_MASK, 0x1);
  45. }
  46. static int max77620_wdt_set_timeout(struct watchdog_device *wdt_dev,
  47. unsigned int timeout)
  48. {
  49. struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  50. unsigned int wdt_timeout;
  51. u8 regval;
  52. int ret;
  53. switch (timeout) {
  54. case 0 ... 2:
  55. regval = MAX77620_TWD_2s;
  56. wdt_timeout = 2;
  57. break;
  58. case 3 ... 16:
  59. regval = MAX77620_TWD_16s;
  60. wdt_timeout = 16;
  61. break;
  62. case 17 ... 64:
  63. regval = MAX77620_TWD_64s;
  64. wdt_timeout = 64;
  65. break;
  66. default:
  67. regval = MAX77620_TWD_128s;
  68. wdt_timeout = 128;
  69. break;
  70. }
  71. ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL3,
  72. MAX77620_WDTC_MASK, 0x1);
  73. if (ret < 0)
  74. return ret;
  75. ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
  76. MAX77620_TWD_MASK, regval);
  77. if (ret < 0)
  78. return ret;
  79. wdt_dev->timeout = wdt_timeout;
  80. return 0;
  81. }
  82. static const struct watchdog_info max77620_wdt_info = {
  83. .identity = "max77620-watchdog",
  84. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  85. };
  86. static const struct watchdog_ops max77620_wdt_ops = {
  87. .start = max77620_wdt_start,
  88. .stop = max77620_wdt_stop,
  89. .ping = max77620_wdt_ping,
  90. .set_timeout = max77620_wdt_set_timeout,
  91. };
  92. static int max77620_wdt_probe(struct platform_device *pdev)
  93. {
  94. struct max77620_wdt *wdt;
  95. struct watchdog_device *wdt_dev;
  96. unsigned int regval;
  97. int ret;
  98. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  99. if (!wdt)
  100. return -ENOMEM;
  101. wdt->dev = &pdev->dev;
  102. wdt->rmap = dev_get_regmap(pdev->dev.parent, NULL);
  103. if (!wdt->rmap) {
  104. dev_err(wdt->dev, "Failed to get parent regmap\n");
  105. return -ENODEV;
  106. }
  107. wdt_dev = &wdt->wdt_dev;
  108. wdt_dev->info = &max77620_wdt_info;
  109. wdt_dev->ops = &max77620_wdt_ops;
  110. wdt_dev->min_timeout = 2;
  111. wdt_dev->max_timeout = 128;
  112. wdt_dev->max_hw_heartbeat_ms = 128 * 1000;
  113. platform_set_drvdata(pdev, wdt);
  114. /* Enable WD_RST_WK - WDT expire results in a restart */
  115. ret = regmap_update_bits(wdt->rmap, MAX77620_REG_ONOFFCNFG2,
  116. MAX77620_ONOFFCNFG2_WD_RST_WK,
  117. MAX77620_ONOFFCNFG2_WD_RST_WK);
  118. if (ret < 0) {
  119. dev_err(wdt->dev, "Failed to set WD_RST_WK: %d\n", ret);
  120. return ret;
  121. }
  122. /* Set WDT clear in OFF and sleep mode */
  123. ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
  124. MAX77620_WDTOFFC | MAX77620_WDTSLPC,
  125. MAX77620_WDTOFFC | MAX77620_WDTSLPC);
  126. if (ret < 0) {
  127. dev_err(wdt->dev, "Failed to set WDT OFF mode: %d\n", ret);
  128. return ret;
  129. }
  130. /* Check if WDT running and if yes then set flags properly */
  131. ret = regmap_read(wdt->rmap, MAX77620_REG_CNFGGLBL2, &regval);
  132. if (ret < 0) {
  133. dev_err(wdt->dev, "Failed to read WDT CFG register: %d\n", ret);
  134. return ret;
  135. }
  136. switch (regval & MAX77620_TWD_MASK) {
  137. case MAX77620_TWD_2s:
  138. wdt_dev->timeout = 2;
  139. break;
  140. case MAX77620_TWD_16s:
  141. wdt_dev->timeout = 16;
  142. break;
  143. case MAX77620_TWD_64s:
  144. wdt_dev->timeout = 64;
  145. break;
  146. default:
  147. wdt_dev->timeout = 128;
  148. break;
  149. }
  150. if (regval & MAX77620_WDTEN)
  151. set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
  152. watchdog_set_nowayout(wdt_dev, nowayout);
  153. watchdog_set_drvdata(wdt_dev, wdt);
  154. ret = watchdog_register_device(wdt_dev);
  155. if (ret < 0) {
  156. dev_err(&pdev->dev, "watchdog registration failed: %d\n", ret);
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int max77620_wdt_remove(struct platform_device *pdev)
  162. {
  163. struct max77620_wdt *wdt = platform_get_drvdata(pdev);
  164. max77620_wdt_stop(&wdt->wdt_dev);
  165. watchdog_unregister_device(&wdt->wdt_dev);
  166. return 0;
  167. }
  168. static const struct platform_device_id max77620_wdt_devtype[] = {
  169. { .name = "max77620-watchdog", },
  170. { },
  171. };
  172. MODULE_DEVICE_TABLE(platform, max77620_wdt_devtype);
  173. static struct platform_driver max77620_wdt_driver = {
  174. .driver = {
  175. .name = "max77620-watchdog",
  176. },
  177. .probe = max77620_wdt_probe,
  178. .remove = max77620_wdt_remove,
  179. .id_table = max77620_wdt_devtype,
  180. };
  181. module_platform_driver(max77620_wdt_driver);
  182. MODULE_DESCRIPTION("Max77620 watchdog timer driver");
  183. module_param(nowayout, bool, 0);
  184. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  185. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  186. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  187. MODULE_LICENSE("GPL v2");