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>
16 #include <linux/gpio.h>
18 #include <linux/mfd/wm831x/core.h>
19 #include <linux/mfd/wm831x/pdata.h>
20 #include <linux/mfd/wm831x/watchdog.h>
22 static bool nowayout
= WATCHDOG_NOWAYOUT
;
23 module_param(nowayout
, bool, 0);
24 MODULE_PARM_DESC(nowayout
,
25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
28 struct wm831x_wdt_drvdata
{
29 struct watchdog_device wdt
;
30 struct wm831x
*wm831x
;
36 /* We can't use the sub-second values here but they're included
37 * for completeness. */
39 unsigned int time
; /* Seconds */
40 u16 val
; /* WDOG_TO value */
41 } wm831x_wdt_cfgs
[] = {
48 { 33, 7 }, /* Actually 32.768s so include both, others round down */
51 static int wm831x_wdt_start(struct watchdog_device
*wdt_dev
)
53 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
54 struct wm831x
*wm831x
= driver_data
->wm831x
;
57 mutex_lock(&driver_data
->lock
);
59 ret
= wm831x_reg_unlock(wm831x
);
61 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
62 WM831X_WDOG_ENA
, WM831X_WDOG_ENA
);
63 wm831x_reg_lock(wm831x
);
65 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
69 mutex_unlock(&driver_data
->lock
);
74 static int wm831x_wdt_stop(struct watchdog_device
*wdt_dev
)
76 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
77 struct wm831x
*wm831x
= driver_data
->wm831x
;
80 mutex_lock(&driver_data
->lock
);
82 ret
= wm831x_reg_unlock(wm831x
);
84 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
86 wm831x_reg_lock(wm831x
);
88 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
92 mutex_unlock(&driver_data
->lock
);
97 static int wm831x_wdt_ping(struct watchdog_device
*wdt_dev
)
99 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
100 struct wm831x
*wm831x
= driver_data
->wm831x
;
104 mutex_lock(&driver_data
->lock
);
106 if (driver_data
->update_gpio
) {
107 gpio_set_value_cansleep(driver_data
->update_gpio
,
108 driver_data
->update_state
);
109 driver_data
->update_state
= !driver_data
->update_state
;
114 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
116 if (!(reg
& WM831X_WDOG_RST_SRC
)) {
117 dev_err(wm831x
->dev
, "Hardware watchdog update unsupported\n");
122 reg
|= WM831X_WDOG_RESET
;
124 ret
= wm831x_reg_unlock(wm831x
);
126 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
127 wm831x_reg_lock(wm831x
);
129 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
134 mutex_unlock(&driver_data
->lock
);
139 static int wm831x_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
140 unsigned int timeout
)
142 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
143 struct wm831x
*wm831x
= driver_data
->wm831x
;
146 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
147 if (wm831x_wdt_cfgs
[i
].time
== timeout
)
149 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
152 ret
= wm831x_reg_unlock(wm831x
);
154 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
156 wm831x_wdt_cfgs
[i
].val
);
157 wm831x_reg_lock(wm831x
);
159 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
163 wdt_dev
->timeout
= timeout
;
168 static const struct watchdog_info wm831x_wdt_info
= {
169 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
170 .identity
= "WM831x Watchdog",
173 static const struct watchdog_ops wm831x_wdt_ops
= {
174 .owner
= THIS_MODULE
,
175 .start
= wm831x_wdt_start
,
176 .stop
= wm831x_wdt_stop
,
177 .ping
= wm831x_wdt_ping
,
178 .set_timeout
= wm831x_wdt_set_timeout
,
181 static int wm831x_wdt_probe(struct platform_device
*pdev
)
183 struct device
*dev
= &pdev
->dev
;
184 struct wm831x
*wm831x
= dev_get_drvdata(dev
->parent
);
185 struct wm831x_pdata
*chip_pdata
= dev_get_platdata(dev
->parent
);
186 struct wm831x_watchdog_pdata
*pdata
;
187 struct wm831x_wdt_drvdata
*driver_data
;
188 struct watchdog_device
*wm831x_wdt
;
191 ret
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
193 dev_err(wm831x
->dev
, "Failed to read watchdog status: %d\n",
199 if (reg
& WM831X_WDOG_DEBUG
)
200 dev_warn(wm831x
->dev
, "Watchdog is paused\n");
202 driver_data
= devm_kzalloc(dev
, sizeof(*driver_data
), GFP_KERNEL
);
206 mutex_init(&driver_data
->lock
);
207 driver_data
->wm831x
= wm831x
;
209 wm831x_wdt
= &driver_data
->wdt
;
211 wm831x_wdt
->info
= &wm831x_wdt_info
;
212 wm831x_wdt
->ops
= &wm831x_wdt_ops
;
213 wm831x_wdt
->parent
= dev
;
214 watchdog_set_nowayout(wm831x_wdt
, nowayout
);
215 watchdog_set_drvdata(wm831x_wdt
, driver_data
);
217 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
218 reg
&= WM831X_WDOG_TO_MASK
;
219 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
220 if (wm831x_wdt_cfgs
[i
].val
== reg
)
222 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
223 dev_warn(wm831x
->dev
,
224 "Unknown watchdog timeout: %x\n", reg
);
226 wm831x_wdt
->timeout
= wm831x_wdt_cfgs
[i
].time
;
228 /* Apply any configuration */
230 pdata
= chip_pdata
->watchdog
;
235 reg
&= ~(WM831X_WDOG_SECACT_MASK
| WM831X_WDOG_PRIMACT_MASK
|
236 WM831X_WDOG_RST_SRC
);
238 reg
|= pdata
->primary
<< WM831X_WDOG_PRIMACT_SHIFT
;
239 reg
|= pdata
->secondary
<< WM831X_WDOG_SECACT_SHIFT
;
240 reg
|= pdata
->software
<< WM831X_WDOG_RST_SRC_SHIFT
;
242 if (pdata
->update_gpio
) {
243 ret
= devm_gpio_request_one(dev
, pdata
->update_gpio
,
248 "Failed to request update GPIO: %d\n",
253 driver_data
->update_gpio
= pdata
->update_gpio
;
255 /* Make sure the watchdog takes hardware updates */
256 reg
|= WM831X_WDOG_RST_SRC
;
259 ret
= wm831x_reg_unlock(wm831x
);
261 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
262 wm831x_reg_lock(wm831x
);
265 "Failed to unlock security key: %d\n", ret
);
270 return devm_watchdog_register_device(dev
, &driver_data
->wdt
);
273 static struct platform_driver wm831x_wdt_driver
= {
274 .probe
= wm831x_wdt_probe
,
276 .name
= "wm831x-watchdog",
280 module_platform_driver(wm831x_wdt_driver
);
282 MODULE_AUTHOR("Mark Brown");
283 MODULE_DESCRIPTION("WM831x Watchdog");
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS("platform:wm831x-watchdog");