blob: ea82efba6f9447aef31c7eed6cf52f8d0576a6ab [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;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010042 bool alarm_enabled;
Ben Dooks1add6782006-07-01 04:36:26 -070043
Krzysztof Kozlowski6b720862017-06-16 21:28:05 +020044 const 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;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010050 spinlock_t alarm_lock;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070051
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +020052 int ticnt_save;
53 int ticnt_en_save;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070054 bool wake_en;
55};
56
Chanwoo Choiae05c952014-10-13 15:52:33 -070057struct s3c_rtc_data {
58 int max_user_freq;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070059 bool needs_src_clk;
Chanwoo Choiae05c952014-10-13 15:52:33 -070060
61 void (*irq_handler) (struct s3c_rtc *info, int mask);
62 void (*set_freq) (struct s3c_rtc *info, int freq);
63 void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
64 void (*select_tick_clk) (struct s3c_rtc *info);
65 void (*save_tick_cnt) (struct s3c_rtc *info);
66 void (*restore_tick_cnt) (struct s3c_rtc *info);
67 void (*enable) (struct s3c_rtc *info);
68 void (*disable) (struct s3c_rtc *info);
69};
70
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020071static int s3c_rtc_enable_clk(struct s3c_rtc *info)
Donggeun Kim88cee8f2011-09-14 16:22:19 -070072{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010073 int ret;
Donggeun Kim88cee8f2011-09-14 16:22:19 -070074
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010075 ret = clk_enable(info->rtc_clk);
76 if (ret)
77 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020078
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010079 if (info->data->needs_src_clk) {
80 ret = clk_enable(info->rtc_src_clk);
81 if (ret) {
82 clk_disable(info->rtc_clk);
83 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020084 }
Joonyoung Shim1fb1c352015-08-12 19:21:46 +090085 }
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010086 return 0;
Chanwoo Choi24e14552015-04-16 12:45:15 -070087}
88
89static void s3c_rtc_disable_clk(struct s3c_rtc *info)
90{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010091 if (info->data->needs_src_clk)
92 clk_disable(info->rtc_src_clk);
93 clk_disable(info->rtc_clk);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070094}
95
Ben Dooks1add6782006-07-01 04:36:26 -070096/* IRQ Handlers */
David Howells7d12e782006-10-05 14:55:46 +010097static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070098{
Chanwoo Choi19be09f2014-10-13 15:52:28 -070099 struct s3c_rtc *info = (struct s3c_rtc *)id;
Ben Dooks1add6782006-07-01 04:36:26 -0700100
Chanwoo Choiae05c952014-10-13 15:52:33 -0700101 if (info->data->irq_handler)
102 info->data->irq_handler(info, S3C2410_INTP_TIC);
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530103
Chanwoo Choiae05c952014-10-13 15:52:33 -0700104 return IRQ_HANDLED;
105}
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530106
Chanwoo Choiae05c952014-10-13 15:52:33 -0700107static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
108{
109 struct s3c_rtc *info = (struct s3c_rtc *)id;
110
111 if (info->data->irq_handler)
112 info->data->irq_handler(info, S3C2410_INTP_ALM);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700113
Ben Dooks1add6782006-07-01 04:36:26 -0700114 return IRQ_HANDLED;
115}
116
117/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -0800118static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700119{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700120 struct s3c_rtc *info = dev_get_drvdata(dev);
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100121 unsigned long flags;
Ben Dooks1add6782006-07-01 04:36:26 -0700122 unsigned int tmp;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200123 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700124
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700125 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700126
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200127 ret = s3c_rtc_enable_clk(info);
128 if (ret)
129 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700130
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700131 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700132
Axel Lin2ec38a02011-03-04 17:36:19 -0800133 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700134 tmp |= S3C2410_RTCALM_ALMEN;
135
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700136 writeb(tmp, info->base + S3C2410_RTCALM);
Axel Lin2ec38a02011-03-04 17:36:19 -0800137
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100138 spin_lock_irqsave(&info->alarm_lock, flags);
139
140 if (info->alarm_enabled && !enabled)
141 s3c_rtc_disable_clk(info);
142 else if (!info->alarm_enabled && enabled)
143 ret = s3c_rtc_enable_clk(info);
144
145 info->alarm_enabled = enabled;
146 spin_unlock_irqrestore(&info->alarm_lock, flags);
147
Chanwoo Choi24e14552015-04-16 12:45:15 -0700148 s3c_rtc_disable_clk(info);
Donggeun Kim88cee8f2011-09-14 16:22:19 -0700149
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100150 return ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700151}
152
Chanwoo Choiae05c952014-10-13 15:52:33 -0700153/* Set RTC frequency */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700154static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700155{
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200156 int ret;
157
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800158 if (!is_power_of_2(freq))
159 return -EINVAL;
160
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200161 ret = s3c_rtc_enable_clk(info);
162 if (ret)
163 return ret;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700164 spin_lock_irq(&info->pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700165
Chanwoo Choiae05c952014-10-13 15:52:33 -0700166 if (info->data->set_freq)
167 info->data->set_freq(info, freq);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700168
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700169 spin_unlock_irq(&info->pie_lock);
Alim Akhtar70c96df2016-07-05 15:28:53 +0530170 s3c_rtc_disable_clk(info);
Ben Dooks773be7e2008-07-23 21:30:45 -0700171
172 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700173}
174
175/* Time read/write */
Ben Dooks1add6782006-07-01 04:36:26 -0700176static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
177{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700178 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700179 unsigned int have_retried = 0;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200180 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700181
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200182 ret = s3c_rtc_enable_clk(info);
183 if (ret)
184 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700185
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200186retry_get_time:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700187 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
188 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
189 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
190 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
191 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
192 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700193
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400194 /* the only way to work out whether the system was mid-update
Ben Dooks1add6782006-07-01 04:36:26 -0700195 * when we read it is to check the second counter, and if it
196 * is zero, then we re-try the entire read
197 */
198
199 if (rtc_tm->tm_sec == 0 && !have_retried) {
200 have_retried = 1;
201 goto retry_get_time;
202 }
203
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700204 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
205 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
206 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
207 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
208 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
209 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700210
Chanwoo Choi24e14552015-04-16 12:45:15 -0700211 s3c_rtc_disable_clk(info);
212
Ben Dooks1add6782006-07-01 04:36:26 -0700213 rtc_tm->tm_year += 100;
214 rtc_tm->tm_mon -= 1;
215
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200216 dev_dbg(dev, "read time %ptR\n", rtc_tm);
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100217 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700218}
219
220static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
221{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700222 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks641741e2006-08-27 01:23:27 -0700223 int year = tm->tm_year - 100;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200224 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700225
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200226 dev_dbg(dev, "set time %ptR\n", tm);
Ben Dooks9a654512006-08-27 01:23:22 -0700227
Ben Dooks641741e2006-08-27 01:23:27 -0700228 /* we get around y2k by simply not supporting it */
229
230 if (year < 0 || year >= 100) {
231 dev_err(dev, "rtc only supports 100 years\n");
232 return -EINVAL;
233 }
234
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200235 ret = s3c_rtc_enable_clk(info);
236 if (ret)
237 return ret;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700238
239 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
240 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
241 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
242 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
243 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
244 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
245
Chanwoo Choi24e14552015-04-16 12:45:15 -0700246 s3c_rtc_disable_clk(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700247
248 return 0;
249}
250
251static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
252{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700253 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700254 struct rtc_time *alm_tm = &alrm->time;
255 unsigned int alm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200256 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700257
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200258 ret = s3c_rtc_enable_clk(info);
259 if (ret)
260 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700261
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700262 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
263 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
264 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
265 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
266 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
267 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700268
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700269 alm_en = readb(info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700270
Chanwoo Choi24e14552015-04-16 12:45:15 -0700271 s3c_rtc_disable_clk(info);
272
David Brownella2db8df2006-12-13 00:35:08 -0800273 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
274
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200275 dev_dbg(dev, "read alarm %d, %ptR\n", alm_en, alm_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700276
Ben Dooks1add6782006-07-01 04:36:26 -0700277 /* decode the alarm enable field */
Ben Dooks1add6782006-07-01 04:36:26 -0700278 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700279 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700280
281 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700282 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700283
284 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700285 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700286
287 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700288 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700289
290 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700291 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700292 alm_tm->tm_mon -= 1;
Ben Dooks1add6782006-07-01 04:36:26 -0700293 }
294
295 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700296 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700297
298 return 0;
299}
300
301static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
302{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700303 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700304 struct rtc_time *tm = &alrm->time;
305 unsigned int alrm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200306 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700307
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200308 dev_dbg(dev, "s3c_rtc_setalarm: %d, %ptR\n", alrm->enabled, tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700309
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200310 ret = s3c_rtc_enable_clk(info);
311 if (ret)
312 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700313
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700314 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
315 writeb(0x00, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700316
317 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
318 alrm_en |= S3C2410_RTCALM_SECEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700319 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700320 }
321
322 if (tm->tm_min < 60 && tm->tm_min >= 0) {
323 alrm_en |= S3C2410_RTCALM_MINEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700324 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700325 }
326
327 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
328 alrm_en |= S3C2410_RTCALM_HOUREN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700329 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700330 }
331
Krzysztof Kozlowskifb4ac3c2015-11-01 20:49:04 +0900332 if (tm->tm_mon < 12 && tm->tm_mon >= 0) {
333 alrm_en |= S3C2410_RTCALM_MONEN;
334 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON);
335 }
336
337 if (tm->tm_mday <= 31 && tm->tm_mday >= 1) {
338 alrm_en |= S3C2410_RTCALM_DAYEN;
339 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE);
340 }
341
Jingoo Hand4a48c22013-02-21 16:45:06 -0800342 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
Ben Dooks1add6782006-07-01 04:36:26 -0700343
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700344 writeb(alrm_en, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700345
Chanwoo Choi24e14552015-04-16 12:45:15 -0700346 s3c_rtc_setaie(dev, alrm->enabled);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700347
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100348 s3c_rtc_disable_clk(info);
349
Ben Dooks1add6782006-07-01 04:36:26 -0700350 return 0;
351}
352
Ben Dooks1add6782006-07-01 04:36:26 -0700353static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
354{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700355 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200356 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700357
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200358 ret = s3c_rtc_enable_clk(info);
359 if (ret)
360 return ret;
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700361
Chanwoo Choiae05c952014-10-13 15:52:33 -0700362 if (info->data->enable_tick)
363 info->data->enable_tick(info, seq);
364
Chanwoo Choi24e14552015-04-16 12:45:15 -0700365 s3c_rtc_disable_clk(info);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700366
Ben Dooks1add6782006-07-01 04:36:26 -0700367 return 0;
368}
369
David Brownellff8371a2006-09-30 23:28:17 -0700370static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700371 .read_time = s3c_rtc_gettime,
372 .set_time = s3c_rtc_settime,
373 .read_alarm = s3c_rtc_getalarm,
374 .set_alarm = s3c_rtc_setalarm,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700375 .proc = s3c_rtc_proc,
376 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700377};
378
Chanwoo Choiae05c952014-10-13 15:52:33 -0700379static void s3c24xx_rtc_enable(struct s3c_rtc *info)
Ben Dooks1add6782006-07-01 04:36:26 -0700380{
Chanwoo Choid67288d2014-10-13 15:52:31 -0700381 unsigned int con, tmp;
Ben Dooks1add6782006-07-01 04:36:26 -0700382
Chanwoo Choid67288d2014-10-13 15:52:31 -0700383 con = readw(info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700384 /* re-enable the device, and check it is ok */
385 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
386 dev_info(info->dev, "rtc disabled, re-enabling\n");
Ben Dooks1add6782006-07-01 04:36:26 -0700387
Chanwoo Choiae05c952014-10-13 15:52:33 -0700388 tmp = readw(info->base + S3C2410_RTCCON);
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200389 writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700390 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700391
392 if (con & S3C2410_RTCCON_CNTSEL) {
393 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
394
395 tmp = readw(info->base + S3C2410_RTCCON);
396 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200397 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700398 }
399
400 if (con & S3C2410_RTCCON_CLKRST) {
401 dev_info(info->dev, "removing RTCCON_CLKRST\n");
402
403 tmp = readw(info->base + S3C2410_RTCCON);
404 writew(tmp & ~S3C2410_RTCCON_CLKRST,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200405 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700406 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700407}
408
409static void s3c24xx_rtc_disable(struct s3c_rtc *info)
410{
411 unsigned int con;
412
Chanwoo Choiae05c952014-10-13 15:52:33 -0700413 con = readw(info->base + S3C2410_RTCCON);
414 con &= ~S3C2410_RTCCON_RTCEN;
415 writew(con, info->base + S3C2410_RTCCON);
416
417 con = readb(info->base + S3C2410_TICNT);
418 con &= ~S3C2410_TICNT_ENABLE;
419 writeb(con, info->base + S3C2410_TICNT);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700420}
421
422static void s3c6410_rtc_disable(struct s3c_rtc *info)
423{
424 unsigned int con;
425
Chanwoo Choiae05c952014-10-13 15:52:33 -0700426 con = readw(info->base + S3C2410_RTCCON);
427 con &= ~S3C64XX_RTCCON_TICEN;
428 con &= ~S3C2410_RTCCON_RTCEN;
429 writew(con, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700430}
431
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700432static int s3c_rtc_remove(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700433{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700434 struct s3c_rtc *info = platform_get_drvdata(pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700435
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700436 s3c_rtc_setaie(info->dev, 0);
437
Joonyoung Shim7f23a932015-08-11 20:28:19 +0900438 if (info->data->needs_src_clk)
439 clk_unprepare(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700440 clk_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530441
Ben Dooks1add6782006-07-01 04:36:26 -0700442 return 0;
443}
444
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900445static const struct of_device_id s3c_rtc_dt_match[];
446
Krzysztof Kozlowski6b720862017-06-16 21:28:05 +0200447static const struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900448{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700449 const struct of_device_id *match;
Chanwoo Choid67288d2014-10-13 15:52:31 -0700450
Chanwoo Choiae05c952014-10-13 15:52:33 -0700451 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
Krzysztof Kozlowski6b720862017-06-16 21:28:05 +0200452 return match->data;
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900453}
454
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800455static int s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700456{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700457 struct s3c_rtc *info = NULL;
Changhwan Youne1df9622010-10-27 15:33:10 -0700458 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700459 struct resource *res;
460 int ret;
461
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700462 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
463 if (!info)
464 return -ENOMEM;
Ben Dooks1add6782006-07-01 04:36:26 -0700465
466 /* find the IRQs */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700467 info->irq_tick = platform_get_irq(pdev, 1);
468 if (info->irq_tick < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700469 dev_err(&pdev->dev, "no irq for rtc tick\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700470 return info->irq_tick;
Ben Dooks1add6782006-07-01 04:36:26 -0700471 }
472
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700473 info->dev = &pdev->dev;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700474 info->data = s3c_rtc_get_data(pdev);
475 if (!info->data) {
476 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
477 return -EINVAL;
478 }
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700479 spin_lock_init(&info->pie_lock);
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100480 spin_lock_init(&info->alarm_lock);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700481
482 platform_set_drvdata(pdev, info);
483
484 info->irq_alarm = platform_get_irq(pdev, 0);
485 if (info->irq_alarm < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700486 dev_err(&pdev->dev, "no irq for alarm\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700487 return info->irq_alarm;
Ben Dooks1add6782006-07-01 04:36:26 -0700488 }
489
Jingoo Hand4a48c22013-02-21 16:45:06 -0800490 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200491 info->irq_tick, info->irq_alarm);
Ben Dooks1add6782006-07-01 04:36:26 -0700492
493 /* get the memory region */
Ben Dooks1add6782006-07-01 04:36:26 -0700494 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700495 info->base = devm_ioremap_resource(&pdev->dev, res);
496 if (IS_ERR(info->base))
497 return PTR_ERR(info->base);
Ben Dooks1add6782006-07-01 04:36:26 -0700498
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700499 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
500 if (IS_ERR(info->rtc_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300501 ret = PTR_ERR(info->rtc_clk);
502 if (ret != -EPROBE_DEFER)
503 dev_err(&pdev->dev, "failed to find rtc clock\n");
504 else
505 dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
506 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530507 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200508 ret = clk_prepare_enable(info->rtc_clk);
509 if (ret)
510 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530511
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700512 if (info->data->needs_src_clk) {
513 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
514 if (IS_ERR(info->rtc_src_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300515 ret = PTR_ERR(info->rtc_src_clk);
516 if (ret != -EPROBE_DEFER)
517 dev_err(&pdev->dev,
518 "failed to find rtc source clock\n");
519 else
520 dev_dbg(&pdev->dev,
521 "probe deferred due to missing rtc src clk\n");
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200522 goto err_src_clk;
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700523 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200524 ret = clk_prepare_enable(info->rtc_src_clk);
525 if (ret)
526 goto err_src_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700527 }
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700528
Ben Dooks1add6782006-07-01 04:36:26 -0700529 /* check to see if everything is setup correctly */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700530 if (info->data->enable)
531 info->data->enable(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700532
Jingoo Hand4a48c22013-02-21 16:45:06 -0800533 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200534 readw(info->base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700535
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700536 device_init_wakeup(&pdev->dev, 1);
537
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700538 /* Check RTC Time */
Krzysztof Kozlowski492da682015-04-16 12:46:08 -0700539 if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700540 rtc_tm.tm_year = 100;
541 rtc_tm.tm_mon = 0;
542 rtc_tm.tm_mday = 1;
543 rtc_tm.tm_hour = 0;
544 rtc_tm.tm_min = 0;
545 rtc_tm.tm_sec = 0;
546
547 s3c_rtc_settime(&pdev->dev, &rtc_tm);
548
549 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
550 }
551
Ben Dooks1add6782006-07-01 04:36:26 -0700552 /* register RTC and exit */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700553 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200554 THIS_MODULE);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700555 if (IS_ERR(info->rtc)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700556 dev_err(&pdev->dev, "cannot attach rtc\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700557 ret = PTR_ERR(info->rtc);
Ben Dooks1add6782006-07-01 04:36:26 -0700558 goto err_nortc;
559 }
560
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700561 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200562 0, "s3c2410-rtc alarm", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700563 if (ret) {
564 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
565 goto err_nortc;
566 }
567
568 ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200569 0, "s3c2410-rtc tick", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700570 if (ret) {
571 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
572 goto err_nortc;
573 }
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700574
Chanwoo Choiae05c952014-10-13 15:52:33 -0700575 if (info->data->select_tick_clk)
576 info->data->select_tick_clk(info);
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900577
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700578 s3c_rtc_setfreq(info, 1);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700579
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100580 s3c_rtc_disable_clk(info);
581
Ben Dooks1add6782006-07-01 04:36:26 -0700582 return 0;
583
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200584err_nortc:
Chanwoo Choiae05c952014-10-13 15:52:33 -0700585 if (info->data->disable)
586 info->data->disable(info);
Chanwoo Choi24e14552015-04-16 12:45:15 -0700587
588 if (info->data->needs_src_clk)
589 clk_disable_unprepare(info->rtc_src_clk);
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200590err_src_clk:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700591 clk_disable_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530592
Ben Dooks1add6782006-07-01 04:36:26 -0700593 return ret;
594}
595
Jingoo Han32e445a2013-04-29 16:19:27 -0700596#ifdef CONFIG_PM_SLEEP
Ben Dooks1add6782006-07-01 04:36:26 -0700597
Jingoo Han32e445a2013-04-29 16:19:27 -0700598static int s3c_rtc_suspend(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700599{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700600 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200601 int ret;
Jingoo Han32e445a2013-04-29 16:19:27 -0700602
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200603 ret = s3c_rtc_enable_clk(info);
604 if (ret)
605 return ret;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700606
Ben Dooks1add6782006-07-01 04:36:26 -0700607 /* save TICNT for anyone using periodic interrupts */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700608 if (info->data->save_tick_cnt)
609 info->data->save_tick_cnt(info);
610
611 if (info->data->disable)
612 info->data->disable(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700613
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700614 if (device_may_wakeup(dev) && !info->wake_en) {
615 if (enable_irq_wake(info->irq_alarm) == 0)
616 info->wake_en = true;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700617 else
Jingoo Han32e445a2013-04-29 16:19:27 -0700618 dev_err(dev, "enable_irq_wake failed\n");
Ben Dooks52cd4e52011-05-11 15:13:28 -0700619 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700620
Ben Dooks1add6782006-07-01 04:36:26 -0700621 return 0;
622}
623
Jingoo Han32e445a2013-04-29 16:19:27 -0700624static int s3c_rtc_resume(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700625{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700626 struct s3c_rtc *info = dev_get_drvdata(dev);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700627
Chanwoo Choiae05c952014-10-13 15:52:33 -0700628 if (info->data->enable)
629 info->data->enable(info);
630
631 if (info->data->restore_tick_cnt)
632 info->data->restore_tick_cnt(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700633
Chanwoo Choi24e14552015-04-16 12:45:15 -0700634 s3c_rtc_disable_clk(info);
635
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700636 if (device_may_wakeup(dev) && info->wake_en) {
637 disable_irq_wake(info->irq_alarm);
638 info->wake_en = false;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700639 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700640
Ben Dooks1add6782006-07-01 04:36:26 -0700641 return 0;
642}
Ben Dooks1add6782006-07-01 04:36:26 -0700643#endif
Jingoo Han32e445a2013-04-29 16:19:27 -0700644static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
645
Chanwoo Choiae05c952014-10-13 15:52:33 -0700646static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
647{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700648 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700649}
650
651static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
652{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700653 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
654 writeb(mask, info->base + S3C2410_INTP);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700655}
656
657static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
658{
659 unsigned int tmp = 0;
660 int val;
661
662 tmp = readb(info->base + S3C2410_TICNT);
663 tmp &= S3C2410_TICNT_ENABLE;
664
665 val = (info->rtc->max_user_freq / freq) - 1;
666 tmp |= val;
667
668 writel(tmp, info->base + S3C2410_TICNT);
669}
670
671static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
672{
673 unsigned int tmp = 0;
674 int val;
675
676 tmp = readb(info->base + S3C2410_TICNT);
677 tmp &= S3C2410_TICNT_ENABLE;
678
679 val = (info->rtc->max_user_freq / freq) - 1;
680
681 tmp |= S3C2443_TICNT_PART(val);
682 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
683
684 writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
685
686 writel(tmp, info->base + S3C2410_TICNT);
687}
688
689static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
690{
691 unsigned int tmp = 0;
692 int val;
693
694 tmp = readb(info->base + S3C2410_TICNT);
695 tmp &= S3C2410_TICNT_ENABLE;
696
697 val = (info->rtc->max_user_freq / freq) - 1;
698
699 tmp |= S3C2443_TICNT_PART(val);
700 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
701
702 writel(tmp, info->base + S3C2410_TICNT);
703}
704
705static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
706{
707 int val;
708
709 val = (info->rtc->max_user_freq / freq) - 1;
710 writel(val, info->base + S3C2410_TICNT);
711}
712
713static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
714{
715 unsigned int ticnt;
716
717 ticnt = readb(info->base + S3C2410_TICNT);
718 ticnt &= S3C2410_TICNT_ENABLE;
719
720 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
721}
722
723static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
724{
725 unsigned int con;
726
727 con = readw(info->base + S3C2410_RTCCON);
728 con |= S3C2443_RTCCON_TICSEL;
729 writew(con, info->base + S3C2410_RTCCON);
730}
731
732static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
733{
734 unsigned int ticnt;
735
736 ticnt = readw(info->base + S3C2410_RTCCON);
737 ticnt &= S3C64XX_RTCCON_TICEN;
738
739 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
740}
741
742static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
743{
744 info->ticnt_save = readb(info->base + S3C2410_TICNT);
745}
746
747static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
748{
749 writeb(info->ticnt_save, info->base + S3C2410_TICNT);
750}
751
752static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
753{
754 info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
755 info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
756 info->ticnt_save = readl(info->base + S3C2410_TICNT);
757}
758
759static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
760{
761 unsigned int con;
762
763 writel(info->ticnt_save, info->base + S3C2410_TICNT);
764 if (info->ticnt_en_save) {
765 con = readw(info->base + S3C2410_RTCCON);
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200766 writew(con | info->ticnt_en_save, info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700767 }
768}
769
770static struct s3c_rtc_data const s3c2410_rtc_data = {
771 .max_user_freq = 128,
772 .irq_handler = s3c24xx_rtc_irq,
773 .set_freq = s3c2410_rtc_setfreq,
774 .enable_tick = s3c24xx_rtc_enable_tick,
775 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
776 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
777 .enable = s3c24xx_rtc_enable,
778 .disable = s3c24xx_rtc_disable,
779};
780
781static struct s3c_rtc_data const s3c2416_rtc_data = {
782 .max_user_freq = 32768,
783 .irq_handler = s3c24xx_rtc_irq,
784 .set_freq = s3c2416_rtc_setfreq,
785 .enable_tick = s3c24xx_rtc_enable_tick,
786 .select_tick_clk = s3c2416_rtc_select_tick_clk,
787 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
788 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
789 .enable = s3c24xx_rtc_enable,
790 .disable = s3c24xx_rtc_disable,
791};
792
793static struct s3c_rtc_data const s3c2443_rtc_data = {
794 .max_user_freq = 32768,
795 .irq_handler = s3c24xx_rtc_irq,
796 .set_freq = s3c2443_rtc_setfreq,
797 .enable_tick = s3c24xx_rtc_enable_tick,
798 .select_tick_clk = s3c2416_rtc_select_tick_clk,
799 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
800 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
801 .enable = s3c24xx_rtc_enable,
802 .disable = s3c24xx_rtc_disable,
803};
804
805static struct s3c_rtc_data const s3c6410_rtc_data = {
806 .max_user_freq = 32768,
Javier Martinez Canillas8792f7772015-03-12 16:25:49 -0700807 .needs_src_clk = true,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700808 .irq_handler = s3c6410_rtc_irq,
809 .set_freq = s3c6410_rtc_setfreq,
810 .enable_tick = s3c6410_rtc_enable_tick,
811 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
812 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
813 .enable = s3c24xx_rtc_enable,
814 .disable = s3c6410_rtc_disable,
Tushar Beherac3cba922012-04-12 12:49:14 -0700815};
816
Thomas Abraham39ce4082011-10-24 14:49:04 +0200817static const struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900818 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700819 .compatible = "samsung,s3c2410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200820 .data = &s3c2410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900821 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700822 .compatible = "samsung,s3c2416-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200823 .data = &s3c2416_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900824 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700825 .compatible = "samsung,s3c2443-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200826 .data = &s3c2443_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900827 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700828 .compatible = "samsung,s3c6410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200829 .data = &s3c6410_rtc_data,
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700830 }, {
831 .compatible = "samsung,exynos3250-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200832 .data = &s3c6410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900833 },
Chanwoo Choiae05c952014-10-13 15:52:33 -0700834 { /* sentinel */ },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200835};
836MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700837
838static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700839 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800840 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700841 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700842 .name = "s3c-rtc",
Jingoo Han32e445a2013-04-29 16:19:27 -0700843 .pm = &s3c_rtc_pm_ops,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800844 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700845 },
846};
Axel Lin0c4eae62012-01-10 15:10:48 -0800847module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700848
849MODULE_DESCRIPTION("Samsung S3C RTC Driver");
850MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
851MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700852MODULE_ALIAS("platform:s3c2410-rtc");