blob: e21d2e1f162389fb566577833ef8326781a23134 [file] [log] [blame]
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +02001/* drivers/rtc/rtc-goldfish.c
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2017 Imagination Technologies Ltd.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
Alexandre Bellonibd013862019-03-20 13:34:14 +010017#include <linux/io.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020018#include <linux/module.h>
Alexandre Belloni6a6ec8c2019-03-20 13:34:15 +010019#include <linux/of.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020020#include <linux/platform_device.h>
21#include <linux/rtc.h>
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020022
23#define TIMER_TIME_LOW 0x00 /* get low bits of current time */
24 /* and update TIMER_TIME_HIGH */
25#define TIMER_TIME_HIGH 0x04 /* get high bits of time at last */
26 /* TIMER_TIME_LOW read */
27#define TIMER_ALARM_LOW 0x08 /* set low bits of alarm and */
28 /* activate it */
29#define TIMER_ALARM_HIGH 0x0c /* set high bits of next alarm */
30#define TIMER_IRQ_ENABLED 0x10
31#define TIMER_CLEAR_ALARM 0x14
32#define TIMER_ALARM_STATUS 0x18
33#define TIMER_CLEAR_INTERRUPT 0x1c
34
35struct goldfish_rtc {
36 void __iomem *base;
37 int irq;
38 struct rtc_device *rtc;
39};
40
41static int goldfish_rtc_read_alarm(struct device *dev,
42 struct rtc_wkalrm *alrm)
43{
44 u64 rtc_alarm;
45 u64 rtc_alarm_low;
46 u64 rtc_alarm_high;
47 void __iomem *base;
48 struct goldfish_rtc *rtcdrv;
49
50 rtcdrv = dev_get_drvdata(dev);
51 base = rtcdrv->base;
52
53 rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
54 rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
55 rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
56
57 do_div(rtc_alarm, NSEC_PER_SEC);
58 memset(alrm, 0, sizeof(struct rtc_wkalrm));
59
Alexandre Bellonib5093062019-03-20 13:34:16 +010060 rtc_time64_to_tm(rtc_alarm, &alrm->time);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020061
62 if (readl(base + TIMER_ALARM_STATUS))
63 alrm->enabled = 1;
64 else
65 alrm->enabled = 0;
66
67 return 0;
68}
69
70static int goldfish_rtc_set_alarm(struct device *dev,
71 struct rtc_wkalrm *alrm)
72{
73 struct goldfish_rtc *rtcdrv;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020074 u64 rtc_alarm64;
75 u64 rtc_status_reg;
76 void __iomem *base;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020077
78 rtcdrv = dev_get_drvdata(dev);
79 base = rtcdrv->base;
80
81 if (alrm->enabled) {
Alexandre Bellonib5093062019-03-20 13:34:16 +010082 rtc_alarm64 = rtc_tm_to_time64(&alrm->time) * NSEC_PER_SEC;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020083 writel((rtc_alarm64 >> 32), base + TIMER_ALARM_HIGH);
84 writel(rtc_alarm64, base + TIMER_ALARM_LOW);
85 } else {
86 /*
87 * if this function was called with enabled=0
88 * then it could mean that the application is
89 * trying to cancel an ongoing alarm
90 */
91 rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
92 if (rtc_status_reg)
93 writel(1, base + TIMER_CLEAR_ALARM);
94 }
95
Alexandre Bellonib5093062019-03-20 13:34:16 +010096 return 0;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +020097}
98
99static int goldfish_rtc_alarm_irq_enable(struct device *dev,
100 unsigned int enabled)
101{
102 void __iomem *base;
103 struct goldfish_rtc *rtcdrv;
104
105 rtcdrv = dev_get_drvdata(dev);
106 base = rtcdrv->base;
107
108 if (enabled)
109 writel(1, base + TIMER_IRQ_ENABLED);
110 else
111 writel(0, base + TIMER_IRQ_ENABLED);
112
113 return 0;
114}
115
116static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
117{
118 struct goldfish_rtc *rtcdrv = dev_id;
119 void __iomem *base = rtcdrv->base;
120
121 writel(1, base + TIMER_CLEAR_INTERRUPT);
122
123 rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
124
125 return IRQ_HANDLED;
126}
127
128static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
129{
130 struct goldfish_rtc *rtcdrv;
131 void __iomem *base;
132 u64 time_high;
133 u64 time_low;
134 u64 time;
135
136 rtcdrv = dev_get_drvdata(dev);
137 base = rtcdrv->base;
138
139 time_low = readl(base + TIMER_TIME_LOW);
140 time_high = readl(base + TIMER_TIME_HIGH);
141 time = (time_high << 32) | time_low;
142
143 do_div(time, NSEC_PER_SEC);
144
Alexandre Bellonib5093062019-03-20 13:34:16 +0100145 rtc_time64_to_tm(time, tm);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200146
147 return 0;
148}
149
150static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
151{
152 struct goldfish_rtc *rtcdrv;
153 void __iomem *base;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200154 u64 now64;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200155
156 rtcdrv = dev_get_drvdata(dev);
157 base = rtcdrv->base;
158
Alexandre Bellonib5093062019-03-20 13:34:16 +0100159 now64 = rtc_tm_to_time64(tm) * NSEC_PER_SEC;
160 writel((now64 >> 32), base + TIMER_TIME_HIGH);
161 writel(now64, base + TIMER_TIME_LOW);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200162
Alexandre Bellonib5093062019-03-20 13:34:16 +0100163 return 0;
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200164}
165
166static const struct rtc_class_ops goldfish_rtc_ops = {
167 .read_time = goldfish_rtc_read_time,
168 .set_time = goldfish_rtc_set_time,
169 .read_alarm = goldfish_rtc_read_alarm,
170 .set_alarm = goldfish_rtc_set_alarm,
171 .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
172};
173
174static int goldfish_rtc_probe(struct platform_device *pdev)
175{
176 struct goldfish_rtc *rtcdrv;
177 struct resource *r;
178 int err;
179
180 rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
181 if (!rtcdrv)
182 return -ENOMEM;
183
184 platform_set_drvdata(pdev, rtcdrv);
185
186 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
187 if (!r)
188 return -ENODEV;
189
190 rtcdrv->base = devm_ioremap_resource(&pdev->dev, r);
191 if (IS_ERR(rtcdrv->base))
192 return -ENODEV;
193
194 rtcdrv->irq = platform_get_irq(pdev, 0);
195 if (rtcdrv->irq < 0)
196 return -ENODEV;
197
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100198 rtcdrv->rtc = devm_rtc_allocate_device(&pdev->dev);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200199 if (IS_ERR(rtcdrv->rtc))
200 return PTR_ERR(rtcdrv->rtc);
201
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100202 rtcdrv->rtc->ops = &goldfish_rtc_ops;
Alexandre Belloni5e2954f2019-03-20 13:34:13 +0100203 rtcdrv->rtc->range_max = U64_MAX / NSEC_PER_SEC;
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100204
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200205 err = devm_request_irq(&pdev->dev, rtcdrv->irq,
206 goldfish_rtc_interrupt,
207 0, pdev->name, rtcdrv);
208 if (err)
209 return err;
210
Alexandre Belloni409b84e2019-03-20 13:34:12 +0100211 return rtc_register_device(rtcdrv->rtc);
Miodrag Dinicf22d9cd2017-08-18 15:08:54 +0200212}
213
214static const struct of_device_id goldfish_rtc_of_match[] = {
215 { .compatible = "google,goldfish-rtc", },
216 {},
217};
218MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
219
220static struct platform_driver goldfish_rtc = {
221 .probe = goldfish_rtc_probe,
222 .driver = {
223 .name = "goldfish_rtc",
224 .of_match_table = goldfish_rtc_of_match,
225 }
226};
227
228module_platform_driver(goldfish_rtc);
James Hogan82d632b2018-01-16 14:45:21 +0000229
230MODULE_LICENSE("GPL v2");