da9052_wdt.c 5.8 KB

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