fcead4c4cd1f339b59b84de1c2d34ea95f38f9f1
[linux-2.6.git] / drivers / rtc / rtc-sh.c
1 /*
2  * SuperH On-Chip RTC Support
3  *
4  * Copyright (C) 2006, 2007, 2008  Paul Mundt
5  * Copyright (C) 2006  Jamie Lenehan
6  * Copyright (C) 2008  Angelo Castello
7  *
8  * Based on the old arch/sh/kernel/cpu/rtc.c by:
9  *
10  *  Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org>
11  *  Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka
12  *
13  * This file is subject to the terms and conditions of the GNU General Public
14  * License.  See the file "COPYING" in the main directory of this archive
15  * for more details.
16  */
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/bcd.h>
20 #include <linux/rtc.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/seq_file.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/io.h>
27 #include <asm/rtc.h>
28
29 #define DRV_NAME        "sh-rtc"
30 #define DRV_VERSION     "0.2.0"
31
32 #define RTC_REG(r)      ((r) * rtc_reg_size)
33
34 #define R64CNT          RTC_REG(0)
35
36 #define RSECCNT         RTC_REG(1)      /* RTC sec */
37 #define RMINCNT         RTC_REG(2)      /* RTC min */
38 #define RHRCNT          RTC_REG(3)      /* RTC hour */
39 #define RWKCNT          RTC_REG(4)      /* RTC week */
40 #define RDAYCNT         RTC_REG(5)      /* RTC day */
41 #define RMONCNT         RTC_REG(6)      /* RTC month */
42 #define RYRCNT          RTC_REG(7)      /* RTC year */
43 #define RSECAR          RTC_REG(8)      /* ALARM sec */
44 #define RMINAR          RTC_REG(9)      /* ALARM min */
45 #define RHRAR           RTC_REG(10)     /* ALARM hour */
46 #define RWKAR           RTC_REG(11)     /* ALARM week */
47 #define RDAYAR          RTC_REG(12)     /* ALARM day */
48 #define RMONAR          RTC_REG(13)     /* ALARM month */
49 #define RCR1            RTC_REG(14)     /* Control */
50 #define RCR2            RTC_REG(15)     /* Control */
51
52 /*
53  * Note on RYRAR and RCR3: Up until this point most of the register
54  * definitions are consistent across all of the available parts. However,
55  * the placement of the optional RYRAR and RCR3 (the RYRAR control
56  * register used to control RYRCNT/RYRAR compare) varies considerably
57  * across various parts, occasionally being mapped in to a completely
58  * unrelated address space. For proper RYRAR support a separate resource
59  * would have to be handed off, but as this is purely optional in
60  * practice, we simply opt not to support it, thereby keeping the code
61  * quite a bit more simplified.
62  */
63
64 /* ALARM Bits - or with BCD encoded value */
65 #define AR_ENB          0x80    /* Enable for alarm cmp   */
66
67 /* Period Bits */
68 #define PF_HP           0x100   /* Enable Half Period to support 8,32,128Hz */
69 #define PF_COUNT        0x200   /* Half periodic counter */
70 #define PF_OXS          0x400   /* Periodic One x Second */
71 #define PF_KOU          0x800   /* Kernel or User periodic request 1=kernel */
72 #define PF_MASK         0xf00
73
74 /* RCR1 Bits */
75 #define RCR1_CF         0x80    /* Carry Flag             */
76 #define RCR1_CIE        0x10    /* Carry Interrupt Enable */
77 #define RCR1_AIE        0x08    /* Alarm Interrupt Enable */
78 #define RCR1_AF         0x01    /* Alarm Flag             */
79
80 /* RCR2 Bits */
81 #define RCR2_PEF        0x80    /* PEriodic interrupt Flag */
82 #define RCR2_PESMASK    0x70    /* Periodic interrupt Set  */
83 #define RCR2_RTCEN      0x08    /* ENable RTC              */
84 #define RCR2_ADJ        0x04    /* ADJustment (30-second)  */
85 #define RCR2_RESET      0x02    /* Reset bit               */
86 #define RCR2_START      0x01    /* Start bit               */
87
88 struct sh_rtc {
89         void __iomem *regbase;
90         unsigned long regsize;
91         struct resource *res;
92         unsigned int alarm_irq, periodic_irq, carry_irq;
93         struct rtc_device *rtc_dev;
94         spinlock_t lock;
95         unsigned long capabilities;     /* See asm-sh/rtc.h for cap bits */
96         unsigned short periodic_freq;
97 };
98
99 static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
100 {
101         struct sh_rtc *rtc = dev_id;
102         unsigned int tmp;
103
104         spin_lock(&rtc->lock);
105
106         tmp = readb(rtc->regbase + RCR1);
107         tmp &= ~RCR1_CF;
108         writeb(tmp, rtc->regbase + RCR1);
109
110         /* Users have requested One x Second IRQ */
111         if (rtc->periodic_freq & PF_OXS)
112                 rtc_update_irq(rtc->rtc_dev, 1, RTC_UF | RTC_IRQF);
113
114         spin_unlock(&rtc->lock);
115
116         return IRQ_HANDLED;
117 }
118
119 static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
120 {
121         struct sh_rtc *rtc = dev_id;
122         unsigned int tmp;
123
124         spin_lock(&rtc->lock);
125
126         tmp = readb(rtc->regbase + RCR1);
127         tmp &= ~(RCR1_AF | RCR1_AIE);
128                 writeb(tmp, rtc->regbase + RCR1);
129
130         rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
131
132         spin_unlock(&rtc->lock);
133
134         return IRQ_HANDLED;
135 }
136
137 static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
138 {
139         struct sh_rtc *rtc = dev_id;
140         struct rtc_device *rtc_dev = rtc->rtc_dev;
141         unsigned int tmp;
142
143         spin_lock(&rtc->lock);
144
145         tmp = readb(rtc->regbase + RCR2);
146         tmp &= ~RCR2_PEF;
147         writeb(tmp, rtc->regbase + RCR2);
148
149         /* Half period enabled than one skipped and the next notified */
150         if ((rtc->periodic_freq & PF_HP) && (rtc->periodic_freq & PF_COUNT))
151                 rtc->periodic_freq &= ~PF_COUNT;
152         else {
153                 if (rtc->periodic_freq & PF_HP)
154                         rtc->periodic_freq |= PF_COUNT;
155                 if (rtc->periodic_freq & PF_KOU) {
156                         spin_lock(&rtc_dev->irq_task_lock);
157                         if (rtc_dev->irq_task)
158                                 rtc_dev->irq_task->func(rtc_dev->irq_task->private_data);
159                         spin_unlock(&rtc_dev->irq_task_lock);
160                 } else
161                         rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
162         }
163
164         spin_unlock(&rtc->lock);
165
166         return IRQ_HANDLED;
167 }
168
169 static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
170 {
171         struct sh_rtc *rtc = dev_get_drvdata(dev);
172         unsigned int tmp;
173
174         spin_lock_irq(&rtc->lock);
175
176         tmp = readb(rtc->regbase + RCR2);
177
178         if (enable) {
179                 tmp &= ~RCR2_PEF;       /* Clear PES bit */
180                 tmp |= (rtc->periodic_freq & ~PF_HP);   /* Set PES2-0 */
181         } else
182                 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
183
184         writeb(tmp, rtc->regbase + RCR2);
185
186         spin_unlock_irq(&rtc->lock);
187 }
188
189 static inline int sh_rtc_setfreq(struct device *dev, unsigned int freq)
190 {
191         struct sh_rtc *rtc = dev_get_drvdata(dev);
192         int tmp, ret = 0;
193
194         spin_lock_irq(&rtc->lock);
195         tmp = rtc->periodic_freq & PF_MASK;
196
197         switch (freq) {
198         case 0:
199                 rtc->periodic_freq = 0x00;
200                 break;
201         case 1:
202                 rtc->periodic_freq = 0x60;
203                 break;
204         case 2:
205                 rtc->periodic_freq = 0x50;
206                 break;
207         case 4:
208                 rtc->periodic_freq = 0x40;
209                 break;
210         case 8:
211                 rtc->periodic_freq = 0x30 | PF_HP;
212                 break;
213         case 16:
214                 rtc->periodic_freq = 0x30;
215                 break;
216         case 32:
217                 rtc->periodic_freq = 0x20 | PF_HP;
218                 break;
219         case 64:
220                 rtc->periodic_freq = 0x20;
221                 break;
222         case 128:
223                 rtc->periodic_freq = 0x10 | PF_HP;
224                 break;
225         case 256:
226                 rtc->periodic_freq = 0x10;
227                 break;
228         default:
229                 ret = -ENOTSUPP;
230         }
231
232         if (ret == 0) {
233                 rtc->periodic_freq |= tmp;
234                 rtc->rtc_dev->irq_freq = freq;
235         }
236
237         spin_unlock_irq(&rtc->lock);
238         return ret;
239 }
240
241 static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
242 {
243         struct sh_rtc *rtc = dev_get_drvdata(dev);
244         unsigned int tmp;
245
246         spin_lock_irq(&rtc->lock);
247
248         tmp = readb(rtc->regbase + RCR1);
249
250         if (!enable)
251                 tmp &= ~RCR1_AIE;
252         else
253                 tmp |= RCR1_AIE;
254
255         writeb(tmp, rtc->regbase + RCR1);
256
257         spin_unlock_irq(&rtc->lock);
258 }
259
260 static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
261 {
262         struct sh_rtc *rtc = dev_get_drvdata(dev);
263         unsigned int tmp;
264
265         tmp = readb(rtc->regbase + RCR1);
266         seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no");
267
268         tmp = readb(rtc->regbase + RCR2);
269         seq_printf(seq, "periodic_IRQ\t: %s\n",
270                    (tmp & RCR2_PESMASK) ? "yes" : "no");
271
272         return 0;
273 }
274
275 static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
276 {
277         struct sh_rtc *rtc = dev_get_drvdata(dev);
278         unsigned int ret = 0;
279
280         switch (cmd) {
281         case RTC_PIE_OFF:
282         case RTC_PIE_ON:
283                 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
284                 break;
285         case RTC_AIE_OFF:
286         case RTC_AIE_ON:
287                 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
288                 break;
289         case RTC_UIE_OFF:
290                 rtc->periodic_freq &= ~PF_OXS;
291                 break;
292         case RTC_UIE_ON:
293                 rtc->periodic_freq |= PF_OXS;
294                 break;
295         case RTC_IRQP_READ:
296                 ret = put_user(rtc->rtc_dev->irq_freq,
297                                (unsigned long __user *)arg);
298                 break;
299         case RTC_IRQP_SET:
300                 ret = sh_rtc_setfreq(dev, arg);
301                 break;
302         default:
303                 ret = -ENOIOCTLCMD;
304         }
305
306         return ret;
307 }
308
309 static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
310 {
311         struct platform_device *pdev = to_platform_device(dev);
312         struct sh_rtc *rtc = platform_get_drvdata(pdev);
313         unsigned int sec128, sec2, yr, yr100, cf_bit;
314
315         do {
316                 unsigned int tmp;
317
318                 spin_lock_irq(&rtc->lock);
319
320                 tmp = readb(rtc->regbase + RCR1);
321                 tmp &= ~RCR1_CF; /* Clear CF-bit */
322                 tmp |= RCR1_CIE;
323                 writeb(tmp, rtc->regbase + RCR1);
324
325                 sec128 = readb(rtc->regbase + R64CNT);
326
327                 tm->tm_sec      = BCD2BIN(readb(rtc->regbase + RSECCNT));
328                 tm->tm_min      = BCD2BIN(readb(rtc->regbase + RMINCNT));
329                 tm->tm_hour     = BCD2BIN(readb(rtc->regbase + RHRCNT));
330                 tm->tm_wday     = BCD2BIN(readb(rtc->regbase + RWKCNT));
331                 tm->tm_mday     = BCD2BIN(readb(rtc->regbase + RDAYCNT));
332                 tm->tm_mon      = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1;
333
334                 if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
335                         yr  = readw(rtc->regbase + RYRCNT);
336                         yr100 = BCD2BIN(yr >> 8);
337                         yr &= 0xff;
338                 } else {
339                         yr  = readb(rtc->regbase + RYRCNT);
340                         yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20);
341                 }
342
343                 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900;
344
345                 sec2 = readb(rtc->regbase + R64CNT);
346                 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
347
348                 spin_unlock_irq(&rtc->lock);
349         } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
350
351 #if RTC_BIT_INVERTED != 0
352         if ((sec128 & RTC_BIT_INVERTED))
353                 tm->tm_sec--;
354 #endif
355
356         dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
357                 "mday=%d, mon=%d, year=%d, wday=%d\n",
358                 __func__,
359                 tm->tm_sec, tm->tm_min, tm->tm_hour,
360                 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
361
362         if (rtc_valid_tm(tm) < 0) {
363                 dev_err(dev, "invalid date\n");
364                 rtc_time_to_tm(0, tm);
365         }
366
367         return 0;
368 }
369
370 static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
371 {
372         struct platform_device *pdev = to_platform_device(dev);
373         struct sh_rtc *rtc = platform_get_drvdata(pdev);
374         unsigned int tmp;
375         int year;
376
377         spin_lock_irq(&rtc->lock);
378
379         /* Reset pre-scaler & stop RTC */
380         tmp = readb(rtc->regbase + RCR2);
381         tmp |= RCR2_RESET;
382         tmp &= ~RCR2_START;
383         writeb(tmp, rtc->regbase + RCR2);
384
385         writeb(BIN2BCD(tm->tm_sec),  rtc->regbase + RSECCNT);
386         writeb(BIN2BCD(tm->tm_min),  rtc->regbase + RMINCNT);
387         writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
388         writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
389         writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
390         writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT);
391
392         if (rtc->capabilities & RTC_CAP_4_DIGIT_YEAR) {
393                 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) |
394                         BIN2BCD(tm->tm_year % 100);
395                 writew(year, rtc->regbase + RYRCNT);
396         } else {
397                 year = tm->tm_year % 100;
398                 writeb(BIN2BCD(year), rtc->regbase + RYRCNT);
399         }
400
401         /* Start RTC */
402         tmp = readb(rtc->regbase + RCR2);
403         tmp &= ~RCR2_RESET;
404         tmp |= RCR2_RTCEN | RCR2_START;
405         writeb(tmp, rtc->regbase + RCR2);
406
407         spin_unlock_irq(&rtc->lock);
408
409         return 0;
410 }
411
412 static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
413 {
414         unsigned int byte;
415         int value = 0xff;       /* return 0xff for ignored values */
416
417         byte = readb(rtc->regbase + reg_off);
418         if (byte & AR_ENB) {
419                 byte &= ~AR_ENB;        /* strip the enable bit */
420                 value = BCD2BIN(byte);
421         }
422
423         return value;
424 }
425
426 static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
427 {
428         struct platform_device *pdev = to_platform_device(dev);
429         struct sh_rtc *rtc = platform_get_drvdata(pdev);
430         struct rtc_time *tm = &wkalrm->time;
431
432         spin_lock_irq(&rtc->lock);
433
434         tm->tm_sec      = sh_rtc_read_alarm_value(rtc, RSECAR);
435         tm->tm_min      = sh_rtc_read_alarm_value(rtc, RMINAR);
436         tm->tm_hour     = sh_rtc_read_alarm_value(rtc, RHRAR);
437         tm->tm_wday     = sh_rtc_read_alarm_value(rtc, RWKAR);
438         tm->tm_mday     = sh_rtc_read_alarm_value(rtc, RDAYAR);
439         tm->tm_mon      = sh_rtc_read_alarm_value(rtc, RMONAR);
440         if (tm->tm_mon > 0)
441                 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
442         tm->tm_year     = 0xffff;
443
444         wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
445
446         spin_unlock_irq(&rtc->lock);
447
448         return 0;
449 }
450
451 static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
452                                             int value, int reg_off)
453 {
454         /* < 0 for a value that is ignored */
455         if (value < 0)
456                 writeb(0, rtc->regbase + reg_off);
457         else
458                 writeb(BIN2BCD(value) | AR_ENB,  rtc->regbase + reg_off);
459 }
460
461 static int sh_rtc_check_alarm(struct rtc_time *tm)
462 {
463         /*
464          * The original rtc says anything > 0xc0 is "don't care" or "match
465          * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
466          * The original rtc doesn't support years - some things use -1 and
467          * some 0xffff. We use -1 to make out tests easier.
468          */
469         if (tm->tm_year == 0xffff)
470                 tm->tm_year = -1;
471         if (tm->tm_mon >= 0xff)
472                 tm->tm_mon = -1;
473         if (tm->tm_mday >= 0xff)
474                 tm->tm_mday = -1;
475         if (tm->tm_wday >= 0xff)
476                 tm->tm_wday = -1;
477         if (tm->tm_hour >= 0xff)
478                 tm->tm_hour = -1;
479         if (tm->tm_min >= 0xff)
480                 tm->tm_min = -1;
481         if (tm->tm_sec >= 0xff)
482                 tm->tm_sec = -1;
483
484         if (tm->tm_year > 9999 ||
485                 tm->tm_mon >= 12 ||
486                 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
487                 tm->tm_wday >= 7 ||
488                 tm->tm_hour >= 24 ||
489                 tm->tm_min >= 60 ||
490                 tm->tm_sec >= 60)
491                 return -EINVAL;
492
493         return 0;
494 }
495
496 static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
497 {
498         struct platform_device *pdev = to_platform_device(dev);
499         struct sh_rtc *rtc = platform_get_drvdata(pdev);
500         unsigned int rcr1;
501         struct rtc_time *tm = &wkalrm->time;
502         int mon, err;
503
504         err = sh_rtc_check_alarm(tm);
505         if (unlikely(err < 0))
506                 return err;
507
508         spin_lock_irq(&rtc->lock);
509
510         /* disable alarm interrupt and clear the alarm flag */
511         rcr1 = readb(rtc->regbase + RCR1);
512         rcr1 &= ~(RCR1_AF | RCR1_AIE);
513         writeb(rcr1, rtc->regbase + RCR1);
514
515         /* set alarm time */
516         sh_rtc_write_alarm_value(rtc, tm->tm_sec,  RSECAR);
517         sh_rtc_write_alarm_value(rtc, tm->tm_min,  RMINAR);
518         sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
519         sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
520         sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
521         mon = tm->tm_mon;
522         if (mon >= 0)
523                 mon += 1;
524         sh_rtc_write_alarm_value(rtc, mon, RMONAR);
525
526         if (wkalrm->enabled) {
527                 rcr1 |= RCR1_AIE;
528                 writeb(rcr1, rtc->regbase + RCR1);
529         }
530
531         spin_unlock_irq(&rtc->lock);
532
533         return 0;
534 }
535
536 static int sh_rtc_irq_set_state(struct device *dev, int enabled)
537 {
538         struct platform_device *pdev = to_platform_device(dev);
539         struct sh_rtc *rtc = platform_get_drvdata(pdev);
540
541         if (enabled) {
542                 rtc->periodic_freq |= PF_KOU;
543                 return sh_rtc_ioctl(dev, RTC_PIE_ON, 0);
544         } else {
545                 rtc->periodic_freq &= ~PF_KOU;
546                 return sh_rtc_ioctl(dev, RTC_PIE_OFF, 0);
547         }
548 }
549
550 static int sh_rtc_irq_set_freq(struct device *dev, int freq)
551 {
552         return sh_rtc_ioctl(dev, RTC_IRQP_SET, freq);
553 }
554
555 static struct rtc_class_ops sh_rtc_ops = {
556         .ioctl          = sh_rtc_ioctl,
557         .read_time      = sh_rtc_read_time,
558         .set_time       = sh_rtc_set_time,
559         .read_alarm     = sh_rtc_read_alarm,
560         .set_alarm      = sh_rtc_set_alarm,
561         .irq_set_state  = sh_rtc_irq_set_state,
562         .irq_set_freq   = sh_rtc_irq_set_freq,
563         .proc           = sh_rtc_proc,
564 };
565
566 static int __devinit sh_rtc_probe(struct platform_device *pdev)
567 {
568         struct sh_rtc *rtc;
569         struct resource *res;
570         unsigned int tmp;
571         int ret = -ENOENT;
572
573         rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
574         if (unlikely(!rtc))
575                 return -ENOMEM;
576
577         spin_lock_init(&rtc->lock);
578
579         /* get periodic/carry/alarm irqs */
580         rtc->periodic_irq = platform_get_irq(pdev, 0);
581         if (unlikely(rtc->periodic_irq < 0)) {
582                 dev_err(&pdev->dev, "No IRQ for period\n");
583                 goto err_badres;
584         }
585
586         rtc->carry_irq = platform_get_irq(pdev, 1);
587         if (unlikely(rtc->carry_irq < 0)) {
588                 dev_err(&pdev->dev, "No IRQ for carry\n");
589                 goto err_badres;
590         }
591
592         rtc->alarm_irq = platform_get_irq(pdev, 2);
593         if (unlikely(rtc->alarm_irq < 0)) {
594                 dev_err(&pdev->dev, "No IRQ for alarm\n");
595                 goto err_badres;
596         }
597
598         res = platform_get_resource(pdev, IORESOURCE_IO, 0);
599         if (unlikely(res == NULL)) {
600                 dev_err(&pdev->dev, "No IO resource\n");
601                 goto err_badres;
602         }
603
604         rtc->regsize = res->end - res->start + 1;
605
606         rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
607         if (unlikely(!rtc->res)) {
608                 ret = -EBUSY;
609                 goto err_badres;
610         }
611
612         rtc->regbase = ioremap_nocache(rtc->res->start, rtc->regsize);
613         if (unlikely(!rtc->regbase)) {
614                 ret = -EINVAL;
615                 goto err_badmap;
616         }
617
618         rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
619                                            &sh_rtc_ops, THIS_MODULE);
620         if (IS_ERR(rtc->rtc_dev)) {
621                 ret = PTR_ERR(rtc->rtc_dev);
622                 goto err_unmap;
623         }
624
625         rtc->capabilities = RTC_DEF_CAPABILITIES;
626         if (pdev->dev.platform_data) {
627                 struct sh_rtc_platform_info *pinfo = pdev->dev.platform_data;
628
629                 /*
630                  * Some CPUs have special capabilities in addition to the
631                  * default set. Add those in here.
632                  */
633                 rtc->capabilities |= pinfo->capabilities;
634         }
635
636         rtc->rtc_dev->max_user_freq = 256;
637         rtc->rtc_dev->irq_freq = 1;
638         rtc->periodic_freq = 0x60;
639
640         platform_set_drvdata(pdev, rtc);
641
642         /* register periodic/carry/alarm irqs */
643         ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED,
644                           "sh-rtc period", rtc);
645         if (unlikely(ret)) {
646                 dev_err(&pdev->dev,
647                         "request period IRQ failed with %d, IRQ %d\n", ret,
648                         rtc->periodic_irq);
649                 goto err_unmap;
650         }
651
652         ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED,
653                           "sh-rtc carry", rtc);
654         if (unlikely(ret)) {
655                 dev_err(&pdev->dev,
656                         "request carry IRQ failed with %d, IRQ %d\n", ret,
657                         rtc->carry_irq);
658                 free_irq(rtc->periodic_irq, rtc);
659                 goto err_unmap;
660         }
661
662         ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
663                           "sh-rtc alarm", rtc);
664         if (unlikely(ret)) {
665                 dev_err(&pdev->dev,
666                         "request alarm IRQ failed with %d, IRQ %d\n", ret,
667                         rtc->alarm_irq);
668                 free_irq(rtc->carry_irq, rtc);
669                 free_irq(rtc->periodic_irq, rtc);
670                 goto err_unmap;
671         }
672
673         tmp = readb(rtc->regbase + RCR1);
674         tmp &= ~RCR1_CF;
675         tmp |= RCR1_CIE;
676         writeb(tmp, rtc->regbase + RCR1);
677
678         return 0;
679
680 err_unmap:
681         iounmap(rtc->regbase);
682 err_badmap:
683         release_resource(rtc->res);
684 err_badres:
685         kfree(rtc);
686
687         return ret;
688 }
689
690 static int __devexit sh_rtc_remove(struct platform_device *pdev)
691 {
692         struct sh_rtc *rtc = platform_get_drvdata(pdev);
693
694         if (likely(rtc->rtc_dev))
695                 rtc_device_unregister(rtc->rtc_dev);
696
697         sh_rtc_setpie(&pdev->dev, 0);
698         sh_rtc_setaie(&pdev->dev, 0);
699
700         free_irq(rtc->carry_irq, rtc);
701         free_irq(rtc->periodic_irq, rtc);
702         free_irq(rtc->alarm_irq, rtc);
703
704         release_resource(rtc->res);
705
706         iounmap(rtc->regbase);
707
708         platform_set_drvdata(pdev, NULL);
709
710         kfree(rtc);
711
712         return 0;
713 }
714 static struct platform_driver sh_rtc_platform_driver = {
715         .driver         = {
716                 .name   = DRV_NAME,
717                 .owner  = THIS_MODULE,
718         },
719         .probe          = sh_rtc_probe,
720         .remove         = __devexit_p(sh_rtc_remove),
721 };
722
723 static int __init sh_rtc_init(void)
724 {
725         return platform_driver_register(&sh_rtc_platform_driver);
726 }
727
728 static void __exit sh_rtc_exit(void)
729 {
730         platform_driver_unregister(&sh_rtc_platform_driver);
731 }
732
733 module_init(sh_rtc_init);
734 module_exit(sh_rtc_exit);
735
736 MODULE_DESCRIPTION("SuperH on-chip RTC driver");
737 MODULE_VERSION(DRV_VERSION);
738 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, "
739               "Jamie Lenehan <lenehan@twibble.org>, "
740               "Angelo Castello <angelo.castello@st.com>");
741 MODULE_LICENSE("GPL");
742 MODULE_ALIAS("platform:" DRV_NAME);