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/pm_wakeup.h>
21 #include <linux/reboot.h>
22 #include <linux/rtc.h>
23 #include <linux/stat.h>
24 #include <linux/suspend.h>
26 struct brcmstb_waketmr
{
27 struct rtc_device
*rtc
;
30 unsigned int wake_irq
;
31 unsigned int alarm_irq
;
32 struct notifier_block reboot_notifier
;
35 unsigned long rtc_alarm
;
40 #define BRCMSTB_WKTMR_EVENT 0x00
41 #define WKTMR_ALARM_EVENT BIT(0)
42 #define BRCMSTB_WKTMR_COUNTER 0x04
43 #define BRCMSTB_WKTMR_ALARM 0x08
44 #define BRCMSTB_WKTMR_PRESCALER 0x0C
45 #define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
47 #define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
49 static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr
*timer
)
53 reg
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
54 return !!(reg
& WKTMR_ALARM_EVENT
);
57 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr
*timer
)
61 if (timer
->alarm_en
&& timer
->alarm_irq
)
62 disable_irq(timer
->alarm_irq
);
63 timer
->alarm_en
= false;
64 reg
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
65 writel_relaxed(reg
- 1, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
66 writel_relaxed(WKTMR_ALARM_EVENT
, timer
->base
+ BRCMSTB_WKTMR_EVENT
);
67 (void)readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
68 if (timer
->alarm_expired
) {
69 timer
->alarm_expired
= false;
70 /* maintain call balance */
71 enable_irq(timer
->alarm_irq
);
75 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr
*timer
,
80 brcmstb_waketmr_clear_alarm(timer
);
82 /* Make sure we are actually counting in seconds */
83 writel_relaxed(timer
->rate
, timer
->base
+ BRCMSTB_WKTMR_PRESCALER
);
85 writel_relaxed(secs
, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
86 now
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
88 while ((int)(secs
- now
) <= 0 &&
89 !brcmstb_waketmr_is_pending(timer
)) {
91 writel_relaxed(secs
, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
92 now
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
96 static irqreturn_t
brcmstb_waketmr_irq(int irq
, void *data
)
98 struct brcmstb_waketmr
*timer
= data
;
100 if (!timer
->alarm_irq
)
101 pm_wakeup_event(timer
->dev
, 0);
105 static irqreturn_t
brcmstb_alarm_irq(int irq
, void *data
)
107 struct brcmstb_waketmr
*timer
= data
;
109 /* Ignore spurious interrupts */
110 if (!brcmstb_waketmr_is_pending(timer
))
113 if (timer
->alarm_en
) {
114 if (device_may_wakeup(timer
->dev
)) {
115 disable_irq_nosync(irq
);
116 timer
->alarm_expired
= true;
118 writel_relaxed(WKTMR_ALARM_EVENT
,
119 timer
->base
+ BRCMSTB_WKTMR_EVENT
);
121 rtc_update_irq(timer
->rtc
, 1, RTC_IRQF
| RTC_AF
);
123 writel_relaxed(WKTMR_ALARM_EVENT
,
124 timer
->base
+ BRCMSTB_WKTMR_EVENT
);
135 static void wktmr_read(struct brcmstb_waketmr
*timer
,
136 struct wktmr_time
*t
)
141 t
->sec
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
142 tmp
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_PRESCALER_VAL
);
143 } while (tmp
>= timer
->rate
);
145 t
->pre
= timer
->rate
- tmp
;
148 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr
*timer
)
150 struct device
*dev
= timer
->dev
;
153 if (device_may_wakeup(dev
)) {
154 ret
= enable_irq_wake(timer
->wake_irq
);
156 dev_err(dev
, "failed to enable wake-up interrupt\n");
159 if (timer
->alarm_en
&& timer
->alarm_irq
) {
160 ret
= enable_irq_wake(timer
->alarm_irq
);
162 dev_err(dev
, "failed to enable rtc interrupt\n");
163 disable_irq_wake(timer
->wake_irq
);
172 /* If enabled as a wakeup-source, arm the timer when powering off */
173 static int brcmstb_waketmr_reboot(struct notifier_block
*nb
,
174 unsigned long action
, void *data
)
176 struct brcmstb_waketmr
*timer
;
178 timer
= container_of(nb
, struct brcmstb_waketmr
, reboot_notifier
);
180 /* Set timer for cold boot */
181 if (action
== SYS_POWER_OFF
)
182 brcmstb_waketmr_prepare_suspend(timer
);
187 static int brcmstb_waketmr_gettime(struct device
*dev
,
190 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
191 struct wktmr_time now
;
193 wktmr_read(timer
, &now
);
195 rtc_time64_to_tm(now
.sec
, tm
);
200 static int brcmstb_waketmr_settime(struct device
*dev
,
203 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
206 sec
= rtc_tm_to_time64(tm
);
208 writel_relaxed(sec
, timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
213 static int brcmstb_waketmr_getalarm(struct device
*dev
,
214 struct rtc_wkalrm
*alarm
)
216 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
218 alarm
->enabled
= timer
->alarm_en
;
219 rtc_time64_to_tm(timer
->rtc_alarm
, &alarm
->time
);
221 alarm
->pending
= brcmstb_waketmr_is_pending(timer
);
226 static int brcmstb_waketmr_alarm_enable(struct device
*dev
,
227 unsigned int enabled
)
229 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
231 if (enabled
&& !timer
->alarm_en
) {
232 if ((int)(readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
) -
233 readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_ALARM
)) >= 0 &&
234 !brcmstb_waketmr_is_pending(timer
))
236 timer
->alarm_en
= true;
237 if (timer
->alarm_irq
) {
238 if (timer
->alarm_expired
) {
239 timer
->alarm_expired
= false;
240 /* maintain call balance */
241 enable_irq(timer
->alarm_irq
);
243 enable_irq(timer
->alarm_irq
);
245 } else if (!enabled
&& timer
->alarm_en
) {
246 if (timer
->alarm_irq
)
247 disable_irq(timer
->alarm_irq
);
248 timer
->alarm_en
= false;
254 static int brcmstb_waketmr_setalarm(struct device
*dev
,
255 struct rtc_wkalrm
*alarm
)
257 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
259 timer
->rtc_alarm
= rtc_tm_to_time64(&alarm
->time
);
261 brcmstb_waketmr_set_alarm(timer
, timer
->rtc_alarm
);
263 return brcmstb_waketmr_alarm_enable(dev
, alarm
->enabled
);
266 static const struct rtc_class_ops brcmstb_waketmr_ops
= {
267 .read_time
= brcmstb_waketmr_gettime
,
268 .set_time
= brcmstb_waketmr_settime
,
269 .read_alarm
= brcmstb_waketmr_getalarm
,
270 .set_alarm
= brcmstb_waketmr_setalarm
,
271 .alarm_irq_enable
= brcmstb_waketmr_alarm_enable
,
274 static int brcmstb_waketmr_probe(struct platform_device
*pdev
)
276 struct device
*dev
= &pdev
->dev
;
277 struct brcmstb_waketmr
*timer
;
280 timer
= devm_kzalloc(dev
, sizeof(*timer
), GFP_KERNEL
);
284 platform_set_drvdata(pdev
, timer
);
287 timer
->base
= devm_platform_ioremap_resource(pdev
, 0);
288 if (IS_ERR(timer
->base
))
289 return PTR_ERR(timer
->base
);
291 timer
->rtc
= devm_rtc_allocate_device(dev
);
292 if (IS_ERR(timer
->rtc
))
293 return PTR_ERR(timer
->rtc
);
296 * Set wakeup capability before requesting wakeup interrupt, so we can
297 * process boot-time "wakeups" (e.g., from S5 soft-off)
299 device_init_wakeup(dev
, true);
301 ret
= platform_get_irq(pdev
, 0);
304 timer
->wake_irq
= (unsigned int)ret
;
306 timer
->clk
= devm_clk_get(dev
, NULL
);
307 if (!IS_ERR(timer
->clk
)) {
308 ret
= clk_prepare_enable(timer
->clk
);
311 timer
->rate
= clk_get_rate(timer
->clk
);
313 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
315 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
319 ret
= devm_request_irq(dev
, timer
->wake_irq
, brcmstb_waketmr_irq
, 0,
320 "brcmstb-waketimer", timer
);
324 brcmstb_waketmr_clear_alarm(timer
);
326 /* Attempt to initialize non-wake irq */
327 ret
= platform_get_irq(pdev
, 1);
329 timer
->alarm_irq
= (unsigned int)ret
;
330 ret
= devm_request_irq(dev
, timer
->alarm_irq
, brcmstb_alarm_irq
,
331 IRQF_NO_AUTOEN
, "brcmstb-waketimer-rtc",
334 timer
->alarm_irq
= 0;
337 timer
->reboot_notifier
.notifier_call
= brcmstb_waketmr_reboot
;
338 register_reboot_notifier(&timer
->reboot_notifier
);
340 timer
->rtc
->ops
= &brcmstb_waketmr_ops
;
341 timer
->rtc
->range_max
= U32_MAX
;
343 ret
= devm_rtc_register_device(timer
->rtc
);
350 unregister_reboot_notifier(&timer
->reboot_notifier
);
353 clk_disable_unprepare(timer
->clk
);
358 static void brcmstb_waketmr_remove(struct platform_device
*pdev
)
360 struct brcmstb_waketmr
*timer
= dev_get_drvdata(&pdev
->dev
);
362 unregister_reboot_notifier(&timer
->reboot_notifier
);
363 clk_disable_unprepare(timer
->clk
);
366 #ifdef CONFIG_PM_SLEEP
367 static int brcmstb_waketmr_suspend(struct device
*dev
)
369 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
371 return brcmstb_waketmr_prepare_suspend(timer
);
374 static int brcmstb_waketmr_suspend_noirq(struct device
*dev
)
376 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
378 /* Catch any alarms occurring prior to noirq */
379 if (timer
->alarm_expired
&& device_may_wakeup(dev
))
385 static int brcmstb_waketmr_resume(struct device
*dev
)
387 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
390 if (!device_may_wakeup(dev
))
393 ret
= disable_irq_wake(timer
->wake_irq
);
394 if (timer
->alarm_en
&& timer
->alarm_irq
)
395 disable_irq_wake(timer
->alarm_irq
);
397 brcmstb_waketmr_clear_alarm(timer
);
402 #define brcmstb_waketmr_suspend NULL
403 #define brcmstb_waketmr_suspend_noirq NULL
404 #define brcmstb_waketmr_resume NULL
405 #endif /* CONFIG_PM_SLEEP */
407 static const struct dev_pm_ops brcmstb_waketmr_pm_ops
= {
408 .suspend
= brcmstb_waketmr_suspend
,
409 .suspend_noirq
= brcmstb_waketmr_suspend_noirq
,
410 .resume
= brcmstb_waketmr_resume
,
413 static const __maybe_unused
struct of_device_id brcmstb_waketmr_of_match
[] = {
414 { .compatible
= "brcm,brcmstb-waketimer" },
418 static struct platform_driver brcmstb_waketmr_driver
= {
419 .probe
= brcmstb_waketmr_probe
,
420 .remove_new
= brcmstb_waketmr_remove
,
422 .name
= "brcmstb-waketimer",
423 .pm
= &brcmstb_waketmr_pm_ops
,
424 .of_match_table
= of_match_ptr(brcmstb_waketmr_of_match
),
427 module_platform_driver(brcmstb_waketmr_driver
);
429 MODULE_LICENSE("GPL v2");
430 MODULE_AUTHOR("Brian Norris");
431 MODULE_AUTHOR("Markus Mayer");
432 MODULE_AUTHOR("Doug Berger");
433 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");