blob: b80fa288240831ff96ca03999f58ddf516430656 [file] [log] [blame]
Ben Dooks1add6782006-07-01 04:36:26 -07001/* drivers/rtc/rtc-s3c.c
2 *
Atul Dahiyae48add82010-07-20 12:19:14 +05303 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
5 *
Ben Dooks1add6782006-07-01 04:36:26 -07006 * Copyright (c) 2004,2006 Simtec Electronics
7 * Ben Dooks, <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
15*/
16
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/interrupt.h>
23#include <linux/rtc.h>
24#include <linux/bcd.h>
25#include <linux/clk.h>
Robert P. J. Day9974b6e2008-02-06 01:38:42 -080026#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Ben Dooks1add6782006-07-01 04:36:26 -070028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/hardware.h>
Ben Dooks1add6782006-07-01 04:36:26 -070030#include <asm/uaccess.h>
31#include <asm/io.h>
32#include <asm/irq.h>
Ben Dookse2cd00c2008-10-30 10:14:34 +000033#include <plat/regs-rtc.h>
Ben Dooks1add6782006-07-01 04:36:26 -070034
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070035enum s3c_cpu_type {
36 TYPE_S3C2410,
37 TYPE_S3C64XX,
38};
39
Ben Dooks1add6782006-07-01 04:36:26 -070040/* I have yet to find an S3C implementation with more than one
41 * of these rtc blocks in */
42
43static struct resource *s3c_rtc_mem;
44
Atul Dahiyae48add82010-07-20 12:19:14 +053045static struct clk *rtc_clk;
Ben Dooks1add6782006-07-01 04:36:26 -070046static void __iomem *s3c_rtc_base;
47static int s3c_rtc_alarmno = NO_IRQ;
48static int s3c_rtc_tickno = NO_IRQ;
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -070049static enum s3c_cpu_type s3c_rtc_cpu_type;
Ben Dooks1add6782006-07-01 04:36:26 -070050
51static DEFINE_SPINLOCK(s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -070052
53/* IRQ Handlers */
54
David Howells7d12e782006-10-05 14:55:46 +010055static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070056{
57 struct rtc_device *rdev = id;
58
David Brownellab6a2d72007-05-08 00:33:30 -070059 rtc_update_irq(rdev, 1, RTC_AF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +053060
61 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
62 writeb(S3C2410_INTP_ALM, s3c_rtc_base + S3C2410_INTP);
63
Ben Dooks1add6782006-07-01 04:36:26 -070064 return IRQ_HANDLED;
65}
66
David Howells7d12e782006-10-05 14:55:46 +010067static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070068{
69 struct rtc_device *rdev = id;
70
Ben Dooks773be7e2008-07-23 21:30:45 -070071 rtc_update_irq(rdev, 1, RTC_PF | RTC_IRQF);
Atul Dahiya2f3478f2010-07-20 16:02:51 +053072
73 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
74 writeb(S3C2410_INTP_TIC, s3c_rtc_base + S3C2410_INTP);
75
Ben Dooks1add6782006-07-01 04:36:26 -070076 return IRQ_HANDLED;
77}
78
79/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -080080static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070081{
82 unsigned int tmp;
83
Axel Lin2ec38a02011-03-04 17:36:19 -080084 pr_debug("%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -070085
Ben Dooks9a654512006-08-27 01:23:22 -070086 tmp = readb(s3c_rtc_base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -070087
Axel Lin2ec38a02011-03-04 17:36:19 -080088 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070089 tmp |= S3C2410_RTCALM_ALMEN;
90
Ben Dooks9a654512006-08-27 01:23:22 -070091 writeb(tmp, s3c_rtc_base + S3C2410_RTCALM);
Axel Lin2ec38a02011-03-04 17:36:19 -080092
93 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -070094}
95
Ben Dooks773be7e2008-07-23 21:30:45 -070096static int s3c_rtc_setpie(struct device *dev, int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070097{
98 unsigned int tmp;
99
Ben Dooks773be7e2008-07-23 21:30:45 -0700100 pr_debug("%s: pie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700101
102 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks1add6782006-07-01 04:36:26 -0700103
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700104 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700105 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700106 tmp &= ~S3C64XX_RTCCON_TICEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700107
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700108 if (enabled)
109 tmp |= S3C64XX_RTCCON_TICEN;
110
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530111 writew(tmp, s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700112 } else {
113 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
114 tmp &= ~S3C2410_TICNT_ENABLE;
115
116 if (enabled)
117 tmp |= S3C2410_TICNT_ENABLE;
118
119 writeb(tmp, s3c_rtc_base + S3C2410_TICNT);
120 }
121
Ben Dooks1add6782006-07-01 04:36:26 -0700122 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700123
124 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700125}
126
Ben Dooks773be7e2008-07-23 21:30:45 -0700127static int s3c_rtc_setfreq(struct device *dev, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700128{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700129 struct platform_device *pdev = to_platform_device(dev);
130 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
131 unsigned int tmp = 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700132
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800133 if (!is_power_of_2(freq))
134 return -EINVAL;
135
Ben Dooks1add6782006-07-01 04:36:26 -0700136 spin_lock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700137
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700138 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
139 tmp = readb(s3c_rtc_base + S3C2410_TICNT);
140 tmp &= S3C2410_TICNT_ENABLE;
141 }
142
143 tmp |= (rtc_dev->max_user_freq / freq)-1;
Ben Dooks1add6782006-07-01 04:36:26 -0700144
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530145 writel(tmp, s3c_rtc_base + S3C2410_TICNT);
Ben Dooks1add6782006-07-01 04:36:26 -0700146 spin_unlock_irq(&s3c_rtc_pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700147
148 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700149}
150
151/* Time read/write */
152
153static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
154{
155 unsigned int have_retried = 0;
Ben Dooks9a654512006-08-27 01:23:22 -0700156 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700157
158 retry_get_time:
Ben Dooks9a654512006-08-27 01:23:22 -0700159 rtc_tm->tm_min = readb(base + S3C2410_RTCMIN);
160 rtc_tm->tm_hour = readb(base + S3C2410_RTCHOUR);
161 rtc_tm->tm_mday = readb(base + S3C2410_RTCDATE);
162 rtc_tm->tm_mon = readb(base + S3C2410_RTCMON);
163 rtc_tm->tm_year = readb(base + S3C2410_RTCYEAR);
164 rtc_tm->tm_sec = readb(base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700165
166 /* the only way to work out wether the system was mid-update
167 * when we read it is to check the second counter, and if it
168 * is zero, then we re-try the entire read
169 */
170
171 if (rtc_tm->tm_sec == 0 && !have_retried) {
172 have_retried = 1;
173 goto retry_get_time;
174 }
175
Kukjin Kim30ffc402010-10-27 15:33:09 -0700176 pr_debug("read time %04d.%02d.%02d %02d:%02d:%02d\n",
177 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700178 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
179
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700180 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
181 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
182 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
183 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
184 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
185 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700186
187 rtc_tm->tm_year += 100;
188 rtc_tm->tm_mon -= 1;
189
Kukjin Kim5b3ffdd2010-10-27 15:33:11 -0700190 return rtc_valid_tm(rtc_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700191}
192
193static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
194{
Ben Dooks9a654512006-08-27 01:23:22 -0700195 void __iomem *base = s3c_rtc_base;
Ben Dooks641741e2006-08-27 01:23:27 -0700196 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700197
Kukjin Kim30ffc402010-10-27 15:33:09 -0700198 pr_debug("set time %04d.%02d.%02d %02d:%02d:%02d\n",
199 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
Ben Dooks9a654512006-08-27 01:23:22 -0700200 tm->tm_hour, tm->tm_min, tm->tm_sec);
201
Ben Dooks641741e2006-08-27 01:23:27 -0700202 /* we get around y2k by simply not supporting it */
203
204 if (year < 0 || year >= 100) {
205 dev_err(dev, "rtc only supports 100 years\n");
206 return -EINVAL;
207 }
208
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700209 writeb(bin2bcd(tm->tm_sec), base + S3C2410_RTCSEC);
210 writeb(bin2bcd(tm->tm_min), base + S3C2410_RTCMIN);
211 writeb(bin2bcd(tm->tm_hour), base + S3C2410_RTCHOUR);
212 writeb(bin2bcd(tm->tm_mday), base + S3C2410_RTCDATE);
213 writeb(bin2bcd(tm->tm_mon + 1), base + S3C2410_RTCMON);
214 writeb(bin2bcd(year), base + S3C2410_RTCYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700215
216 return 0;
217}
218
219static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
220{
221 struct rtc_time *alm_tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700222 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700223 unsigned int alm_en;
224
Ben Dooks9a654512006-08-27 01:23:22 -0700225 alm_tm->tm_sec = readb(base + S3C2410_ALMSEC);
226 alm_tm->tm_min = readb(base + S3C2410_ALMMIN);
227 alm_tm->tm_hour = readb(base + S3C2410_ALMHOUR);
228 alm_tm->tm_mon = readb(base + S3C2410_ALMMON);
229 alm_tm->tm_mday = readb(base + S3C2410_ALMDATE);
230 alm_tm->tm_year = readb(base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700231
Ben Dooks9a654512006-08-27 01:23:22 -0700232 alm_en = readb(base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700233
David Brownella2db8df2006-12-13 00:35:08 -0800234 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
235
Kukjin Kim30ffc402010-10-27 15:33:09 -0700236 pr_debug("read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700237 alm_en,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700238 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700239 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
240
241
242 /* decode the alarm enable field */
243
244 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700245 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700246 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700247 alm_tm->tm_sec = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700248
249 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700250 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700251 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700252 alm_tm->tm_min = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700253
254 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700255 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700256 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700257 alm_tm->tm_hour = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700258
259 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700260 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700261 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700262 alm_tm->tm_mday = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700263
264 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700265 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700266 alm_tm->tm_mon -= 1;
267 } else {
Changhwan Youndd061d12010-10-27 15:33:08 -0700268 alm_tm->tm_mon = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700269 }
270
271 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700272 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700273 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700274 alm_tm->tm_year = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700275
276 return 0;
277}
278
279static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
280{
281 struct rtc_time *tm = &alrm->time;
Ben Dooks9a654512006-08-27 01:23:22 -0700282 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700283 unsigned int alrm_en;
284
Kukjin Kim30ffc402010-10-27 15:33:09 -0700285 pr_debug("s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700286 alrm->enabled,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700287 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
288 tm->tm_hour, tm->tm_min, tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700289
290
Ben Dooks9a654512006-08-27 01:23:22 -0700291 alrm_en = readb(base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
292 writeb(0x00, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700293
294 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
295 alrm_en |= S3C2410_RTCALM_SECEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700296 writeb(bin2bcd(tm->tm_sec), base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700297 }
298
299 if (tm->tm_min < 60 && tm->tm_min >= 0) {
300 alrm_en |= S3C2410_RTCALM_MINEN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700301 writeb(bin2bcd(tm->tm_min), base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700302 }
303
304 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
305 alrm_en |= S3C2410_RTCALM_HOUREN;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700306 writeb(bin2bcd(tm->tm_hour), base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700307 }
308
309 pr_debug("setting S3C2410_RTCALM to %08x\n", alrm_en);
310
Ben Dooks9a654512006-08-27 01:23:22 -0700311 writeb(alrm_en, base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700312
Axel Lin2ec38a02011-03-04 17:36:19 -0800313 s3c_rtc_setaie(dev, alrm->enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700314
Ben Dooks1add6782006-07-01 04:36:26 -0700315 return 0;
316}
317
Ben Dooks1add6782006-07-01 04:36:26 -0700318static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
319{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700320 unsigned int ticnt;
Ben Dooks1add6782006-07-01 04:36:26 -0700321
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700322 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700323 ticnt = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700324 ticnt &= S3C64XX_RTCCON_TICEN;
325 } else {
326 ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
327 ticnt &= S3C2410_TICNT_ENABLE;
328 }
329
330 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
Ben Dooks1add6782006-07-01 04:36:26 -0700331 return 0;
332}
333
334static int s3c_rtc_open(struct device *dev)
335{
336 struct platform_device *pdev = to_platform_device(dev);
337 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
338 int ret;
339
340 ret = request_irq(s3c_rtc_alarmno, s3c_rtc_alarmirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800341 IRQF_DISABLED, "s3c2410-rtc alarm", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700342
343 if (ret) {
344 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_alarmno, ret);
345 return ret;
346 }
347
348 ret = request_irq(s3c_rtc_tickno, s3c_rtc_tickirq,
Thomas Gleixner38515e92007-02-14 00:33:16 -0800349 IRQF_DISABLED, "s3c2410-rtc tick", rtc_dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700350
351 if (ret) {
352 dev_err(dev, "IRQ%d error %d\n", s3c_rtc_tickno, ret);
353 goto tick_err;
354 }
355
356 return ret;
357
358 tick_err:
359 free_irq(s3c_rtc_alarmno, rtc_dev);
360 return ret;
361}
362
363static void s3c_rtc_release(struct device *dev)
364{
365 struct platform_device *pdev = to_platform_device(dev);
366 struct rtc_device *rtc_dev = platform_get_drvdata(pdev);
367
368 /* do not clear AIE here, it may be needed for wake */
369
Ben Dooks773be7e2008-07-23 21:30:45 -0700370 s3c_rtc_setpie(dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700371 free_irq(s3c_rtc_alarmno, rtc_dev);
372 free_irq(s3c_rtc_tickno, rtc_dev);
373}
374
David Brownellff8371a2006-09-30 23:28:17 -0700375static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700376 .open = s3c_rtc_open,
377 .release = s3c_rtc_release,
Ben Dooks1add6782006-07-01 04:36:26 -0700378 .read_time = s3c_rtc_gettime,
379 .set_time = s3c_rtc_settime,
380 .read_alarm = s3c_rtc_getalarm,
381 .set_alarm = s3c_rtc_setalarm,
Ben Dooks773be7e2008-07-23 21:30:45 -0700382 .irq_set_freq = s3c_rtc_setfreq,
383 .irq_set_state = s3c_rtc_setpie,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700384 .proc = s3c_rtc_proc,
385 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700386};
387
388static void s3c_rtc_enable(struct platform_device *pdev, int en)
389{
Ben Dooks9a654512006-08-27 01:23:22 -0700390 void __iomem *base = s3c_rtc_base;
Ben Dooks1add6782006-07-01 04:36:26 -0700391 unsigned int tmp;
392
393 if (s3c_rtc_base == NULL)
394 return;
395
396 if (!en) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700397 tmp = readw(base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700398 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
399 tmp &= ~S3C64XX_RTCCON_TICEN;
400 tmp &= ~S3C2410_RTCCON_RTCEN;
Changhwan Younf61ae672010-10-27 15:33:06 -0700401 writew(tmp, base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700402
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700403 if (s3c_rtc_cpu_type == TYPE_S3C2410) {
404 tmp = readb(base + S3C2410_TICNT);
405 tmp &= ~S3C2410_TICNT_ENABLE;
406 writeb(tmp, base + S3C2410_TICNT);
407 }
Ben Dooks1add6782006-07-01 04:36:26 -0700408 } else {
409 /* re-enable the device, and check it is ok */
410
Changhwan Younf61ae672010-10-27 15:33:06 -0700411 if ((readw(base+S3C2410_RTCCON) & S3C2410_RTCCON_RTCEN) == 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700412 dev_info(&pdev->dev, "rtc disabled, re-enabling\n");
413
Changhwan Younf61ae672010-10-27 15:33:06 -0700414 tmp = readw(base + S3C2410_RTCCON);
415 writew(tmp | S3C2410_RTCCON_RTCEN,
416 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700417 }
418
Changhwan Younf61ae672010-10-27 15:33:06 -0700419 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CNTSEL)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700420 dev_info(&pdev->dev, "removing RTCCON_CNTSEL\n");
421
Changhwan Younf61ae672010-10-27 15:33:06 -0700422 tmp = readw(base + S3C2410_RTCCON);
423 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
424 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700425 }
426
Changhwan Younf61ae672010-10-27 15:33:06 -0700427 if ((readw(base + S3C2410_RTCCON) & S3C2410_RTCCON_CLKRST)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700428 dev_info(&pdev->dev, "removing RTCCON_CLKRST\n");
429
Changhwan Younf61ae672010-10-27 15:33:06 -0700430 tmp = readw(base + S3C2410_RTCCON);
431 writew(tmp & ~S3C2410_RTCCON_CLKRST,
432 base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700433 }
434 }
435}
436
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700437static int __devexit s3c_rtc_remove(struct platform_device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700438{
439 struct rtc_device *rtc = platform_get_drvdata(dev);
440
441 platform_set_drvdata(dev, NULL);
442 rtc_device_unregister(rtc);
443
Ben Dooks773be7e2008-07-23 21:30:45 -0700444 s3c_rtc_setpie(&dev->dev, 0);
Axel Lin2ec38a02011-03-04 17:36:19 -0800445 s3c_rtc_setaie(&dev->dev, 0);
Ben Dooks1add6782006-07-01 04:36:26 -0700446
Atul Dahiyae48add82010-07-20 12:19:14 +0530447 clk_disable(rtc_clk);
448 clk_put(rtc_clk);
449 rtc_clk = NULL;
450
Ben Dooks1add6782006-07-01 04:36:26 -0700451 iounmap(s3c_rtc_base);
452 release_resource(s3c_rtc_mem);
453 kfree(s3c_rtc_mem);
454
455 return 0;
456}
457
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700458static int __devinit s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700459{
460 struct rtc_device *rtc;
Changhwan Youne1df9622010-10-27 15:33:10 -0700461 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700462 struct resource *res;
463 int ret;
464
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700465 pr_debug("%s: probe=%p\n", __func__, pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700466
467 /* find the IRQs */
468
469 s3c_rtc_tickno = platform_get_irq(pdev, 1);
470 if (s3c_rtc_tickno < 0) {
471 dev_err(&pdev->dev, "no irq for rtc tick\n");
472 return -ENOENT;
473 }
474
475 s3c_rtc_alarmno = platform_get_irq(pdev, 0);
476 if (s3c_rtc_alarmno < 0) {
477 dev_err(&pdev->dev, "no irq for alarm\n");
478 return -ENOENT;
479 }
480
481 pr_debug("s3c2410_rtc: tick irq %d, alarm irq %d\n",
482 s3c_rtc_tickno, s3c_rtc_alarmno);
483
484 /* get the memory region */
485
486 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
487 if (res == NULL) {
488 dev_err(&pdev->dev, "failed to get memory region resource\n");
489 return -ENOENT;
490 }
491
492 s3c_rtc_mem = request_mem_region(res->start,
Ben Dooks9a654512006-08-27 01:23:22 -0700493 res->end-res->start+1,
494 pdev->name);
Ben Dooks1add6782006-07-01 04:36:26 -0700495
496 if (s3c_rtc_mem == NULL) {
497 dev_err(&pdev->dev, "failed to reserve memory region\n");
498 ret = -ENOENT;
499 goto err_nores;
500 }
501
502 s3c_rtc_base = ioremap(res->start, res->end - res->start + 1);
503 if (s3c_rtc_base == NULL) {
504 dev_err(&pdev->dev, "failed ioremap()\n");
505 ret = -EINVAL;
506 goto err_nomap;
507 }
508
Atul Dahiyae48add82010-07-20 12:19:14 +0530509 rtc_clk = clk_get(&pdev->dev, "rtc");
510 if (IS_ERR(rtc_clk)) {
511 dev_err(&pdev->dev, "failed to find rtc clock source\n");
512 ret = PTR_ERR(rtc_clk);
513 rtc_clk = NULL;
514 goto err_clk;
515 }
516
517 clk_enable(rtc_clk);
518
Ben Dooks1add6782006-07-01 04:36:26 -0700519 /* check to see if everything is setup correctly */
520
521 s3c_rtc_enable(pdev, 1);
522
Changhwan Younf61ae672010-10-27 15:33:06 -0700523 pr_debug("s3c2410_rtc: RTCCON=%02x\n",
524 readw(s3c_rtc_base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700525
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700526 device_init_wakeup(&pdev->dev, 1);
527
Ben Dooks1add6782006-07-01 04:36:26 -0700528 /* register RTC and exit */
529
530 rtc = rtc_device_register("s3c", &pdev->dev, &s3c_rtcops,
531 THIS_MODULE);
532
533 if (IS_ERR(rtc)) {
534 dev_err(&pdev->dev, "cannot attach rtc\n");
535 ret = PTR_ERR(rtc);
536 goto err_nortc;
537 }
538
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700539 s3c_rtc_cpu_type = platform_get_device_id(pdev)->driver_data;
540
Taekgyun Ko051fe542010-07-29 13:00:42 +0900541 /* Check RTC Time */
542
Changhwan Youne1df9622010-10-27 15:33:10 -0700543 s3c_rtc_gettime(NULL, &rtc_tm);
Taekgyun Ko051fe542010-07-29 13:00:42 +0900544
Changhwan Youne1df9622010-10-27 15:33:10 -0700545 if (rtc_valid_tm(&rtc_tm)) {
546 rtc_tm.tm_year = 100;
547 rtc_tm.tm_mon = 0;
548 rtc_tm.tm_mday = 1;
549 rtc_tm.tm_hour = 0;
550 rtc_tm.tm_min = 0;
551 rtc_tm.tm_sec = 0;
552
553 s3c_rtc_settime(NULL, &rtc_tm);
554
555 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
Taekgyun Ko051fe542010-07-29 13:00:42 +0900556 }
557
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700558 if (s3c_rtc_cpu_type == TYPE_S3C64XX)
559 rtc->max_user_freq = 32768;
560 else
561 rtc->max_user_freq = 128;
562
Ben Dooks1add6782006-07-01 04:36:26 -0700563 platform_set_drvdata(pdev, rtc);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700564
565 s3c_rtc_setfreq(&pdev->dev, 1);
566
Ben Dooks1add6782006-07-01 04:36:26 -0700567 return 0;
568
569 err_nortc:
570 s3c_rtc_enable(pdev, 0);
Atul Dahiyae48add82010-07-20 12:19:14 +0530571 clk_disable(rtc_clk);
572 clk_put(rtc_clk);
573
574 err_clk:
Ben Dooks1add6782006-07-01 04:36:26 -0700575 iounmap(s3c_rtc_base);
576
577 err_nomap:
578 release_resource(s3c_rtc_mem);
579
580 err_nores:
581 return ret;
582}
583
584#ifdef CONFIG_PM
585
586/* RTC Power management control */
587
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700588static int ticnt_save, ticnt_en_save;
Ben Dooks1add6782006-07-01 04:36:26 -0700589
590static int s3c_rtc_suspend(struct platform_device *pdev, pm_message_t state)
591{
Ben Dooks1add6782006-07-01 04:36:26 -0700592 /* save TICNT for anyone using periodic interrupts */
Ben Dooks9a654512006-08-27 01:23:22 -0700593 ticnt_save = readb(s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700594 if (s3c_rtc_cpu_type == TYPE_S3C64XX) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700595 ticnt_en_save = readw(s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700596 ticnt_en_save &= S3C64XX_RTCCON_TICEN;
597 }
Ben Dooks1add6782006-07-01 04:36:26 -0700598 s3c_rtc_enable(pdev, 0);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700599
600 if (device_may_wakeup(&pdev->dev))
601 enable_irq_wake(s3c_rtc_alarmno);
602
Ben Dooks1add6782006-07-01 04:36:26 -0700603 return 0;
604}
605
606static int s3c_rtc_resume(struct platform_device *pdev)
607{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700608 unsigned int tmp;
609
Ben Dooks1add6782006-07-01 04:36:26 -0700610 s3c_rtc_enable(pdev, 1);
Ben Dooks9a654512006-08-27 01:23:22 -0700611 writeb(ticnt_save, s3c_rtc_base + S3C2410_TICNT);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700612 if (s3c_rtc_cpu_type == TYPE_S3C64XX && ticnt_en_save) {
Changhwan Younf61ae672010-10-27 15:33:06 -0700613 tmp = readw(s3c_rtc_base + S3C2410_RTCCON);
614 writew(tmp | ticnt_en_save, s3c_rtc_base + S3C2410_RTCCON);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700615 }
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700616
617 if (device_may_wakeup(&pdev->dev))
618 disable_irq_wake(s3c_rtc_alarmno);
619
Ben Dooks1add6782006-07-01 04:36:26 -0700620 return 0;
621}
622#else
623#define s3c_rtc_suspend NULL
624#define s3c_rtc_resume NULL
625#endif
626
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700627static struct platform_device_id s3c_rtc_driver_ids[] = {
628 {
629 .name = "s3c2410-rtc",
630 .driver_data = TYPE_S3C2410,
631 }, {
632 .name = "s3c64xx-rtc",
633 .driver_data = TYPE_S3C64XX,
634 },
635 { }
636};
637
638MODULE_DEVICE_TABLE(platform, s3c_rtc_driver_ids);
639
640static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700641 .probe = s3c_rtc_probe,
Ben Dooks4cd0c5c2008-07-23 21:30:44 -0700642 .remove = __devexit_p(s3c_rtc_remove),
Ben Dooks1add6782006-07-01 04:36:26 -0700643 .suspend = s3c_rtc_suspend,
644 .resume = s3c_rtc_resume,
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700645 .id_table = s3c_rtc_driver_ids,
Ben Dooks1add6782006-07-01 04:36:26 -0700646 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700647 .name = "s3c-rtc",
Ben Dooks1add6782006-07-01 04:36:26 -0700648 .owner = THIS_MODULE,
649 },
650};
651
652static char __initdata banner[] = "S3C24XX RTC, (c) 2004,2006 Simtec Electronics\n";
653
654static int __init s3c_rtc_init(void)
655{
656 printk(banner);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700657 return platform_driver_register(&s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700658}
659
660static void __exit s3c_rtc_exit(void)
661{
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700662 platform_driver_unregister(&s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700663}
664
665module_init(s3c_rtc_init);
666module_exit(s3c_rtc_exit);
667
668MODULE_DESCRIPTION("Samsung S3C RTC Driver");
669MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
670MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700671MODULE_ALIAS("platform:s3c2410-rtc");