blob: e0d7b9991505564c412e4771f8061154c5adc449 [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
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Ben Dooks1add6782006-07-01 04:36:26 -070026#include <asm/uaccess.h>
27#include <asm/io.h>
28#include <asm/irq.h>
Ben Dookse2cd00c2008-10-30 10:14:34 +000029#include <plat/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;
Ben Dooks1add6782006-07-01 04:36:26 -070039
40static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -070041
42/* IRQ Handlers */
43
David Howells7d12e782006-10-05 14:55:46 +010044static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070045{
46 struct rtc_device *rdev = id;
47
David Brownellab6a2d72007-05-08 00:33:30 -070048 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
Ben Dooks1add6782006-07-01 04:36:26 -070049 return IRQ_HANDLED;
50}
51
David Howells7d12e782006-10-05 14:55:46 +010052static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070053{
54 struct rtc_device *rdev = id;
55
Ben Dooks773be7e2008-07-23 21:30:45 -070056 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
Ben Dooks1add6782006-07-01 04:36:26 -070057 return IRQ_HANDLED;
58}
59
60/* Update control registers */
61static void s3c_rtc_setaie(int to)
62{
63 unsigned int tmp;
64
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070065 pr_debug("%s: aie=%d\n", __func__, to);
Ben Dooks1add6782006-07-01 04:36:26 -070066
Ben Dooks9a654512006-08-27 01:23:22 -070067 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -070068
69 if (to)
70 tmp |= S3C2410_RTCALM_ALMEN;
71
Ben Dooks9a654512006-08-27 01:23:22 -070072 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -070073}
74
Ben Dooks773be7e2008-07-23 21:30:45 -070075static int s3c_rtc_setpie(struct device *dev, int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070076{
77 unsigned int tmp;
78
Ben Dooks773be7e2008-07-23 21:30:45 -070079 pr_debug("%s: pie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -070080
81 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks9a654512006-08-27 01:23:22 -070082 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & ~S3C2410_TICNT_ENABLE;
Ben Dooks1add6782006-07-01 04:36:26 -070083
Ben Dooks773be7e2008-07-23 21:30:45 -070084 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070085 tmp |= S3C2410_TICNT_ENABLE;
86
Ben Dooks9a654512006-08-27 01:23:22 -070087 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -070088 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -070089
90 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -070091}
92
Ben Dooks773be7e2008-07-23 21:30:45 -070093static int s3c_rtc_setfreq(struct device *dev, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -070094{
95 unsigned int tmp;
96
Jonathan Cameron5d2a5032009-01-06 14:42:12 -080097 if (!is_power_of_2(freq))
98 return -EINVAL;
99
Ben Dooks1add6782006-07-01 04:36:26 -0700100 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700101
Ben Dooks9a654512006-08-27 01:23:22 -0700102 tmp = readb(s3c_rtc_base + S3C2410_TICNT) & S3C2410_TICNT_ENABLE;
Ben Dooks1add6782006-07-01 04:36:26 -0700103 tmp |= (128 / freq)-1;
104
Ben Dooks9a654512006-08-27 01:23:22 -0700105 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700106 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700107
108 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700109}
110
111/* Time read/write */
112
113static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
114{
115 unsigned int have_retried = 0;
Ben Dooks9a654512006-08-27 01:23:22 -0700116 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700117
118 retry_get_time:
Ben Dooks9a654512006-08-27 01:23:22 -0700119 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);
Ben Dooks1add6782006-07-01 04:36:26 -0700125
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
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700140 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);
Ben Dooks1add6782006-07-01 04:36:26 -0700146
147 rtc_tm->tm_year += 100;
148 rtc_tm->tm_mon -= 1;
149
150 return 0;
151}
152
153static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
154{
Ben Dooks9a654512006-08-27 01:23:22 -0700155 void __iomem *base = s3c_rtc_base;
Ben Dooks641741e2006-08-27 01:23:27 -0700156 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700157
Ben Dooks9a654512006-08-27 01:23:22 -0700158 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
Ben Dooks641741e2006-08-27 01:23:27 -0700162 /* 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
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700169 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);
Ben Dooks1add6782006-07-01 04:36:26 -0700175
176 return 0;
177}
178
179static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
180{
181 struct rtc_time *alm_tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700182 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700183 unsigned int alm_en;
184
Ben Dooks9a654512006-08-27 01:23:22 -0700185 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);
Ben Dooks1add6782006-07-01 04:36:26 -0700191
Ben Dooks9a654512006-08-27 01:23:22 -0700192 alm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700193
David Brownella2db8df2006-12-13 00:35:08 -0800194 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
195
Ben Dooks1add6782006-07-01 04:36:26 -0700196 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)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700205 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700206 else
207 alm_tm->tm_sec = 0xff;
208
209 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700210 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700211 else
212 alm_tm->tm_min = 0xff;
213
214 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700215 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700216 else
217 alm_tm->tm_hour = 0xff;
218
219 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700220 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700221 else
222 alm_tm->tm_mday = 0xff;
223
224 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700225 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700226 alm_tm->tm_mon -= 1;
227 } else {
228 alm_tm->tm_mon = 0xff;
229 }
230
231 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700232 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700233 else
234 alm_tm->tm_year = 0xffff;
235
236 return 0;
237}
238
239static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
240{
241 struct rtc_time *tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700242 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700243 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
Ben Dooks9a654512006-08-27 01:23:22 -0700251 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
252 writeb(0x00, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700253
254 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
255 alrm_en |= S3C2410_RTCALM_SECEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700256 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700257 }
258
259 if (tm->tm_min < 60 && tm->tm_min >= 0) {
260 alrm_en |= S3C2410_RTCALM_MINEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700261 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700262 }
263
264 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
265 alrm_en |= S3C2410_RTCALM_HOUREN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700266 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700267 }
268
269 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
270
Ben Dooks9a654512006-08-27 01:23:22 -0700271 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700272
Ben Dooks773be7e2008-07-23 21:30:45 -0700273 s3c_rtc_setaie(alrm->enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700274
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
Ben Dooks1add6782006-07-01 04:36:26 -0700283static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
284{
Ben Dooks9a654512006-08-27 01:23:22 -0700285 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700286
Ben Dooks1add6782006-07-01 04:36:26 -0700287 seq_printf(seq, "periodic_IRQ\t: %s\n",
288 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
Ben Dooks1add6782006-07-01 04:36:26 -0700289 return 0;
290}
291
292static 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,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800299 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700300
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,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800307 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700308
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
321static 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
Ben Dooks773be7e2008-07-23 21:30:45 -0700328 s3c_rtc_setpie(dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700329 free_irq(s3c_rtc_alarmno, rtc_dev);
330 free_irq(s3c_rtc_tickno, rtc_dev);
331}
332
David Brownellff8371a2006-09-30 23:28:17 -0700333static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700334 .open = s3c_rtc_open,
335 .release = s3c_rtc_release,
Ben Dooks1add6782006-07-01 04:36:26 -0700336 .read_time = s3c_rtc_gettime,
337 .set_time = s3c_rtc_settime,
338 .read_alarm = s3c_rtc_getalarm,
339 .set_alarm = s3c_rtc_setalarm,
Ben Dooks773be7e2008-07-23 21:30:45 -0700340 .irq_set_freq = s3c_rtc_setfreq,
341 .irq_set_state = s3c_rtc_setpie,
Ben Dooks1add6782006-07-01 04:36:26 -0700342 .proc = s3c_rtc_proc,
343};
344
345static void s3c_rtc_enable(struct platform_device *pdev, int en)
346{
Ben Dooks9a654512006-08-27 01:23:22 -0700347 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700348 unsigned int tmp;
349
350 if (s3c_rtc_base == NULL)
351 return;
352
353 if (!en) {
Ben Dooks9a654512006-08-27 01:23:22 -0700354 tmp = readb(base + S3C2410_RTCCON);
355 writeb(tmp & ~S3C2410_RTCCON_RTCEN, base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700356
Ben Dooks9a654512006-08-27 01:23:22 -0700357 tmp = readb(base + S3C2410_TICNT);
358 writeb(tmp & ~S3C2410_TICNT_ENABLE, base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700359 } else {
360 /* re-enable the device, and check it is ok */
361
Ben Dooks9a654512006-08-27 01:23:22 -0700362 if ((readb(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0){
Ben Dooks1add6782006-07-01 04:36:26 -0700363 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
364
Ben Dooks9a654512006-08-27 01:23:22 -0700365 tmp = readb(base + S3C2410_RTCCON);
366 writeb(tmp|S3C2410_RTCCON_RTCEN, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700367 }
368
Ben Dooks9a654512006-08-27 01:23:22 -0700369 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)){
Ben Dooks1add6782006-07-01 04:36:26 -0700370 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
371
Ben Dooks9a654512006-08-27 01:23:22 -0700372 tmp = readb(base + S3C2410_RTCCON);
373 writeb(tmp& ~S3C2410_RTCCON_CNTSEL, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700374 }
375
Ben Dooks9a654512006-08-27 01:23:22 -0700376 if ((readb(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)){
Ben Dooks1add6782006-07-01 04:36:26 -0700377 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
378
Ben Dooks9a654512006-08-27 01:23:22 -0700379 tmp = readb(base + S3C2410_RTCCON);
380 writeb(tmp & ~S3C2410_RTCCON_CLKRST, base+S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700381 }
382 }
383}
384
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700385static int __devexit s3c_rtc_remove(struct platform_device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700386{
387 struct rtc_device *rtc = platform_get_drvdata(dev);
388
389 platform_set_drvdata(dev, NULL);
390 rtc_device_unregister(rtc);
391
Ben Dooks773be7e2008-07-23 21:30:45 -0700392 s3c_rtc_setpie(&dev->dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700393 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
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700402static int __devinit s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700403{
404 struct rtc_device *rtc;
405 struct resource *res;
406 int ret;
407
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700408 pr_debug("%s: probe=%p\n", __func__, pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700409
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,
Ben Dooks9a654512006-08-27 01:23:22 -0700436 res->end-res->start+1,
437 pdev->name);
Ben Dooks1add6782006-07-01 04:36:26 -0700438
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
Ben Dooks9a654512006-08-27 01:23:22 -0700456 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
457 readb(s3c_rtc_base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700458
Ben Dooks773be7e2008-07-23 21:30:45 -0700459 s3c_rtc_setfreq(&pdev->dev, 1);
Ben Dooks1add6782006-07-01 04:36:26 -0700460
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700461 device_init_wakeup(&pdev->dev, 1);
462
Ben Dooks1add6782006-07-01 04:36:26 -0700463 /* 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
Ben Dooks1add6782006-07-01 04:36:26 -0700494static int ticnt_save;
495
496static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
497{
Ben Dooks1add6782006-07-01 04:36:26 -0700498 /* save TICNT for anyone using periodic interrupts */
Ben Dooks9a654512006-08-27 01:23:22 -0700499 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700500 s3c_rtc_enable(pdev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700501 return 0;
502}
503
504static int s3c_rtc_resume(struct platform_device *pdev)
505{
Ben Dooks1add6782006-07-01 04:36:26 -0700506 s3c_rtc_enable(pdev, 1);
Ben Dooks9a654512006-08-27 01:23:22 -0700507 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700508 return 0;
509}
510#else
511#define s3c_rtc_suspend NULL
512#define s3c_rtc_resume NULL
513#endif
514
Yauhen Kharuzhyeb944db2008-10-29 14:00:59 -0700515static struct platform_driver s3c2410_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700516 .probe = s3c_rtc_probe,
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700517 .remove = __devexit_p(s3c_rtc_remove),
Ben Dooks1add6782006-07-01 04:36:26 -0700518 .suspend = s3c_rtc_suspend,
519 .resume = s3c_rtc_resume,
520 .driver = {
521 .name = "s3c2410-rtc",
522 .owner = THIS_MODULE,
523 },
524};
525
526static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
527
528static int __init s3c_rtc_init(void)
529{
530 printk(banner);
Yauhen Kharuzhyeb944db2008-10-29 14:00:59 -0700531 return platform_driver_register(&s3c2410_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700532}
533
534static void __exit s3c_rtc_exit(void)
535{
Yauhen Kharuzhyeb944db2008-10-29 14:00:59 -0700536 platform_driver_unregister(&s3c2410_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700537}
538
539module_init(s3c_rtc_init);
540module_exit(s3c_rtc_exit);
541
542MODULE_DESCRIPTION("Samsung S3C RTC Driver");
543MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
544MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700545MODULE_ALIAS("platform:s3c2410-rtc");