2 * Copyright © 2014-2017 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/clk.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
22 #include <linux/irqreturn.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
28 #include <linux/pm_wakeup.h>
29 #include <linux/reboot.h>
30 #include <linux/rtc.h>
31 #include <linux/stat.h>
32 #include <linux/suspend.h>
34 struct brcmstb_waketmr
{
35 struct rtc_device
*rtc
;
39 struct notifier_block reboot_notifier
;
44 #define BRCMSTB_WKTMR_EVENT 0x00
45 #define BRCMSTB_WKTMR_COUNTER 0x04
46 #define BRCMSTB_WKTMR_ALARM 0x08
47 #define BRCMSTB_WKTMR_PRESCALER 0x0C
48 #define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
50 #define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
52 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr
*timer
)
54 writel_relaxed(1, timer
->base
+ BRCMSTB_WKTMR_EVENT
);
55 (void)readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
58 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr
*timer
,
61 brcmstb_waketmr_clear_alarm(timer
);
63 /* Make sure we are actually counting in seconds */
64 writel_relaxed(timer
->rate
, timer
->base
+ BRCMSTB_WKTMR_PRESCALER
);
66 writel_relaxed(secs
+ 1, timer
->base
+ BRCMSTB_WKTMR_ALARM
);
69 static irqreturn_t
brcmstb_waketmr_irq(int irq
, void *data
)
71 struct brcmstb_waketmr
*timer
= data
;
73 pm_wakeup_event(timer
->dev
, 0);
83 static void wktmr_read(struct brcmstb_waketmr
*timer
,
89 t
->sec
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
90 tmp
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_PRESCALER_VAL
);
91 } while (tmp
>= timer
->rate
);
93 t
->pre
= timer
->rate
- tmp
;
96 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr
*timer
)
98 struct device
*dev
= timer
->dev
;
101 if (device_may_wakeup(dev
)) {
102 ret
= enable_irq_wake(timer
->irq
);
104 dev_err(dev
, "failed to enable wake-up interrupt\n");
112 /* If enabled as a wakeup-source, arm the timer when powering off */
113 static int brcmstb_waketmr_reboot(struct notifier_block
*nb
,
114 unsigned long action
, void *data
)
116 struct brcmstb_waketmr
*timer
;
118 timer
= container_of(nb
, struct brcmstb_waketmr
, reboot_notifier
);
120 /* Set timer for cold boot */
121 if (action
== SYS_POWER_OFF
)
122 brcmstb_waketmr_prepare_suspend(timer
);
127 static int brcmstb_waketmr_gettime(struct device
*dev
,
130 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
131 struct wktmr_time now
;
133 wktmr_read(timer
, &now
);
135 rtc_time_to_tm(now
.sec
, tm
);
140 static int brcmstb_waketmr_settime(struct device
*dev
,
143 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
146 sec
= rtc_tm_to_time64(tm
);
148 writel_relaxed(sec
, timer
->base
+ BRCMSTB_WKTMR_COUNTER
);
153 static int brcmstb_waketmr_getalarm(struct device
*dev
,
154 struct rtc_wkalrm
*alarm
)
156 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
160 sec
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_ALARM
);
162 /* Alarm is enabled */
164 rtc_time64_to_tm(sec
, &alarm
->time
);
167 reg
= readl_relaxed(timer
->base
+ BRCMSTB_WKTMR_EVENT
);
168 alarm
->pending
= !!(reg
& 1);
173 static int brcmstb_waketmr_setalarm(struct device
*dev
,
174 struct rtc_wkalrm
*alarm
)
176 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
180 sec
= rtc_tm_to_time64(&alarm
->time
);
184 brcmstb_waketmr_set_alarm(timer
, sec
);
190 * Does not do much but keep the RTC class happy. We always support
193 static int brcmstb_waketmr_alarm_enable(struct device
*dev
,
194 unsigned int enabled
)
199 static const struct rtc_class_ops brcmstb_waketmr_ops
= {
200 .read_time
= brcmstb_waketmr_gettime
,
201 .set_time
= brcmstb_waketmr_settime
,
202 .read_alarm
= brcmstb_waketmr_getalarm
,
203 .set_alarm
= brcmstb_waketmr_setalarm
,
204 .alarm_irq_enable
= brcmstb_waketmr_alarm_enable
,
207 static int brcmstb_waketmr_probe(struct platform_device
*pdev
)
209 struct device
*dev
= &pdev
->dev
;
210 struct brcmstb_waketmr
*timer
;
211 struct resource
*res
;
214 timer
= devm_kzalloc(dev
, sizeof(*timer
), GFP_KERNEL
);
218 platform_set_drvdata(pdev
, timer
);
221 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
222 timer
->base
= devm_ioremap_resource(dev
, res
);
223 if (IS_ERR(timer
->base
))
224 return PTR_ERR(timer
->base
);
226 timer
->rtc
= devm_rtc_allocate_device(dev
);
227 if (IS_ERR(timer
->rtc
))
228 return PTR_ERR(timer
->rtc
);
231 * Set wakeup capability before requesting wakeup interrupt, so we can
232 * process boot-time "wakeups" (e.g., from S5 soft-off)
234 device_set_wakeup_capable(dev
, true);
235 device_wakeup_enable(dev
);
237 timer
->irq
= platform_get_irq(pdev
, 0);
241 timer
->clk
= devm_clk_get(dev
, NULL
);
242 if (!IS_ERR(timer
->clk
)) {
243 ret
= clk_prepare_enable(timer
->clk
);
246 timer
->rate
= clk_get_rate(timer
->clk
);
248 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
250 timer
->rate
= BRCMSTB_WKTMR_DEFAULT_FREQ
;
254 ret
= devm_request_irq(dev
, timer
->irq
, brcmstb_waketmr_irq
, 0,
255 "brcmstb-waketimer", timer
);
259 timer
->reboot_notifier
.notifier_call
= brcmstb_waketmr_reboot
;
260 register_reboot_notifier(&timer
->reboot_notifier
);
262 timer
->rtc
->ops
= &brcmstb_waketmr_ops
;
263 timer
->rtc
->range_max
= U32_MAX
;
265 ret
= rtc_register_device(timer
->rtc
);
267 dev_err(dev
, "unable to register device\n");
271 dev_info(dev
, "registered, with irq %d\n", timer
->irq
);
276 unregister_reboot_notifier(&timer
->reboot_notifier
);
280 clk_disable_unprepare(timer
->clk
);
285 static int brcmstb_waketmr_remove(struct platform_device
*pdev
)
287 struct brcmstb_waketmr
*timer
= dev_get_drvdata(&pdev
->dev
);
289 unregister_reboot_notifier(&timer
->reboot_notifier
);
294 #ifdef CONFIG_PM_SLEEP
295 static int brcmstb_waketmr_suspend(struct device
*dev
)
297 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
299 return brcmstb_waketmr_prepare_suspend(timer
);
302 static int brcmstb_waketmr_resume(struct device
*dev
)
304 struct brcmstb_waketmr
*timer
= dev_get_drvdata(dev
);
307 if (!device_may_wakeup(dev
))
310 ret
= disable_irq_wake(timer
->irq
);
312 brcmstb_waketmr_clear_alarm(timer
);
316 #endif /* CONFIG_PM_SLEEP */
318 static SIMPLE_DEV_PM_OPS(brcmstb_waketmr_pm_ops
,
319 brcmstb_waketmr_suspend
, brcmstb_waketmr_resume
);
321 static const struct of_device_id brcmstb_waketmr_of_match
[] = {
322 { .compatible
= "brcm,brcmstb-waketimer" },
326 static struct platform_driver brcmstb_waketmr_driver
= {
327 .probe
= brcmstb_waketmr_probe
,
328 .remove
= brcmstb_waketmr_remove
,
330 .name
= "brcmstb-waketimer",
331 .pm
= &brcmstb_waketmr_pm_ops
,
332 .of_match_table
= of_match_ptr(brcmstb_waketmr_of_match
),
335 module_platform_driver(brcmstb_waketmr_driver
);
337 MODULE_LICENSE("GPL v2");
338 MODULE_AUTHOR("Brian Norris");
339 MODULE_AUTHOR("Markus Mayer");
340 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");