[WATCHDOG] at32ap700x-wdt: add missing iounmap in _remove
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / char / watchdog / at32ap700x_wdt.c
CommitLineData
a9cb3959
HCE
1/*
2 * Watchdog driver for Atmel AT32AP700X devices
3 *
4 * Copyright (C) 2005-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/miscdevice.h>
16#include <linux/fs.h>
17#include <linux/platform_device.h>
18#include <linux/watchdog.h>
c37f2713
AM
19#include <linux/uaccess.h>
20#include <linux/io.h>
a9cb3959
HCE
21
22#define TIMEOUT_MIN 1
23#define TIMEOUT_DEFAULT CONFIG_AT32AP700X_WDT_TIMEOUT
24#define TIMEOUT_MAX 2
25
26/* Watchdog registers and write/read macro */
27#define WDT_CTRL 0x00
28#define WDT_CTRL_EN 0
29#define WDT_CTRL_PSEL 8
30#define WDT_CTRL_KEY 24
31
32#define WDT_CLR 0x04
33
34#define WDT_BIT(name) (1 << WDT_##name)
35#define WDT_BF(name,value) ((value) << WDT_##name)
36
37#define wdt_readl(dev,reg) \
38 __raw_readl((dev)->regs + WDT_##reg)
39#define wdt_writel(dev,reg,value) \
40 __raw_writel((value), (dev)->regs + WDT_##reg)
41
42struct wdt_at32ap700x {
43 void __iomem *regs;
44 int timeout;
45 int users;
46 struct miscdevice miscdev;
47};
48
49static struct wdt_at32ap700x *wdt;
50
51/*
52 * Disable the watchdog.
53 */
54static void inline at32_wdt_stop(void)
55{
56 unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
57 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
58 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
59}
60
61/*
62 * Enable and reset the watchdog.
63 */
64static void inline at32_wdt_start(void)
65{
66 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
67 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
68
69 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
70 | WDT_BF(CTRL_PSEL, psel)
71 | WDT_BF(CTRL_KEY, 0x55));
72 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
73 | WDT_BF(CTRL_PSEL, psel)
74 | WDT_BF(CTRL_KEY, 0xaa));
75}
76
77/*
78 * Pat the watchdog timer.
79 */
80static void inline at32_wdt_pat(void)
81{
82 wdt_writel(wdt, CLR, 0x42);
83}
84
85/*
86 * Watchdog device is opened, and watchdog starts running.
87 */
88static int at32_wdt_open(struct inode *inode, struct file *file)
89{
90 if (test_and_set_bit(1, &wdt->users))
91 return -EBUSY;
92
93 at32_wdt_start();
94 return nonseekable_open(inode, file);
95}
96
97/*
98 * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
99 * the watchdog is also disabled.
100 */
101static int at32_wdt_close(struct inode *inode, struct file *file)
102{
103#ifndef CONFIG_WATCHDOG_NOWAYOUT
104 at32_wdt_stop();
105#endif
106 clear_bit(1, &wdt->users);
107 return 0;
108}
109
110/*
111 * Change the watchdog time interval.
112 */
113static int at32_wdt_settimeout(int time)
114{
115 /*
116 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
117 * 2 ^ 16 allowing up to 2 seconds timeout.
118 */
119 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
120 return -EINVAL;
121
c37f2713
AM
122 /*
123 * Set new watchdog time. It will be used when at32_wdt_start() is
124 * called.
125 */
a9cb3959
HCE
126 wdt->timeout = time;
127 return 0;
128}
129
130static struct watchdog_info at32_wdt_info = {
131 .identity = "at32ap700x watchdog",
132 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
133};
134
135/*
136 * Handle commands from user-space.
137 */
138static int at32_wdt_ioctl(struct inode *inode, struct file *file,
139 unsigned int cmd, unsigned long arg)
140{
141 int ret = -ENOTTY;
142 int time;
143 void __user *argp = (void __user *)arg;
144 int __user *p = argp;
145
c37f2713 146 switch (cmd) {
a9cb3959
HCE
147 case WDIOC_KEEPALIVE:
148 at32_wdt_pat();
149 ret = 0;
150 break;
151 case WDIOC_GETSUPPORT:
152 ret = copy_to_user(argp, &at32_wdt_info,
153 sizeof(at32_wdt_info)) ? -EFAULT : 0;
154 break;
155 case WDIOC_SETTIMEOUT:
156 ret = get_user(time, p);
157 if (ret)
158 break;
159 ret = at32_wdt_settimeout(time);
160 if (ret)
161 break;
162 /* Enable new time value */
163 at32_wdt_start();
164 /* fall through */
165 case WDIOC_GETTIMEOUT:
166 ret = put_user(wdt->timeout, p);
167 break;
168 case WDIOC_GETSTATUS: /* fall through */
169 case WDIOC_GETBOOTSTATUS:
170 ret = put_user(0, p);
171 break;
172 case WDIOC_SETOPTIONS:
173 ret = get_user(time, p);
174 if (ret)
175 break;
176 if (time & WDIOS_DISABLECARD)
177 at32_wdt_stop();
178 if (time & WDIOS_ENABLECARD)
179 at32_wdt_start();
180 ret = 0;
181 break;
182 }
183
184 return ret;
185}
186
c37f2713
AM
187static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len,
188 loff_t *ppos)
a9cb3959
HCE
189{
190 at32_wdt_pat();
191 return len;
192}
193
194static const struct file_operations at32_wdt_fops = {
195 .owner = THIS_MODULE,
196 .llseek = no_llseek,
197 .ioctl = at32_wdt_ioctl,
198 .open = at32_wdt_open,
199 .release = at32_wdt_close,
200 .write = at32_wdt_write,
201};
202
203static int __init at32_wdt_probe(struct platform_device *pdev)
204{
205 struct resource *regs;
206 int ret;
207
208 if (wdt) {
209 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
210 return -EBUSY;
211 }
212
213 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 if (!regs) {
215 dev_dbg(&pdev->dev, "missing mmio resource\n");
216 return -ENXIO;
217 }
218
219 wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
220 if (!wdt) {
221 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
222 return -ENOMEM;
223 }
224
225 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
226 wdt->users = 0;
227 wdt->miscdev.minor = WATCHDOG_MINOR;
228 wdt->miscdev.name = "watchdog";
229 wdt->miscdev.fops = &at32_wdt_fops;
230
231 if (at32_wdt_settimeout(TIMEOUT_DEFAULT)) {
232 at32_wdt_settimeout(TIMEOUT_MAX);
233 dev_dbg(&pdev->dev,
234 "default timeout invalid, set to %d sec.\n",
235 TIMEOUT_MAX);
236 }
237
238 ret = misc_register(&wdt->miscdev);
239 if (ret) {
240 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
241 goto err_register;
242 }
243
244 platform_set_drvdata(pdev, wdt);
245 wdt->miscdev.parent = &pdev->dev;
246 dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p\n", wdt->regs);
247
248 return 0;
249
250err_register:
251 kfree(wdt);
252 wdt = NULL;
253 return ret;
254}
255
256static int __exit at32_wdt_remove(struct platform_device *pdev)
257{
258 if (wdt && platform_get_drvdata(pdev) == wdt) {
259 misc_deregister(&wdt->miscdev);
ff732316 260 iounmap(wdt->regs);
a9cb3959
HCE
261 kfree(wdt);
262 wdt = NULL;
263 platform_set_drvdata(pdev, NULL);
264 }
265
266 return 0;
267}
268
269static void at32_wdt_shutdown(struct platform_device *pdev)
270{
271 at32_wdt_stop();
272}
273
274#ifdef CONFIG_PM
275static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
276{
277 at32_wdt_stop();
278 return 0;
279}
280
281static int at32_wdt_resume(struct platform_device *pdev)
282{
283 if (wdt->users)
284 at32_wdt_start();
285 return 0;
286}
97a2a2ea
AM
287#else
288#define at32_wdt_suspend NULL
289#define at32_wdt_resume NULL
a9cb3959
HCE
290#endif
291
292static struct platform_driver at32_wdt_driver = {
293 .remove = __exit_p(at32_wdt_remove),
a9cb3959
HCE
294 .suspend = at32_wdt_suspend,
295 .resume = at32_wdt_resume,
a9cb3959
HCE
296 .driver = {
297 .name = "at32_wdt",
298 .owner = THIS_MODULE,
299 },
300 .shutdown = at32_wdt_shutdown,
301};
302
303static int __init at32_wdt_init(void)
304{
305 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
306}
307module_init(at32_wdt_init);
308
309static void __exit at32_wdt_exit(void)
310{
311 platform_driver_unregister(&at32_wdt_driver);
312}
313module_exit(at32_wdt_exit);
314
315MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
316MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
317MODULE_LICENSE("GPL");
318MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);