Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Watchdog driver for the wm831x PMICs |
| 3 | * |
| 4 | * Copyright (C) 2009 Wolfson Microelectronics |
| 5 | * |
| 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 |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/watchdog.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/gpio.h> |
| 20 | |
| 21 | #include <linux/mfd/wm831x/core.h> |
| 22 | #include <linux/mfd/wm831x/pdata.h> |
| 23 | #include <linux/mfd/wm831x/watchdog.h> |
| 24 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame^] | 25 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 26 | module_param(nowayout, bool, 0); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 27 | MODULE_PARM_DESC(nowayout, |
| 28 | "Watchdog cannot be stopped once started (default=" |
| 29 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 30 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 31 | struct wm831x_wdt_drvdata { |
| 32 | struct watchdog_device wdt; |
| 33 | struct wm831x *wm831x; |
| 34 | struct mutex lock; |
| 35 | int update_gpio; |
| 36 | int update_state; |
| 37 | }; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 38 | |
| 39 | /* We can't use the sub-second values here but they're included |
| 40 | * for completeness. */ |
| 41 | static struct { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 42 | unsigned int time; /* Seconds */ |
| 43 | u16 val; /* WDOG_TO value */ |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 44 | } wm831x_wdt_cfgs[] = { |
| 45 | { 1, 2 }, |
| 46 | { 2, 3 }, |
| 47 | { 4, 4 }, |
| 48 | { 8, 5 }, |
| 49 | { 16, 6 }, |
| 50 | { 32, 7 }, |
| 51 | { 33, 7 }, /* Actually 32.768s so include both, others round down */ |
| 52 | }; |
| 53 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 54 | static int wm831x_wdt_start(struct watchdog_device *wdt_dev) |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 55 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 56 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
| 57 | struct wm831x *wm831x = driver_data->wm831x; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 58 | int ret; |
| 59 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 60 | mutex_lock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 61 | |
| 62 | ret = wm831x_reg_unlock(wm831x); |
| 63 | if (ret == 0) { |
| 64 | ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, |
| 65 | WM831X_WDOG_ENA, WM831X_WDOG_ENA); |
| 66 | wm831x_reg_lock(wm831x); |
| 67 | } else { |
| 68 | dev_err(wm831x->dev, "Failed to unlock security key: %d\n", |
| 69 | ret); |
| 70 | } |
| 71 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 72 | mutex_unlock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 77 | static int wm831x_wdt_stop(struct watchdog_device *wdt_dev) |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 78 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 79 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
| 80 | struct wm831x *wm831x = driver_data->wm831x; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 81 | int ret; |
| 82 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 83 | mutex_lock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 84 | |
| 85 | ret = wm831x_reg_unlock(wm831x); |
| 86 | if (ret == 0) { |
| 87 | ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, |
| 88 | WM831X_WDOG_ENA, 0); |
| 89 | wm831x_reg_lock(wm831x); |
| 90 | } else { |
| 91 | dev_err(wm831x->dev, "Failed to unlock security key: %d\n", |
| 92 | ret); |
| 93 | } |
| 94 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 95 | mutex_unlock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 100 | static int wm831x_wdt_ping(struct watchdog_device *wdt_dev) |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 101 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 102 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
| 103 | struct wm831x *wm831x = driver_data->wm831x; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 104 | int ret; |
| 105 | u16 reg; |
| 106 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 107 | mutex_lock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 108 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 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; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 113 | ret = 0; |
| 114 | goto out; |
| 115 | } |
| 116 | |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 117 | reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); |
| 118 | |
| 119 | if (!(reg & WM831X_WDOG_RST_SRC)) { |
| 120 | dev_err(wm831x->dev, "Hardware watchdog update unsupported\n"); |
| 121 | ret = -EINVAL; |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | reg |= WM831X_WDOG_RESET; |
| 126 | |
| 127 | ret = wm831x_reg_unlock(wm831x); |
| 128 | if (ret == 0) { |
| 129 | ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); |
| 130 | wm831x_reg_lock(wm831x); |
| 131 | } else { |
| 132 | dev_err(wm831x->dev, "Failed to unlock security key: %d\n", |
| 133 | ret); |
| 134 | } |
| 135 | |
| 136 | out: |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 137 | mutex_unlock(&driver_data->lock); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 142 | static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev, |
| 143 | unsigned int timeout) |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 144 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 145 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
| 146 | struct wm831x *wm831x = driver_data->wm831x; |
| 147 | int ret, i; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 148 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 149 | for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) |
| 150 | if (wm831x_wdt_cfgs[i].time == timeout) |
| 151 | break; |
| 152 | if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) |
Mark Brown | f984910 | 2011-11-16 11:52:12 +0000 | [diff] [blame] | 153 | return -EINVAL; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 154 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 155 | ret = wm831x_reg_unlock(wm831x); |
| 156 | if (ret == 0) { |
| 157 | ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG, |
| 158 | WM831X_WDOG_TO_MASK, |
| 159 | wm831x_wdt_cfgs[i].val); |
| 160 | wm831x_reg_lock(wm831x); |
| 161 | } else { |
| 162 | dev_err(wm831x->dev, "Failed to unlock security key: %d\n", |
| 163 | ret); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 166 | return ret; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 169 | static const struct watchdog_info wm831x_wdt_info = { |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 170 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
| 171 | .identity = "WM831x Watchdog", |
| 172 | }; |
| 173 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 174 | static const struct watchdog_ops wm831x_wdt_ops = { |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 175 | .owner = THIS_MODULE, |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 176 | .start = wm831x_wdt_start, |
| 177 | .stop = wm831x_wdt_stop, |
| 178 | .ping = wm831x_wdt_ping, |
| 179 | .set_timeout = wm831x_wdt_set_timeout, |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static int __devinit wm831x_wdt_probe(struct platform_device *pdev) |
| 183 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 184 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 185 | struct wm831x_pdata *chip_pdata; |
| 186 | struct wm831x_watchdog_pdata *pdata; |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 187 | struct wm831x_wdt_drvdata *driver_data; |
| 188 | struct watchdog_device *wm831x_wdt; |
| 189 | int reg, ret, i; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 190 | |
| 191 | ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG); |
| 192 | if (ret < 0) { |
| 193 | dev_err(wm831x->dev, "Failed to read watchdog status: %d\n", |
| 194 | ret); |
| 195 | goto err; |
| 196 | } |
| 197 | reg = ret; |
| 198 | |
| 199 | if (reg & WM831X_WDOG_DEBUG) |
| 200 | dev_warn(wm831x->dev, "Watchdog is paused\n"); |
| 201 | |
Mark Brown | 7b9bb6d | 2011-12-15 02:23:19 +0800 | [diff] [blame] | 202 | driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), |
| 203 | GFP_KERNEL); |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 204 | if (!driver_data) { |
| 205 | dev_err(wm831x->dev, "Unable to alloacate watchdog device\n"); |
| 206 | ret = -ENOMEM; |
| 207 | goto err; |
| 208 | } |
| 209 | |
| 210 | mutex_init(&driver_data->lock); |
| 211 | driver_data->wm831x = wm831x; |
| 212 | |
| 213 | wm831x_wdt = &driver_data->wdt; |
| 214 | |
| 215 | wm831x_wdt->info = &wm831x_wdt_info; |
| 216 | wm831x_wdt->ops = &wm831x_wdt_ops; |
Wim Van Sebroeck | ff0b3cd | 2011-11-29 16:24:16 +0100 | [diff] [blame] | 217 | watchdog_set_nowayout(wm831x_wdt, nowayout); |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 218 | watchdog_set_drvdata(wm831x_wdt, driver_data); |
| 219 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 220 | reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG); |
| 221 | reg &= WM831X_WDOG_TO_MASK; |
| 222 | for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++) |
| 223 | if (wm831x_wdt_cfgs[i].val == reg) |
| 224 | break; |
| 225 | if (i == ARRAY_SIZE(wm831x_wdt_cfgs)) |
| 226 | dev_warn(wm831x->dev, |
| 227 | "Unknown watchdog timeout: %x\n", reg); |
| 228 | else |
| 229 | wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time; |
| 230 | |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 231 | /* Apply any configuration */ |
| 232 | if (pdev->dev.parent->platform_data) { |
| 233 | chip_pdata = pdev->dev.parent->platform_data; |
| 234 | pdata = chip_pdata->watchdog; |
| 235 | } else { |
| 236 | pdata = NULL; |
| 237 | } |
| 238 | |
| 239 | if (pdata) { |
| 240 | reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK | |
| 241 | WM831X_WDOG_RST_SRC); |
| 242 | |
| 243 | reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT; |
| 244 | reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT; |
| 245 | reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT; |
| 246 | |
| 247 | if (pdata->update_gpio) { |
| 248 | ret = gpio_request(pdata->update_gpio, |
| 249 | "Watchdog update"); |
| 250 | if (ret < 0) { |
| 251 | dev_err(wm831x->dev, |
| 252 | "Failed to request update GPIO: %d\n", |
| 253 | ret); |
Mark Brown | 7b9bb6d | 2011-12-15 02:23:19 +0800 | [diff] [blame] | 254 | goto err; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | ret = gpio_direction_output(pdata->update_gpio, 0); |
| 258 | if (ret != 0) { |
| 259 | dev_err(wm831x->dev, |
| 260 | "gpio_direction_output returned: %d\n", |
| 261 | ret); |
| 262 | goto err_gpio; |
| 263 | } |
| 264 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 265 | driver_data->update_gpio = pdata->update_gpio; |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 266 | |
| 267 | /* Make sure the watchdog takes hardware updates */ |
| 268 | reg |= WM831X_WDOG_RST_SRC; |
| 269 | } |
| 270 | |
| 271 | ret = wm831x_reg_unlock(wm831x); |
| 272 | if (ret == 0) { |
| 273 | ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); |
| 274 | wm831x_reg_lock(wm831x); |
| 275 | } else { |
| 276 | dev_err(wm831x->dev, |
| 277 | "Failed to unlock security key: %d\n", ret); |
| 278 | goto err_gpio; |
| 279 | } |
| 280 | } |
| 281 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 282 | ret = watchdog_register_device(&driver_data->wdt); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 283 | if (ret != 0) { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 284 | dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n", |
| 285 | ret); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 286 | goto err_gpio; |
| 287 | } |
| 288 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 289 | dev_set_drvdata(&pdev->dev, driver_data); |
| 290 | |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 291 | return 0; |
| 292 | |
| 293 | err_gpio: |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 294 | if (driver_data->update_gpio) |
| 295 | gpio_free(driver_data->update_gpio); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 296 | err: |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int __devexit wm831x_wdt_remove(struct platform_device *pdev) |
| 301 | { |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 302 | struct wm831x_wdt_drvdata *driver_data = dev_get_drvdata(&pdev->dev); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 303 | |
Mark Brown | 00411ee | 2011-10-04 16:43:40 +0100 | [diff] [blame] | 304 | watchdog_unregister_device(&driver_data->wdt); |
| 305 | |
| 306 | if (driver_data->update_gpio) |
| 307 | gpio_free(driver_data->update_gpio); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static struct platform_driver wm831x_wdt_driver = { |
| 313 | .probe = wm831x_wdt_probe, |
| 314 | .remove = __devexit_p(wm831x_wdt_remove), |
| 315 | .driver = { |
| 316 | .name = "wm831x-watchdog", |
| 317 | }, |
| 318 | }; |
| 319 | |
Mark Brown | 216f3ad | 2011-11-23 15:22:36 +0000 | [diff] [blame] | 320 | module_platform_driver(wm831x_wdt_driver); |
Mark Brown | 502a010 | 2009-07-27 14:46:12 +0100 | [diff] [blame] | 321 | |
| 322 | MODULE_AUTHOR("Mark Brown"); |
| 323 | MODULE_DESCRIPTION("WM831x Watchdog"); |
| 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_ALIAS("platform:wm831x-watchdog"); |