blob: a6b1252c9941884a93dac49ed4c10c828ad0964d [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>
Thomas Abraham39ce4082011-10-24 14:49:04 +020028#include <linux/of.h>
Sachin Kamatdbd9acb2012-07-30 14:41:48 -070029#include <linux/uaccess.h>
30#include <linux/io.h>
Ben Dooks1add6782006-07-01 04:36:26 -070031
Ben Dooks1add6782006-07-01 04:36:26 -070032#include <asm/irq.h>
Arnd Bergmannb9d7c5d2013-03-05 12:04:37 +010033#include "rtc-s3c.h"
Ben Dooks1add6782006-07-01 04:36:26 -070034
Chanwoo Choi19be09f2014-10-13 15:52:28 -070035struct s3c_rtc {
36 struct device *dev;
37 struct rtc_device *rtc;
Ben Dooks1add6782006-07-01 04:36:26 -070038
Chanwoo Choi19be09f2014-10-13 15:52:28 -070039 void __iomem *base;
40 struct clk *rtc_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070041 struct clk *rtc_src_clk;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070042 bool enabled;
Ben Dooks1add6782006-07-01 04:36:26 -070043
Chanwoo Choiae05c952014-10-13 15:52:33 -070044 struct s3c_rtc_data *data;
Ben Dooks1add6782006-07-01 04:36:26 -070045
Chanwoo Choi19be09f2014-10-13 15:52:28 -070046 int irq_alarm;
47 int irq_tick;
48
49 spinlock_t pie_lock;
50 spinlock_t alarm_clk_lock;
51
52 int ticnt_save, ticnt_en_save;
53 bool wake_en;
54};
55
Chanwoo Choiae05c952014-10-13 15:52:33 -070056struct s3c_rtc_data {
57 int max_user_freq;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070058 bool needs_src_clk;
Chanwoo Choiae05c952014-10-13 15:52:33 -070059
60 void (*irq_handler) (struct s3c_rtc *info, int mask);
61 void (*set_freq) (struct s3c_rtc *info, int freq);
62 void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
63 void (*select_tick_clk) (struct s3c_rtc *info);
64 void (*save_tick_cnt) (struct s3c_rtc *info);
65 void (*restore_tick_cnt) (struct s3c_rtc *info);
66 void (*enable) (struct s3c_rtc *info);
67 void (*disable) (struct s3c_rtc *info);
68};
69
Chanwoo Choi19be09f2014-10-13 15:52:28 -070070static void s3c_rtc_alarm_clk_enable(struct s3c_rtc *info, bool enable)
Donggeun Kim88cee8f2011-09-14 16:22:19 -070071{
Donggeun Kim88cee8f2011-09-14 16:22:19 -070072 unsigned long irq_flags;
73
Chanwoo Choi19be09f2014-10-13 15:52:28 -070074 spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070075 if (enable) {
Chanwoo Choi19be09f2014-10-13 15:52:28 -070076 if (!info->enabled) {
77 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070078 if (info->data->needs_src_clk)
79 clk_enable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -070080 info->enabled = true;
Donggeun Kim88cee8f2011-09-14 16:22:19 -070081 }
82 } else {
Chanwoo Choi19be09f2014-10-13 15:52:28 -070083 if (info->enabled) {
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070084 if (info->data->needs_src_clk)
85 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -070086 clk_disable(info->rtc_clk);
87 info->enabled = false;
Donggeun Kim88cee8f2011-09-14 16:22:19 -070088 }
89 }
Chanwoo Choi19be09f2014-10-13 15:52:28 -070090 spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070091}
92
Ben Dooks1add6782006-07-01 04:36:26 -070093/* IRQ Handlers */
David Howells7d12e782006-10-05 14:55:46 +010094static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070095{
Chanwoo Choi19be09f2014-10-13 15:52:28 -070096 struct s3c_rtc *info = (struct s3c_rtc *)id;
Ben Dooks1add6782006-07-01 04:36:26 -070097
Chanwoo Choiae05c952014-10-13 15:52:33 -070098 if (info->data->irq_handler)
99 info->data->irq_handler(info, S3C2410_INTP_TIC);
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530100
Chanwoo Choiae05c952014-10-13 15:52:33 -0700101 return IRQ_HANDLED;
102}
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530103
Chanwoo Choiae05c952014-10-13 15:52:33 -0700104static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
105{
106 struct s3c_rtc *info = (struct s3c_rtc *)id;
107
108 if (info->data->irq_handler)
109 info->data->irq_handler(info, S3C2410_INTP_ALM);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700110
Ben Dooks1add6782006-07-01 04:36:26 -0700111 return IRQ_HANDLED;
112}
113
114/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -0800115static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700116{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700117 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700118 unsigned int tmp;
119
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700120 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700121
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700122 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700123 if (info->data->needs_src_clk)
124 clk_enable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700125 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700126
Axel Lin2ec38a02011-03-04 17:36:19 -0800127 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700128 tmp |= S3C2410_RTCALM_ALMEN;
129
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700130 writeb(tmp, info->base + S3C2410_RTCALM);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700131 if (info->data->needs_src_clk)
132 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700133 clk_disable(info->rtc_clk);
Axel Lin2ec38a02011-03-04 17:36:19 -0800134
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700135 s3c_rtc_alarm_clk_enable(info, enabled);
Donggeun Kim88cee8f2011-09-14 16:22:19 -0700136
Axel Lin2ec38a02011-03-04 17:36:19 -0800137 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700138}
139
Chanwoo Choiae05c952014-10-13 15:52:33 -0700140/* Set RTC frequency */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700141static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700142{
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800143 if (!is_power_of_2(freq))
144 return -EINVAL;
145
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700146 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700147 if (info->data->needs_src_clk)
148 clk_enable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700149 spin_lock_irq(&info->pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700150
Chanwoo Choiae05c952014-10-13 15:52:33 -0700151 if (info->data->set_freq)
152 info->data->set_freq(info, freq);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700153
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700154 spin_unlock_irq(&info->pie_lock);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700155 if (info->data->needs_src_clk)
156 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700157 clk_disable(info->rtc_clk);
Ben Dooks773be7e2008-07-23 21:30:45 -0700158
159 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700160}
161
162/* Time read/write */
Ben Dooks1add6782006-07-01 04:36:26 -0700163static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
164{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700165 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700166 unsigned int have_retried = 0;
167
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700168 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700169 if (info->data->needs_src_clk)
170 clk_enable(info->rtc_src_clk);
171
Ben Dooks1add6782006-07-01 04:36:26 -0700172 retry_get_time:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700173 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
174 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
175 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
176 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
177 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
178 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700179
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400180 /* the only way to work out whether the system was mid-update
Ben Dooks1add6782006-07-01 04:36:26 -0700181 * when we read it is to check the second counter, and if it
182 * is zero, then we re-try the entire read
183 */
184
185 if (rtc_tm->tm_sec == 0 && !have_retried) {
186 have_retried = 1;
187 goto retry_get_time;
188 }
189
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700190 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
191 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
192 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
193 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
194 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
195 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700196
197 rtc_tm->tm_year += 100;
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700198
Jingoo Hand4a48c22013-02-21 16:45:06 -0800199 dev_dbg(dev, "read time %04d.%02d.%02d %02d:%02d:%02d\n",
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700200 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
201 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
202
Ben Dooks1add6782006-07-01 04:36:26 -0700203 rtc_tm->tm_mon -= 1;
204
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700205 if (info->data->needs_src_clk)
206 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700207 clk_disable(info->rtc_clk);
208
Kukjin Kim5b3ffdd2010-10-27 15:33:11 -0700209 return rtc_valid_tm(rtc_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700210}
211
212static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
213{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700214 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks641741e2006-08-27 01:23:27 -0700215 int year = tm->tm_year - 100;
Ben Dooks1add6782006-07-01 04:36:26 -0700216
Jingoo Hand4a48c22013-02-21 16:45:06 -0800217 dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
Kukjin Kim30ffc402010-10-27 15:33:09 -0700218 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
Ben Dooks9a654512006-08-27 01:23:22 -0700219 tm->tm_hour, tm->tm_min, tm->tm_sec);
220
Ben Dooks641741e2006-08-27 01:23:27 -0700221 /* we get around y2k by simply not supporting it */
222
223 if (year < 0 || year >= 100) {
224 dev_err(dev, "rtc only supports 100 years\n");
225 return -EINVAL;
226 }
227
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700228 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700229 if (info->data->needs_src_clk)
230 clk_enable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700231
232 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
233 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
234 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
235 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
236 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
237 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
238
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700239 if (info->data->needs_src_clk)
240 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700241 clk_disable(info->rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700242
243 return 0;
244}
245
246static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
247{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700248 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700249 struct rtc_time *alm_tm = &alrm->time;
250 unsigned int alm_en;
251
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700252 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700253 if (info->data->needs_src_clk)
254 clk_enable(info->rtc_src_clk);
255
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700256 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
257 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
258 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
259 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
260 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
261 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700262
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700263 alm_en = readb(info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700264
David Brownella2db8df2006-12-13 00:35:08 -0800265 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
266
Jingoo Hand4a48c22013-02-21 16:45:06 -0800267 dev_dbg(dev, "read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700268 alm_en,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700269 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
Ben Dooks1add6782006-07-01 04:36:26 -0700270 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
271
272
273 /* decode the alarm enable field */
274
275 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700276 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700277 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700278 alm_tm->tm_sec = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700279
280 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700281 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700282 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700283 alm_tm->tm_min = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700284
285 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700286 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700287 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700288 alm_tm->tm_hour = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700289
290 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700291 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700292 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700293 alm_tm->tm_mday = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700294
295 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700296 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700297 alm_tm->tm_mon -= 1;
298 } else {
Changhwan Youndd061d12010-10-27 15:33:08 -0700299 alm_tm->tm_mon = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700300 }
301
302 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700303 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700304 else
Changhwan Youndd061d12010-10-27 15:33:08 -0700305 alm_tm->tm_year = -1;
Ben Dooks1add6782006-07-01 04:36:26 -0700306
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700307 if (info->data->needs_src_clk)
308 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700309 clk_disable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700310
Ben Dooks1add6782006-07-01 04:36:26 -0700311 return 0;
312}
313
314static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
315{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700316 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700317 struct rtc_time *tm = &alrm->time;
318 unsigned int alrm_en;
319
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700320 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700321 if (info->data->needs_src_clk)
322 clk_enable(info->rtc_src_clk);
323
Jingoo Hand4a48c22013-02-21 16:45:06 -0800324 dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
Ben Dooks1add6782006-07-01 04:36:26 -0700325 alrm->enabled,
MyungJoo Ham4e8896c2011-08-25 15:59:22 -0700326 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
Kukjin Kim30ffc402010-10-27 15:33:09 -0700327 tm->tm_hour, tm->tm_min, tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700328
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700329 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
330 writeb(0x00, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700331
332 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
333 alrm_en |= S3C2410_RTCALM_SECEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700334 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700335 }
336
337 if (tm->tm_min < 60 && tm->tm_min >= 0) {
338 alrm_en |= S3C2410_RTCALM_MINEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700339 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700340 }
341
342 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
343 alrm_en |= S3C2410_RTCALM_HOUREN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700344 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700345 }
346
Jingoo Hand4a48c22013-02-21 16:45:06 -0800347 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
Ben Dooks1add6782006-07-01 04:36:26 -0700348
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700349 writeb(alrm_en, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700350
Axel Lin2ec38a02011-03-04 17:36:19 -0800351 s3c_rtc_setaie(dev, alrm->enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700352
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700353 if (info->data->needs_src_clk)
354 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700355 clk_disable(info->rtc_clk);
356
Ben Dooks1add6782006-07-01 04:36:26 -0700357 return 0;
358}
359
Ben Dooks1add6782006-07-01 04:36:26 -0700360static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
361{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700362 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700363
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700364 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700365 if (info->data->needs_src_clk)
366 clk_enable(info->rtc_src_clk);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700367
Chanwoo Choiae05c952014-10-13 15:52:33 -0700368 if (info->data->enable_tick)
369 info->data->enable_tick(info, seq);
370
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700371 if (info->data->needs_src_clk)
372 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700373 clk_disable(info->rtc_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700374
Ben Dooks1add6782006-07-01 04:36:26 -0700375 return 0;
376}
377
David Brownellff8371a2006-09-30 23:28:17 -0700378static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700379 .read_time = s3c_rtc_gettime,
380 .set_time = s3c_rtc_settime,
381 .read_alarm = s3c_rtc_getalarm,
382 .set_alarm = s3c_rtc_setalarm,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700383 .proc = s3c_rtc_proc,
384 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700385};
386
Chanwoo Choiae05c952014-10-13 15:52:33 -0700387static void s3c24xx_rtc_enable(struct s3c_rtc *info)
Ben Dooks1add6782006-07-01 04:36:26 -0700388{
Chanwoo Choid67288d2014-10-13 15:52:31 -0700389 unsigned int con, tmp;
Ben Dooks1add6782006-07-01 04:36:26 -0700390
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700391 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700392 if (info->data->needs_src_clk)
393 clk_enable(info->rtc_src_clk);
Chanwoo Choid67288d2014-10-13 15:52:31 -0700394
395 con = readw(info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700396 /* re-enable the device, and check it is ok */
397 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
398 dev_info(info->dev, "rtc disabled, re-enabling\n");
Ben Dooks1add6782006-07-01 04:36:26 -0700399
Chanwoo Choiae05c952014-10-13 15:52:33 -0700400 tmp = readw(info->base + S3C2410_RTCCON);
401 writew(tmp | S3C2410_RTCCON_RTCEN,
402 info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700403 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700404
405 if (con & S3C2410_RTCCON_CNTSEL) {
406 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
407
408 tmp = readw(info->base + S3C2410_RTCCON);
409 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
410 info->base + S3C2410_RTCCON);
411 }
412
413 if (con & S3C2410_RTCCON_CLKRST) {
414 dev_info(info->dev, "removing RTCCON_CLKRST\n");
415
416 tmp = readw(info->base + S3C2410_RTCCON);
417 writew(tmp & ~S3C2410_RTCCON_CLKRST,
418 info->base + S3C2410_RTCCON);
419 }
420
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700421 if (info->data->needs_src_clk)
422 clk_disable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700423 clk_disable(info->rtc_clk);
424}
425
426static void s3c24xx_rtc_disable(struct s3c_rtc *info)
427{
428 unsigned int con;
429
430 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700431 if (info->data->needs_src_clk)
432 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700433
434 con = readw(info->base + S3C2410_RTCCON);
435 con &= ~S3C2410_RTCCON_RTCEN;
436 writew(con, info->base + S3C2410_RTCCON);
437
438 con = readb(info->base + S3C2410_TICNT);
439 con &= ~S3C2410_TICNT_ENABLE;
440 writeb(con, info->base + S3C2410_TICNT);
441
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700442 if (info->data->needs_src_clk)
443 clk_disable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700444 clk_disable(info->rtc_clk);
445}
446
447static void s3c6410_rtc_disable(struct s3c_rtc *info)
448{
449 unsigned int con;
450
451 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700452 if (info->data->needs_src_clk)
453 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700454
455 con = readw(info->base + S3C2410_RTCCON);
456 con &= ~S3C64XX_RTCCON_TICEN;
457 con &= ~S3C2410_RTCCON_RTCEN;
458 writew(con, info->base + S3C2410_RTCCON);
459
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700460 if (info->data->needs_src_clk)
461 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700462 clk_disable(info->rtc_clk);
Ben Dooks1add6782006-07-01 04:36:26 -0700463}
464
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700465static int s3c_rtc_remove(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700466{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700467 struct s3c_rtc *info = platform_get_drvdata(pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700468
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700469 s3c_rtc_setaie(info->dev, 0);
470
471 clk_unprepare(info->rtc_clk);
472 info->rtc_clk = NULL;
Atul Dahiyae48add82010-07-20 12:19:14 +0530473
Ben Dooks1add6782006-07-01 04:36:26 -0700474 return 0;
475}
476
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900477static const struct of_device_id s3c_rtc_dt_match[];
478
Chanwoo Choiae05c952014-10-13 15:52:33 -0700479static struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900480{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700481 const struct of_device_id *match;
Chanwoo Choid67288d2014-10-13 15:52:31 -0700482
Chanwoo Choiae05c952014-10-13 15:52:33 -0700483 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
484 return (struct s3c_rtc_data *)match->data;
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900485}
486
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800487static int s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700488{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700489 struct s3c_rtc *info = NULL;
Changhwan Youne1df9622010-10-27 15:33:10 -0700490 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700491 struct resource *res;
492 int ret;
493
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700494 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
495 if (!info)
496 return -ENOMEM;
Ben Dooks1add6782006-07-01 04:36:26 -0700497
498 /* find the IRQs */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700499 info->irq_tick = platform_get_irq(pdev, 1);
500 if (info->irq_tick < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700501 dev_err(&pdev->dev, "no irq for rtc tick\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700502 return info->irq_tick;
Ben Dooks1add6782006-07-01 04:36:26 -0700503 }
504
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700505 info->dev = &pdev->dev;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700506 info->data = s3c_rtc_get_data(pdev);
507 if (!info->data) {
508 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
509 return -EINVAL;
510 }
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700511 spin_lock_init(&info->pie_lock);
512 spin_lock_init(&info->alarm_clk_lock);
513
514 platform_set_drvdata(pdev, info);
515
516 info->irq_alarm = platform_get_irq(pdev, 0);
517 if (info->irq_alarm < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700518 dev_err(&pdev->dev, "no irq for alarm\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700519 return info->irq_alarm;
Ben Dooks1add6782006-07-01 04:36:26 -0700520 }
521
Jingoo Hand4a48c22013-02-21 16:45:06 -0800522 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700523 info->irq_tick, info->irq_alarm);
Ben Dooks1add6782006-07-01 04:36:26 -0700524
525 /* get the memory region */
Ben Dooks1add6782006-07-01 04:36:26 -0700526 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700527 info->base = devm_ioremap_resource(&pdev->dev, res);
528 if (IS_ERR(info->base))
529 return PTR_ERR(info->base);
Ben Dooks1add6782006-07-01 04:36:26 -0700530
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700531 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
532 if (IS_ERR(info->rtc_clk)) {
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700533 dev_err(&pdev->dev, "failed to find rtc clock\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700534 return PTR_ERR(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530535 }
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700536 clk_prepare_enable(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530537
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700538 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
539 if (IS_ERR(info->rtc_src_clk)) {
540 dev_err(&pdev->dev, "failed to find rtc source clock\n");
541 return PTR_ERR(info->rtc_src_clk);
542 }
543 clk_prepare_enable(info->rtc_src_clk);
544
545
Ben Dooks1add6782006-07-01 04:36:26 -0700546 /* check to see if everything is setup correctly */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700547 if (info->data->enable)
548 info->data->enable(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700549
Jingoo Hand4a48c22013-02-21 16:45:06 -0800550 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700551 readw(info->base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700552
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700553 device_init_wakeup(&pdev->dev, 1);
554
Ben Dooks1add6782006-07-01 04:36:26 -0700555 /* register RTC and exit */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700556 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
Ben Dooks1add6782006-07-01 04:36:26 -0700557 THIS_MODULE);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700558 if (IS_ERR(info->rtc)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700559 dev_err(&pdev->dev, "cannot attach rtc\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700560 ret = PTR_ERR(info->rtc);
Ben Dooks1add6782006-07-01 04:36:26 -0700561 goto err_nortc;
562 }
563
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700564 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
565 0, "s3c2410-rtc alarm", info);
566 if (ret) {
567 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
568 goto err_nortc;
569 }
570
571 ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
572 0, "s3c2410-rtc tick", info);
573 if (ret) {
574 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
575 goto err_nortc;
576 }
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700577
Taekgyun Ko051fe542010-07-29 13:00:42 +0900578 /* Check RTC Time */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700579 s3c_rtc_gettime(&pdev->dev, &rtc_tm);
Taekgyun Ko051fe542010-07-29 13:00:42 +0900580
Changhwan Youne1df9622010-10-27 15:33:10 -0700581 if (rtc_valid_tm(&rtc_tm)) {
582 rtc_tm.tm_year = 100;
583 rtc_tm.tm_mon = 0;
584 rtc_tm.tm_mday = 1;
585 rtc_tm.tm_hour = 0;
586 rtc_tm.tm_min = 0;
587 rtc_tm.tm_sec = 0;
588
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700589 s3c_rtc_settime(&pdev->dev, &rtc_tm);
Changhwan Youne1df9622010-10-27 15:33:10 -0700590
591 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
Taekgyun Ko051fe542010-07-29 13:00:42 +0900592 }
593
Chanwoo Choiae05c952014-10-13 15:52:33 -0700594 if (info->data->select_tick_clk)
595 info->data->select_tick_clk(info);
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900596
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700597 s3c_rtc_setfreq(info, 1);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700598
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700599 if (info->data->needs_src_clk)
600 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700601 clk_disable(info->rtc_clk);
Donggeun Kimcefe4fb2011-07-25 17:13:32 -0700602
Ben Dooks1add6782006-07-01 04:36:26 -0700603 return 0;
604
605 err_nortc:
Chanwoo Choiae05c952014-10-13 15:52:33 -0700606 if (info->data->disable)
607 info->data->disable(info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700608 clk_disable_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530609
Ben Dooks1add6782006-07-01 04:36:26 -0700610 return ret;
611}
612
Jingoo Han32e445a2013-04-29 16:19:27 -0700613#ifdef CONFIG_PM_SLEEP
Ben Dooks1add6782006-07-01 04:36:26 -0700614
Jingoo Han32e445a2013-04-29 16:19:27 -0700615static int s3c_rtc_suspend(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700616{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700617 struct s3c_rtc *info = dev_get_drvdata(dev);
Jingoo Han32e445a2013-04-29 16:19:27 -0700618
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700619 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700620 if (info->data->needs_src_clk)
621 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700622
Ben Dooks1add6782006-07-01 04:36:26 -0700623 /* save TICNT for anyone using periodic interrupts */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700624 if (info->data->save_tick_cnt)
625 info->data->save_tick_cnt(info);
626
627 if (info->data->disable)
628 info->data->disable(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700629
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700630 if (device_may_wakeup(dev) && !info->wake_en) {
631 if (enable_irq_wake(info->irq_alarm) == 0)
632 info->wake_en = true;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700633 else
Jingoo Han32e445a2013-04-29 16:19:27 -0700634 dev_err(dev, "enable_irq_wake failed\n");
Ben Dooks52cd4e52011-05-11 15:13:28 -0700635 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700636
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700637 if (info->data->needs_src_clk)
638 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700639 clk_disable(info->rtc_clk);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700640
Ben Dooks1add6782006-07-01 04:36:26 -0700641 return 0;
642}
643
Jingoo Han32e445a2013-04-29 16:19:27 -0700644static int s3c_rtc_resume(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700645{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700646 struct s3c_rtc *info = dev_get_drvdata(dev);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700647
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700648 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700649 if (info->data->needs_src_clk)
650 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700651
652 if (info->data->enable)
653 info->data->enable(info);
654
655 if (info->data->restore_tick_cnt)
656 info->data->restore_tick_cnt(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700657
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700658 if (device_may_wakeup(dev) && info->wake_en) {
659 disable_irq_wake(info->irq_alarm);
660 info->wake_en = false;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700661 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700662
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700663 if (info->data->needs_src_clk)
664 clk_disable(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700665 clk_disable(info->rtc_clk);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700666
Ben Dooks1add6782006-07-01 04:36:26 -0700667 return 0;
668}
Ben Dooks1add6782006-07-01 04:36:26 -0700669#endif
Jingoo Han32e445a2013-04-29 16:19:27 -0700670static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
671
Chanwoo Choiae05c952014-10-13 15:52:33 -0700672static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
673{
674 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700675 if (info->data->needs_src_clk)
676 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700677 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700678 if (info->data->needs_src_clk)
679 clk_disable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700680 clk_disable(info->rtc_clk);
681
682 s3c_rtc_alarm_clk_enable(info, false);
683}
684
685static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
686{
687 clk_enable(info->rtc_clk);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700688 if (info->data->needs_src_clk)
689 clk_enable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700690 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
691 writeb(mask, info->base + S3C2410_INTP);
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700692 if (info->data->needs_src_clk)
693 clk_disable(info->rtc_src_clk);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700694 clk_disable(info->rtc_clk);
695
696 s3c_rtc_alarm_clk_enable(info, false);
697}
698
699static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
700{
701 unsigned int tmp = 0;
702 int val;
703
704 tmp = readb(info->base + S3C2410_TICNT);
705 tmp &= S3C2410_TICNT_ENABLE;
706
707 val = (info->rtc->max_user_freq / freq) - 1;
708 tmp |= val;
709
710 writel(tmp, info->base + S3C2410_TICNT);
711}
712
713static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
714{
715 unsigned int tmp = 0;
716 int val;
717
718 tmp = readb(info->base + S3C2410_TICNT);
719 tmp &= S3C2410_TICNT_ENABLE;
720
721 val = (info->rtc->max_user_freq / freq) - 1;
722
723 tmp |= S3C2443_TICNT_PART(val);
724 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
725
726 writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
727
728 writel(tmp, info->base + S3C2410_TICNT);
729}
730
731static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
732{
733 unsigned int tmp = 0;
734 int val;
735
736 tmp = readb(info->base + S3C2410_TICNT);
737 tmp &= S3C2410_TICNT_ENABLE;
738
739 val = (info->rtc->max_user_freq / freq) - 1;
740
741 tmp |= S3C2443_TICNT_PART(val);
742 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
743
744 writel(tmp, info->base + S3C2410_TICNT);
745}
746
747static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
748{
749 int val;
750
751 val = (info->rtc->max_user_freq / freq) - 1;
752 writel(val, info->base + S3C2410_TICNT);
753}
754
755static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
756{
757 unsigned int ticnt;
758
759 ticnt = readb(info->base + S3C2410_TICNT);
760 ticnt &= S3C2410_TICNT_ENABLE;
761
762 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
763}
764
765static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
766{
767 unsigned int con;
768
769 con = readw(info->base + S3C2410_RTCCON);
770 con |= S3C2443_RTCCON_TICSEL;
771 writew(con, info->base + S3C2410_RTCCON);
772}
773
774static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
775{
776 unsigned int ticnt;
777
778 ticnt = readw(info->base + S3C2410_RTCCON);
779 ticnt &= S3C64XX_RTCCON_TICEN;
780
781 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
782}
783
784static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
785{
786 info->ticnt_save = readb(info->base + S3C2410_TICNT);
787}
788
789static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
790{
791 writeb(info->ticnt_save, info->base + S3C2410_TICNT);
792}
793
794static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
795{
796 info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
797 info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
798 info->ticnt_save = readl(info->base + S3C2410_TICNT);
799}
800
801static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
802{
803 unsigned int con;
804
805 writel(info->ticnt_save, info->base + S3C2410_TICNT);
806 if (info->ticnt_en_save) {
807 con = readw(info->base + S3C2410_RTCCON);
808 writew(con | info->ticnt_en_save,
809 info->base + S3C2410_RTCCON);
810 }
811}
812
813static struct s3c_rtc_data const s3c2410_rtc_data = {
814 .max_user_freq = 128,
815 .irq_handler = s3c24xx_rtc_irq,
816 .set_freq = s3c2410_rtc_setfreq,
817 .enable_tick = s3c24xx_rtc_enable_tick,
818 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
819 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
820 .enable = s3c24xx_rtc_enable,
821 .disable = s3c24xx_rtc_disable,
822};
823
824static struct s3c_rtc_data const s3c2416_rtc_data = {
825 .max_user_freq = 32768,
826 .irq_handler = s3c24xx_rtc_irq,
827 .set_freq = s3c2416_rtc_setfreq,
828 .enable_tick = s3c24xx_rtc_enable_tick,
829 .select_tick_clk = s3c2416_rtc_select_tick_clk,
830 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
831 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
832 .enable = s3c24xx_rtc_enable,
833 .disable = s3c24xx_rtc_disable,
834};
835
836static struct s3c_rtc_data const s3c2443_rtc_data = {
837 .max_user_freq = 32768,
838 .irq_handler = s3c24xx_rtc_irq,
839 .set_freq = s3c2443_rtc_setfreq,
840 .enable_tick = s3c24xx_rtc_enable_tick,
841 .select_tick_clk = s3c2416_rtc_select_tick_clk,
842 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
843 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
844 .enable = s3c24xx_rtc_enable,
845 .disable = s3c24xx_rtc_disable,
846};
847
848static struct s3c_rtc_data const s3c6410_rtc_data = {
849 .max_user_freq = 32768,
850 .irq_handler = s3c6410_rtc_irq,
851 .set_freq = s3c6410_rtc_setfreq,
852 .enable_tick = s3c6410_rtc_enable_tick,
853 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
854 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
855 .enable = s3c24xx_rtc_enable,
856 .disable = s3c6410_rtc_disable,
Tushar Beherac3cba922012-04-12 12:49:14 -0700857};
858
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700859static struct s3c_rtc_data const exynos3250_rtc_data = {
860 .max_user_freq = 32768,
861 .needs_src_clk = true,
862 .irq_handler = s3c6410_rtc_irq,
863 .set_freq = s3c6410_rtc_setfreq,
864 .enable_tick = s3c6410_rtc_enable_tick,
865 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
866 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
867 .enable = s3c24xx_rtc_enable,
868 .disable = s3c6410_rtc_disable,
869};
870
Thomas Abraham39ce4082011-10-24 14:49:04 +0200871static const struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900872 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700873 .compatible = "samsung,s3c2410-rtc",
Chanwoo Choiae05c952014-10-13 15:52:33 -0700874 .data = (void *)&s3c2410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900875 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700876 .compatible = "samsung,s3c2416-rtc",
Chanwoo Choiae05c952014-10-13 15:52:33 -0700877 .data = (void *)&s3c2416_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900878 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700879 .compatible = "samsung,s3c2443-rtc",
Chanwoo Choiae05c952014-10-13 15:52:33 -0700880 .data = (void *)&s3c2443_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900881 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700882 .compatible = "samsung,s3c6410-rtc",
Chanwoo Choiae05c952014-10-13 15:52:33 -0700883 .data = (void *)&s3c6410_rtc_data,
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700884 }, {
885 .compatible = "samsung,exynos3250-rtc",
886 .data = (void *)&exynos3250_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900887 },
Chanwoo Choiae05c952014-10-13 15:52:33 -0700888 { /* sentinel */ },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200889};
890MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700891
892static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700893 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800894 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700895 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700896 .name = "s3c-rtc",
Ben Dooks1add6782006-07-01 04:36:26 -0700897 .owner = THIS_MODULE,
Jingoo Han32e445a2013-04-29 16:19:27 -0700898 .pm = &s3c_rtc_pm_ops,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800899 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700900 },
901};
Axel Lin0c4eae62012-01-10 15:10:48 -0800902module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700903
904MODULE_DESCRIPTION("Samsung S3C RTC Driver");
905MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
906MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700907MODULE_ALIAS("platform:s3c2410-rtc");