1 /* drivers/rtc/rtc-s3c.c
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
6 * Copyright (c) 2004,2006 Simtec Electronics
7 * Ben Dooks, <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
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.
14 * S3C2410/S3C2440/S3C24XX Internal RTC Driver
17 #include <linux/module.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>
26 #include <linux/log2.h>
27 #include <linux/slab.h>
29 #include <linux/uaccess.h>
37 struct rtc_device
*rtc
;
41 struct clk
*rtc_src_clk
;
44 struct s3c_rtc_data
*data
;
50 spinlock_t alarm_clk_lock
;
52 int ticnt_save
, ticnt_en_save
;
60 void (*irq_handler
) (struct s3c_rtc
*info
, int mask
);
61 void (*set_freq
) (struct s3c_rtc
*info
, int freq
);
62 void (*enable_tick
) (struct s3c_rtc
*info
, struct seq_file
*seq
);
63 void (*select_tick_clk
) (struct s3c_rtc
*info
);
64 void (*save_tick_cnt
) (struct s3c_rtc
*info
);
65 void (*restore_tick_cnt
) (struct s3c_rtc
*info
);
66 void (*enable
) (struct s3c_rtc
*info
);
67 void (*disable
) (struct s3c_rtc
*info
);
70 static void s3c_rtc_alarm_clk_enable(struct s3c_rtc
*info
, bool enable
)
72 unsigned long irq_flags
;
74 spin_lock_irqsave(&info
->alarm_clk_lock
, irq_flags
);
77 clk_enable(info
->rtc_clk
);
78 if (info
->data
->needs_src_clk
)
79 clk_enable(info
->rtc_src_clk
);
84 if (info
->data
->needs_src_clk
)
85 clk_disable(info
->rtc_src_clk
);
86 clk_disable(info
->rtc_clk
);
87 info
->enabled
= false;
90 spin_unlock_irqrestore(&info
->alarm_clk_lock
, irq_flags
);
94 static irqreturn_t
s3c_rtc_tickirq(int irq
, void *id
)
96 struct s3c_rtc
*info
= (struct s3c_rtc
*)id
;
98 if (info
->data
->irq_handler
)
99 info
->data
->irq_handler(info
, S3C2410_INTP_TIC
);
104 static irqreturn_t
s3c_rtc_alarmirq(int irq
, void *id
)
106 struct s3c_rtc
*info
= (struct s3c_rtc
*)id
;
108 if (info
->data
->irq_handler
)
109 info
->data
->irq_handler(info
, S3C2410_INTP_ALM
);
114 /* Update control registers */
115 static int s3c_rtc_setaie(struct device
*dev
, unsigned int enabled
)
117 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
120 dev_dbg(info
->dev
, "%s: aie=%d\n", __func__
, enabled
);
122 clk_enable(info
->rtc_clk
);
123 if (info
->data
->needs_src_clk
)
124 clk_enable(info
->rtc_src_clk
);
125 tmp
= readb(info
->base
+ S3C2410_RTCALM
) & ~S3C2410_RTCALM_ALMEN
;
128 tmp
|= S3C2410_RTCALM_ALMEN
;
130 writeb(tmp
, info
->base
+ S3C2410_RTCALM
);
131 if (info
->data
->needs_src_clk
)
132 clk_disable(info
->rtc_src_clk
);
133 clk_disable(info
->rtc_clk
);
135 s3c_rtc_alarm_clk_enable(info
, enabled
);
140 /* Set RTC frequency */
141 static int s3c_rtc_setfreq(struct s3c_rtc
*info
, int freq
)
143 if (!is_power_of_2(freq
))
146 clk_enable(info
->rtc_clk
);
147 if (info
->data
->needs_src_clk
)
148 clk_enable(info
->rtc_src_clk
);
149 spin_lock_irq(&info
->pie_lock
);
151 if (info
->data
->set_freq
)
152 info
->data
->set_freq(info
, freq
);
154 spin_unlock_irq(&info
->pie_lock
);
155 if (info
->data
->needs_src_clk
)
156 clk_disable(info
->rtc_src_clk
);
157 clk_disable(info
->rtc_clk
);
162 /* Time read/write */
163 static int s3c_rtc_gettime(struct device
*dev
, struct rtc_time
*rtc_tm
)
165 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
166 unsigned int have_retried
= 0;
168 clk_enable(info
->rtc_clk
);
169 if (info
->data
->needs_src_clk
)
170 clk_enable(info
->rtc_src_clk
);
173 rtc_tm
->tm_min
= readb(info
->base
+ S3C2410_RTCMIN
);
174 rtc_tm
->tm_hour
= readb(info
->base
+ S3C2410_RTCHOUR
);
175 rtc_tm
->tm_mday
= readb(info
->base
+ S3C2410_RTCDATE
);
176 rtc_tm
->tm_mon
= readb(info
->base
+ S3C2410_RTCMON
);
177 rtc_tm
->tm_year
= readb(info
->base
+ S3C2410_RTCYEAR
);
178 rtc_tm
->tm_sec
= readb(info
->base
+ S3C2410_RTCSEC
);
180 /* the only way to work out whether the system was mid-update
181 * when we read it is to check the second counter, and if it
182 * is zero, then we re-try the entire read
185 if (rtc_tm
->tm_sec
== 0 && !have_retried
) {
190 rtc_tm
->tm_sec
= bcd2bin(rtc_tm
->tm_sec
);
191 rtc_tm
->tm_min
= bcd2bin(rtc_tm
->tm_min
);
192 rtc_tm
->tm_hour
= bcd2bin(rtc_tm
->tm_hour
);
193 rtc_tm
->tm_mday
= bcd2bin(rtc_tm
->tm_mday
);
194 rtc_tm
->tm_mon
= bcd2bin(rtc_tm
->tm_mon
);
195 rtc_tm
->tm_year
= bcd2bin(rtc_tm
->tm_year
);
197 rtc_tm
->tm_year
+= 100;
199 dev_dbg(dev
, "read time %04d.%02d.%02d %02d:%02d:%02d\n",
200 1900 + rtc_tm
->tm_year
, rtc_tm
->tm_mon
, rtc_tm
->tm_mday
,
201 rtc_tm
->tm_hour
, rtc_tm
->tm_min
, rtc_tm
->tm_sec
);
205 if (info
->data
->needs_src_clk
)
206 clk_disable(info
->rtc_src_clk
);
207 clk_disable(info
->rtc_clk
);
209 return rtc_valid_tm(rtc_tm
);
212 static int s3c_rtc_settime(struct device
*dev
, struct rtc_time
*tm
)
214 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
215 int year
= tm
->tm_year
- 100;
217 dev_dbg(dev
, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
218 1900 + tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
219 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
221 /* we get around y2k by simply not supporting it */
223 if (year
< 0 || year
>= 100) {
224 dev_err(dev
, "rtc only supports 100 years\n");
228 clk_enable(info
->rtc_clk
);
229 if (info
->data
->needs_src_clk
)
230 clk_enable(info
->rtc_src_clk
);
232 writeb(bin2bcd(tm
->tm_sec
), info
->base
+ S3C2410_RTCSEC
);
233 writeb(bin2bcd(tm
->tm_min
), info
->base
+ S3C2410_RTCMIN
);
234 writeb(bin2bcd(tm
->tm_hour
), info
->base
+ S3C2410_RTCHOUR
);
235 writeb(bin2bcd(tm
->tm_mday
), info
->base
+ S3C2410_RTCDATE
);
236 writeb(bin2bcd(tm
->tm_mon
+ 1), info
->base
+ S3C2410_RTCMON
);
237 writeb(bin2bcd(year
), info
->base
+ S3C2410_RTCYEAR
);
239 if (info
->data
->needs_src_clk
)
240 clk_disable(info
->rtc_src_clk
);
241 clk_disable(info
->rtc_clk
);
246 static int s3c_rtc_getalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
248 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
249 struct rtc_time
*alm_tm
= &alrm
->time
;
252 clk_enable(info
->rtc_clk
);
253 if (info
->data
->needs_src_clk
)
254 clk_enable(info
->rtc_src_clk
);
256 alm_tm
->tm_sec
= readb(info
->base
+ S3C2410_ALMSEC
);
257 alm_tm
->tm_min
= readb(info
->base
+ S3C2410_ALMMIN
);
258 alm_tm
->tm_hour
= readb(info
->base
+ S3C2410_ALMHOUR
);
259 alm_tm
->tm_mon
= readb(info
->base
+ S3C2410_ALMMON
);
260 alm_tm
->tm_mday
= readb(info
->base
+ S3C2410_ALMDATE
);
261 alm_tm
->tm_year
= readb(info
->base
+ S3C2410_ALMYEAR
);
263 alm_en
= readb(info
->base
+ S3C2410_RTCALM
);
265 alrm
->enabled
= (alm_en
& S3C2410_RTCALM_ALMEN
) ? 1 : 0;
267 dev_dbg(dev
, "read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
269 1900 + alm_tm
->tm_year
, alm_tm
->tm_mon
, alm_tm
->tm_mday
,
270 alm_tm
->tm_hour
, alm_tm
->tm_min
, alm_tm
->tm_sec
);
273 /* decode the alarm enable field */
275 if (alm_en
& S3C2410_RTCALM_SECEN
)
276 alm_tm
->tm_sec
= bcd2bin(alm_tm
->tm_sec
);
280 if (alm_en
& S3C2410_RTCALM_MINEN
)
281 alm_tm
->tm_min
= bcd2bin(alm_tm
->tm_min
);
285 if (alm_en
& S3C2410_RTCALM_HOUREN
)
286 alm_tm
->tm_hour
= bcd2bin(alm_tm
->tm_hour
);
288 alm_tm
->tm_hour
= -1;
290 if (alm_en
& S3C2410_RTCALM_DAYEN
)
291 alm_tm
->tm_mday
= bcd2bin(alm_tm
->tm_mday
);
293 alm_tm
->tm_mday
= -1;
295 if (alm_en
& S3C2410_RTCALM_MONEN
) {
296 alm_tm
->tm_mon
= bcd2bin(alm_tm
->tm_mon
);
302 if (alm_en
& S3C2410_RTCALM_YEAREN
)
303 alm_tm
->tm_year
= bcd2bin(alm_tm
->tm_year
);
305 alm_tm
->tm_year
= -1;
307 if (info
->data
->needs_src_clk
)
308 clk_disable(info
->rtc_src_clk
);
309 clk_disable(info
->rtc_clk
);
314 static int s3c_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
316 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
317 struct rtc_time
*tm
= &alrm
->time
;
318 unsigned int alrm_en
;
320 clk_enable(info
->rtc_clk
);
321 if (info
->data
->needs_src_clk
)
322 clk_enable(info
->rtc_src_clk
);
324 dev_dbg(dev
, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
326 1900 + tm
->tm_year
, tm
->tm_mon
+ 1, tm
->tm_mday
,
327 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
329 alrm_en
= readb(info
->base
+ S3C2410_RTCALM
) & S3C2410_RTCALM_ALMEN
;
330 writeb(0x00, info
->base
+ S3C2410_RTCALM
);
332 if (tm
->tm_sec
< 60 && tm
->tm_sec
>= 0) {
333 alrm_en
|= S3C2410_RTCALM_SECEN
;
334 writeb(bin2bcd(tm
->tm_sec
), info
->base
+ S3C2410_ALMSEC
);
337 if (tm
->tm_min
< 60 && tm
->tm_min
>= 0) {
338 alrm_en
|= S3C2410_RTCALM_MINEN
;
339 writeb(bin2bcd(tm
->tm_min
), info
->base
+ S3C2410_ALMMIN
);
342 if (tm
->tm_hour
< 24 && tm
->tm_hour
>= 0) {
343 alrm_en
|= S3C2410_RTCALM_HOUREN
;
344 writeb(bin2bcd(tm
->tm_hour
), info
->base
+ S3C2410_ALMHOUR
);
347 dev_dbg(dev
, "setting S3C2410_RTCALM to %08x\n", alrm_en
);
349 writeb(alrm_en
, info
->base
+ S3C2410_RTCALM
);
351 s3c_rtc_setaie(dev
, alrm
->enabled
);
353 if (info
->data
->needs_src_clk
)
354 clk_disable(info
->rtc_src_clk
);
355 clk_disable(info
->rtc_clk
);
360 static int s3c_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
362 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
364 clk_enable(info
->rtc_clk
);
365 if (info
->data
->needs_src_clk
)
366 clk_enable(info
->rtc_src_clk
);
368 if (info
->data
->enable_tick
)
369 info
->data
->enable_tick(info
, seq
);
371 if (info
->data
->needs_src_clk
)
372 clk_disable(info
->rtc_src_clk
);
373 clk_disable(info
->rtc_clk
);
378 static const struct rtc_class_ops s3c_rtcops
= {
379 .read_time
= s3c_rtc_gettime
,
380 .set_time
= s3c_rtc_settime
,
381 .read_alarm
= s3c_rtc_getalarm
,
382 .set_alarm
= s3c_rtc_setalarm
,
383 .proc
= s3c_rtc_proc
,
384 .alarm_irq_enable
= s3c_rtc_setaie
,
387 static void s3c24xx_rtc_enable(struct s3c_rtc
*info
)
389 unsigned int con
, tmp
;
391 clk_enable(info
->rtc_clk
);
392 if (info
->data
->needs_src_clk
)
393 clk_enable(info
->rtc_src_clk
);
395 con
= readw(info
->base
+ S3C2410_RTCCON
);
396 /* re-enable the device, and check it is ok */
397 if ((con
& S3C2410_RTCCON_RTCEN
) == 0) {
398 dev_info(info
->dev
, "rtc disabled, re-enabling\n");
400 tmp
= readw(info
->base
+ S3C2410_RTCCON
);
401 writew(tmp
| S3C2410_RTCCON_RTCEN
,
402 info
->base
+ S3C2410_RTCCON
);
405 if (con
& S3C2410_RTCCON_CNTSEL
) {
406 dev_info(info
->dev
, "removing RTCCON_CNTSEL\n");
408 tmp
= readw(info
->base
+ S3C2410_RTCCON
);
409 writew(tmp
& ~S3C2410_RTCCON_CNTSEL
,
410 info
->base
+ S3C2410_RTCCON
);
413 if (con
& S3C2410_RTCCON_CLKRST
) {
414 dev_info(info
->dev
, "removing RTCCON_CLKRST\n");
416 tmp
= readw(info
->base
+ S3C2410_RTCCON
);
417 writew(tmp
& ~S3C2410_RTCCON_CLKRST
,
418 info
->base
+ S3C2410_RTCCON
);
421 if (info
->data
->needs_src_clk
)
422 clk_disable(info
->rtc_src_clk
);
423 clk_disable(info
->rtc_clk
);
426 static void s3c24xx_rtc_disable(struct s3c_rtc
*info
)
430 clk_enable(info
->rtc_clk
);
431 if (info
->data
->needs_src_clk
)
432 clk_enable(info
->rtc_src_clk
);
434 con
= readw(info
->base
+ S3C2410_RTCCON
);
435 con
&= ~S3C2410_RTCCON_RTCEN
;
436 writew(con
, info
->base
+ S3C2410_RTCCON
);
438 con
= readb(info
->base
+ S3C2410_TICNT
);
439 con
&= ~S3C2410_TICNT_ENABLE
;
440 writeb(con
, info
->base
+ S3C2410_TICNT
);
442 if (info
->data
->needs_src_clk
)
443 clk_disable(info
->rtc_src_clk
);
444 clk_disable(info
->rtc_clk
);
447 static void s3c6410_rtc_disable(struct s3c_rtc
*info
)
451 clk_enable(info
->rtc_clk
);
452 if (info
->data
->needs_src_clk
)
453 clk_enable(info
->rtc_src_clk
);
455 con
= readw(info
->base
+ S3C2410_RTCCON
);
456 con
&= ~S3C64XX_RTCCON_TICEN
;
457 con
&= ~S3C2410_RTCCON_RTCEN
;
458 writew(con
, info
->base
+ S3C2410_RTCCON
);
460 if (info
->data
->needs_src_clk
)
461 clk_disable(info
->rtc_src_clk
);
462 clk_disable(info
->rtc_clk
);
465 static int s3c_rtc_remove(struct platform_device
*pdev
)
467 struct s3c_rtc
*info
= platform_get_drvdata(pdev
);
469 s3c_rtc_setaie(info
->dev
, 0);
471 clk_unprepare(info
->rtc_clk
);
472 info
->rtc_clk
= NULL
;
477 static const struct of_device_id s3c_rtc_dt_match
[];
479 static struct s3c_rtc_data
*s3c_rtc_get_data(struct platform_device
*pdev
)
481 const struct of_device_id
*match
;
483 match
= of_match_node(s3c_rtc_dt_match
, pdev
->dev
.of_node
);
484 return (struct s3c_rtc_data
*)match
->data
;
487 static int s3c_rtc_probe(struct platform_device
*pdev
)
489 struct s3c_rtc
*info
= NULL
;
490 struct rtc_time rtc_tm
;
491 struct resource
*res
;
494 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
499 info
->irq_tick
= platform_get_irq(pdev
, 1);
500 if (info
->irq_tick
< 0) {
501 dev_err(&pdev
->dev
, "no irq for rtc tick\n");
502 return info
->irq_tick
;
505 info
->dev
= &pdev
->dev
;
506 info
->data
= s3c_rtc_get_data(pdev
);
508 dev_err(&pdev
->dev
, "failed getting s3c_rtc_data\n");
511 spin_lock_init(&info
->pie_lock
);
512 spin_lock_init(&info
->alarm_clk_lock
);
514 platform_set_drvdata(pdev
, info
);
516 info
->irq_alarm
= platform_get_irq(pdev
, 0);
517 if (info
->irq_alarm
< 0) {
518 dev_err(&pdev
->dev
, "no irq for alarm\n");
519 return info
->irq_alarm
;
522 dev_dbg(&pdev
->dev
, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
523 info
->irq_tick
, info
->irq_alarm
);
525 /* get the memory region */
526 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
527 info
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
528 if (IS_ERR(info
->base
))
529 return PTR_ERR(info
->base
);
531 info
->rtc_clk
= devm_clk_get(&pdev
->dev
, "rtc");
532 if (IS_ERR(info
->rtc_clk
)) {
533 dev_err(&pdev
->dev
, "failed to find rtc clock\n");
534 return PTR_ERR(info
->rtc_clk
);
536 clk_prepare_enable(info
->rtc_clk
);
538 if (info
->data
->needs_src_clk
) {
539 info
->rtc_src_clk
= devm_clk_get(&pdev
->dev
, "rtc_src");
540 if (IS_ERR(info
->rtc_src_clk
)) {
542 "failed to find rtc source clock\n");
543 return PTR_ERR(info
->rtc_src_clk
);
545 clk_prepare_enable(info
->rtc_src_clk
);
548 /* check to see if everything is setup correctly */
549 if (info
->data
->enable
)
550 info
->data
->enable(info
);
552 dev_dbg(&pdev
->dev
, "s3c2410_rtc: RTCCON=%02x\n",
553 readw(info
->base
+ S3C2410_RTCCON
));
555 device_init_wakeup(&pdev
->dev
, 1);
557 /* register RTC and exit */
558 info
->rtc
= devm_rtc_device_register(&pdev
->dev
, "s3c", &s3c_rtcops
,
560 if (IS_ERR(info
->rtc
)) {
561 dev_err(&pdev
->dev
, "cannot attach rtc\n");
562 ret
= PTR_ERR(info
->rtc
);
566 ret
= devm_request_irq(&pdev
->dev
, info
->irq_alarm
, s3c_rtc_alarmirq
,
567 0, "s3c2410-rtc alarm", info
);
569 dev_err(&pdev
->dev
, "IRQ%d error %d\n", info
->irq_alarm
, ret
);
573 ret
= devm_request_irq(&pdev
->dev
, info
->irq_tick
, s3c_rtc_tickirq
,
574 0, "s3c2410-rtc tick", info
);
576 dev_err(&pdev
->dev
, "IRQ%d error %d\n", info
->irq_tick
, ret
);
581 s3c_rtc_gettime(&pdev
->dev
, &rtc_tm
);
583 if (rtc_valid_tm(&rtc_tm
)) {
584 rtc_tm
.tm_year
= 100;
591 s3c_rtc_settime(&pdev
->dev
, &rtc_tm
);
593 dev_warn(&pdev
->dev
, "warning: invalid RTC value so initializing it\n");
596 if (info
->data
->select_tick_clk
)
597 info
->data
->select_tick_clk(info
);
599 s3c_rtc_setfreq(info
, 1);
601 if (info
->data
->needs_src_clk
)
602 clk_disable(info
->rtc_src_clk
);
603 clk_disable(info
->rtc_clk
);
608 if (info
->data
->disable
)
609 info
->data
->disable(info
);
610 clk_disable_unprepare(info
->rtc_clk
);
615 #ifdef CONFIG_PM_SLEEP
617 static int s3c_rtc_suspend(struct device
*dev
)
619 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
621 clk_enable(info
->rtc_clk
);
622 if (info
->data
->needs_src_clk
)
623 clk_enable(info
->rtc_src_clk
);
625 /* save TICNT for anyone using periodic interrupts */
626 if (info
->data
->save_tick_cnt
)
627 info
->data
->save_tick_cnt(info
);
629 if (info
->data
->disable
)
630 info
->data
->disable(info
);
632 if (device_may_wakeup(dev
) && !info
->wake_en
) {
633 if (enable_irq_wake(info
->irq_alarm
) == 0)
634 info
->wake_en
= true;
636 dev_err(dev
, "enable_irq_wake failed\n");
639 if (info
->data
->needs_src_clk
)
640 clk_disable(info
->rtc_src_clk
);
641 clk_disable(info
->rtc_clk
);
646 static int s3c_rtc_resume(struct device
*dev
)
648 struct s3c_rtc
*info
= dev_get_drvdata(dev
);
650 clk_enable(info
->rtc_clk
);
651 if (info
->data
->needs_src_clk
)
652 clk_enable(info
->rtc_src_clk
);
654 if (info
->data
->enable
)
655 info
->data
->enable(info
);
657 if (info
->data
->restore_tick_cnt
)
658 info
->data
->restore_tick_cnt(info
);
660 if (device_may_wakeup(dev
) && info
->wake_en
) {
661 disable_irq_wake(info
->irq_alarm
);
662 info
->wake_en
= false;
665 if (info
->data
->needs_src_clk
)
666 clk_disable(info
->rtc_src_clk
);
667 clk_disable(info
->rtc_clk
);
672 static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops
, s3c_rtc_suspend
, s3c_rtc_resume
);
674 static void s3c24xx_rtc_irq(struct s3c_rtc
*info
, int mask
)
676 clk_enable(info
->rtc_clk
);
677 if (info
->data
->needs_src_clk
)
678 clk_enable(info
->rtc_src_clk
);
679 rtc_update_irq(info
->rtc
, 1, RTC_AF
| RTC_IRQF
);
680 if (info
->data
->needs_src_clk
)
681 clk_disable(info
->rtc_src_clk
);
682 clk_disable(info
->rtc_clk
);
684 s3c_rtc_alarm_clk_enable(info
, false);
687 static void s3c6410_rtc_irq(struct s3c_rtc
*info
, int mask
)
689 clk_enable(info
->rtc_clk
);
690 if (info
->data
->needs_src_clk
)
691 clk_enable(info
->rtc_src_clk
);
692 rtc_update_irq(info
->rtc
, 1, RTC_AF
| RTC_IRQF
);
693 writeb(mask
, info
->base
+ S3C2410_INTP
);
694 if (info
->data
->needs_src_clk
)
695 clk_disable(info
->rtc_src_clk
);
696 clk_disable(info
->rtc_clk
);
698 s3c_rtc_alarm_clk_enable(info
, false);
701 static void s3c2410_rtc_setfreq(struct s3c_rtc
*info
, int freq
)
703 unsigned int tmp
= 0;
706 tmp
= readb(info
->base
+ S3C2410_TICNT
);
707 tmp
&= S3C2410_TICNT_ENABLE
;
709 val
= (info
->rtc
->max_user_freq
/ freq
) - 1;
712 writel(tmp
, info
->base
+ S3C2410_TICNT
);
715 static void s3c2416_rtc_setfreq(struct s3c_rtc
*info
, int freq
)
717 unsigned int tmp
= 0;
720 tmp
= readb(info
->base
+ S3C2410_TICNT
);
721 tmp
&= S3C2410_TICNT_ENABLE
;
723 val
= (info
->rtc
->max_user_freq
/ freq
) - 1;
725 tmp
|= S3C2443_TICNT_PART(val
);
726 writel(S3C2443_TICNT1_PART(val
), info
->base
+ S3C2443_TICNT1
);
728 writel(S3C2416_TICNT2_PART(val
), info
->base
+ S3C2416_TICNT2
);
730 writel(tmp
, info
->base
+ S3C2410_TICNT
);
733 static void s3c2443_rtc_setfreq(struct s3c_rtc
*info
, int freq
)
735 unsigned int tmp
= 0;
738 tmp
= readb(info
->base
+ S3C2410_TICNT
);
739 tmp
&= S3C2410_TICNT_ENABLE
;
741 val
= (info
->rtc
->max_user_freq
/ freq
) - 1;
743 tmp
|= S3C2443_TICNT_PART(val
);
744 writel(S3C2443_TICNT1_PART(val
), info
->base
+ S3C2443_TICNT1
);
746 writel(tmp
, info
->base
+ S3C2410_TICNT
);
749 static void s3c6410_rtc_setfreq(struct s3c_rtc
*info
, int freq
)
753 val
= (info
->rtc
->max_user_freq
/ freq
) - 1;
754 writel(val
, info
->base
+ S3C2410_TICNT
);
757 static void s3c24xx_rtc_enable_tick(struct s3c_rtc
*info
, struct seq_file
*seq
)
761 ticnt
= readb(info
->base
+ S3C2410_TICNT
);
762 ticnt
&= S3C2410_TICNT_ENABLE
;
764 seq_printf(seq
, "periodic_IRQ\t: %s\n", ticnt
? "yes" : "no");
767 static void s3c2416_rtc_select_tick_clk(struct s3c_rtc
*info
)
771 con
= readw(info
->base
+ S3C2410_RTCCON
);
772 con
|= S3C2443_RTCCON_TICSEL
;
773 writew(con
, info
->base
+ S3C2410_RTCCON
);
776 static void s3c6410_rtc_enable_tick(struct s3c_rtc
*info
, struct seq_file
*seq
)
780 ticnt
= readw(info
->base
+ S3C2410_RTCCON
);
781 ticnt
&= S3C64XX_RTCCON_TICEN
;
783 seq_printf(seq
, "periodic_IRQ\t: %s\n", ticnt
? "yes" : "no");
786 static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc
*info
)
788 info
->ticnt_save
= readb(info
->base
+ S3C2410_TICNT
);
791 static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc
*info
)
793 writeb(info
->ticnt_save
, info
->base
+ S3C2410_TICNT
);
796 static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc
*info
)
798 info
->ticnt_en_save
= readw(info
->base
+ S3C2410_RTCCON
);
799 info
->ticnt_en_save
&= S3C64XX_RTCCON_TICEN
;
800 info
->ticnt_save
= readl(info
->base
+ S3C2410_TICNT
);
803 static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc
*info
)
807 writel(info
->ticnt_save
, info
->base
+ S3C2410_TICNT
);
808 if (info
->ticnt_en_save
) {
809 con
= readw(info
->base
+ S3C2410_RTCCON
);
810 writew(con
| info
->ticnt_en_save
,
811 info
->base
+ S3C2410_RTCCON
);
815 static struct s3c_rtc_data
const s3c2410_rtc_data
= {
816 .max_user_freq
= 128,
817 .irq_handler
= s3c24xx_rtc_irq
,
818 .set_freq
= s3c2410_rtc_setfreq
,
819 .enable_tick
= s3c24xx_rtc_enable_tick
,
820 .save_tick_cnt
= s3c24xx_rtc_save_tick_cnt
,
821 .restore_tick_cnt
= s3c24xx_rtc_restore_tick_cnt
,
822 .enable
= s3c24xx_rtc_enable
,
823 .disable
= s3c24xx_rtc_disable
,
826 static struct s3c_rtc_data
const s3c2416_rtc_data
= {
827 .max_user_freq
= 32768,
828 .irq_handler
= s3c24xx_rtc_irq
,
829 .set_freq
= s3c2416_rtc_setfreq
,
830 .enable_tick
= s3c24xx_rtc_enable_tick
,
831 .select_tick_clk
= s3c2416_rtc_select_tick_clk
,
832 .save_tick_cnt
= s3c24xx_rtc_save_tick_cnt
,
833 .restore_tick_cnt
= s3c24xx_rtc_restore_tick_cnt
,
834 .enable
= s3c24xx_rtc_enable
,
835 .disable
= s3c24xx_rtc_disable
,
838 static struct s3c_rtc_data
const s3c2443_rtc_data
= {
839 .max_user_freq
= 32768,
840 .irq_handler
= s3c24xx_rtc_irq
,
841 .set_freq
= s3c2443_rtc_setfreq
,
842 .enable_tick
= s3c24xx_rtc_enable_tick
,
843 .select_tick_clk
= s3c2416_rtc_select_tick_clk
,
844 .save_tick_cnt
= s3c24xx_rtc_save_tick_cnt
,
845 .restore_tick_cnt
= s3c24xx_rtc_restore_tick_cnt
,
846 .enable
= s3c24xx_rtc_enable
,
847 .disable
= s3c24xx_rtc_disable
,
850 static struct s3c_rtc_data
const s3c6410_rtc_data
= {
851 .max_user_freq
= 32768,
852 .irq_handler
= s3c6410_rtc_irq
,
853 .set_freq
= s3c6410_rtc_setfreq
,
854 .enable_tick
= s3c6410_rtc_enable_tick
,
855 .save_tick_cnt
= s3c6410_rtc_save_tick_cnt
,
856 .restore_tick_cnt
= s3c6410_rtc_restore_tick_cnt
,
857 .enable
= s3c24xx_rtc_enable
,
858 .disable
= s3c6410_rtc_disable
,
861 static struct s3c_rtc_data
const exynos3250_rtc_data
= {
862 .max_user_freq
= 32768,
863 .needs_src_clk
= true,
864 .irq_handler
= s3c6410_rtc_irq
,
865 .set_freq
= s3c6410_rtc_setfreq
,
866 .enable_tick
= s3c6410_rtc_enable_tick
,
867 .save_tick_cnt
= s3c6410_rtc_save_tick_cnt
,
868 .restore_tick_cnt
= s3c6410_rtc_restore_tick_cnt
,
869 .enable
= s3c24xx_rtc_enable
,
870 .disable
= s3c6410_rtc_disable
,
873 static const struct of_device_id s3c_rtc_dt_match
[] = {
875 .compatible
= "samsung,s3c2410-rtc",
876 .data
= (void *)&s3c2410_rtc_data
,
878 .compatible
= "samsung,s3c2416-rtc",
879 .data
= (void *)&s3c2416_rtc_data
,
881 .compatible
= "samsung,s3c2443-rtc",
882 .data
= (void *)&s3c2443_rtc_data
,
884 .compatible
= "samsung,s3c6410-rtc",
885 .data
= (void *)&s3c6410_rtc_data
,
887 .compatible
= "samsung,exynos3250-rtc",
888 .data
= (void *)&exynos3250_rtc_data
,
892 MODULE_DEVICE_TABLE(of
, s3c_rtc_dt_match
);
894 static struct platform_driver s3c_rtc_driver
= {
895 .probe
= s3c_rtc_probe
,
896 .remove
= s3c_rtc_remove
,
899 .pm
= &s3c_rtc_pm_ops
,
900 .of_match_table
= of_match_ptr(s3c_rtc_dt_match
),
903 module_platform_driver(s3c_rtc_driver
);
905 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
906 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
907 MODULE_LICENSE("GPL");
908 MODULE_ALIAS("platform:s3c2410-rtc");