da9052_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * System monitoring driver for DA9052 PMICs.
  4. *
  5. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  6. *
  7. * Author: Anthony Olech <Anthony.Olech@diasemi.com>
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/time.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/mfd/da9052/reg.h>
  20. #include <linux/mfd/da9052/da9052.h>
  21. #define DA9052_DEF_TIMEOUT 4
  22. #define DA9052_TWDMIN 256
  23. struct da9052_wdt_data {
  24. struct watchdog_device wdt;
  25. struct da9052 *da9052;
  26. unsigned long jpast;
  27. };
  28. static const struct {
  29. u8 reg_val;
  30. int time; /* Seconds */
  31. } da9052_wdt_maps[] = {
  32. { 1, 2 },
  33. { 2, 4 },
  34. { 3, 8 },
  35. { 4, 16 },
  36. { 5, 32 },
  37. { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
  38. { 6, 65 },
  39. { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
  40. { 7, 131 },
  41. };
  42. static int da9052_wdt_set_timeout(struct watchdog_device *wdt_dev,
  43. unsigned int timeout)
  44. {
  45. struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  46. struct da9052 *da9052 = driver_data->da9052;
  47. int ret, i;
  48. /*
  49. * Disable the Watchdog timer before setting
  50. * new time out.
  51. */
  52. ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
  53. DA9052_CONTROLD_TWDSCALE, 0);
  54. if (ret < 0) {
  55. dev_err(da9052->dev, "Failed to disable watchdog bit, %d\n",
  56. ret);
  57. return ret;
  58. }
  59. if (timeout) {
  60. /*
  61. * To change the timeout, da9052 needs to
  62. * be disabled for at least 150 us.
  63. */
  64. udelay(150);
  65. /* Set the desired timeout */
  66. for (i = 0; i < ARRAY_SIZE(da9052_wdt_maps); i++)
  67. if (da9052_wdt_maps[i].time == timeout)
  68. break;
  69. if (i == ARRAY_SIZE(da9052_wdt_maps))
  70. ret = -EINVAL;
  71. else
  72. ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
  73. DA9052_CONTROLD_TWDSCALE,
  74. da9052_wdt_maps[i].reg_val);
  75. if (ret < 0) {
  76. dev_err(da9052->dev,
  77. "Failed to update timescale bit, %d\n", ret);
  78. return ret;
  79. }
  80. wdt_dev->timeout = timeout;
  81. driver_data->jpast = jiffies;
  82. }
  83. return 0;
  84. }
  85. static int da9052_wdt_start(struct watchdog_device *wdt_dev)
  86. {
  87. return da9052_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  88. }
  89. static int da9052_wdt_stop(struct watchdog_device *wdt_dev)
  90. {
  91. return da9052_wdt_set_timeout(wdt_dev, 0);
  92. }
  93. static int da9052_wdt_ping(struct watchdog_device *wdt_dev)
  94. {
  95. struct da9052_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
  96. struct da9052 *da9052 = driver_data->da9052;
  97. unsigned long msec, jnow = jiffies;
  98. int ret;
  99. /*
  100. * We have a minimum time for watchdog window called TWDMIN. A write
  101. * to the watchdog before this elapsed time should cause an error.
  102. */
  103. msec = (jnow - driver_data->jpast) * 1000/HZ;
  104. if (msec < DA9052_TWDMIN)
  105. mdelay(msec);
  106. /* Reset the watchdog timer */
  107. ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
  108. DA9052_CONTROLD_WATCHDOG, 1 << 7);
  109. if (ret < 0)
  110. return ret;
  111. /*
  112. * FIXME: Reset the watchdog core, in general PMIC
  113. * is supposed to do this
  114. */
  115. return da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
  116. DA9052_CONTROLD_WATCHDOG, 0 << 7);
  117. }
  118. static const struct watchdog_info da9052_wdt_info = {
  119. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  120. .identity = "DA9052 Watchdog",
  121. };
  122. static const struct watchdog_ops da9052_wdt_ops = {
  123. .owner = THIS_MODULE,
  124. .start = da9052_wdt_start,
  125. .stop = da9052_wdt_stop,
  126. .ping = da9052_wdt_ping,
  127. .set_timeout = da9052_wdt_set_timeout,
  128. };
  129. static int da9052_wdt_probe(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct da9052 *da9052 = dev_get_drvdata(dev->parent);
  133. struct da9052_wdt_data *driver_data;
  134. struct watchdog_device *da9052_wdt;
  135. int ret;
  136. driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
  137. if (!driver_data)
  138. return -ENOMEM;
  139. driver_data->da9052 = da9052;
  140. da9052_wdt = &driver_data->wdt;
  141. da9052_wdt->timeout = DA9052_DEF_TIMEOUT;
  142. da9052_wdt->info = &da9052_wdt_info;
  143. da9052_wdt->ops = &da9052_wdt_ops;
  144. da9052_wdt->parent = dev;
  145. watchdog_set_drvdata(da9052_wdt, driver_data);
  146. ret = da9052_reg_update(da9052, DA9052_CONTROL_D_REG,
  147. DA9052_CONTROLD_TWDSCALE, 0);
  148. if (ret < 0) {
  149. dev_err(dev, "Failed to disable watchdog bits, %d\n", ret);
  150. return ret;
  151. }
  152. return devm_watchdog_register_device(dev, &driver_data->wdt);
  153. }
  154. static struct platform_driver da9052_wdt_driver = {
  155. .probe = da9052_wdt_probe,
  156. .driver = {
  157. .name = "da9052-watchdog",
  158. },
  159. };
  160. module_platform_driver(da9052_wdt_driver);
  161. MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
  162. MODULE_DESCRIPTION("DA9052 SM Device Driver");
  163. MODULE_LICENSE("GPL");
  164. MODULE_ALIAS("platform:da9052-watchdog");