da9063_wdt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/reboot.h>
  23. #include <linux/regmap.h>
  24. /*
  25. * Watchdog selector to timeout in seconds.
  26. * 0: WDT disabled;
  27. * others: timeout = 2048 ms * 2^(TWDSCALE-1).
  28. */
  29. static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
  30. #define DA9063_TWDSCALE_DISABLE 0
  31. #define DA9063_TWDSCALE_MIN 1
  32. #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
  33. #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN]
  34. #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX]
  35. #define DA9063_WDG_TIMEOUT wdt_timeout[3]
  36. struct da9063_watchdog {
  37. struct da9063 *da9063;
  38. struct watchdog_device wdtdev;
  39. struct notifier_block restart_handler;
  40. };
  41. static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  42. {
  43. unsigned int i;
  44. for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
  45. if (wdt_timeout[i] >= secs)
  46. return i;
  47. }
  48. return DA9063_TWDSCALE_MAX;
  49. }
  50. static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
  51. {
  52. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  53. DA9063_TWDSCALE_MASK, regval);
  54. }
  55. static int da9063_wdt_start(struct watchdog_device *wdd)
  56. {
  57. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  58. unsigned int selector;
  59. int ret;
  60. selector = da9063_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  61. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  62. if (ret)
  63. dev_err(wdt->da9063->dev, "Watchdog failed to start (err = %d)\n",
  64. ret);
  65. return ret;
  66. }
  67. static int da9063_wdt_stop(struct watchdog_device *wdd)
  68. {
  69. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  70. int ret;
  71. ret = regmap_update_bits(wdt->da9063->regmap, DA9063_REG_CONTROL_D,
  72. DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
  73. if (ret)
  74. dev_alert(wdt->da9063->dev, "Watchdog failed to stop (err = %d)\n",
  75. ret);
  76. return ret;
  77. }
  78. static int da9063_wdt_ping(struct watchdog_device *wdd)
  79. {
  80. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  81. int ret;
  82. ret = regmap_write(wdt->da9063->regmap, DA9063_REG_CONTROL_F,
  83. DA9063_WATCHDOG);
  84. if (ret)
  85. dev_alert(wdt->da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  86. ret);
  87. return ret;
  88. }
  89. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  90. unsigned int timeout)
  91. {
  92. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  93. unsigned int selector;
  94. int ret;
  95. selector = da9063_wdt_timeout_to_sel(timeout);
  96. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  97. if (ret)
  98. dev_err(wdt->da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  99. ret);
  100. else
  101. wdd->timeout = wdt_timeout[selector];
  102. return ret;
  103. }
  104. static int da9063_wdt_restart_handler(struct notifier_block *this,
  105. unsigned long mode, void *cmd)
  106. {
  107. struct da9063_watchdog *wdt = container_of(this,
  108. struct da9063_watchdog,
  109. restart_handler);
  110. int ret;
  111. ret = regmap_write(wdt->da9063->regmap, DA9063_REG_CONTROL_F,
  112. DA9063_SHUTDOWN);
  113. if (ret)
  114. dev_alert(wdt->da9063->dev, "Failed to shutdown (err = %d)\n",
  115. ret);
  116. return NOTIFY_DONE;
  117. }
  118. static const struct watchdog_info da9063_watchdog_info = {
  119. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  120. .identity = "DA9063 Watchdog",
  121. };
  122. static const struct watchdog_ops da9063_watchdog_ops = {
  123. .owner = THIS_MODULE,
  124. .start = da9063_wdt_start,
  125. .stop = da9063_wdt_stop,
  126. .ping = da9063_wdt_ping,
  127. .set_timeout = da9063_wdt_set_timeout,
  128. };
  129. static int da9063_wdt_probe(struct platform_device *pdev)
  130. {
  131. int ret;
  132. struct da9063 *da9063;
  133. struct da9063_watchdog *wdt;
  134. if (!pdev->dev.parent)
  135. return -EINVAL;
  136. da9063 = dev_get_drvdata(pdev->dev.parent);
  137. if (!da9063)
  138. return -EINVAL;
  139. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  140. if (!wdt)
  141. return -ENOMEM;
  142. wdt->da9063 = da9063;
  143. wdt->wdtdev.info = &da9063_watchdog_info;
  144. wdt->wdtdev.ops = &da9063_watchdog_ops;
  145. wdt->wdtdev.min_timeout = DA9063_WDT_MIN_TIMEOUT;
  146. wdt->wdtdev.max_timeout = DA9063_WDT_MAX_TIMEOUT;
  147. wdt->wdtdev.timeout = DA9063_WDG_TIMEOUT;
  148. wdt->wdtdev.parent = &pdev->dev;
  149. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  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. wdt->restart_handler.notifier_call = da9063_wdt_restart_handler;
  156. wdt->restart_handler.priority = 128;
  157. ret = register_restart_handler(&wdt->restart_handler);
  158. if (ret)
  159. dev_err(wdt->da9063->dev,
  160. "Failed to register restart handler (err = %d)\n", ret);
  161. return 0;
  162. }
  163. static int da9063_wdt_remove(struct platform_device *pdev)
  164. {
  165. struct da9063_watchdog *wdt = dev_get_drvdata(&pdev->dev);
  166. unregister_restart_handler(&wdt->restart_handler);
  167. watchdog_unregister_device(&wdt->wdtdev);
  168. return 0;
  169. }
  170. static struct platform_driver da9063_wdt_driver = {
  171. .probe = da9063_wdt_probe,
  172. .remove = da9063_wdt_remove,
  173. .driver = {
  174. .name = DA9063_DRVNAME_WATCHDOG,
  175. },
  176. };
  177. module_platform_driver(da9063_wdt_driver);
  178. MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
  179. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  180. MODULE_LICENSE("GPL");
  181. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);