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 2586a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT; 2686a1e189SWim 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 1660197c1c4SWim Van Sebroeck wdt_dev->timeout = timeout; 1670197c1c4SWim Van Sebroeck 168502a0106SMark Brown return ret; 169502a0106SMark Brown } 170502a0106SMark Brown 17100411ee9SMark Brown static const struct watchdog_info wm831x_wdt_info = { 172502a0106SMark Brown .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 173502a0106SMark Brown .identity = "WM831x Watchdog", 174502a0106SMark Brown }; 175502a0106SMark Brown 17600411ee9SMark Brown static const struct watchdog_ops wm831x_wdt_ops = { 177502a0106SMark Brown .owner = THIS_MODULE, 17800411ee9SMark Brown .start = wm831x_wdt_start, 17900411ee9SMark Brown .stop = wm831x_wdt_stop, 18000411ee9SMark Brown .ping = wm831x_wdt_ping, 18100411ee9SMark Brown .set_timeout = wm831x_wdt_set_timeout, 182502a0106SMark Brown }; 183502a0106SMark Brown 1842d991a16SBill Pemberton static int wm831x_wdt_probe(struct platform_device *pdev) 185502a0106SMark Brown { 18600411ee9SMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 187502a0106SMark Brown struct wm831x_pdata *chip_pdata; 188502a0106SMark Brown struct wm831x_watchdog_pdata *pdata; 18900411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data; 19000411ee9SMark Brown struct watchdog_device *wm831x_wdt; 19100411ee9SMark Brown int reg, ret, i; 192502a0106SMark Brown 193502a0106SMark Brown ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 194502a0106SMark Brown if (ret < 0) { 195502a0106SMark Brown dev_err(wm831x->dev, "Failed to read watchdog status: %d\n", 196502a0106SMark Brown ret); 197502a0106SMark Brown goto err; 198502a0106SMark Brown } 199502a0106SMark Brown reg = ret; 200502a0106SMark Brown 201502a0106SMark Brown if (reg & WM831X_WDOG_DEBUG) 202502a0106SMark Brown dev_warn(wm831x->dev, "Watchdog is paused\n"); 203502a0106SMark Brown 2047b9bb6d8SMark Brown driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), 2057b9bb6d8SMark Brown GFP_KERNEL); 20600411ee9SMark Brown if (!driver_data) { 20700411ee9SMark Brown dev_err(wm831x->dev, "Unable to alloacate watchdog device\n"); 20800411ee9SMark Brown ret = -ENOMEM; 20900411ee9SMark Brown goto err; 21000411ee9SMark Brown } 21100411ee9SMark Brown 21200411ee9SMark Brown mutex_init(&driver_data->lock); 21300411ee9SMark Brown driver_data->wm831x = wm831x; 21400411ee9SMark Brown 21500411ee9SMark Brown wm831x_wdt = &driver_data->wdt; 21600411ee9SMark Brown 21700411ee9SMark Brown wm831x_wdt->info = &wm831x_wdt_info; 21800411ee9SMark Brown wm831x_wdt->ops = &wm831x_wdt_ops; 219ff0b3cd4SWim Van Sebroeck watchdog_set_nowayout(wm831x_wdt, nowayout); 22000411ee9SMark Brown watchdog_set_drvdata(wm831x_wdt, driver_data); 22100411ee9SMark Brown 22200411ee9SMark Brown reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 22300411ee9SMark Brown reg &= WM831X_WDOG_TO_MASK; 22400411ee9SMark Brown for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) 22500411ee9SMark Brown if (wm831x_wdt_cfgs[i].val == reg) 22600411ee9SMark Brown break; 22700411ee9SMark Brown if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) 22800411ee9SMark Brown dev_warn(wm831x->dev, 22900411ee9SMark Brown "Unknown watchdog timeout: %x\n", reg); 23000411ee9SMark Brown else 23100411ee9SMark Brown wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time; 23200411ee9SMark Brown 233502a0106SMark Brown /* Apply any configuration */ 234502a0106SMark Brown if (pdev->dev.parent->platform_data) { 235502a0106SMark Brown chip_pdata = pdev->dev.parent->platform_data; 236502a0106SMark Brown pdata = chip_pdata->watchdog; 237502a0106SMark Brown } else { 238502a0106SMark Brown pdata = NULL; 239502a0106SMark Brown } 240502a0106SMark Brown 241502a0106SMark Brown if (pdata) { 242502a0106SMark Brown reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK | 243502a0106SMark Brown WM831X_WDOG_RST_SRC); 244502a0106SMark Brown 245502a0106SMark Brown reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT; 246502a0106SMark Brown reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT; 247502a0106SMark Brown reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT; 248502a0106SMark Brown 249502a0106SMark Brown if (pdata->update_gpio) { 250abcf834bSMark Brown ret = gpio_request_one(pdata->update_gpio, 251abcf834bSMark Brown GPIOF_DIR_OUT | GPIOF_INIT_LOW, 252502a0106SMark Brown "Watchdog update"); 253502a0106SMark Brown if (ret < 0) { 254502a0106SMark Brown dev_err(wm831x->dev, 255502a0106SMark Brown "Failed to request update GPIO: %d\n", 256502a0106SMark Brown ret); 2577b9bb6d8SMark Brown goto err; 258502a0106SMark Brown } 259502a0106SMark Brown 26000411ee9SMark Brown driver_data->update_gpio = pdata->update_gpio; 261502a0106SMark Brown 262502a0106SMark Brown /* Make sure the watchdog takes hardware updates */ 263502a0106SMark Brown reg |= WM831X_WDOG_RST_SRC; 264502a0106SMark Brown } 265502a0106SMark Brown 266502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 267502a0106SMark Brown if (ret == 0) { 268502a0106SMark Brown ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); 269502a0106SMark Brown wm831x_reg_lock(wm831x); 270502a0106SMark Brown } else { 271502a0106SMark Brown dev_err(wm831x->dev, 272502a0106SMark Brown "Failed to unlock security key: %d\n", ret); 273502a0106SMark Brown goto err_gpio; 274502a0106SMark Brown } 275502a0106SMark Brown } 276502a0106SMark Brown 27700411ee9SMark Brown ret = watchdog_register_device(&driver_data->wdt); 278502a0106SMark Brown if (ret != 0) { 27900411ee9SMark Brown dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n", 28000411ee9SMark Brown ret); 281502a0106SMark Brown goto err_gpio; 282502a0106SMark Brown } 283502a0106SMark Brown 28400411ee9SMark Brown dev_set_drvdata(&pdev->dev, driver_data); 28500411ee9SMark Brown 286502a0106SMark Brown return 0; 287502a0106SMark Brown 288502a0106SMark Brown err_gpio: 28900411ee9SMark Brown if (driver_data->update_gpio) 29000411ee9SMark Brown gpio_free(driver_data->update_gpio); 291502a0106SMark Brown err: 292502a0106SMark Brown return ret; 293502a0106SMark Brown } 294502a0106SMark Brown 295*4b12b896SBill Pemberton static int wm831x_wdt_remove(struct platform_device *pdev) 296502a0106SMark Brown { 29700411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = dev_get_drvdata(&pdev->dev); 298502a0106SMark Brown 29900411ee9SMark Brown watchdog_unregister_device(&driver_data->wdt); 30000411ee9SMark Brown 30100411ee9SMark Brown if (driver_data->update_gpio) 30200411ee9SMark Brown gpio_free(driver_data->update_gpio); 303502a0106SMark Brown 304502a0106SMark Brown return 0; 305502a0106SMark Brown } 306502a0106SMark Brown 307502a0106SMark Brown static struct platform_driver wm831x_wdt_driver = { 308502a0106SMark Brown .probe = wm831x_wdt_probe, 30982268714SBill Pemberton .remove = wm831x_wdt_remove, 310502a0106SMark Brown .driver = { 311502a0106SMark Brown .name = "wm831x-watchdog", 312502a0106SMark Brown }, 313502a0106SMark Brown }; 314502a0106SMark Brown 315216f3ad9SMark Brown module_platform_driver(wm831x_wdt_driver); 316502a0106SMark Brown 317502a0106SMark Brown MODULE_AUTHOR("Mark Brown"); 318502a0106SMark Brown MODULE_DESCRIPTION("WM831x Watchdog"); 319502a0106SMark Brown MODULE_LICENSE("GPL"); 320502a0106SMark Brown MODULE_ALIAS("platform:wm831x-watchdog"); 321