blob: c74130e8f496d1d37cecf8eecf3a9aa1f4264277 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Brian Norrisc4f07ec2017-06-26 14:15:03 -07002/*
3 * Copyright © 2014-2017 Broadcom
Brian Norrisc4f07ec2017-06-26 14:15:03 -07004 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/clk.h>
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/irqreturn.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/pm.h>
20#include <linux/pm_wakeup.h>
21#include <linux/reboot.h>
22#include <linux/rtc.h>
23#include <linux/stat.h>
24#include <linux/suspend.h>
25
26struct brcmstb_waketmr {
27 struct rtc_device *rtc;
28 struct device *dev;
29 void __iomem *base;
30 int irq;
31 struct notifier_block reboot_notifier;
32 struct clk *clk;
33 u32 rate;
34};
35
36#define BRCMSTB_WKTMR_EVENT 0x00
37#define BRCMSTB_WKTMR_COUNTER 0x04
38#define BRCMSTB_WKTMR_ALARM 0x08
39#define BRCMSTB_WKTMR_PRESCALER 0x0C
40#define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
41
42#define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
43
44static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer)
45{
46 writel_relaxed(1, timer->base + BRCMSTB_WKTMR_EVENT);
47 (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
48}
49
50static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer,
51 unsigned int secs)
52{
53 brcmstb_waketmr_clear_alarm(timer);
54
Justin Chen58d3d5e2018-02-26 12:09:58 -080055 /* Make sure we are actually counting in seconds */
56 writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER);
57
Brian Norrisc4f07ec2017-06-26 14:15:03 -070058 writel_relaxed(secs + 1, timer->base + BRCMSTB_WKTMR_ALARM);
59}
60
61static irqreturn_t brcmstb_waketmr_irq(int irq, void *data)
62{
63 struct brcmstb_waketmr *timer = data;
64
65 pm_wakeup_event(timer->dev, 0);
66
67 return IRQ_HANDLED;
68}
69
70struct wktmr_time {
71 u32 sec;
72 u32 pre;
73};
74
75static void wktmr_read(struct brcmstb_waketmr *timer,
76 struct wktmr_time *t)
77{
78 u32 tmp;
79
80 do {
81 t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER);
82 tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL);
83 } while (tmp >= timer->rate);
84
85 t->pre = timer->rate - tmp;
86}
87
88static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer)
89{
90 struct device *dev = timer->dev;
91 int ret = 0;
92
93 if (device_may_wakeup(dev)) {
94 ret = enable_irq_wake(timer->irq);
95 if (ret) {
96 dev_err(dev, "failed to enable wake-up interrupt\n");
97 return ret;
98 }
99 }
100
101 return ret;
102}
103
104/* If enabled as a wakeup-source, arm the timer when powering off */
105static int brcmstb_waketmr_reboot(struct notifier_block *nb,
106 unsigned long action, void *data)
107{
108 struct brcmstb_waketmr *timer;
109
110 timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier);
111
112 /* Set timer for cold boot */
113 if (action == SYS_POWER_OFF)
114 brcmstb_waketmr_prepare_suspend(timer);
115
116 return NOTIFY_DONE;
117}
118
119static int brcmstb_waketmr_gettime(struct device *dev,
120 struct rtc_time *tm)
121{
122 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
123 struct wktmr_time now;
124
125 wktmr_read(timer, &now);
126
Alexandre Belloni9a8f2d12019-03-25 18:17:19 +0100127 rtc_time64_to_tm(now.sec, tm);
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700128
129 return 0;
130}
131
132static int brcmstb_waketmr_settime(struct device *dev,
133 struct rtc_time *tm)
134{
135 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
136 time64_t sec;
137
138 sec = rtc_tm_to_time64(tm);
139
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700140 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
141
142 return 0;
143}
144
145static int brcmstb_waketmr_getalarm(struct device *dev,
146 struct rtc_wkalrm *alarm)
147{
148 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
149 time64_t sec;
150 u32 reg;
151
152 sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM);
153 if (sec != 0) {
154 /* Alarm is enabled */
155 alarm->enabled = 1;
156 rtc_time64_to_tm(sec, &alarm->time);
157 }
158
159 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
160 alarm->pending = !!(reg & 1);
161
162 return 0;
163}
164
165static int brcmstb_waketmr_setalarm(struct device *dev,
166 struct rtc_wkalrm *alarm)
167{
168 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
169 time64_t sec;
170
171 if (alarm->enabled)
172 sec = rtc_tm_to_time64(&alarm->time);
173 else
174 sec = 0;
175
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700176 brcmstb_waketmr_set_alarm(timer, sec);
177
178 return 0;
179}
180
181/*
182 * Does not do much but keep the RTC class happy. We always support
183 * alarms.
184 */
185static int brcmstb_waketmr_alarm_enable(struct device *dev,
186 unsigned int enabled)
187{
188 return 0;
189}
190
191static const struct rtc_class_ops brcmstb_waketmr_ops = {
192 .read_time = brcmstb_waketmr_gettime,
193 .set_time = brcmstb_waketmr_settime,
194 .read_alarm = brcmstb_waketmr_getalarm,
195 .set_alarm = brcmstb_waketmr_setalarm,
196 .alarm_irq_enable = brcmstb_waketmr_alarm_enable,
197};
198
199static int brcmstb_waketmr_probe(struct platform_device *pdev)
200{
201 struct device *dev = &pdev->dev;
202 struct brcmstb_waketmr *timer;
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700203 int ret;
204
205 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
206 if (!timer)
207 return -ENOMEM;
208
209 platform_set_drvdata(pdev, timer);
210 timer->dev = dev;
211
YueHaibing09ef18b2019-10-06 18:29:20 +0800212 timer->base = devm_platform_ioremap_resource(pdev, 0);
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700213 if (IS_ERR(timer->base))
214 return PTR_ERR(timer->base);
215
Alexandre Belloni2abf2862018-05-20 22:27:29 +0200216 timer->rtc = devm_rtc_allocate_device(dev);
217 if (IS_ERR(timer->rtc))
218 return PTR_ERR(timer->rtc);
219
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700220 /*
221 * Set wakeup capability before requesting wakeup interrupt, so we can
222 * process boot-time "wakeups" (e.g., from S5 soft-off)
223 */
224 device_set_wakeup_capable(dev, true);
225 device_wakeup_enable(dev);
226
227 timer->irq = platform_get_irq(pdev, 0);
228 if (timer->irq < 0)
229 return -ENODEV;
230
231 timer->clk = devm_clk_get(dev, NULL);
232 if (!IS_ERR(timer->clk)) {
233 ret = clk_prepare_enable(timer->clk);
234 if (ret)
235 return ret;
236 timer->rate = clk_get_rate(timer->clk);
237 if (!timer->rate)
238 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
239 } else {
240 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
241 timer->clk = NULL;
242 }
243
244 ret = devm_request_irq(dev, timer->irq, brcmstb_waketmr_irq, 0,
245 "brcmstb-waketimer", timer);
246 if (ret < 0)
Alexey Khoroshilovf2eef042017-11-18 00:15:58 +0300247 goto err_clk;
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700248
249 timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot;
250 register_reboot_notifier(&timer->reboot_notifier);
251
Alexandre Belloni2abf2862018-05-20 22:27:29 +0200252 timer->rtc->ops = &brcmstb_waketmr_ops;
Alexandre Bellonided67662018-05-20 22:27:30 +0200253 timer->rtc->range_max = U32_MAX;
Alexandre Belloni2abf2862018-05-20 22:27:29 +0200254
Bartosz Golaszewskifdcfd852020-11-09 17:34:08 +0100255 ret = devm_rtc_register_device(timer->rtc);
Alexandre Belloni44c638c2019-08-19 00:00:41 +0200256 if (ret)
Alexey Khoroshilovf2eef042017-11-18 00:15:58 +0300257 goto err_notifier;
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700258
259 dev_info(dev, "registered, with irq %d\n", timer->irq);
260
Alexey Khoroshilovf2eef042017-11-18 00:15:58 +0300261 return 0;
262
263err_notifier:
264 unregister_reboot_notifier(&timer->reboot_notifier);
265
266err_clk:
Xu Wang910d0022020-11-13 07:45:38 +0000267 clk_disable_unprepare(timer->clk);
Alexey Khoroshilovf2eef042017-11-18 00:15:58 +0300268
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700269 return ret;
270}
271
272static int brcmstb_waketmr_remove(struct platform_device *pdev)
273{
274 struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev);
275
276 unregister_reboot_notifier(&timer->reboot_notifier);
Chuhong Yuan94303f82019-11-06 00:00:43 +0800277 clk_disable_unprepare(timer->clk);
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700278
279 return 0;
280}
281
282#ifdef CONFIG_PM_SLEEP
283static int brcmstb_waketmr_suspend(struct device *dev)
284{
285 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
286
287 return brcmstb_waketmr_prepare_suspend(timer);
288}
289
290static int brcmstb_waketmr_resume(struct device *dev)
291{
292 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
293 int ret;
294
295 if (!device_may_wakeup(dev))
296 return 0;
297
298 ret = disable_irq_wake(timer->irq);
299
300 brcmstb_waketmr_clear_alarm(timer);
301
302 return ret;
303}
304#endif /* CONFIG_PM_SLEEP */
305
306static SIMPLE_DEV_PM_OPS(brcmstb_waketmr_pm_ops,
307 brcmstb_waketmr_suspend, brcmstb_waketmr_resume);
308
Alexandre Belloniae1247f2021-02-02 12:22:01 +0100309static const __maybe_unused struct of_device_id brcmstb_waketmr_of_match[] = {
Brian Norrisc4f07ec2017-06-26 14:15:03 -0700310 { .compatible = "brcm,brcmstb-waketimer" },
311 { /* sentinel */ },
312};
313
314static struct platform_driver brcmstb_waketmr_driver = {
315 .probe = brcmstb_waketmr_probe,
316 .remove = brcmstb_waketmr_remove,
317 .driver = {
318 .name = "brcmstb-waketimer",
319 .pm = &brcmstb_waketmr_pm_ops,
320 .of_match_table = of_match_ptr(brcmstb_waketmr_of_match),
321 }
322};
323module_platform_driver(brcmstb_waketmr_driver);
324
325MODULE_LICENSE("GPL v2");
326MODULE_AUTHOR("Brian Norris");
327MODULE_AUTHOR("Markus Mayer");
328MODULE_DESCRIPTION("Wake-up timer driver for STB chips");