• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* drivers/rtc/rtc-goldfish.c
2 **
3 ** Copyright (C) 2007 Google, Inc.
4 **
5 ** This software is licensed under the terms of the GNU General Public
6 ** License version 2, as published by the Free Software Foundation, and
7 ** may be copied, distributed, and modified under those terms.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 */
15 
16 #include <asm/io.h>
17 #include <linux/acpi.h>
18 #include <linux/export.h>
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/rtc.h>
24 #include <linux/slab.h>
25 
26 enum {
27 	TIMER_TIME_LOW          = 0x00, // get low bits of current time and update TIMER_TIME_HIGH
28 	TIMER_TIME_HIGH         = 0x04, // get high bits of time at last TIMER_TIME_LOW read
29 	TIMER_ALARM_LOW         = 0x08, // set low bits of alarm and activate it
30 	TIMER_ALARM_HIGH        = 0x0c, // set high bits of next alarm
31 	TIMER_CLEAR_INTERRUPT   = 0x10,
32 	TIMER_CLEAR_ALARM       = 0x14
33 };
34 
35 struct goldfish_rtc {
36 	void __iomem *base;
37 	uint32_t irq;
38 	struct rtc_device *rtc;
39 };
40 
41 static irqreturn_t
goldfish_rtc_interrupt(int irq,void * dev_id)42 goldfish_rtc_interrupt(int irq, void *dev_id)
43 {
44 	struct goldfish_rtc	*qrtc = dev_id;
45 	unsigned long		events = 0;
46 	void __iomem *base = qrtc->base;
47 
48 	writel(1, base + TIMER_CLEAR_INTERRUPT);
49 	events = RTC_IRQF | RTC_AF;
50 
51 	rtc_update_irq(qrtc->rtc, 1, events);
52 
53 	return IRQ_HANDLED;
54 }
55 
goldfish_rtc_read_time(struct device * dev,struct rtc_time * tm)56 static int goldfish_rtc_read_time(struct device *dev, struct rtc_time *tm)
57 {
58 	int64_t time;
59 	struct goldfish_rtc	*qrtc = platform_get_drvdata(to_platform_device(dev));
60 	void __iomem *base = qrtc->base;
61 
62 	time = readl(base + TIMER_TIME_LOW);
63 	time |= (int64_t)readl(base + TIMER_TIME_HIGH) << 32;
64 	do_div(time, NSEC_PER_SEC);
65 
66 	rtc_time_to_tm(time, tm);
67 	return 0;
68 }
69 
70 static struct rtc_class_ops goldfish_rtc_ops = {
71 //	.ioctl		= goldfish_rtc_ioctl,
72 	.read_time	= goldfish_rtc_read_time,
73 //	.set_time	= goldfish_rtc_set_time,
74 //	.read_alarm	= goldfish_rtc_read_alarm,
75 //	.set_alarm	= goldfish_rtc_set_alarm,
76 };
77 
78 
goldfish_rtc_probe(struct platform_device * pdev)79 static int goldfish_rtc_probe(struct platform_device *pdev)
80 {
81 	int ret;
82 	struct resource *r;
83 	struct goldfish_rtc *qrtc;
84 	unsigned long rtc_dev_len;
85 	unsigned long rtc_dev_addr;
86 
87 	qrtc = kzalloc(sizeof(*qrtc), GFP_KERNEL);
88 	if(qrtc == NULL) {
89 		ret = -ENOMEM;
90 		goto err_qrtc_alloc_failed;
91 	}
92 	platform_set_drvdata(pdev, qrtc);
93 
94 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 	if(r == NULL) {
96 		ret = -ENODEV;
97 		goto err_no_io_base;
98 	}
99 
100 	rtc_dev_addr = r->start;
101 	rtc_dev_len = resource_size(r);
102 	qrtc->base = ioremap(rtc_dev_addr, rtc_dev_len);
103 	qrtc->irq = platform_get_irq(pdev, 0);
104 	if(qrtc->irq < 0) {
105 		ret = -ENODEV;
106 		goto err_no_irq;
107 	}
108 	qrtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
109 	                                &goldfish_rtc_ops, THIS_MODULE);
110 	if (IS_ERR(qrtc->rtc)) {
111 		ret = PTR_ERR(qrtc->rtc);
112 		goto err_rtc_device_register_failed;
113 	}
114 
115 	ret = request_irq(qrtc->irq, goldfish_rtc_interrupt, 0, pdev->name, qrtc);
116 	if(ret)
117 		goto request_irq;
118 
119 	return 0;
120 
121 	free_irq(qrtc->irq, qrtc);
122 request_irq:
123 	rtc_device_unregister(qrtc->rtc);
124 err_rtc_device_register_failed:
125 err_no_irq:
126 err_no_io_base:
127 	kfree(qrtc);
128 err_qrtc_alloc_failed:
129 	return ret;
130 }
131 
goldfish_rtc_remove(struct platform_device * pdev)132 static int goldfish_rtc_remove(struct platform_device *pdev)
133 {
134 	struct goldfish_rtc	*qrtc = platform_get_drvdata(pdev);
135 	free_irq(qrtc->irq, qrtc);
136 	rtc_device_unregister(qrtc->rtc);
137 	kfree(qrtc);
138 	return 0;
139 }
140 
141 static const struct of_device_id goldfish_rtc_of_match[] = {
142 	{ .compatible = "generic,goldfish-rtc", },
143 	{},
144 };
145 MODULE_DEVICE_TABLE(of, goldfish_rtc_of_match);
146 
147 static const struct acpi_device_id goldfish_rtc_acpi_match[] = {
148 	{ "GFSH0007", 0 },
149 	{ },
150 };
151 MODULE_DEVICE_TABLE(acpi, golfish_rtc_acpi_match);
152 
153 static struct platform_driver goldfish_rtc = {
154 	.probe = goldfish_rtc_probe,
155 	.remove = goldfish_rtc_remove,
156 	.driver = {
157 		.name = "goldfish_rtc",
158 		.of_match_table = goldfish_rtc_of_match,
159 		.acpi_match_table = ACPI_PTR(goldfish_rtc_acpi_match),
160 	}
161 };
162 
163 module_platform_driver(goldfish_rtc);
164 
165