1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2014-2023 Broadcom
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/irqreturn.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/rtc.h>
22 #include <linux/stat.h>
23 #include <linux/suspend.h>
25 struct brcmstb_waketmr
{
26 struct rtc_device
*rtc
;
29 unsigned int wake_irq
;
30 unsigned int alarm_irq
;
31 struct notifier_block reboot_notifier
;
34 unsigned long rtc_alarm
;
39 #define BRCMSTB_WKTMR_EVENT 0x00
40 #define WKTMR_ALARM_EVENT BIT(0)
41 #define BRCMSTB_WKTMR_COUNTER 0x04
42 #define BRCMSTB_WKTMR_ALARM 0x08
43 #define BRCMSTB_WKTMR_PRESCALER 0x0C
44 #define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
46 #define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
48 static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr
*timer
)
52 reg
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
53 return !!(reg
& WKTMR_ALARM_EVENT
);
56 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr
*timer
)
60 if (timer
->alarm_en
&& timer
->alarm_irq
)
61 disable_irq(timer
->alarm_irq
);
62 timer
->alarm_en
= false;
63 reg
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
64 writel_relaxed(reg
- 1, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
65 writel_relaxed(WKTMR_ALARM_EVENT
, timer
->base
+ BRCMSTB_WKTMR_EVENT
);
66 (void)readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
67 if (timer
->alarm_expired
) {
68 timer
->alarm_expired
= false;
69 /* maintain call balance */
70 enable_irq(timer
->alarm_irq
);
74 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr
*timer
,
79 brcmstb_waketmr_clear_alarm(timer
);
81 /* Make sure we are actually counting in seconds */
82 writel_relaxed(timer
->rate
, timer
->base
+ BRCMSTB_WKTMR_PRESCALER
);
84 writel_relaxed(secs
, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
85 now
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
87 while ((int)(secs
- now
) <= 0 &&
88 !brcmstb_waketmr_is_pending(timer
)) {
90 writel_relaxed(secs
, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
91 now
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
95 static irqreturn_t
brcmstb_waketmr_irq(int irq
, void *data
)
97 struct brcmstb_waketmr
*timer
= data
;
99 if (!timer
->alarm_irq
)
100 pm_wakeup_event(timer
->dev
, 0);
104 static irqreturn_t
brcmstb_alarm_irq(int irq
, void *data
)
106 struct brcmstb_waketmr
*timer
= data
;
108 /* Ignore spurious interrupts */
109 if (!brcmstb_waketmr_is_pending(timer
))
112 if (timer
->alarm_en
) {
113 if (device_may_wakeup(timer
->dev
)) {
114 disable_irq_nosync(irq
);
115 timer
->alarm_expired
= true;
117 writel_relaxed(WKTMR_ALARM_EVENT
,
118 timer
->base
+ BRCMSTB_WKTMR_EVENT
);
120 rtc_update_irq(timer
->rtc
, 1, RTC_IRQF
| RTC_AF
);
122 writel_relaxed(WKTMR_ALARM_EVENT
,
123 timer
->base
+ BRCMSTB_WKTMR_EVENT
);
134 static void wktmr_read(struct brcmstb_waketmr
*timer
,
135 struct wktmr_time
*t
)
140 t
->sec
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
141 tmp
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_PRESCALER_VAL
);
142 } while (tmp
>= timer
->rate
);
144 t
->pre
= timer
->rate
- tmp
;
147 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr
*timer
)
149 struct device
*dev
= timer
->dev
;
152 if (device_may_wakeup(dev
)) {
153 ret
= enable_irq_wake(timer
->wake_irq
);
155 dev_err(dev
, "failed to enable wake-up interrupt\n");
158 if (timer
->alarm_en
&& timer
->alarm_irq
) {
159 ret
= enable_irq_wake(timer
->alarm_irq
);
161 dev_err(dev
, "failed to enable rtc interrupt\n");
162 disable_irq_wake(timer
->wake_irq
);
171 /* If enabled as a wakeup-source, arm the timer when powering off */
172 static int brcmstb_waketmr_reboot(struct notifier_block
*nb
,
173 unsigned long action
, void *data
)
175 struct brcmstb_waketmr
*timer
;
177 timer
= container_of(nb
, struct brcmstb_waketmr
, reboot_notifier
);
179 /* Set timer for cold boot */
180 if (action
== SYS_POWER_OFF
)
181 brcmstb_waketmr_prepare_suspend(timer
);
186 static int brcmstb_waketmr_gettime(struct device
*dev
,
189 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
190 struct wktmr_time now
;
192 wktmr_read(timer
, &now
);
194 rtc_time64_to_tm(now
.sec
, tm
);
199 static int brcmstb_waketmr_settime(struct device
*dev
,
202 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
205 sec
= rtc_tm_to_time64(tm
);
207 writel_relaxed(sec
, timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
212 static int brcmstb_waketmr_getalarm(struct device
*dev
,
213 struct rtc_wkalrm
*alarm
)
215 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
217 alarm
->enabled
= timer
->alarm_en
;
218 rtc_time64_to_tm(timer
->rtc_alarm
, &alarm
->time
);
220 alarm
->pending
= brcmstb_waketmr_is_pending(timer
);
225 static int brcmstb_waketmr_alarm_enable(struct device
*dev
,
226 unsigned int enabled
)
228 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
230 if (enabled
&& !timer
->alarm_en
) {
231 if ((int)(readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
) -
232 readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_ALARM
)) >= 0 &&
233 !brcmstb_waketmr_is_pending(timer
))
235 timer
->alarm_en
= true;
236 if (timer
->alarm_irq
) {
237 if (timer
->alarm_expired
) {
238 timer
->alarm_expired
= false;
239 /* maintain call balance */
240 enable_irq(timer
->alarm_irq
);
242 enable_irq(timer
->alarm_irq
);
244 } else if (!enabled
&& timer
->alarm_en
) {
245 if (timer
->alarm_irq
)
246 disable_irq(timer
->alarm_irq
);
247 timer
->alarm_en
= false;
253 static int brcmstb_waketmr_setalarm(struct device
*dev
,
254 struct rtc_wkalrm
*alarm
)
256 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
258 timer
->rtc_alarm
= rtc_tm_to_time64(&alarm
->time
);
260 brcmstb_waketmr_set_alarm(timer
, timer
->rtc_alarm
);
262 return brcmstb_waketmr_alarm_enable(dev
, alarm
->enabled
);
265 static const struct rtc_class_ops brcmstb_waketmr_ops
= {
266 .read_time
= brcmstb_waketmr_gettime
,
267 .set_time
= brcmstb_waketmr_settime
,
268 .read_alarm
= brcmstb_waketmr_getalarm
,
269 .set_alarm
= brcmstb_waketmr_setalarm
,
270 .alarm_irq_enable
= brcmstb_waketmr_alarm_enable
,
273 static int brcmstb_waketmr_probe(struct platform_device
*pdev
)
275 struct device
*dev
= &pdev
->dev
;
276 struct brcmstb_waketmr
*timer
;
279 timer
= devm_kzalloc(dev
, sizeof(*timer
), GFP_KERNEL
);
283 platform_set_drvdata(pdev
, timer
);
286 timer
->base
= devm_platform_ioremap_resource(pdev
, 0);
287 if (IS_ERR(timer
->base
))
288 return PTR_ERR(timer
->base
);
290 timer
->rtc
= devm_rtc_allocate_device(dev
);
291 if (IS_ERR(timer
->rtc
))
292 return PTR_ERR(timer
->rtc
);
295 * Set wakeup capability before requesting wakeup interrupt, so we can
296 * process boot-time "wakeups" (e.g., from S5 soft-off)
298 device_init_wakeup(dev
, true);
300 ret
= platform_get_irq(pdev
, 0);
303 timer
->wake_irq
= (unsigned int)ret
;
305 timer
->clk
= devm_clk_get(dev
, NULL
);
306 if (!IS_ERR(timer
->clk
)) {
307 ret
= clk_prepare_enable(timer
->clk
);
310 timer
->rate
= clk_get_rate(timer
->clk
);
312 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
314 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
318 ret
= devm_request_irq(dev
, timer
->wake_irq
, brcmstb_waketmr_irq
, 0,
319 "brcmstb-waketimer", timer
);
323 brcmstb_waketmr_clear_alarm(timer
);
325 /* Attempt to initialize non-wake irq */
326 ret
= platform_get_irq(pdev
, 1);
328 timer
->alarm_irq
= (unsigned int)ret
;
329 ret
= devm_request_irq(dev
, timer
->alarm_irq
, brcmstb_alarm_irq
,
330 IRQF_NO_AUTOEN
, "brcmstb-waketimer-rtc",
333 timer
->alarm_irq
= 0;
336 timer
->reboot_notifier
.notifier_call
= brcmstb_waketmr_reboot
;
337 register_reboot_notifier(&timer
->reboot_notifier
);
339 timer
->rtc
->ops
= &brcmstb_waketmr_ops
;
340 timer
->rtc
->range_max
= U32_MAX
;
342 ret
= devm_rtc_register_device(timer
->rtc
);
349 unregister_reboot_notifier(&timer
->reboot_notifier
);
352 clk_disable_unprepare(timer
->clk
);
357 static void brcmstb_waketmr_remove(struct platform_device
*pdev
)
359 struct brcmstb_waketmr
*timer
= dev_get_drvdata(&pdev
->dev
);
361 unregister_reboot_notifier(&timer
->reboot_notifier
);
362 clk_disable_unprepare(timer
->clk
);
365 #ifdef CONFIG_PM_SLEEP
366 static int brcmstb_waketmr_suspend(struct device
*dev
)
368 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
370 return brcmstb_waketmr_prepare_suspend(timer
);
373 static int brcmstb_waketmr_suspend_noirq(struct device
*dev
)
375 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
377 /* Catch any alarms occurring prior to noirq */
378 if (timer
->alarm_expired
&& device_may_wakeup(dev
))
384 static int brcmstb_waketmr_resume(struct device
*dev
)
386 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
389 if (!device_may_wakeup(dev
))
392 ret
= disable_irq_wake(timer
->wake_irq
);
393 if (timer
->alarm_en
&& timer
->alarm_irq
)
394 disable_irq_wake(timer
->alarm_irq
);
396 brcmstb_waketmr_clear_alarm(timer
);
401 #define brcmstb_waketmr_suspend NULL
402 #define brcmstb_waketmr_suspend_noirq NULL
403 #define brcmstb_waketmr_resume NULL
404 #endif /* CONFIG_PM_SLEEP */
406 static const struct dev_pm_ops brcmstb_waketmr_pm_ops
= {
407 .suspend
= brcmstb_waketmr_suspend
,
408 .suspend_noirq
= brcmstb_waketmr_suspend_noirq
,
409 .resume
= brcmstb_waketmr_resume
,
412 static const __maybe_unused
struct of_device_id brcmstb_waketmr_of_match
[] = {
413 { .compatible
= "brcm,brcmstb-waketimer" },
417 static struct platform_driver brcmstb_waketmr_driver
= {
418 .probe
= brcmstb_waketmr_probe
,
419 .remove
= brcmstb_waketmr_remove
,
421 .name
= "brcmstb-waketimer",
422 .pm
= &brcmstb_waketmr_pm_ops
,
423 .of_match_table
= of_match_ptr(brcmstb_waketmr_of_match
),
426 module_platform_driver(brcmstb_waketmr_driver
);
428 MODULE_LICENSE("GPL v2");
429 MODULE_AUTHOR("Brian Norris");
430 MODULE_AUTHOR("Markus Mayer");
431 MODULE_AUTHOR("Doug Berger");
432 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");