watchdog: Convert to use devm_platform_ioremap_resource
[linux-2.6-block.git] / drivers / watchdog / atlas7_wdt.c
CommitLineData
b466ee89
GZ
1/*
2 * Watchdog driver for CSR Atlas7
3 *
4 * Copyright (c) 2015 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2.
7 */
8
9#include <linux/clk.h>
10#include <linux/io.h>
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/watchdog.h>
16
17#define ATLAS7_TIMER_WDT_INDEX 5
18#define ATLAS7_WDT_DEFAULT_TIMEOUT 20
19
20#define ATLAS7_WDT_CNT_CTRL (0 + 4 * ATLAS7_TIMER_WDT_INDEX)
21#define ATLAS7_WDT_CNT_MATCH (0x18 + 4 * ATLAS7_TIMER_WDT_INDEX)
22#define ATLAS7_WDT_CNT (0x48 + 4 * ATLAS7_TIMER_WDT_INDEX)
23#define ATLAS7_WDT_CNT_EN (BIT(0) | BIT(1))
24#define ATLAS7_WDT_EN 0x64
25
26static unsigned int timeout = ATLAS7_WDT_DEFAULT_TIMEOUT;
27static bool nowayout = WATCHDOG_NOWAYOUT;
28
29module_param(timeout, uint, 0);
30module_param(nowayout, bool, 0);
31
32MODULE_PARM_DESC(timeout, "Default watchdog timeout (in seconds)");
33MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
34 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
35
36struct atlas7_wdog {
37 struct device *dev;
38 void __iomem *base;
39 unsigned long tick_rate;
40 struct clk *clk;
41};
42
43static unsigned int atlas7_wdt_gettimeleft(struct watchdog_device *wdd)
44{
45 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
46 u32 counter, match, delta;
47
48 counter = readl(wdt->base + ATLAS7_WDT_CNT);
49 match = readl(wdt->base + ATLAS7_WDT_CNT_MATCH);
50 delta = match - counter;
51
52 return delta / wdt->tick_rate;
53}
54
55static int atlas7_wdt_ping(struct watchdog_device *wdd)
56{
57 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
58 u32 counter, match, delta;
59
60 counter = readl(wdt->base + ATLAS7_WDT_CNT);
61 delta = wdd->timeout * wdt->tick_rate;
62 match = counter + delta;
63
64 writel(match, wdt->base + ATLAS7_WDT_CNT_MATCH);
65
66 return 0;
67}
68
69static int atlas7_wdt_enable(struct watchdog_device *wdd)
70{
71 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
72
73 atlas7_wdt_ping(wdd);
74
75 writel(readl(wdt->base + ATLAS7_WDT_CNT_CTRL) | ATLAS7_WDT_CNT_EN,
76 wdt->base + ATLAS7_WDT_CNT_CTRL);
77 writel(1, wdt->base + ATLAS7_WDT_EN);
78
79 return 0;
80}
81
82static int atlas7_wdt_disable(struct watchdog_device *wdd)
83{
84 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
85
86 writel(0, wdt->base + ATLAS7_WDT_EN);
87 writel(readl(wdt->base + ATLAS7_WDT_CNT_CTRL) & ~ATLAS7_WDT_CNT_EN,
88 wdt->base + ATLAS7_WDT_CNT_CTRL);
89
90 return 0;
91}
92
93static int atlas7_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
94{
95 wdd->timeout = to;
96
97 return 0;
98}
99
100#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
101
102static const struct watchdog_info atlas7_wdt_ident = {
103 .options = OPTIONS,
104 .firmware_version = 0,
105 .identity = "atlas7 Watchdog",
106};
107
b893e344 108static const struct watchdog_ops atlas7_wdt_ops = {
b466ee89
GZ
109 .owner = THIS_MODULE,
110 .start = atlas7_wdt_enable,
111 .stop = atlas7_wdt_disable,
112 .get_timeleft = atlas7_wdt_gettimeleft,
113 .ping = atlas7_wdt_ping,
114 .set_timeout = atlas7_wdt_settimeout,
115};
116
117static struct watchdog_device atlas7_wdd = {
118 .info = &atlas7_wdt_ident,
119 .ops = &atlas7_wdt_ops,
120 .timeout = ATLAS7_WDT_DEFAULT_TIMEOUT,
121};
122
123static const struct of_device_id atlas7_wdt_ids[] = {
124 { .compatible = "sirf,atlas7-tick"},
125 {}
126};
127
128static int atlas7_wdt_probe(struct platform_device *pdev)
129{
130 struct device_node *np = pdev->dev.of_node;
131 struct atlas7_wdog *wdt;
b466ee89
GZ
132 struct clk *clk;
133 int ret;
134
135 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
136 if (!wdt)
137 return -ENOMEM;
0f0a6a28 138 wdt->base = devm_platform_ioremap_resource(pdev, 0);
b466ee89
GZ
139 if (IS_ERR(wdt->base))
140 return PTR_ERR(wdt->base);
141
142 clk = of_clk_get(np, 0);
143 if (IS_ERR(clk))
144 return PTR_ERR(clk);
145 ret = clk_prepare_enable(clk);
146 if (ret) {
147 dev_err(&pdev->dev, "clk enable failed\n");
148 goto err;
149 }
150
151 /* disable watchdog hardware */
152 writel(0, wdt->base + ATLAS7_WDT_CNT_CTRL);
153
154 wdt->tick_rate = clk_get_rate(clk);
ccc8208d
WS
155 if (!wdt->tick_rate) {
156 ret = -EINVAL;
157 goto err1;
158 }
159
b466ee89
GZ
160 wdt->clk = clk;
161 atlas7_wdd.min_timeout = 1;
162 atlas7_wdd.max_timeout = UINT_MAX / wdt->tick_rate;
163
164 watchdog_init_timeout(&atlas7_wdd, 0, &pdev->dev);
165 watchdog_set_nowayout(&atlas7_wdd, nowayout);
166
167 watchdog_set_drvdata(&atlas7_wdd, wdt);
168 platform_set_drvdata(pdev, &atlas7_wdd);
169
170 ret = watchdog_register_device(&atlas7_wdd);
171 if (ret)
172 goto err1;
173
174 return 0;
175
176err1:
177 clk_disable_unprepare(clk);
178err:
179 clk_put(clk);
180 return ret;
181}
182
183static void atlas7_wdt_shutdown(struct platform_device *pdev)
184{
185 struct watchdog_device *wdd = platform_get_drvdata(pdev);
186 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
187
188 atlas7_wdt_disable(wdd);
189 clk_disable_unprepare(wdt->clk);
190}
191
192static int atlas7_wdt_remove(struct platform_device *pdev)
193{
194 struct watchdog_device *wdd = platform_get_drvdata(pdev);
195 struct atlas7_wdog *wdt = watchdog_get_drvdata(wdd);
196
197 atlas7_wdt_shutdown(pdev);
198 clk_put(wdt->clk);
199 return 0;
200}
201
202static int __maybe_unused atlas7_wdt_suspend(struct device *dev)
203{
204 /*
205 * NOTE:timer controller registers settings are saved
206 * and restored back by the timer-atlas7.c
207 */
208 return 0;
209}
210
211static int __maybe_unused atlas7_wdt_resume(struct device *dev)
212{
213 struct watchdog_device *wdd = dev_get_drvdata(dev);
214
215 /*
216 * NOTE: Since timer controller registers settings are saved
217 * and restored back by the timer-atlas7.c, so we need not
218 * update WD settings except refreshing timeout.
219 */
220 atlas7_wdt_ping(wdd);
221
222 return 0;
223}
224
225static SIMPLE_DEV_PM_OPS(atlas7_wdt_pm_ops,
226 atlas7_wdt_suspend, atlas7_wdt_resume);
227
228MODULE_DEVICE_TABLE(of, atlas7_wdt_ids);
229
230static struct platform_driver atlas7_wdt_driver = {
231 .driver = {
232 .name = "atlas7-wdt",
233 .pm = &atlas7_wdt_pm_ops,
234 .of_match_table = atlas7_wdt_ids,
235 },
236 .probe = atlas7_wdt_probe,
237 .remove = atlas7_wdt_remove,
238 .shutdown = atlas7_wdt_shutdown,
239};
240module_platform_driver(atlas7_wdt_driver);
241
242MODULE_DESCRIPTION("CSRatlas7 watchdog driver");
243MODULE_AUTHOR("Guo Zeng <Guo.Zeng@csr.com>");
244MODULE_LICENSE("GPL v2");
245MODULE_ALIAS("platform:atlas7-wdt");