1 // SPDX-License-Identifier: GPL-2.0+
3 * Watchdog driver for the wm831x PMICs
5 * Copyright (C) 2009 Wolfson Microelectronics
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/watchdog.h>
15 #include <linux/uaccess.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/pdata.h>
19 #include <linux/mfd/wm831x/watchdog.h>
21 static bool nowayout
= WATCHDOG_NOWAYOUT
;
22 module_param(nowayout
, bool, 0);
23 MODULE_PARM_DESC(nowayout
,
24 "Watchdog cannot be stopped once started (default="
25 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
27 struct wm831x_wdt_drvdata
{
28 struct watchdog_device wdt
;
29 struct wm831x
*wm831x
;
34 /* We can't use the sub-second values here but they're included
35 * for completeness. */
37 unsigned int time
; /* Seconds */
38 u16 val
; /* WDOG_TO value */
39 } wm831x_wdt_cfgs
[] = {
46 { 33, 7 }, /* Actually 32.768s so include both, others round down */
49 static int wm831x_wdt_start(struct watchdog_device
*wdt_dev
)
51 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
52 struct wm831x
*wm831x
= driver_data
->wm831x
;
55 mutex_lock(&driver_data
->lock
);
57 ret
= wm831x_reg_unlock(wm831x
);
59 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
60 WM831X_WDOG_ENA
, WM831X_WDOG_ENA
);
61 wm831x_reg_lock(wm831x
);
63 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
67 mutex_unlock(&driver_data
->lock
);
72 static int wm831x_wdt_stop(struct watchdog_device
*wdt_dev
)
74 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
75 struct wm831x
*wm831x
= driver_data
->wm831x
;
78 mutex_lock(&driver_data
->lock
);
80 ret
= wm831x_reg_unlock(wm831x
);
82 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
84 wm831x_reg_lock(wm831x
);
86 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
90 mutex_unlock(&driver_data
->lock
);
95 static int wm831x_wdt_ping(struct watchdog_device
*wdt_dev
)
97 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
98 struct wm831x
*wm831x
= driver_data
->wm831x
;
102 mutex_lock(&driver_data
->lock
);
104 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
106 if (!(reg
& WM831X_WDOG_RST_SRC
)) {
107 dev_err(wm831x
->dev
, "Hardware watchdog update unsupported\n");
112 reg
|= WM831X_WDOG_RESET
;
114 ret
= wm831x_reg_unlock(wm831x
);
116 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
117 wm831x_reg_lock(wm831x
);
119 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
124 mutex_unlock(&driver_data
->lock
);
129 static int wm831x_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
130 unsigned int timeout
)
132 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
133 struct wm831x
*wm831x
= driver_data
->wm831x
;
136 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
137 if (wm831x_wdt_cfgs
[i
].time
== timeout
)
139 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
142 ret
= wm831x_reg_unlock(wm831x
);
144 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
146 wm831x_wdt_cfgs
[i
].val
);
147 wm831x_reg_lock(wm831x
);
149 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
153 wdt_dev
->timeout
= timeout
;
158 static const struct watchdog_info wm831x_wdt_info
= {
159 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
160 .identity
= "WM831x Watchdog",
163 static const struct watchdog_ops wm831x_wdt_ops
= {
164 .owner
= THIS_MODULE
,
165 .start
= wm831x_wdt_start
,
166 .stop
= wm831x_wdt_stop
,
167 .ping
= wm831x_wdt_ping
,
168 .set_timeout
= wm831x_wdt_set_timeout
,
171 static int wm831x_wdt_probe(struct platform_device
*pdev
)
173 struct device
*dev
= &pdev
->dev
;
174 struct wm831x
*wm831x
= dev_get_drvdata(dev
->parent
);
175 struct wm831x_pdata
*chip_pdata
= dev_get_platdata(dev
->parent
);
176 struct wm831x_watchdog_pdata
*pdata
;
177 struct wm831x_wdt_drvdata
*driver_data
;
178 struct watchdog_device
*wm831x_wdt
;
181 ret
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
183 dev_err(wm831x
->dev
, "Failed to read watchdog status: %d\n",
189 if (reg
& WM831X_WDOG_DEBUG
)
190 dev_warn(wm831x
->dev
, "Watchdog is paused\n");
192 driver_data
= devm_kzalloc(dev
, sizeof(*driver_data
), GFP_KERNEL
);
196 mutex_init(&driver_data
->lock
);
197 driver_data
->wm831x
= wm831x
;
199 wm831x_wdt
= &driver_data
->wdt
;
201 wm831x_wdt
->info
= &wm831x_wdt_info
;
202 wm831x_wdt
->ops
= &wm831x_wdt_ops
;
203 wm831x_wdt
->parent
= dev
;
204 watchdog_set_nowayout(wm831x_wdt
, nowayout
);
205 watchdog_set_drvdata(wm831x_wdt
, driver_data
);
207 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
208 reg
&= WM831X_WDOG_TO_MASK
;
209 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
210 if (wm831x_wdt_cfgs
[i
].val
== reg
)
212 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
213 dev_warn(wm831x
->dev
,
214 "Unknown watchdog timeout: %x\n", reg
);
216 wm831x_wdt
->timeout
= wm831x_wdt_cfgs
[i
].time
;
218 /* Apply any configuration */
220 pdata
= chip_pdata
->watchdog
;
225 reg
&= ~(WM831X_WDOG_SECACT_MASK
| WM831X_WDOG_PRIMACT_MASK
|
226 WM831X_WDOG_RST_SRC
);
228 reg
|= pdata
->primary
<< WM831X_WDOG_PRIMACT_SHIFT
;
229 reg
|= pdata
->secondary
<< WM831X_WDOG_SECACT_SHIFT
;
230 reg
|= pdata
->software
<< WM831X_WDOG_RST_SRC_SHIFT
;
232 ret
= wm831x_reg_unlock(wm831x
);
234 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
235 wm831x_reg_lock(wm831x
);
238 "Failed to unlock security key: %d\n", ret
);
243 return devm_watchdog_register_device(dev
, &driver_data
->wdt
);
246 static struct platform_driver wm831x_wdt_driver
= {
247 .probe
= wm831x_wdt_probe
,
249 .name
= "wm831x-watchdog",
253 module_platform_driver(wm831x_wdt_driver
);
255 MODULE_AUTHOR("Mark Brown");
256 MODULE_DESCRIPTION("WM831x Watchdog");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:wm831x-watchdog");