blob: e81a2b22a5c374451ddfd06818c0f36df2c5d135 [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>
Marek Szyprowski64704c92019-01-18 14:28:37 +010029#include <linux/of_device.h>
Sachin Kamatdbd9acb2012-07-30 14:41:48 -070030#include <linux/uaccess.h>
31#include <linux/io.h>
Ben Dooks1add6782006-07-01 04:36:26 -070032
Ben Dooks1add6782006-07-01 04:36:26 -070033#include <asm/irq.h>
Arnd Bergmannb9d7c5d2013-03-05 12:04:37 +010034#include "rtc-s3c.h"
Ben Dooks1add6782006-07-01 04:36:26 -070035
Chanwoo Choi19be09f2014-10-13 15:52:28 -070036struct s3c_rtc {
37 struct device *dev;
38 struct rtc_device *rtc;
Ben Dooks1add6782006-07-01 04:36:26 -070039
Chanwoo Choi19be09f2014-10-13 15:52:28 -070040 void __iomem *base;
41 struct clk *rtc_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070042 struct clk *rtc_src_clk;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010043 bool alarm_enabled;
Ben Dooks1add6782006-07-01 04:36:26 -070044
Krzysztof Kozlowski6b720862017-06-16 21:28:05 +020045 const struct s3c_rtc_data *data;
Ben Dooks1add6782006-07-01 04:36:26 -070046
Chanwoo Choi19be09f2014-10-13 15:52:28 -070047 int irq_alarm;
48 int irq_tick;
49
50 spinlock_t pie_lock;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010051 spinlock_t alarm_lock;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070052
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +020053 int ticnt_save;
54 int ticnt_en_save;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070055 bool wake_en;
56};
57
Chanwoo Choiae05c952014-10-13 15:52:33 -070058struct s3c_rtc_data {
59 int max_user_freq;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070060 bool needs_src_clk;
Chanwoo Choiae05c952014-10-13 15:52:33 -070061
62 void (*irq_handler) (struct s3c_rtc *info, int mask);
63 void (*set_freq) (struct s3c_rtc *info, int freq);
64 void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
65 void (*select_tick_clk) (struct s3c_rtc *info);
66 void (*save_tick_cnt) (struct s3c_rtc *info);
67 void (*restore_tick_cnt) (struct s3c_rtc *info);
68 void (*enable) (struct s3c_rtc *info);
69 void (*disable) (struct s3c_rtc *info);
70};
71
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020072static int s3c_rtc_enable_clk(struct s3c_rtc *info)
Donggeun Kim88cee8f2011-09-14 16:22:19 -070073{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010074 int ret;
Donggeun Kim88cee8f2011-09-14 16:22:19 -070075
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010076 ret = clk_enable(info->rtc_clk);
77 if (ret)
78 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020079
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010080 if (info->data->needs_src_clk) {
81 ret = clk_enable(info->rtc_src_clk);
82 if (ret) {
83 clk_disable(info->rtc_clk);
84 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020085 }
Joonyoung Shim1fb1c352015-08-12 19:21:46 +090086 }
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010087 return 0;
Chanwoo Choi24e14552015-04-16 12:45:15 -070088}
89
90static void s3c_rtc_disable_clk(struct s3c_rtc *info)
91{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010092 if (info->data->needs_src_clk)
93 clk_disable(info->rtc_src_clk);
94 clk_disable(info->rtc_clk);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070095}
96
Ben Dooks1add6782006-07-01 04:36:26 -070097/* IRQ Handlers */
David Howells7d12e782006-10-05 14:55:46 +010098static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
Ben Dooks1add6782006-07-01 04:36:26 -070099{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700100 struct s3c_rtc *info = (struct s3c_rtc *)id;
Ben Dooks1add6782006-07-01 04:36:26 -0700101
Chanwoo Choiae05c952014-10-13 15:52:33 -0700102 if (info->data->irq_handler)
103 info->data->irq_handler(info, S3C2410_INTP_TIC);
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530104
Chanwoo Choiae05c952014-10-13 15:52:33 -0700105 return IRQ_HANDLED;
106}
Atul Dahiya2f3478f2010-07-20 16:02:51 +0530107
Chanwoo Choiae05c952014-10-13 15:52:33 -0700108static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
109{
110 struct s3c_rtc *info = (struct s3c_rtc *)id;
111
112 if (info->data->irq_handler)
113 info->data->irq_handler(info, S3C2410_INTP_ALM);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700114
Ben Dooks1add6782006-07-01 04:36:26 -0700115 return IRQ_HANDLED;
116}
117
118/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -0800119static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700120{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700121 struct s3c_rtc *info = dev_get_drvdata(dev);
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100122 unsigned long flags;
Ben Dooks1add6782006-07-01 04:36:26 -0700123 unsigned int tmp;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200124 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700125
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700126 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700127
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200128 ret = s3c_rtc_enable_clk(info);
129 if (ret)
130 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700131
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700132 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700133
Axel Lin2ec38a02011-03-04 17:36:19 -0800134 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700135 tmp |= S3C2410_RTCALM_ALMEN;
136
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700137 writeb(tmp, info->base + S3C2410_RTCALM);
Axel Lin2ec38a02011-03-04 17:36:19 -0800138
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100139 spin_lock_irqsave(&info->alarm_lock, flags);
140
141 if (info->alarm_enabled && !enabled)
142 s3c_rtc_disable_clk(info);
143 else if (!info->alarm_enabled && enabled)
144 ret = s3c_rtc_enable_clk(info);
145
146 info->alarm_enabled = enabled;
147 spin_unlock_irqrestore(&info->alarm_lock, flags);
148
Chanwoo Choi24e14552015-04-16 12:45:15 -0700149 s3c_rtc_disable_clk(info);
Donggeun Kim88cee8f2011-09-14 16:22:19 -0700150
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100151 return ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700152}
153
Chanwoo Choiae05c952014-10-13 15:52:33 -0700154/* Set RTC frequency */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700155static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
Ben Dooks1add6782006-07-01 04:36:26 -0700156{
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200157 int ret;
158
Jonathan Cameron5d2a5032009-01-06 14:42:12 -0800159 if (!is_power_of_2(freq))
160 return -EINVAL;
161
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200162 ret = s3c_rtc_enable_clk(info);
163 if (ret)
164 return ret;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700165 spin_lock_irq(&info->pie_lock);
Ben Dooks773be7e2008-07-23 21:30:45 -0700166
Chanwoo Choiae05c952014-10-13 15:52:33 -0700167 if (info->data->set_freq)
168 info->data->set_freq(info, freq);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700169
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700170 spin_unlock_irq(&info->pie_lock);
Alim Akhtar70c96df2016-07-05 15:28:53 +0530171 s3c_rtc_disable_clk(info);
Ben Dooks773be7e2008-07-23 21:30:45 -0700172
173 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700174}
175
176/* Time read/write */
Ben Dooks1add6782006-07-01 04:36:26 -0700177static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
178{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700179 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700180 unsigned int have_retried = 0;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200181 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700182
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200183 ret = s3c_rtc_enable_clk(info);
184 if (ret)
185 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700186
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200187retry_get_time:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700188 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
189 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
190 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
191 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
192 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
193 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700194
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400195 /* the only way to work out whether the system was mid-update
Ben Dooks1add6782006-07-01 04:36:26 -0700196 * when we read it is to check the second counter, and if it
197 * is zero, then we re-try the entire read
198 */
199
200 if (rtc_tm->tm_sec == 0 && !have_retried) {
201 have_retried = 1;
202 goto retry_get_time;
203 }
204
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700205 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
206 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
207 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
208 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
209 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
210 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700211
Chanwoo Choi24e14552015-04-16 12:45:15 -0700212 s3c_rtc_disable_clk(info);
213
Ben Dooks1add6782006-07-01 04:36:26 -0700214 rtc_tm->tm_year += 100;
215 rtc_tm->tm_mon -= 1;
216
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200217 dev_dbg(dev, "read time %ptR\n", rtc_tm);
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100218 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700219}
220
221static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
222{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700223 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks641741e2006-08-27 01:23:27 -0700224 int year = tm->tm_year - 100;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200225 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700226
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200227 dev_dbg(dev, "set time %ptR\n", tm);
Ben Dooks9a654512006-08-27 01:23:22 -0700228
Ben Dooks641741e2006-08-27 01:23:27 -0700229 /* we get around y2k by simply not supporting it */
230
231 if (year < 0 || year >= 100) {
232 dev_err(dev, "rtc only supports 100 years\n");
233 return -EINVAL;
234 }
235
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200236 ret = s3c_rtc_enable_clk(info);
237 if (ret)
238 return ret;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700239
240 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
241 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
242 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
243 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
244 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
245 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
246
Chanwoo Choi24e14552015-04-16 12:45:15 -0700247 s3c_rtc_disable_clk(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700248
249 return 0;
250}
251
252static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
253{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700254 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700255 struct rtc_time *alm_tm = &alrm->time;
256 unsigned int alm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200257 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700258
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200259 ret = s3c_rtc_enable_clk(info);
260 if (ret)
261 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700262
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700263 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
264 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
265 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
266 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
267 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
268 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700269
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700270 alm_en = readb(info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700271
Chanwoo Choi24e14552015-04-16 12:45:15 -0700272 s3c_rtc_disable_clk(info);
273
David Brownella2db8df2006-12-13 00:35:08 -0800274 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
275
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200276 dev_dbg(dev, "read alarm %d, %ptR\n", alm_en, alm_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700277
Ben Dooks1add6782006-07-01 04:36:26 -0700278 /* decode the alarm enable field */
Ben Dooks1add6782006-07-01 04:36:26 -0700279 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700280 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700281
282 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700283 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
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
288 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700289 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700290
291 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700292 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700293 alm_tm->tm_mon -= 1;
Ben Dooks1add6782006-07-01 04:36:26 -0700294 }
295
296 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700297 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700298
299 return 0;
300}
301
302static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
303{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700304 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700305 struct rtc_time *tm = &alrm->time;
306 unsigned int alrm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200307 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700308
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200309 dev_dbg(dev, "s3c_rtc_setalarm: %d, %ptR\n", alrm->enabled, tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700310
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200311 ret = s3c_rtc_enable_clk(info);
312 if (ret)
313 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700314
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700315 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
316 writeb(0x00, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700317
318 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
319 alrm_en |= S3C2410_RTCALM_SECEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700320 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700321 }
322
323 if (tm->tm_min < 60 && tm->tm_min >= 0) {
324 alrm_en |= S3C2410_RTCALM_MINEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700325 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700326 }
327
328 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
329 alrm_en |= S3C2410_RTCALM_HOUREN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700330 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700331 }
332
Krzysztof Kozlowskifb4ac3c2015-11-01 20:49:04 +0900333 if (tm->tm_mon < 12 && tm->tm_mon >= 0) {
334 alrm_en |= S3C2410_RTCALM_MONEN;
335 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON);
336 }
337
338 if (tm->tm_mday <= 31 && tm->tm_mday >= 1) {
339 alrm_en |= S3C2410_RTCALM_DAYEN;
340 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE);
341 }
342
Jingoo Hand4a48c22013-02-21 16:45:06 -0800343 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
Ben Dooks1add6782006-07-01 04:36:26 -0700344
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700345 writeb(alrm_en, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700346
Chanwoo Choi24e14552015-04-16 12:45:15 -0700347 s3c_rtc_setaie(dev, alrm->enabled);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700348
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100349 s3c_rtc_disable_clk(info);
350
Ben Dooks1add6782006-07-01 04:36:26 -0700351 return 0;
352}
353
Ben Dooks1add6782006-07-01 04:36:26 -0700354static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
355{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700356 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200357 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700358
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200359 ret = s3c_rtc_enable_clk(info);
360 if (ret)
361 return ret;
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700362
Chanwoo Choiae05c952014-10-13 15:52:33 -0700363 if (info->data->enable_tick)
364 info->data->enable_tick(info, seq);
365
Chanwoo Choi24e14552015-04-16 12:45:15 -0700366 s3c_rtc_disable_clk(info);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700367
Ben Dooks1add6782006-07-01 04:36:26 -0700368 return 0;
369}
370
David Brownellff8371a2006-09-30 23:28:17 -0700371static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700372 .read_time = s3c_rtc_gettime,
373 .set_time = s3c_rtc_settime,
374 .read_alarm = s3c_rtc_getalarm,
375 .set_alarm = s3c_rtc_setalarm,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700376 .proc = s3c_rtc_proc,
377 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700378};
379
Chanwoo Choiae05c952014-10-13 15:52:33 -0700380static void s3c24xx_rtc_enable(struct s3c_rtc *info)
Ben Dooks1add6782006-07-01 04:36:26 -0700381{
Chanwoo Choid67288d2014-10-13 15:52:31 -0700382 unsigned int con, tmp;
Ben Dooks1add6782006-07-01 04:36:26 -0700383
Chanwoo Choid67288d2014-10-13 15:52:31 -0700384 con = readw(info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700385 /* re-enable the device, and check it is ok */
386 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
387 dev_info(info->dev, "rtc disabled, re-enabling\n");
Ben Dooks1add6782006-07-01 04:36:26 -0700388
Chanwoo Choiae05c952014-10-13 15:52:33 -0700389 tmp = readw(info->base + S3C2410_RTCCON);
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200390 writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700391 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700392
393 if (con & S3C2410_RTCCON_CNTSEL) {
394 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
395
396 tmp = readw(info->base + S3C2410_RTCCON);
397 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200398 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700399 }
400
401 if (con & S3C2410_RTCCON_CLKRST) {
402 dev_info(info->dev, "removing RTCCON_CLKRST\n");
403
404 tmp = readw(info->base + S3C2410_RTCCON);
405 writew(tmp & ~S3C2410_RTCCON_CLKRST,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200406 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700407 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700408}
409
410static void s3c24xx_rtc_disable(struct s3c_rtc *info)
411{
412 unsigned int con;
413
Chanwoo Choiae05c952014-10-13 15:52:33 -0700414 con = readw(info->base + S3C2410_RTCCON);
415 con &= ~S3C2410_RTCCON_RTCEN;
416 writew(con, info->base + S3C2410_RTCCON);
417
418 con = readb(info->base + S3C2410_TICNT);
419 con &= ~S3C2410_TICNT_ENABLE;
420 writeb(con, info->base + S3C2410_TICNT);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700421}
422
423static void s3c6410_rtc_disable(struct s3c_rtc *info)
424{
425 unsigned int con;
426
Chanwoo Choiae05c952014-10-13 15:52:33 -0700427 con = readw(info->base + S3C2410_RTCCON);
428 con &= ~S3C64XX_RTCCON_TICEN;
429 con &= ~S3C2410_RTCCON_RTCEN;
430 writew(con, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700431}
432
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700433static int s3c_rtc_remove(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700434{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700435 struct s3c_rtc *info = platform_get_drvdata(pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700436
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700437 s3c_rtc_setaie(info->dev, 0);
438
Joonyoung Shim7f23a932015-08-11 20:28:19 +0900439 if (info->data->needs_src_clk)
440 clk_unprepare(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700441 clk_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530442
Ben Dooks1add6782006-07-01 04:36:26 -0700443 return 0;
444}
445
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800446static int s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700447{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700448 struct s3c_rtc *info = NULL;
Changhwan Youne1df9622010-10-27 15:33:10 -0700449 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700450 struct resource *res;
451 int ret;
452
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700453 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
454 if (!info)
455 return -ENOMEM;
Ben Dooks1add6782006-07-01 04:36:26 -0700456
457 /* find the IRQs */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700458 info->irq_tick = platform_get_irq(pdev, 1);
459 if (info->irq_tick < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700460 dev_err(&pdev->dev, "no irq for rtc tick\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700461 return info->irq_tick;
Ben Dooks1add6782006-07-01 04:36:26 -0700462 }
463
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700464 info->dev = &pdev->dev;
Marek Szyprowski64704c92019-01-18 14:28:37 +0100465 info->data = of_device_get_match_data(&pdev->dev);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700466 if (!info->data) {
467 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
468 return -EINVAL;
469 }
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700470 spin_lock_init(&info->pie_lock);
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100471 spin_lock_init(&info->alarm_lock);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700472
473 platform_set_drvdata(pdev, info);
474
475 info->irq_alarm = platform_get_irq(pdev, 0);
476 if (info->irq_alarm < 0) {
Ben Dooks1add6782006-07-01 04:36:26 -0700477 dev_err(&pdev->dev, "no irq for alarm\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700478 return info->irq_alarm;
Ben Dooks1add6782006-07-01 04:36:26 -0700479 }
480
Jingoo Hand4a48c22013-02-21 16:45:06 -0800481 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200482 info->irq_tick, info->irq_alarm);
Ben Dooks1add6782006-07-01 04:36:26 -0700483
484 /* get the memory region */
Ben Dooks1add6782006-07-01 04:36:26 -0700485 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700486 info->base = devm_ioremap_resource(&pdev->dev, res);
487 if (IS_ERR(info->base))
488 return PTR_ERR(info->base);
Ben Dooks1add6782006-07-01 04:36:26 -0700489
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700490 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
491 if (IS_ERR(info->rtc_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300492 ret = PTR_ERR(info->rtc_clk);
493 if (ret != -EPROBE_DEFER)
494 dev_err(&pdev->dev, "failed to find rtc clock\n");
495 else
496 dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
497 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530498 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200499 ret = clk_prepare_enable(info->rtc_clk);
500 if (ret)
501 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530502
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700503 if (info->data->needs_src_clk) {
504 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
505 if (IS_ERR(info->rtc_src_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300506 ret = PTR_ERR(info->rtc_src_clk);
507 if (ret != -EPROBE_DEFER)
508 dev_err(&pdev->dev,
509 "failed to find rtc source clock\n");
510 else
511 dev_dbg(&pdev->dev,
512 "probe deferred due to missing rtc src clk\n");
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200513 goto err_src_clk;
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700514 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200515 ret = clk_prepare_enable(info->rtc_src_clk);
516 if (ret)
517 goto err_src_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700518 }
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700519
Ben Dooks1add6782006-07-01 04:36:26 -0700520 /* check to see if everything is setup correctly */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700521 if (info->data->enable)
522 info->data->enable(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700523
Jingoo Hand4a48c22013-02-21 16:45:06 -0800524 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200525 readw(info->base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700526
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700527 device_init_wakeup(&pdev->dev, 1);
528
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700529 /* Check RTC Time */
Krzysztof Kozlowski492da682015-04-16 12:46:08 -0700530 if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700531 rtc_tm.tm_year = 100;
532 rtc_tm.tm_mon = 0;
533 rtc_tm.tm_mday = 1;
534 rtc_tm.tm_hour = 0;
535 rtc_tm.tm_min = 0;
536 rtc_tm.tm_sec = 0;
537
538 s3c_rtc_settime(&pdev->dev, &rtc_tm);
539
540 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
541 }
542
Ben Dooks1add6782006-07-01 04:36:26 -0700543 /* register RTC and exit */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700544 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200545 THIS_MODULE);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700546 if (IS_ERR(info->rtc)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700547 dev_err(&pdev->dev, "cannot attach rtc\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700548 ret = PTR_ERR(info->rtc);
Ben Dooks1add6782006-07-01 04:36:26 -0700549 goto err_nortc;
550 }
551
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700552 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200553 0, "s3c2410-rtc alarm", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700554 if (ret) {
555 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
556 goto err_nortc;
557 }
558
559 ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200560 0, "s3c2410-rtc tick", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700561 if (ret) {
562 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
563 goto err_nortc;
564 }
Maurus Cuelenaereeaa6e4d2010-06-04 14:14:46 -0700565
Chanwoo Choiae05c952014-10-13 15:52:33 -0700566 if (info->data->select_tick_clk)
567 info->data->select_tick_clk(info);
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900568
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700569 s3c_rtc_setfreq(info, 1);
Maurus Cuelenaeree893de52010-06-04 14:14:44 -0700570
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100571 s3c_rtc_disable_clk(info);
572
Ben Dooks1add6782006-07-01 04:36:26 -0700573 return 0;
574
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200575err_nortc:
Chanwoo Choiae05c952014-10-13 15:52:33 -0700576 if (info->data->disable)
577 info->data->disable(info);
Chanwoo Choi24e14552015-04-16 12:45:15 -0700578
579 if (info->data->needs_src_clk)
580 clk_disable_unprepare(info->rtc_src_clk);
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200581err_src_clk:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700582 clk_disable_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530583
Ben Dooks1add6782006-07-01 04:36:26 -0700584 return ret;
585}
586
Jingoo Han32e445a2013-04-29 16:19:27 -0700587#ifdef CONFIG_PM_SLEEP
Ben Dooks1add6782006-07-01 04:36:26 -0700588
Jingoo Han32e445a2013-04-29 16:19:27 -0700589static int s3c_rtc_suspend(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700590{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700591 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200592 int ret;
Jingoo Han32e445a2013-04-29 16:19:27 -0700593
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200594 ret = s3c_rtc_enable_clk(info);
595 if (ret)
596 return ret;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700597
Ben Dooks1add6782006-07-01 04:36:26 -0700598 /* save TICNT for anyone using periodic interrupts */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700599 if (info->data->save_tick_cnt)
600 info->data->save_tick_cnt(info);
601
602 if (info->data->disable)
603 info->data->disable(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700604
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700605 if (device_may_wakeup(dev) && !info->wake_en) {
606 if (enable_irq_wake(info->irq_alarm) == 0)
607 info->wake_en = true;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700608 else
Jingoo Han32e445a2013-04-29 16:19:27 -0700609 dev_err(dev, "enable_irq_wake failed\n");
Ben Dooks52cd4e52011-05-11 15:13:28 -0700610 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700611
Ben Dooks1add6782006-07-01 04:36:26 -0700612 return 0;
613}
614
Jingoo Han32e445a2013-04-29 16:19:27 -0700615static int s3c_rtc_resume(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);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700618
Chanwoo Choiae05c952014-10-13 15:52:33 -0700619 if (info->data->enable)
620 info->data->enable(info);
621
622 if (info->data->restore_tick_cnt)
623 info->data->restore_tick_cnt(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700624
Chanwoo Choi24e14552015-04-16 12:45:15 -0700625 s3c_rtc_disable_clk(info);
626
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700627 if (device_may_wakeup(dev) && info->wake_en) {
628 disable_irq_wake(info->irq_alarm);
629 info->wake_en = false;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700630 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700631
Ben Dooks1add6782006-07-01 04:36:26 -0700632 return 0;
633}
Ben Dooks1add6782006-07-01 04:36:26 -0700634#endif
Jingoo Han32e445a2013-04-29 16:19:27 -0700635static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
636
Chanwoo Choiae05c952014-10-13 15:52:33 -0700637static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
638{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700639 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700640}
641
642static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
643{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700644 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
645 writeb(mask, info->base + S3C2410_INTP);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700646}
647
648static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
649{
650 unsigned int tmp = 0;
651 int val;
652
653 tmp = readb(info->base + S3C2410_TICNT);
654 tmp &= S3C2410_TICNT_ENABLE;
655
656 val = (info->rtc->max_user_freq / freq) - 1;
657 tmp |= val;
658
659 writel(tmp, info->base + S3C2410_TICNT);
660}
661
662static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
663{
664 unsigned int tmp = 0;
665 int val;
666
667 tmp = readb(info->base + S3C2410_TICNT);
668 tmp &= S3C2410_TICNT_ENABLE;
669
670 val = (info->rtc->max_user_freq / freq) - 1;
671
672 tmp |= S3C2443_TICNT_PART(val);
673 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
674
675 writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
676
677 writel(tmp, info->base + S3C2410_TICNT);
678}
679
680static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
681{
682 unsigned int tmp = 0;
683 int val;
684
685 tmp = readb(info->base + S3C2410_TICNT);
686 tmp &= S3C2410_TICNT_ENABLE;
687
688 val = (info->rtc->max_user_freq / freq) - 1;
689
690 tmp |= S3C2443_TICNT_PART(val);
691 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
692
693 writel(tmp, info->base + S3C2410_TICNT);
694}
695
696static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
697{
698 int val;
699
700 val = (info->rtc->max_user_freq / freq) - 1;
701 writel(val, info->base + S3C2410_TICNT);
702}
703
704static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
705{
706 unsigned int ticnt;
707
708 ticnt = readb(info->base + S3C2410_TICNT);
709 ticnt &= S3C2410_TICNT_ENABLE;
710
711 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
712}
713
714static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
715{
716 unsigned int con;
717
718 con = readw(info->base + S3C2410_RTCCON);
719 con |= S3C2443_RTCCON_TICSEL;
720 writew(con, info->base + S3C2410_RTCCON);
721}
722
723static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
724{
725 unsigned int ticnt;
726
727 ticnt = readw(info->base + S3C2410_RTCCON);
728 ticnt &= S3C64XX_RTCCON_TICEN;
729
730 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
731}
732
733static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
734{
735 info->ticnt_save = readb(info->base + S3C2410_TICNT);
736}
737
738static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
739{
740 writeb(info->ticnt_save, info->base + S3C2410_TICNT);
741}
742
743static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
744{
745 info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
746 info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
747 info->ticnt_save = readl(info->base + S3C2410_TICNT);
748}
749
750static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
751{
752 unsigned int con;
753
754 writel(info->ticnt_save, info->base + S3C2410_TICNT);
755 if (info->ticnt_en_save) {
756 con = readw(info->base + S3C2410_RTCCON);
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200757 writew(con | info->ticnt_en_save, info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700758 }
759}
760
761static struct s3c_rtc_data const s3c2410_rtc_data = {
762 .max_user_freq = 128,
763 .irq_handler = s3c24xx_rtc_irq,
764 .set_freq = s3c2410_rtc_setfreq,
765 .enable_tick = s3c24xx_rtc_enable_tick,
766 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
767 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
768 .enable = s3c24xx_rtc_enable,
769 .disable = s3c24xx_rtc_disable,
770};
771
772static struct s3c_rtc_data const s3c2416_rtc_data = {
773 .max_user_freq = 32768,
774 .irq_handler = s3c24xx_rtc_irq,
775 .set_freq = s3c2416_rtc_setfreq,
776 .enable_tick = s3c24xx_rtc_enable_tick,
777 .select_tick_clk = s3c2416_rtc_select_tick_clk,
778 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
779 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
780 .enable = s3c24xx_rtc_enable,
781 .disable = s3c24xx_rtc_disable,
782};
783
784static struct s3c_rtc_data const s3c2443_rtc_data = {
785 .max_user_freq = 32768,
786 .irq_handler = s3c24xx_rtc_irq,
787 .set_freq = s3c2443_rtc_setfreq,
788 .enable_tick = s3c24xx_rtc_enable_tick,
789 .select_tick_clk = s3c2416_rtc_select_tick_clk,
790 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
791 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
792 .enable = s3c24xx_rtc_enable,
793 .disable = s3c24xx_rtc_disable,
794};
795
796static struct s3c_rtc_data const s3c6410_rtc_data = {
797 .max_user_freq = 32768,
Javier Martinez Canillas8792f7772015-03-12 16:25:49 -0700798 .needs_src_clk = true,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700799 .irq_handler = s3c6410_rtc_irq,
800 .set_freq = s3c6410_rtc_setfreq,
801 .enable_tick = s3c6410_rtc_enable_tick,
802 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
803 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
804 .enable = s3c24xx_rtc_enable,
805 .disable = s3c6410_rtc_disable,
Tushar Beherac3cba922012-04-12 12:49:14 -0700806};
807
Thomas Abraham39ce4082011-10-24 14:49:04 +0200808static const struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900809 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700810 .compatible = "samsung,s3c2410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200811 .data = &s3c2410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900812 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700813 .compatible = "samsung,s3c2416-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200814 .data = &s3c2416_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900815 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700816 .compatible = "samsung,s3c2443-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200817 .data = &s3c2443_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900818 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700819 .compatible = "samsung,s3c6410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200820 .data = &s3c6410_rtc_data,
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700821 }, {
822 .compatible = "samsung,exynos3250-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200823 .data = &s3c6410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900824 },
Chanwoo Choiae05c952014-10-13 15:52:33 -0700825 { /* sentinel */ },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200826};
827MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700828
829static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700830 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800831 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700832 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700833 .name = "s3c-rtc",
Jingoo Han32e445a2013-04-29 16:19:27 -0700834 .pm = &s3c_rtc_pm_ops,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800835 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700836 },
837};
Axel Lin0c4eae62012-01-10 15:10:48 -0800838module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700839
840MODULE_DESCRIPTION("Samsung S3C RTC Driver");
841MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
842MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700843MODULE_ALIAS("platform:s3c2410-rtc");