string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / 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 <mach/hardware.h>
26 #include <asm/uaccess.h>
27 #include <asm/io.h>
28 #include <asm/irq.h>
29 #include <plat/regs-rtc.h>
30
31 /* I have yet to find an S3C implementation with more than one
32  * of these rtc blocks in */
33
34 static struct resource *s3c_rtc_mem;
35
36 static void __iomem *s3c_rtc_base;
37 static int s3c_rtc_alarmno = NO_IRQ;
38 static int s3c_rtc_tickno  = NO_IRQ;
39
40 static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
41
42 /* IRQ Handlers */
43
44 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
45 {
46         struct rtc_device *rdev = id;
47
48         rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
49         return IRQ_HANDLED;
50 }
51
52 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
53 {
54         struct rtc_device *rdev = id;
55
56         rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
57         return IRQ_HANDLED;
58 }
59
60 /* Update control registers */
61 static void s3c_rtc_setaie(int to)
62 {
63         unsigned int tmp;
64
65         pr_debug("%s: aie=%d\n", __func__, to);
66
67         tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
68
69         if (to)
70                 tmp |= S3C2410_RTCALM_ALMEN;
71
72         writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
73 }
74
75 static int s3c_rtc_setpie(struct device *dev, int enabled)
76 {
77         unsigned int tmp;
78
79         pr_debug("%s: pie=%d\n", __func__, enabled);
80
81         spin_lock_irq(&s3c_rtc_pie_lock);
82         tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
83
84         if (enabled)
85                 tmp |= S3C2410_TICNT_ENABLE;
86
87         writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
88         spin_unlock_irq(&s3c_rtc_pie_lock);
89
90         return 0;
91 }
92
93 static int s3c_rtc_setfreq(struct device *dev, int freq)
94 {
95         unsigned int tmp;
96
97         if (!is_power_of_2(freq))
98                 return -EINVAL;
99
100         spin_lock_irq(&s3c_rtc_pie_lock);
101
102         tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
103         tmp |= (128 / freq)-1;
104
105         writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
106         spin_unlock_irq(&s3c_rtc_pie_lock);
107
108         return 0;
109 }
110
111 /* Time read/write */
112
113 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
114 {
115         unsigned int have_retried = 0;
116         void __iomem *base = s3c_rtc_base;
117
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);
125
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
129          */
130
131         if (rtc_tm->tm_sec == 0 && !have_retried) {
132                 have_retried = 1;
133                 goto retry_get_time;
134         }
135
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);
139
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);
146
147         rtc_tm->tm_year += 100;
148         rtc_tm->tm_mon -= 1;
149
150         return 0;
151 }
152
153 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
154 {
155         void __iomem *base = s3c_rtc_base;
156         int year = tm->tm_year - 100;
157
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);
161
162         /* we get around y2k by simply not supporting it */
163
164         if (year < 0 || year >= 100) {
165                 dev_err(dev, "rtc only supports 100 years\n");
166                 return -EINVAL;
167         }
168
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);
175
176         return 0;
177 }
178
179 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
180 {
181         struct rtc_time *alm_tm = &alrm->time;
182         void __iomem *base = s3c_rtc_base;
183         unsigned int alm_en;
184
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);
191
192         alm_en = readb(base + S3C2410_RTCALM);
193
194         alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
195
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);
200
201
202         /* decode the alarm enable field */
203
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;
208
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;
213
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;
218
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;
223
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;
229         }
230
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;
235
236         return 0;
237 }
238
239 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
240 {
241         struct rtc_time *tm = &alrm->time;
242         void __iomem *base = s3c_rtc_base;
243         unsigned int alrm_en;
244
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);
249
250
251         alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
252         writeb(0x00, base + S3C2410_RTCALM);
253
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);
257         }
258
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);
262         }
263
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);
267         }
268
269         pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
270
271         writeb(alrm_en, base + S3C2410_RTCALM);
272
273         s3c_rtc_setaie(alrm->enabled);
274
275         if (alrm->enabled)
276                 enable_irq_wake(s3c_rtc_alarmno);
277         else
278                 disable_irq_wake(s3c_rtc_alarmno);
279
280         return 0;
281 }
282
283 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
284 {
285         unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
286
287         seq_printf(seq, "periodic_IRQ\t: %s\n",
288                      (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
289         return 0;
290 }
291
292 static int s3c_rtc_open(struct device *dev)
293 {
294         struct platform_device *pdev = to_platform_device(dev);
295         struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
296         int ret;
297
298         ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
299                           IRQF_DISABLED,  "s3c2410-rtc alarm", rtc_dev);
300
301         if (ret) {
302                 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
303                 return ret;
304         }
305
306         ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
307                           IRQF_DISABLED,  "s3c2410-rtc tick", rtc_dev);
308
309         if (ret) {
310                 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
311                 goto tick_err;
312         }
313
314         return ret;
315
316  tick_err:
317         free_irq(s3c_rtc_alarmno, rtc_dev);
318         return ret;
319 }
320
321 static void s3c_rtc_release(struct device *dev)
322 {
323         struct platform_device *pdev = to_platform_device(dev);
324         struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
325
326         /* do not clear AIE here, it may be needed for wake */
327
328         s3c_rtc_setpie(dev, 0);
329         free_irq(s3c_rtc_alarmno, rtc_dev);
330         free_irq(s3c_rtc_tickno, rtc_dev);
331 }
332
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,
343 };
344
345 static void s3c_rtc_enable(struct platform_device *pdev, int en)
346 {
347         void __iomem *base = s3c_rtc_base;
348         unsigned int tmp;
349
350         if (s3c_rtc_base == NULL)
351                 return;
352
353         if (!en) {
354                 tmp = readb(base + S3C2410_RTCCON);
355                 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
356
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 */
361
362                 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
363                         dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
364
365                         tmp = readb(base + S3C2410_RTCCON);
366                         writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
367                 }
368
369                 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
370                         dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
371
372                         tmp = readb(base + S3C2410_RTCCON);
373                         writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
374                 }
375
376                 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
377                         dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
378
379                         tmp = readb(base + S3C2410_RTCCON);
380                         writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
381                 }
382         }
383 }
384
385 static int __devexit s3c_rtc_remove(struct platform_device *dev)
386 {
387         struct rtc_device *rtc = platform_get_drvdata(dev);
388
389         platform_set_drvdata(dev, NULL);
390         rtc_device_unregister(rtc);
391
392         s3c_rtc_setpie(&dev->dev, 0);
393         s3c_rtc_setaie(0);
394
395         iounmap(s3c_rtc_base);
396         release_resource(s3c_rtc_mem);
397         kfree(s3c_rtc_mem);
398
399         return 0;
400 }
401
402 static int __devinit s3c_rtc_probe(struct platform_device *pdev)
403 {
404         struct rtc_device *rtc;
405         struct resource *res;
406         int ret;
407
408         pr_debug("%s: probe=%p\n", __func__, pdev);
409
410         /* find the IRQs */
411
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;
416         }
417
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;
422         }
423
424         pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
425                  s3c_rtc_tickno, s3c_rtc_alarmno);
426
427         /* get the memory region */
428
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;
433         }
434
435         s3c_rtc_mem = request_mem_region(res->start,
436                                          res->end-res->start+1,
437                                          pdev->name);
438
439         if (s3c_rtc_mem == NULL) {
440                 dev_err(&pdev->dev, "failed to reserve memory region\n");
441                 ret = -ENOENT;
442                 goto err_nores;
443         }
444
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;
450         }
451
452         /* check to see if everything is setup correctly */
453
454         s3c_rtc_enable(pdev, 1);
455
456         pr_debug("s3c2410_rtc: RTCCON=%02x\n",
457                  readb(s3c_rtc_base + S3C2410_RTCCON));
458
459         s3c_rtc_setfreq(&pdev->dev, 1);
460
461         device_init_wakeup(&pdev->dev, 1);
462
463         /* register RTC and exit */
464
465         rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
466                                   THIS_MODULE);
467
468         if (IS_ERR(rtc)) {
469                 dev_err(&pdev->dev, "cannot attach rtc\n");
470                 ret = PTR_ERR(rtc);
471                 goto err_nortc;
472         }
473
474         rtc->max_user_freq = 128;
475
476         platform_set_drvdata(pdev, rtc);
477         return 0;
478
479  err_nortc:
480         s3c_rtc_enable(pdev, 0);
481         iounmap(s3c_rtc_base);
482
483  err_nomap:
484         release_resource(s3c_rtc_mem);
485
486  err_nores:
487         return ret;
488 }
489
490 #ifdef CONFIG_PM
491
492 /* RTC Power management control */
493
494 static int ticnt_save;
495
496 static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
497 {
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;
502 }
503
504 static int s3c_rtc_resume(struct platform_device *pdev)
505 {
506         s3c_rtc_enable(pdev, 1);
507         writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
508         return 0;
509 }
510 #else
511 #define s3c_rtc_suspend NULL
512 #define s3c_rtc_resume  NULL
513 #endif
514
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,
523         },
524 };
525
526 static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
527
528 static int __init s3c_rtc_init(void)
529 {
530         printk(banner);
531         return platform_driver_register(&s3c2410_rtc_driver);
532 }
533
534 static void __exit s3c_rtc_exit(void)
535 {
536         platform_driver_unregister(&s3c2410_rtc_driver);
537 }
538
539 module_init(s3c_rtc_init);
540 module_exit(s3c_rtc_exit);
541
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");