1502a0106SMark Brown /* 2502a0106SMark Brown * Watchdog driver for the wm831x PMICs 3502a0106SMark Brown * 4502a0106SMark Brown * Copyright (C) 2009 Wolfson Microelectronics 5502a0106SMark Brown * 6502a0106SMark Brown * This program is free software; you can redistribute it and/or 7502a0106SMark Brown * modify it under the terms of the GNU General Public License 8502a0106SMark Brown * as published by the Free Software Foundation 9502a0106SMark Brown */ 10502a0106SMark Brown 11502a0106SMark Brown #include <linux/module.h> 12502a0106SMark Brown #include <linux/moduleparam.h> 13502a0106SMark Brown #include <linux/types.h> 14502a0106SMark Brown #include <linux/kernel.h> 1500411ee9SMark Brown #include <linux/slab.h> 16502a0106SMark Brown #include <linux/platform_device.h> 17502a0106SMark Brown #include <linux/watchdog.h> 18502a0106SMark Brown #include <linux/uaccess.h> 19502a0106SMark Brown #include <linux/gpio.h> 20502a0106SMark Brown 21502a0106SMark Brown #include <linux/mfd/wm831x/core.h> 22502a0106SMark Brown #include <linux/mfd/wm831x/pdata.h> 23502a0106SMark Brown #include <linux/mfd/wm831x/watchdog.h> 24502a0106SMark Brown 25*86a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 26*86a1e189SWim Van Sebroeck module_param(nowayout, bool, 0); 27502a0106SMark Brown MODULE_PARM_DESC(nowayout, 28502a0106SMark Brown "Watchdog cannot be stopped once started (default=" 29502a0106SMark Brown __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 30502a0106SMark Brown 3100411ee9SMark Brown struct wm831x_wdt_drvdata { 3200411ee9SMark Brown struct watchdog_device wdt; 3300411ee9SMark Brown struct wm831x *wm831x; 3400411ee9SMark Brown struct mutex lock; 3500411ee9SMark Brown int update_gpio; 3600411ee9SMark Brown int update_state; 3700411ee9SMark Brown }; 38502a0106SMark Brown 39502a0106SMark Brown /* We can't use the sub-second values here but they're included 40502a0106SMark Brown * for completeness. */ 41502a0106SMark Brown static struct { 4200411ee9SMark Brown unsigned int time; /* Seconds */ 43502a0106SMark Brown u16 val; /* WDOG_TO value */ 44502a0106SMark Brown } wm831x_wdt_cfgs[] = { 45502a0106SMark Brown { 1, 2 }, 46502a0106SMark Brown { 2, 3 }, 47502a0106SMark Brown { 4, 4 }, 48502a0106SMark Brown { 8, 5 }, 49502a0106SMark Brown { 16, 6 }, 50502a0106SMark Brown { 32, 7 }, 51502a0106SMark Brown { 33, 7 }, /* Actually 32.768s so include both, others round down */ 52502a0106SMark Brown }; 53502a0106SMark Brown 5400411ee9SMark Brown static int wm831x_wdt_start(struct watchdog_device *wdt_dev) 55502a0106SMark Brown { 5600411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 5700411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 58502a0106SMark Brown int ret; 59502a0106SMark Brown 6000411ee9SMark Brown mutex_lock(&driver_data->lock); 61502a0106SMark Brown 62502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 63502a0106SMark Brown if (ret == 0) { 64502a0106SMark Brown ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, 65502a0106SMark Brown WM831X_WDOG_ENA, WM831X_WDOG_ENA); 66502a0106SMark Brown wm831x_reg_lock(wm831x); 67502a0106SMark Brown } else { 68502a0106SMark Brown dev_err(wm831x->dev, "Failed to unlock security key: %d\n", 69502a0106SMark Brown ret); 70502a0106SMark Brown } 71502a0106SMark Brown 7200411ee9SMark Brown mutex_unlock(&driver_data->lock); 73502a0106SMark Brown 74502a0106SMark Brown return ret; 75502a0106SMark Brown } 76502a0106SMark Brown 7700411ee9SMark Brown static int wm831x_wdt_stop(struct watchdog_device *wdt_dev) 78502a0106SMark Brown { 7900411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 8000411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 81502a0106SMark Brown int ret; 82502a0106SMark Brown 8300411ee9SMark Brown mutex_lock(&driver_data->lock); 84502a0106SMark Brown 85502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 86502a0106SMark Brown if (ret == 0) { 87502a0106SMark Brown ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, 88502a0106SMark Brown WM831X_WDOG_ENA, 0); 89502a0106SMark Brown wm831x_reg_lock(wm831x); 90502a0106SMark Brown } else { 91502a0106SMark Brown dev_err(wm831x->dev, "Failed to unlock security key: %d\n", 92502a0106SMark Brown ret); 93502a0106SMark Brown } 94502a0106SMark Brown 9500411ee9SMark Brown mutex_unlock(&driver_data->lock); 96502a0106SMark Brown 97502a0106SMark Brown return ret; 98502a0106SMark Brown } 99502a0106SMark Brown 10000411ee9SMark Brown static int wm831x_wdt_ping(struct watchdog_device *wdt_dev) 101502a0106SMark Brown { 10200411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 10300411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 104502a0106SMark Brown int ret; 105502a0106SMark Brown u16 reg; 106502a0106SMark Brown 10700411ee9SMark Brown mutex_lock(&driver_data->lock); 108502a0106SMark Brown 10900411ee9SMark Brown if (driver_data->update_gpio) { 11000411ee9SMark Brown gpio_set_value_cansleep(driver_data->update_gpio, 11100411ee9SMark Brown driver_data->update_state); 11200411ee9SMark Brown driver_data->update_state = !driver_data->update_state; 113502a0106SMark Brown ret = 0; 114502a0106SMark Brown goto out; 115502a0106SMark Brown } 116502a0106SMark Brown 117502a0106SMark Brown reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 118502a0106SMark Brown 119502a0106SMark Brown if (!(reg & WM831X_WDOG_RST_SRC)) { 120502a0106SMark Brown dev_err(wm831x->dev, "Hardware watchdog update unsupported\n"); 121502a0106SMark Brown ret = -EINVAL; 122502a0106SMark Brown goto out; 123502a0106SMark Brown } 124502a0106SMark Brown 125502a0106SMark Brown reg |= WM831X_WDOG_RESET; 126502a0106SMark Brown 127502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 128502a0106SMark Brown if (ret == 0) { 129502a0106SMark Brown ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); 130502a0106SMark Brown wm831x_reg_lock(wm831x); 131502a0106SMark Brown } else { 132502a0106SMark Brown dev_err(wm831x->dev, "Failed to unlock security key: %d\n", 133502a0106SMark Brown ret); 134502a0106SMark Brown } 135502a0106SMark Brown 136502a0106SMark Brown out: 13700411ee9SMark Brown mutex_unlock(&driver_data->lock); 138502a0106SMark Brown 139502a0106SMark Brown return ret; 140502a0106SMark Brown } 141502a0106SMark Brown 14200411ee9SMark Brown static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev, 14300411ee9SMark Brown unsigned int timeout) 144502a0106SMark Brown { 14500411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 14600411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 14700411ee9SMark Brown int ret, i; 148502a0106SMark Brown 14900411ee9SMark Brown for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) 15000411ee9SMark Brown if (wm831x_wdt_cfgs[i].time == timeout) 15100411ee9SMark Brown break; 15200411ee9SMark Brown if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) 153f9849100SMark Brown return -EINVAL; 154502a0106SMark Brown 15500411ee9SMark Brown ret = wm831x_reg_unlock(wm831x); 15600411ee9SMark Brown if (ret == 0) { 15700411ee9SMark Brown ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, 15800411ee9SMark Brown WM831X_WDOG_TO_MASK, 15900411ee9SMark Brown wm831x_wdt_cfgs[i].val); 16000411ee9SMark Brown wm831x_reg_lock(wm831x); 16100411ee9SMark Brown } else { 16200411ee9SMark Brown dev_err(wm831x->dev, "Failed to unlock security key: %d\n", 16300411ee9SMark Brown ret); 16400411ee9SMark Brown } 165502a0106SMark Brown 166502a0106SMark Brown return ret; 167502a0106SMark Brown } 168502a0106SMark Brown 16900411ee9SMark Brown static const struct watchdog_info wm831x_wdt_info = { 170502a0106SMark Brown .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 171502a0106SMark Brown .identity = "WM831x Watchdog", 172502a0106SMark Brown }; 173502a0106SMark Brown 17400411ee9SMark Brown static const struct watchdog_ops wm831x_wdt_ops = { 175502a0106SMark Brown .owner = THIS_MODULE, 17600411ee9SMark Brown .start = wm831x_wdt_start, 17700411ee9SMark Brown .stop = wm831x_wdt_stop, 17800411ee9SMark Brown .ping = wm831x_wdt_ping, 17900411ee9SMark Brown .set_timeout = wm831x_wdt_set_timeout, 180502a0106SMark Brown }; 181502a0106SMark Brown 182502a0106SMark Brown static int __devinit wm831x_wdt_probe(struct platform_device *pdev) 183502a0106SMark Brown { 18400411ee9SMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 185502a0106SMark Brown struct wm831x_pdata *chip_pdata; 186502a0106SMark Brown struct wm831x_watchdog_pdata *pdata; 18700411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data; 18800411ee9SMark Brown struct watchdog_device *wm831x_wdt; 18900411ee9SMark Brown int reg, ret, i; 190502a0106SMark Brown 191502a0106SMark Brown ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 192502a0106SMark Brown if (ret < 0) { 193502a0106SMark Brown dev_err(wm831x->dev, "Failed to read watchdog status: %d\n", 194502a0106SMark Brown ret); 195502a0106SMark Brown goto err; 196502a0106SMark Brown } 197502a0106SMark Brown reg = ret; 198502a0106SMark Brown 199502a0106SMark Brown if (reg & WM831X_WDOG_DEBUG) 200502a0106SMark Brown dev_warn(wm831x->dev, "Watchdog is paused\n"); 201502a0106SMark Brown 2027b9bb6d8SMark Brown driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), 2037b9bb6d8SMark Brown GFP_KERNEL); 20400411ee9SMark Brown if (!driver_data) { 20500411ee9SMark Brown dev_err(wm831x->dev, "Unable to alloacate watchdog device\n"); 20600411ee9SMark Brown ret = -ENOMEM; 20700411ee9SMark Brown goto err; 20800411ee9SMark Brown } 20900411ee9SMark Brown 21000411ee9SMark Brown mutex_init(&driver_data->lock); 21100411ee9SMark Brown driver_data->wm831x = wm831x; 21200411ee9SMark Brown 21300411ee9SMark Brown wm831x_wdt = &driver_data->wdt; 21400411ee9SMark Brown 21500411ee9SMark Brown wm831x_wdt->info = &wm831x_wdt_info; 21600411ee9SMark Brown wm831x_wdt->ops = &wm831x_wdt_ops; 217ff0b3cd4SWim Van Sebroeck watchdog_set_nowayout(wm831x_wdt, nowayout); 21800411ee9SMark Brown watchdog_set_drvdata(wm831x_wdt, driver_data); 21900411ee9SMark Brown 22000411ee9SMark Brown reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 22100411ee9SMark Brown reg &= WM831X_WDOG_TO_MASK; 22200411ee9SMark Brown for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) 22300411ee9SMark Brown if (wm831x_wdt_cfgs[i].val == reg) 22400411ee9SMark Brown break; 22500411ee9SMark Brown if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) 22600411ee9SMark Brown dev_warn(wm831x->dev, 22700411ee9SMark Brown "Unknown watchdog timeout: %x\n", reg); 22800411ee9SMark Brown else 22900411ee9SMark Brown wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time; 23000411ee9SMark Brown 231502a0106SMark Brown /* Apply any configuration */ 232502a0106SMark Brown if (pdev->dev.parent->platform_data) { 233502a0106SMark Brown chip_pdata = pdev->dev.parent->platform_data; 234502a0106SMark Brown pdata = chip_pdata->watchdog; 235502a0106SMark Brown } else { 236502a0106SMark Brown pdata = NULL; 237502a0106SMark Brown } 238502a0106SMark Brown 239502a0106SMark Brown if (pdata) { 240502a0106SMark Brown reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK | 241502a0106SMark Brown WM831X_WDOG_RST_SRC); 242502a0106SMark Brown 243502a0106SMark Brown reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT; 244502a0106SMark Brown reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT; 245502a0106SMark Brown reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT; 246502a0106SMark Brown 247502a0106SMark Brown if (pdata->update_gpio) { 248502a0106SMark Brown ret = gpio_request(pdata->update_gpio, 249502a0106SMark Brown "Watchdog update"); 250502a0106SMark Brown if (ret < 0) { 251502a0106SMark Brown dev_err(wm831x->dev, 252502a0106SMark Brown "Failed to request update GPIO: %d\n", 253502a0106SMark Brown ret); 2547b9bb6d8SMark Brown goto err; 255502a0106SMark Brown } 256502a0106SMark Brown 257502a0106SMark Brown ret = gpio_direction_output(pdata->update_gpio, 0); 258502a0106SMark Brown if (ret != 0) { 259502a0106SMark Brown dev_err(wm831x->dev, 260502a0106SMark Brown "gpio_direction_output returned: %d\n", 261502a0106SMark Brown ret); 262502a0106SMark Brown goto err_gpio; 263502a0106SMark Brown } 264502a0106SMark Brown 26500411ee9SMark Brown driver_data->update_gpio = pdata->update_gpio; 266502a0106SMark Brown 267502a0106SMark Brown /* Make sure the watchdog takes hardware updates */ 268502a0106SMark Brown reg |= WM831X_WDOG_RST_SRC; 269502a0106SMark Brown } 270502a0106SMark Brown 271502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 272502a0106SMark Brown if (ret == 0) { 273502a0106SMark Brown ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); 274502a0106SMark Brown wm831x_reg_lock(wm831x); 275502a0106SMark Brown } else { 276502a0106SMark Brown dev_err(wm831x->dev, 277502a0106SMark Brown "Failed to unlock security key: %d\n", ret); 278502a0106SMark Brown goto err_gpio; 279502a0106SMark Brown } 280502a0106SMark Brown } 281502a0106SMark Brown 28200411ee9SMark Brown ret = watchdog_register_device(&driver_data->wdt); 283502a0106SMark Brown if (ret != 0) { 28400411ee9SMark Brown dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n", 28500411ee9SMark Brown ret); 286502a0106SMark Brown goto err_gpio; 287502a0106SMark Brown } 288502a0106SMark Brown 28900411ee9SMark Brown dev_set_drvdata(&pdev->dev, driver_data); 29000411ee9SMark Brown 291502a0106SMark Brown return 0; 292502a0106SMark Brown 293502a0106SMark Brown err_gpio: 29400411ee9SMark Brown if (driver_data->update_gpio) 29500411ee9SMark Brown gpio_free(driver_data->update_gpio); 296502a0106SMark Brown err: 297502a0106SMark Brown return ret; 298502a0106SMark Brown } 299502a0106SMark Brown 300502a0106SMark Brown static int __devexit wm831x_wdt_remove(struct platform_device *pdev) 301502a0106SMark Brown { 30200411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = dev_get_drvdata(&pdev->dev); 303502a0106SMark Brown 30400411ee9SMark Brown watchdog_unregister_device(&driver_data->wdt); 30500411ee9SMark Brown 30600411ee9SMark Brown if (driver_data->update_gpio) 30700411ee9SMark Brown gpio_free(driver_data->update_gpio); 308502a0106SMark Brown 309502a0106SMark Brown return 0; 310502a0106SMark Brown } 311502a0106SMark Brown 312502a0106SMark Brown static struct platform_driver wm831x_wdt_driver = { 313502a0106SMark Brown .probe = wm831x_wdt_probe, 314502a0106SMark Brown .remove = __devexit_p(wm831x_wdt_remove), 315502a0106SMark Brown .driver = { 316502a0106SMark Brown .name = "wm831x-watchdog", 317502a0106SMark Brown }, 318502a0106SMark Brown }; 319502a0106SMark Brown 320216f3ad9SMark Brown module_platform_driver(wm831x_wdt_driver); 321502a0106SMark Brown 322502a0106SMark Brown MODULE_AUTHOR("Mark Brown"); 323502a0106SMark Brown MODULE_DESCRIPTION("WM831x Watchdog"); 324502a0106SMark Brown MODULE_LICENSE("GPL"); 325502a0106SMark Brown MODULE_ALIAS("platform:wm831x-watchdog"); 326