Lines Matching full:timer
49 static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr *timer) in brcmstb_waketmr_is_pending() argument
53 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_is_pending()
57 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer) in brcmstb_waketmr_clear_alarm() argument
61 if (timer->alarm_en && timer->alarm_irq) in brcmstb_waketmr_clear_alarm()
62 disable_irq(timer->alarm_irq); in brcmstb_waketmr_clear_alarm()
63 timer->alarm_en = false; in brcmstb_waketmr_clear_alarm()
64 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_clear_alarm()
65 writel_relaxed(reg - 1, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_clear_alarm()
66 writel_relaxed(WKTMR_ALARM_EVENT, timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_clear_alarm()
67 (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_clear_alarm()
68 if (timer->alarm_expired) { in brcmstb_waketmr_clear_alarm()
69 timer->alarm_expired = false; in brcmstb_waketmr_clear_alarm()
71 enable_irq(timer->alarm_irq); in brcmstb_waketmr_clear_alarm()
75 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer, in brcmstb_waketmr_set_alarm() argument
80 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_set_alarm()
83 writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER); in brcmstb_waketmr_set_alarm()
85 writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_set_alarm()
86 now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_set_alarm()
89 !brcmstb_waketmr_is_pending(timer)) { in brcmstb_waketmr_set_alarm()
91 writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_set_alarm()
92 now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_set_alarm()
98 struct brcmstb_waketmr *timer = data; in brcmstb_waketmr_irq() local
100 if (!timer->alarm_irq) in brcmstb_waketmr_irq()
101 pm_wakeup_event(timer->dev, 0); in brcmstb_waketmr_irq()
107 struct brcmstb_waketmr *timer = data; in brcmstb_alarm_irq() local
110 if (!brcmstb_waketmr_is_pending(timer)) in brcmstb_alarm_irq()
113 if (timer->alarm_en) { in brcmstb_alarm_irq()
114 if (device_may_wakeup(timer->dev)) { in brcmstb_alarm_irq()
116 timer->alarm_expired = true; in brcmstb_alarm_irq()
119 timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_alarm_irq()
121 rtc_update_irq(timer->rtc, 1, RTC_IRQF | RTC_AF); in brcmstb_alarm_irq()
124 timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_alarm_irq()
135 static void wktmr_read(struct brcmstb_waketmr *timer, in wktmr_read() argument
141 t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in wktmr_read()
142 tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL); in wktmr_read()
143 } while (tmp >= timer->rate); in wktmr_read()
145 t->pre = timer->rate - tmp; in wktmr_read()
148 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer) in brcmstb_waketmr_prepare_suspend() argument
150 struct device *dev = timer->dev; in brcmstb_waketmr_prepare_suspend()
154 ret = enable_irq_wake(timer->wake_irq); in brcmstb_waketmr_prepare_suspend()
159 if (timer->alarm_en && timer->alarm_irq) { in brcmstb_waketmr_prepare_suspend()
160 ret = enable_irq_wake(timer->alarm_irq); in brcmstb_waketmr_prepare_suspend()
163 disable_irq_wake(timer->wake_irq); in brcmstb_waketmr_prepare_suspend()
172 /* If enabled as a wakeup-source, arm the timer when powering off */
176 struct brcmstb_waketmr *timer; in brcmstb_waketmr_reboot() local
178 timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier); in brcmstb_waketmr_reboot()
180 /* Set timer for cold boot */ in brcmstb_waketmr_reboot()
182 brcmstb_waketmr_prepare_suspend(timer); in brcmstb_waketmr_reboot()
190 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_gettime() local
193 wktmr_read(timer, &now); in brcmstb_waketmr_gettime()
203 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_settime() local
208 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_settime()
216 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_getalarm() local
218 alarm->enabled = timer->alarm_en; in brcmstb_waketmr_getalarm()
219 rtc_time64_to_tm(timer->rtc_alarm, &alarm->time); in brcmstb_waketmr_getalarm()
221 alarm->pending = brcmstb_waketmr_is_pending(timer); in brcmstb_waketmr_getalarm()
229 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_alarm_enable() local
231 if (enabled && !timer->alarm_en) { in brcmstb_waketmr_alarm_enable()
232 if ((int)(readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER) - in brcmstb_waketmr_alarm_enable()
233 readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM)) >= 0 && in brcmstb_waketmr_alarm_enable()
234 !brcmstb_waketmr_is_pending(timer)) in brcmstb_waketmr_alarm_enable()
236 timer->alarm_en = true; in brcmstb_waketmr_alarm_enable()
237 if (timer->alarm_irq) { in brcmstb_waketmr_alarm_enable()
238 if (timer->alarm_expired) { in brcmstb_waketmr_alarm_enable()
239 timer->alarm_expired = false; in brcmstb_waketmr_alarm_enable()
241 enable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
243 enable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
245 } else if (!enabled && timer->alarm_en) { in brcmstb_waketmr_alarm_enable()
246 if (timer->alarm_irq) in brcmstb_waketmr_alarm_enable()
247 disable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
248 timer->alarm_en = false; in brcmstb_waketmr_alarm_enable()
257 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_setalarm() local
259 timer->rtc_alarm = rtc_tm_to_time64(&alarm->time); in brcmstb_waketmr_setalarm()
261 brcmstb_waketmr_set_alarm(timer, timer->rtc_alarm); in brcmstb_waketmr_setalarm()
277 struct brcmstb_waketmr *timer; in brcmstb_waketmr_probe() local
280 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL); in brcmstb_waketmr_probe()
281 if (!timer) in brcmstb_waketmr_probe()
284 platform_set_drvdata(pdev, timer); in brcmstb_waketmr_probe()
285 timer->dev = dev; in brcmstb_waketmr_probe()
287 timer->base = devm_platform_ioremap_resource(pdev, 0); in brcmstb_waketmr_probe()
288 if (IS_ERR(timer->base)) in brcmstb_waketmr_probe()
289 return PTR_ERR(timer->base); in brcmstb_waketmr_probe()
291 timer->rtc = devm_rtc_allocate_device(dev); in brcmstb_waketmr_probe()
292 if (IS_ERR(timer->rtc)) in brcmstb_waketmr_probe()
293 return PTR_ERR(timer->rtc); in brcmstb_waketmr_probe()
304 timer->wake_irq = (unsigned int)ret; in brcmstb_waketmr_probe()
306 timer->clk = devm_clk_get(dev, NULL); in brcmstb_waketmr_probe()
307 if (!IS_ERR(timer->clk)) { in brcmstb_waketmr_probe()
308 ret = clk_prepare_enable(timer->clk); in brcmstb_waketmr_probe()
311 timer->rate = clk_get_rate(timer->clk); in brcmstb_waketmr_probe()
312 if (!timer->rate) in brcmstb_waketmr_probe()
313 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ; in brcmstb_waketmr_probe()
315 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ; in brcmstb_waketmr_probe()
316 timer->clk = NULL; in brcmstb_waketmr_probe()
319 ret = devm_request_irq(dev, timer->wake_irq, brcmstb_waketmr_irq, 0, in brcmstb_waketmr_probe()
320 "brcmstb-waketimer", timer); in brcmstb_waketmr_probe()
324 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_probe()
329 timer->alarm_irq = (unsigned int)ret; in brcmstb_waketmr_probe()
330 ret = devm_request_irq(dev, timer->alarm_irq, brcmstb_alarm_irq, in brcmstb_waketmr_probe()
332 timer); in brcmstb_waketmr_probe()
334 timer->alarm_irq = 0; in brcmstb_waketmr_probe()
337 timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot; in brcmstb_waketmr_probe()
338 register_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_probe()
340 timer->rtc->ops = &brcmstb_waketmr_ops; in brcmstb_waketmr_probe()
341 timer->rtc->range_max = U32_MAX; in brcmstb_waketmr_probe()
343 ret = devm_rtc_register_device(timer->rtc); in brcmstb_waketmr_probe()
350 unregister_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_probe()
353 clk_disable_unprepare(timer->clk); in brcmstb_waketmr_probe()
360 struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev); in brcmstb_waketmr_remove() local
362 unregister_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_remove()
363 clk_disable_unprepare(timer->clk); in brcmstb_waketmr_remove()
369 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_suspend() local
371 return brcmstb_waketmr_prepare_suspend(timer); in brcmstb_waketmr_suspend()
376 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_suspend_noirq() local
379 if (timer->alarm_expired && device_may_wakeup(dev)) in brcmstb_waketmr_suspend_noirq()
387 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_resume() local
393 ret = disable_irq_wake(timer->wake_irq); in brcmstb_waketmr_resume()
394 if (timer->alarm_en && timer->alarm_irq) in brcmstb_waketmr_resume()
395 disable_irq_wake(timer->alarm_irq); in brcmstb_waketmr_resume()
397 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_resume()
433 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");