da9052_wdt.c 6.0 KB

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