2 * drivers/rtc/rtc-vt8500.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/rtc.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
23 #include <linux/bcd.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
28 * Register definitions
30 #define VT8500_RTC_TS 0x00 /* Time set */
31 #define VT8500_RTC_DS 0x04 /* Date set */
32 #define VT8500_RTC_AS 0x08 /* Alarm set */
33 #define VT8500_RTC_CR 0x0c /* Control */
34 #define VT8500_RTC_TR 0x10 /* Time read */
35 #define VT8500_RTC_DR 0x14 /* Date read */
36 #define VT8500_RTC_WS 0x18 /* Write status */
37 #define VT8500_RTC_CL 0x20 /* Calibration */
38 #define VT8500_RTC_IS 0x24 /* Interrupt status */
39 #define VT8500_RTC_ST 0x28 /* Status */
41 #define INVALID_TIME_BIT (1 << 31)
43 #define DATE_CENTURY_S 19
44 #define DATE_YEAR_S 11
45 #define DATE_YEAR_MASK (0xff << DATE_YEAR_S)
46 #define DATE_MONTH_S 6
47 #define DATE_MONTH_MASK (0x1f << DATE_MONTH_S)
48 #define DATE_DAY_MASK 0x3f
51 #define TIME_DOW_MASK (0x07 << TIME_DOW_S)
52 #define TIME_HOUR_S 14
53 #define TIME_HOUR_MASK (0x3f << TIME_HOUR_S)
55 #define TIME_MIN_MASK (0x7f << TIME_MIN_S)
56 #define TIME_SEC_MASK 0x7f
58 #define ALARM_DAY_S 20
59 #define ALARM_DAY_MASK (0x3f << ALARM_DAY_S)
61 #define ALARM_DAY_BIT (1 << 29)
62 #define ALARM_HOUR_BIT (1 << 28)
63 #define ALARM_MIN_BIT (1 << 27)
64 #define ALARM_SEC_BIT (1 << 26)
66 #define ALARM_ENABLE_MASK (ALARM_DAY_BIT \
71 #define VT8500_RTC_CR_ENABLE (1 << 0) /* Enable RTC */
72 #define VT8500_RTC_CR_24H (1 << 1) /* 24h time format */
73 #define VT8500_RTC_CR_SM_ENABLE (1 << 2) /* Enable periodic irqs */
74 #define VT8500_RTC_CR_SM_SEC (1 << 3) /* 0: 1Hz/60, 1: 1Hz */
75 #define VT8500_RTC_CR_CALIB (1 << 4) /* Enable calibration */
78 void __iomem
*regbase
;
81 struct rtc_device
*rtc
;
82 spinlock_t lock
; /* Protects this structure */
85 static irqreturn_t
vt8500_rtc_irq(int irq
, void *dev_id
)
87 struct vt8500_rtc
*vt8500_rtc
= dev_id
;
89 unsigned long events
= 0;
91 spin_lock(&vt8500_rtc
->lock
);
93 /* clear interrupt sources */
94 isr
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
95 writel(isr
, vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
97 spin_unlock(&vt8500_rtc
->lock
);
100 events
|= RTC_AF
| RTC_IRQF
;
102 rtc_update_irq(vt8500_rtc
->rtc
, 1, events
);
107 static int vt8500_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
109 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
112 date
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_DR
);
113 time
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_TR
);
115 tm
->tm_sec
= bcd2bin(time
& TIME_SEC_MASK
);
116 tm
->tm_min
= bcd2bin((time
& TIME_MIN_MASK
) >> TIME_MIN_S
);
117 tm
->tm_hour
= bcd2bin((time
& TIME_HOUR_MASK
) >> TIME_HOUR_S
);
118 tm
->tm_mday
= bcd2bin(date
& DATE_DAY_MASK
);
119 tm
->tm_mon
= bcd2bin((date
& DATE_MONTH_MASK
) >> DATE_MONTH_S
);
120 tm
->tm_year
= bcd2bin((date
& DATE_YEAR_MASK
) >> DATE_YEAR_S
)
121 + ((date
>> DATE_CENTURY_S
) & 1 ? 200 : 100);
122 tm
->tm_wday
= (time
& TIME_DOW_MASK
) >> TIME_DOW_S
;
127 static int vt8500_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
129 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
131 if (tm
->tm_year
< 100) {
132 dev_warn(dev
, "Only years 2000-2199 are supported by the "
137 writel((bin2bcd(tm
->tm_year
- 100) << DATE_YEAR_S
)
138 | (bin2bcd(tm
->tm_mon
) << DATE_MONTH_S
)
139 | (bin2bcd(tm
->tm_mday
)),
140 vt8500_rtc
->regbase
+ VT8500_RTC_DS
);
141 writel((bin2bcd(tm
->tm_wday
) << TIME_DOW_S
)
142 | (bin2bcd(tm
->tm_hour
) << TIME_HOUR_S
)
143 | (bin2bcd(tm
->tm_min
) << TIME_MIN_S
)
144 | (bin2bcd(tm
->tm_sec
)),
145 vt8500_rtc
->regbase
+ VT8500_RTC_TS
);
150 static int vt8500_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
152 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
155 alarm
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
156 isr
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
158 alrm
->time
.tm_mday
= bcd2bin((alarm
& ALARM_DAY_MASK
) >> ALARM_DAY_S
);
159 alrm
->time
.tm_hour
= bcd2bin((alarm
& TIME_HOUR_MASK
) >> TIME_HOUR_S
);
160 alrm
->time
.tm_min
= bcd2bin((alarm
& TIME_MIN_MASK
) >> TIME_MIN_S
);
161 alrm
->time
.tm_sec
= bcd2bin((alarm
& TIME_SEC_MASK
));
163 alrm
->enabled
= (alarm
& ALARM_ENABLE_MASK
) ? 1 : 0;
165 alrm
->pending
= (isr
& 1) ? 1 : 0;
166 return rtc_valid_tm(&alrm
->time
);
169 static int vt8500_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
171 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
173 writel((alrm
->enabled
? ALARM_ENABLE_MASK
: 0)
174 | (bin2bcd(alrm
->time
.tm_mday
) << ALARM_DAY_S
)
175 | (bin2bcd(alrm
->time
.tm_hour
) << TIME_HOUR_S
)
176 | (bin2bcd(alrm
->time
.tm_min
) << TIME_MIN_S
)
177 | (bin2bcd(alrm
->time
.tm_sec
)),
178 vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
183 static int vt8500_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
185 struct vt8500_rtc
*vt8500_rtc
= dev_get_drvdata(dev
);
186 unsigned long tmp
= readl(vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
189 tmp
|= ALARM_ENABLE_MASK
;
191 tmp
&= ~ALARM_ENABLE_MASK
;
193 writel(tmp
, vt8500_rtc
->regbase
+ VT8500_RTC_AS
);
197 static const struct rtc_class_ops vt8500_rtc_ops
= {
198 .read_time
= vt8500_rtc_read_time
,
199 .set_time
= vt8500_rtc_set_time
,
200 .read_alarm
= vt8500_rtc_read_alarm
,
201 .set_alarm
= vt8500_rtc_set_alarm
,
202 .alarm_irq_enable
= vt8500_alarm_irq_enable
,
205 static int __devinit
vt8500_rtc_probe(struct platform_device
*pdev
)
207 struct vt8500_rtc
*vt8500_rtc
;
210 vt8500_rtc
= kzalloc(sizeof(struct vt8500_rtc
), GFP_KERNEL
);
214 spin_lock_init(&vt8500_rtc
->lock
);
215 platform_set_drvdata(pdev
, vt8500_rtc
);
217 vt8500_rtc
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
218 if (!vt8500_rtc
->res
) {
219 dev_err(&pdev
->dev
, "No I/O memory resource defined\n");
224 vt8500_rtc
->irq_alarm
= platform_get_irq(pdev
, 0);
225 if (vt8500_rtc
->irq_alarm
< 0) {
226 dev_err(&pdev
->dev
, "No alarm IRQ resource defined\n");
231 vt8500_rtc
->res
= request_mem_region(vt8500_rtc
->res
->start
,
232 resource_size(vt8500_rtc
->res
),
234 if (vt8500_rtc
->res
== NULL
) {
235 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
240 vt8500_rtc
->regbase
= ioremap(vt8500_rtc
->res
->start
,
241 resource_size(vt8500_rtc
->res
));
242 if (!vt8500_rtc
->regbase
) {
243 dev_err(&pdev
->dev
, "Unable to map RTC I/O memory\n");
248 /* Enable RTC and set it to 24-hour mode */
249 writel(VT8500_RTC_CR_ENABLE
| VT8500_RTC_CR_24H
,
250 vt8500_rtc
->regbase
+ VT8500_RTC_CR
);
252 vt8500_rtc
->rtc
= rtc_device_register("vt8500-rtc", &pdev
->dev
,
253 &vt8500_rtc_ops
, THIS_MODULE
);
254 if (IS_ERR(vt8500_rtc
->rtc
)) {
255 ret
= PTR_ERR(vt8500_rtc
->rtc
);
257 "Failed to register RTC device -> %d\n", ret
);
261 ret
= request_irq(vt8500_rtc
->irq_alarm
, vt8500_rtc_irq
, 0,
262 "rtc alarm", vt8500_rtc
);
264 dev_err(&pdev
->dev
, "can't get irq %i, err %d\n",
265 vt8500_rtc
->irq_alarm
, ret
);
272 rtc_device_unregister(vt8500_rtc
->rtc
);
274 iounmap(vt8500_rtc
->regbase
);
276 release_mem_region(vt8500_rtc
->res
->start
,
277 resource_size(vt8500_rtc
->res
));
283 static int __devexit
vt8500_rtc_remove(struct platform_device
*pdev
)
285 struct vt8500_rtc
*vt8500_rtc
= platform_get_drvdata(pdev
);
287 free_irq(vt8500_rtc
->irq_alarm
, vt8500_rtc
);
289 rtc_device_unregister(vt8500_rtc
->rtc
);
291 /* Disable alarm matching */
292 writel(0, vt8500_rtc
->regbase
+ VT8500_RTC_IS
);
293 iounmap(vt8500_rtc
->regbase
);
294 release_mem_region(vt8500_rtc
->res
->start
,
295 resource_size(vt8500_rtc
->res
));
298 platform_set_drvdata(pdev
, NULL
);
303 static struct platform_driver vt8500_rtc_driver
= {
304 .probe
= vt8500_rtc_probe
,
305 .remove
= __devexit_p(vt8500_rtc_remove
),
307 .name
= "vt8500-rtc",
308 .owner
= THIS_MODULE
,
312 static int __init
vt8500_rtc_init(void)
314 return platform_driver_register(&vt8500_rtc_driver
);
316 module_init(vt8500_rtc_init
);
318 static void __exit
vt8500_rtc_exit(void)
320 platform_driver_unregister(&vt8500_rtc_driver
);
322 module_exit(vt8500_rtc_exit
);
324 MODULE_AUTHOR("Alexey Charkov <alchark@gmail.com>");
325 MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:vt8500-rtc");