drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / rtc / rtc-brcmstb-waketimer.c
blob1a65a4e0dc00350bcf86812d46f811fbc912956f
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/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;
28 struct device *dev;
29 void __iomem *base;
30 unsigned int wake_irq;
31 unsigned int alarm_irq;
32 struct notifier_block reboot_notifier;
33 struct clk *clk;
34 u32 rate;
35 unsigned long rtc_alarm;
36 bool alarm_en;
37 bool alarm_expired;
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)
51 u32 reg;
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)
59 u32 reg;
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,
76 unsigned int secs)
78 unsigned int now;
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)) {
90 secs = now + 1;
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);
102 return IRQ_HANDLED;
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))
111 return IRQ_HANDLED;
113 if (timer->alarm_en) {
114 if (device_may_wakeup(timer->dev)) {
115 disable_irq_nosync(irq);
116 timer->alarm_expired = true;
117 } else {
118 writel_relaxed(WKTMR_ALARM_EVENT,
119 timer->base + BRCMSTB_WKTMR_EVENT);
121 rtc_update_irq(timer->rtc, 1, RTC_IRQF | RTC_AF);
122 } else {
123 writel_relaxed(WKTMR_ALARM_EVENT,
124 timer->base + BRCMSTB_WKTMR_EVENT);
127 return IRQ_HANDLED;
130 struct wktmr_time {
131 u32 sec;
132 u32 pre;
135 static void wktmr_read(struct brcmstb_waketmr *timer,
136 struct wktmr_time *t)
138 u32 tmp;
140 do {
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;
151 int ret;
153 if (device_may_wakeup(dev)) {
154 ret = enable_irq_wake(timer->wake_irq);
155 if (ret) {
156 dev_err(dev, "failed to enable wake-up interrupt\n");
157 return ret;
159 if (timer->alarm_en && timer->alarm_irq) {
160 ret = enable_irq_wake(timer->alarm_irq);
161 if (ret) {
162 dev_err(dev, "failed to enable rtc interrupt\n");
163 disable_irq_wake(timer->wake_irq);
164 return ret;
169 return 0;
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);
184 return NOTIFY_DONE;
187 static int brcmstb_waketmr_gettime(struct device *dev,
188 struct rtc_time *tm)
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);
197 return 0;
200 static int brcmstb_waketmr_settime(struct device *dev,
201 struct rtc_time *tm)
203 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
204 time64_t sec;
206 sec = rtc_tm_to_time64(tm);
208 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
210 return 0;
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);
223 return 0;
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))
235 return -EINVAL;
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;
251 return 0;
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;
278 int ret;
280 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
281 if (!timer)
282 return -ENOMEM;
284 platform_set_drvdata(pdev, timer);
285 timer->dev = dev;
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);
302 if (ret < 0)
303 return -ENODEV;
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);
309 if (ret)
310 return ret;
311 timer->rate = clk_get_rate(timer->clk);
312 if (!timer->rate)
313 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
314 } else {
315 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
316 timer->clk = NULL;
319 ret = devm_request_irq(dev, timer->wake_irq, brcmstb_waketmr_irq, 0,
320 "brcmstb-waketimer", timer);
321 if (ret < 0)
322 goto err_clk;
324 brcmstb_waketmr_clear_alarm(timer);
326 /* Attempt to initialize non-wake irq */
327 ret = platform_get_irq(pdev, 1);
328 if (ret > 0) {
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",
332 timer);
333 if (ret < 0)
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);
344 if (ret)
345 goto err_notifier;
347 return 0;
349 err_notifier:
350 unregister_reboot_notifier(&timer->reboot_notifier);
352 err_clk:
353 clk_disable_unprepare(timer->clk);
355 return ret;
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))
380 return -EBUSY;
382 return 0;
385 static int brcmstb_waketmr_resume(struct device *dev)
387 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
388 int ret;
390 if (!device_may_wakeup(dev))
391 return 0;
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);
399 return ret;
401 #else
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" },
415 { /* sentinel */ },
418 static struct platform_driver brcmstb_waketmr_driver = {
419 .probe = brcmstb_waketmr_probe,
420 .remove_new = brcmstb_waketmr_remove,
421 .driver = {
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");