blob: cb6b0ad7ec3f26e04c19b19e424cb44e409e5e25 [file] [log] [blame]
Alexandre Belloni2be7f1b2019-03-20 13:34:17 +01001// SPDX-License-Identifier: GPL-2.0
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +02002/* drivers/rtc/rtc-goldfish.c
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (C) 2017 Imagination Technologies Ltd.
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +02006 */
7
Alexandre Bellonibd013862019-03-20 13:34:14 +01008#include <linux/io.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +02009#include <linux/module.h>
Alexandre Belloni6a6ec8c2019-03-20 13:34:15 +010010#include <linux/of.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020011#include <linux/platform_device.h>
12#include <linux/rtc.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020013
14#define TIMER_TIME_LOW 0x00 /* get low bits of current time */
15 /* and update TIMER_TIME_HIGH */
16#define TIMER_TIME_HIGH 0x04 /* get high bits of time at last */
17 /* TIMER_TIME_LOW read */
18#define TIMER_ALARM_LOW 0x08 /* set low bits of alarm and */
19 /* activate it */
20#define TIMER_ALARM_HIGH 0x0c /* set high bits of next alarm */
21#define TIMER_IRQ_ENABLED 0x10
22#define TIMER_CLEAR_ALARM 0x14
23#define TIMER_ALARM_STATUS 0x18
24#define TIMER_CLEAR_INTERRUPT 0x1c
25
26struct goldfish_rtc {
27 void __iomem *base;
28 int irq;
29 struct rtc_device *rtc;
30};
31
32static int goldfish_rtc_read_alarm(struct device *dev,
33 struct rtc_wkalrm *alrm)
34{
35 u64 rtc_alarm;
36 u64 rtc_alarm_low;
37 u64 rtc_alarm_high;
38 void __iomem *base;
39 struct goldfish_rtc *rtcdrv;
40
41 rtcdrv = dev_get_drvdata(dev);
42 base = rtcdrv->base;
43
44 rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
45 rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
46 rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
47
48 do_div(rtc_alarm, NSEC_PER_SEC);
49 memset(alrm, 0, sizeof(struct rtc_wkalrm));
50
Alexandre Bellonib5093062019-03-20 13:34:16 +010051 rtc_time64_to_tm(rtc_alarm, &alrm->time);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020052
53 if (readl(base + TIMER_ALARM_STATUS))
54 alrm->enabled = 1;
55 else
56 alrm->enabled = 0;
57
58 return 0;
59}
60
61static int goldfish_rtc_set_alarm(struct device *dev,
62 struct rtc_wkalrm *alrm)
63{
64 struct goldfish_rtc *rtcdrv;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020065 u64 rtc_alarm64;
66 u64 rtc_status_reg;
67 void __iomem *base;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020068
69 rtcdrv = dev_get_drvdata(dev);
70 base = rtcdrv->base;
71
72 if (alrm->enabled) {
Alexandre Bellonib5093062019-03-20 13:34:16 +010073 rtc_alarm64 = rtc_tm_to_time64(&alrm->time) * NSEC_PER_SEC;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020074 writel((rtc_alarm64 >> 32), base + TIMER_ALARM_HIGH);
75 writel(rtc_alarm64, base + TIMER_ALARM_LOW);
76 } else {
77 /*
78 * if this function was called with enabled=0
79 * then it could mean that the application is
80 * trying to cancel an ongoing alarm
81 */
82 rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
83 if (rtc_status_reg)
84 writel(1, base + TIMER_CLEAR_ALARM);
85 }
86
Alexandre Bellonib5093062019-03-20 13:34:16 +010087 return 0;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020088}
89
90static int goldfish_rtc_alarm_irq_enable(struct device *dev,
91 unsigned int enabled)
92{
93 void __iomem *base;
94 struct goldfish_rtc *rtcdrv;
95
96 rtcdrv = dev_get_drvdata(dev);
97 base = rtcdrv->base;
98
99 if (enabled)
100 writel(1, base + TIMER_IRQ_ENABLED);
101 else
102 writel(0, base + TIMER_IRQ_ENABLED);
103
104 return 0;
105}
106
107static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
108{
109 struct goldfish_rtc *rtcdrv = dev_id;
110 void __iomem *base = rtcdrv->base;
111
112 writel(1, base + TIMER_CLEAR_INTERRUPT);
113
114 rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
115
116 return IRQ_HANDLED;
117}
118
119static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
120{
121 struct goldfish_rtc *rtcdrv;
122 void __iomem *base;
123 u64 time_high;
124 u64 time_low;
125 u64 time;
126
127 rtcdrv = dev_get_drvdata(dev);
128 base = rtcdrv->base;
129
130 time_low = readl(base + TIMER_TIME_LOW);
131 time_high = readl(base + TIMER_TIME_HIGH);
132 time = (time_high << 32) | time_low;
133
134 do_div(time, NSEC_PER_SEC);
135
Alexandre Bellonib5093062019-03-20 13:34:16 +0100136 rtc_time64_to_tm(time, tm);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200137
138 return 0;
139}
140
141static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
142{
143 struct goldfish_rtc *rtcdrv;
144 void __iomem *base;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200145 u64 now64;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200146
147 rtcdrv = dev_get_drvdata(dev);
148 base = rtcdrv->base;
149
Alexandre Bellonib5093062019-03-20 13:34:16 +0100150 now64 = rtc_tm_to_time64(tm) * NSEC_PER_SEC;
151 writel((now64 >> 32), base + TIMER_TIME_HIGH);
152 writel(now64, base + TIMER_TIME_LOW);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200153
Alexandre Bellonib5093062019-03-20 13:34:16 +0100154 return 0;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200155}
156
157static const struct rtc_class_ops goldfish_rtc_ops = {
158 .read_time = goldfish_rtc_read_time,
159 .set_time = goldfish_rtc_set_time,
160 .read_alarm = goldfish_rtc_read_alarm,
161 .set_alarm = goldfish_rtc_set_alarm,
162 .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
163};
164
165static int goldfish_rtc_probe(struct platform_device *pdev)
166{
167 struct goldfish_rtc *rtcdrv;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200168 int err;
169
170 rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
171 if (!rtcdrv)
172 return -ENOMEM;
173
174 platform_set_drvdata(pdev, rtcdrv);
Markus Elfring89576be2019-09-21 11:49:01 +0200175 rtcdrv->base = devm_platform_ioremap_resource(pdev, 0);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200176 if (IS_ERR(rtcdrv->base))
177 return -ENODEV;
178
179 rtcdrv->irq = platform_get_irq(pdev, 0);
180 if (rtcdrv->irq < 0)
181 return -ENODEV;
182
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100183 rtcdrv->rtc = devm_rtc_allocate_device(&pdev->dev);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200184 if (IS_ERR(rtcdrv->rtc))
185 return PTR_ERR(rtcdrv->rtc);
186
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100187 rtcdrv->rtc->ops = &goldfish_rtc_ops;
Alexandre Belloni5e2954f2019-03-20 13:34:13 +0100188 rtcdrv->rtc->range_max = U64_MAX / NSEC_PER_SEC;
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100189
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200190 err = devm_request_irq(&pdev->dev, rtcdrv->irq,
191 goldfish_rtc_interrupt,
192 0, pdev->name, rtcdrv);
193 if (err)
194 return err;
195
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100196 return rtc_register_device(rtcdrv->rtc);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200197}
198
199static const struct of_device_id goldfish_rtc_of_match[] = {
200 { .compatible = "google,goldfish-rtc", },
201 {},
202};
203MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
204
205static struct platform_driver goldfish_rtc = {
206 .probe = goldfish_rtc_probe,
207 .driver = {
208 .name = "goldfish_rtc",
209 .of_match_table = goldfish_rtc_of_match,
210 }
211};
212
213module_platform_driver(goldfish_rtc);
James Hogan82d632b2018-01-16 14:45:21 +0000214
215MODULE_LICENSE("GPL v2");