watchdog: s3c2410_wdt: Use devm_* functions
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / watchdog / s3c2410_wdt.c
1 /* linux/drivers/char/watchdog/s3c2410_wdt.c
2  *
3  * Copyright (c) 2004 Simtec Electronics
4  *      Ben Dooks <ben@simtec.co.uk>
5  *
6  * S3C2410 Watchdog Timer Support
7  *
8  * Based on, softdog.c by Alan Cox,
9  *     (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/types.h>
31 #include <linux/timer.h>
32 #include <linux/miscdevice.h> /* for MODULE_ALIAS_MISCDEV */
33 #include <linux/watchdog.h>
34 #include <linux/init.h>
35 #include <linux/platform_device.h>
36 #include <linux/interrupt.h>
37 #include <linux/clk.h>
38 #include <linux/uaccess.h>
39 #include <linux/io.h>
40 #include <linux/cpufreq.h>
41 #include <linux/slab.h>
42 #include <linux/err.h>
43 #include <linux/of.h>
44
45 #include <mach/map.h>
46
47 #undef S3C_VA_WATCHDOG
48 #define S3C_VA_WATCHDOG (0)
49
50 #include <plat/regs-watchdog.h>
51
52 #define CONFIG_S3C2410_WATCHDOG_ATBOOT          (0)
53 #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME    (15)
54
55 static bool nowayout    = WATCHDOG_NOWAYOUT;
56 static int tmr_margin   = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
57 static int tmr_atboot   = CONFIG_S3C2410_WATCHDOG_ATBOOT;
58 static int soft_noboot;
59 static int debug;
60
61 module_param(tmr_margin,  int, 0);
62 module_param(tmr_atboot,  int, 0);
63 module_param(nowayout,   bool, 0);
64 module_param(soft_noboot, int, 0);
65 module_param(debug,       int, 0);
66
67 MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
68                 __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
69 MODULE_PARM_DESC(tmr_atboot,
70                 "Watchdog is started at boot time if set to 1, default="
71                         __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
72 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
73                         __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
74 MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
75                         "0 to reboot (default 0)");
76 MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
77
78 static struct device    *wdt_dev;       /* platform device attached to */
79 static struct resource  *wdt_mem;
80 static struct resource  *wdt_irq;
81 static struct clk       *wdt_clock;
82 static void __iomem     *wdt_base;
83 static unsigned int      wdt_count;
84 static DEFINE_SPINLOCK(wdt_lock);
85
86 /* watchdog control routines */
87
88 #define DBG(fmt, ...)                                   \
89 do {                                                    \
90         if (debug)                                      \
91                 pr_info(fmt, ##__VA_ARGS__);            \
92 } while (0)
93
94 /* functions */
95
96 static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
97 {
98         spin_lock(&wdt_lock);
99         writel(wdt_count, wdt_base + S3C2410_WTCNT);
100         spin_unlock(&wdt_lock);
101
102         return 0;
103 }
104
105 static void __s3c2410wdt_stop(void)
106 {
107         unsigned long wtcon;
108
109         wtcon = readl(wdt_base + S3C2410_WTCON);
110         wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
111         writel(wtcon, wdt_base + S3C2410_WTCON);
112 }
113
114 static int s3c2410wdt_stop(struct watchdog_device *wdd)
115 {
116         spin_lock(&wdt_lock);
117         __s3c2410wdt_stop();
118         spin_unlock(&wdt_lock);
119
120         return 0;
121 }
122
123 static int s3c2410wdt_start(struct watchdog_device *wdd)
124 {
125         unsigned long wtcon;
126
127         spin_lock(&wdt_lock);
128
129         __s3c2410wdt_stop();
130
131         wtcon = readl(wdt_base + S3C2410_WTCON);
132         wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
133
134         if (soft_noboot) {
135                 wtcon |= S3C2410_WTCON_INTEN;
136                 wtcon &= ~S3C2410_WTCON_RSTEN;
137         } else {
138                 wtcon &= ~S3C2410_WTCON_INTEN;
139                 wtcon |= S3C2410_WTCON_RSTEN;
140         }
141
142         DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
143             __func__, wdt_count, wtcon);
144
145         writel(wdt_count, wdt_base + S3C2410_WTDAT);
146         writel(wdt_count, wdt_base + S3C2410_WTCNT);
147         writel(wtcon, wdt_base + S3C2410_WTCON);
148         spin_unlock(&wdt_lock);
149
150         return 0;
151 }
152
153 static inline int s3c2410wdt_is_running(void)
154 {
155         return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
156 }
157
158 static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
159 {
160         unsigned long freq = clk_get_rate(wdt_clock);
161         unsigned int count;
162         unsigned int divisor = 1;
163         unsigned long wtcon;
164
165         if (timeout < 1)
166                 return -EINVAL;
167
168         freq /= 128;
169         count = timeout * freq;
170
171         DBG("%s: count=%d, timeout=%d, freq=%lu\n",
172             __func__, count, timeout, freq);
173
174         /* if the count is bigger than the watchdog register,
175            then work out what we need to do (and if) we can
176            actually make this value
177         */
178
179         if (count >= 0x10000) {
180                 for (divisor = 1; divisor <= 0x100; divisor++) {
181                         if ((count / divisor) < 0x10000)
182                                 break;
183                 }
184
185                 if ((count / divisor) >= 0x10000) {
186                         dev_err(wdt_dev, "timeout %d too big\n", timeout);
187                         return -EINVAL;
188                 }
189         }
190
191         DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
192             __func__, timeout, divisor, count, count/divisor);
193
194         count /= divisor;
195         wdt_count = count;
196
197         /* update the pre-scaler */
198         wtcon = readl(wdt_base + S3C2410_WTCON);
199         wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
200         wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
201
202         writel(count, wdt_base + S3C2410_WTDAT);
203         writel(wtcon, wdt_base + S3C2410_WTCON);
204
205         wdd->timeout = (count * divisor) / freq;
206
207         return 0;
208 }
209
210 #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
211
212 static const struct watchdog_info s3c2410_wdt_ident = {
213         .options          =     OPTIONS,
214         .firmware_version =     0,
215         .identity         =     "S3C2410 Watchdog",
216 };
217
218 static struct watchdog_ops s3c2410wdt_ops = {
219         .owner = THIS_MODULE,
220         .start = s3c2410wdt_start,
221         .stop = s3c2410wdt_stop,
222         .ping = s3c2410wdt_keepalive,
223         .set_timeout = s3c2410wdt_set_heartbeat,
224 };
225
226 static struct watchdog_device s3c2410_wdd = {
227         .info = &s3c2410_wdt_ident,
228         .ops = &s3c2410wdt_ops,
229 };
230
231 /* interrupt handler code */
232
233 static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
234 {
235         dev_info(wdt_dev, "watchdog timer expired (irq)\n");
236
237         s3c2410wdt_keepalive(&s3c2410_wdd);
238         return IRQ_HANDLED;
239 }
240
241
242 #ifdef CONFIG_CPU_FREQ
243
244 static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
245                                           unsigned long val, void *data)
246 {
247         int ret;
248
249         if (!s3c2410wdt_is_running())
250                 goto done;
251
252         if (val == CPUFREQ_PRECHANGE) {
253                 /* To ensure that over the change we don't cause the
254                  * watchdog to trigger, we perform an keep-alive if
255                  * the watchdog is running.
256                  */
257
258                 s3c2410wdt_keepalive(&s3c2410_wdd);
259         } else if (val == CPUFREQ_POSTCHANGE) {
260                 s3c2410wdt_stop(&s3c2410_wdd);
261
262                 ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout);
263
264                 if (ret >= 0)
265                         s3c2410wdt_start(&s3c2410_wdd);
266                 else
267                         goto err;
268         }
269
270 done:
271         return 0;
272
273  err:
274         dev_err(wdt_dev, "cannot set new value for timeout %d\n",
275                                 s3c2410_wdd.timeout);
276         return ret;
277 }
278
279 static struct notifier_block s3c2410wdt_cpufreq_transition_nb = {
280         .notifier_call  = s3c2410wdt_cpufreq_transition,
281 };
282
283 static inline int s3c2410wdt_cpufreq_register(void)
284 {
285         return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb,
286                                          CPUFREQ_TRANSITION_NOTIFIER);
287 }
288
289 static inline void s3c2410wdt_cpufreq_deregister(void)
290 {
291         cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb,
292                                     CPUFREQ_TRANSITION_NOTIFIER);
293 }
294
295 #else
296 static inline int s3c2410wdt_cpufreq_register(void)
297 {
298         return 0;
299 }
300
301 static inline void s3c2410wdt_cpufreq_deregister(void)
302 {
303 }
304 #endif
305
306 static int s3c2410wdt_probe(struct platform_device *pdev)
307 {
308         struct device *dev;
309         unsigned int wtcon;
310         int started = 0;
311         int ret;
312
313         DBG("%s: probe=%p\n", __func__, pdev);
314
315         dev = &pdev->dev;
316         wdt_dev = &pdev->dev;
317
318         wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
319         if (wdt_mem == NULL) {
320                 dev_err(dev, "no memory resource specified\n");
321                 return -ENOENT;
322         }
323
324         wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
325         if (wdt_irq == NULL) {
326                 dev_err(dev, "no irq resource specified\n");
327                 ret = -ENOENT;
328                 goto err;
329         }
330
331         /* get the memory region for the watchdog timer */
332         wdt_base = devm_request_and_ioremap(dev, wdt_mem);
333         if (wdt_base == NULL) {
334                 dev_err(dev, "failed to devm_request_and_ioremap() region\n");
335                 ret = -ENOMEM;
336                 goto err;
337         }
338
339         DBG("probe: mapped wdt_base=%p\n", wdt_base);
340
341         wdt_clock = devm_clk_get(dev, "watchdog");
342         if (IS_ERR(wdt_clock)) {
343                 dev_err(dev, "failed to find watchdog clock source\n");
344                 ret = PTR_ERR(wdt_clock);
345                 goto err;
346         }
347
348         clk_prepare_enable(wdt_clock);
349
350         ret = s3c2410wdt_cpufreq_register();
351         if (ret < 0) {
352                 pr_err("failed to register cpufreq\n");
353                 goto err_clk;
354         }
355
356         /* see if we can actually set the requested timer margin, and if
357          * not, try the default value */
358
359         if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, tmr_margin)) {
360                 started = s3c2410wdt_set_heartbeat(&s3c2410_wdd,
361                                         CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
362
363                 if (started == 0)
364                         dev_info(dev,
365                            "tmr_margin value out of range, default %d used\n",
366                                CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
367                 else
368                         dev_info(dev, "default timer value is out of range, "
369                                                         "cannot start\n");
370         }
371
372         ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
373                                 pdev->name, pdev);
374         if (ret != 0) {
375                 dev_err(dev, "failed to install irq (%d)\n", ret);
376                 goto err_cpufreq;
377         }
378
379         watchdog_set_nowayout(&s3c2410_wdd, nowayout);
380
381         ret = watchdog_register_device(&s3c2410_wdd);
382         if (ret) {
383                 dev_err(dev, "cannot register watchdog (%d)\n", ret);
384                 goto err_cpufreq;
385         }
386
387         if (tmr_atboot && started == 0) {
388                 dev_info(dev, "starting watchdog timer\n");
389                 s3c2410wdt_start(&s3c2410_wdd);
390         } else if (!tmr_atboot) {
391                 /* if we're not enabling the watchdog, then ensure it is
392                  * disabled if it has been left running from the bootloader
393                  * or other source */
394
395                 s3c2410wdt_stop(&s3c2410_wdd);
396         }
397
398         /* print out a statement of readiness */
399
400         wtcon = readl(wdt_base + S3C2410_WTCON);
401
402         dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
403                  (wtcon & S3C2410_WTCON_ENABLE) ?  "" : "in",
404                  (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
405                  (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
406
407         return 0;
408
409  err_cpufreq:
410         s3c2410wdt_cpufreq_deregister();
411
412  err_clk:
413         clk_disable_unprepare(wdt_clock);
414         wdt_clock = NULL;
415
416  err:
417         wdt_irq = NULL;
418         wdt_mem = NULL;
419         return ret;
420 }
421
422 static int s3c2410wdt_remove(struct platform_device *dev)
423 {
424         watchdog_unregister_device(&s3c2410_wdd);
425
426         s3c2410wdt_cpufreq_deregister();
427
428         clk_disable_unprepare(wdt_clock);
429         wdt_clock = NULL;
430
431         wdt_irq = NULL;
432         wdt_mem = NULL;
433         return 0;
434 }
435
436 static void s3c2410wdt_shutdown(struct platform_device *dev)
437 {
438         s3c2410wdt_stop(&s3c2410_wdd);
439 }
440
441 #ifdef CONFIG_PM
442
443 static unsigned long wtcon_save;
444 static unsigned long wtdat_save;
445
446 static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
447 {
448         /* Save watchdog state, and turn it off. */
449         wtcon_save = readl(wdt_base + S3C2410_WTCON);
450         wtdat_save = readl(wdt_base + S3C2410_WTDAT);
451
452         /* Note that WTCNT doesn't need to be saved. */
453         s3c2410wdt_stop(&s3c2410_wdd);
454
455         return 0;
456 }
457
458 static int s3c2410wdt_resume(struct platform_device *dev)
459 {
460         /* Restore watchdog state. */
461
462         writel(wtdat_save, wdt_base + S3C2410_WTDAT);
463         writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
464         writel(wtcon_save, wdt_base + S3C2410_WTCON);
465
466         pr_info("watchdog %sabled\n",
467                 (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
468
469         return 0;
470 }
471
472 #else
473 #define s3c2410wdt_suspend NULL
474 #define s3c2410wdt_resume  NULL
475 #endif /* CONFIG_PM */
476
477 #ifdef CONFIG_OF
478 static const struct of_device_id s3c2410_wdt_match[] = {
479         { .compatible = "samsung,s3c2410-wdt" },
480         {},
481 };
482 MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
483 #endif
484
485 static struct platform_driver s3c2410wdt_driver = {
486         .probe          = s3c2410wdt_probe,
487         .remove         = s3c2410wdt_remove,
488         .shutdown       = s3c2410wdt_shutdown,
489         .suspend        = s3c2410wdt_suspend,
490         .resume         = s3c2410wdt_resume,
491         .driver         = {
492                 .owner  = THIS_MODULE,
493                 .name   = "s3c2410-wdt",
494                 .of_match_table = of_match_ptr(s3c2410_wdt_match),
495         },
496 };
497
498 module_platform_driver(s3c2410wdt_driver);
499
500 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
501               "Dimitry Andric <dimitry.andric@tomtom.com>");
502 MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
503 MODULE_LICENSE("GPL");
504 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
505 MODULE_ALIAS("platform:s3c2410-wdt");