2 * Copyright (C) Nokia Corporation
4 * Written by Timo Kokkonen <timo.t.kokkonen at nokia.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
25 #include <linux/watchdog.h>
26 #include <linux/platform_device.h>
27 #include <linux/miscdevice.h>
28 #include <linux/uaccess.h>
29 #include <linux/i2c/twl.h>
31 #define TWL4030_WATCHDOG_CFG_REG_OFFS 0x3
33 #define TWL4030_WDT_STATE_OPEN 0x1
34 #define TWL4030_WDT_STATE_ACTIVE 0x8
36 static struct platform_device
*twl4030_wdt_dev
;
39 struct miscdevice miscdev
;
44 static int nowayout
= WATCHDOG_NOWAYOUT
;
45 module_param(nowayout
, int, 0);
46 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
47 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
49 static int twl4030_wdt_write(unsigned char val
)
51 return twl_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER
, val
,
52 TWL4030_WATCHDOG_CFG_REG_OFFS
);
55 static int twl4030_wdt_enable(struct twl4030_wdt
*wdt
)
57 return twl4030_wdt_write(wdt
->timer_margin
+ 1);
60 static int twl4030_wdt_disable(struct twl4030_wdt
*wdt
)
62 return twl4030_wdt_write(0);
65 static int twl4030_wdt_set_timeout(struct twl4030_wdt
*wdt
, int timeout
)
67 if (timeout
< 0 || timeout
> 30) {
68 dev_warn(wdt
->miscdev
.parent
,
69 "Timeout can only be in the range [0-30] seconds");
72 wdt
->timer_margin
= timeout
;
73 return twl4030_wdt_enable(wdt
);
76 static ssize_t
twl4030_wdt_write_fop(struct file
*file
,
77 const char __user
*data
, size_t len
, loff_t
*ppos
)
79 struct twl4030_wdt
*wdt
= file
->private_data
;
82 twl4030_wdt_enable(wdt
);
87 static long twl4030_wdt_ioctl(struct file
*file
,
88 unsigned int cmd
, unsigned long arg
)
90 void __user
*argp
= (void __user
*)arg
;
93 struct twl4030_wdt
*wdt
= file
->private_data
;
95 static const struct watchdog_info twl4030_wd_ident
= {
96 .identity
= "TWL4030 Watchdog",
97 .options
= WDIOF_SETTIMEOUT
,
98 .firmware_version
= 0,
102 case WDIOC_GETSUPPORT
:
103 return copy_to_user(argp
, &twl4030_wd_ident
,
104 sizeof(twl4030_wd_ident
)) ? -EFAULT
: 0;
106 case WDIOC_GETSTATUS
:
107 case WDIOC_GETBOOTSTATUS
:
108 return put_user(0, p
);
110 case WDIOC_KEEPALIVE
:
111 twl4030_wdt_enable(wdt
);
114 case WDIOC_SETTIMEOUT
:
115 if (get_user(new_margin
, p
))
117 if (twl4030_wdt_set_timeout(wdt
, new_margin
))
119 return put_user(wdt
->timer_margin
, p
);
121 case WDIOC_GETTIMEOUT
:
122 return put_user(wdt
->timer_margin
, p
);
131 static int twl4030_wdt_open(struct inode
*inode
, struct file
*file
)
133 struct twl4030_wdt
*wdt
= platform_get_drvdata(twl4030_wdt_dev
);
135 /* /dev/watchdog can only be opened once */
136 if (test_and_set_bit(0, &wdt
->state
))
139 wdt
->state
|= TWL4030_WDT_STATE_ACTIVE
;
140 file
->private_data
= (void *) wdt
;
142 twl4030_wdt_enable(wdt
);
143 return nonseekable_open(inode
, file
);
146 static int twl4030_wdt_release(struct inode
*inode
, struct file
*file
)
148 struct twl4030_wdt
*wdt
= file
->private_data
;
150 dev_alert(wdt
->miscdev
.parent
,
151 "Unexpected close, watchdog still running!\n");
152 twl4030_wdt_enable(wdt
);
154 if (twl4030_wdt_disable(wdt
))
156 wdt
->state
&= ~TWL4030_WDT_STATE_ACTIVE
;
159 clear_bit(0, &wdt
->state
);
163 static const struct file_operations twl4030_wdt_fops
= {
164 .owner
= THIS_MODULE
,
166 .open
= twl4030_wdt_open
,
167 .release
= twl4030_wdt_release
,
168 .unlocked_ioctl
= twl4030_wdt_ioctl
,
169 .write
= twl4030_wdt_write_fop
,
172 static int __devinit
twl4030_wdt_probe(struct platform_device
*pdev
)
175 struct twl4030_wdt
*wdt
;
177 wdt
= kzalloc(sizeof(struct twl4030_wdt
), GFP_KERNEL
);
182 wdt
->timer_margin
= 30;
183 wdt
->miscdev
.parent
= &pdev
->dev
;
184 wdt
->miscdev
.fops
= &twl4030_wdt_fops
;
185 wdt
->miscdev
.minor
= WATCHDOG_MINOR
;
186 wdt
->miscdev
.name
= "watchdog";
188 platform_set_drvdata(pdev
, wdt
);
190 twl4030_wdt_dev
= pdev
;
192 ret
= misc_register(&wdt
->miscdev
);
194 dev_err(wdt
->miscdev
.parent
,
195 "Failed to register misc device\n");
196 platform_set_drvdata(pdev
, NULL
);
198 twl4030_wdt_dev
= NULL
;
204 static int __devexit
twl4030_wdt_remove(struct platform_device
*pdev
)
206 struct twl4030_wdt
*wdt
= platform_get_drvdata(pdev
);
208 if (wdt
->state
& TWL4030_WDT_STATE_ACTIVE
)
209 if (twl4030_wdt_disable(wdt
))
212 wdt
->state
&= ~TWL4030_WDT_STATE_ACTIVE
;
213 misc_deregister(&wdt
->miscdev
);
215 platform_set_drvdata(pdev
, NULL
);
217 twl4030_wdt_dev
= NULL
;
223 static int twl4030_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
225 struct twl4030_wdt
*wdt
= platform_get_drvdata(pdev
);
226 if (wdt
->state
& TWL4030_WDT_STATE_ACTIVE
)
227 return twl4030_wdt_disable(wdt
);
232 static int twl4030_wdt_resume(struct platform_device
*pdev
)
234 struct twl4030_wdt
*wdt
= platform_get_drvdata(pdev
);
235 if (wdt
->state
& TWL4030_WDT_STATE_ACTIVE
)
236 return twl4030_wdt_enable(wdt
);
241 #define twl4030_wdt_suspend NULL
242 #define twl4030_wdt_resume NULL
245 static struct platform_driver twl4030_wdt_driver
= {
246 .probe
= twl4030_wdt_probe
,
247 .remove
= __devexit_p(twl4030_wdt_remove
),
248 .suspend
= twl4030_wdt_suspend
,
249 .resume
= twl4030_wdt_resume
,
251 .owner
= THIS_MODULE
,
252 .name
= "twl4030_wdt",
256 static int __devinit
twl4030_wdt_init(void)
258 return platform_driver_register(&twl4030_wdt_driver
);
260 module_init(twl4030_wdt_init
);
262 static void __devexit
twl4030_wdt_exit(void)
264 platform_driver_unregister(&twl4030_wdt_driver
);
266 module_exit(twl4030_wdt_exit
);
268 MODULE_AUTHOR("Nokia Corporation");
269 MODULE_LICENSE("GPL");
270 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
271 MODULE_ALIAS("platform:twl4030_wdt");