blob: 2fa17e746ff6f43dfff2042d5306934c4eab4cf8 [file] [log] [blame]
Mark Brown502a0102009-07-27 14:46:12 +01001/*
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 Brown00411ee2011-10-04 16:43:40 +010015#include <linux/slab.h>
Mark Brown502a0102009-07-27 14:46:12 +010016#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 Sebroeck86a1e182012-03-05 16:51:11 +010025static bool nowayout = WATCHDOG_NOWAYOUT;
26module_param(nowayout, bool, 0);
Mark Brown502a0102009-07-27 14:46:12 +010027MODULE_PARM_DESC(nowayout,
28 "Watchdog cannot be stopped once started (default="
29 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
30
Mark Brown00411ee2011-10-04 16:43:40 +010031struct 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 Brown502a0102009-07-27 14:46:12 +010038
39/* We can't use the sub-second values here but they're included
40 * for completeness. */
41static struct {
Mark Brown00411ee2011-10-04 16:43:40 +010042 unsigned int time; /* Seconds */
43 u16 val; /* WDOG_TO value */
Mark Brown502a0102009-07-27 14:46:12 +010044} 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 Brown00411ee2011-10-04 16:43:40 +010054static int wm831x_wdt_start(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +010055{
Mark Brown00411ee2011-10-04 16:43:40 +010056 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
57 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +010058 int ret;
59
Mark Brown00411ee2011-10-04 16:43:40 +010060 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010061
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 Brown00411ee2011-10-04 16:43:40 +010072 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010073
74 return ret;
75}
76
Mark Brown00411ee2011-10-04 16:43:40 +010077static int wm831x_wdt_stop(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +010078{
Mark Brown00411ee2011-10-04 16:43:40 +010079 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
80 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +010081 int ret;
82
Mark Brown00411ee2011-10-04 16:43:40 +010083 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010084
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 Brown00411ee2011-10-04 16:43:40 +010095 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010096
97 return ret;
98}
99
Mark Brown00411ee2011-10-04 16:43:40 +0100100static int wm831x_wdt_ping(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +0100101{
Mark Brown00411ee2011-10-04 16:43:40 +0100102 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
103 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +0100104 int ret;
105 u16 reg;
106
Mark Brown00411ee2011-10-04 16:43:40 +0100107 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +0100108
Mark Brown00411ee2011-10-04 16:43:40 +0100109 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 Brown502a0102009-07-27 14:46:12 +0100113 ret = 0;
114 goto out;
115 }
116
Mark Brown502a0102009-07-27 14:46:12 +0100117 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
136out:
Mark Brown00411ee2011-10-04 16:43:40 +0100137 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +0100138
139 return ret;
140}
141
Mark Brown00411ee2011-10-04 16:43:40 +0100142static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev,
143 unsigned int timeout)
Mark Brown502a0102009-07-27 14:46:12 +0100144{
Mark Brown00411ee2011-10-04 16:43:40 +0100145 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
146 struct wm831x *wm831x = driver_data->wm831x;
147 int ret, i;
Mark Brown502a0102009-07-27 14:46:12 +0100148
Mark Brown00411ee2011-10-04 16:43:40 +0100149 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 Brownf9849102011-11-16 11:52:12 +0000153 return -EINVAL;
Mark Brown502a0102009-07-27 14:46:12 +0100154
Mark Brown00411ee2011-10-04 16:43:40 +0100155 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 Brown502a0102009-07-27 14:46:12 +0100164 }
165
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100166 wdt_dev->timeout = timeout;
167
Mark Brown00411ee2011-10-04 16:43:40 +0100168 return ret;
Mark Brown502a0102009-07-27 14:46:12 +0100169}
170
Mark Brown00411ee2011-10-04 16:43:40 +0100171static const struct watchdog_info wm831x_wdt_info = {
Mark Brown502a0102009-07-27 14:46:12 +0100172 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
173 .identity = "WM831x Watchdog",
174};
175
Mark Brown00411ee2011-10-04 16:43:40 +0100176static const struct watchdog_ops wm831x_wdt_ops = {
Mark Brown502a0102009-07-27 14:46:12 +0100177 .owner = THIS_MODULE,
Mark Brown00411ee2011-10-04 16:43:40 +0100178 .start = wm831x_wdt_start,
179 .stop = wm831x_wdt_stop,
180 .ping = wm831x_wdt_ping,
181 .set_timeout = wm831x_wdt_set_timeout,
Mark Brown502a0102009-07-27 14:46:12 +0100182};
183
Bill Pemberton2d991a12012-11-19 13:21:41 -0500184static int wm831x_wdt_probe(struct platform_device *pdev)
Mark Brown502a0102009-07-27 14:46:12 +0100185{
Mark Brown00411ee2011-10-04 16:43:40 +0100186 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900187 struct wm831x_pdata *chip_pdata = dev_get_platdata(pdev->dev.parent);
Mark Brown502a0102009-07-27 14:46:12 +0100188 struct wm831x_watchdog_pdata *pdata;
Mark Brown00411ee2011-10-04 16:43:40 +0100189 struct wm831x_wdt_drvdata *driver_data;
190 struct watchdog_device *wm831x_wdt;
191 int reg, ret, i;
Mark Brown502a0102009-07-27 14:46:12 +0100192
193 ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
194 if (ret < 0) {
195 dev_err(wm831x->dev, "Failed to read watchdog status: %d\n",
196 ret);
197 goto err;
198 }
199 reg = ret;
200
201 if (reg & WM831X_WDOG_DEBUG)
202 dev_warn(wm831x->dev, "Watchdog is paused\n");
203
Mark Brown7b9bb6d2011-12-15 02:23:19 +0800204 driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
205 GFP_KERNEL);
Mark Brown00411ee2011-10-04 16:43:40 +0100206 if (!driver_data) {
Mark Brown00411ee2011-10-04 16:43:40 +0100207 ret = -ENOMEM;
208 goto err;
209 }
210
211 mutex_init(&driver_data->lock);
212 driver_data->wm831x = wm831x;
213
214 wm831x_wdt = &driver_data->wdt;
215
216 wm831x_wdt->info = &wm831x_wdt_info;
217 wm831x_wdt->ops = &wm831x_wdt_ops;
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100218 watchdog_set_nowayout(wm831x_wdt, nowayout);
Mark Brown00411ee2011-10-04 16:43:40 +0100219 watchdog_set_drvdata(wm831x_wdt, driver_data);
220
Mark Brown00411ee2011-10-04 16:43:40 +0100221 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
222 reg &= WM831X_WDOG_TO_MASK;
223 for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
224 if (wm831x_wdt_cfgs[i].val == reg)
225 break;
226 if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
227 dev_warn(wm831x->dev,
228 "Unknown watchdog timeout: %x\n", reg);
229 else
230 wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time;
231
Mark Brown502a0102009-07-27 14:46:12 +0100232 /* Apply any configuration */
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900233 if (chip_pdata)
Mark Brown502a0102009-07-27 14:46:12 +0100234 pdata = chip_pdata->watchdog;
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900235 else
Mark Brown502a0102009-07-27 14:46:12 +0100236 pdata = NULL;
Mark Brown502a0102009-07-27 14:46:12 +0100237
238 if (pdata) {
239 reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
240 WM831X_WDOG_RST_SRC);
241
242 reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT;
243 reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT;
244 reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT;
245
246 if (pdata->update_gpio) {
Jingoo Han7a5da032013-04-29 18:31:20 +0900247 ret = devm_gpio_request_one(&pdev->dev,
248 pdata->update_gpio,
249 GPIOF_OUT_INIT_LOW,
250 "Watchdog update");
Mark Brown502a0102009-07-27 14:46:12 +0100251 if (ret < 0) {
252 dev_err(wm831x->dev,
253 "Failed to request update GPIO: %d\n",
254 ret);
Mark Brown7b9bb6d2011-12-15 02:23:19 +0800255 goto err;
Mark Brown502a0102009-07-27 14:46:12 +0100256 }
257
Mark Brown00411ee2011-10-04 16:43:40 +0100258 driver_data->update_gpio = pdata->update_gpio;
Mark Brown502a0102009-07-27 14:46:12 +0100259
260 /* Make sure the watchdog takes hardware updates */
261 reg |= WM831X_WDOG_RST_SRC;
262 }
263
264 ret = wm831x_reg_unlock(wm831x);
265 if (ret == 0) {
266 ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
267 wm831x_reg_lock(wm831x);
268 } else {
269 dev_err(wm831x->dev,
270 "Failed to unlock security key: %d\n", ret);
Jingoo Han7a5da032013-04-29 18:31:20 +0900271 goto err;
Mark Brown502a0102009-07-27 14:46:12 +0100272 }
273 }
274
Mark Brown00411ee2011-10-04 16:43:40 +0100275 ret = watchdog_register_device(&driver_data->wdt);
Mark Brown502a0102009-07-27 14:46:12 +0100276 if (ret != 0) {
Mark Brown00411ee2011-10-04 16:43:40 +0100277 dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n",
278 ret);
Jingoo Han7a5da032013-04-29 18:31:20 +0900279 goto err;
Mark Brown502a0102009-07-27 14:46:12 +0100280 }
281
Jingoo Han1ae995d2013-05-23 19:44:53 +0900282 platform_set_drvdata(pdev, driver_data);
Mark Brown00411ee2011-10-04 16:43:40 +0100283
Mark Brown502a0102009-07-27 14:46:12 +0100284 return 0;
285
Mark Brown502a0102009-07-27 14:46:12 +0100286err:
287 return ret;
288}
289
Bill Pemberton4b12b892012-11-19 13:26:24 -0500290static int wm831x_wdt_remove(struct platform_device *pdev)
Mark Brown502a0102009-07-27 14:46:12 +0100291{
Jingoo Han1ae995d2013-05-23 19:44:53 +0900292 struct wm831x_wdt_drvdata *driver_data = platform_get_drvdata(pdev);
Mark Brown502a0102009-07-27 14:46:12 +0100293
Mark Brown00411ee2011-10-04 16:43:40 +0100294 watchdog_unregister_device(&driver_data->wdt);
295
Mark Brown502a0102009-07-27 14:46:12 +0100296 return 0;
297}
298
299static struct platform_driver wm831x_wdt_driver = {
300 .probe = wm831x_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500301 .remove = wm831x_wdt_remove,
Mark Brown502a0102009-07-27 14:46:12 +0100302 .driver = {
303 .name = "wm831x-watchdog",
304 },
305};
306
Mark Brown216f3ad2011-11-23 15:22:36 +0000307module_platform_driver(wm831x_wdt_driver);
Mark Brown502a0102009-07-27 14:46:12 +0100308
309MODULE_AUTHOR("Mark Brown");
310MODULE_DESCRIPTION("WM831x Watchdog");
311MODULE_LICENSE("GPL");
312MODULE_ALIAS("platform:wm831x-watchdog");