Lines Matching refs:timer
48 static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr *timer) in brcmstb_waketmr_is_pending() argument
52 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_is_pending()
56 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer) in brcmstb_waketmr_clear_alarm() argument
60 if (timer->alarm_en && timer->alarm_irq) in brcmstb_waketmr_clear_alarm()
61 disable_irq(timer->alarm_irq); in brcmstb_waketmr_clear_alarm()
62 timer->alarm_en = false; in brcmstb_waketmr_clear_alarm()
63 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_clear_alarm()
64 writel_relaxed(reg - 1, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_clear_alarm()
65 writel_relaxed(WKTMR_ALARM_EVENT, timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_clear_alarm()
66 (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_waketmr_clear_alarm()
67 if (timer->alarm_expired) { in brcmstb_waketmr_clear_alarm()
68 timer->alarm_expired = false; in brcmstb_waketmr_clear_alarm()
70 enable_irq(timer->alarm_irq); in brcmstb_waketmr_clear_alarm()
74 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer, in brcmstb_waketmr_set_alarm() argument
79 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_set_alarm()
82 writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER); in brcmstb_waketmr_set_alarm()
84 writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_set_alarm()
85 now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_set_alarm()
88 !brcmstb_waketmr_is_pending(timer)) { in brcmstb_waketmr_set_alarm()
90 writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); in brcmstb_waketmr_set_alarm()
91 now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_set_alarm()
97 struct brcmstb_waketmr *timer = data; in brcmstb_waketmr_irq() local
99 if (!timer->alarm_irq) in brcmstb_waketmr_irq()
100 pm_wakeup_event(timer->dev, 0); in brcmstb_waketmr_irq()
106 struct brcmstb_waketmr *timer = data; in brcmstb_alarm_irq() local
109 if (!brcmstb_waketmr_is_pending(timer)) in brcmstb_alarm_irq()
112 if (timer->alarm_en) { in brcmstb_alarm_irq()
113 if (device_may_wakeup(timer->dev)) { in brcmstb_alarm_irq()
115 timer->alarm_expired = true; in brcmstb_alarm_irq()
118 timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_alarm_irq()
120 rtc_update_irq(timer->rtc, 1, RTC_IRQF | RTC_AF); in brcmstb_alarm_irq()
123 timer->base + BRCMSTB_WKTMR_EVENT); in brcmstb_alarm_irq()
134 static void wktmr_read(struct brcmstb_waketmr *timer, in wktmr_read() argument
140 t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); in wktmr_read()
141 tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL); in wktmr_read()
142 } while (tmp >= timer->rate); in wktmr_read()
144 t->pre = timer->rate - tmp; in wktmr_read()
147 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer) in brcmstb_waketmr_prepare_suspend() argument
149 struct device *dev = timer->dev; in brcmstb_waketmr_prepare_suspend()
153 ret = enable_irq_wake(timer->wake_irq); in brcmstb_waketmr_prepare_suspend()
158 if (timer->alarm_en && timer->alarm_irq) { in brcmstb_waketmr_prepare_suspend()
159 ret = enable_irq_wake(timer->alarm_irq); in brcmstb_waketmr_prepare_suspend()
162 disable_irq_wake(timer->wake_irq); in brcmstb_waketmr_prepare_suspend()
175 struct brcmstb_waketmr *timer; in brcmstb_waketmr_reboot() local
177 timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier); in brcmstb_waketmr_reboot()
181 brcmstb_waketmr_prepare_suspend(timer); in brcmstb_waketmr_reboot()
189 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_gettime() local
192 wktmr_read(timer, &now); in brcmstb_waketmr_gettime()
202 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_settime() local
207 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER); in brcmstb_waketmr_settime()
215 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_getalarm() local
217 alarm->enabled = timer->alarm_en; in brcmstb_waketmr_getalarm()
218 rtc_time64_to_tm(timer->rtc_alarm, &alarm->time); in brcmstb_waketmr_getalarm()
220 alarm->pending = brcmstb_waketmr_is_pending(timer); in brcmstb_waketmr_getalarm()
228 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_alarm_enable() local
230 if (enabled && !timer->alarm_en) { in brcmstb_waketmr_alarm_enable()
231 if ((int)(readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER) - in brcmstb_waketmr_alarm_enable()
232 readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM)) >= 0 && in brcmstb_waketmr_alarm_enable()
233 !brcmstb_waketmr_is_pending(timer)) in brcmstb_waketmr_alarm_enable()
235 timer->alarm_en = true; in brcmstb_waketmr_alarm_enable()
236 if (timer->alarm_irq) { in brcmstb_waketmr_alarm_enable()
237 if (timer->alarm_expired) { in brcmstb_waketmr_alarm_enable()
238 timer->alarm_expired = false; in brcmstb_waketmr_alarm_enable()
240 enable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
242 enable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
244 } else if (!enabled && timer->alarm_en) { in brcmstb_waketmr_alarm_enable()
245 if (timer->alarm_irq) in brcmstb_waketmr_alarm_enable()
246 disable_irq(timer->alarm_irq); in brcmstb_waketmr_alarm_enable()
247 timer->alarm_en = false; in brcmstb_waketmr_alarm_enable()
256 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_setalarm() local
258 timer->rtc_alarm = rtc_tm_to_time64(&alarm->time); in brcmstb_waketmr_setalarm()
260 brcmstb_waketmr_set_alarm(timer, timer->rtc_alarm); in brcmstb_waketmr_setalarm()
276 struct brcmstb_waketmr *timer; in brcmstb_waketmr_probe() local
279 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL); in brcmstb_waketmr_probe()
280 if (!timer) in brcmstb_waketmr_probe()
283 platform_set_drvdata(pdev, timer); in brcmstb_waketmr_probe()
284 timer->dev = dev; in brcmstb_waketmr_probe()
286 timer->base = devm_platform_ioremap_resource(pdev, 0); in brcmstb_waketmr_probe()
287 if (IS_ERR(timer->base)) in brcmstb_waketmr_probe()
288 return PTR_ERR(timer->base); in brcmstb_waketmr_probe()
290 timer->rtc = devm_rtc_allocate_device(dev); in brcmstb_waketmr_probe()
291 if (IS_ERR(timer->rtc)) in brcmstb_waketmr_probe()
292 return PTR_ERR(timer->rtc); in brcmstb_waketmr_probe()
303 timer->wake_irq = (unsigned int)ret; in brcmstb_waketmr_probe()
305 timer->clk = devm_clk_get(dev, NULL); in brcmstb_waketmr_probe()
306 if (!IS_ERR(timer->clk)) { in brcmstb_waketmr_probe()
307 ret = clk_prepare_enable(timer->clk); in brcmstb_waketmr_probe()
310 timer->rate = clk_get_rate(timer->clk); in brcmstb_waketmr_probe()
311 if (!timer->rate) in brcmstb_waketmr_probe()
312 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ; in brcmstb_waketmr_probe()
314 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ; in brcmstb_waketmr_probe()
315 timer->clk = NULL; in brcmstb_waketmr_probe()
318 ret = devm_request_irq(dev, timer->wake_irq, brcmstb_waketmr_irq, 0, in brcmstb_waketmr_probe()
319 "brcmstb-waketimer", timer); in brcmstb_waketmr_probe()
323 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_probe()
328 timer->alarm_irq = (unsigned int)ret; in brcmstb_waketmr_probe()
329 ret = devm_request_irq(dev, timer->alarm_irq, brcmstb_alarm_irq, in brcmstb_waketmr_probe()
331 timer); in brcmstb_waketmr_probe()
333 timer->alarm_irq = 0; in brcmstb_waketmr_probe()
336 timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot; in brcmstb_waketmr_probe()
337 register_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_probe()
339 timer->rtc->ops = &brcmstb_waketmr_ops; in brcmstb_waketmr_probe()
340 timer->rtc->range_max = U32_MAX; in brcmstb_waketmr_probe()
342 ret = devm_rtc_register_device(timer->rtc); in brcmstb_waketmr_probe()
349 unregister_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_probe()
352 clk_disable_unprepare(timer->clk); in brcmstb_waketmr_probe()
359 struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev); in brcmstb_waketmr_remove() local
361 unregister_reboot_notifier(&timer->reboot_notifier); in brcmstb_waketmr_remove()
362 clk_disable_unprepare(timer->clk); in brcmstb_waketmr_remove()
368 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_suspend() local
370 return brcmstb_waketmr_prepare_suspend(timer); in brcmstb_waketmr_suspend()
375 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_suspend_noirq() local
378 if (timer->alarm_expired && device_may_wakeup(dev)) in brcmstb_waketmr_suspend_noirq()
386 struct brcmstb_waketmr *timer = dev_get_drvdata(dev); in brcmstb_waketmr_resume() local
392 ret = disable_irq_wake(timer->wake_irq); in brcmstb_waketmr_resume()
393 if (timer->alarm_en && timer->alarm_irq) in brcmstb_waketmr_resume()
394 disable_irq_wake(timer->alarm_irq); in brcmstb_waketmr_resume()
396 brcmstb_waketmr_clear_alarm(timer); in brcmstb_waketmr_resume()