]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/retu-wdt.c
Add Nokia CBUS support
[linux-2.6-omap-h63xx.git] / drivers / cbus / retu-wdt.c
1 /**
2  * drivers/cbus/retu-wdt.c
3  *
4  * Driver for Retu watchdog
5  *
6  * Copyright (C) 2004, 2005 Nokia Corporation
7  *
8  * Written by Amit Kucheria <amit.kucheria@nokia.com>
9  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
28
29 #include <linux/completion.h>
30 #include <linux/errno.h>
31 #include <linux/moduleparam.h>
32 #include <linux/platform_device.h>
33
34 #include "cbus.h"
35 #include "retu.h"
36
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
41
42 static struct completion retu_wdt_completion;
43 static DECLARE_MUTEX(retu_wdt_mutex);   /* Avoid simultaneous writes to watchdog register */
44
45 static unsigned int period_val = RETU_WDT_DEFAULT_TIMER;        /* Current period of watchdog */
46 static int counter_param = RETU_WDT_MAX_TIMER;
47
48 static int retu_modify_counter(unsigned int new)
49 {
50         int ret = 0;
51
52         if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
53                 return -EINVAL;
54
55         down_interruptible(&retu_wdt_mutex);
56
57         period_val = new;
58         retu_write_reg(RETU_REG_WATCHDOG, (u16)period_val);
59
60         up(&retu_wdt_mutex);
61         return ret;
62 }
63
64 static ssize_t retu_wdt_period_show(struct device *dev, struct device_attribute *attr,
65                                     char *buf)
66 {
67         /* Show current max counter */
68         return sprintf(buf, "%u\n", (u16)period_val);
69 }
70
71 static ssize_t retu_wdt_period_store(struct device *dev, struct device_attribute *attr,
72                                      const char *buf, size_t count)
73 {
74         unsigned int new_period;
75         int ret;
76
77         if (sscanf(buf, "%u", &new_period) != 1) {
78                 printk(KERN_ALERT "retu_wdt_period_store: Invalid input\n");
79                 return -EINVAL;
80         }
81
82         ret = retu_modify_counter(new_period);
83         if (ret < 0)
84                 return ret;
85
86         return strnlen(buf, count);
87 }
88
89 static ssize_t retu_wdt_counter_show(struct device *dev, struct device_attribute *attr,
90                                      char *buf)
91 {
92         u16 counter;
93
94         /* Show current value in watchdog counter */
95         counter = retu_read_reg(RETU_REG_WATCHDOG);
96
97         /* Only the 5 LSB are important */
98         return snprintf(buf, PAGE_SIZE, "%u\n", (counter & 0x3F));
99 }
100
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);
104
105 static int __devinit retu_wdt_probe(struct device *dev)
106 {
107         int ret;
108
109         ret = device_create_file(dev, &dev_attr_period);
110         if (ret) {
111                 printk(KERN_ERR "retu_wdt_probe: Error creating sys device file: period\n");
112                 return ret;
113         }
114
115         ret = device_create_file(dev, &dev_attr_counter);
116         if (ret) {
117                 device_remove_file(dev, &dev_attr_period);
118                 printk(KERN_ERR "retu_wdt_probe: Error creating sys device file: counter\n");
119         }
120
121         return ret;
122 }
123
124 static int __devexit retu_wdt_remove(struct device *dev)
125 {
126         device_remove_file(dev, &dev_attr_period);
127         device_remove_file(dev, &dev_attr_counter);
128         return 0;
129 }
130
131 static void retu_wdt_device_release(struct device *dev)
132 {
133         complete(&retu_wdt_completion);
134 }
135
136 static struct platform_device retu_wdt_device = {
137         .name = "retu-watchdog",
138         .id = -1,
139         .dev = {
140                 .release = retu_wdt_device_release,
141         },
142 };
143
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),
149 };
150
151 static int __init retu_wdt_init(void)
152 {
153         int ret;
154
155         init_completion(&retu_wdt_completion);
156
157         ret = driver_register(&retu_wdt_driver);
158         if (ret)
159                 return ret;
160
161         ret = platform_device_register(&retu_wdt_device);
162         if (ret)
163                 goto exit1;
164
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);
169                 printk(KERN_INFO
170                        "retu_wdt_init: Intializing to default value\n");
171         }
172
173         printk(KERN_INFO "Retu watchdog driver initialized\n");
174         return ret;
175
176 exit1:
177         driver_unregister(&retu_wdt_driver);
178         wait_for_completion(&retu_wdt_completion);
179
180         return ret;
181 }
182
183 static void __exit retu_wdt_exit(void)
184 {
185         platform_device_unregister(&retu_wdt_device);
186         driver_unregister(&retu_wdt_driver);
187
188         wait_for_completion(&retu_wdt_completion);
189 }
190
191 module_init(retu_wdt_init);
192 module_exit(retu_wdt_exit);
193 module_param(counter_param, int, 0);
194
195 MODULE_DESCRIPTION("Retu WatchDog");
196 MODULE_AUTHOR("Amit Kucheria");
197 MODULE_LICENSE("GPL");
198