2 * Watchdog device driver for DA9062 and DA9061 PMICs
3 * Copyright (C) 2015 Dialog Semiconductor Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/watchdog.h>
19 #include <linux/platform_device.h>
20 #include <linux/uaccess.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/jiffies.h>
24 #include <linux/mfd/da9062/registers.h>
25 #include <linux/mfd/da9062/core.h>
26 #include <linux/regmap.h>
29 static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
30 #define DA9062_TWDSCALE_DISABLE 0
31 #define DA9062_TWDSCALE_MIN 1
32 #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
33 #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN]
34 #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX]
35 #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1]
36 #define DA9062_RESET_PROTECTION_MS 300
38 struct da9062_watchdog {
40 struct watchdog_device wdtdev;
41 unsigned long j_time_stamp;
44 static void da9062_set_window_start(struct da9062_watchdog *wdt)
46 wdt->j_time_stamp = jiffies;
49 static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
53 for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
54 if (wdt_timeout[i] >= secs)
58 return DA9062_TWDSCALE_MAX;
61 static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
65 ret = regmap_update_bits(wdt->hw->regmap,
67 DA9062AA_WATCHDOG_MASK,
68 DA9062AA_WATCHDOG_MASK);
70 da9062_set_window_start(wdt);
75 static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
78 struct da9062 *chip = wdt->hw;
81 ret = da9062_reset_watchdog_timer(wdt);
85 regmap_update_bits(chip->regmap,
87 DA9062AA_TWDSCALE_MASK,
88 DA9062_TWDSCALE_DISABLE);
90 usleep_range(150, 300);
92 return regmap_update_bits(chip->regmap,
94 DA9062AA_TWDSCALE_MASK,
98 static int da9062_wdt_start(struct watchdog_device *wdd)
100 struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
101 unsigned int selector;
104 selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
105 ret = da9062_wdt_update_timeout_register(wdt, selector);
107 dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
113 static int da9062_wdt_stop(struct watchdog_device *wdd)
115 struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
118 ret = da9062_reset_watchdog_timer(wdt);
120 dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
125 ret = regmap_update_bits(wdt->hw->regmap,
127 DA9062AA_TWDSCALE_MASK,
128 DA9062_TWDSCALE_DISABLE);
130 dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
136 static int da9062_wdt_ping(struct watchdog_device *wdd)
138 struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
141 ret = da9062_reset_watchdog_timer(wdt);
143 dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
149 static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
150 unsigned int timeout)
152 struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
153 unsigned int selector;
156 selector = da9062_wdt_timeout_to_sel(timeout);
157 ret = da9062_wdt_update_timeout_register(wdt, selector);
159 dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
162 wdd->timeout = wdt_timeout[selector];
167 static int da9062_wdt_restart(struct watchdog_device *wdd, unsigned long action,
170 struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
173 ret = regmap_write(wdt->hw->regmap,
175 DA9062AA_SHUTDOWN_MASK);
177 dev_alert(wdt->hw->dev, "Failed to shutdown (err = %d)\n",
180 /* wait for reset to assert... */
186 static const struct watchdog_info da9062_watchdog_info = {
187 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
188 .identity = "DA9062 WDT",
191 static const struct watchdog_ops da9062_watchdog_ops = {
192 .owner = THIS_MODULE,
193 .start = da9062_wdt_start,
194 .stop = da9062_wdt_stop,
195 .ping = da9062_wdt_ping,
196 .set_timeout = da9062_wdt_set_timeout,
197 .restart = da9062_wdt_restart,
200 static const struct of_device_id da9062_compatible_id_table[] = {
201 { .compatible = "dlg,da9062-watchdog", },
205 MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
207 static int da9062_wdt_probe(struct platform_device *pdev)
211 struct da9062_watchdog *wdt;
213 chip = dev_get_drvdata(pdev->dev.parent);
217 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
223 wdt->wdtdev.info = &da9062_watchdog_info;
224 wdt->wdtdev.ops = &da9062_watchdog_ops;
225 wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
226 wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
227 wdt->wdtdev.min_hw_heartbeat_ms = DA9062_RESET_PROTECTION_MS;
228 wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
229 wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
230 wdt->wdtdev.parent = &pdev->dev;
232 watchdog_set_restart_priority(&wdt->wdtdev, 128);
234 watchdog_set_drvdata(&wdt->wdtdev, wdt);
236 ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdtdev);
238 dev_err(wdt->hw->dev,
239 "watchdog registration failed (%d)\n", ret);
243 da9062_set_window_start(wdt);
245 return da9062_wdt_ping(&wdt->wdtdev);
248 static struct platform_driver da9062_wdt_driver = {
249 .probe = da9062_wdt_probe,
251 .name = "da9062-watchdog",
252 .of_match_table = da9062_compatible_id_table,
255 module_platform_driver(da9062_wdt_driver);
257 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
258 MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
259 MODULE_LICENSE("GPL");
260 MODULE_ALIAS("platform:da9062-watchdog");