Merge remote-tracking branches 'asoc/fix/adsp', 'asoc/fix/arizona', 'asoc/fix/atmel...
[linux-drm-fsl-dcu.git] / drivers / watchdog / stmp3xxx_rtc_wdt.c
1 /*
2  * Watchdog driver for the RTC based watchdog in STMP3xxx and i.MX23/28
3  *
4  * Author: Wolfram Sang <w.sang@pengutronix.de>
5  *
6  * Copyright (C) 2011-12 Wolfram Sang, Pengutronix
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  */
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/watchdog.h>
16 #include <linux/platform_device.h>
17 #include <linux/stmp3xxx_rtc_wdt.h>
18
19 #define WDOG_TICK_RATE 1000 /* 1 kHz clock */
20 #define STMP3XXX_DEFAULT_TIMEOUT 19
21 #define STMP3XXX_MAX_TIMEOUT (UINT_MAX / WDOG_TICK_RATE)
22
23 static int heartbeat = STMP3XXX_DEFAULT_TIMEOUT;
24 module_param(heartbeat, uint, 0);
25 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat period in seconds from 1 to "
26                  __MODULE_STRING(STMP3XXX_MAX_TIMEOUT) ", default "
27                  __MODULE_STRING(STMP3XXX_DEFAULT_TIMEOUT));
28
29 static int wdt_start(struct watchdog_device *wdd)
30 {
31         struct device *dev = watchdog_get_drvdata(wdd);
32         struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
33
34         pdata->wdt_set_timeout(dev->parent, wdd->timeout * WDOG_TICK_RATE);
35         return 0;
36 }
37
38 static int wdt_stop(struct watchdog_device *wdd)
39 {
40         struct device *dev = watchdog_get_drvdata(wdd);
41         struct stmp3xxx_wdt_pdata *pdata = dev_get_platdata(dev);
42
43         pdata->wdt_set_timeout(dev->parent, 0);
44         return 0;
45 }
46
47 static int wdt_set_timeout(struct watchdog_device *wdd, unsigned new_timeout)
48 {
49         wdd->timeout = new_timeout;
50         return wdt_start(wdd);
51 }
52
53 static const struct watchdog_info stmp3xxx_wdt_ident = {
54         .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
55         .identity = "STMP3XXX RTC Watchdog",
56 };
57
58 static const struct watchdog_ops stmp3xxx_wdt_ops = {
59         .owner = THIS_MODULE,
60         .start = wdt_start,
61         .stop = wdt_stop,
62         .set_timeout = wdt_set_timeout,
63 };
64
65 static struct watchdog_device stmp3xxx_wdd = {
66         .info = &stmp3xxx_wdt_ident,
67         .ops = &stmp3xxx_wdt_ops,
68         .min_timeout = 1,
69         .max_timeout = STMP3XXX_MAX_TIMEOUT,
70         .status = WATCHDOG_NOWAYOUT_INIT_STATUS,
71 };
72
73 static int stmp3xxx_wdt_probe(struct platform_device *pdev)
74 {
75         int ret;
76
77         watchdog_set_drvdata(&stmp3xxx_wdd, &pdev->dev);
78
79         stmp3xxx_wdd.timeout = clamp_t(unsigned, heartbeat, 1, STMP3XXX_MAX_TIMEOUT);
80
81         ret = watchdog_register_device(&stmp3xxx_wdd);
82         if (ret < 0) {
83                 dev_err(&pdev->dev, "cannot register watchdog device\n");
84                 return ret;
85         }
86
87         dev_info(&pdev->dev, "initialized watchdog with heartbeat %ds\n",
88                         stmp3xxx_wdd.timeout);
89         return 0;
90 }
91
92 static int stmp3xxx_wdt_remove(struct platform_device *pdev)
93 {
94         watchdog_unregister_device(&stmp3xxx_wdd);
95         return 0;
96 }
97
98 static struct platform_driver stmp3xxx_wdt_driver = {
99         .driver = {
100                 .name = "stmp3xxx_rtc_wdt",
101         },
102         .probe = stmp3xxx_wdt_probe,
103         .remove = stmp3xxx_wdt_remove,
104 };
105 module_platform_driver(stmp3xxx_wdt_driver);
106
107 MODULE_DESCRIPTION("STMP3XXX RTC Watchdog Driver");
108 MODULE_LICENSE("GPL v2");
109 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");