1 // SPDX-License-Identifier: GPL-2.0+
3 * Watchdog device driver for DA9062 and DA9061 PMICs
4 * Copyright (C) 2015 Dialog Semiconductor Ltd.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/watchdog.h>
11 #include <linux/platform_device.h>
12 #include <linux/uaccess.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/mfd/da9062/registers.h>
17 #include <linux/mfd/da9062/core.h>
18 #include <linux/regmap.h>
21 static const unsigned int wdt_timeout
[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
22 #define DA9062_TWDSCALE_DISABLE 0
23 #define DA9062_TWDSCALE_MIN 1
24 #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
25 #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN]
26 #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX]
27 #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1]
28 #define DA9062_RESET_PROTECTION_MS 300
30 struct da9062_watchdog
{
32 struct watchdog_device wdtdev
;
35 static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs
)
39 for (i
= DA9062_TWDSCALE_MIN
; i
<= DA9062_TWDSCALE_MAX
; i
++) {
40 if (wdt_timeout
[i
] >= secs
)
44 return DA9062_TWDSCALE_MAX
;
47 static int da9062_reset_watchdog_timer(struct da9062_watchdog
*wdt
)
51 ret
= regmap_update_bits(wdt
->hw
->regmap
,
53 DA9062AA_WATCHDOG_MASK
,
54 DA9062AA_WATCHDOG_MASK
);
59 static int da9062_wdt_update_timeout_register(struct da9062_watchdog
*wdt
,
62 struct da9062
*chip
= wdt
->hw
;
64 regmap_update_bits(chip
->regmap
,
66 DA9062AA_TWDSCALE_MASK
,
67 DA9062_TWDSCALE_DISABLE
);
69 usleep_range(150, 300);
71 return regmap_update_bits(chip
->regmap
,
73 DA9062AA_TWDSCALE_MASK
,
77 static int da9062_wdt_start(struct watchdog_device
*wdd
)
79 struct da9062_watchdog
*wdt
= watchdog_get_drvdata(wdd
);
80 unsigned int selector
;
83 selector
= da9062_wdt_timeout_to_sel(wdt
->wdtdev
.timeout
);
84 ret
= da9062_wdt_update_timeout_register(wdt
, selector
);
86 dev_err(wdt
->hw
->dev
, "Watchdog failed to start (err = %d)\n",
92 static int da9062_wdt_stop(struct watchdog_device
*wdd
)
94 struct da9062_watchdog
*wdt
= watchdog_get_drvdata(wdd
);
97 ret
= regmap_update_bits(wdt
->hw
->regmap
,
99 DA9062AA_TWDSCALE_MASK
,
100 DA9062_TWDSCALE_DISABLE
);
102 dev_err(wdt
->hw
->dev
, "Watchdog failed to stop (err = %d)\n",
108 static int da9062_wdt_ping(struct watchdog_device
*wdd
)
110 struct da9062_watchdog
*wdt
= watchdog_get_drvdata(wdd
);
113 ret
= da9062_reset_watchdog_timer(wdt
);
115 dev_err(wdt
->hw
->dev
, "Failed to ping the watchdog (err = %d)\n",
121 static int da9062_wdt_set_timeout(struct watchdog_device
*wdd
,
122 unsigned int timeout
)
124 struct da9062_watchdog
*wdt
= watchdog_get_drvdata(wdd
);
125 unsigned int selector
;
128 selector
= da9062_wdt_timeout_to_sel(timeout
);
129 ret
= da9062_wdt_update_timeout_register(wdt
, selector
);
131 dev_err(wdt
->hw
->dev
, "Failed to set watchdog timeout (err = %d)\n",
134 wdd
->timeout
= wdt_timeout
[selector
];
139 static int da9062_wdt_restart(struct watchdog_device
*wdd
, unsigned long action
,
142 struct da9062_watchdog
*wdt
= watchdog_get_drvdata(wdd
);
145 ret
= regmap_write(wdt
->hw
->regmap
,
147 DA9062AA_SHUTDOWN_MASK
);
149 dev_alert(wdt
->hw
->dev
, "Failed to shutdown (err = %d)\n",
152 /* wait for reset to assert... */
158 static const struct watchdog_info da9062_watchdog_info
= {
159 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
160 .identity
= "DA9062 WDT",
163 static const struct watchdog_ops da9062_watchdog_ops
= {
164 .owner
= THIS_MODULE
,
165 .start
= da9062_wdt_start
,
166 .stop
= da9062_wdt_stop
,
167 .ping
= da9062_wdt_ping
,
168 .set_timeout
= da9062_wdt_set_timeout
,
169 .restart
= da9062_wdt_restart
,
172 static const struct of_device_id da9062_compatible_id_table
[] = {
173 { .compatible
= "dlg,da9062-watchdog", },
177 MODULE_DEVICE_TABLE(of
, da9062_compatible_id_table
);
179 static int da9062_wdt_probe(struct platform_device
*pdev
)
183 struct da9062_watchdog
*wdt
;
185 chip
= dev_get_drvdata(pdev
->dev
.parent
);
189 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
195 wdt
->wdtdev
.info
= &da9062_watchdog_info
;
196 wdt
->wdtdev
.ops
= &da9062_watchdog_ops
;
197 wdt
->wdtdev
.min_timeout
= DA9062_WDT_MIN_TIMEOUT
;
198 wdt
->wdtdev
.max_timeout
= DA9062_WDT_MAX_TIMEOUT
;
199 wdt
->wdtdev
.min_hw_heartbeat_ms
= DA9062_RESET_PROTECTION_MS
;
200 wdt
->wdtdev
.timeout
= DA9062_WDG_DEFAULT_TIMEOUT
;
201 wdt
->wdtdev
.status
= WATCHDOG_NOWAYOUT_INIT_STATUS
;
202 wdt
->wdtdev
.parent
= &pdev
->dev
;
204 watchdog_set_restart_priority(&wdt
->wdtdev
, 128);
206 watchdog_set_drvdata(&wdt
->wdtdev
, wdt
);
208 ret
= devm_watchdog_register_device(&pdev
->dev
, &wdt
->wdtdev
);
210 dev_err(wdt
->hw
->dev
,
211 "watchdog registration failed (%d)\n", ret
);
215 return da9062_wdt_ping(&wdt
->wdtdev
);
218 static struct platform_driver da9062_wdt_driver
= {
219 .probe
= da9062_wdt_probe
,
221 .name
= "da9062-watchdog",
222 .of_match_table
= da9062_compatible_id_table
,
225 module_platform_driver(da9062_wdt_driver
);
227 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
228 MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
229 MODULE_LICENSE("GPL");
230 MODULE_ALIAS("platform:da9062-watchdog");