aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/wm8350_wdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/watchdog/wm8350_wdt.c')
-rw-r--r--drivers/watchdog/wm8350_wdt.c223
1 files changed, 42 insertions, 181 deletions
diff --git a/drivers/watchdog/wm8350_wdt.c b/drivers/watchdog/wm8350_wdt.c
index 5d7113c7e501..3c76693447fd 100644
--- a/drivers/watchdog/wm8350_wdt.c
+++ b/drivers/watchdog/wm8350_wdt.c
@@ -8,63 +8,65 @@
8 * as published by the Free Software Foundation 8 * as published by the Free Software Foundation
9 */ 9 */
10 10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
11#include <linux/module.h> 13#include <linux/module.h>
12#include <linux/moduleparam.h> 14#include <linux/moduleparam.h>
13#include <linux/types.h> 15#include <linux/types.h>
14#include <linux/kernel.h> 16#include <linux/kernel.h>
15#include <linux/fs.h>
16#include <linux/miscdevice.h>
17#include <linux/platform_device.h> 17#include <linux/platform_device.h>
18#include <linux/watchdog.h> 18#include <linux/watchdog.h>
19#include <linux/uaccess.h> 19#include <linux/uaccess.h>
20#include <linux/mfd/wm8350/core.h> 20#include <linux/mfd/wm8350/core.h>
21 21
22static int nowayout = WATCHDOG_NOWAYOUT; 22static bool nowayout = WATCHDOG_NOWAYOUT;
23module_param(nowayout, int, 0); 23module_param(nowayout, bool, 0);
24MODULE_PARM_DESC(nowayout, 24MODULE_PARM_DESC(nowayout,
25 "Watchdog cannot be stopped once started (default=" 25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27 27
28static unsigned long wm8350_wdt_users;
29static struct miscdevice wm8350_wdt_miscdev;
30static int wm8350_wdt_expect_close;
31static DEFINE_MUTEX(wdt_mutex); 28static DEFINE_MUTEX(wdt_mutex);
32 29
33static struct { 30static struct {
34 int time; /* Seconds */ 31 unsigned int time; /* Seconds */
35 u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */ 32 u16 val; /* To be set in WM8350_SYSTEM_CONTROL_2 */
36} wm8350_wdt_cfgs[] = { 33} wm8350_wdt_cfgs[] = {
37 { 1, 0x02 }, 34 { 1, 0x02 },
38 { 2, 0x04 }, 35 { 2, 0x04 },
39 { 4, 0x05 }, 36 { 4, 0x05 },
40}; 37};
41 38
42static struct wm8350 *get_wm8350(void) 39static int wm8350_wdt_set_timeout(struct watchdog_device *wdt_dev,
43{ 40 unsigned int timeout)
44 return dev_get_drvdata(wm8350_wdt_miscdev.parent);
45}
46
47static int wm8350_wdt_set_timeout(struct wm8350 *wm8350, u16 value)
48{ 41{
49 int ret; 42 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
43 int ret, i;
50 u16 reg; 44 u16 reg;
51 45
46 for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
47 if (wm8350_wdt_cfgs[i].time == timeout)
48 break;
49 if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
50 return -EINVAL;
51
52 mutex_lock(&wdt_mutex); 52 mutex_lock(&wdt_mutex);
53 wm8350_reg_unlock(wm8350); 53 wm8350_reg_unlock(wm8350);
54 54
55 reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2); 55 reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
56 reg &= ~WM8350_WDOG_TO_MASK; 56 reg &= ~WM8350_WDOG_TO_MASK;
57 reg |= value; 57 reg |= wm8350_wdt_cfgs[i].val;
58 ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg); 58 ret = wm8350_reg_write(wm8350, WM8350_SYSTEM_CONTROL_2, reg);
59 59
60 wm8350_reg_lock(wm8350); 60 wm8350_reg_lock(wm8350);
61 mutex_unlock(&wdt_mutex); 61 mutex_unlock(&wdt_mutex);
62 62
63 wdt_dev->timeout = timeout;
63 return ret; 64 return ret;
64} 65}
65 66
66static int wm8350_wdt_start(struct wm8350 *wm8350) 67static int wm8350_wdt_start(struct watchdog_device *wdt_dev)
67{ 68{
69 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
68 int ret; 70 int ret;
69 u16 reg; 71 u16 reg;
70 72
@@ -82,8 +84,9 @@ static int wm8350_wdt_start(struct wm8350 *wm8350)
82 return ret; 84 return ret;
83} 85}
84 86
85static int wm8350_wdt_stop(struct wm8350 *wm8350) 87static int wm8350_wdt_stop(struct watchdog_device *wdt_dev)
86{ 88{
89 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
87 int ret; 90 int ret;
88 u16 reg; 91 u16 reg;
89 92
@@ -100,8 +103,9 @@ static int wm8350_wdt_stop(struct wm8350 *wm8350)
100 return ret; 103 return ret;
101} 104}
102 105
103static int wm8350_wdt_kick(struct wm8350 *wm8350) 106static int wm8350_wdt_ping(struct watchdog_device *wdt_dev)
104{ 107{
108 struct wm8350 *wm8350 = watchdog_get_drvdata(wdt_dev);
105 int ret; 109 int ret;
106 u16 reg; 110 u16 reg;
107 111
@@ -115,168 +119,25 @@ static int wm8350_wdt_kick(struct wm8350 *wm8350)
115 return ret; 119 return ret;
116} 120}
117 121
118static int wm8350_wdt_open(struct inode *inode, struct file *file) 122static const struct watchdog_info wm8350_wdt_info = {
119{
120 struct wm8350 *wm8350 = get_wm8350();
121 int ret;
122
123 if (!wm8350)
124 return -ENODEV;
125
126 if (test_and_set_bit(0, &wm8350_wdt_users))
127 return -EBUSY;
128
129 ret = wm8350_wdt_start(wm8350);
130 if (ret != 0)
131 return ret;
132
133 return nonseekable_open(inode, file);
134}
135
136static int wm8350_wdt_release(struct inode *inode, struct file *file)
137{
138 struct wm8350 *wm8350 = get_wm8350();
139
140 if (wm8350_wdt_expect_close)
141 wm8350_wdt_stop(wm8350);
142 else {
143 dev_warn(wm8350->dev, "Watchdog device closed uncleanly\n");
144 wm8350_wdt_kick(wm8350);
145 }
146
147 clear_bit(0, &wm8350_wdt_users);
148
149 return 0;
150}
151
152static ssize_t wm8350_wdt_write(struct file *file,
153 const char __user *data, size_t count,
154 loff_t *ppos)
155{
156 struct wm8350 *wm8350 = get_wm8350();
157 size_t i;
158
159 if (count) {
160 wm8350_wdt_kick(wm8350);
161
162 if (!nowayout) {
163 /* In case it was set long ago */
164 wm8350_wdt_expect_close = 0;
165
166 /* scan to see whether or not we got the magic
167 character */
168 for (i = 0; i != count; i++) {
169 char c;
170 if (get_user(c, data + i))
171 return -EFAULT;
172 if (c == 'V')
173 wm8350_wdt_expect_close = 42;
174 }
175 }
176 }
177 return count;
178}
179
180static const struct watchdog_info ident = {
181 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 123 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
182 .identity = "WM8350 Watchdog", 124 .identity = "WM8350 Watchdog",
183}; 125};
184 126
185static long wm8350_wdt_ioctl(struct file *file, unsigned int cmd, 127static const struct watchdog_ops wm8350_wdt_ops = {
186 unsigned long arg)
187{
188 struct wm8350 *wm8350 = get_wm8350();
189 int ret = -ENOTTY, time, i;
190 void __user *argp = (void __user *)arg;
191 int __user *p = argp;
192 u16 reg;
193
194 switch (cmd) {
195 case WDIOC_GETSUPPORT:
196 ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
197 break;
198
199 case WDIOC_GETSTATUS:
200 case WDIOC_GETBOOTSTATUS:
201 ret = put_user(0, p);
202 break;
203
204 case WDIOC_SETOPTIONS:
205 {
206 int options;
207
208 if (get_user(options, p))
209 return -EFAULT;
210
211 ret = -EINVAL;
212
213 /* Setting both simultaneously means at least one must fail */
214 if (options == WDIOS_DISABLECARD)
215 ret = wm8350_wdt_stop(wm8350);
216
217 if (options == WDIOS_ENABLECARD)
218 ret = wm8350_wdt_start(wm8350);
219 break;
220 }
221
222 case WDIOC_KEEPALIVE:
223 ret = wm8350_wdt_kick(wm8350);
224 break;
225
226 case WDIOC_SETTIMEOUT:
227 ret = get_user(time, p);
228 if (ret)
229 break;
230
231 if (time == 0) {
232 if (nowayout)
233 ret = -EINVAL;
234 else
235 wm8350_wdt_stop(wm8350);
236 break;
237 }
238
239 for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
240 if (wm8350_wdt_cfgs[i].time == time)
241 break;
242 if (i == ARRAY_SIZE(wm8350_wdt_cfgs))
243 ret = -EINVAL;
244 else
245 ret = wm8350_wdt_set_timeout(wm8350,
246 wm8350_wdt_cfgs[i].val);
247 break;
248
249 case WDIOC_GETTIMEOUT:
250 reg = wm8350_reg_read(wm8350, WM8350_SYSTEM_CONTROL_2);
251 reg &= WM8350_WDOG_TO_MASK;
252 for (i = 0; i < ARRAY_SIZE(wm8350_wdt_cfgs); i++)
253 if (wm8350_wdt_cfgs[i].val == reg)
254 break;
255 if (i == ARRAY_SIZE(wm8350_wdt_cfgs)) {
256 dev_warn(wm8350->dev,
257 "Unknown watchdog configuration: %x\n", reg);
258 ret = -EINVAL;
259 } else
260 ret = put_user(wm8350_wdt_cfgs[i].time, p);
261
262 }
263
264 return ret;
265}
266
267static const struct file_operations wm8350_wdt_fops = {
268 .owner = THIS_MODULE, 128 .owner = THIS_MODULE,
269 .llseek = no_llseek, 129 .start = wm8350_wdt_start,
270 .write = wm8350_wdt_write, 130 .stop = wm8350_wdt_stop,
271 .unlocked_ioctl = wm8350_wdt_ioctl, 131 .ping = wm8350_wdt_ping,
272 .open = wm8350_wdt_open, 132 .set_timeout = wm8350_wdt_set_timeout,
273 .release = wm8350_wdt_release,
274}; 133};
275 134
276static struct miscdevice wm8350_wdt_miscdev = { 135static struct watchdog_device wm8350_wdt = {
277 .minor = WATCHDOG_MINOR, 136 .info = &wm8350_wdt_info,
278 .name = "watchdog", 137 .ops = &wm8350_wdt_ops,
279 .fops = &wm8350_wdt_fops, 138 .timeout = 4,
139 .min_timeout = 1,
140 .max_timeout = 4,
280}; 141};
281 142
282static int __devinit wm8350_wdt_probe(struct platform_device *pdev) 143static int __devinit wm8350_wdt_probe(struct platform_device *pdev)
@@ -288,18 +149,18 @@ static int __devinit wm8350_wdt_probe(struct platform_device *pdev)
288 return -ENODEV; 149 return -ENODEV;
289 } 150 }
290 151
291 /* Default to 4s timeout */ 152 watchdog_set_nowayout(&wm8350_wdt, nowayout);
292 wm8350_wdt_set_timeout(wm8350, 0x05); 153 watchdog_set_drvdata(&wm8350_wdt, wm8350);
293 154
294 wm8350_wdt_miscdev.parent = &pdev->dev; 155 /* Default to 4s timeout */
156 wm8350_wdt_set_timeout(&wm8350_wdt, 4);
295 157
296 return misc_register(&wm8350_wdt_miscdev); 158 return watchdog_register_device(&wm8350_wdt);
297} 159}
298 160
299static int __devexit wm8350_wdt_remove(struct platform_device *pdev) 161static int __devexit wm8350_wdt_remove(struct platform_device *pdev)
300{ 162{
301 misc_deregister(&wm8350_wdt_miscdev); 163 watchdog_unregister_device(&wm8350_wdt);
302
303 return 0; 164 return 0;
304} 165}
305 166