rn5t618_wdt.c 5.2 KB

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