[RTC] remove references to asm/mach/time.h
[linux-2.6.git] / drivers / rtc / rtc-s3c.c
1 /* drivers/rtc/rtc-s3c.c
2  *
3  * Copyright (c) 2004,2006 Simtec Electronics
4  *      Ben Dooks, <ben@simtec.co.uk>
5  *      http://armlinux.simtec.co.uk/
6  *
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.
10  *
11  * S3C2410/S3C2440/S3C24XX Internal RTC Driver
12 */
13
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>
24
25 #include <asm/hardware.h>
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/irq.h>
29 #include <asm/rtc.h>
30
31 #include <asm/plat-s3c/regs-rtc.h>
32
33 /* I have yet to find an S3C implementation with more than one
34  * of these rtc blocks in */
35
36 static struct resource *s3c_rtc_mem;
37
38 static void __iomem *s3c_rtc_base;
39 static int s3c_rtc_alarmno = NO_IRQ;
40 static int s3c_rtc_tickno  = NO_IRQ;
41 static int s3c_rtc_freq    = 1;
42
43 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
44 static unsigned int tick_count;
45
46 /* IRQ Handlers */
47
48 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
49 {
50         struct rtc_device *rdev = id;
51
52         rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
53         return IRQ_HANDLED;
54 }
55
56 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
57 {
58         struct rtc_device *rdev = id;
59
60         rtc_update_irq(rdev, tick_count++, RTC_PF | RTC_IRQF);
61         return IRQ_HANDLED;
62 }
63
64 /* Update control registers */
65 static void s3c_rtc_setaie(int to)
66 {
67         unsigned int tmp;
68
69         pr_debug("%s: aie=%d\n", __func__, to);
70
71         tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
72
73         if (to)
74                 tmp |= S3C2410_RTCALM_ALMEN;
75
76         writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
77 }
78
79 static void s3c_rtc_setpie(int to)
80 {
81         unsigned int tmp;
82
83         pr_debug("%s: pie=%d\n", __func__, to);
84
85         spin_lock_irq(&s3c_rtc_pie_lock);
86         tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
87
88         if (to)
89                 tmp |= S3C2410_TICNT_ENABLE;
90
91         writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
92         spin_unlock_irq(&s3c_rtc_pie_lock);
93 }
94
95 static void s3c_rtc_setfreq(int freq)
96 {
97         unsigned int tmp;
98
99         spin_lock_irq(&s3c_rtc_pie_lock);
100         tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
101
102         s3c_rtc_freq = freq;
103
104         tmp |= (128 / freq)-1;
105
106         writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
107         spin_unlock_irq(&s3c_rtc_pie_lock);
108 }
109
110 /* Time read/write */
111
112 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
113 {
114         unsigned int have_retried = 0;
115         void __iomem *base = s3c_rtc_base;
116
117  retry_get_time:
118         rtc_tm->tm_min  = readb(base + S3C2410_RTCMIN);
119         rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
120         rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
121         rtc_tm->tm_mon  = readb(base + S3C2410_RTCMON);
122         rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
123         rtc_tm->tm_sec  = readb(base + S3C2410_RTCSEC);
124
125         /* the only way to work out wether the system was mid-update
126          * when we read it is to check the second counter, and if it
127          * is zero, then we re-try the entire read
128          */
129
130         if (rtc_tm->tm_sec == 0 && !have_retried) {
131                 have_retried = 1;
132                 goto retry_get_time;
133         }
134
135         pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
136                  rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
137                  rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
138
139         BCD_TO_BIN(rtc_tm->tm_sec);
140         BCD_TO_BIN(rtc_tm->tm_min);
141         BCD_TO_BIN(rtc_tm->tm_hour);
142         BCD_TO_BIN(rtc_tm->tm_mday);
143         BCD_TO_BIN(rtc_tm->tm_mon);
144         BCD_TO_BIN(rtc_tm->tm_year);
145
146         rtc_tm->tm_year += 100;
147         rtc_tm->tm_mon -= 1;
148
149         return 0;
150 }
151
152 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
153 {
154         void __iomem *base = s3c_rtc_base;
155         int year = tm->tm_year - 100;
156
157         pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
158                  tm->tm_year, tm->tm_mon, tm->tm_mday,
159                  tm->tm_hour, tm->tm_min, tm->tm_sec);
160
161         /* we get around y2k by simply not supporting it */
162
163         if (year < 0 || year >= 100) {
164                 dev_err(dev, "rtc only supports 100 years\n");
165                 return -EINVAL;
166         }
167
168         writeb(BIN2BCD(tm->tm_sec),  base + S3C2410_RTCSEC);
169         writeb(BIN2BCD(tm->tm_min),  base + S3C2410_RTCMIN);
170         writeb(BIN2BCD(tm->tm_hour), base + S3C2410_RTCHOUR);
171         writeb(BIN2BCD(tm->tm_mday), base + S3C2410_RTCDATE);
172         writeb(BIN2BCD(tm->tm_mon + 1), base + S3C2410_RTCMON);
173         writeb(BIN2BCD(year), base + S3C2410_RTCYEAR);
174
175         return 0;
176 }
177
178 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
179 {
180         struct rtc_time *alm_tm = &alrm->time;
181         void __iomem *base = s3c_rtc_base;
182         unsigned int alm_en;
183
184         alm_tm->tm_sec  = readb(base + S3C2410_ALMSEC);
185         alm_tm->tm_min  = readb(base + S3C2410_ALMMIN);
186         alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
187         alm_tm->tm_mon  = readb(base + S3C2410_ALMMON);
188         alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
189         alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
190
191         alm_en = readb(base + S3C2410_RTCALM);
192
193         alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
194
195         pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
196                  alm_en,
197                  alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
198                  alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
199
200
201         /* decode the alarm enable field */
202
203         if (alm_en & S3C2410_RTCALM_SECEN)
204                 BCD_TO_BIN(alm_tm->tm_sec);
205         else
206                 alm_tm->tm_sec = 0xff;
207
208         if (alm_en & S3C2410_RTCALM_MINEN)
209                 BCD_TO_BIN(alm_tm->tm_min);
210         else
211                 alm_tm->tm_min = 0xff;
212
213         if (alm_en & S3C2410_RTCALM_HOUREN)
214                 BCD_TO_BIN(alm_tm->tm_hour);
215         else
216                 alm_tm->tm_hour = 0xff;
217
218         if (alm_en & S3C2410_RTCALM_DAYEN)
219                 BCD_TO_BIN(alm_tm->tm_mday);
220         else
221                 alm_tm->tm_mday = 0xff;
222
223         if (alm_en & S3C2410_RTCALM_MONEN) {
224                 BCD_TO_BIN(alm_tm->tm_mon);
225                 alm_tm->tm_mon -= 1;
226         } else {
227                 alm_tm->tm_mon = 0xff;
228         }
229
230         if (alm_en & S3C2410_RTCALM_YEAREN)
231                 BCD_TO_BIN(alm_tm->tm_year);
232         else
233                 alm_tm->tm_year = 0xffff;
234
235         return 0;
236 }
237
238 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
239 {
240         struct rtc_time *tm = &alrm->time;
241         void __iomem *base = s3c_rtc_base;
242         unsigned int alrm_en;
243
244         pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
245                  alrm->enabled,
246                  tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
247                  tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
248
249
250         alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
251         writeb(0x00, base + S3C2410_RTCALM);
252
253         if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
254                 alrm_en |= S3C2410_RTCALM_SECEN;
255                 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_ALMSEC);
256         }
257
258         if (tm->tm_min < 60 && tm->tm_min >= 0) {
259                 alrm_en |= S3C2410_RTCALM_MINEN;
260                 writeb(BIN2BCD(tm->tm_min), base + S3C2410_ALMMIN);
261         }
262
263         if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
264                 alrm_en |= S3C2410_RTCALM_HOUREN;
265                 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_ALMHOUR);
266         }
267
268         pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
269
270         writeb(alrm_en, base + S3C2410_RTCALM);
271
272         if (0) {
273                 alrm_en = readb(base + S3C2410_RTCALM);
274                 alrm_en &= ~S3C2410_RTCALM_ALMEN;
275                 writeb(alrm_en, base + S3C2410_RTCALM);
276                 disable_irq_wake(s3c_rtc_alarmno);
277         }
278
279         if (alrm->enabled)
280                 enable_irq_wake(s3c_rtc_alarmno);
281         else
282                 disable_irq_wake(s3c_rtc_alarmno);
283
284         return 0;
285 }
286
287 static int s3c_rtc_ioctl(struct device *dev,
288                          unsigned int cmd, unsigned long arg)
289 {
290         unsigned int ret = -ENOIOCTLCMD;
291
292         switch (cmd) {
293         case RTC_AIE_OFF:
294         case RTC_AIE_ON:
295                 s3c_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
296                 ret = 0;
297                 break;
298
299         case RTC_PIE_OFF:
300         case RTC_PIE_ON:
301                 tick_count = 0;
302                 s3c_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
303                 ret = 0;
304                 break;
305
306         case RTC_IRQP_READ:
307                 ret = put_user(s3c_rtc_freq, (unsigned long __user *)arg);
308                 break;
309
310         case RTC_IRQP_SET:
311                 if (!is_power_of_2(arg)) {
312                         ret = -EINVAL;
313                         goto exit;
314                 }
315
316                 pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
317
318                 s3c_rtc_setfreq(arg);
319                 ret = 0;
320                 break;
321
322         case RTC_UIE_ON:
323         case RTC_UIE_OFF:
324                 ret = -EINVAL;
325         }
326
327  exit:
328         return ret;
329 }
330
331 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
332 {
333         unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
334
335         seq_printf(seq, "periodic_IRQ\t: %s\n",
336                      (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
337
338         seq_printf(seq, "periodic_freq\t: %d\n", s3c_rtc_freq);
339
340         return 0;
341 }
342
343 static int s3c_rtc_open(struct device *dev)
344 {
345         struct platform_device *pdev = to_platform_device(dev);
346         struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
347         int ret;
348
349         ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
350                           IRQF_DISABLED,  "s3c2410-rtc alarm", rtc_dev);
351
352         if (ret) {
353                 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
354                 return ret;
355         }
356
357         ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
358                           IRQF_DISABLED,  "s3c2410-rtc tick", rtc_dev);
359
360         if (ret) {
361                 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
362                 goto tick_err;
363         }
364
365         return ret;
366
367  tick_err:
368         free_irq(s3c_rtc_alarmno, rtc_dev);
369         return ret;
370 }
371
372 static void s3c_rtc_release(struct device *dev)
373 {
374         struct platform_device *pdev = to_platform_device(dev);
375         struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
376
377         /* do not clear AIE here, it may be needed for wake */
378
379         s3c_rtc_setpie(0);
380         free_irq(s3c_rtc_alarmno, rtc_dev);
381         free_irq(s3c_rtc_tickno, rtc_dev);
382 }
383
384 static const struct rtc_class_ops s3c_rtcops = {
385         .open           = s3c_rtc_open,
386         .release        = s3c_rtc_release,
387         .ioctl          = s3c_rtc_ioctl,
388         .read_time      = s3c_rtc_gettime,
389         .set_time       = s3c_rtc_settime,
390         .read_alarm     = s3c_rtc_getalarm,
391         .set_alarm      = s3c_rtc_setalarm,
392         .proc           = s3c_rtc_proc,
393 };
394
395 static void s3c_rtc_enable(struct platform_device *pdev, int en)
396 {
397         void __iomem *base = s3c_rtc_base;
398         unsigned int tmp;
399
400         if (s3c_rtc_base == NULL)
401                 return;
402
403         if (!en) {
404                 tmp = readb(base + S3C2410_RTCCON);
405                 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
406
407                 tmp = readb(base + S3C2410_TICNT);
408                 writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
409         } else {
410                 /* re-enable the device, and check it is ok */
411
412                 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
413                         dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
414
415                         tmp = readb(base + S3C2410_RTCCON);
416                         writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
417                 }
418
419                 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
420                         dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
421
422                         tmp = readb(base + S3C2410_RTCCON);
423                         writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
424                 }
425
426                 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
427                         dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
428
429                         tmp = readb(base + S3C2410_RTCCON);
430                         writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
431                 }
432         }
433 }
434
435 static int s3c_rtc_remove(struct platform_device *dev)
436 {
437         struct rtc_device *rtc = platform_get_drvdata(dev);
438
439         platform_set_drvdata(dev, NULL);
440         rtc_device_unregister(rtc);
441
442         s3c_rtc_setpie(0);
443         s3c_rtc_setaie(0);
444
445         iounmap(s3c_rtc_base);
446         release_resource(s3c_rtc_mem);
447         kfree(s3c_rtc_mem);
448
449         return 0;
450 }
451
452 static int s3c_rtc_probe(struct platform_device *pdev)
453 {
454         struct rtc_device *rtc;
455         struct resource *res;
456         int ret;
457
458         pr_debug("%s: probe=%p\n", __func__, pdev);
459
460         /* find the IRQs */
461
462         s3c_rtc_tickno = platform_get_irq(pdev, 1);
463         if (s3c_rtc_tickno < 0) {
464                 dev_err(&pdev->dev, "no irq for rtc tick\n");
465                 return -ENOENT;
466         }
467
468         s3c_rtc_alarmno = platform_get_irq(pdev, 0);
469         if (s3c_rtc_alarmno < 0) {
470                 dev_err(&pdev->dev, "no irq for alarm\n");
471                 return -ENOENT;
472         }
473
474         pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
475                  s3c_rtc_tickno, s3c_rtc_alarmno);
476
477         /* get the memory region */
478
479         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
480         if (res == NULL) {
481                 dev_err(&pdev->dev, "failed to get memory region resource\n");
482                 return -ENOENT;
483         }
484
485         s3c_rtc_mem = request_mem_region(res->start,
486                                          res->end-res->start+1,
487                                          pdev->name);
488
489         if (s3c_rtc_mem == NULL) {
490                 dev_err(&pdev->dev, "failed to reserve memory region\n");
491                 ret = -ENOENT;
492                 goto err_nores;
493         }
494
495         s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
496         if (s3c_rtc_base == NULL) {
497                 dev_err(&pdev->dev, "failed ioremap()\n");
498                 ret = -EINVAL;
499                 goto err_nomap;
500         }
501
502         /* check to see if everything is setup correctly */
503
504         s3c_rtc_enable(pdev, 1);
505
506         pr_debug("s3c2410_rtc: RTCCON=%02x\n",
507                  readb(s3c_rtc_base + S3C2410_RTCCON));
508
509         s3c_rtc_setfreq(s3c_rtc_freq);
510
511         /* register RTC and exit */
512
513         rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
514                                   THIS_MODULE);
515
516         if (IS_ERR(rtc)) {
517                 dev_err(&pdev->dev, "cannot attach rtc\n");
518                 ret = PTR_ERR(rtc);
519                 goto err_nortc;
520         }
521
522         rtc->max_user_freq = 128;
523
524         platform_set_drvdata(pdev, rtc);
525         return 0;
526
527  err_nortc:
528         s3c_rtc_enable(pdev, 0);
529         iounmap(s3c_rtc_base);
530
531  err_nomap:
532         release_resource(s3c_rtc_mem);
533
534  err_nores:
535         return ret;
536 }
537
538 #ifdef CONFIG_PM
539
540 /* RTC Power management control */
541
542 static int ticnt_save;
543
544 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
545 {
546         /* save TICNT for anyone using periodic interrupts */
547         ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
548         s3c_rtc_enable(pdev, 0);
549         return 0;
550 }
551
552 static int s3c_rtc_resume(struct platform_device *pdev)
553 {
554         s3c_rtc_enable(pdev, 1);
555         writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
556         return 0;
557 }
558 #else
559 #define s3c_rtc_suspend NULL
560 #define s3c_rtc_resume  NULL
561 #endif
562
563 static struct platform_driver s3c2410_rtcdrv = {
564         .probe          = s3c_rtc_probe,
565         .remove         = s3c_rtc_remove,
566         .suspend        = s3c_rtc_suspend,
567         .resume         = s3c_rtc_resume,
568         .driver         = {
569                 .name   = "s3c2410-rtc",
570                 .owner  = THIS_MODULE,
571         },
572 };
573
574 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
575
576 static int __init s3c_rtc_init(void)
577 {
578         printk(banner);
579         return platform_driver_register(&s3c2410_rtcdrv);
580 }
581
582 static void __exit s3c_rtc_exit(void)
583 {
584         platform_driver_unregister(&s3c2410_rtcdrv);
585 }
586
587 module_init(s3c_rtc_init);
588 module_exit(s3c_rtc_exit);
589
590 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
591 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
592 MODULE_LICENSE("GPL");
593 MODULE_ALIAS("platform:s3c2410-rtc");