rn5t618_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Watchdog driver for Ricoh RN5T618 PMIC
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/mfd/rn5t618.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/watchdog.h>
  12. #define DRIVER_NAME "rn5t618-wdt"
  13. static bool nowayout = WATCHDOG_NOWAYOUT;
  14. static unsigned int timeout;
  15. module_param(timeout, uint, 0);
  16. MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds");
  17. module_param(nowayout, bool, 0);
  18. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  19. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  20. struct rn5t618_wdt {
  21. struct watchdog_device wdt_dev;
  22. struct rn5t618 *rn5t618;
  23. };
  24. /*
  25. * This array encodes the values of WDOGTIM field for the supported
  26. * watchdog expiration times. If the watchdog is not accessed before
  27. * the timer expiration, the PMU generates an interrupt and if the CPU
  28. * doesn't clear it within one second the system is restarted.
  29. */
  30. static const struct {
  31. u8 reg_val;
  32. unsigned int time;
  33. } rn5t618_wdt_map[] = {
  34. { 0, 1 },
  35. { 1, 8 },
  36. { 2, 32 },
  37. { 3, 128 },
  38. };
  39. static int rn5t618_wdt_set_timeout(struct watchdog_device *wdt_dev,
  40. unsigned int t)
  41. {
  42. struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  43. int ret, i;
  44. for (i = 0; i < ARRAY_SIZE(rn5t618_wdt_map); i++) {
  45. if (rn5t618_wdt_map[i].time + 1 >= t)
  46. break;
  47. }
  48. if (i == ARRAY_SIZE(rn5t618_wdt_map))
  49. return -EINVAL;
  50. ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
  51. RN5T618_WATCHDOG_WDOGTIM_M,
  52. rn5t618_wdt_map[i].reg_val);
  53. if (!ret)
  54. wdt_dev->timeout = rn5t618_wdt_map[i].time;
  55. return ret;
  56. }
  57. static int rn5t618_wdt_start(struct watchdog_device *wdt_dev)
  58. {
  59. struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  60. int ret;
  61. ret = rn5t618_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  62. if (ret)
  63. return ret;
  64. /* enable repower-on */
  65. ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_REPCNT,
  66. RN5T618_REPCNT_REPWRON,
  67. RN5T618_REPCNT_REPWRON);
  68. if (ret)
  69. return ret;
  70. /* enable watchdog */
  71. ret = regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
  72. RN5T618_WATCHDOG_WDOGEN,
  73. RN5T618_WATCHDOG_WDOGEN);
  74. if (ret)
  75. return ret;
  76. /* enable watchdog interrupt */
  77. return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIREN,
  78. RN5T618_PWRIRQ_IR_WDOG,
  79. RN5T618_PWRIRQ_IR_WDOG);
  80. }
  81. static int rn5t618_wdt_stop(struct watchdog_device *wdt_dev)
  82. {
  83. struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  84. return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_WATCHDOG,
  85. RN5T618_WATCHDOG_WDOGEN, 0);
  86. }
  87. static int rn5t618_wdt_ping(struct watchdog_device *wdt_dev)
  88. {
  89. struct rn5t618_wdt *wdt = watchdog_get_drvdata(wdt_dev);
  90. unsigned int val;
  91. int ret;
  92. /* The counter is restarted after a R/W access to watchdog register */
  93. ret = regmap_read(wdt->rn5t618->regmap, RN5T618_WATCHDOG, &val);
  94. if (ret)
  95. return ret;
  96. ret = regmap_write(wdt->rn5t618->regmap, RN5T618_WATCHDOG, val);
  97. if (ret)
  98. return ret;
  99. /* Clear pending watchdog interrupt */
  100. return regmap_update_bits(wdt->rn5t618->regmap, RN5T618_PWRIRQ,
  101. RN5T618_PWRIRQ_IR_WDOG, 0);
  102. }
  103. static const struct watchdog_info rn5t618_wdt_info = {
  104. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  105. WDIOF_KEEPALIVEPING,
  106. .identity = DRIVER_NAME,
  107. };
  108. static const struct watchdog_ops rn5t618_wdt_ops = {
  109. .owner = THIS_MODULE,
  110. .start = rn5t618_wdt_start,
  111. .stop = rn5t618_wdt_stop,
  112. .ping = rn5t618_wdt_ping,
  113. .set_timeout = rn5t618_wdt_set_timeout,
  114. };
  115. static int rn5t618_wdt_probe(struct platform_device *pdev)
  116. {
  117. struct device *dev = &pdev->dev;
  118. struct rn5t618 *rn5t618 = dev_get_drvdata(dev->parent);
  119. struct rn5t618_wdt *wdt;
  120. int min_timeout, max_timeout;
  121. wdt = devm_kzalloc(dev, sizeof(struct rn5t618_wdt), GFP_KERNEL);
  122. if (!wdt)
  123. return -ENOMEM;
  124. min_timeout = rn5t618_wdt_map[0].time;
  125. max_timeout = rn5t618_wdt_map[ARRAY_SIZE(rn5t618_wdt_map) - 1].time;
  126. wdt->rn5t618 = rn5t618;
  127. wdt->wdt_dev.info = &rn5t618_wdt_info;
  128. wdt->wdt_dev.ops = &rn5t618_wdt_ops;
  129. wdt->wdt_dev.min_timeout = min_timeout;
  130. wdt->wdt_dev.max_timeout = max_timeout;
  131. wdt->wdt_dev.timeout = max_timeout;
  132. wdt->wdt_dev.parent = dev;
  133. watchdog_set_drvdata(&wdt->wdt_dev, wdt);
  134. watchdog_init_timeout(&wdt->wdt_dev, timeout, dev);
  135. watchdog_set_nowayout(&wdt->wdt_dev, nowayout);
  136. platform_set_drvdata(pdev, wdt);
  137. return watchdog_register_device(&wdt->wdt_dev);
  138. }
  139. static int rn5t618_wdt_remove(struct platform_device *pdev)
  140. {
  141. struct rn5t618_wdt *wdt = platform_get_drvdata(pdev);
  142. watchdog_unregister_device(&wdt->wdt_dev);
  143. return 0;
  144. }
  145. static struct platform_driver rn5t618_wdt_driver = {
  146. .probe = rn5t618_wdt_probe,
  147. .remove = rn5t618_wdt_remove,
  148. .driver = {
  149. .name = DRIVER_NAME,
  150. },
  151. };
  152. module_platform_driver(rn5t618_wdt_driver);
  153. MODULE_ALIAS("platform:rn5t618-wdt");
  154. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  155. MODULE_DESCRIPTION("RN5T618 watchdog driver");
  156. MODULE_LICENSE("GPL v2");