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