max77620_wdt.c 5.3 KB

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