1 // SPDX-License-Identifier: GPL-2.0
2 /* drivers/rtc/rtc-goldfish.c
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (C) 2017 Imagination Technologies Ltd.
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/rtc.h>
13 #include <linux/goldfish.h>
14 #include <clocksource/timer-goldfish.h>
19 struct rtc_device
*rtc
;
22 static int goldfish_rtc_read_alarm(struct device
*dev
,
23 struct rtc_wkalrm
*alrm
)
29 struct goldfish_rtc
*rtcdrv
;
31 rtcdrv
= dev_get_drvdata(dev
);
34 rtc_alarm_low
= gf_ioread32(base
+ TIMER_ALARM_LOW
);
35 rtc_alarm_high
= gf_ioread32(base
+ TIMER_ALARM_HIGH
);
36 rtc_alarm
= (rtc_alarm_high
<< 32) | rtc_alarm_low
;
38 do_div(rtc_alarm
, NSEC_PER_SEC
);
39 memset(alrm
, 0, sizeof(struct rtc_wkalrm
));
41 rtc_time64_to_tm(rtc_alarm
, &alrm
->time
);
43 if (gf_ioread32(base
+ TIMER_ALARM_STATUS
))
51 static int goldfish_rtc_set_alarm(struct device
*dev
,
52 struct rtc_wkalrm
*alrm
)
54 struct goldfish_rtc
*rtcdrv
;
59 rtcdrv
= dev_get_drvdata(dev
);
63 rtc_alarm64
= rtc_tm_to_time64(&alrm
->time
) * NSEC_PER_SEC
;
64 gf_iowrite32((rtc_alarm64
>> 32), base
+ TIMER_ALARM_HIGH
);
65 gf_iowrite32(rtc_alarm64
, base
+ TIMER_ALARM_LOW
);
66 gf_iowrite32(1, base
+ TIMER_IRQ_ENABLED
);
69 * if this function was called with enabled=0
70 * then it could mean that the application is
71 * trying to cancel an ongoing alarm
73 rtc_status_reg
= gf_ioread32(base
+ TIMER_ALARM_STATUS
);
75 gf_iowrite32(1, base
+ TIMER_CLEAR_ALARM
);
81 static int goldfish_rtc_alarm_irq_enable(struct device
*dev
,
85 struct goldfish_rtc
*rtcdrv
;
87 rtcdrv
= dev_get_drvdata(dev
);
91 gf_iowrite32(1, base
+ TIMER_IRQ_ENABLED
);
93 gf_iowrite32(0, base
+ TIMER_IRQ_ENABLED
);
98 static irqreturn_t
goldfish_rtc_interrupt(int irq
, void *dev_id
)
100 struct goldfish_rtc
*rtcdrv
= dev_id
;
101 void __iomem
*base
= rtcdrv
->base
;
103 gf_iowrite32(1, base
+ TIMER_CLEAR_INTERRUPT
);
105 rtc_update_irq(rtcdrv
->rtc
, 1, RTC_IRQF
| RTC_AF
);
110 static int goldfish_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
112 struct goldfish_rtc
*rtcdrv
;
118 rtcdrv
= dev_get_drvdata(dev
);
121 time_low
= gf_ioread32(base
+ TIMER_TIME_LOW
);
122 time_high
= gf_ioread32(base
+ TIMER_TIME_HIGH
);
123 time
= (time_high
<< 32) | time_low
;
125 do_div(time
, NSEC_PER_SEC
);
127 rtc_time64_to_tm(time
, tm
);
132 static int goldfish_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
134 struct goldfish_rtc
*rtcdrv
;
138 rtcdrv
= dev_get_drvdata(dev
);
141 now64
= rtc_tm_to_time64(tm
) * NSEC_PER_SEC
;
142 gf_iowrite32((now64
>> 32), base
+ TIMER_TIME_HIGH
);
143 gf_iowrite32(now64
, base
+ TIMER_TIME_LOW
);
148 static const struct rtc_class_ops goldfish_rtc_ops
= {
149 .read_time
= goldfish_rtc_read_time
,
150 .set_time
= goldfish_rtc_set_time
,
151 .read_alarm
= goldfish_rtc_read_alarm
,
152 .set_alarm
= goldfish_rtc_set_alarm
,
153 .alarm_irq_enable
= goldfish_rtc_alarm_irq_enable
156 static int goldfish_rtc_probe(struct platform_device
*pdev
)
158 struct goldfish_rtc
*rtcdrv
;
161 rtcdrv
= devm_kzalloc(&pdev
->dev
, sizeof(*rtcdrv
), GFP_KERNEL
);
165 platform_set_drvdata(pdev
, rtcdrv
);
166 rtcdrv
->base
= devm_platform_ioremap_resource(pdev
, 0);
167 if (IS_ERR(rtcdrv
->base
))
168 return PTR_ERR(rtcdrv
->base
);
170 rtcdrv
->irq
= platform_get_irq(pdev
, 0);
174 rtcdrv
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
175 if (IS_ERR(rtcdrv
->rtc
))
176 return PTR_ERR(rtcdrv
->rtc
);
178 rtcdrv
->rtc
->ops
= &goldfish_rtc_ops
;
179 rtcdrv
->rtc
->range_max
= U64_MAX
/ NSEC_PER_SEC
;
181 err
= devm_request_irq(&pdev
->dev
, rtcdrv
->irq
,
182 goldfish_rtc_interrupt
,
183 0, pdev
->name
, rtcdrv
);
187 return devm_rtc_register_device(rtcdrv
->rtc
);
190 static const struct of_device_id goldfish_rtc_of_match
[] = {
191 { .compatible
= "google,goldfish-rtc", },
194 MODULE_DEVICE_TABLE(of
, goldfish_rtc_of_match
);
196 static struct platform_driver goldfish_rtc
= {
197 .probe
= goldfish_rtc_probe
,
199 .name
= "goldfish_rtc",
200 .of_match_table
= goldfish_rtc_of_match
,
204 module_platform_driver(goldfish_rtc
);
206 MODULE_DESCRIPTION("Android Goldfish Real Time Clock driver");
207 MODULE_LICENSE("GPL v2");