]>
Commit | Line | Data |
---|---|---|
502a0106 MB |
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> | |
00411ee9 | 15 | #include <linux/slab.h> |
502a0106 MB |
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 | ||
25 | static int nowayout = WATCHDOG_NOWAYOUT; | |
26 | module_param(nowayout, int, 0); | |
27 | MODULE_PARM_DESC(nowayout, | |
28 | "Watchdog cannot be stopped once started (default=" | |
29 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
30 | ||
00411ee9 MB |
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 | }; | |
502a0106 MB |
38 | |
39 | /* We can't use the sub-second values here but they're included | |
40 | * for completeness. */ | |
41 | static struct { | |
00411ee9 MB |
42 | unsigned int time; /* Seconds */ |
43 | u16 val; /* WDOG_TO value */ | |
502a0106 MB |
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 | ||
00411ee9 | 54 | static int wm831x_wdt_start(struct watchdog_device *wdt_dev) |
502a0106 | 55 | { |
00411ee9 MB |
56 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
57 | struct wm831x *wm831x = driver_data->wm831x; | |
502a0106 MB |
58 | int ret; |
59 | ||
00411ee9 | 60 | mutex_lock(&driver_data->lock); |
502a0106 MB |
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 | ||
00411ee9 | 72 | mutex_unlock(&driver_data->lock); |
502a0106 MB |
73 | |
74 | return ret; | |
75 | } | |
76 | ||
00411ee9 | 77 | static int wm831x_wdt_stop(struct watchdog_device *wdt_dev) |
502a0106 | 78 | { |
00411ee9 MB |
79 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
80 | struct wm831x *wm831x = driver_data->wm831x; | |
502a0106 MB |
81 | int ret; |
82 | ||
00411ee9 | 83 | mutex_lock(&driver_data->lock); |
502a0106 MB |
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 | ||
00411ee9 | 95 | mutex_unlock(&driver_data->lock); |
502a0106 MB |
96 | |
97 | return ret; | |
98 | } | |
99 | ||
00411ee9 | 100 | static int wm831x_wdt_ping(struct watchdog_device *wdt_dev) |
502a0106 | 101 | { |
00411ee9 MB |
102 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
103 | struct wm831x *wm831x = driver_data->wm831x; | |
502a0106 MB |
104 | int ret; |
105 | u16 reg; | |
106 | ||
00411ee9 | 107 | mutex_lock(&driver_data->lock); |
502a0106 | 108 | |
00411ee9 MB |
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; | |
502a0106 MB |
113 | ret = 0; |
114 | goto out; | |
115 | } | |
116 | ||
502a0106 MB |
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: | |
00411ee9 | 137 | mutex_unlock(&driver_data->lock); |
502a0106 MB |
138 | |
139 | return ret; | |
140 | } | |
141 | ||
00411ee9 MB |
142 | static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev, |
143 | unsigned int timeout) | |
502a0106 | 144 | { |
00411ee9 MB |
145 | struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev); |
146 | struct wm831x *wm831x = driver_data->wm831x; | |
147 | int ret, i; | |
502a0106 | 148 | |
00411ee9 MB |
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)) | |
153 | ret = -EINVAL; | |
502a0106 | 154 | |
00411ee9 MB |
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); | |
502a0106 MB |
164 | } |
165 | ||
00411ee9 | 166 | return ret; |
502a0106 MB |
167 | } |
168 | ||
00411ee9 | 169 | static const struct watchdog_info wm831x_wdt_info = { |
502a0106 MB |
170 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
171 | .identity = "WM831x Watchdog", | |
172 | }; | |
173 | ||
00411ee9 | 174 | static const struct watchdog_ops wm831x_wdt_ops = { |
502a0106 | 175 | .owner = THIS_MODULE, |
00411ee9 MB |
176 | .start = wm831x_wdt_start, |
177 | .stop = wm831x_wdt_stop, | |
178 | .ping = wm831x_wdt_ping, | |
179 | .set_timeout = wm831x_wdt_set_timeout, | |
502a0106 MB |
180 | }; |
181 | ||
182 | static int __devinit wm831x_wdt_probe(struct platform_device *pdev) | |
183 | { | |
00411ee9 | 184 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
502a0106 MB |
185 | struct wm831x_pdata *chip_pdata; |
186 | struct wm831x_watchdog_pdata *pdata; | |
00411ee9 MB |
187 | struct wm831x_wdt_drvdata *driver_data; |
188 | struct watchdog_device *wm831x_wdt; | |
189 | int reg, ret, i; | |
502a0106 MB |
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 | ||
00411ee9 MB |
202 | driver_data = kzalloc(sizeof(*driver_data), GFP_KERNEL); |
203 | if (!driver_data) { | |
204 | dev_err(wm831x->dev, "Unable to alloacate watchdog device\n"); | |
205 | ret = -ENOMEM; | |
206 | goto err; | |
207 | } | |
208 | ||
209 | mutex_init(&driver_data->lock); | |
210 | driver_data->wm831x = wm831x; | |
211 | ||
212 | wm831x_wdt = &driver_data->wdt; | |
213 | ||
214 | wm831x_wdt->info = &wm831x_wdt_info; | |
215 | wm831x_wdt->ops = &wm831x_wdt_ops; | |
216 | watchdog_set_drvdata(wm831x_wdt, driver_data); | |
217 | ||
218 | if (nowayout) | |
219 | wm831x_wdt->status |= WDOG_NO_WAY_OUT; | |
220 | ||
221 | 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 | ||
502a0106 MB |
232 | /* Apply any configuration */ |
233 | if (pdev->dev.parent->platform_data) { | |
234 | chip_pdata = pdev->dev.parent->platform_data; | |
235 | pdata = chip_pdata->watchdog; | |
236 | } else { | |
237 | pdata = NULL; | |
238 | } | |
239 | ||
240 | if (pdata) { | |
241 | reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK | | |
242 | WM831X_WDOG_RST_SRC); | |
243 | ||
244 | reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT; | |
245 | reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT; | |
246 | reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT; | |
247 | ||
248 | if (pdata->update_gpio) { | |
249 | ret = gpio_request(pdata->update_gpio, | |
250 | "Watchdog update"); | |
251 | if (ret < 0) { | |
252 | dev_err(wm831x->dev, | |
253 | "Failed to request update GPIO: %d\n", | |
254 | ret); | |
00411ee9 | 255 | goto err_alloc; |
502a0106 MB |
256 | } |
257 | ||
258 | ret = gpio_direction_output(pdata->update_gpio, 0); | |
259 | if (ret != 0) { | |
260 | dev_err(wm831x->dev, | |
261 | "gpio_direction_output returned: %d\n", | |
262 | ret); | |
263 | goto err_gpio; | |
264 | } | |
265 | ||
00411ee9 | 266 | driver_data->update_gpio = pdata->update_gpio; |
502a0106 MB |
267 | |
268 | /* Make sure the watchdog takes hardware updates */ | |
269 | reg |= WM831X_WDOG_RST_SRC; | |
270 | } | |
271 | ||
272 | ret = wm831x_reg_unlock(wm831x); | |
273 | if (ret == 0) { | |
274 | ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg); | |
275 | wm831x_reg_lock(wm831x); | |
276 | } else { | |
277 | dev_err(wm831x->dev, | |
278 | "Failed to unlock security key: %d\n", ret); | |
279 | goto err_gpio; | |
280 | } | |
281 | } | |
282 | ||
00411ee9 | 283 | ret = watchdog_register_device(&driver_data->wdt); |
502a0106 | 284 | if (ret != 0) { |
00411ee9 MB |
285 | dev_err(wm831x->dev, "watchdog_register_device() failed: %d\n", |
286 | ret); | |
502a0106 MB |
287 | goto err_gpio; |
288 | } | |
289 | ||
00411ee9 MB |
290 | dev_set_drvdata(&pdev->dev, driver_data); |
291 | ||
502a0106 MB |
292 | return 0; |
293 | ||
294 | err_gpio: | |
00411ee9 MB |
295 | if (driver_data->update_gpio) |
296 | gpio_free(driver_data->update_gpio); | |
297 | err_alloc: | |
298 | kfree(driver_data); | |
502a0106 MB |
299 | err: |
300 | return ret; | |
301 | } | |
302 | ||
303 | static int __devexit wm831x_wdt_remove(struct platform_device *pdev) | |
304 | { | |
00411ee9 MB |
305 | struct wm831x_wdt_drvdata *driver_data = dev_get_drvdata(&pdev->dev); |
306 | ||
307 | watchdog_unregister_device(&driver_data->wdt); | |
502a0106 | 308 | |
00411ee9 MB |
309 | if (driver_data->update_gpio) |
310 | gpio_free(driver_data->update_gpio); | |
502a0106 MB |
311 | |
312 | return 0; | |
313 | } | |
314 | ||
315 | static struct platform_driver wm831x_wdt_driver = { | |
316 | .probe = wm831x_wdt_probe, | |
317 | .remove = __devexit_p(wm831x_wdt_remove), | |
318 | .driver = { | |
319 | .name = "wm831x-watchdog", | |
320 | }, | |
321 | }; | |
322 | ||
323 | static int __init wm831x_wdt_init(void) | |
324 | { | |
325 | return platform_driver_register(&wm831x_wdt_driver); | |
326 | } | |
327 | module_init(wm831x_wdt_init); | |
328 | ||
329 | static void __exit wm831x_wdt_exit(void) | |
330 | { | |
331 | platform_driver_unregister(&wm831x_wdt_driver); | |
332 | } | |
333 | module_exit(wm831x_wdt_exit); | |
334 | ||
335 | MODULE_AUTHOR("Mark Brown"); | |
336 | MODULE_DESCRIPTION("WM831x Watchdog"); | |
337 | MODULE_LICENSE("GPL"); | |
338 | MODULE_ALIAS("platform:wm831x-watchdog"); |