da9063_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. struct da9063_watchdog {
  36. struct da9063 *da9063;
  37. struct watchdog_device wdtdev;
  38. };
  39. static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  40. {
  41. unsigned int i;
  42. for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
  43. if (wdt_timeout[i] >= secs)
  44. return i;
  45. }
  46. return DA9063_TWDSCALE_MAX;
  47. }
  48. static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
  49. {
  50. return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
  51. DA9063_TWDSCALE_MASK, regval);
  52. }
  53. static int da9063_wdt_start(struct watchdog_device *wdd)
  54. {
  55. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  56. unsigned int selector;
  57. int ret;
  58. selector = da9063_wdt_timeout_to_sel(wdt->wdtdev.timeout);
  59. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  60. if (ret)
  61. dev_err(wdt->da9063->dev, "Watchdog failed to start (err = %d)\n",
  62. ret);
  63. return ret;
  64. }
  65. static int da9063_wdt_stop(struct watchdog_device *wdd)
  66. {
  67. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  68. int ret;
  69. ret = regmap_update_bits(wdt->da9063->regmap, DA9063_REG_CONTROL_D,
  70. DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
  71. if (ret)
  72. dev_alert(wdt->da9063->dev, "Watchdog failed to stop (err = %d)\n",
  73. ret);
  74. return ret;
  75. }
  76. static int da9063_wdt_ping(struct watchdog_device *wdd)
  77. {
  78. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  79. int ret;
  80. ret = regmap_write(wdt->da9063->regmap, DA9063_REG_CONTROL_F,
  81. DA9063_WATCHDOG);
  82. if (ret)
  83. dev_alert(wdt->da9063->dev, "Failed to ping the watchdog (err = %d)\n",
  84. ret);
  85. return ret;
  86. }
  87. static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  88. unsigned int timeout)
  89. {
  90. struct da9063_watchdog *wdt = watchdog_get_drvdata(wdd);
  91. unsigned int selector;
  92. int ret;
  93. selector = da9063_wdt_timeout_to_sel(timeout);
  94. ret = _da9063_wdt_set_timeout(wdt->da9063, selector);
  95. if (ret)
  96. dev_err(wdt->da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  97. ret);
  98. else
  99. wdd->timeout = wdt_timeout[selector];
  100. return ret;
  101. }
  102. static const struct watchdog_info da9063_watchdog_info = {
  103. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  104. .identity = "DA9063 Watchdog",
  105. };
  106. static const struct watchdog_ops da9063_watchdog_ops = {
  107. .owner = THIS_MODULE,
  108. .start = da9063_wdt_start,
  109. .stop = da9063_wdt_stop,
  110. .ping = da9063_wdt_ping,
  111. .set_timeout = da9063_wdt_set_timeout,
  112. };
  113. static int da9063_wdt_probe(struct platform_device *pdev)
  114. {
  115. int ret;
  116. struct da9063 *da9063;
  117. struct da9063_watchdog *wdt;
  118. if (!pdev->dev.parent)
  119. return -EINVAL;
  120. da9063 = dev_get_drvdata(pdev->dev.parent);
  121. if (!da9063)
  122. return -EINVAL;
  123. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  124. if (!wdt)
  125. return -ENOMEM;
  126. wdt->da9063 = da9063;
  127. wdt->wdtdev.info = &da9063_watchdog_info;
  128. wdt->wdtdev.ops = &da9063_watchdog_ops;
  129. wdt->wdtdev.min_timeout = DA9063_WDT_MIN_TIMEOUT;
  130. wdt->wdtdev.max_timeout = DA9063_WDT_MAX_TIMEOUT;
  131. wdt->wdtdev.timeout = DA9063_WDG_TIMEOUT;
  132. wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
  133. watchdog_set_drvdata(&wdt->wdtdev, wdt);
  134. dev_set_drvdata(&pdev->dev, wdt);
  135. ret = watchdog_register_device(&wdt->wdtdev);
  136. return ret;
  137. }
  138. static int da9063_wdt_remove(struct platform_device *pdev)
  139. {
  140. struct da9063_watchdog *wdt = dev_get_drvdata(&pdev->dev);
  141. watchdog_unregister_device(&wdt->wdtdev);
  142. return 0;
  143. }
  144. static struct platform_driver da9063_wdt_driver = {
  145. .probe = da9063_wdt_probe,
  146. .remove = da9063_wdt_remove,
  147. .driver = {
  148. .name = DA9063_DRVNAME_WATCHDOG,
  149. },
  150. };
  151. module_platform_driver(da9063_wdt_driver);
  152. MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
  153. MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
  154. MODULE_LICENSE("GPL");
  155. MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);