perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / drivers / rtc / rtc-brcmstb-waketimer.c
blob2f65943867f517a9d1cebc2ed349d022fb23da34
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright © 2014-2017 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 int irq;
31 struct notifier_block reboot_notifier;
32 struct clk *clk;
33 u32 rate;
36 #define BRCMSTB_WKTMR_EVENT 0x00
37 #define BRCMSTB_WKTMR_COUNTER 0x04
38 #define BRCMSTB_WKTMR_ALARM 0x08
39 #define BRCMSTB_WKTMR_PRESCALER 0x0C
40 #define BRCMSTB_WKTMR_PRESCALER_VAL 0x10
42 #define BRCMSTB_WKTMR_DEFAULT_FREQ 27000000
44 static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer)
46 writel_relaxed(1, timer->base + BRCMSTB_WKTMR_EVENT);
47 (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
50 static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer,
51 unsigned int secs)
53 brcmstb_waketmr_clear_alarm(timer);
55 /* Make sure we are actually counting in seconds */
56 writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER);
58 writel_relaxed(secs + 1, timer->base + BRCMSTB_WKTMR_ALARM);
61 static irqreturn_t brcmstb_waketmr_irq(int irq, void *data)
63 struct brcmstb_waketmr *timer = data;
65 pm_wakeup_event(timer->dev, 0);
67 return IRQ_HANDLED;
70 struct wktmr_time {
71 u32 sec;
72 u32 pre;
75 static void wktmr_read(struct brcmstb_waketmr *timer,
76 struct wktmr_time *t)
78 u32 tmp;
80 do {
81 t->sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER);
82 tmp = readl_relaxed(timer->base + BRCMSTB_WKTMR_PRESCALER_VAL);
83 } while (tmp >= timer->rate);
85 t->pre = timer->rate - tmp;
88 static int brcmstb_waketmr_prepare_suspend(struct brcmstb_waketmr *timer)
90 struct device *dev = timer->dev;
91 int ret = 0;
93 if (device_may_wakeup(dev)) {
94 ret = enable_irq_wake(timer->irq);
95 if (ret) {
96 dev_err(dev, "failed to enable wake-up interrupt\n");
97 return ret;
101 return ret;
104 /* If enabled as a wakeup-source, arm the timer when powering off */
105 static int brcmstb_waketmr_reboot(struct notifier_block *nb,
106 unsigned long action, void *data)
108 struct brcmstb_waketmr *timer;
110 timer = container_of(nb, struct brcmstb_waketmr, reboot_notifier);
112 /* Set timer for cold boot */
113 if (action == SYS_POWER_OFF)
114 brcmstb_waketmr_prepare_suspend(timer);
116 return NOTIFY_DONE;
119 static int brcmstb_waketmr_gettime(struct device *dev,
120 struct rtc_time *tm)
122 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
123 struct wktmr_time now;
125 wktmr_read(timer, &now);
127 rtc_time64_to_tm(now.sec, tm);
129 return 0;
132 static int brcmstb_waketmr_settime(struct device *dev,
133 struct rtc_time *tm)
135 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
136 time64_t sec;
138 sec = rtc_tm_to_time64(tm);
140 writel_relaxed(sec, timer->base + BRCMSTB_WKTMR_COUNTER);
142 return 0;
145 static int brcmstb_waketmr_getalarm(struct device *dev,
146 struct rtc_wkalrm *alarm)
148 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
149 time64_t sec;
150 u32 reg;
152 sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM);
153 if (sec != 0) {
154 /* Alarm is enabled */
155 alarm->enabled = 1;
156 rtc_time64_to_tm(sec, &alarm->time);
159 reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT);
160 alarm->pending = !!(reg & 1);
162 return 0;
165 static int brcmstb_waketmr_setalarm(struct device *dev,
166 struct rtc_wkalrm *alarm)
168 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
169 time64_t sec;
171 if (alarm->enabled)
172 sec = rtc_tm_to_time64(&alarm->time);
173 else
174 sec = 0;
176 brcmstb_waketmr_set_alarm(timer, sec);
178 return 0;
182 * Does not do much but keep the RTC class happy. We always support
183 * alarms.
185 static int brcmstb_waketmr_alarm_enable(struct device *dev,
186 unsigned int enabled)
188 return 0;
191 static const struct rtc_class_ops brcmstb_waketmr_ops = {
192 .read_time = brcmstb_waketmr_gettime,
193 .set_time = brcmstb_waketmr_settime,
194 .read_alarm = brcmstb_waketmr_getalarm,
195 .set_alarm = brcmstb_waketmr_setalarm,
196 .alarm_irq_enable = brcmstb_waketmr_alarm_enable,
199 static int brcmstb_waketmr_probe(struct platform_device *pdev)
201 struct device *dev = &pdev->dev;
202 struct brcmstb_waketmr *timer;
203 struct resource *res;
204 int ret;
206 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
207 if (!timer)
208 return -ENOMEM;
210 platform_set_drvdata(pdev, timer);
211 timer->dev = dev;
213 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 timer->base = devm_ioremap_resource(dev, res);
215 if (IS_ERR(timer->base))
216 return PTR_ERR(timer->base);
218 timer->rtc = devm_rtc_allocate_device(dev);
219 if (IS_ERR(timer->rtc))
220 return PTR_ERR(timer->rtc);
223 * Set wakeup capability before requesting wakeup interrupt, so we can
224 * process boot-time "wakeups" (e.g., from S5 soft-off)
226 device_set_wakeup_capable(dev, true);
227 device_wakeup_enable(dev);
229 timer->irq = platform_get_irq(pdev, 0);
230 if (timer->irq < 0)
231 return -ENODEV;
233 timer->clk = devm_clk_get(dev, NULL);
234 if (!IS_ERR(timer->clk)) {
235 ret = clk_prepare_enable(timer->clk);
236 if (ret)
237 return ret;
238 timer->rate = clk_get_rate(timer->clk);
239 if (!timer->rate)
240 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
241 } else {
242 timer->rate = BRCMSTB_WKTMR_DEFAULT_FREQ;
243 timer->clk = NULL;
246 ret = devm_request_irq(dev, timer->irq, brcmstb_waketmr_irq, 0,
247 "brcmstb-waketimer", timer);
248 if (ret < 0)
249 goto err_clk;
251 timer->reboot_notifier.notifier_call = brcmstb_waketmr_reboot;
252 register_reboot_notifier(&timer->reboot_notifier);
254 timer->rtc->ops = &brcmstb_waketmr_ops;
255 timer->rtc->range_max = U32_MAX;
257 ret = rtc_register_device(timer->rtc);
258 if (ret) {
259 dev_err(dev, "unable to register device\n");
260 goto err_notifier;
263 dev_info(dev, "registered, with irq %d\n", timer->irq);
265 return 0;
267 err_notifier:
268 unregister_reboot_notifier(&timer->reboot_notifier);
270 err_clk:
271 if (timer->clk)
272 clk_disable_unprepare(timer->clk);
274 return ret;
277 static int brcmstb_waketmr_remove(struct platform_device *pdev)
279 struct brcmstb_waketmr *timer = dev_get_drvdata(&pdev->dev);
281 unregister_reboot_notifier(&timer->reboot_notifier);
283 return 0;
286 #ifdef CONFIG_PM_SLEEP
287 static int brcmstb_waketmr_suspend(struct device *dev)
289 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
291 return brcmstb_waketmr_prepare_suspend(timer);
294 static int brcmstb_waketmr_resume(struct device *dev)
296 struct brcmstb_waketmr *timer = dev_get_drvdata(dev);
297 int ret;
299 if (!device_may_wakeup(dev))
300 return 0;
302 ret = disable_irq_wake(timer->irq);
304 brcmstb_waketmr_clear_alarm(timer);
306 return ret;
308 #endif /* CONFIG_PM_SLEEP */
310 static SIMPLE_DEV_PM_OPS(brcmstb_waketmr_pm_ops,
311 brcmstb_waketmr_suspend, brcmstb_waketmr_resume);
313 static const struct of_device_id brcmstb_waketmr_of_match[] = {
314 { .compatible = "brcm,brcmstb-waketimer" },
315 { /* sentinel */ },
318 static struct platform_driver brcmstb_waketmr_driver = {
319 .probe = brcmstb_waketmr_probe,
320 .remove = brcmstb_waketmr_remove,
321 .driver = {
322 .name = "brcmstb-waketimer",
323 .pm = &brcmstb_waketmr_pm_ops,
324 .of_match_table = of_match_ptr(brcmstb_waketmr_of_match),
327 module_platform_driver(brcmstb_waketmr_driver);
329 MODULE_LICENSE("GPL v2");
330 MODULE_AUTHOR("Brian Norris");
331 MODULE_AUTHOR("Markus Mayer");
332 MODULE_DESCRIPTION("Wake-up timer driver for STB chips");