blob: e0f91dfce0f56bcb8f340058d208557e613a44dd [file] [log] [blame]
Paul Mundt317a6102006-09-27 17:13:19 +09001/*
2 * SuperH On-Chip RTC Support
3 *
4 * Copyright (C) 2006 Paul Mundt
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +09005 * Copyright (C) 2006 Jamie Lenehan
Paul Mundt317a6102006-09-27 17:13:19 +09006 *
7 * Based on the old arch/sh/kernel/cpu/rtc.c by:
8 *
9 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
10 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
11 *
12 * This file is subject to the terms and conditions of the GNU General Public
13 * License. See the file "COPYING" in the main directory of this archive
14 * for more details.
15 */
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/bcd.h>
19#include <linux/rtc.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/seq_file.h>
23#include <linux/interrupt.h>
24#include <linux/spinlock.h>
Jamie Lenehan31ccb082006-12-07 17:23:50 +090025#include <linux/io.h>
Paul Mundt317a6102006-09-27 17:13:19 +090026
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090027#define DRV_NAME "sh-rtc"
28#define DRV_VERSION "0.1.2"
29
Paul Mundt317a6102006-09-27 17:13:19 +090030#ifdef CONFIG_CPU_SH3
31#define rtc_reg_size sizeof(u16)
32#define RTC_BIT_INVERTED 0 /* No bug on SH7708, SH7709A */
33#elif defined(CONFIG_CPU_SH4)
34#define rtc_reg_size sizeof(u32)
35#define RTC_BIT_INVERTED 0x40 /* bug on SH7750, SH7750S */
36#endif
37
38#define RTC_REG(r) ((r) * rtc_reg_size)
39
Jamie Lenehan31ccb082006-12-07 17:23:50 +090040#define R64CNT RTC_REG(0)
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090041
42#define RSECCNT RTC_REG(1) /* RTC sec */
43#define RMINCNT RTC_REG(2) /* RTC min */
44#define RHRCNT RTC_REG(3) /* RTC hour */
45#define RWKCNT RTC_REG(4) /* RTC week */
46#define RDAYCNT RTC_REG(5) /* RTC day */
47#define RMONCNT RTC_REG(6) /* RTC month */
48#define RYRCNT RTC_REG(7) /* RTC year */
49#define RSECAR RTC_REG(8) /* ALARM sec */
50#define RMINAR RTC_REG(9) /* ALARM min */
51#define RHRAR RTC_REG(10) /* ALARM hour */
52#define RWKAR RTC_REG(11) /* ALARM week */
53#define RDAYAR RTC_REG(12) /* ALARM day */
54#define RMONAR RTC_REG(13) /* ALARM month */
55#define RCR1 RTC_REG(14) /* Control */
56#define RCR2 RTC_REG(15) /* Control */
57
58/* ALARM Bits - or with BCD encoded value */
59#define AR_ENB 0x80 /* Enable for alarm cmp */
Paul Mundt317a6102006-09-27 17:13:19 +090060
61/* RCR1 Bits */
62#define RCR1_CF 0x80 /* Carry Flag */
63#define RCR1_CIE 0x10 /* Carry Interrupt Enable */
64#define RCR1_AIE 0x08 /* Alarm Interrupt Enable */
65#define RCR1_AF 0x01 /* Alarm Flag */
66
67/* RCR2 Bits */
68#define RCR2_PEF 0x80 /* PEriodic interrupt Flag */
69#define RCR2_PESMASK 0x70 /* Periodic interrupt Set */
70#define RCR2_RTCEN 0x08 /* ENable RTC */
71#define RCR2_ADJ 0x04 /* ADJustment (30-second) */
72#define RCR2_RESET 0x02 /* Reset bit */
73#define RCR2_START 0x01 /* Start bit */
74
75struct sh_rtc {
76 void __iomem *regbase;
77 unsigned long regsize;
78 struct resource *res;
79 unsigned int alarm_irq, periodic_irq, carry_irq;
80 struct rtc_device *rtc_dev;
81 spinlock_t lock;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090082 int rearm_aie;
Paul Mundt317a6102006-09-27 17:13:19 +090083};
84
Jamie Lenehan31ccb082006-12-07 17:23:50 +090085static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
Paul Mundt317a6102006-09-27 17:13:19 +090086{
Jamie Lenehan31ccb082006-12-07 17:23:50 +090087 struct platform_device *pdev = to_platform_device(dev_id);
Paul Mundt317a6102006-09-27 17:13:19 +090088 struct sh_rtc *rtc = platform_get_drvdata(pdev);
89 unsigned int tmp, events = 0;
90
91 spin_lock(&rtc->lock);
92
93 tmp = readb(rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090094 tmp &= ~RCR1_CF;
Paul Mundt317a6102006-09-27 17:13:19 +090095
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +090096 if (rtc->rearm_aie) {
97 if (tmp & RCR1_AF)
98 tmp &= ~RCR1_AF; /* try to clear AF again */
99 else {
100 tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */
101 rtc->rearm_aie = 0;
102 }
103 }
Paul Mundt317a6102006-09-27 17:13:19 +0900104
105 writeb(tmp, rtc->regbase + RCR1);
106
Paul Mundta361a682007-05-09 12:37:53 +0900107 rtc_update_irq(rtc->rtc_dev, 1, events);
Paul Mundt317a6102006-09-27 17:13:19 +0900108
109 spin_unlock(&rtc->lock);
110
111 return IRQ_HANDLED;
112}
113
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900114static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
115{
116 struct platform_device *pdev = to_platform_device(dev_id);
117 struct sh_rtc *rtc = platform_get_drvdata(pdev);
118 unsigned int tmp, events = 0;
119
120 spin_lock(&rtc->lock);
121
122 tmp = readb(rtc->regbase + RCR1);
123
124 /*
125 * If AF is set then the alarm has triggered. If we clear AF while
126 * the alarm time still matches the RTC time then AF will
127 * immediately be set again, and if AIE is enabled then the alarm
128 * interrupt will immediately be retrigger. So we clear AIE here
129 * and use rtc->rearm_aie so that the carry interrupt will keep
130 * trying to clear AF and once it stays cleared it'll re-enable
131 * AIE.
132 */
133 if (tmp & RCR1_AF) {
134 events |= RTC_AF | RTC_IRQF;
135
136 tmp &= ~(RCR1_AF|RCR1_AIE);
137
138 writeb(tmp, rtc->regbase + RCR1);
139
140 rtc->rearm_aie = 1;
141
Paul Mundta361a682007-05-09 12:37:53 +0900142 rtc_update_irq(rtc->rtc_dev, 1, events);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900143 }
144
145 spin_unlock(&rtc->lock);
146 return IRQ_HANDLED;
147}
148
Jamie Lenehan31ccb082006-12-07 17:23:50 +0900149static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
Paul Mundt317a6102006-09-27 17:13:19 +0900150{
Jamie Lenehan31ccb082006-12-07 17:23:50 +0900151 struct platform_device *pdev = to_platform_device(dev_id);
152 struct sh_rtc *rtc = platform_get_drvdata(pdev);
Paul Mundt317a6102006-09-27 17:13:19 +0900153
154 spin_lock(&rtc->lock);
155
Paul Mundta361a682007-05-09 12:37:53 +0900156 rtc_update_irq(rtc->rtc_dev, 1, RTC_PF | RTC_IRQF);
Paul Mundt317a6102006-09-27 17:13:19 +0900157
158 spin_unlock(&rtc->lock);
159
160 return IRQ_HANDLED;
161}
162
163static inline void sh_rtc_setpie(struct device *dev, unsigned int enable)
164{
165 struct sh_rtc *rtc = dev_get_drvdata(dev);
166 unsigned int tmp;
167
168 spin_lock_irq(&rtc->lock);
169
170 tmp = readb(rtc->regbase + RCR2);
171
172 if (enable) {
173 tmp &= ~RCR2_PESMASK;
174 tmp |= RCR2_PEF | (2 << 4);
175 } else
176 tmp &= ~(RCR2_PESMASK | RCR2_PEF);
177
178 writeb(tmp, rtc->regbase + RCR2);
179
180 spin_unlock_irq(&rtc->lock);
181}
182
183static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
184{
185 struct sh_rtc *rtc = dev_get_drvdata(dev);
186 unsigned int tmp;
187
188 spin_lock_irq(&rtc->lock);
189
190 tmp = readb(rtc->regbase + RCR1);
191
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900192 if (!enable) {
Paul Mundt317a6102006-09-27 17:13:19 +0900193 tmp &= ~RCR1_AIE;
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900194 rtc->rearm_aie = 0;
195 } else if (rtc->rearm_aie == 0)
196 tmp |= RCR1_AIE;
Paul Mundt317a6102006-09-27 17:13:19 +0900197
198 writeb(tmp, rtc->regbase + RCR1);
199
200 spin_unlock_irq(&rtc->lock);
201}
202
203static int sh_rtc_open(struct device *dev)
204{
205 struct sh_rtc *rtc = dev_get_drvdata(dev);
206 unsigned int tmp;
207 int ret;
208
209 tmp = readb(rtc->regbase + RCR1);
210 tmp &= ~RCR1_CF;
211 tmp |= RCR1_CIE;
212 writeb(tmp, rtc->regbase + RCR1);
213
Paul Mundt35f3c512006-10-06 15:31:16 +0900214 ret = request_irq(rtc->periodic_irq, sh_rtc_periodic, IRQF_DISABLED,
Paul Mundt317a6102006-09-27 17:13:19 +0900215 "sh-rtc period", dev);
216 if (unlikely(ret)) {
217 dev_err(dev, "request period IRQ failed with %d, IRQ %d\n",
218 ret, rtc->periodic_irq);
219 return ret;
220 }
221
Paul Mundt35f3c512006-10-06 15:31:16 +0900222 ret = request_irq(rtc->carry_irq, sh_rtc_interrupt, IRQF_DISABLED,
Paul Mundt317a6102006-09-27 17:13:19 +0900223 "sh-rtc carry", dev);
224 if (unlikely(ret)) {
225 dev_err(dev, "request carry IRQ failed with %d, IRQ %d\n",
226 ret, rtc->carry_irq);
227 free_irq(rtc->periodic_irq, dev);
228 goto err_bad_carry;
229 }
230
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900231 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
Paul Mundt317a6102006-09-27 17:13:19 +0900232 "sh-rtc alarm", dev);
233 if (unlikely(ret)) {
234 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n",
235 ret, rtc->alarm_irq);
236 goto err_bad_alarm;
237 }
238
239 return 0;
240
241err_bad_alarm:
242 free_irq(rtc->carry_irq, dev);
243err_bad_carry:
244 free_irq(rtc->periodic_irq, dev);
245
246 return ret;
247}
248
249static void sh_rtc_release(struct device *dev)
250{
251 struct sh_rtc *rtc = dev_get_drvdata(dev);
252
253 sh_rtc_setpie(dev, 0);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900254 sh_rtc_setaie(dev, 0);
Paul Mundt317a6102006-09-27 17:13:19 +0900255
256 free_irq(rtc->periodic_irq, dev);
257 free_irq(rtc->carry_irq, dev);
258 free_irq(rtc->alarm_irq, dev);
259}
260
261static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
262{
263 struct sh_rtc *rtc = dev_get_drvdata(dev);
264 unsigned int tmp;
265
266 tmp = readb(rtc->regbase + RCR1);
Paul Mundt317a6102006-09-27 17:13:19 +0900267 seq_printf(seq, "carry_IRQ\t: %s\n",
268 (tmp & RCR1_CIE) ? "yes" : "no");
269
270 tmp = readb(rtc->regbase + RCR2);
271 seq_printf(seq, "periodic_IRQ\t: %s\n",
272 (tmp & RCR2_PEF) ? "yes" : "no");
273
274 return 0;
275}
276
277static int sh_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
278{
279 unsigned int ret = -ENOIOCTLCMD;
280
281 switch (cmd) {
282 case RTC_PIE_OFF:
283 case RTC_PIE_ON:
284 sh_rtc_setpie(dev, cmd == RTC_PIE_ON);
285 ret = 0;
286 break;
287 case RTC_AIE_OFF:
288 case RTC_AIE_ON:
289 sh_rtc_setaie(dev, cmd == RTC_AIE_ON);
290 ret = 0;
291 break;
292 }
293
294 return ret;
295}
296
297static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
298{
299 struct platform_device *pdev = to_platform_device(dev);
300 struct sh_rtc *rtc = platform_get_drvdata(pdev);
301 unsigned int sec128, sec2, yr, yr100, cf_bit;
302
303 do {
304 unsigned int tmp;
305
306 spin_lock_irq(&rtc->lock);
307
308 tmp = readb(rtc->regbase + RCR1);
309 tmp &= ~RCR1_CF; /* Clear CF-bit */
310 tmp |= RCR1_CIE;
311 writeb(tmp, rtc->regbase + RCR1);
312
313 sec128 = readb(rtc->regbase + R64CNT);
314
315 tm->tm_sec = BCD2BIN(readb(rtc->regbase + RSECCNT));
316 tm->tm_min = BCD2BIN(readb(rtc->regbase + RMINCNT));
317 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT));
318 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT));
319 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT));
Jamie Lenehana1614792006-12-08 14:49:30 +0900320 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1;
Paul Mundt317a6102006-09-27 17:13:19 +0900321
322#if defined(CONFIG_CPU_SH4)
323 yr = readw(rtc->regbase + RYRCNT);
324 yr100 = BCD2BIN(yr >> 8);
325 yr &= 0xff;
326#else
327 yr = readb(rtc->regbase + RYRCNT);
328 yr100 = BCD2BIN((yr == 0x99) ? 0x19 : 0x20);
329#endif
330
331 tm->tm_year = (yr100 * 100 + BCD2BIN(yr)) - 1900;
332
333 sec2 = readb(rtc->regbase + R64CNT);
334 cf_bit = readb(rtc->regbase + RCR1) & RCR1_CF;
335
336 spin_unlock_irq(&rtc->lock);
337 } while (cf_bit != 0 || ((sec128 ^ sec2) & RTC_BIT_INVERTED) != 0);
338
339#if RTC_BIT_INVERTED != 0
340 if ((sec128 & RTC_BIT_INVERTED))
341 tm->tm_sec--;
342#endif
343
Paul Mundt435c55d2007-05-08 11:56:27 +0900344 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
Paul Mundt317a6102006-09-27 17:13:19 +0900345 "mday=%d, mon=%d, year=%d, wday=%d\n",
346 __FUNCTION__,
347 tm->tm_sec, tm->tm_min, tm->tm_hour,
Jamie Lenehana1614792006-12-08 14:49:30 +0900348 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
Paul Mundt317a6102006-09-27 17:13:19 +0900349
350 if (rtc_valid_tm(tm) < 0)
351 dev_err(dev, "invalid date\n");
352
353 return 0;
354}
355
356static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
357{
358 struct platform_device *pdev = to_platform_device(dev);
359 struct sh_rtc *rtc = platform_get_drvdata(pdev);
360 unsigned int tmp;
361 int year;
362
363 spin_lock_irq(&rtc->lock);
364
365 /* Reset pre-scaler & stop RTC */
366 tmp = readb(rtc->regbase + RCR2);
367 tmp |= RCR2_RESET;
368 writeb(tmp, rtc->regbase + RCR2);
369
370 writeb(BIN2BCD(tm->tm_sec), rtc->regbase + RSECCNT);
371 writeb(BIN2BCD(tm->tm_min), rtc->regbase + RMINCNT);
372 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
373 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
374 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
Jamie Lenehana1614792006-12-08 14:49:30 +0900375 writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT);
Paul Mundt317a6102006-09-27 17:13:19 +0900376
377#ifdef CONFIG_CPU_SH3
378 year = tm->tm_year % 100;
379 writeb(BIN2BCD(year), rtc->regbase + RYRCNT);
380#else
381 year = (BIN2BCD((tm->tm_year + 1900) / 100) << 8) |
382 BIN2BCD(tm->tm_year % 100);
383 writew(year, rtc->regbase + RYRCNT);
384#endif
385
386 /* Start RTC */
387 tmp = readb(rtc->regbase + RCR2);
388 tmp &= ~RCR2_RESET;
389 tmp |= RCR2_RTCEN | RCR2_START;
390 writeb(tmp, rtc->regbase + RCR2);
391
392 spin_unlock_irq(&rtc->lock);
393
394 return 0;
395}
396
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900397static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
398{
399 unsigned int byte;
400 int value = 0xff; /* return 0xff for ignored values */
401
402 byte = readb(rtc->regbase + reg_off);
403 if (byte & AR_ENB) {
404 byte &= ~AR_ENB; /* strip the enable bit */
405 value = BCD2BIN(byte);
406 }
407
408 return value;
409}
410
411static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
412{
413 struct platform_device *pdev = to_platform_device(dev);
414 struct sh_rtc *rtc = platform_get_drvdata(pdev);
415 struct rtc_time* tm = &wkalrm->time;
416
417 spin_lock_irq(&rtc->lock);
418
419 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
420 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
421 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
422 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
423 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
424 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
425 if (tm->tm_mon > 0)
426 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
427 tm->tm_year = 0xffff;
428
David Brownell0d103e92007-01-10 23:15:32 -0800429 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
430
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900431 spin_unlock_irq(&rtc->lock);
432
433 return 0;
434}
435
436static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
437 int value, int reg_off)
438{
439 /* < 0 for a value that is ignored */
440 if (value < 0)
441 writeb(0, rtc->regbase + reg_off);
442 else
443 writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off);
444}
445
446static int sh_rtc_check_alarm(struct rtc_time* tm)
447{
448 /*
449 * The original rtc says anything > 0xc0 is "don't care" or "match
450 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
451 * The original rtc doesn't support years - some things use -1 and
452 * some 0xffff. We use -1 to make out tests easier.
453 */
454 if (tm->tm_year == 0xffff)
455 tm->tm_year = -1;
456 if (tm->tm_mon >= 0xff)
457 tm->tm_mon = -1;
458 if (tm->tm_mday >= 0xff)
459 tm->tm_mday = -1;
460 if (tm->tm_wday >= 0xff)
461 tm->tm_wday = -1;
462 if (tm->tm_hour >= 0xff)
463 tm->tm_hour = -1;
464 if (tm->tm_min >= 0xff)
465 tm->tm_min = -1;
466 if (tm->tm_sec >= 0xff)
467 tm->tm_sec = -1;
468
469 if (tm->tm_year > 9999 ||
470 tm->tm_mon >= 12 ||
471 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
472 tm->tm_wday >= 7 ||
473 tm->tm_hour >= 24 ||
474 tm->tm_min >= 60 ||
475 tm->tm_sec >= 60)
476 return -EINVAL;
477
478 return 0;
479}
480
481static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
482{
483 struct platform_device *pdev = to_platform_device(dev);
484 struct sh_rtc *rtc = platform_get_drvdata(pdev);
485 unsigned int rcr1;
486 struct rtc_time *tm = &wkalrm->time;
487 int mon, err;
488
489 err = sh_rtc_check_alarm(tm);
490 if (unlikely(err < 0))
491 return err;
492
493 spin_lock_irq(&rtc->lock);
494
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800495 /* disable alarm interrupt and clear the alarm flag */
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900496 rcr1 = readb(rtc->regbase + RCR1);
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800497 rcr1 &= ~(RCR1_AF|RCR1_AIE);
498 writeb(rcr1, rtc->regbase + RCR1);
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900499
500 rtc->rearm_aie = 0;
501
502 /* set alarm time */
503 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
504 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
505 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
506 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
507 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
508 mon = tm->tm_mon;
509 if (mon >= 0)
510 mon += 1;
511 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
512
Jamie Lenehan15c945c2007-01-22 20:40:41 -0800513 if (wkalrm->enabled) {
514 rcr1 |= RCR1_AIE;
515 writeb(rcr1, rtc->regbase + RCR1);
516 }
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900517
518 spin_unlock_irq(&rtc->lock);
519
520 return 0;
521}
522
Paul Mundt317a6102006-09-27 17:13:19 +0900523static struct rtc_class_ops sh_rtc_ops = {
524 .open = sh_rtc_open,
525 .release = sh_rtc_release,
526 .ioctl = sh_rtc_ioctl,
527 .read_time = sh_rtc_read_time,
528 .set_time = sh_rtc_set_time,
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900529 .read_alarm = sh_rtc_read_alarm,
530 .set_alarm = sh_rtc_set_alarm,
Paul Mundt317a6102006-09-27 17:13:19 +0900531 .proc = sh_rtc_proc,
532};
533
534static int __devinit sh_rtc_probe(struct platform_device *pdev)
535{
536 struct sh_rtc *rtc;
537 struct resource *res;
538 int ret = -ENOENT;
539
540 rtc = kzalloc(sizeof(struct sh_rtc), GFP_KERNEL);
541 if (unlikely(!rtc))
542 return -ENOMEM;
543
544 spin_lock_init(&rtc->lock);
545
546 rtc->periodic_irq = platform_get_irq(pdev, 0);
547 if (unlikely(rtc->periodic_irq < 0)) {
548 dev_err(&pdev->dev, "No IRQ for period\n");
549 goto err_badres;
550 }
551
552 rtc->carry_irq = platform_get_irq(pdev, 1);
553 if (unlikely(rtc->carry_irq < 0)) {
554 dev_err(&pdev->dev, "No IRQ for carry\n");
555 goto err_badres;
556 }
557
558 rtc->alarm_irq = platform_get_irq(pdev, 2);
559 if (unlikely(rtc->alarm_irq < 0)) {
560 dev_err(&pdev->dev, "No IRQ for alarm\n");
561 goto err_badres;
562 }
563
564 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
565 if (unlikely(res == NULL)) {
566 dev_err(&pdev->dev, "No IO resource\n");
567 goto err_badres;
568 }
569
570 rtc->regsize = res->end - res->start + 1;
571
572 rtc->res = request_mem_region(res->start, rtc->regsize, pdev->name);
573 if (unlikely(!rtc->res)) {
574 ret = -EBUSY;
575 goto err_badres;
576 }
577
578 rtc->regbase = (void __iomem *)rtc->res->start;
579 if (unlikely(!rtc->regbase)) {
580 ret = -EINVAL;
581 goto err_badmap;
582 }
583
584 rtc->rtc_dev = rtc_device_register("sh", &pdev->dev,
585 &sh_rtc_ops, THIS_MODULE);
586 if (IS_ERR(rtc)) {
587 ret = PTR_ERR(rtc->rtc_dev);
588 goto err_badmap;
589 }
590
591 platform_set_drvdata(pdev, rtc);
592
593 return 0;
594
595err_badmap:
596 release_resource(rtc->res);
597err_badres:
598 kfree(rtc);
599
600 return ret;
601}
602
603static int __devexit sh_rtc_remove(struct platform_device *pdev)
604{
605 struct sh_rtc *rtc = platform_get_drvdata(pdev);
606
607 if (likely(rtc->rtc_dev))
608 rtc_device_unregister(rtc->rtc_dev);
609
610 sh_rtc_setpie(&pdev->dev, 0);
611 sh_rtc_setaie(&pdev->dev, 0);
612
613 release_resource(rtc->res);
614
615 platform_set_drvdata(pdev, NULL);
616
617 kfree(rtc);
618
619 return 0;
620}
621static struct platform_driver sh_rtc_platform_driver = {
622 .driver = {
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900623 .name = DRV_NAME,
Paul Mundt317a6102006-09-27 17:13:19 +0900624 .owner = THIS_MODULE,
625 },
626 .probe = sh_rtc_probe,
627 .remove = __devexit_p(sh_rtc_remove),
628};
629
630static int __init sh_rtc_init(void)
631{
632 return platform_driver_register(&sh_rtc_platform_driver);
633}
634
635static void __exit sh_rtc_exit(void)
636{
637 platform_driver_unregister(&sh_rtc_platform_driver);
638}
639
640module_init(sh_rtc_init);
641module_exit(sh_rtc_exit);
642
643MODULE_DESCRIPTION("SuperH on-chip RTC driver");
Jamie Lenehan1b73e6a2006-12-08 15:26:15 +0900644MODULE_VERSION(DRV_VERSION);
645MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>");
Paul Mundt317a6102006-09-27 17:13:19 +0900646MODULE_LICENSE("GPL");