blob: fed86e507fdfb87aa17e610d1758317731fc1990 [file] [log] [blame]
Ben Dooks1add6782006-07-01 04:36:26 -07001/* 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>
Robert P. J. Day9974b6e2008-02-06 01:38:42 -080023#include <linux/log2.h>
Ben Dooks1add6782006-07-01 04:36:26 -070024
25#include <asm/hardware.h>
26#include <asm/uaccess.h>
27#include <asm/io.h>
28#include <asm/irq.h>
Ben Dooks252e4832007-07-31 00:37:28 -070029#include <asm/plat-s3c/regs-rtc.h>
Ben Dooks1add6782006-07-01 04:36:26 -070030
31/* I have yet to find an S3C implementation with more than one
32 * of these rtc blocks in */
33
34static struct resource *s3c_rtc_mem;
35
36static void __iomem *s3c_rtc_base;
37static int s3c_rtc_alarmno = NO_IRQ;
38static int s3c_rtc_tickno = NO_IRQ;
39static int s3c_rtc_freq = 1;
40
41static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
42static unsigned int tick_count;
43
44/* IRQ Handlers */
45
David Howells7d12e782006-10-05 14:55:46 +010046static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070047{
48 struct rtc_device *rdev = id;
49
David Brownellab6a2d72007-05-08 00:33:30 -070050 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
Ben Dooks1add6782006-07-01 04:36:26 -070051 return IRQ_HANDLED;
52}
53
David Howells7d12e782006-10-05 14:55:46 +010054static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070055{
56 struct rtc_device *rdev = id;
57
David Brownellab6a2d72007-05-08 00:33:30 -070058 rtc_update_irq(rdev, tick_count++, RTC_PF | RTC_IRQF);
Ben Dooks1add6782006-07-01 04:36:26 -070059 return IRQ_HANDLED;
60}
61
62/* Update control registers */
63static void s3c_rtc_setaie(int to)
64{
65 unsigned int tmp;
66
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070067 pr_debug("%s: aie=%d\n", __func__, to);
Ben Dooks1add6782006-07-01 04:36:26 -070068
Ben Dooks9a654512006-08-27 01:23:22 -070069 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -070070
71 if (to)
72 tmp |= S3C2410_RTCALM_ALMEN;
73
Ben Dooks9a654512006-08-27 01:23:22 -070074 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -070075}
76
77static void s3c_rtc_setpie(int to)
78{
79 unsigned int tmp;
80
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070081 pr_debug("%s: pie=%d\n", __func__, to);
Ben Dooks1add6782006-07-01 04:36:26 -070082
83 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks9a654512006-08-27 01:23:22 -070084 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
Ben Dooks1add6782006-07-01 04:36:26 -070085
86 if (to)
87 tmp |= S3C2410_TICNT_ENABLE;
88
Ben Dooks9a654512006-08-27 01:23:22 -070089 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -070090 spin_unlock_irq(&s3c_rtc_pie_lock);
91}
92
93static void s3c_rtc_setfreq(int freq)
94{
95 unsigned int tmp;
96
97 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks9a654512006-08-27 01:23:22 -070098 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
Ben Dooks1add6782006-07-01 04:36:26 -070099
100 s3c_rtc_freq = freq;
101
102 tmp |= (128 / freq)-1;
103
Ben Dooks9a654512006-08-27 01:23:22 -0700104 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700105 spin_unlock_irq(&s3c_rtc_pie_lock);
106}
107
108/* Time read/write */
109
110static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
111{
112 unsigned int have_retried = 0;
Ben Dooks9a654512006-08-27 01:23:22 -0700113 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700114
115 retry_get_time:
Ben Dooks9a654512006-08-27 01:23:22 -0700116 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
117 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
118 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
119 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
120 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
121 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700122
123 /* the only way to work out wether the system was mid-update
124 * when we read it is to check the second counter, and if it
125 * is zero, then we re-try the entire read
126 */
127
128 if (rtc_tm->tm_sec == 0 && !have_retried) {
129 have_retried = 1;
130 goto retry_get_time;
131 }
132
133 pr_debug("read time %02x.%02x.%02x %02x/%02x/%02x\n",
134 rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
135 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
136
137 BCD_TO_BIN(rtc_tm->tm_sec);
138 BCD_TO_BIN(rtc_tm->tm_min);
139 BCD_TO_BIN(rtc_tm->tm_hour);
140 BCD_TO_BIN(rtc_tm->tm_mday);
141 BCD_TO_BIN(rtc_tm->tm_mon);
142 BCD_TO_BIN(rtc_tm->tm_year);
143
144 rtc_tm->tm_year += 100;
145 rtc_tm->tm_mon -= 1;
146
147 return 0;
148}
149
150static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
151{
Ben Dooks9a654512006-08-27 01:23:22 -0700152 void __iomem *base = s3c_rtc_base;
Ben Dooks641741e2006-08-27 01:23:27 -0700153 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700154
Ben Dooks9a654512006-08-27 01:23:22 -0700155 pr_debug("set time %02d.%02d.%02d %02d/%02d/%02d\n",
156 tm->tm_year, tm->tm_mon, tm->tm_mday,
157 tm->tm_hour, tm->tm_min, tm->tm_sec);
158
Ben Dooks641741e2006-08-27 01:23:27 -0700159 /* we get around y2k by simply not supporting it */
160
161 if (year < 0 || year >= 100) {
162 dev_err(dev, "rtc only supports 100 years\n");
163 return -EINVAL;
164 }
165
Ben Dooks9a654512006-08-27 01:23:22 -0700166 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_RTCSEC);
167 writeb(BIN2BCD(tm->tm_min), base + S3C2410_RTCMIN);
168 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_RTCHOUR);
169 writeb(BIN2BCD(tm->tm_mday), base + S3C2410_RTCDATE);
170 writeb(BIN2BCD(tm->tm_mon + 1), base + S3C2410_RTCMON);
Ben Dooks641741e2006-08-27 01:23:27 -0700171 writeb(BIN2BCD(year), base + S3C2410_RTCYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700172
173 return 0;
174}
175
176static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
177{
178 struct rtc_time *alm_tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700179 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700180 unsigned int alm_en;
181
Ben Dooks9a654512006-08-27 01:23:22 -0700182 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
183 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
184 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
185 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
186 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
187 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700188
Ben Dooks9a654512006-08-27 01:23:22 -0700189 alm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700190
David Brownella2db8df2006-12-13 00:35:08 -0800191 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
192
Ben Dooks1add6782006-07-01 04:36:26 -0700193 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
194 alm_en,
195 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
196 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
197
198
199 /* decode the alarm enable field */
200
201 if (alm_en & S3C2410_RTCALM_SECEN)
202 BCD_TO_BIN(alm_tm->tm_sec);
203 else
204 alm_tm->tm_sec = 0xff;
205
206 if (alm_en & S3C2410_RTCALM_MINEN)
207 BCD_TO_BIN(alm_tm->tm_min);
208 else
209 alm_tm->tm_min = 0xff;
210
211 if (alm_en & S3C2410_RTCALM_HOUREN)
212 BCD_TO_BIN(alm_tm->tm_hour);
213 else
214 alm_tm->tm_hour = 0xff;
215
216 if (alm_en & S3C2410_RTCALM_DAYEN)
217 BCD_TO_BIN(alm_tm->tm_mday);
218 else
219 alm_tm->tm_mday = 0xff;
220
221 if (alm_en & S3C2410_RTCALM_MONEN) {
222 BCD_TO_BIN(alm_tm->tm_mon);
223 alm_tm->tm_mon -= 1;
224 } else {
225 alm_tm->tm_mon = 0xff;
226 }
227
228 if (alm_en & S3C2410_RTCALM_YEAREN)
229 BCD_TO_BIN(alm_tm->tm_year);
230 else
231 alm_tm->tm_year = 0xffff;
232
233 return 0;
234}
235
236static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
237{
238 struct rtc_time *tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700239 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700240 unsigned int alrm_en;
241
242 pr_debug("s3c_rtc_setalarm: %d, %02x/%02x/%02x %02x.%02x.%02x\n",
243 alrm->enabled,
244 tm->tm_mday & 0xff, tm->tm_mon & 0xff, tm->tm_year & 0xff,
245 tm->tm_hour & 0xff, tm->tm_min & 0xff, tm->tm_sec);
246
247
Ben Dooks9a654512006-08-27 01:23:22 -0700248 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
249 writeb(0x00, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700250
251 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
252 alrm_en |= S3C2410_RTCALM_SECEN;
Ben Dooks9a654512006-08-27 01:23:22 -0700253 writeb(BIN2BCD(tm->tm_sec), base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700254 }
255
256 if (tm->tm_min < 60 && tm->tm_min >= 0) {
257 alrm_en |= S3C2410_RTCALM_MINEN;
Ben Dooks9a654512006-08-27 01:23:22 -0700258 writeb(BIN2BCD(tm->tm_min), base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700259 }
260
261 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
262 alrm_en |= S3C2410_RTCALM_HOUREN;
Ben Dooks9a654512006-08-27 01:23:22 -0700263 writeb(BIN2BCD(tm->tm_hour), base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700264 }
265
266 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
267
Ben Dooks9a654512006-08-27 01:23:22 -0700268 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700269
270 if (0) {
Ben Dooks9a654512006-08-27 01:23:22 -0700271 alrm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700272 alrm_en &= ~S3C2410_RTCALM_ALMEN;
Ben Dooks9a654512006-08-27 01:23:22 -0700273 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700274 disable_irq_wake(s3c_rtc_alarmno);
275 }
276
277 if (alrm->enabled)
278 enable_irq_wake(s3c_rtc_alarmno);
279 else
280 disable_irq_wake(s3c_rtc_alarmno);
281
282 return 0;
283}
284
285static int s3c_rtc_ioctl(struct device *dev,
286 unsigned int cmd, unsigned long arg)
287{
288 unsigned int ret = -ENOIOCTLCMD;
289
290 switch (cmd) {
291 case RTC_AIE_OFF:
292 case RTC_AIE_ON:
293 s3c_rtc_setaie((cmd == RTC_AIE_ON) ? 1 : 0);
294 ret = 0;
295 break;
296
297 case RTC_PIE_OFF:
298 case RTC_PIE_ON:
299 tick_count = 0;
300 s3c_rtc_setpie((cmd == RTC_PIE_ON) ? 1 : 0);
301 ret = 0;
302 break;
303
304 case RTC_IRQP_READ:
305 ret = put_user(s3c_rtc_freq, (unsigned long __user *)arg);
306 break;
307
308 case RTC_IRQP_SET:
Robert P. J. Day9974b6e2008-02-06 01:38:42 -0800309 if (!is_power_of_2(arg)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700310 ret = -EINVAL;
311 goto exit;
312 }
313
314 pr_debug("s3c2410_rtc: setting frequency %ld\n", arg);
315
316 s3c_rtc_setfreq(arg);
317 ret = 0;
318 break;
319
320 case RTC_UIE_ON:
321 case RTC_UIE_OFF:
322 ret = -EINVAL;
323 }
324
325 exit:
326 return ret;
327}
328
329static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
330{
Ben Dooks9a654512006-08-27 01:23:22 -0700331 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700332
Ben Dooks1add6782006-07-01 04:36:26 -0700333 seq_printf(seq, "periodic_IRQ\t: %s\n",
334 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
335
336 seq_printf(seq, "periodic_freq\t: %d\n", s3c_rtc_freq);
337
338 return 0;
339}
340
341static int s3c_rtc_open(struct device *dev)
342{
343 struct platform_device *pdev = to_platform_device(dev);
344 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
345 int ret;
346
347 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800348 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700349
350 if (ret) {
351 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
352 return ret;
353 }
354
355 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800356 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700357
358 if (ret) {
359 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
360 goto tick_err;
361 }
362
363 return ret;
364
365 tick_err:
366 free_irq(s3c_rtc_alarmno, rtc_dev);
367 return ret;
368}
369
370static void s3c_rtc_release(struct device *dev)
371{
372 struct platform_device *pdev = to_platform_device(dev);
373 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
374
375 /* do not clear AIE here, it may be needed for wake */
376
377 s3c_rtc_setpie(0);
378 free_irq(s3c_rtc_alarmno, rtc_dev);
379 free_irq(s3c_rtc_tickno, rtc_dev);
380}
381
David Brownellff8371a2006-09-30 23:28:17 -0700382static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700383 .open = s3c_rtc_open,
384 .release = s3c_rtc_release,
385 .ioctl = s3c_rtc_ioctl,
386 .read_time = s3c_rtc_gettime,
387 .set_time = s3c_rtc_settime,
388 .read_alarm = s3c_rtc_getalarm,
389 .set_alarm = s3c_rtc_setalarm,
390 .proc = s3c_rtc_proc,
391};
392
393static void s3c_rtc_enable(struct platform_device *pdev, int en)
394{
Ben Dooks9a654512006-08-27 01:23:22 -0700395 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700396 unsigned int tmp;
397
398 if (s3c_rtc_base == NULL)
399 return;
400
401 if (!en) {
Ben Dooks9a654512006-08-27 01:23:22 -0700402 tmp = readb(base + S3C2410_RTCCON);
403 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700404
Ben Dooks9a654512006-08-27 01:23:22 -0700405 tmp = readb(base + S3C2410_TICNT);
406 writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700407 } else {
408 /* re-enable the device, and check it is ok */
409
Ben Dooks9a654512006-08-27 01:23:22 -0700410 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
Ben Dooks1add6782006-07-01 04:36:26 -0700411 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
412
Ben Dooks9a654512006-08-27 01:23:22 -0700413 tmp = readb(base + S3C2410_RTCCON);
414 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700415 }
416
Ben Dooks9a654512006-08-27 01:23:22 -0700417 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
Ben Dooks1add6782006-07-01 04:36:26 -0700418 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
419
Ben Dooks9a654512006-08-27 01:23:22 -0700420 tmp = readb(base + S3C2410_RTCCON);
421 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700422 }
423
Ben Dooks9a654512006-08-27 01:23:22 -0700424 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
Ben Dooks1add6782006-07-01 04:36:26 -0700425 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
426
Ben Dooks9a654512006-08-27 01:23:22 -0700427 tmp = readb(base + S3C2410_RTCCON);
428 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700429 }
430 }
431}
432
433static int s3c_rtc_remove(struct platform_device *dev)
434{
435 struct rtc_device *rtc = platform_get_drvdata(dev);
436
437 platform_set_drvdata(dev, NULL);
438 rtc_device_unregister(rtc);
439
440 s3c_rtc_setpie(0);
441 s3c_rtc_setaie(0);
442
443 iounmap(s3c_rtc_base);
444 release_resource(s3c_rtc_mem);
445 kfree(s3c_rtc_mem);
446
447 return 0;
448}
449
450static int s3c_rtc_probe(struct platform_device *pdev)
451{
452 struct rtc_device *rtc;
453 struct resource *res;
454 int ret;
455
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700456 pr_debug("%s: probe=%p\n", __func__, pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700457
458 /* find the IRQs */
459
460 s3c_rtc_tickno = platform_get_irq(pdev, 1);
461 if (s3c_rtc_tickno < 0) {
462 dev_err(&pdev->dev, "no irq for rtc tick\n");
463 return -ENOENT;
464 }
465
466 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
467 if (s3c_rtc_alarmno < 0) {
468 dev_err(&pdev->dev, "no irq for alarm\n");
469 return -ENOENT;
470 }
471
472 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
473 s3c_rtc_tickno, s3c_rtc_alarmno);
474
475 /* get the memory region */
476
477 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
478 if (res == NULL) {
479 dev_err(&pdev->dev, "failed to get memory region resource\n");
480 return -ENOENT;
481 }
482
483 s3c_rtc_mem = request_mem_region(res->start,
Ben Dooks9a654512006-08-27 01:23:22 -0700484 res->end-res->start+1,
485 pdev->name);
Ben Dooks1add6782006-07-01 04:36:26 -0700486
487 if (s3c_rtc_mem == NULL) {
488 dev_err(&pdev->dev, "failed to reserve memory region\n");
489 ret = -ENOENT;
490 goto err_nores;
491 }
492
493 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
494 if (s3c_rtc_base == NULL) {
495 dev_err(&pdev->dev, "failed ioremap()\n");
496 ret = -EINVAL;
497 goto err_nomap;
498 }
499
500 /* check to see if everything is setup correctly */
501
502 s3c_rtc_enable(pdev, 1);
503
Ben Dooks9a654512006-08-27 01:23:22 -0700504 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
505 readb(s3c_rtc_base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700506
507 s3c_rtc_setfreq(s3c_rtc_freq);
508
509 /* register RTC and exit */
510
511 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
512 THIS_MODULE);
513
514 if (IS_ERR(rtc)) {
515 dev_err(&pdev->dev, "cannot attach rtc\n");
516 ret = PTR_ERR(rtc);
517 goto err_nortc;
518 }
519
520 rtc->max_user_freq = 128;
521
522 platform_set_drvdata(pdev, rtc);
523 return 0;
524
525 err_nortc:
526 s3c_rtc_enable(pdev, 0);
527 iounmap(s3c_rtc_base);
528
529 err_nomap:
530 release_resource(s3c_rtc_mem);
531
532 err_nores:
533 return ret;
534}
535
536#ifdef CONFIG_PM
537
538/* RTC Power management control */
539
Ben Dooks1add6782006-07-01 04:36:26 -0700540static int ticnt_save;
541
542static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
543{
Ben Dooks1add6782006-07-01 04:36:26 -0700544 /* save TICNT for anyone using periodic interrupts */
Ben Dooks9a654512006-08-27 01:23:22 -0700545 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700546 s3c_rtc_enable(pdev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700547 return 0;
548}
549
550static int s3c_rtc_resume(struct platform_device *pdev)
551{
Ben Dooks1add6782006-07-01 04:36:26 -0700552 s3c_rtc_enable(pdev, 1);
Ben Dooks9a654512006-08-27 01:23:22 -0700553 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700554 return 0;
555}
556#else
557#define s3c_rtc_suspend NULL
558#define s3c_rtc_resume NULL
559#endif
560
561static struct platform_driver s3c2410_rtcdrv = {
562 .probe = s3c_rtc_probe,
563 .remove = s3c_rtc_remove,
564 .suspend = s3c_rtc_suspend,
565 .resume = s3c_rtc_resume,
566 .driver = {
567 .name = "s3c2410-rtc",
568 .owner = THIS_MODULE,
569 },
570};
571
572static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
573
574static int __init s3c_rtc_init(void)
575{
576 printk(banner);
577 return platform_driver_register(&s3c2410_rtcdrv);
578}
579
580static void __exit s3c_rtc_exit(void)
581{
582 platform_driver_unregister(&s3c2410_rtcdrv);
583}
584
585module_init(s3c_rtc_init);
586module_exit(s3c_rtc_exit);
587
588MODULE_DESCRIPTION("Samsung S3C RTC Driver");
589MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
590MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700591MODULE_ALIAS("platform:s3c2410-rtc");