xref: /openbmc/linux/drivers/rtc/rtc-brcmstb-waketimer.c (revision 9c6d26df1fae6ad4718d51c48e6517913304ed27)
1 /*
2  * Copyright © 2014-2017 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 
14 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
15 
16 #include <linux/clk.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/irqreturn.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm.h>
28 #include <linux/pm_wakeup.h>
29 #include <linux/reboot.h>
30 #include <linux/rtc.h>
31 #include <linux/stat.h>
32 #include <linux/suspend.h>
33 
34 struct brcmstb_waketmr {
35 	struct rtc_device *rtc;
36 	struct device *dev;
37 	void __iomem *base;
38 	int irq;
39 	struct notifier_block reboot_notifier;
40 	struct clk *clk;
41 	u32 rate;
42 };
43 
44 #define BRCMSTB_WKTMR_EVENT		0x00
45 #define BRCMSTB_WKTMR_COUNTER		0x04
46 #define BRCMSTB_WKTMR_ALARM		0x08
47 #define BRCMSTB_WKTMR_PRESCALER		0x0C
48 #define BRCMSTB_WKTMR_PRESCALER_VAL	0x10
49 
50 #define BRCMSTB_WKTMR_DEFAULT_FREQ	27000000
51 
52 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer)
53 {
54 	writel_relaxed(1, timer->base + BRCMSTB_WKTMR_EVENT);
55 	(void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
56 }
57 
58 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer,
59 				      unsigned int secs)
60 {
61 	brcmstb_waketmr_clear_alarm(timer);
62 
63 	/* Make sure we are actually counting in seconds */
64 	writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER);
65 
66 	writel_relaxed(secs + 1, timer->base + BRCMSTB_WKTMR_ALARM);
67 }
68 
69 static irqreturn_t brcmstb_waketmr_irq(int irq, void *data)
70 {
71 	struct brcmstb_waketmr *timer = data;
72 
73 	pm_wakeup_event(timer->dev, 0);
74 
75 	return IRQ_HANDLED;
76 }
77 
78 struct wktmr_time {
79 	u32 sec;
80 	u32 pre;
81 };
82 
83 static void wktmr_read(struct brcmstb_waketmr *timer,
84 		       struct wktmr_time *t)
85 {
86 	u32 tmp;
87 
88 	do {
89 		t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER);
90 		tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL);
91 	} while (tmp >= timer->rate);
92 
93 	t->pre = timer->rate - tmp;
94 }
95 
96 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer)
97 {
98 	struct device *dev = timer->dev;
99 	int ret = 0;
100 
101 	if (device_may_wakeup(dev)) {
102 		ret = enable_irq_wake(timer->irq);
103 		if (ret) {
104 			dev_err(dev, "failed to enable wake-up interrupt\n");
105 			return ret;
106 		}
107 	}
108 
109 	return ret;
110 }
111 
112 /* If enabled as a wakeup-source, arm the timer when powering off */
113 static int brcmstb_waketmr_reboot(struct notifier_block *nb,
114 		unsigned long action, void *data)
115 {
116 	struct brcmstb_waketmr *timer;
117 
118 	timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier);
119 
120 	/* Set timer for cold boot */
121 	if (action == SYS_POWER_OFF)
122 		brcmstb_waketmr_prepare_suspend(timer);
123 
124 	return NOTIFY_DONE;
125 }
126 
127 static int brcmstb_waketmr_gettime(struct device *dev,
128 				   struct rtc_time *tm)
129 {
130 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
131 	struct wktmr_time now;
132 
133 	wktmr_read(timer, &now);
134 
135 	rtc_time_to_tm(now.sec, tm);
136 
137 	return 0;
138 }
139 
140 static int brcmstb_waketmr_settime(struct device *dev,
141 				   struct rtc_time *tm)
142 {
143 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
144 	time64_t sec;
145 
146 	sec = rtc_tm_to_time64(tm);
147 
148 	if (sec > U32_MAX || sec < 0)
149 		return -EINVAL;
150 
151 	writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
152 
153 	return 0;
154 }
155 
156 static int brcmstb_waketmr_getalarm(struct device *dev,
157 				    struct rtc_wkalrm *alarm)
158 {
159 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
160 	time64_t sec;
161 	u32 reg;
162 
163 	sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM);
164 	if (sec != 0) {
165 		/* Alarm is enabled */
166 		alarm->enabled = 1;
167 		rtc_time64_to_tm(sec, &alarm->time);
168 	}
169 
170 	reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
171 	alarm->pending = !!(reg & 1);
172 
173 	return 0;
174 }
175 
176 static int brcmstb_waketmr_setalarm(struct device *dev,
177 				     struct rtc_wkalrm *alarm)
178 {
179 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
180 	time64_t sec;
181 
182 	if (alarm->enabled)
183 		sec = rtc_tm_to_time64(&alarm->time);
184 	else
185 		sec = 0;
186 
187 	if (sec > U32_MAX || sec < 0)
188 		return -EINVAL;
189 
190 	brcmstb_waketmr_set_alarm(timer, sec);
191 
192 	return 0;
193 }
194 
195 /*
196  * Does not do much but keep the RTC class happy. We always support
197  * alarms.
198  */
199 static int brcmstb_waketmr_alarm_enable(struct device *dev,
200 					unsigned int enabled)
201 {
202 	return 0;
203 }
204 
205 static const struct rtc_class_ops brcmstb_waketmr_ops = {
206 	.read_time	= brcmstb_waketmr_gettime,
207 	.set_time	= brcmstb_waketmr_settime,
208 	.read_alarm	= brcmstb_waketmr_getalarm,
209 	.set_alarm	= brcmstb_waketmr_setalarm,
210 	.alarm_irq_enable = brcmstb_waketmr_alarm_enable,
211 };
212 
213 static int brcmstb_waketmr_probe(struct platform_device *pdev)
214 {
215 	struct device *dev = &pdev->dev;
216 	struct brcmstb_waketmr *timer;
217 	struct resource *res;
218 	int ret;
219 
220 	timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
221 	if (!timer)
222 		return -ENOMEM;
223 
224 	platform_set_drvdata(pdev, timer);
225 	timer->dev = dev;
226 
227 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
228 	timer->base = devm_ioremap_resource(dev, res);
229 	if (IS_ERR(timer->base))
230 		return PTR_ERR(timer->base);
231 
232 	/*
233 	 * Set wakeup capability before requesting wakeup interrupt, so we can
234 	 * process boot-time "wakeups" (e.g., from S5 soft-off)
235 	 */
236 	device_set_wakeup_capable(dev, true);
237 	device_wakeup_enable(dev);
238 
239 	timer->irq = platform_get_irq(pdev, 0);
240 	if (timer->irq < 0)
241 		return -ENODEV;
242 
243 	timer->clk = devm_clk_get(dev, NULL);
244 	if (!IS_ERR(timer->clk)) {
245 		ret = clk_prepare_enable(timer->clk);
246 		if (ret)
247 			return ret;
248 		timer->rate = clk_get_rate(timer->clk);
249 		if (!timer->rate)
250 			timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
251 	} else {
252 		timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
253 		timer->clk = NULL;
254 	}
255 
256 	ret = devm_request_irq(dev, timer->irq, brcmstb_waketmr_irq, 0,
257 			       "brcmstb-waketimer", timer);
258 	if (ret < 0)
259 		goto err_clk;
260 
261 	timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot;
262 	register_reboot_notifier(&timer->reboot_notifier);
263 
264 	timer->rtc = rtc_device_register("brcmstb-waketmr", dev,
265 					 &brcmstb_waketmr_ops, THIS_MODULE);
266 	if (IS_ERR(timer->rtc)) {
267 		dev_err(dev, "unable to register device\n");
268 		ret = PTR_ERR(timer->rtc);
269 		goto err_notifier;
270 	}
271 
272 	dev_info(dev, "registered, with irq %d\n", timer->irq);
273 
274 	return 0;
275 
276 err_notifier:
277 	unregister_reboot_notifier(&timer->reboot_notifier);
278 
279 err_clk:
280 	if (timer->clk)
281 		clk_disable_unprepare(timer->clk);
282 
283 	return ret;
284 }
285 
286 static int brcmstb_waketmr_remove(struct platform_device *pdev)
287 {
288 	struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev);
289 
290 	unregister_reboot_notifier(&timer->reboot_notifier);
291 	rtc_device_unregister(timer->rtc);
292 
293 	return 0;
294 }
295 
296 #ifdef CONFIG_PM_SLEEP
297 static int brcmstb_waketmr_suspend(struct device *dev)
298 {
299 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
300 
301 	return brcmstb_waketmr_prepare_suspend(timer);
302 }
303 
304 static int brcmstb_waketmr_resume(struct device *dev)
305 {
306 	struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
307 	int ret;
308 
309 	if (!device_may_wakeup(dev))
310 		return 0;
311 
312 	ret = disable_irq_wake(timer->irq);
313 
314 	brcmstb_waketmr_clear_alarm(timer);
315 
316 	return ret;
317 }
318 #endif /* CONFIG_PM_SLEEP */
319 
320 static SIMPLE_DEV_PM_OPS(brcmstb_waketmr_pm_ops,
321 			 brcmstb_waketmr_suspend, brcmstb_waketmr_resume);
322 
323 static const struct of_device_id brcmstb_waketmr_of_match[] = {
324 	{ .compatible = "brcm,brcmstb-waketimer" },
325 	{ /* sentinel */ },
326 };
327 
328 static struct platform_driver brcmstb_waketmr_driver = {
329 	.probe			= brcmstb_waketmr_probe,
330 	.remove			= brcmstb_waketmr_remove,
331 	.driver = {
332 		.name		= "brcmstb-waketimer",
333 		.pm		= &brcmstb_waketmr_pm_ops,
334 		.of_match_table	= of_match_ptr(brcmstb_waketmr_of_match),
335 	}
336 };
337 module_platform_driver(brcmstb_waketmr_driver);
338 
339 MODULE_LICENSE("GPL v2");
340 MODULE_AUTHOR("Brian Norris");
341 MODULE_AUTHOR("Markus Mayer");
342 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");
343