[PATCH 2/3] watchdog: da9063: Use pre configured timeout when watchdog is running

From: Stefan Riedmueller
Date: Thu Mar 26 2020 - 11:02:08 EST


If the watchdog is already running during probe use its pre-configured
timeout instead of a default timeout to make sure the watchdog is pinged
in time until userspace takes over.

Signed-off-by: Stefan Riedmueller <s.riedmueller@xxxxxxxxx>
---
drivers/watchdog/da9063_wdt.c | 29 ++++++++++++++++++-----------
1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/drivers/watchdog/da9063_wdt.c b/drivers/watchdog/da9063_wdt.c
index 3d65e92a4e3f..34d0c4f03814 100644
--- a/drivers/watchdog/da9063_wdt.c
+++ b/drivers/watchdog/da9063_wdt.c
@@ -46,15 +46,16 @@ static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
}

/*
- * Return 0 if watchdog is disabled, else non zero.
+ * Read the currently active timeout.
+ * Zero means the watchdog is disabled.
*/
-static unsigned int da9063_wdt_is_running(struct da9063 *da9063)
+static unsigned int da9063_wdt_read_timeout(struct da9063 *da9063)
{
unsigned int val;

regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);

- return val & DA9063_TWDSCALE_MASK;
+ return wdt_timeout[val & DA9063_TWDSCALE_MASK];
}

static int da9063_wdt_disable_timer(struct da9063 *da9063)
@@ -191,6 +192,7 @@ static int da9063_wdt_probe(struct platform_device *pdev)
struct device *dev = &pdev->dev;
struct da9063 *da9063;
struct watchdog_device *wdd;
+ int timeout;

if (!dev->parent)
return -EINVAL;
@@ -214,15 +216,20 @@ static int da9063_wdt_probe(struct platform_device *pdev)
watchdog_set_restart_priority(wdd, 128);
watchdog_set_drvdata(wdd, da9063);

- /* Set default timeout, maybe override it with DT value, scale it */
- wdd->timeout = DA9063_WDG_TIMEOUT;
- watchdog_init_timeout(wdd, 0, dev);
- da9063_wdt_set_timeout(wdd, wdd->timeout);
-
- /* Change the timeout to the default value if the watchdog is running */
- if (da9063_wdt_is_running(da9063)) {
- da9063_wdt_update_timeout(da9063, wdd->timeout);
+ /*
+ * Use pre-configured timeout if watchdog is already running.
+ * Otherwise set default timeout, maybe override it with DT value,
+ * scale it
+ */
+ timeout = da9063_wdt_read_timeout(da9063);
+ if (timeout) {
+ wdd->timeout = timeout;
set_bit(WDOG_HW_RUNNING, &wdd->status);
+ dev_info(da9063->dev, "watchdog is running (%u s)", timeout);
+ } else {
+ wdd->timeout = DA9063_WDG_TIMEOUT;
+ watchdog_init_timeout(wdd, 0, dev);
+ da9063_wdt_set_timeout(wdd, wdd->timeout);
}

return devm_watchdog_register_device(dev, wdd);
--
2.23.0