2 * drivers/cbus/retu-wdt.c
4 * Driver for Retu watchdog
6 * Copyright (C) 2004, 2005 Nokia Corporation
8 * Written by Amit Kucheria <amit.kucheria@nokia.com>
10 * This file is subject to the terms and conditions of the GNU General
11 * Public License. See the file "COPYING" in the main directory of this
12 * archive for more details.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
29 #include <linux/completion.h>
30 #include <linux/errno.h>
31 #include <linux/moduleparam.h>
32 #include <linux/platform_device.h>
37 /* Watchdog timeout in seconds */
38 #define RETU_WDT_MIN_TIMER 0
39 #define RETU_WDT_DEFAULT_TIMER 32
40 #define RETU_WDT_MAX_TIMER 63
42 static struct completion retu_wdt_completion;
43 static DECLARE_MUTEX(retu_wdt_mutex); /* Avoid simultaneous writes to watchdog register */
45 static unsigned int period_val = RETU_WDT_DEFAULT_TIMER; /* Current period of watchdog */
46 static int counter_param = RETU_WDT_MAX_TIMER;
48 static int retu_modify_counter(unsigned int new)
52 if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
55 down_interruptible(&retu_wdt_mutex);
58 retu_write_reg(RETU_REG_WATCHDOG, (u16)period_val);
64 static ssize_t retu_wdt_period_show(struct device *dev, struct device_attribute *attr,
67 /* Show current max counter */
68 return sprintf(buf, "%u\n", (u16)period_val);
71 static ssize_t retu_wdt_period_store(struct device *dev, struct device_attribute *attr,
72 const char *buf, size_t count)
74 unsigned int new_period;
77 if (sscanf(buf, "%u", &new_period) != 1) {
78 printk(KERN_ALERT "retu_wdt_period_store: Invalid input\n");
82 ret = retu_modify_counter(new_period);
86 return strnlen(buf, count);
89 static ssize_t retu_wdt_counter_show(struct device *dev, struct device_attribute *attr,
94 /* Show current value in watchdog counter */
95 counter = retu_read_reg(RETU_REG_WATCHDOG);
97 /* Only the 5 LSB are important */
98 return snprintf(buf, PAGE_SIZE, "%u\n", (counter & 0x3F));
101 static DEVICE_ATTR(period, S_IRUGO | S_IWUSR, retu_wdt_period_show, \
102 retu_wdt_period_store);
103 static DEVICE_ATTR(counter, S_IRUGO, retu_wdt_counter_show, NULL);
105 static int __devinit retu_wdt_probe(struct device *dev)
109 ret = device_create_file(dev, &dev_attr_period);
111 printk(KERN_ERR "retu_wdt_probe: Error creating sys device file: period\n");
115 ret = device_create_file(dev, &dev_attr_counter);
117 device_remove_file(dev, &dev_attr_period);
118 printk(KERN_ERR "retu_wdt_probe: Error creating sys device file: counter\n");
124 static int __devexit retu_wdt_remove(struct device *dev)
126 device_remove_file(dev, &dev_attr_period);
127 device_remove_file(dev, &dev_attr_counter);
131 static void retu_wdt_device_release(struct device *dev)
133 complete(&retu_wdt_completion);
136 static struct platform_device retu_wdt_device = {
137 .name = "retu-watchdog",
140 .release = retu_wdt_device_release,
144 static struct device_driver retu_wdt_driver = {
145 .name = "retu-watchdog",
146 .bus = &platform_bus_type,
147 .probe = retu_wdt_probe,
148 .remove = __devexit_p(retu_wdt_remove),
151 static int __init retu_wdt_init(void)
155 init_completion(&retu_wdt_completion);
157 ret = driver_register(&retu_wdt_driver);
161 ret = platform_device_register(&retu_wdt_device);
165 /* passed as module parameter? */
166 ret = retu_modify_counter(counter_param);
167 if (ret == -EINVAL) {
168 ret = retu_modify_counter(RETU_WDT_DEFAULT_TIMER);
170 "retu_wdt_init: Intializing to default value\n");
173 printk(KERN_INFO "Retu watchdog driver initialized\n");
177 driver_unregister(&retu_wdt_driver);
178 wait_for_completion(&retu_wdt_completion);
183 static void __exit retu_wdt_exit(void)
185 platform_device_unregister(&retu_wdt_device);
186 driver_unregister(&retu_wdt_driver);
188 wait_for_completion(&retu_wdt_completion);
191 module_init(retu_wdt_init);
192 module_exit(retu_wdt_exit);
193 module_param(counter_param, int, 0);
195 MODULE_DESCRIPTION("Retu WatchDog");
196 MODULE_AUTHOR("Amit Kucheria");
197 MODULE_LICENSE("GPL");