Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / rtc / rtc-brcmstb-waketimer.c
blobfb47c32ab5ff4f89fc1512bf1a9f8278f84dc958
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright © 2014-2023 Broadcom
4 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/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;
27 struct device *dev;
28 void __iomem *base;
29 unsigned int wake_irq;
30 unsigned int alarm_irq;
31 struct notifier_block reboot_notifier;
32 struct clk *clk;
33 u32 rate;
34 unsigned long rtc_alarm;
35 bool alarm_en;
36 bool alarm_expired;
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)
50 u32 reg;
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)
58 u32 reg;
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,
75 unsigned int secs)
77 unsigned int now;
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)) {
89 secs = now + 1;
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);
101 return IRQ_HANDLED;
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))
110 return IRQ_HANDLED;
112 if (timer->alarm_en) {
113 if (device_may_wakeup(timer->dev)) {
114 disable_irq_nosync(irq);
115 timer->alarm_expired = true;
116 } else {
117 writel_relaxed(WKTMR_ALARM_EVENT,
118 timer->base + BRCMSTB_WKTMR_EVENT);
120 rtc_update_irq(timer->rtc, 1, RTC_IRQF | RTC_AF);
121 } else {
122 writel_relaxed(WKTMR_ALARM_EVENT,
123 timer->base + BRCMSTB_WKTMR_EVENT);
126 return IRQ_HANDLED;
129 struct wktmr_time {
130 u32 sec;
131 u32 pre;
134 static void wktmr_read(struct brcmstb_waketmr *timer,
135 struct wktmr_time *t)
137 u32 tmp;
139 do {
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;
150 int ret;
152 if (device_may_wakeup(dev)) {
153 ret = enable_irq_wake(timer->wake_irq);
154 if (ret) {
155 dev_err(dev, "failed to enable wake-up interrupt\n");
156 return ret;
158 if (timer->alarm_en && timer->alarm_irq) {
159 ret = enable_irq_wake(timer->alarm_irq);
160 if (ret) {
161 dev_err(dev, "failed to enable rtc interrupt\n");
162 disable_irq_wake(timer->wake_irq);
163 return ret;
168 return 0;
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);
183 return NOTIFY_DONE;
186 static int brcmstb_waketmr_gettime(struct device *dev,
187 struct rtc_time *tm)
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);
196 return 0;
199 static int brcmstb_waketmr_settime(struct device *dev,
200 struct rtc_time *tm)
202 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
203 time64_t sec;
205 sec = rtc_tm_to_time64(tm);
207 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
209 return 0;
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);
222 return 0;
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))
234 return -EINVAL;
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;
250 return 0;
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;
277 int ret;
279 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
280 if (!timer)
281 return -ENOMEM;
283 platform_set_drvdata(pdev, timer);
284 timer->dev = dev;
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);
301 if (ret < 0)
302 return -ENODEV;
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);
308 if (ret)
309 return ret;
310 timer->rate = clk_get_rate(timer->clk);
311 if (!timer->rate)
312 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
313 } else {
314 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
315 timer->clk = NULL;
318 ret = devm_request_irq(dev, timer->wake_irq, brcmstb_waketmr_irq, 0,
319 "brcmstb-waketimer", timer);
320 if (ret < 0)
321 goto err_clk;
323 brcmstb_waketmr_clear_alarm(timer);
325 /* Attempt to initialize non-wake irq */
326 ret = platform_get_irq(pdev, 1);
327 if (ret > 0) {
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",
331 timer);
332 if (ret < 0)
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);
343 if (ret)
344 goto err_notifier;
346 return 0;
348 err_notifier:
349 unregister_reboot_notifier(&timer->reboot_notifier);
351 err_clk:
352 clk_disable_unprepare(timer->clk);
354 return ret;
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))
379 return -EBUSY;
381 return 0;
384 static int brcmstb_waketmr_resume(struct device *dev)
386 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
387 int ret;
389 if (!device_may_wakeup(dev))
390 return 0;
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);
398 return ret;
400 #else
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" },
414 { /* sentinel */ },
417 static struct platform_driver brcmstb_waketmr_driver = {
418 .probe = brcmstb_waketmr_probe,
419 .remove = brcmstb_waketmr_remove,
420 .driver = {
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");