thinkpad-acpi: add quirklist engine
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / rtc / rtc-s3c.c
blobe0d7b9991505564c412e4771f8061154c5adc449
1 /* drivers/rtc/rtc-s3c.c
3 * Copyright (c) 2004,2006 Simtec Electronics
4 * Ben Dooks, <ben@simtec.co.uk>
5 * http://armlinux.simtec.co.uk/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
14 #include <linux/module.h>
15 #include <linux/fs.h>
16 #include <linux/string.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/clk.h>
23 #include <linux/log2.h>
25 #include <mach/hardware.h>
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/irq.h>
29 #include <plat/regs-rtc.h>
31 /* I have yet to find an S3C implementation with more than one
32 * of these rtc blocks in */
34 static struct resource *s3c_rtc_mem;
36 static void __iomem *s3c_rtc_base;
37 static int s3c_rtc_alarmno = NO_IRQ;
38 static int s3c_rtc_tickno = NO_IRQ;
40 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
42 /* IRQ Handlers */
44 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
46 struct rtc_device *rdev = id;
48 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
49 return IRQ_HANDLED;
52 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
54 struct rtc_device *rdev = id;
56 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
57 return IRQ_HANDLED;
60 /* Update control registers */
61 static void s3c_rtc_setaie(int to)
63 unsigned int tmp;
65 pr_debug("%s: aie=%d\n", __func__, to);
67 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
69 if (to)
70 tmp |= S3C2410_RTCALM_ALMEN;
72 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
75 static int s3c_rtc_setpie(struct device *dev, int enabled)
77 unsigned int tmp;
79 pr_debug("%s: pie=%d\n", __func__, enabled);
81 spin_lock_irq(&s3c_rtc_pie_lock);
82 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
84 if (enabled)
85 tmp |= S3C2410_TICNT_ENABLE;
87 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
88 spin_unlock_irq(&s3c_rtc_pie_lock);
90 return 0;
93 static int s3c_rtc_setfreq(struct device *dev, int freq)
95 unsigned int tmp;
97 if (!is_power_of_2(freq))
98 return -EINVAL;
100 spin_lock_irq(&s3c_rtc_pie_lock);
102 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
103 tmp |= (128 / freq)-1;
105 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
106 spin_unlock_irq(&s3c_rtc_pie_lock);
108 return 0;
111 /* Time read/write */
113 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
115 unsigned int have_retried = 0;
116 void __iomem *base = s3c_rtc_base;
118 retry_get_time:
119 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
120 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
121 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
122 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
123 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
124 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
126 /* the only way to work out wether the system was mid-update
127 * when we read it is to check the second counter, and if it
128 * is zero, then we re-try the entire read
131 if (rtc_tm->tm_sec == 0 && !have_retried) {
132 have_retried = 1;
133 goto retry_get_time;
136 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
137 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
138 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
140 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
141 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
142 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
143 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
144 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
145 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
147 rtc_tm->tm_year += 100;
148 rtc_tm->tm_mon -= 1;
150 return 0;
153 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
155 void __iomem *base = s3c_rtc_base;
156 int year = tm->tm_year - 100;
158 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
159 tm->tm_year, tm->tm_mon, tm->tm_mday,
160 tm->tm_hour, tm->tm_min, tm->tm_sec);
162 /* we get around y2k by simply not supporting it */
164 if (year < 0 || year >= 100) {
165 dev_err(dev, "rtc only supports 100 years\n");
166 return -EINVAL;
169 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
170 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
171 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
172 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
173 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
174 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
176 return 0;
179 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
181 struct rtc_time *alm_tm = &alrm->time;
182 void __iomem *base = s3c_rtc_base;
183 unsigned int alm_en;
185 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
186 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
187 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
188 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
189 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
190 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
192 alm_en = readb(base + S3C2410_RTCALM);
194 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
196 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
197 alm_en,
198 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
199 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
202 /* decode the alarm enable field */
204 if (alm_en & S3C2410_RTCALM_SECEN)
205 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
206 else
207 alm_tm->tm_sec = 0xff;
209 if (alm_en & S3C2410_RTCALM_MINEN)
210 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
211 else
212 alm_tm->tm_min = 0xff;
214 if (alm_en & S3C2410_RTCALM_HOUREN)
215 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
216 else
217 alm_tm->tm_hour = 0xff;
219 if (alm_en & S3C2410_RTCALM_DAYEN)
220 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
221 else
222 alm_tm->tm_mday = 0xff;
224 if (alm_en & S3C2410_RTCALM_MONEN) {
225 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
226 alm_tm->tm_mon -= 1;
227 } else {
228 alm_tm->tm_mon = 0xff;
231 if (alm_en & S3C2410_RTCALM_YEAREN)
232 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
233 else
234 alm_tm->tm_year = 0xffff;
236 return 0;
239 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
241 struct rtc_time *tm = &alrm->time;
242 void __iomem *base = s3c_rtc_base;
243 unsigned int alrm_en;
245 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
246 alrm->enabled,
247 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
248 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
251 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
252 writeb(0x00, base + S3C2410_RTCALM);
254 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
255 alrm_en |= S3C2410_RTCALM_SECEN;
256 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
259 if (tm->tm_min < 60 && tm->tm_min >= 0) {
260 alrm_en |= S3C2410_RTCALM_MINEN;
261 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
264 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
265 alrm_en |= S3C2410_RTCALM_HOUREN;
266 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
269 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
271 writeb(alrm_en, base + S3C2410_RTCALM);
273 s3c_rtc_setaie(alrm->enabled);
275 if (alrm->enabled)
276 enable_irq_wake(s3c_rtc_alarmno);
277 else
278 disable_irq_wake(s3c_rtc_alarmno);
280 return 0;
283 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
285 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
287 seq_printf(seq, "periodic_IRQ\t: %s\n",
288 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
289 return 0;
292 static int s3c_rtc_open(struct device *dev)
294 struct platform_device *pdev = to_platform_device(dev);
295 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
296 int ret;
298 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
299 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
301 if (ret) {
302 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
303 return ret;
306 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
307 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
309 if (ret) {
310 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
311 goto tick_err;
314 return ret;
316 tick_err:
317 free_irq(s3c_rtc_alarmno, rtc_dev);
318 return ret;
321 static void s3c_rtc_release(struct device *dev)
323 struct platform_device *pdev = to_platform_device(dev);
324 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
326 /* do not clear AIE here, it may be needed for wake */
328 s3c_rtc_setpie(dev, 0);
329 free_irq(s3c_rtc_alarmno, rtc_dev);
330 free_irq(s3c_rtc_tickno, rtc_dev);
333 static const struct rtc_class_ops s3c_rtcops = {
334 .open = s3c_rtc_open,
335 .release = s3c_rtc_release,
336 .read_time = s3c_rtc_gettime,
337 .set_time = s3c_rtc_settime,
338 .read_alarm = s3c_rtc_getalarm,
339 .set_alarm = s3c_rtc_setalarm,
340 .irq_set_freq = s3c_rtc_setfreq,
341 .irq_set_state = s3c_rtc_setpie,
342 .proc = s3c_rtc_proc,
345 static void s3c_rtc_enable(struct platform_device *pdev, int en)
347 void __iomem *base = s3c_rtc_base;
348 unsigned int tmp;
350 if (s3c_rtc_base == NULL)
351 return;
353 if (!en) {
354 tmp = readb(base + S3C2410_RTCCON);
355 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
357 tmp = readb(base + S3C2410_TICNT);
358 writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
359 } else {
360 /* re-enable the device, and check it is ok */
362 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
363 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
365 tmp = readb(base + S3C2410_RTCCON);
366 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
369 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
370 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
372 tmp = readb(base + S3C2410_RTCCON);
373 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
376 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
377 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
379 tmp = readb(base + S3C2410_RTCCON);
380 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
385 static int __devexit s3c_rtc_remove(struct platform_device *dev)
387 struct rtc_device *rtc = platform_get_drvdata(dev);
389 platform_set_drvdata(dev, NULL);
390 rtc_device_unregister(rtc);
392 s3c_rtc_setpie(&dev->dev, 0);
393 s3c_rtc_setaie(0);
395 iounmap(s3c_rtc_base);
396 release_resource(s3c_rtc_mem);
397 kfree(s3c_rtc_mem);
399 return 0;
402 static int __devinit s3c_rtc_probe(struct platform_device *pdev)
404 struct rtc_device *rtc;
405 struct resource *res;
406 int ret;
408 pr_debug("%s: probe=%p\n", __func__, pdev);
410 /* find the IRQs */
412 s3c_rtc_tickno = platform_get_irq(pdev, 1);
413 if (s3c_rtc_tickno < 0) {
414 dev_err(&pdev->dev, "no irq for rtc tick\n");
415 return -ENOENT;
418 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
419 if (s3c_rtc_alarmno < 0) {
420 dev_err(&pdev->dev, "no irq for alarm\n");
421 return -ENOENT;
424 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
425 s3c_rtc_tickno, s3c_rtc_alarmno);
427 /* get the memory region */
429 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
430 if (res == NULL) {
431 dev_err(&pdev->dev, "failed to get memory region resource\n");
432 return -ENOENT;
435 s3c_rtc_mem = request_mem_region(res->start,
436 res->end-res->start+1,
437 pdev->name);
439 if (s3c_rtc_mem == NULL) {
440 dev_err(&pdev->dev, "failed to reserve memory region\n");
441 ret = -ENOENT;
442 goto err_nores;
445 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
446 if (s3c_rtc_base == NULL) {
447 dev_err(&pdev->dev, "failed ioremap()\n");
448 ret = -EINVAL;
449 goto err_nomap;
452 /* check to see if everything is setup correctly */
454 s3c_rtc_enable(pdev, 1);
456 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
457 readb(s3c_rtc_base + S3C2410_RTCCON));
459 s3c_rtc_setfreq(&pdev->dev, 1);
461 device_init_wakeup(&pdev->dev, 1);
463 /* register RTC and exit */
465 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
466 THIS_MODULE);
468 if (IS_ERR(rtc)) {
469 dev_err(&pdev->dev, "cannot attach rtc\n");
470 ret = PTR_ERR(rtc);
471 goto err_nortc;
474 rtc->max_user_freq = 128;
476 platform_set_drvdata(pdev, rtc);
477 return 0;
479 err_nortc:
480 s3c_rtc_enable(pdev, 0);
481 iounmap(s3c_rtc_base);
483 err_nomap:
484 release_resource(s3c_rtc_mem);
486 err_nores:
487 return ret;
490 #ifdef CONFIG_PM
492 /* RTC Power management control */
494 static int ticnt_save;
496 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
498 /* save TICNT for anyone using periodic interrupts */
499 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
500 s3c_rtc_enable(pdev, 0);
501 return 0;
504 static int s3c_rtc_resume(struct platform_device *pdev)
506 s3c_rtc_enable(pdev, 1);
507 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
508 return 0;
510 #else
511 #define s3c_rtc_suspend NULL
512 #define s3c_rtc_resume NULL
513 #endif
515 static struct platform_driver s3c2410_rtc_driver = {
516 .probe = s3c_rtc_probe,
517 .remove = __devexit_p(s3c_rtc_remove),
518 .suspend = s3c_rtc_suspend,
519 .resume = s3c_rtc_resume,
520 .driver = {
521 .name = "s3c2410-rtc",
522 .owner = THIS_MODULE,
526 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
528 static int __init s3c_rtc_init(void)
530 printk(banner);
531 return platform_driver_register(&s3c2410_rtc_driver);
534 static void __exit s3c_rtc_exit(void)
536 platform_driver_unregister(&s3c2410_rtc_driver);
539 module_init(s3c_rtc_init);
540 module_exit(s3c_rtc_exit);
542 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
543 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
544 MODULE_LICENSE("GPL");
545 MODULE_ALIAS("platform:s3c2410-rtc");