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> 15*00411ee9SMark 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 25502a0106SMark Brown static int nowayout = WATCHDOG_NOWAYOUT; 26502a0106SMark Brown module_param(nowayout, int, 0); 27502a0106SMark Brown MODULE_PARM_DESC(nowayout, 28502a0106SMark Brown "Watchdog cannot be stopped once started (default=" 29502a0106SMark Brown __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 30502a0106SMark Brown 31*00411ee9SMark Brown struct wm831x_wdt_drvdata { 32*00411ee9SMark Brown struct watchdog_device wdt; 33*00411ee9SMark Brown struct wm831x *wm831x; 34*00411ee9SMark Brown struct mutex lock; 35*00411ee9SMark Brown int update_gpio; 36*00411ee9SMark Brown int update_state; 37*00411ee9SMark 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 { 42*00411ee9SMark 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 54*00411ee9SMark Brown static int wm831x_wdt_start(struct watchdog_device *wdt_dev) 55502a0106SMark Brown { 56*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 57*00411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 58502a0106SMark Brown int ret; 59502a0106SMark Brown 60*00411ee9SMark 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 72*00411ee9SMark Brown mutex_unlock(&driver_data->lock); 73502a0106SMark Brown 74502a0106SMark Brown return ret; 75502a0106SMark Brown } 76502a0106SMark Brown 77*00411ee9SMark Brown static int wm831x_wdt_stop(struct watchdog_device *wdt_dev) 78502a0106SMark Brown { 79*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 80*00411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 81502a0106SMark Brown int ret; 82502a0106SMark Brown 83*00411ee9SMark 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 95*00411ee9SMark Brown mutex_unlock(&driver_data->lock); 96502a0106SMark Brown 97502a0106SMark Brown return ret; 98502a0106SMark Brown } 99502a0106SMark Brown 100*00411ee9SMark Brown static int wm831x_wdt_ping(struct watchdog_device *wdt_dev) 101502a0106SMark Brown { 102*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 103*00411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 104502a0106SMark Brown int ret; 105502a0106SMark Brown u16 reg; 106502a0106SMark Brown 107*00411ee9SMark Brown mutex_lock(&driver_data->lock); 108502a0106SMark Brown 109*00411ee9SMark Brown if (driver_data->update_gpio) { 110*00411ee9SMark Brown gpio_set_value_cansleep(driver_data->update_gpio, 111*00411ee9SMark Brown driver_data->update_state); 112*00411ee9SMark 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: 137*00411ee9SMark Brown mutex_unlock(&driver_data->lock); 138502a0106SMark Brown 139502a0106SMark Brown return ret; 140502a0106SMark Brown } 141502a0106SMark Brown 142*00411ee9SMark Brown static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev, 143*00411ee9SMark Brown unsigned int timeout) 144502a0106SMark Brown { 145*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); 146*00411ee9SMark Brown struct wm831x *wm831x = driver_data->wm831x; 147*00411ee9SMark Brown int ret, i; 148502a0106SMark Brown 149*00411ee9SMark Brown for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) 150*00411ee9SMark Brown if (wm831x_wdt_cfgs[i].time == timeout) 151*00411ee9SMark Brown break; 152*00411ee9SMark Brown if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) 153*00411ee9SMark Brown ret = -EINVAL; 154502a0106SMark Brown 155*00411ee9SMark Brown ret = wm831x_reg_unlock(wm831x); 156*00411ee9SMark Brown if (ret == 0) { 157*00411ee9SMark Brown ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, 158*00411ee9SMark Brown WM831X_WDOG_TO_MASK, 159*00411ee9SMark Brown wm831x_wdt_cfgs[i].val); 160*00411ee9SMark Brown wm831x_reg_lock(wm831x); 161*00411ee9SMark Brown } else { 162*00411ee9SMark Brown dev_err(wm831x->dev, "Failed to unlock security key: %d\n", 163*00411ee9SMark Brown ret); 164*00411ee9SMark Brown } 165502a0106SMark Brown 166502a0106SMark Brown return ret; 167502a0106SMark Brown } 168502a0106SMark Brown 169*00411ee9SMark 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 174*00411ee9SMark Brown static const struct watchdog_ops wm831x_wdt_ops = { 175502a0106SMark Brown .owner = THIS_MODULE, 176*00411ee9SMark Brown .start = wm831x_wdt_start, 177*00411ee9SMark Brown .stop = wm831x_wdt_stop, 178*00411ee9SMark Brown .ping = wm831x_wdt_ping, 179*00411ee9SMark 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 { 184*00411ee9SMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 185502a0106SMark Brown struct wm831x_pdata *chip_pdata; 186502a0106SMark Brown struct wm831x_watchdog_pdata *pdata; 187*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data; 188*00411ee9SMark Brown struct watchdog_device *wm831x_wdt; 189*00411ee9SMark 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 202*00411ee9SMark Brown driver_data = kzalloc(sizeof(*driver_data), GFP_KERNEL); 203*00411ee9SMark Brown if (!driver_data) { 204*00411ee9SMark Brown dev_err(wm831x->dev, "Unable to alloacate watchdog device\n"); 205*00411ee9SMark Brown ret = -ENOMEM; 206*00411ee9SMark Brown goto err; 207*00411ee9SMark Brown } 208*00411ee9SMark Brown 209*00411ee9SMark Brown mutex_init(&driver_data->lock); 210*00411ee9SMark Brown driver_data->wm831x = wm831x; 211*00411ee9SMark Brown 212*00411ee9SMark Brown wm831x_wdt = &driver_data->wdt; 213*00411ee9SMark Brown 214*00411ee9SMark Brown wm831x_wdt->info = &wm831x_wdt_info; 215*00411ee9SMark Brown wm831x_wdt->ops = &wm831x_wdt_ops; 216*00411ee9SMark Brown watchdog_set_drvdata(wm831x_wdt, driver_data); 217*00411ee9SMark Brown 218*00411ee9SMark Brown if (nowayout) 219*00411ee9SMark Brown wm831x_wdt->status |= WDOG_NO_WAY_OUT; 220*00411ee9SMark Brown 221*00411ee9SMark Brown reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); 222*00411ee9SMark Brown reg &= WM831X_WDOG_TO_MASK; 223*00411ee9SMark Brown for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) 224*00411ee9SMark Brown if (wm831x_wdt_cfgs[i].val == reg) 225*00411ee9SMark Brown break; 226*00411ee9SMark Brown if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) 227*00411ee9SMark Brown dev_warn(wm831x->dev, 228*00411ee9SMark Brown "Unknown watchdog timeout: %x\n", reg); 229*00411ee9SMark Brown else 230*00411ee9SMark Brown wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time; 231*00411ee9SMark Brown 232502a0106SMark Brown /* Apply any configuration */ 233502a0106SMark Brown if (pdev->dev.parent->platform_data) { 234502a0106SMark Brown chip_pdata = pdev->dev.parent->platform_data; 235502a0106SMark Brown pdata = chip_pdata->watchdog; 236502a0106SMark Brown } else { 237502a0106SMark Brown pdata = NULL; 238502a0106SMark Brown } 239502a0106SMark Brown 240502a0106SMark Brown if (pdata) { 241502a0106SMark Brown reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK | 242502a0106SMark Brown WM831X_WDOG_RST_SRC); 243502a0106SMark Brown 244502a0106SMark Brown reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT; 245502a0106SMark Brown reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT; 246502a0106SMark Brown reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT; 247502a0106SMark Brown 248502a0106SMark Brown if (pdata->update_gpio) { 249502a0106SMark Brown ret = gpio_request(pdata->update_gpio, 250502a0106SMark Brown "Watchdog update"); 251502a0106SMark Brown if (ret < 0) { 252502a0106SMark Brown dev_err(wm831x->dev, 253502a0106SMark Brown "Failed to request update GPIO: %d\n", 254502a0106SMark Brown ret); 255*00411ee9SMark Brown goto err_alloc; 256502a0106SMark Brown } 257502a0106SMark Brown 258502a0106SMark Brown ret = gpio_direction_output(pdata->update_gpio, 0); 259502a0106SMark Brown if (ret != 0) { 260502a0106SMark Brown dev_err(wm831x->dev, 261502a0106SMark Brown "gpio_direction_output returned: %d\n", 262502a0106SMark Brown ret); 263502a0106SMark Brown goto err_gpio; 264502a0106SMark Brown } 265502a0106SMark Brown 266*00411ee9SMark Brown driver_data->update_gpio = pdata->update_gpio; 267502a0106SMark Brown 268502a0106SMark Brown /* Make sure the watchdog takes hardware updates */ 269502a0106SMark Brown reg |= WM831X_WDOG_RST_SRC; 270502a0106SMark Brown } 271502a0106SMark Brown 272502a0106SMark Brown ret = wm831x_reg_unlock(wm831x); 273502a0106SMark Brown if (ret == 0) { 274502a0106SMark Brown ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); 275502a0106SMark Brown wm831x_reg_lock(wm831x); 276502a0106SMark Brown } else { 277502a0106SMark Brown dev_err(wm831x->dev, 278502a0106SMark Brown "Failed to unlock security key: %d\n", ret); 279502a0106SMark Brown goto err_gpio; 280502a0106SMark Brown } 281502a0106SMark Brown } 282502a0106SMark Brown 283*00411ee9SMark Brown ret = watchdog_register_device(&driver_data->wdt); 284502a0106SMark Brown if (ret != 0) { 285*00411ee9SMark Brown dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n", 286*00411ee9SMark Brown ret); 287502a0106SMark Brown goto err_gpio; 288502a0106SMark Brown } 289502a0106SMark Brown 290*00411ee9SMark Brown dev_set_drvdata(&pdev->dev, driver_data); 291*00411ee9SMark Brown 292502a0106SMark Brown return 0; 293502a0106SMark Brown 294502a0106SMark Brown err_gpio: 295*00411ee9SMark Brown if (driver_data->update_gpio) 296*00411ee9SMark Brown gpio_free(driver_data->update_gpio); 297*00411ee9SMark Brown err_alloc: 298*00411ee9SMark Brown kfree(driver_data); 299502a0106SMark Brown err: 300502a0106SMark Brown return ret; 301502a0106SMark Brown } 302502a0106SMark Brown 303502a0106SMark Brown static int __devexit wm831x_wdt_remove(struct platform_device *pdev) 304502a0106SMark Brown { 305*00411ee9SMark Brown struct wm831x_wdt_drvdata *driver_data = dev_get_drvdata(&pdev->dev); 306502a0106SMark Brown 307*00411ee9SMark Brown watchdog_unregister_device(&driver_data->wdt); 308*00411ee9SMark Brown 309*00411ee9SMark Brown if (driver_data->update_gpio) 310*00411ee9SMark Brown gpio_free(driver_data->update_gpio); 311502a0106SMark Brown 312502a0106SMark Brown return 0; 313502a0106SMark Brown } 314502a0106SMark Brown 315502a0106SMark Brown static struct platform_driver wm831x_wdt_driver = { 316502a0106SMark Brown .probe = wm831x_wdt_probe, 317502a0106SMark Brown .remove = __devexit_p(wm831x_wdt_remove), 318502a0106SMark Brown .driver = { 319502a0106SMark Brown .name = "wm831x-watchdog", 320502a0106SMark Brown }, 321502a0106SMark Brown }; 322502a0106SMark Brown 323502a0106SMark Brown static int __init wm831x_wdt_init(void) 324502a0106SMark Brown { 325502a0106SMark Brown return platform_driver_register(&wm831x_wdt_driver); 326502a0106SMark Brown } 327502a0106SMark Brown module_init(wm831x_wdt_init); 328502a0106SMark Brown 329502a0106SMark Brown static void __exit wm831x_wdt_exit(void) 330502a0106SMark Brown { 331502a0106SMark Brown platform_driver_unregister(&wm831x_wdt_driver); 332502a0106SMark Brown } 333502a0106SMark Brown module_exit(wm831x_wdt_exit); 334502a0106SMark Brown 335502a0106SMark Brown MODULE_AUTHOR("Mark Brown"); 336502a0106SMark Brown MODULE_DESCRIPTION("WM831x Watchdog"); 337502a0106SMark Brown MODULE_LICENSE("GPL"); 338502a0106SMark Brown MODULE_ALIAS("platform:wm831x-watchdog"); 339