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>
16 #include <linux/miscdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
20 #include <linux/gpio.h>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 #include <linux/mfd/wm831x/watchdog.h>
26 static int nowayout = WATCHDOG_NOWAYOUT;
27 module_param(nowayout, int, 0);
28 MODULE_PARM_DESC(nowayout,
29 "Watchdog cannot be stopped once started (default="
30 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
32 static unsigned long wm831x_wdt_users;
33 static struct miscdevice wm831x_wdt_miscdev;
34 static int wm831x_wdt_expect_close;
35 static DEFINE_MUTEX(wdt_mutex);
36 static struct wm831x *wm831x;
37 static unsigned int update_gpio;
38 static unsigned int update_state;
40 /* We can't use the sub-second values here but they're included
41 * for completeness. */
43 int time; /* Seconds */
44 u16 val; /* WDOG_TO value */
45 } wm831x_wdt_cfgs[] = {
52 { 33, 7 }, /* Actually 32.768s so include both, others round down */
55 static int wm831x_wdt_set_timeout(struct wm831x *wm831x, u16 value)
59 mutex_lock(&wdt_mutex);
61 ret = wm831x_reg_unlock(wm831x);
63 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
64 WM831X_WDOG_TO_MASK, value);
65 wm831x_reg_lock(wm831x);
67 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
71 mutex_unlock(&wdt_mutex);
76 static int wm831x_wdt_start(struct wm831x *wm831x)
80 mutex_lock(&wdt_mutex);
82 ret = wm831x_reg_unlock(wm831x);
84 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
85 WM831X_WDOG_ENA, WM831X_WDOG_ENA);
86 wm831x_reg_lock(wm831x);
88 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
92 mutex_unlock(&wdt_mutex);
97 static int wm831x_wdt_stop(struct wm831x *wm831x)
101 mutex_lock(&wdt_mutex);
103 ret = wm831x_reg_unlock(wm831x);
105 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
107 wm831x_reg_lock(wm831x);
109 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
113 mutex_unlock(&wdt_mutex);
118 static int wm831x_wdt_kick(struct wm831x *wm831x)
123 mutex_lock(&wdt_mutex);
126 gpio_set_value_cansleep(update_gpio, update_state);
127 update_state = !update_state;
133 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
135 if (!(reg & WM831X_WDOG_RST_SRC)) {
136 dev_err(wm831x->dev, "Hardware watchdog update unsupported\n");
141 reg |= WM831X_WDOG_RESET;
143 ret = wm831x_reg_unlock(wm831x);
145 ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
146 wm831x_reg_lock(wm831x);
148 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
153 mutex_unlock(&wdt_mutex);
158 static int wm831x_wdt_open(struct inode *inode, struct file *file)
165 if (test_and_set_bit(0, &wm831x_wdt_users))
168 ret = wm831x_wdt_start(wm831x);
172 return nonseekable_open(inode, file);
175 static int wm831x_wdt_release(struct inode *inode, struct file *file)
177 if (wm831x_wdt_expect_close)
178 wm831x_wdt_stop(wm831x);
180 dev_warn(wm831x->dev, "Watchdog device closed uncleanly\n");
181 wm831x_wdt_kick(wm831x);
184 clear_bit(0, &wm831x_wdt_users);
189 static ssize_t wm831x_wdt_write(struct file *file,
190 const char __user *data, size_t count,
196 wm831x_wdt_kick(wm831x);
199 /* In case it was set long ago */
200 wm831x_wdt_expect_close = 0;
202 /* scan to see whether or not we got the magic
204 for (i = 0; i != count; i++) {
206 if (get_user(c, data + i))
209 wm831x_wdt_expect_close = 42;
216 static const struct watchdog_info ident = {
217 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
218 .identity = "WM831x Watchdog",
221 static long wm831x_wdt_ioctl(struct file *file, unsigned int cmd,
224 int ret = -ENOTTY, time, i;
225 void __user *argp = (void __user *)arg;
226 int __user *p = argp;
230 case WDIOC_GETSUPPORT:
231 ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
234 case WDIOC_GETSTATUS:
235 case WDIOC_GETBOOTSTATUS:
236 ret = put_user(0, p);
239 case WDIOC_SETOPTIONS:
243 if (get_user(options, p))
248 /* Setting both simultaneously means at least one must fail */
249 if (options == WDIOS_DISABLECARD)
250 ret = wm831x_wdt_start(wm831x);
252 if (options == WDIOS_ENABLECARD)
253 ret = wm831x_wdt_stop(wm831x);
257 case WDIOC_KEEPALIVE:
258 ret = wm831x_wdt_kick(wm831x);
261 case WDIOC_SETTIMEOUT:
262 ret = get_user(time, p);
270 wm831x_wdt_stop(wm831x);
274 for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
275 if (wm831x_wdt_cfgs[i].time == time)
277 if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
280 ret = wm831x_wdt_set_timeout(wm831x,
281 wm831x_wdt_cfgs[i].val);
284 case WDIOC_GETTIMEOUT:
285 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
286 reg &= WM831X_WDOG_TO_MASK;
287 for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
288 if (wm831x_wdt_cfgs[i].val == reg)
290 if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) {
291 dev_warn(wm831x->dev,
292 "Unknown watchdog configuration: %x\n", reg);
295 ret = put_user(wm831x_wdt_cfgs[i].time, p);
302 static const struct file_operations wm831x_wdt_fops = {
303 .owner = THIS_MODULE,
305 .write = wm831x_wdt_write,
306 .unlocked_ioctl = wm831x_wdt_ioctl,
307 .open = wm831x_wdt_open,
308 .release = wm831x_wdt_release,
311 static struct miscdevice wm831x_wdt_miscdev = {
312 .minor = WATCHDOG_MINOR,
314 .fops = &wm831x_wdt_fops,
317 static int __devinit wm831x_wdt_probe(struct platform_device *pdev)
319 struct wm831x_pdata *chip_pdata;
320 struct wm831x_watchdog_pdata *pdata;
324 dev_err(&pdev->dev, "wm831x watchdog already registered\n");
328 wm831x = dev_get_drvdata(pdev->dev.parent);
330 ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
332 dev_err(wm831x->dev, "Failed to read watchdog status: %d\n",
338 if (reg & WM831X_WDOG_DEBUG)
339 dev_warn(wm831x->dev, "Watchdog is paused\n");
341 /* Apply any configuration */
342 if (pdev->dev.parent->platform_data) {
343 chip_pdata = pdev->dev.parent->platform_data;
344 pdata = chip_pdata->watchdog;
350 reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
351 WM831X_WDOG_RST_SRC);
353 reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT;
354 reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT;
355 reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT;
357 if (pdata->update_gpio) {
358 ret = gpio_request(pdata->update_gpio,
362 "Failed to request update GPIO: %d\n",
367 ret = gpio_direction_output(pdata->update_gpio, 0);
370 "gpio_direction_output returned: %d\n",
375 update_gpio = pdata->update_gpio;
377 /* Make sure the watchdog takes hardware updates */
378 reg |= WM831X_WDOG_RST_SRC;
381 ret = wm831x_reg_unlock(wm831x);
383 ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
384 wm831x_reg_lock(wm831x);
387 "Failed to unlock security key: %d\n", ret);
392 wm831x_wdt_miscdev.parent = &pdev->dev;
394 ret = misc_register(&wm831x_wdt_miscdev);
396 dev_err(wm831x->dev, "Failed to register miscdev: %d\n", ret);
404 gpio_free(update_gpio);
411 static int __devexit wm831x_wdt_remove(struct platform_device *pdev)
414 gpio_free(update_gpio);
418 misc_deregister(&wm831x_wdt_miscdev);
423 static struct platform_driver wm831x_wdt_driver = {
424 .probe = wm831x_wdt_probe,
425 .remove = __devexit_p(wm831x_wdt_remove),
427 .name = "wm831x-watchdog",
431 static int __init wm831x_wdt_init(void)
433 return platform_driver_register(&wm831x_wdt_driver);
435 module_init(wm831x_wdt_init);
437 static void __exit wm831x_wdt_exit(void)
439 platform_driver_unregister(&wm831x_wdt_driver);
441 module_exit(wm831x_wdt_exit);
443 MODULE_AUTHOR("Mark Brown");
444 MODULE_DESCRIPTION("WM831x Watchdog");
445 MODULE_LICENSE("GPL");
446 MODULE_ALIAS("platform:wm831x-watchdog");