da9052_wdt.c 6.0 KB

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