xref: /linux/drivers/rtc/rtc-brcmstb-waketimer.c (revision 6a34dfa15d6edf7e78b8118d862d2db0889cf669)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright © 2014-2023 Broadcom
4  */
5 
6 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
7 
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>
24 
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;
37 };
38 
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
45 
46 #define BRCMSTB_WKTMR_DEFAULT_FREQ	27000000
47 
48 static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr *timer)
49 {
50 	u32 reg;
51 
52 	reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
53 	return !!(reg & WKTMR_ALARM_EVENT);
54 }
55 
56 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer)
57 {
58 	u32 reg;
59 
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);
71 	}
72 }
73 
74 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer,
75 				      unsigned int secs)
76 {
77 	unsigned int now;
78 
79 	brcmstb_waketmr_clear_alarm(timer);
80 
81 	/* Make sure we are actually counting in seconds */
82 	writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER);
83 
84 	writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM);
85 	now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER);
86 
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);
92 	}
93 }
94 
95 static irqreturn_t brcmstb_waketmr_irq(int irq, void *data)
96 {
97 	struct brcmstb_waketmr *timer = data;
98 
99 	if (!timer->alarm_irq)
100 		pm_wakeup_event(timer->dev, 0);
101 	return IRQ_HANDLED;
102 }
103 
104 static irqreturn_t brcmstb_alarm_irq(int irq, void *data)
105 {
106 	struct brcmstb_waketmr *timer = data;
107 
108 	/* Ignore spurious interrupts */
109 	if (!brcmstb_waketmr_is_pending(timer))
110 		return IRQ_HANDLED;
111 
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);
119 		}
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);
124 	}
125 
126 	return IRQ_HANDLED;
127 }
128 
129 struct wktmr_time {
130 	u32 sec;
131 	u32 pre;
132 };
133 
134 static void wktmr_read(struct brcmstb_waketmr *timer,
135 		       struct wktmr_time *t)
136 {
137 	u32 tmp;
138 
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);
143 
144 	t->pre = timer->rate - tmp;
145 }
146 
147 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer)
148 {
149 	struct device *dev = timer->dev;
150 	int ret;
151 
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;
157 		}
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;
164 			}
165 		}
166 	}
167 
168 	return 0;
169 }
170 
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)
174 {
175 	struct brcmstb_waketmr *timer;
176 
177 	timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier);
178 
179 	/* Set timer for cold boot */
180 	if (action == SYS_POWER_OFF)
181 		brcmstb_waketmr_prepare_suspend(timer);
182 
183 	return NOTIFY_DONE;
184 }
185 
186 static int brcmstb_waketmr_gettime(struct device *dev,
187 				   struct rtc_time *tm)
188 {
189 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
190 	struct wktmr_time now;
191 
192 	wktmr_read(timer, &now);
193 
194 	rtc_time64_to_tm(now.sec, tm);
195 
196 	return 0;
197 }
198 
199 static int brcmstb_waketmr_settime(struct device *dev,
200 				   struct rtc_time *tm)
201 {
202 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
203 	time64_t sec;
204 
205 	sec = rtc_tm_to_time64(tm);
206 
207 	writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
208 
209 	return 0;
210 }
211 
212 static int brcmstb_waketmr_getalarm(struct device *dev,
213 				    struct rtc_wkalrm *alarm)
214 {
215 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
216 
217 	alarm->enabled = timer->alarm_en;
218 	rtc_time64_to_tm(timer->rtc_alarm, &alarm->time);
219 
220 	alarm->pending = brcmstb_waketmr_is_pending(timer);
221 
222 	return 0;
223 }
224 
225 static int brcmstb_waketmr_alarm_enable(struct device *dev,
226 					unsigned int enabled)
227 {
228 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
229 
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);
241 			}
242 			enable_irq(timer->alarm_irq);
243 		}
244 	} else if (!enabled && timer->alarm_en) {
245 		if (timer->alarm_irq)
246 			disable_irq(timer->alarm_irq);
247 		timer->alarm_en = false;
248 	}
249 
250 	return 0;
251 }
252 
253 static int brcmstb_waketmr_setalarm(struct device *dev,
254 				     struct rtc_wkalrm *alarm)
255 {
256 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
257 
258 	timer->rtc_alarm = rtc_tm_to_time64(&alarm->time);
259 
260 	brcmstb_waketmr_set_alarm(timer, timer->rtc_alarm);
261 
262 	return brcmstb_waketmr_alarm_enable(dev, alarm->enabled);
263 }
264 
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,
271 };
272 
273 static int brcmstb_waketmr_probe(struct platform_device *pdev)
274 {
275 	struct device *dev = &pdev->dev;
276 	struct brcmstb_waketmr *timer;
277 	int ret;
278 
279 	timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
280 	if (!timer)
281 		return -ENOMEM;
282 
283 	platform_set_drvdata(pdev, timer);
284 	timer->dev = dev;
285 
286 	timer->base = devm_platform_ioremap_resource(pdev, 0);
287 	if (IS_ERR(timer->base))
288 		return PTR_ERR(timer->base);
289 
290 	timer->rtc = devm_rtc_allocate_device(dev);
291 	if (IS_ERR(timer->rtc))
292 		return PTR_ERR(timer->rtc);
293 
294 	/*
295 	 * Set wakeup capability before requesting wakeup interrupt, so we can
296 	 * process boot-time "wakeups" (e.g., from S5 soft-off)
297 	 */
298 	device_init_wakeup(dev, true);
299 
300 	ret = platform_get_irq(pdev, 0);
301 	if (ret < 0)
302 		return -ENODEV;
303 	timer->wake_irq = (unsigned int)ret;
304 
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;
316 	}
317 
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;
322 
323 	brcmstb_waketmr_clear_alarm(timer);
324 
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;
334 	}
335 
336 	timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot;
337 	register_reboot_notifier(&timer->reboot_notifier);
338 
339 	timer->rtc->ops = &brcmstb_waketmr_ops;
340 	timer->rtc->range_max = U32_MAX;
341 
342 	ret = devm_rtc_register_device(timer->rtc);
343 	if (ret)
344 		goto err_notifier;
345 
346 	return 0;
347 
348 err_notifier:
349 	unregister_reboot_notifier(&timer->reboot_notifier);
350 
351 err_clk:
352 	clk_disable_unprepare(timer->clk);
353 
354 	return ret;
355 }
356 
357 static void brcmstb_waketmr_remove(struct platform_device *pdev)
358 {
359 	struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev);
360 
361 	unregister_reboot_notifier(&timer->reboot_notifier);
362 	clk_disable_unprepare(timer->clk);
363 }
364 
365 #ifdef CONFIG_PM_SLEEP
366 static int brcmstb_waketmr_suspend(struct device *dev)
367 {
368 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
369 
370 	return brcmstb_waketmr_prepare_suspend(timer);
371 }
372 
373 static int brcmstb_waketmr_suspend_noirq(struct device *dev)
374 {
375 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
376 
377 	/* Catch any alarms occurring prior to noirq */
378 	if (timer->alarm_expired && device_may_wakeup(dev))
379 		return -EBUSY;
380 
381 	return 0;
382 }
383 
384 static int brcmstb_waketmr_resume(struct device *dev)
385 {
386 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
387 	int ret;
388 
389 	if (!device_may_wakeup(dev))
390 		return 0;
391 
392 	ret = disable_irq_wake(timer->wake_irq);
393 	if (timer->alarm_en && timer->alarm_irq)
394 		disable_irq_wake(timer->alarm_irq);
395 
396 	brcmstb_waketmr_clear_alarm(timer);
397 
398 	return ret;
399 }
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 */
405 
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,
410 };
411 
412 static const __maybe_unused struct of_device_id brcmstb_waketmr_of_match[] = {
413 	{ .compatible = "brcm,brcmstb-waketimer" },
414 	{ /* sentinel */ },
415 };
416 
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),
424 	}
425 };
426 module_platform_driver(brcmstb_waketmr_driver);
427 
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");
433