gemini: add Linux 4.4 support
[openwrt/openwrt.git] / target / linux / gemini / files / drivers / rtc / rtc-gemini.c
1 /*
2 * Gemini OnChip RTC
3 *
4 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Original code for older kernel 2.6.15 are form Stormlinksemi
21 * first update from Janos Laube for > 2.6.29 kernels
22 *
23 * checkpatch fixes and usage off rtc-lib code
24 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
25 */
26
27 #include <linux/rtc.h>
28 #include <linux/io.h>
29 #include <linux/slab.h>
30 #include <linux/platform_device.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include <mach/hardware.h>
35
36 #define DRV_NAME "rtc-gemini"
37
38 MODULE_DESCRIPTION("RTC driver for Gemini SoC");
39 MODULE_ALIAS("platform:" DRV_NAME);
40 MODULE_AUTHOR("Hans Ulli Kroll <ulli.kroll@googlemail.com>");
41 MODULE_LICENSE("GPL");
42
43 struct gemini_rtc {
44 struct rtc_device *dev;
45 void __iomem *base;
46 int irq;
47 };
48
49 enum gemini_rtc_offsets {
50 GEMINI_RTC_SECOND = 0x00,
51 GEMINI_RTC_MINUTE = 0x04,
52 GEMINI_RTC_HOUR = 0x08,
53 GEMINI_RTC_DAYS = 0x0C,
54 GEMINI_RTC_ALARM_SECOND = 0x10,
55 GEMINI_RTC_ALARM_MINUTE = 0x14,
56 GEMINI_RTC_ALARM_HOUR = 0x18,
57 GEMINI_RTC_RECORD = 0x1C,
58 GEMINI_RTC_CR = 0x20
59 };
60
61 static irqreturn_t gemini_rtc_interrupt(int irq, void *dev)
62 {
63 return IRQ_HANDLED;
64 }
65
66 /*
67 * Looks like the RTC in the Gemini SoC is (totaly) broken
68 * We can't read/write directly the time from RTC registers.
69 * We must do some "offset" calculation to get the real time
70 *
71 * The register "day" seams to be fixed, and the register "hour"
72 * has his own mind.
73 *
74 * Maybe we can write directly the hour and days since EPOCH
75 * but in this case the RTC will recalucate to some (other) strange values.
76 * If you write time to the registers you will not read the same values.
77 *
78 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
79 * the same thing, without the rtc-lib.c calls.
80 */
81
82 static int gemini_rtc_read_time(struct device *dev, struct rtc_time *tm)
83 {
84 struct gemini_rtc *rtc = dev_get_drvdata(dev);
85
86 unsigned int days, hour, min, sec;
87 unsigned long offset, time;
88
89 sec = readl(rtc->base + GEMINI_RTC_SECOND);
90 min = readl(rtc->base + GEMINI_RTC_MINUTE);
91 hour = readl(rtc->base + GEMINI_RTC_HOUR);
92 days = readl(rtc->base + GEMINI_RTC_DAYS);
93 offset = readl(rtc->base + GEMINI_RTC_RECORD);
94
95 time = offset + days * 86400 + hour * 3600 + min * 60 + sec;
96
97 rtc_time_to_tm(time, tm);
98 return 0;
99 }
100
101 /*
102 * Maybe there is some hidden register to care ?
103 * looks like register GEMINI_RTC_DAY can count
104 * 365 days * 179 years >= 65535 (uint16)
105 */
106
107 static int gemini_rtc_set_time(struct device *dev, struct rtc_time *tm)
108 {
109 struct gemini_rtc *rtc = dev_get_drvdata(dev);
110 unsigned int sec, min, hour, day;
111 unsigned long offset, time;
112
113 if (tm->tm_year >= 2148) /* EPOCH Year + 179 */
114 return -EINVAL;
115
116 rtc_tm_to_time(tm , &time);
117
118 sec = readl(rtc->base + GEMINI_RTC_SECOND);
119 min = readl(rtc->base + GEMINI_RTC_MINUTE);
120 hour = readl(rtc->base + GEMINI_RTC_HOUR);
121 day = readl(rtc->base + GEMINI_RTC_DAYS);
122
123 offset = time - (day*86400 + hour*3600 + min*60 + sec);
124
125 writel(offset, rtc->base + GEMINI_RTC_RECORD);
126 writel(0x01, rtc->base + GEMINI_RTC_CR);
127 return 0;
128 }
129
130 static struct rtc_class_ops gemini_rtc_ops = {
131 .read_time = gemini_rtc_read_time,
132 .set_time = gemini_rtc_set_time,
133 };
134
135 static int gemini_rtc_probe(struct platform_device *pdev)
136 {
137 struct gemini_rtc *rtc;
138 struct device *dev = &pdev->dev;
139 struct resource *res;
140 int ret;
141
142 rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
143 if (unlikely(!rtc))
144 return -ENOMEM;
145 platform_set_drvdata(pdev, rtc);
146
147 rtc->irq = platform_get_irq(pdev, 0);
148 if (rtc->irq < 0) {
149 ret = -rtc->irq;
150 goto err_mem;
151 }
152
153 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154 if (!res) {
155 ret = -ENODEV;
156 goto err_mem;
157 }
158 rtc->base = devm_ioremap(&pdev->dev, res->start,
159 res->end - res->start + 1);
160
161 ret = request_irq(rtc->irq, gemini_rtc_interrupt,
162 IRQF_SHARED, pdev->name, dev);
163 if (unlikely(ret))
164 goto err_mem;
165
166 rtc->dev = rtc_device_register(pdev->name, dev,
167 &gemini_rtc_ops, THIS_MODULE);
168 if (unlikely(IS_ERR(rtc->dev))) {
169 ret = PTR_ERR(rtc->dev);
170 goto err_irq;
171 }
172 return 0;
173
174 err_irq:
175 free_irq(rtc->irq, dev);
176
177 err_mem:
178 kfree(rtc);
179 return ret;
180 }
181
182 static int gemini_rtc_remove(struct platform_device *pdev)
183 {
184 struct gemini_rtc *rtc = platform_get_drvdata(pdev);
185 struct device *dev = &pdev->dev;
186
187 free_irq(rtc->irq, dev);
188 rtc_device_unregister(rtc->dev);
189 platform_set_drvdata(pdev, NULL);
190 kfree(rtc);
191
192 return 0;
193 }
194
195 static struct platform_driver gemini_rtc_driver = {
196 .driver = {
197 .name = DRV_NAME,
198 .owner = THIS_MODULE,
199 },
200 .probe = gemini_rtc_probe,
201 .remove = gemini_rtc_remove,
202 };
203
204 static int __init gemini_rtc_init(void)
205 {
206 int retval;
207
208 retval = platform_driver_register(&gemini_rtc_driver);
209 if (retval == 0)
210 pr_info(DRV_NAME ": registered successfully");
211 return retval;
212 }
213
214 static void __exit gemini_rtc_exit(void)
215 {
216 platform_driver_unregister(&gemini_rtc_driver);
217 }
218
219 module_init(gemini_rtc_init);
220 module_exit(gemini_rtc_exit);