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