blob: e57d3ca70a7828864a53615327908e7e66e8d568 [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;
Ben Dooks1add6782006-07-01 04:36:26 -0700385 int ret;
386
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700387 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
388 if (!info)
389 return -ENOMEM;
Ben Dooks1add6782006-07-01 04:36:26 -0700390
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700391 info->dev = &pdev->dev;
Marek Szyprowski64704c92019-01-18 14:28:37 +0100392 info->data = of_device_get_match_data(&pdev->dev);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700393 if (!info->data) {
394 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
395 return -EINVAL;
396 }
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100397 spin_lock_init(&info->alarm_lock);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700398
399 platform_set_drvdata(pdev, info);
400
401 info->irq_alarm = platform_get_irq(pdev, 0);
Stephen Boydfaac9102019-07-30 11:15:39 -0700402 if (info->irq_alarm < 0)
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700403 return info->irq_alarm;
Ben Dooks1add6782006-07-01 04:36:26 -0700404
Marek Szyprowskice9af8932020-12-02 12:13:18 +0100405 dev_dbg(&pdev->dev, "s3c2410_rtc: alarm irq %d\n", info->irq_alarm);
Ben Dooks1add6782006-07-01 04:36:26 -0700406
407 /* get the memory region */
YueHaibing09ef18b2019-10-06 18:29:20 +0800408 info->base = devm_platform_ioremap_resource(pdev, 0);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700409 if (IS_ERR(info->base))
410 return PTR_ERR(info->base);
Ben Dooks1add6782006-07-01 04:36:26 -0700411
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700412 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
413 if (IS_ERR(info->rtc_clk)) {
Javier Martinez Canillasae6e00b2016-03-14 22:38:38 -0300414 ret = PTR_ERR(info->rtc_clk);
415 if (ret != -EPROBE_DEFER)
416 dev_err(&pdev->dev, "failed to find rtc clock\n");
417 else
418 dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
419 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530420 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200421 ret = clk_prepare_enable(info->rtc_clk);
422 if (ret)
423 return ret;
Atul Dahiyae48add82010-07-20 12:19:14 +0530424
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700425 if (info->data->needs_src_clk) {
426 info->rtc_src_clk = devm_clk_get(&pdev->dev, "rtc_src");
427 if (IS_ERR(info->rtc_src_clk)) {
Krzysztof Kozlowskic52d2702020-08-30 10:09:37 +0200428 ret = dev_err_probe(&pdev->dev, PTR_ERR(info->rtc_src_clk),
429 "failed to find rtc source clock\n");
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200430 goto err_src_clk;
Marek Szyprowskieaf3a652014-10-29 14:50:38 -0700431 }
Krzysztof Kozlowski9903f682017-06-16 21:28:06 +0200432 ret = clk_prepare_enable(info->rtc_src_clk);
433 if (ret)
434 goto err_src_clk;
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700435 }
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700436
Marek Szyprowski31b16d92020-12-02 12:13:17 +0100437 /* disable RTC enable bits potentially set by the bootloader */
438 if (info->data->disable)
439 info->data->disable(info);
440
Ben Dooks1add6782006-07-01 04:36:26 -0700441 /* check to see if everything is setup correctly */
Chanwoo Choiae05c952014-10-13 15:52:33 -0700442 if (info->data->enable)
443 info->data->enable(info);
Ben Dooks1add6782006-07-01 04:36:26 -0700444
Jingoo Hand4a48c22013-02-21 16:45:06 -0800445 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200446 readw(info->base + S3C2410_RTCCON));
Ben Dooks1add6782006-07-01 04:36:26 -0700447
Yauhen Kharuzhy51b76162008-10-29 14:01:16 -0700448 device_init_wakeup(&pdev->dev, 1);
449
Ben Dooks1add6782006-07-01 04:36:26 -0700450 /* register RTC and exit */
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700451 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200452 THIS_MODULE);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700453 if (IS_ERR(info->rtc)) {
Ben Dooks1add6782006-07-01 04:36:26 -0700454 dev_err(&pdev->dev, "cannot attach rtc\n");
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700455 ret = PTR_ERR(info->rtc);
Ben Dooks1add6782006-07-01 04:36:26 -0700456 goto err_nortc;
457 }
458
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700459 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200460 0, "s3c2410-rtc alarm", info);
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700461 if (ret) {
462 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
463 goto err_nortc;
464 }
465
Marek Szyprowski5a5b6142019-01-21 12:09:30 +0100466 s3c_rtc_disable_clk(info);
467
Ben Dooks1add6782006-07-01 04:36:26 -0700468 return 0;
469
Krzysztof Kozlowskifc1afe62017-06-16 21:28:03 +0200470err_nortc:
Chanwoo Choiae05c952014-10-13 15:52:33 -0700471 if (info->data->disable)
472 info->data->disable(info);
Chanwoo Choi24e14552015-04-16 12:45:15 -0700473
474 if (info->data->needs_src_clk)
475 clk_disable_unprepare(info->rtc_src_clk);
Krzysztof Kozlowski8768e7b2017-06-16 21:28:02 +0200476err_src_clk:
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700477 clk_disable_unprepare(info->rtc_clk);
Atul Dahiyae48add82010-07-20 12:19:14 +0530478
Ben Dooks1add6782006-07-01 04:36:26 -0700479 return ret;
480}
481
Jingoo Han32e445a2013-04-29 16:19:27 -0700482#ifdef CONFIG_PM_SLEEP
Ben Dooks1add6782006-07-01 04:36:26 -0700483
Jingoo Han32e445a2013-04-29 16:19:27 -0700484static int s3c_rtc_suspend(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700485{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700486 struct s3c_rtc *info = dev_get_drvdata(dev);
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200487 int ret;
Jingoo Han32e445a2013-04-29 16:19:27 -0700488
Krzysztof Kozlowski498bcf32017-06-16 21:28:07 +0200489 ret = s3c_rtc_enable_clk(info);
490 if (ret)
491 return ret;
Chanwoo Choiae05c952014-10-13 15:52:33 -0700492
Chanwoo Choiae05c952014-10-13 15:52:33 -0700493 if (info->data->disable)
494 info->data->disable(info);
Vladimir Zapolskiyf501ed52010-09-22 13:05:13 -0700495
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700496 if (device_may_wakeup(dev) && !info->wake_en) {
497 if (enable_irq_wake(info->irq_alarm) == 0)
498 info->wake_en = true;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700499 else
Jingoo Han32e445a2013-04-29 16:19:27 -0700500 dev_err(dev, "enable_irq_wake failed\n");
Ben Dooks52cd4e52011-05-11 15:13:28 -0700501 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700502
Ben Dooks1add6782006-07-01 04:36:26 -0700503 return 0;
504}
505
Jingoo Han32e445a2013-04-29 16:19:27 -0700506static int s3c_rtc_resume(struct device *dev)
Ben Dooks1add6782006-07-01 04:36:26 -0700507{
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700508 struct s3c_rtc *info = dev_get_drvdata(dev);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700509
Chanwoo Choiae05c952014-10-13 15:52:33 -0700510 if (info->data->enable)
511 info->data->enable(info);
512
Chanwoo Choi24e14552015-04-16 12:45:15 -0700513 s3c_rtc_disable_clk(info);
514
Chanwoo Choi19be09f2014-10-13 15:52:28 -0700515 if (device_may_wakeup(dev) && info->wake_en) {
516 disable_irq_wake(info->irq_alarm);
517 info->wake_en = false;
Ben Dooks52cd4e52011-05-11 15:13:28 -0700518 }
Chanwoo Choiae05c952014-10-13 15:52:33 -0700519
Ben Dooks1add6782006-07-01 04:36:26 -0700520 return 0;
521}
Ben Dooks1add6782006-07-01 04:36:26 -0700522#endif
Jingoo Han32e445a2013-04-29 16:19:27 -0700523static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
524
Chanwoo Choiae05c952014-10-13 15:52:33 -0700525static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
526{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700527 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700528}
529
530static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
531{
Chanwoo Choiae05c952014-10-13 15:52:33 -0700532 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
533 writeb(mask, info->base + S3C2410_INTP);
Chanwoo Choiae05c952014-10-13 15:52:33 -0700534}
535
Chanwoo Choiae05c952014-10-13 15:52:33 -0700536static struct s3c_rtc_data const s3c2410_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700537 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700538 .enable = s3c24xx_rtc_enable,
539 .disable = s3c24xx_rtc_disable,
540};
541
542static struct s3c_rtc_data const s3c2416_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700543 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700544 .enable = s3c24xx_rtc_enable,
545 .disable = s3c24xx_rtc_disable,
546};
547
548static struct s3c_rtc_data const s3c2443_rtc_data = {
Chanwoo Choiae05c952014-10-13 15:52:33 -0700549 .irq_handler = s3c24xx_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700550 .enable = s3c24xx_rtc_enable,
551 .disable = s3c24xx_rtc_disable,
552};
553
554static struct s3c_rtc_data const s3c6410_rtc_data = {
Javier Martinez Canillas8792f7772015-03-12 16:25:49 -0700555 .needs_src_clk = true,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700556 .irq_handler = s3c6410_rtc_irq,
Chanwoo Choiae05c952014-10-13 15:52:33 -0700557 .enable = s3c24xx_rtc_enable,
558 .disable = s3c6410_rtc_disable,
Tushar Beherac3cba922012-04-12 12:49:14 -0700559};
560
Alexandre Belloni7d6bec22021-02-02 12:29:34 +0100561static const __maybe_unused struct of_device_id s3c_rtc_dt_match[] = {
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900562 {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700563 .compatible = "samsung,s3c2410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200564 .data = &s3c2410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900565 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700566 .compatible = "samsung,s3c2416-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200567 .data = &s3c2416_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900568 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700569 .compatible = "samsung,s3c2443-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200570 .data = &s3c2443_rtc_data,
Heiko Stuebner25c1a242011-12-24 10:52:19 +0900571 }, {
Tushar Beheracd1e6f92012-04-12 12:49:14 -0700572 .compatible = "samsung,s3c6410-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200573 .data = &s3c6410_rtc_data,
Chanwoo Choidf9e26d2014-10-13 15:52:35 -0700574 }, {
575 .compatible = "samsung,exynos3250-rtc",
Krzysztof Kozlowski21df6fe2017-06-16 21:28:04 +0200576 .data = &s3c6410_rtc_data,
Heiko Stuebnerd2524ca2011-12-24 10:52:14 +0900577 },
Chanwoo Choiae05c952014-10-13 15:52:33 -0700578 { /* sentinel */ },
Thomas Abraham39ce4082011-10-24 14:49:04 +0200579};
580MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700581
582static struct platform_driver s3c_rtc_driver = {
Ben Dooks1add6782006-07-01 04:36:26 -0700583 .probe = s3c_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800584 .remove = s3c_rtc_remove,
Ben Dooks1add6782006-07-01 04:36:26 -0700585 .driver = {
Maurus Cuelenaere9f4123b2010-05-24 14:33:43 -0700586 .name = "s3c-rtc",
Jingoo Han32e445a2013-04-29 16:19:27 -0700587 .pm = &s3c_rtc_pm_ops,
Sachin Kamat04a373f2012-12-17 16:02:52 -0800588 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
Ben Dooks1add6782006-07-01 04:36:26 -0700589 },
590};
Axel Lin0c4eae62012-01-10 15:10:48 -0800591module_platform_driver(s3c_rtc_driver);
Ben Dooks1add6782006-07-01 04:36:26 -0700592
593MODULE_DESCRIPTION("Samsung S3C RTC Driver");
594MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
595MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700596MODULE_ALIAS("platform:s3c2410-rtc");