4 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
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.
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.
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.
20 * Original code for older kernel 2.6.15 are form Stormlinksemi
21 * first update from Janos Laube for > 2.6.29 kernels
23 * checkpatch fixes and usage off rtc-lib code
24 * Hans Ulli Kroll <ulli.kroll@googlemail.com>
27 #include <linux/rtc.h>
29 #include <linux/slab.h>
30 #include <linux/platform_device.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
34 #include <mach/hardware.h>
36 #define DRV_NAME "rtc-gemini"
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");
44 struct rtc_device
*dev
;
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,
61 static irqreturn_t
gemini_rtc_interrupt(int irq
, void *dev
)
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
71 * The register "day" seams to be fixed, and the register "hour"
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.
78 * This FIX works pretty fine and Stormlinksemi aka Cortina-Networks does
79 * the same thing, without the rtc-lib.c calls.
82 static int gemini_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
84 struct gemini_rtc
*rtc
= dev_get_drvdata(dev
);
86 unsigned int days
, hour
, min
, sec
;
87 unsigned long offset
, time
;
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
);
95 time
= offset
+ days
* 86400 + hour
* 3600 + min
* 60 + sec
;
97 rtc_time_to_tm(time
, tm
);
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)
107 static int gemini_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
109 struct gemini_rtc
*rtc
= dev_get_drvdata(dev
);
110 unsigned int sec
, min
, hour
, day
;
111 unsigned long offset
, time
;
113 if (tm
->tm_year
>= 2148) /* EPOCH Year + 179 */
116 rtc_tm_to_time(tm
, &time
);
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
);
123 offset
= time
- (day
*86400 + hour
*3600 + min
*60 + sec
);
125 writel(offset
, rtc
->base
+ GEMINI_RTC_RECORD
);
126 writel(0x01, rtc
->base
+ GEMINI_RTC_CR
);
130 static struct rtc_class_ops gemini_rtc_ops
= {
131 .read_time
= gemini_rtc_read_time
,
132 .set_time
= gemini_rtc_set_time
,
135 static int gemini_rtc_probe(struct platform_device
*pdev
)
137 struct gemini_rtc
*rtc
;
138 struct device
*dev
= &pdev
->dev
;
139 struct resource
*res
;
142 rtc
= kzalloc(sizeof(*rtc
), GFP_KERNEL
);
145 platform_set_drvdata(pdev
, rtc
);
147 rtc
->irq
= platform_get_irq(pdev
, 0);
153 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
158 rtc
->base
= devm_ioremap(&pdev
->dev
, res
->start
,
159 res
->end
- res
->start
+ 1);
161 ret
= request_irq(rtc
->irq
, gemini_rtc_interrupt
,
162 IRQF_SHARED
, pdev
->name
, dev
);
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
);
175 free_irq(rtc
->irq
, dev
);
182 static int gemini_rtc_remove(struct platform_device
*pdev
)
184 struct gemini_rtc
*rtc
= platform_get_drvdata(pdev
);
185 struct device
*dev
= &pdev
->dev
;
187 free_irq(rtc
->irq
, dev
);
188 rtc_device_unregister(rtc
->dev
);
189 platform_set_drvdata(pdev
, NULL
);
195 static struct platform_driver gemini_rtc_driver
= {
198 .owner
= THIS_MODULE
,
200 .probe
= gemini_rtc_probe
,
201 .remove
= gemini_rtc_remove
,
204 static int __init
gemini_rtc_init(void)
208 retval
= platform_driver_register(&gemini_rtc_driver
);
210 pr_info(DRV_NAME
": registered successfully");
214 static void __exit
gemini_rtc_exit(void)
216 platform_driver_unregister(&gemini_rtc_driver
);
219 module_init(gemini_rtc_init
);
220 module_exit(gemini_rtc_exit
);