blob: fab326ba9ceced1c9b7eea1f594773a02e5f0b0c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Ben Dooks1add6782006-07-01 04:36:26 -07002/* drivers/rtc/rtc-s3c.c
3 *
Atul Dahiyae48add82010-07-20 12:19:14 +05304 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
Ben Dooks1add6782006-07-01 04:36:26 -07007 * Copyright (c) 2004,2006 Simtec Electronics
8 * Ben Dooks, <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
Ben Dooks1add6782006-07-01 04:36:26 -070011 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
12*/
13
14#include <linux/module.h>
15#include <linux/fs.h>
16#include <linux/string.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/interrupt.h>
20#include <linux/rtc.h>
21#include <linux/bcd.h>
22#include <linux/clk.h>
Robert P. J. Day9974b6e2008-02-06 01:38:42 -080023#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Thomas Abraham39ce4082011-10-24 14:49:04 +020025#include <linux/of.h>
Marek Szyprowski64704c92019-01-18 14:28:37 +010026#include <linux/of_device.h>
Sachin Kamatdbd9acb2012-07-30 14:41:48 -070027#include <linux/uaccess.h>
28#include <linux/io.h>
Ben Dooks1add6782006-07-01 04:36:26 -070029
Ben Dooks1add6782006-07-01 04:36:26 -070030#include <asm/irq.h>
Arnd Bergmannb9d7c5d2013-03-05 12:04:37 +010031#include "rtc-s3c.h"
Ben Dooks1add6782006-07-01 04:36:26 -070032
Chanwoo Choi19be09f2014-10-13 15:52:28 -070033struct s3c_rtc {
34 struct device *dev;
35 struct rtc_device *rtc;
Ben Dooks1add6782006-07-01 04:36:26 -070036
Chanwoo Choi19be09f2014-10-13 15:52:28 -070037 void __iomem *base;
38 struct clk *rtc_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070039 struct clk *rtc_src_clk;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010040 bool alarm_enabled;
Ben Dooks1add6782006-07-01 04:36:26 -070041
Krzysztof Kozlowski6b720862017-06-16 21:28:05 +020042 const struct s3c_rtc_data *data;
Ben Dooks1add6782006-07-01 04:36:26 -070043
Chanwoo Choi19be09f2014-10-13 15:52:28 -070044 int irq_alarm;
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010045 spinlock_t alarm_lock;
Chanwoo Choi19be09f2014-10-13 15:52:28 -070046
Chanwoo Choi19be09f2014-10-13 15:52:28 -070047 bool wake_en;
48};
49
Chanwoo Choiae05c952014-10-13 15:52:33 -070050struct s3c_rtc_data {
Chanwoo Choidf9e26d2014-10-13 15:52:35 -070051 bool needs_src_clk;
Chanwoo Choiae05c952014-10-13 15:52:33 -070052
53 void (*irq_handler) (struct s3c_rtc *info, int mask);
Chanwoo Choiae05c952014-10-13 15:52:33 -070054 void (*enable) (struct s3c_rtc *info);
55 void (*disable) (struct s3c_rtc *info);
56};
57
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020058static int s3c_rtc_enable_clk(struct s3c_rtc *info)
Donggeun Kim88cee8f2011-09-14 16:22:19 -070059{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010060 int ret;
Donggeun Kim88cee8f2011-09-14 16:22:19 -070061
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010062 ret = clk_enable(info->rtc_clk);
63 if (ret)
64 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020065
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010066 if (info->data->needs_src_clk) {
67 ret = clk_enable(info->rtc_src_clk);
68 if (ret) {
69 clk_disable(info->rtc_clk);
70 return ret;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +020071 }
Joonyoung Shim1fb1c352015-08-12 19:21:46 +090072 }
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010073 return 0;
Chanwoo Choi24e14552015-04-16 12:45:15 -070074}
75
76static void s3c_rtc_disable_clk(struct s3c_rtc *info)
77{
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010078 if (info->data->needs_src_clk)
79 clk_disable(info->rtc_src_clk);
80 clk_disable(info->rtc_clk);
Donggeun Kim88cee8f2011-09-14 16:22:19 -070081}
82
Marek Szyprowskice9af8932020-12-02 12:13:18 +010083/* IRQ Handler */
Chanwoo Choiae05c952014-10-13 15:52:33 -070084static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
85{
86 struct s3c_rtc *info = (struct s3c_rtc *)id;
87
88 if (info->data->irq_handler)
89 info->data->irq_handler(info, S3C2410_INTP_ALM);
Chanwoo Choi19be09f2014-10-13 15:52:28 -070090
Ben Dooks1add6782006-07-01 04:36:26 -070091 return IRQ_HANDLED;
92}
93
94/* Update control registers */
Axel Lin2ec38a02011-03-04 17:36:19 -080095static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
Ben Dooks1add6782006-07-01 04:36:26 -070096{
Chanwoo Choi19be09f2014-10-13 15:52:28 -070097 struct s3c_rtc *info = dev_get_drvdata(dev);
Marek Szyprowski5a5b6142019-01-21 12:09:30 +010098 unsigned long flags;
Ben Dooks1add6782006-07-01 04:36:26 -070099 unsigned int tmp;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200100 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700101
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700102 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
Ben Dooks1add6782006-07-01 04:36:26 -0700103
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200104 ret = s3c_rtc_enable_clk(info);
105 if (ret)
106 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700107
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700108 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
Ben Dooks1add6782006-07-01 04:36:26 -0700109
Axel Lin2ec38a02011-03-04 17:36:19 -0800110 if (enabled)
Ben Dooks1add6782006-07-01 04:36:26 -0700111 tmp |= S3C2410_RTCALM_ALMEN;
112
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700113 writeb(tmp, info->base + S3C2410_RTCALM);
Axel Lin2ec38a02011-03-04 17:36:19 -0800114
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100115 spin_lock_irqsave(&info->alarm_lock, flags);
116
117 if (info->alarm_enabled && !enabled)
118 s3c_rtc_disable_clk(info);
119 else if (!info->alarm_enabled && enabled)
120 ret = s3c_rtc_enable_clk(info);
121
122 info->alarm_enabled = enabled;
123 spin_unlock_irqrestore(&info->alarm_lock, flags);
124
Chanwoo Choi24e14552015-04-16 12:45:15 -0700125 s3c_rtc_disable_clk(info);
Donggeun Kim88cee8f2011-09-14 16:22:19 -0700126
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100127 return ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700128}
129
Ben Dooks1add6782006-07-01 04:36:26 -0700130/* Time read/write */
Ben Dooks1add6782006-07-01 04:36:26 -0700131static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
132{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700133 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700134 unsigned int have_retried = 0;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200135 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700136
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200137 ret = s3c_rtc_enable_clk(info);
138 if (ret)
139 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700140
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200141retry_get_time:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700142 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
143 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
144 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
145 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
146 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
147 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700148
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400149 /* the only way to work out whether the system was mid-update
Ben Dooks1add6782006-07-01 04:36:26 -0700150 * when we read it is to check the second counter, and if it
151 * is zero, then we re-try the entire read
152 */
153
154 if (rtc_tm->tm_sec == 0 && !have_retried) {
155 have_retried = 1;
156 goto retry_get_time;
157 }
158
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700159 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
160 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
161 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
162 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
163 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
164 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700165
Chanwoo Choi24e14552015-04-16 12:45:15 -0700166 s3c_rtc_disable_clk(info);
167
Ben Dooks1add6782006-07-01 04:36:26 -0700168 rtc_tm->tm_year += 100;
169 rtc_tm->tm_mon -= 1;
170
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200171 dev_dbg(dev, "read time %ptR\n", rtc_tm);
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100172 return 0;
Ben Dooks1add6782006-07-01 04:36:26 -0700173}
174
175static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
176{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700177 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks641741e2006-08-27 01:23:27 -0700178 int year = tm->tm_year - 100;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200179 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700180
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200181 dev_dbg(dev, "set time %ptR\n", tm);
Ben Dooks9a654512006-08-27 01:23:22 -0700182
Ben Dooks641741e2006-08-27 01:23:27 -0700183 /* we get around y2k by simply not supporting it */
184
185 if (year < 0 || year >= 100) {
186 dev_err(dev, "rtc only supports 100 years\n");
187 return -EINVAL;
188 }
189
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200190 ret = s3c_rtc_enable_clk(info);
191 if (ret)
192 return ret;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700193
194 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
195 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
196 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
197 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
198 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
199 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
200
Chanwoo Choi24e14552015-04-16 12:45:15 -0700201 s3c_rtc_disable_clk(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700202
203 return 0;
204}
205
206static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
207{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700208 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700209 struct rtc_time *alm_tm = &alrm->time;
210 unsigned int alm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200211 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700212
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200213 ret = s3c_rtc_enable_clk(info);
214 if (ret)
215 return ret;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700216
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700217 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
218 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
219 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
220 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
221 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
222 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
Ben Dooks1add6782006-07-01 04:36:26 -0700223
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700224 alm_en = readb(info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700225
Chanwoo Choi24e14552015-04-16 12:45:15 -0700226 s3c_rtc_disable_clk(info);
227
David Brownella2db8df2006-12-13 00:35:08 -0800228 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
229
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200230 dev_dbg(dev, "read alarm %d, %ptR\n", alm_en, alm_tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700231
Ben Dooks1add6782006-07-01 04:36:26 -0700232 /* decode the alarm enable field */
Ben Dooks1add6782006-07-01 04:36:26 -0700233 if (alm_en & S3C2410_RTCALM_SECEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700234 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
Ben Dooks1add6782006-07-01 04:36:26 -0700235
236 if (alm_en & S3C2410_RTCALM_MINEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700237 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
Ben Dooks1add6782006-07-01 04:36:26 -0700238
239 if (alm_en & S3C2410_RTCALM_HOUREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700240 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
Ben Dooks1add6782006-07-01 04:36:26 -0700241
242 if (alm_en & S3C2410_RTCALM_DAYEN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700243 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
Ben Dooks1add6782006-07-01 04:36:26 -0700244
245 if (alm_en & S3C2410_RTCALM_MONEN) {
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700246 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
Ben Dooks1add6782006-07-01 04:36:26 -0700247 alm_tm->tm_mon -= 1;
Ben Dooks1add6782006-07-01 04:36:26 -0700248 }
249
250 if (alm_en & S3C2410_RTCALM_YEAREN)
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700251 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
Ben Dooks1add6782006-07-01 04:36:26 -0700252
253 return 0;
254}
255
256static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
257{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700258 struct s3c_rtc *info = dev_get_drvdata(dev);
Ben Dooks1add6782006-07-01 04:36:26 -0700259 struct rtc_time *tm = &alrm->time;
260 unsigned int alrm_en;
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200261 int ret;
Ben Dooks1add6782006-07-01 04:36:26 -0700262
Andy Shevchenko9a1bacf2018-12-04 23:23:25 +0200263 dev_dbg(dev, "s3c_rtc_setalarm: %d, %ptR\n", alrm->enabled, tm);
Ben Dooks1add6782006-07-01 04:36:26 -0700264
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200265 ret = s3c_rtc_enable_clk(info);
266 if (ret)
267 return ret;
Chanwoo Choi24e14552015-04-16 12:45:15 -0700268
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700269 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
270 writeb(0x00, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700271
272 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
273 alrm_en |= S3C2410_RTCALM_SECEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700274 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
Ben Dooks1add6782006-07-01 04:36:26 -0700275 }
276
277 if (tm->tm_min < 60 && tm->tm_min >= 0) {
278 alrm_en |= S3C2410_RTCALM_MINEN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700279 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
Ben Dooks1add6782006-07-01 04:36:26 -0700280 }
281
282 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
283 alrm_en |= S3C2410_RTCALM_HOUREN;
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700284 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
Ben Dooks1add6782006-07-01 04:36:26 -0700285 }
286
Krzysztof Kozlowskifb4ac3c2015-11-01 20:49:04 +0900287 if (tm->tm_mon < 12 && tm->tm_mon >= 0) {
288 alrm_en |= S3C2410_RTCALM_MONEN;
289 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON);
290 }
291
292 if (tm->tm_mday <= 31 && tm->tm_mday >= 1) {
293 alrm_en |= S3C2410_RTCALM_DAYEN;
294 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE);
295 }
296
Jingoo Hand4a48c22013-02-21 16:45:06 -0800297 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
Ben Dooks1add6782006-07-01 04:36:26 -0700298
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700299 writeb(alrm_en, info->base + S3C2410_RTCALM);
Ben Dooks1add6782006-07-01 04:36:26 -0700300
Chanwoo Choi24e14552015-04-16 12:45:15 -0700301 s3c_rtc_setaie(dev, alrm->enabled);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700302
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100303 s3c_rtc_disable_clk(info);
304
Ben Dooks1add6782006-07-01 04:36:26 -0700305 return 0;
306}
307
David Brownellff8371a2006-09-30 23:28:17 -0700308static const struct rtc_class_ops s3c_rtcops = {
Ben Dooks1add6782006-07-01 04:36:26 -0700309 .read_time = s3c_rtc_gettime,
310 .set_time = s3c_rtc_settime,
311 .read_alarm = s3c_rtc_getalarm,
312 .set_alarm = s3c_rtc_setalarm,
Changhwan Youne6eb5242010-10-27 15:33:09 -0700313 .alarm_irq_enable = s3c_rtc_setaie,
Ben Dooks1add6782006-07-01 04:36:26 -0700314};
315
Chanwoo Choiae05c952014-10-13 15:52:33 -0700316static void s3c24xx_rtc_enable(struct s3c_rtc *info)
Ben Dooks1add6782006-07-01 04:36:26 -0700317{
Chanwoo Choid67288d2014-10-13 15:52:31 -0700318 unsigned int con, tmp;
Ben Dooks1add6782006-07-01 04:36:26 -0700319
Chanwoo Choid67288d2014-10-13 15:52:31 -0700320 con = readw(info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700321 /* re-enable the device, and check it is ok */
322 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
323 dev_info(info->dev, "rtc disabled, re-enabling\n");
Ben Dooks1add6782006-07-01 04:36:26 -0700324
Chanwoo Choiae05c952014-10-13 15:52:33 -0700325 tmp = readw(info->base + S3C2410_RTCCON);
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200326 writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700327 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700328
329 if (con & S3C2410_RTCCON_CNTSEL) {
330 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
331
332 tmp = readw(info->base + S3C2410_RTCCON);
333 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200334 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700335 }
336
337 if (con & S3C2410_RTCCON_CLKRST) {
338 dev_info(info->dev, "removing RTCCON_CLKRST\n");
339
340 tmp = readw(info->base + S3C2410_RTCCON);
341 writew(tmp & ~S3C2410_RTCCON_CLKRST,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200342 info->base + S3C2410_RTCCON);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700343 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700344}
345
346static void s3c24xx_rtc_disable(struct s3c_rtc *info)
347{
348 unsigned int con;
349
Chanwoo Choiae05c952014-10-13 15:52:33 -0700350 con = readw(info->base + S3C2410_RTCCON);
351 con &= ~S3C2410_RTCCON_RTCEN;
352 writew(con, info->base + S3C2410_RTCCON);
353
354 con = readb(info->base + S3C2410_TICNT);
355 con &= ~S3C2410_TICNT_ENABLE;
356 writeb(con, info->base + S3C2410_TICNT);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700357}
358
359static void s3c6410_rtc_disable(struct s3c_rtc *info)
360{
361 unsigned int con;
362
Chanwoo Choiae05c952014-10-13 15:52:33 -0700363 con = readw(info->base + S3C2410_RTCCON);
364 con &= ~S3C64XX_RTCCON_TICEN;
365 con &= ~S3C2410_RTCCON_RTCEN;
366 writew(con, info->base + S3C2410_RTCCON);
Ben Dooks1add6782006-07-01 04:36:26 -0700367}
368
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700369static int s3c_rtc_remove(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700370{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700371 struct s3c_rtc *info = platform_get_drvdata(pdev);
Ben Dooks1add6782006-07-01 04:36:26 -0700372
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700373 s3c_rtc_setaie(info->dev, 0);
374
Joonyoung Shim7f23a932015-08-11 20:28:19 +0900375 if (info->data->needs_src_clk)
376 clk_unprepare(info->rtc_src_clk);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700377 clk_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530378
Ben Dooks1add6782006-07-01 04:36:26 -0700379 return 0;
380}
381
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800382static int s3c_rtc_probe(struct platform_device *pdev)
Ben Dooks1add6782006-07-01 04:36:26 -0700383{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700384 struct s3c_rtc *info = NULL;
Changhwan Youne1df9622010-10-27 15:33:10 -0700385 struct rtc_time rtc_tm;
Ben Dooks1add6782006-07-01 04:36:26 -0700386 int ret;
387
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700388 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
389 if (!info)
390 return -ENOMEM;
Ben Dooks1add6782006-07-01 04:36:26 -0700391
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700392 info->dev = &pdev->dev;
Marek Szyprowski64704c92019-01-18 14:28:37 +0100393 info->data = of_device_get_match_data(&pdev->dev);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700394 if (!info->data) {
395 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
396 return -EINVAL;
397 }
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100398 spin_lock_init(&info->alarm_lock);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700399
400 platform_set_drvdata(pdev, info);
401
402 info->irq_alarm = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700403 if (info->irq_alarm < 0)
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700404 return info->irq_alarm;
Ben Dooks1add6782006-07-01 04:36:26 -0700405
Marek Szyprowskice9af8932020-12-02 12:13:18 +0100406 dev_dbg(&pdev->dev, "s3c2410_rtc: alarm irq %d\n", info->irq_alarm);
Ben Dooks1add6782006-07-01 04:36:26 -0700407
408 /* get the memory region */
YueHaibing09ef18b2019-10-06 18:29:20 +0800409 info->base = devm_platform_ioremap_resource(pdev, 0);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700410 if (IS_ERR(info->base))
411 return PTR_ERR(info->base);
Ben Dooks1add6782006-07-01 04:36:26 -0700412
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700413 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
414 if (IS_ERR(info->rtc_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300415 ret = PTR_ERR(info->rtc_clk);
416 if (ret != -EPROBE_DEFER)
417 dev_err(&pdev->dev, "failed to find rtc clock\n");
418 else
419 dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
420 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530421 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200422 ret = clk_prepare_enable(info->rtc_clk);
423 if (ret)
424 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530425
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700426 if (info->data->needs_src_clk) {
427 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
428 if (IS_ERR(info->rtc_src_clk)) {
Krzysztof Kozlowskic52d2702020-08-30 10:09:37 +0200429 ret = dev_err_probe(&pdev->dev, PTR_ERR(info->rtc_src_clk),
430 "failed to find rtc source clock\n");
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200431 goto err_src_clk;
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700432 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200433 ret = clk_prepare_enable(info->rtc_src_clk);
434 if (ret)
435 goto err_src_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700436 }
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700437
Marek Szyprowski31b16d92020-12-02 12:13:17 +0100438 /* disable RTC enable bits potentially set by the bootloader */
439 if (info->data->disable)
440 info->data->disable(info);
441
Ben Dooks1add6782006-07-01 04:36:26 -0700442 /* check to see if everything is setup correctly */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700443 if (info->data->enable)
444 info->data->enable(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700445
Jingoo Hand4a48c22013-02-21 16:45:06 -0800446 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200447 readw(info->base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700448
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700449 device_init_wakeup(&pdev->dev, 1);
450
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700451 /* Check RTC Time */
Krzysztof Kozlowski492da682015-04-16 12:46:08 -0700452 if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
Krzysztof Kozlowski202fe4c2015-04-16 12:45:57 -0700453 rtc_tm.tm_year = 100;
454 rtc_tm.tm_mon = 0;
455 rtc_tm.tm_mday = 1;
456 rtc_tm.tm_hour = 0;
457 rtc_tm.tm_min = 0;
458 rtc_tm.tm_sec = 0;
459
460 s3c_rtc_settime(&pdev->dev, &rtc_tm);
461
462 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
463 }
464
Ben Dooks1add6782006-07-01 04:36:26 -0700465 /* register RTC and exit */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700466 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200467 THIS_MODULE);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700468 if (IS_ERR(info->rtc)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700469 dev_err(&pdev->dev, "cannot attach rtc\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700470 ret = PTR_ERR(info->rtc);
Ben Dooks1add6782006-07-01 04:36:26 -0700471 goto err_nortc;
472 }
473
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700474 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200475 0, "s3c2410-rtc alarm", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700476 if (ret) {
477 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
478 goto err_nortc;
479 }
480
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100481 s3c_rtc_disable_clk(info);
482
Ben Dooks1add6782006-07-01 04:36:26 -0700483 return 0;
484
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200485err_nortc:
Chanwoo Choiae05c952014-10-13 15:52:33 -0700486 if (info->data->disable)
487 info->data->disable(info);
Chanwoo Choi24e14552015-04-16 12:45:15 -0700488
489 if (info->data->needs_src_clk)
490 clk_disable_unprepare(info->rtc_src_clk);
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200491err_src_clk:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700492 clk_disable_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530493
Ben Dooks1add6782006-07-01 04:36:26 -0700494 return ret;
495}
496
Jingoo Han32e445a2013-04-29 16:19:27 -0700497#ifdef CONFIG_PM_SLEEP
Ben Dooks1add6782006-07-01 04:36:26 -0700498
Jingoo Han32e445a2013-04-29 16:19:27 -0700499static int s3c_rtc_suspend(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700500{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700501 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200502 int ret;
Jingoo Han32e445a2013-04-29 16:19:27 -0700503
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200504 ret = s3c_rtc_enable_clk(info);
505 if (ret)
506 return ret;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700507
Chanwoo Choiae05c952014-10-13 15:52:33 -0700508 if (info->data->disable)
509 info->data->disable(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700510
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700511 if (device_may_wakeup(dev) && !info->wake_en) {
512 if (enable_irq_wake(info->irq_alarm) == 0)
513 info->wake_en = true;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700514 else
Jingoo Han32e445a2013-04-29 16:19:27 -0700515 dev_err(dev, "enable_irq_wake failed\n");
Ben Dooks52cd4e52011-05-11 15:13:28 -0700516 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700517
Ben Dooks1add6782006-07-01 04:36:26 -0700518 return 0;
519}
520
Jingoo Han32e445a2013-04-29 16:19:27 -0700521static int s3c_rtc_resume(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700522{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700523 struct s3c_rtc *info = dev_get_drvdata(dev);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700524
Chanwoo Choiae05c952014-10-13 15:52:33 -0700525 if (info->data->enable)
526 info->data->enable(info);
527
Chanwoo Choi24e14552015-04-16 12:45:15 -0700528 s3c_rtc_disable_clk(info);
529
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700530 if (device_may_wakeup(dev) && info->wake_en) {
531 disable_irq_wake(info->irq_alarm);
532 info->wake_en = false;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700533 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700534
Ben Dooks1add6782006-07-01 04:36:26 -0700535 return 0;
536}
Ben Dooks1add6782006-07-01 04:36:26 -0700537#endif
Jingoo Han32e445a2013-04-29 16:19:27 -0700538static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
539
Chanwoo Choiae05c952014-10-13 15:52:33 -0700540static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
541{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700542 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700543}
544
545static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
546{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700547 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
548 writeb(mask, info->base + S3C2410_INTP);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700549}
550
Chanwoo Choiae05c952014-10-13 15:52:33 -0700551static struct s3c_rtc_data const s3c2410_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700552 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700553 .enable = s3c24xx_rtc_enable,
554 .disable = s3c24xx_rtc_disable,
555};
556
557static struct s3c_rtc_data const s3c2416_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700558 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700559 .enable = s3c24xx_rtc_enable,
560 .disable = s3c24xx_rtc_disable,
561};
562
563static struct s3c_rtc_data const s3c2443_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700564 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700565 .enable = s3c24xx_rtc_enable,
566 .disable = s3c24xx_rtc_disable,
567};
568
569static struct s3c_rtc_data const s3c6410_rtc_data = {
Javier Martinez Canillas8792f7772015-03-12 16:25:49 -0700570 .needs_src_clk = true,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700571 .irq_handler = s3c6410_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700572 .enable = s3c24xx_rtc_enable,
573 .disable = s3c6410_rtc_disable,
Tushar Beherac3cba922012-04-12 12:49:14 -0700574};
575
Thomas Abraham39ce4082011-10-24 14:49:04 +0200576static const struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900577 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700578 .compatible = "samsung,s3c2410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200579 .data = &s3c2410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900580 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700581 .compatible = "samsung,s3c2416-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200582 .data = &s3c2416_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900583 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700584 .compatible = "samsung,s3c2443-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200585 .data = &s3c2443_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900586 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700587 .compatible = "samsung,s3c6410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200588 .data = &s3c6410_rtc_data,
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700589 }, {
590 .compatible = "samsung,exynos3250-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200591 .data = &s3c6410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900592 },
Chanwoo Choiae05c952014-10-13 15:52:33 -0700593 { /* sentinel */ },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200594};
595MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700596
597static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700598 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800599 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700600 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700601 .name = "s3c-rtc",
Jingoo Han32e445a2013-04-29 16:19:27 -0700602 .pm = &s3c_rtc_pm_ops,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800603 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700604 },
605};
Axel Lin0c4eae62012-01-10 15:10:48 -0800606module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700607
608MODULE_DESCRIPTION("Samsung S3C RTC Driver");
609MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
610MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700611MODULE_ALIAS("platform:s3c2410-rtc");