1 /* drivers/rtc/rtc-goldfish.c
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (C) 2017 Imagination Technologies Ltd.
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.
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.
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/rtc.h>
22 #define TIMER_TIME_LOW 0x00 /* get low bits of current time */
23 /* and update TIMER_TIME_HIGH */
24 #define TIMER_TIME_HIGH 0x04 /* get high bits of time at last */
25 /* TIMER_TIME_LOW read */
26 #define TIMER_ALARM_LOW 0x08 /* set low bits of alarm and */
28 #define TIMER_ALARM_HIGH 0x0c /* set high bits of next alarm */
29 #define TIMER_IRQ_ENABLED 0x10
30 #define TIMER_CLEAR_ALARM 0x14
31 #define TIMER_ALARM_STATUS 0x18
32 #define TIMER_CLEAR_INTERRUPT 0x1c
37 struct rtc_device *rtc;
40 static int goldfish_rtc_read_alarm(struct device *dev,
41 struct rtc_wkalrm *alrm)
47 struct goldfish_rtc *rtcdrv;
49 rtcdrv = dev_get_drvdata(dev);
52 rtc_alarm_low = readl(base + TIMER_ALARM_LOW);
53 rtc_alarm_high = readl(base + TIMER_ALARM_HIGH);
54 rtc_alarm = (rtc_alarm_high << 32) | rtc_alarm_low;
56 do_div(rtc_alarm, NSEC_PER_SEC);
57 memset(alrm, 0, sizeof(struct rtc_wkalrm));
59 rtc_time_to_tm(rtc_alarm, &alrm->time);
61 if (readl(base + TIMER_ALARM_STATUS))
69 static int goldfish_rtc_set_alarm(struct device *dev,
70 struct rtc_wkalrm *alrm)
72 struct goldfish_rtc *rtcdrv;
73 unsigned long rtc_alarm;
79 rtcdrv = dev_get_drvdata(dev);
83 ret = rtc_tm_to_time(&alrm->time, &rtc_alarm);
87 rtc_alarm64 = rtc_alarm * NSEC_PER_SEC;
88 writel((rtc_alarm64 >> 32), base + TIMER_ALARM_HIGH);
89 writel(rtc_alarm64, base + TIMER_ALARM_LOW);
90 writel(1, base + TIMER_IRQ_ENABLED);
93 * if this function was called with enabled=0
94 * then it could mean that the application is
95 * trying to cancel an ongoing alarm
97 rtc_status_reg = readl(base + TIMER_ALARM_STATUS);
99 writel(1, base + TIMER_CLEAR_ALARM);
105 static int goldfish_rtc_alarm_irq_enable(struct device *dev,
106 unsigned int enabled)
109 struct goldfish_rtc *rtcdrv;
111 rtcdrv = dev_get_drvdata(dev);
115 writel(1, base + TIMER_IRQ_ENABLED);
117 writel(0, base + TIMER_IRQ_ENABLED);
122 static irqreturn_t goldfish_rtc_interrupt(int irq, void *dev_id)
124 struct goldfish_rtc *rtcdrv = dev_id;
125 void __iomem *base = rtcdrv->base;
127 writel(1, base + TIMER_CLEAR_INTERRUPT);
129 rtc_update_irq(rtcdrv->rtc, 1, RTC_IRQF | RTC_AF);
134 static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
136 struct goldfish_rtc *rtcdrv;
142 rtcdrv = dev_get_drvdata(dev);
145 time_low = readl(base + TIMER_TIME_LOW);
146 time_high = readl(base + TIMER_TIME_HIGH);
147 time = (time_high << 32) | time_low;
149 do_div(time, NSEC_PER_SEC);
151 rtc_time_to_tm(time, tm);
156 static int goldfish_rtc_set_time(struct device *dev, struct rtc_time *tm)
158 struct goldfish_rtc *rtcdrv;
164 rtcdrv = dev_get_drvdata(dev);
167 ret = rtc_tm_to_time(tm, &now);
169 now64 = now * NSEC_PER_SEC;
170 writel((now64 >> 32), base + TIMER_TIME_HIGH);
171 writel(now64, base + TIMER_TIME_LOW);
177 static const struct rtc_class_ops goldfish_rtc_ops = {
178 .read_time = goldfish_rtc_read_time,
179 .set_time = goldfish_rtc_set_time,
180 .read_alarm = goldfish_rtc_read_alarm,
181 .set_alarm = goldfish_rtc_set_alarm,
182 .alarm_irq_enable = goldfish_rtc_alarm_irq_enable
185 static int goldfish_rtc_probe(struct platform_device *pdev)
187 struct goldfish_rtc *rtcdrv;
191 rtcdrv = devm_kzalloc(&pdev->dev, sizeof(*rtcdrv), GFP_KERNEL);
195 platform_set_drvdata(pdev, rtcdrv);
197 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 rtcdrv->base = devm_ioremap_resource(&pdev->dev, r);
202 if (IS_ERR(rtcdrv->base))
205 rtcdrv->irq = platform_get_irq(pdev, 0);
209 rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
212 if (IS_ERR(rtcdrv->rtc))
213 return PTR_ERR(rtcdrv->rtc);
215 err = devm_request_irq(&pdev->dev, rtcdrv->irq,
216 goldfish_rtc_interrupt,
217 0, pdev->name, rtcdrv);
224 static const struct of_device_id goldfish_rtc_of_match[] = {
225 { .compatible = "google,goldfish-rtc", },
228 MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
230 static struct platform_driver goldfish_rtc = {
231 .probe = goldfish_rtc_probe,
233 .name = "goldfish_rtc",
234 .of_match_table = goldfish_rtc_of_match,
238 module_platform_driver(goldfish_rtc);
240 MODULE_LICENSE("GPL v2");