Linux 4.19.133
[linux/fpc-iii.git] / drivers / rtc / rtc-s3c.c
blob58e03ac3578b7310fbc3badacc2fff2b55da861e
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>
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>
26 #include <linux/log2.h>
27 #include <linux/slab.h>
28 #include <linux/of.h>
29 #include <linux/uaccess.h>
30 #include <linux/io.h>
32 #include <asm/irq.h>
33 #include "rtc-s3c.h"
35 struct s3c_rtc {
36 struct device *dev;
37 struct rtc_device *rtc;
39 void __iomem *base;
40 struct clk *rtc_clk;
41 struct clk *rtc_src_clk;
42 bool clk_disabled;
44 const struct s3c_rtc_data *data;
46 int irq_alarm;
47 int irq_tick;
49 spinlock_t pie_lock;
50 spinlock_t alarm_clk_lock;
52 int ticnt_save;
53 int ticnt_en_save;
54 bool wake_en;
57 struct s3c_rtc_data {
58 int max_user_freq;
59 bool needs_src_clk;
61 void (*irq_handler) (struct s3c_rtc *info, int mask);
62 void (*set_freq) (struct s3c_rtc *info, int freq);
63 void (*enable_tick) (struct s3c_rtc *info, struct seq_file *seq);
64 void (*select_tick_clk) (struct s3c_rtc *info);
65 void (*save_tick_cnt) (struct s3c_rtc *info);
66 void (*restore_tick_cnt) (struct s3c_rtc *info);
67 void (*enable) (struct s3c_rtc *info);
68 void (*disable) (struct s3c_rtc *info);
71 static int s3c_rtc_enable_clk(struct s3c_rtc *info)
73 unsigned long irq_flags;
74 int ret = 0;
76 spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
78 if (info->clk_disabled) {
79 ret = clk_enable(info->rtc_clk);
80 if (ret)
81 goto out;
83 if (info->data->needs_src_clk) {
84 ret = clk_enable(info->rtc_src_clk);
85 if (ret) {
86 clk_disable(info->rtc_clk);
87 goto out;
90 info->clk_disabled = false;
93 out:
94 spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
96 return ret;
99 static void s3c_rtc_disable_clk(struct s3c_rtc *info)
101 unsigned long irq_flags;
103 spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
104 if (!info->clk_disabled) {
105 if (info->data->needs_src_clk)
106 clk_disable(info->rtc_src_clk);
107 clk_disable(info->rtc_clk);
108 info->clk_disabled = true;
110 spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
113 /* IRQ Handlers */
114 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
116 struct s3c_rtc *info = (struct s3c_rtc *)id;
118 if (info->data->irq_handler)
119 info->data->irq_handler(info, S3C2410_INTP_TIC);
121 return IRQ_HANDLED;
124 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
126 struct s3c_rtc *info = (struct s3c_rtc *)id;
128 if (info->data->irq_handler)
129 info->data->irq_handler(info, S3C2410_INTP_ALM);
131 return IRQ_HANDLED;
134 /* Update control registers */
135 static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
137 struct s3c_rtc *info = dev_get_drvdata(dev);
138 unsigned int tmp;
139 int ret;
141 dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
143 ret = s3c_rtc_enable_clk(info);
144 if (ret)
145 return ret;
147 tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
149 if (enabled)
150 tmp |= S3C2410_RTCALM_ALMEN;
152 writeb(tmp, info->base + S3C2410_RTCALM);
154 s3c_rtc_disable_clk(info);
156 if (enabled) {
157 ret = s3c_rtc_enable_clk(info);
158 if (ret)
159 return ret;
160 } else {
161 s3c_rtc_disable_clk(info);
164 return 0;
167 /* Set RTC frequency */
168 static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
170 int ret;
172 if (!is_power_of_2(freq))
173 return -EINVAL;
175 ret = s3c_rtc_enable_clk(info);
176 if (ret)
177 return ret;
178 spin_lock_irq(&info->pie_lock);
180 if (info->data->set_freq)
181 info->data->set_freq(info, freq);
183 spin_unlock_irq(&info->pie_lock);
184 s3c_rtc_disable_clk(info);
186 return 0;
189 /* Time read/write */
190 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
192 struct s3c_rtc *info = dev_get_drvdata(dev);
193 unsigned int have_retried = 0;
194 int ret;
196 ret = s3c_rtc_enable_clk(info);
197 if (ret)
198 return ret;
200 retry_get_time:
201 rtc_tm->tm_min = readb(info->base + S3C2410_RTCMIN);
202 rtc_tm->tm_hour = readb(info->base + S3C2410_RTCHOUR);
203 rtc_tm->tm_mday = readb(info->base + S3C2410_RTCDATE);
204 rtc_tm->tm_mon = readb(info->base + S3C2410_RTCMON);
205 rtc_tm->tm_year = readb(info->base + S3C2410_RTCYEAR);
206 rtc_tm->tm_sec = readb(info->base + S3C2410_RTCSEC);
208 /* the only way to work out whether the system was mid-update
209 * when we read it is to check the second counter, and if it
210 * is zero, then we re-try the entire read
213 if (rtc_tm->tm_sec == 0 && !have_retried) {
214 have_retried = 1;
215 goto retry_get_time;
218 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
219 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
220 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
221 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
222 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
223 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
225 s3c_rtc_disable_clk(info);
227 rtc_tm->tm_year += 100;
229 dev_dbg(dev, "read time %04d.%02d.%02d %02d:%02d:%02d\n",
230 1900 + rtc_tm->tm_year, rtc_tm->tm_mon, rtc_tm->tm_mday,
231 rtc_tm->tm_hour, rtc_tm->tm_min, rtc_tm->tm_sec);
233 rtc_tm->tm_mon -= 1;
235 return 0;
238 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
240 struct s3c_rtc *info = dev_get_drvdata(dev);
241 int year = tm->tm_year - 100;
242 int ret;
244 dev_dbg(dev, "set time %04d.%02d.%02d %02d:%02d:%02d\n",
245 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
246 tm->tm_hour, tm->tm_min, tm->tm_sec);
248 /* we get around y2k by simply not supporting it */
250 if (year < 0 || year >= 100) {
251 dev_err(dev, "rtc only supports 100 years\n");
252 return -EINVAL;
255 ret = s3c_rtc_enable_clk(info);
256 if (ret)
257 return ret;
259 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_RTCSEC);
260 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_RTCMIN);
261 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_RTCHOUR);
262 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_RTCDATE);
263 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_RTCMON);
264 writeb(bin2bcd(year), info->base + S3C2410_RTCYEAR);
266 s3c_rtc_disable_clk(info);
268 return 0;
271 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
273 struct s3c_rtc *info = dev_get_drvdata(dev);
274 struct rtc_time *alm_tm = &alrm->time;
275 unsigned int alm_en;
276 int ret;
278 ret = s3c_rtc_enable_clk(info);
279 if (ret)
280 return ret;
282 alm_tm->tm_sec = readb(info->base + S3C2410_ALMSEC);
283 alm_tm->tm_min = readb(info->base + S3C2410_ALMMIN);
284 alm_tm->tm_hour = readb(info->base + S3C2410_ALMHOUR);
285 alm_tm->tm_mon = readb(info->base + S3C2410_ALMMON);
286 alm_tm->tm_mday = readb(info->base + S3C2410_ALMDATE);
287 alm_tm->tm_year = readb(info->base + S3C2410_ALMYEAR);
289 alm_en = readb(info->base + S3C2410_RTCALM);
291 s3c_rtc_disable_clk(info);
293 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
295 dev_dbg(dev, "read alarm %d, %04d.%02d.%02d %02d:%02d:%02d\n",
296 alm_en,
297 1900 + alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
298 alm_tm->tm_hour, alm_tm->tm_min, alm_tm->tm_sec);
300 /* decode the alarm enable field */
301 if (alm_en & S3C2410_RTCALM_SECEN)
302 alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
304 if (alm_en & S3C2410_RTCALM_MINEN)
305 alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
307 if (alm_en & S3C2410_RTCALM_HOUREN)
308 alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
310 if (alm_en & S3C2410_RTCALM_DAYEN)
311 alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
313 if (alm_en & S3C2410_RTCALM_MONEN) {
314 alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
315 alm_tm->tm_mon -= 1;
318 if (alm_en & S3C2410_RTCALM_YEAREN)
319 alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
321 return 0;
324 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
326 struct s3c_rtc *info = dev_get_drvdata(dev);
327 struct rtc_time *tm = &alrm->time;
328 unsigned int alrm_en;
329 int ret;
331 dev_dbg(dev, "s3c_rtc_setalarm: %d, %04d.%02d.%02d %02d:%02d:%02d\n",
332 alrm->enabled,
333 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
334 tm->tm_hour, tm->tm_min, tm->tm_sec);
336 ret = s3c_rtc_enable_clk(info);
337 if (ret)
338 return ret;
340 alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
341 writeb(0x00, info->base + S3C2410_RTCALM);
343 if (tm->tm_sec < 60 && tm->tm_sec >= 0) {
344 alrm_en |= S3C2410_RTCALM_SECEN;
345 writeb(bin2bcd(tm->tm_sec), info->base + S3C2410_ALMSEC);
348 if (tm->tm_min < 60 && tm->tm_min >= 0) {
349 alrm_en |= S3C2410_RTCALM_MINEN;
350 writeb(bin2bcd(tm->tm_min), info->base + S3C2410_ALMMIN);
353 if (tm->tm_hour < 24 && tm->tm_hour >= 0) {
354 alrm_en |= S3C2410_RTCALM_HOUREN;
355 writeb(bin2bcd(tm->tm_hour), info->base + S3C2410_ALMHOUR);
358 if (tm->tm_mon < 12 && tm->tm_mon >= 0) {
359 alrm_en |= S3C2410_RTCALM_MONEN;
360 writeb(bin2bcd(tm->tm_mon + 1), info->base + S3C2410_ALMMON);
363 if (tm->tm_mday <= 31 && tm->tm_mday >= 1) {
364 alrm_en |= S3C2410_RTCALM_DAYEN;
365 writeb(bin2bcd(tm->tm_mday), info->base + S3C2410_ALMDATE);
368 dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
370 writeb(alrm_en, info->base + S3C2410_RTCALM);
372 s3c_rtc_disable_clk(info);
374 s3c_rtc_setaie(dev, alrm->enabled);
376 return 0;
379 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
381 struct s3c_rtc *info = dev_get_drvdata(dev);
382 int ret;
384 ret = s3c_rtc_enable_clk(info);
385 if (ret)
386 return ret;
388 if (info->data->enable_tick)
389 info->data->enable_tick(info, seq);
391 s3c_rtc_disable_clk(info);
393 return 0;
396 static const struct rtc_class_ops s3c_rtcops = {
397 .read_time = s3c_rtc_gettime,
398 .set_time = s3c_rtc_settime,
399 .read_alarm = s3c_rtc_getalarm,
400 .set_alarm = s3c_rtc_setalarm,
401 .proc = s3c_rtc_proc,
402 .alarm_irq_enable = s3c_rtc_setaie,
405 static void s3c24xx_rtc_enable(struct s3c_rtc *info)
407 unsigned int con, tmp;
409 con = readw(info->base + S3C2410_RTCCON);
410 /* re-enable the device, and check it is ok */
411 if ((con & S3C2410_RTCCON_RTCEN) == 0) {
412 dev_info(info->dev, "rtc disabled, re-enabling\n");
414 tmp = readw(info->base + S3C2410_RTCCON);
415 writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON);
418 if (con & S3C2410_RTCCON_CNTSEL) {
419 dev_info(info->dev, "removing RTCCON_CNTSEL\n");
421 tmp = readw(info->base + S3C2410_RTCCON);
422 writew(tmp & ~S3C2410_RTCCON_CNTSEL,
423 info->base + S3C2410_RTCCON);
426 if (con & S3C2410_RTCCON_CLKRST) {
427 dev_info(info->dev, "removing RTCCON_CLKRST\n");
429 tmp = readw(info->base + S3C2410_RTCCON);
430 writew(tmp & ~S3C2410_RTCCON_CLKRST,
431 info->base + S3C2410_RTCCON);
435 static void s3c24xx_rtc_disable(struct s3c_rtc *info)
437 unsigned int con;
439 con = readw(info->base + S3C2410_RTCCON);
440 con &= ~S3C2410_RTCCON_RTCEN;
441 writew(con, info->base + S3C2410_RTCCON);
443 con = readb(info->base + S3C2410_TICNT);
444 con &= ~S3C2410_TICNT_ENABLE;
445 writeb(con, info->base + S3C2410_TICNT);
448 static void s3c6410_rtc_disable(struct s3c_rtc *info)
450 unsigned int con;
452 con = readw(info->base + S3C2410_RTCCON);
453 con &= ~S3C64XX_RTCCON_TICEN;
454 con &= ~S3C2410_RTCCON_RTCEN;
455 writew(con, info->base + S3C2410_RTCCON);
458 static int s3c_rtc_remove(struct platform_device *pdev)
460 struct s3c_rtc *info = platform_get_drvdata(pdev);
462 s3c_rtc_setaie(info->dev, 0);
464 if (info->data->needs_src_clk)
465 clk_unprepare(info->rtc_src_clk);
466 clk_unprepare(info->rtc_clk);
468 return 0;
471 static const struct of_device_id s3c_rtc_dt_match[];
473 static const struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
475 const struct of_device_id *match;
477 match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
478 return match->data;
481 static int s3c_rtc_probe(struct platform_device *pdev)
483 struct s3c_rtc *info = NULL;
484 struct rtc_time rtc_tm;
485 struct resource *res;
486 int ret;
488 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
489 if (!info)
490 return -ENOMEM;
492 /* find the IRQs */
493 info->irq_tick = platform_get_irq(pdev, 1);
494 if (info->irq_tick < 0) {
495 dev_err(&pdev->dev, "no irq for rtc tick\n");
496 return info->irq_tick;
499 info->dev = &pdev->dev;
500 info->data = s3c_rtc_get_data(pdev);
501 if (!info->data) {
502 dev_err(&pdev->dev, "failed getting s3c_rtc_data\n");
503 return -EINVAL;
505 spin_lock_init(&info->pie_lock);
506 spin_lock_init(&info->alarm_clk_lock);
508 platform_set_drvdata(pdev, info);
510 info->irq_alarm = platform_get_irq(pdev, 0);
511 if (info->irq_alarm < 0) {
512 dev_err(&pdev->dev, "no irq for alarm\n");
513 return info->irq_alarm;
516 dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
517 info->irq_tick, info->irq_alarm);
519 /* get the memory region */
520 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
521 info->base = devm_ioremap_resource(&pdev->dev, res);
522 if (IS_ERR(info->base))
523 return PTR_ERR(info->base);
525 info->rtc_clk = devm_clk_get(&pdev->dev, "rtc");
526 if (IS_ERR(info->rtc_clk)) {
527 ret = PTR_ERR(info->rtc_clk);
528 if (ret != -EPROBE_DEFER)
529 dev_err(&pdev->dev, "failed to find rtc clock\n");
530 else
531 dev_dbg(&pdev->dev, "probe deferred due to missing rtc clk\n");
532 return ret;
534 ret = clk_prepare_enable(info->rtc_clk);
535 if (ret)
536 return ret;
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)) {
541 ret = PTR_ERR(info->rtc_src_clk);
542 if (ret != -EPROBE_DEFER)
543 dev_err(&pdev->dev,
544 "failed to find rtc source clock\n");
545 else
546 dev_dbg(&pdev->dev,
547 "probe deferred due to missing rtc src clk\n");
548 goto err_src_clk;
550 ret = clk_prepare_enable(info->rtc_src_clk);
551 if (ret)
552 goto err_src_clk;
555 /* check to see if everything is setup correctly */
556 if (info->data->enable)
557 info->data->enable(info);
559 dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
560 readw(info->base + S3C2410_RTCCON));
562 device_init_wakeup(&pdev->dev, 1);
564 /* Check RTC Time */
565 if (s3c_rtc_gettime(&pdev->dev, &rtc_tm)) {
566 rtc_tm.tm_year = 100;
567 rtc_tm.tm_mon = 0;
568 rtc_tm.tm_mday = 1;
569 rtc_tm.tm_hour = 0;
570 rtc_tm.tm_min = 0;
571 rtc_tm.tm_sec = 0;
573 s3c_rtc_settime(&pdev->dev, &rtc_tm);
575 dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
578 /* register RTC and exit */
579 info->rtc = devm_rtc_device_register(&pdev->dev, "s3c", &s3c_rtcops,
580 THIS_MODULE);
581 if (IS_ERR(info->rtc)) {
582 dev_err(&pdev->dev, "cannot attach rtc\n");
583 ret = PTR_ERR(info->rtc);
584 goto err_nortc;
587 ret = devm_request_irq(&pdev->dev, info->irq_alarm, s3c_rtc_alarmirq,
588 0, "s3c2410-rtc alarm", info);
589 if (ret) {
590 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_alarm, ret);
591 goto err_nortc;
594 ret = devm_request_irq(&pdev->dev, info->irq_tick, s3c_rtc_tickirq,
595 0, "s3c2410-rtc tick", info);
596 if (ret) {
597 dev_err(&pdev->dev, "IRQ%d error %d\n", info->irq_tick, ret);
598 goto err_nortc;
601 if (info->data->select_tick_clk)
602 info->data->select_tick_clk(info);
604 s3c_rtc_setfreq(info, 1);
606 return 0;
608 err_nortc:
609 if (info->data->disable)
610 info->data->disable(info);
612 if (info->data->needs_src_clk)
613 clk_disable_unprepare(info->rtc_src_clk);
614 err_src_clk:
615 clk_disable_unprepare(info->rtc_clk);
617 return ret;
620 #ifdef CONFIG_PM_SLEEP
622 static int s3c_rtc_suspend(struct device *dev)
624 struct s3c_rtc *info = dev_get_drvdata(dev);
625 int ret;
627 ret = s3c_rtc_enable_clk(info);
628 if (ret)
629 return ret;
631 /* save TICNT for anyone using periodic interrupts */
632 if (info->data->save_tick_cnt)
633 info->data->save_tick_cnt(info);
635 if (info->data->disable)
636 info->data->disable(info);
638 if (device_may_wakeup(dev) && !info->wake_en) {
639 if (enable_irq_wake(info->irq_alarm) == 0)
640 info->wake_en = true;
641 else
642 dev_err(dev, "enable_irq_wake failed\n");
645 return 0;
648 static int s3c_rtc_resume(struct device *dev)
650 struct s3c_rtc *info = dev_get_drvdata(dev);
652 if (info->data->enable)
653 info->data->enable(info);
655 if (info->data->restore_tick_cnt)
656 info->data->restore_tick_cnt(info);
658 s3c_rtc_disable_clk(info);
660 if (device_may_wakeup(dev) && info->wake_en) {
661 disable_irq_wake(info->irq_alarm);
662 info->wake_en = false;
665 return 0;
667 #endif
668 static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
670 static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
672 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
675 static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
677 rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
678 writeb(mask, info->base + S3C2410_INTP);
681 static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
683 unsigned int tmp = 0;
684 int val;
686 tmp = readb(info->base + S3C2410_TICNT);
687 tmp &= S3C2410_TICNT_ENABLE;
689 val = (info->rtc->max_user_freq / freq) - 1;
690 tmp |= val;
692 writel(tmp, info->base + S3C2410_TICNT);
695 static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
697 unsigned int tmp = 0;
698 int val;
700 tmp = readb(info->base + S3C2410_TICNT);
701 tmp &= S3C2410_TICNT_ENABLE;
703 val = (info->rtc->max_user_freq / freq) - 1;
705 tmp |= S3C2443_TICNT_PART(val);
706 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
708 writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
710 writel(tmp, info->base + S3C2410_TICNT);
713 static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
715 unsigned int tmp = 0;
716 int val;
718 tmp = readb(info->base + S3C2410_TICNT);
719 tmp &= S3C2410_TICNT_ENABLE;
721 val = (info->rtc->max_user_freq / freq) - 1;
723 tmp |= S3C2443_TICNT_PART(val);
724 writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
726 writel(tmp, info->base + S3C2410_TICNT);
729 static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
731 int val;
733 val = (info->rtc->max_user_freq / freq) - 1;
734 writel(val, info->base + S3C2410_TICNT);
737 static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
739 unsigned int ticnt;
741 ticnt = readb(info->base + S3C2410_TICNT);
742 ticnt &= S3C2410_TICNT_ENABLE;
744 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
747 static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
749 unsigned int con;
751 con = readw(info->base + S3C2410_RTCCON);
752 con |= S3C2443_RTCCON_TICSEL;
753 writew(con, info->base + S3C2410_RTCCON);
756 static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
758 unsigned int ticnt;
760 ticnt = readw(info->base + S3C2410_RTCCON);
761 ticnt &= S3C64XX_RTCCON_TICEN;
763 seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt ? "yes" : "no");
766 static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
768 info->ticnt_save = readb(info->base + S3C2410_TICNT);
771 static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
773 writeb(info->ticnt_save, info->base + S3C2410_TICNT);
776 static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
778 info->ticnt_en_save = readw(info->base + S3C2410_RTCCON);
779 info->ticnt_en_save &= S3C64XX_RTCCON_TICEN;
780 info->ticnt_save = readl(info->base + S3C2410_TICNT);
783 static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
785 unsigned int con;
787 writel(info->ticnt_save, info->base + S3C2410_TICNT);
788 if (info->ticnt_en_save) {
789 con = readw(info->base + S3C2410_RTCCON);
790 writew(con | info->ticnt_en_save, info->base + S3C2410_RTCCON);
794 static struct s3c_rtc_data const s3c2410_rtc_data = {
795 .max_user_freq = 128,
796 .irq_handler = s3c24xx_rtc_irq,
797 .set_freq = s3c2410_rtc_setfreq,
798 .enable_tick = s3c24xx_rtc_enable_tick,
799 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
800 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
801 .enable = s3c24xx_rtc_enable,
802 .disable = s3c24xx_rtc_disable,
805 static struct s3c_rtc_data const s3c2416_rtc_data = {
806 .max_user_freq = 32768,
807 .irq_handler = s3c24xx_rtc_irq,
808 .set_freq = s3c2416_rtc_setfreq,
809 .enable_tick = s3c24xx_rtc_enable_tick,
810 .select_tick_clk = s3c2416_rtc_select_tick_clk,
811 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
812 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
813 .enable = s3c24xx_rtc_enable,
814 .disable = s3c24xx_rtc_disable,
817 static struct s3c_rtc_data const s3c2443_rtc_data = {
818 .max_user_freq = 32768,
819 .irq_handler = s3c24xx_rtc_irq,
820 .set_freq = s3c2443_rtc_setfreq,
821 .enable_tick = s3c24xx_rtc_enable_tick,
822 .select_tick_clk = s3c2416_rtc_select_tick_clk,
823 .save_tick_cnt = s3c24xx_rtc_save_tick_cnt,
824 .restore_tick_cnt = s3c24xx_rtc_restore_tick_cnt,
825 .enable = s3c24xx_rtc_enable,
826 .disable = s3c24xx_rtc_disable,
829 static struct s3c_rtc_data const s3c6410_rtc_data = {
830 .max_user_freq = 32768,
831 .needs_src_clk = true,
832 .irq_handler = s3c6410_rtc_irq,
833 .set_freq = s3c6410_rtc_setfreq,
834 .enable_tick = s3c6410_rtc_enable_tick,
835 .save_tick_cnt = s3c6410_rtc_save_tick_cnt,
836 .restore_tick_cnt = s3c6410_rtc_restore_tick_cnt,
837 .enable = s3c24xx_rtc_enable,
838 .disable = s3c6410_rtc_disable,
841 static const struct of_device_id s3c_rtc_dt_match[] = {
843 .compatible = "samsung,s3c2410-rtc",
844 .data = &s3c2410_rtc_data,
845 }, {
846 .compatible = "samsung,s3c2416-rtc",
847 .data = &s3c2416_rtc_data,
848 }, {
849 .compatible = "samsung,s3c2443-rtc",
850 .data = &s3c2443_rtc_data,
851 }, {
852 .compatible = "samsung,s3c6410-rtc",
853 .data = &s3c6410_rtc_data,
854 }, {
855 .compatible = "samsung,exynos3250-rtc",
856 .data = &s3c6410_rtc_data,
858 { /* sentinel */ },
860 MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
862 static struct platform_driver s3c_rtc_driver = {
863 .probe = s3c_rtc_probe,
864 .remove = s3c_rtc_remove,
865 .driver = {
866 .name = "s3c-rtc",
867 .pm = &s3c_rtc_pm_ops,
868 .of_match_table = of_match_ptr(s3c_rtc_dt_match),
871 module_platform_driver(s3c_rtc_driver);
873 MODULE_DESCRIPTION("Samsung S3C RTC Driver");
874 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
875 MODULE_LICENSE("GPL");
876 MODULE_ALIAS("platform:s3c2410-rtc");