da9063_wdt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Watchdog driver for DA9063 PMICs.
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/mfd/da9063/registers.h>
  21. #include <linux/mfd/da9063/core.h>
  22. #include <linux/regmap.h>
  23. /*
  24. * Watchdog selector to timeout in seconds.
  25. * 0: WDT disabled;
  26. * others: timeout = 2048 ms * 2^(TWDSCALE-1).
  27. */
  28. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  29. #define DA9063_TWDSCALE_DISABLE 0
  30. #define DA9063_TWDSCALE_MIN 1
  31. #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  32. #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN]
  33. #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX]
  34. #define DA9063_WDG_TIMEOUT wdt_timeout[3]
  35. #define DA9063_RESET_PROTECTION_MS 256
  36. struct da9063_watchdog {
  37. struct da9063 *da9063;
  38. struct watchdog_device wdtdev;
  39. };
  40. static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  41. {
  42. unsigned int i;
  43. for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
  44. if (wdt_timeout[i] >= secs)
  45. return i;
  46. }
  47. return DA9063_TWDSCALE_MAX;
  48. }
  49. static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
  50. {
  51. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  52. DA9063_TWDSCALE_MASK, regval);
  53. }
  54. static int da9063_wdt_start(struct watchdog_device *wdd)
  55. {
  56. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  57. unsigned int selector;
  58. int ret;
  59. selector = da9063_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  60. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  61. if (ret)
  62. dev_err(wdt->da9063->dev, "Watchdog failed to start (err = %d)\n",
  63. ret);
  64. return ret;
  65. }
  66. static int da9063_wdt_stop(struct watchdog_device *wdd)
  67. {
  68. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  69. int ret;
  70. ret = regmap_update_bits(wdt->da9063->regmap, DA9063_REG_CONTROL_D,
  71. DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
  72. if (ret)
  73. dev_alert(wdt->da9063->dev, "Watchdog failed to stop (err = %d)\n",
  74. ret);
  75. return ret;
  76. }
  77. static int da9063_wdt_ping(struct watchdog_device *wdd)
  78. {
  79. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  80. int ret;
  81. ret = regmap_write(wdt->da9063->regmap, DA9063_REG_CONTROL_F,
  82. DA9063_WATCHDOG);
  83. if (ret)
  84. dev_alert(wdt->da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  85. ret);
  86. return ret;
  87. }
  88. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  89. unsigned int timeout)
  90. {
  91. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  92. unsigned int selector;
  93. int ret;
  94. selector = da9063_wdt_timeout_to_sel(timeout);
  95. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  96. if (ret)
  97. dev_err(wdt->da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  98. ret);
  99. else
  100. wdd->timeout = wdt_timeout[selector];
  101. return ret;
  102. }
  103. static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
  104. void *data)
  105. {
  106. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  107. int ret;
  108. ret = regmap_write(wdt->da9063->regmap, DA9063_REG_CONTROL_F,
  109. DA9063_SHUTDOWN);
  110. if (ret)
  111. dev_alert(wdt->da9063->dev, "Failed to shutdown (err = %d)\n",
  112. ret);
  113. return ret;
  114. }
  115. static const struct watchdog_info da9063_watchdog_info = {
  116. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  117. .identity = "DA9063 Watchdog",
  118. };
  119. static const struct watchdog_ops da9063_watchdog_ops = {
  120. .owner = THIS_MODULE,
  121. .start = da9063_wdt_start,
  122. .stop = da9063_wdt_stop,
  123. .ping = da9063_wdt_ping,
  124. .set_timeout = da9063_wdt_set_timeout,
  125. .restart = da9063_wdt_restart,
  126. };
  127. static int da9063_wdt_probe(struct platform_device *pdev)
  128. {
  129. int ret;
  130. struct da9063 *da9063;
  131. struct da9063_watchdog *wdt;
  132. if (!pdev->dev.parent)
  133. return -EINVAL;
  134. da9063 = dev_get_drvdata(pdev->dev.parent);
  135. if (!da9063)
  136. return -EINVAL;
  137. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  138. if (!wdt)
  139. return -ENOMEM;
  140. wdt->da9063 = da9063;
  141. wdt->wdtdev.info = &da9063_watchdog_info;
  142. wdt->wdtdev.ops = &da9063_watchdog_ops;
  143. wdt->wdtdev.min_timeout = DA9063_WDT_MIN_TIMEOUT;
  144. wdt->wdtdev.max_timeout = DA9063_WDT_MAX_TIMEOUT;
  145. wdt->wdtdev.min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS;
  146. wdt->wdtdev.timeout = DA9063_WDG_TIMEOUT;
  147. wdt->wdtdev.parent = &pdev->dev;
  148. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  149. watchdog_set_restart_priority(&wdt->wdtdev, 128);
  150. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  151. dev_set_drvdata(&pdev->dev, wdt);
  152. ret = watchdog_register_device(&wdt->wdtdev);
  153. if (ret)
  154. return ret;
  155. return 0;
  156. }
  157. static int da9063_wdt_remove(struct platform_device *pdev)
  158. {
  159. struct da9063_watchdog *wdt = dev_get_drvdata(&pdev->dev);
  160. watchdog_unregister_device(&wdt->wdtdev);
  161. return 0;
  162. }
  163. static struct platform_driver da9063_wdt_driver = {
  164. .probe = da9063_wdt_probe,
  165. .remove = da9063_wdt_remove,
  166. .driver = {
  167. .name = DA9063_DRVNAME_WATCHDOG,
  168. },
  169. };
  170. module_platform_driver(da9063_wdt_driver);
  171. MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
  172. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  173. MODULE_LICENSE("GPL");
  174. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);