da9052_wdt.c 5.2 KB

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