2 * Watchdog driver for the wm831x PMICs
4 * Copyright (C) 2009 Wolfson Microelectronics
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/watchdog.h>
18 #include <linux/uaccess.h>
19 #include <linux/gpio.h>
21 #include <linux/mfd/wm831x/core.h>
22 #include <linux/mfd/wm831x/pdata.h>
23 #include <linux/mfd/wm831x/watchdog.h>
25 static bool nowayout
= WATCHDOG_NOWAYOUT
;
26 module_param(nowayout
, bool, 0);
27 MODULE_PARM_DESC(nowayout
,
28 "Watchdog cannot be stopped once started (default="
29 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
31 struct wm831x_wdt_drvdata
{
32 struct watchdog_device wdt
;
33 struct wm831x
*wm831x
;
39 /* We can't use the sub-second values here but they're included
40 * for completeness. */
42 unsigned int time
; /* Seconds */
43 u16 val
; /* WDOG_TO value */
44 } wm831x_wdt_cfgs
[] = {
51 { 33, 7 }, /* Actually 32.768s so include both, others round down */
54 static int wm831x_wdt_start(struct watchdog_device
*wdt_dev
)
56 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
57 struct wm831x
*wm831x
= driver_data
->wm831x
;
60 mutex_lock(&driver_data
->lock
);
62 ret
= wm831x_reg_unlock(wm831x
);
64 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
65 WM831X_WDOG_ENA
, WM831X_WDOG_ENA
);
66 wm831x_reg_lock(wm831x
);
68 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
72 mutex_unlock(&driver_data
->lock
);
77 static int wm831x_wdt_stop(struct watchdog_device
*wdt_dev
)
79 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
80 struct wm831x
*wm831x
= driver_data
->wm831x
;
83 mutex_lock(&driver_data
->lock
);
85 ret
= wm831x_reg_unlock(wm831x
);
87 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
89 wm831x_reg_lock(wm831x
);
91 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
95 mutex_unlock(&driver_data
->lock
);
100 static int wm831x_wdt_ping(struct watchdog_device
*wdt_dev
)
102 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
103 struct wm831x
*wm831x
= driver_data
->wm831x
;
107 mutex_lock(&driver_data
->lock
);
109 if (driver_data
->update_gpio
) {
110 gpio_set_value_cansleep(driver_data
->update_gpio
,
111 driver_data
->update_state
);
112 driver_data
->update_state
= !driver_data
->update_state
;
117 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
119 if (!(reg
& WM831X_WDOG_RST_SRC
)) {
120 dev_err(wm831x
->dev
, "Hardware watchdog update unsupported\n");
125 reg
|= WM831X_WDOG_RESET
;
127 ret
= wm831x_reg_unlock(wm831x
);
129 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
130 wm831x_reg_lock(wm831x
);
132 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
137 mutex_unlock(&driver_data
->lock
);
142 static int wm831x_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
143 unsigned int timeout
)
145 struct wm831x_wdt_drvdata
*driver_data
= watchdog_get_drvdata(wdt_dev
);
146 struct wm831x
*wm831x
= driver_data
->wm831x
;
149 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
150 if (wm831x_wdt_cfgs
[i
].time
== timeout
)
152 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
155 ret
= wm831x_reg_unlock(wm831x
);
157 ret
= wm831x_set_bits(wm831x
, WM831X_WATCHDOG
,
159 wm831x_wdt_cfgs
[i
].val
);
160 wm831x_reg_lock(wm831x
);
162 dev_err(wm831x
->dev
, "Failed to unlock security key: %d\n",
166 wdt_dev
->timeout
= timeout
;
171 static const struct watchdog_info wm831x_wdt_info
= {
172 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
173 .identity
= "WM831x Watchdog",
176 static const struct watchdog_ops wm831x_wdt_ops
= {
177 .owner
= THIS_MODULE
,
178 .start
= wm831x_wdt_start
,
179 .stop
= wm831x_wdt_stop
,
180 .ping
= wm831x_wdt_ping
,
181 .set_timeout
= wm831x_wdt_set_timeout
,
184 static int wm831x_wdt_probe(struct platform_device
*pdev
)
186 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
187 struct wm831x_pdata
*chip_pdata
= dev_get_platdata(pdev
->dev
.parent
);
188 struct wm831x_watchdog_pdata
*pdata
;
189 struct wm831x_wdt_drvdata
*driver_data
;
190 struct watchdog_device
*wm831x_wdt
;
193 ret
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
195 dev_err(wm831x
->dev
, "Failed to read watchdog status: %d\n",
201 if (reg
& WM831X_WDOG_DEBUG
)
202 dev_warn(wm831x
->dev
, "Watchdog is paused\n");
204 driver_data
= devm_kzalloc(&pdev
->dev
, sizeof(*driver_data
),
211 mutex_init(&driver_data
->lock
);
212 driver_data
->wm831x
= wm831x
;
214 wm831x_wdt
= &driver_data
->wdt
;
216 wm831x_wdt
->info
= &wm831x_wdt_info
;
217 wm831x_wdt
->ops
= &wm831x_wdt_ops
;
218 watchdog_set_nowayout(wm831x_wdt
, nowayout
);
219 watchdog_set_drvdata(wm831x_wdt
, driver_data
);
221 reg
= wm831x_reg_read(wm831x
, WM831X_WATCHDOG
);
222 reg
&= WM831X_WDOG_TO_MASK
;
223 for (i
= 0; i
< ARRAY_SIZE(wm831x_wdt_cfgs
); i
++)
224 if (wm831x_wdt_cfgs
[i
].val
== reg
)
226 if (i
== ARRAY_SIZE(wm831x_wdt_cfgs
))
227 dev_warn(wm831x
->dev
,
228 "Unknown watchdog timeout: %x\n", reg
);
230 wm831x_wdt
->timeout
= wm831x_wdt_cfgs
[i
].time
;
232 /* Apply any configuration */
234 pdata
= chip_pdata
->watchdog
;
239 reg
&= ~(WM831X_WDOG_SECACT_MASK
| WM831X_WDOG_PRIMACT_MASK
|
240 WM831X_WDOG_RST_SRC
);
242 reg
|= pdata
->primary
<< WM831X_WDOG_PRIMACT_SHIFT
;
243 reg
|= pdata
->secondary
<< WM831X_WDOG_SECACT_SHIFT
;
244 reg
|= pdata
->software
<< WM831X_WDOG_RST_SRC_SHIFT
;
246 if (pdata
->update_gpio
) {
247 ret
= devm_gpio_request_one(&pdev
->dev
,
253 "Failed to request update GPIO: %d\n",
258 driver_data
->update_gpio
= pdata
->update_gpio
;
260 /* Make sure the watchdog takes hardware updates */
261 reg
|= WM831X_WDOG_RST_SRC
;
264 ret
= wm831x_reg_unlock(wm831x
);
266 ret
= wm831x_reg_write(wm831x
, WM831X_WATCHDOG
, reg
);
267 wm831x_reg_lock(wm831x
);
270 "Failed to unlock security key: %d\n", ret
);
275 ret
= watchdog_register_device(&driver_data
->wdt
);
277 dev_err(wm831x
->dev
, "watchdog_register_device() failed: %d\n",
282 platform_set_drvdata(pdev
, driver_data
);
290 static int wm831x_wdt_remove(struct platform_device
*pdev
)
292 struct wm831x_wdt_drvdata
*driver_data
= platform_get_drvdata(pdev
);
294 watchdog_unregister_device(&driver_data
->wdt
);
299 static struct platform_driver wm831x_wdt_driver
= {
300 .probe
= wm831x_wdt_probe
,
301 .remove
= wm831x_wdt_remove
,
303 .name
= "wm831x-watchdog",
307 module_platform_driver(wm831x_wdt_driver
);
309 MODULE_AUTHOR("Mark Brown");
310 MODULE_DESCRIPTION("WM831x Watchdog");
311 MODULE_LICENSE("GPL");
312 MODULE_ALIAS("platform:wm831x-watchdog");