1 /* drivers/rtc/rtc-s3c.c
2  *
3  * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4  *		http://www.samsung.com/
5  *
6  * Copyright (c) 2004,2006 Simtec Electronics
7  *	Ben Dooks, <ben@simtec.co.uk>
8  *	http://armlinux.simtec.co.uk/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * S3C2410/S3C2440/S3C24XX Internal RTC Driver
15 */
16 
17 #include <linux/module.h>
18 #include <linux/fs.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/interrupt.h>
23 #include <linux/rtc.h>
24 #include <linux/bcd.h>
25 #include <linux/clk.h>
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>
31 
32 #include <asm/irq.h>
33 #include "rtc-s3c.h"
34 
35 struct s3c_rtc {
36 	struct device *dev;
37 	struct rtc_device *rtc;
38 
39 	void __iomem *base;
40 	struct clk *rtc_clk;
41 	struct clk *rtc_src_clk;
42 	bool clk_disabled;
43 
44 	const struct s3c_rtc_data *data;
45 
46 	int irq_alarm;
47 	int irq_tick;
48 
49 	spinlock_t pie_lock;
50 	spinlock_t alarm_clk_lock;
51 
52 	int ticnt_save;
53 	int ticnt_en_save;
54 	bool wake_en;
55 };
56 
57 struct s3c_rtc_data {
58 	int max_user_freq;
59 	bool needs_src_clk;
60 
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);
69 };
70 
s3c_rtc_enable_clk(struct s3c_rtc * info)71 static int s3c_rtc_enable_clk(struct s3c_rtc *info)
72 {
73 	unsigned long irq_flags;
74 	int ret = 0;
75 
76 	spin_lock_irqsave(&info->alarm_clk_lock, irq_flags);
77 
78 	if (info->clk_disabled) {
79 		ret = clk_enable(info->rtc_clk);
80 		if (ret)
81 			goto out;
82 
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;
88 			}
89 		}
90 		info->clk_disabled = false;
91 	}
92 
93 out:
94 	spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
95 
96 	return ret;
97 }
98 
s3c_rtc_disable_clk(struct s3c_rtc * info)99 static void s3c_rtc_disable_clk(struct s3c_rtc *info)
100 {
101 	unsigned long irq_flags;
102 
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;
109 	}
110 	spin_unlock_irqrestore(&info->alarm_clk_lock, irq_flags);
111 }
112 
113 /* IRQ Handlers */
s3c_rtc_tickirq(int irq,void * id)114 static irqreturn_t s3c_rtc_tickirq(int irq, void *id)
115 {
116 	struct s3c_rtc *info = (struct s3c_rtc *)id;
117 
118 	if (info->data->irq_handler)
119 		info->data->irq_handler(info, S3C2410_INTP_TIC);
120 
121 	return IRQ_HANDLED;
122 }
123 
s3c_rtc_alarmirq(int irq,void * id)124 static irqreturn_t s3c_rtc_alarmirq(int irq, void *id)
125 {
126 	struct s3c_rtc *info = (struct s3c_rtc *)id;
127 
128 	if (info->data->irq_handler)
129 		info->data->irq_handler(info, S3C2410_INTP_ALM);
130 
131 	return IRQ_HANDLED;
132 }
133 
134 /* Update control registers */
s3c_rtc_setaie(struct device * dev,unsigned int enabled)135 static int s3c_rtc_setaie(struct device *dev, unsigned int enabled)
136 {
137 	struct s3c_rtc *info = dev_get_drvdata(dev);
138 	unsigned int tmp;
139 	int ret;
140 
141 	dev_dbg(info->dev, "%s: aie=%d\n", __func__, enabled);
142 
143 	ret = s3c_rtc_enable_clk(info);
144 	if (ret)
145 		return ret;
146 
147 	tmp = readb(info->base + S3C2410_RTCALM) & ~S3C2410_RTCALM_ALMEN;
148 
149 	if (enabled)
150 		tmp |= S3C2410_RTCALM_ALMEN;
151 
152 	writeb(tmp, info->base + S3C2410_RTCALM);
153 
154 	s3c_rtc_disable_clk(info);
155 
156 	if (enabled) {
157 		ret = s3c_rtc_enable_clk(info);
158 		if (ret)
159 			return ret;
160 	} else {
161 		s3c_rtc_disable_clk(info);
162 	}
163 
164 	return 0;
165 }
166 
167 /* Set RTC frequency */
s3c_rtc_setfreq(struct s3c_rtc * info,int freq)168 static int s3c_rtc_setfreq(struct s3c_rtc *info, int freq)
169 {
170 	int ret;
171 
172 	if (!is_power_of_2(freq))
173 		return -EINVAL;
174 
175 	ret = s3c_rtc_enable_clk(info);
176 	if (ret)
177 		return ret;
178 	spin_lock_irq(&info->pie_lock);
179 
180 	if (info->data->set_freq)
181 		info->data->set_freq(info, freq);
182 
183 	spin_unlock_irq(&info->pie_lock);
184 	s3c_rtc_disable_clk(info);
185 
186 	return 0;
187 }
188 
189 /* Time read/write */
s3c_rtc_gettime(struct device * dev,struct rtc_time * rtc_tm)190 static int s3c_rtc_gettime(struct device *dev, struct rtc_time *rtc_tm)
191 {
192 	struct s3c_rtc *info = dev_get_drvdata(dev);
193 	unsigned int have_retried = 0;
194 	int ret;
195 
196 	ret = s3c_rtc_enable_clk(info);
197 	if (ret)
198 		return ret;
199 
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);
207 
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
211 	 */
212 
213 	if (rtc_tm->tm_sec == 0 && !have_retried) {
214 		have_retried = 1;
215 		goto retry_get_time;
216 	}
217 
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);
224 
225 	s3c_rtc_disable_clk(info);
226 
227 	rtc_tm->tm_year += 100;
228 
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);
232 
233 	rtc_tm->tm_mon -= 1;
234 
235 	return 0;
236 }
237 
s3c_rtc_settime(struct device * dev,struct rtc_time * tm)238 static int s3c_rtc_settime(struct device *dev, struct rtc_time *tm)
239 {
240 	struct s3c_rtc *info = dev_get_drvdata(dev);
241 	int year = tm->tm_year - 100;
242 	int ret;
243 
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);
247 
248 	/* we get around y2k by simply not supporting it */
249 
250 	if (year < 0 || year >= 100) {
251 		dev_err(dev, "rtc only supports 100 years\n");
252 		return -EINVAL;
253 	}
254 
255 	ret = s3c_rtc_enable_clk(info);
256 	if (ret)
257 		return ret;
258 
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);
265 
266 	s3c_rtc_disable_clk(info);
267 
268 	return 0;
269 }
270 
s3c_rtc_getalarm(struct device * dev,struct rtc_wkalrm * alrm)271 static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm)
272 {
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;
277 
278 	ret = s3c_rtc_enable_clk(info);
279 	if (ret)
280 		return ret;
281 
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);
288 
289 	alm_en = readb(info->base + S3C2410_RTCALM);
290 
291 	s3c_rtc_disable_clk(info);
292 
293 	alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
294 
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);
299 
300 	/* decode the alarm enable field */
301 	if (alm_en & S3C2410_RTCALM_SECEN)
302 		alm_tm->tm_sec = bcd2bin(alm_tm->tm_sec);
303 
304 	if (alm_en & S3C2410_RTCALM_MINEN)
305 		alm_tm->tm_min = bcd2bin(alm_tm->tm_min);
306 
307 	if (alm_en & S3C2410_RTCALM_HOUREN)
308 		alm_tm->tm_hour = bcd2bin(alm_tm->tm_hour);
309 
310 	if (alm_en & S3C2410_RTCALM_DAYEN)
311 		alm_tm->tm_mday = bcd2bin(alm_tm->tm_mday);
312 
313 	if (alm_en & S3C2410_RTCALM_MONEN) {
314 		alm_tm->tm_mon = bcd2bin(alm_tm->tm_mon);
315 		alm_tm->tm_mon -= 1;
316 	}
317 
318 	if (alm_en & S3C2410_RTCALM_YEAREN)
319 		alm_tm->tm_year = bcd2bin(alm_tm->tm_year);
320 
321 	return 0;
322 }
323 
s3c_rtc_setalarm(struct device * dev,struct rtc_wkalrm * alrm)324 static int s3c_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
325 {
326 	struct s3c_rtc *info = dev_get_drvdata(dev);
327 	struct rtc_time *tm = &alrm->time;
328 	unsigned int alrm_en;
329 	int ret;
330 
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);
335 
336 	ret = s3c_rtc_enable_clk(info);
337 	if (ret)
338 		return ret;
339 
340 	alrm_en = readb(info->base + S3C2410_RTCALM) & S3C2410_RTCALM_ALMEN;
341 	writeb(0x00, info->base + S3C2410_RTCALM);
342 
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);
346 	}
347 
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);
351 	}
352 
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);
356 	}
357 
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);
361 	}
362 
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);
366 	}
367 
368 	dev_dbg(dev, "setting S3C2410_RTCALM to %08x\n", alrm_en);
369 
370 	writeb(alrm_en, info->base + S3C2410_RTCALM);
371 
372 	s3c_rtc_disable_clk(info);
373 
374 	s3c_rtc_setaie(dev, alrm->enabled);
375 
376 	return 0;
377 }
378 
s3c_rtc_proc(struct device * dev,struct seq_file * seq)379 static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
380 {
381 	struct s3c_rtc *info = dev_get_drvdata(dev);
382 	int ret;
383 
384 	ret = s3c_rtc_enable_clk(info);
385 	if (ret)
386 		return ret;
387 
388 	if (info->data->enable_tick)
389 		info->data->enable_tick(info, seq);
390 
391 	s3c_rtc_disable_clk(info);
392 
393 	return 0;
394 }
395 
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,
403 };
404 
s3c24xx_rtc_enable(struct s3c_rtc * info)405 static void s3c24xx_rtc_enable(struct s3c_rtc *info)
406 {
407 	unsigned int con, tmp;
408 
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");
413 
414 		tmp = readw(info->base + S3C2410_RTCCON);
415 		writew(tmp | S3C2410_RTCCON_RTCEN, info->base + S3C2410_RTCCON);
416 	}
417 
418 	if (con & S3C2410_RTCCON_CNTSEL) {
419 		dev_info(info->dev, "removing RTCCON_CNTSEL\n");
420 
421 		tmp = readw(info->base + S3C2410_RTCCON);
422 		writew(tmp & ~S3C2410_RTCCON_CNTSEL,
423 		       info->base + S3C2410_RTCCON);
424 	}
425 
426 	if (con & S3C2410_RTCCON_CLKRST) {
427 		dev_info(info->dev, "removing RTCCON_CLKRST\n");
428 
429 		tmp = readw(info->base + S3C2410_RTCCON);
430 		writew(tmp & ~S3C2410_RTCCON_CLKRST,
431 		       info->base + S3C2410_RTCCON);
432 	}
433 }
434 
s3c24xx_rtc_disable(struct s3c_rtc * info)435 static void s3c24xx_rtc_disable(struct s3c_rtc *info)
436 {
437 	unsigned int con;
438 
439 	con = readw(info->base + S3C2410_RTCCON);
440 	con &= ~S3C2410_RTCCON_RTCEN;
441 	writew(con, info->base + S3C2410_RTCCON);
442 
443 	con = readb(info->base + S3C2410_TICNT);
444 	con &= ~S3C2410_TICNT_ENABLE;
445 	writeb(con, info->base + S3C2410_TICNT);
446 }
447 
s3c6410_rtc_disable(struct s3c_rtc * info)448 static void s3c6410_rtc_disable(struct s3c_rtc *info)
449 {
450 	unsigned int con;
451 
452 	con = readw(info->base + S3C2410_RTCCON);
453 	con &= ~S3C64XX_RTCCON_TICEN;
454 	con &= ~S3C2410_RTCCON_RTCEN;
455 	writew(con, info->base + S3C2410_RTCCON);
456 }
457 
s3c_rtc_remove(struct platform_device * pdev)458 static int s3c_rtc_remove(struct platform_device *pdev)
459 {
460 	struct s3c_rtc *info = platform_get_drvdata(pdev);
461 
462 	s3c_rtc_setaie(info->dev, 0);
463 
464 	if (info->data->needs_src_clk)
465 		clk_unprepare(info->rtc_src_clk);
466 	clk_unprepare(info->rtc_clk);
467 
468 	return 0;
469 }
470 
471 static const struct of_device_id s3c_rtc_dt_match[];
472 
s3c_rtc_get_data(struct platform_device * pdev)473 static const struct s3c_rtc_data *s3c_rtc_get_data(struct platform_device *pdev)
474 {
475 	const struct of_device_id *match;
476 
477 	match = of_match_node(s3c_rtc_dt_match, pdev->dev.of_node);
478 	return match->data;
479 }
480 
s3c_rtc_probe(struct platform_device * pdev)481 static int s3c_rtc_probe(struct platform_device *pdev)
482 {
483 	struct s3c_rtc *info = NULL;
484 	struct rtc_time rtc_tm;
485 	struct resource *res;
486 	int ret;
487 
488 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
489 	if (!info)
490 		return -ENOMEM;
491 
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;
497 	}
498 
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;
504 	}
505 	spin_lock_init(&info->pie_lock);
506 	spin_lock_init(&info->alarm_clk_lock);
507 
508 	platform_set_drvdata(pdev, info);
509 
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;
514 	}
515 
516 	dev_dbg(&pdev->dev, "s3c2410_rtc: tick irq %d, alarm irq %d\n",
517 		info->irq_tick, info->irq_alarm);
518 
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);
524 
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;
533 	}
534 	ret = clk_prepare_enable(info->rtc_clk);
535 	if (ret)
536 		return ret;
537 
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;
549 		}
550 		ret = clk_prepare_enable(info->rtc_src_clk);
551 		if (ret)
552 			goto err_src_clk;
553 	}
554 
555 	/* check to see if everything is setup correctly */
556 	if (info->data->enable)
557 		info->data->enable(info);
558 
559 	dev_dbg(&pdev->dev, "s3c2410_rtc: RTCCON=%02x\n",
560 		readw(info->base + S3C2410_RTCCON));
561 
562 	device_init_wakeup(&pdev->dev, 1);
563 
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;
572 
573 		s3c_rtc_settime(&pdev->dev, &rtc_tm);
574 
575 		dev_warn(&pdev->dev, "warning: invalid RTC value so initializing it\n");
576 	}
577 
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;
585 	}
586 
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;
592 	}
593 
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;
599 	}
600 
601 	if (info->data->select_tick_clk)
602 		info->data->select_tick_clk(info);
603 
604 	s3c_rtc_setfreq(info, 1);
605 
606 	return 0;
607 
608 err_nortc:
609 	if (info->data->disable)
610 		info->data->disable(info);
611 
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);
616 
617 	return ret;
618 }
619 
620 #ifdef CONFIG_PM_SLEEP
621 
s3c_rtc_suspend(struct device * dev)622 static int s3c_rtc_suspend(struct device *dev)
623 {
624 	struct s3c_rtc *info = dev_get_drvdata(dev);
625 	int ret;
626 
627 	ret = s3c_rtc_enable_clk(info);
628 	if (ret)
629 		return ret;
630 
631 	/* save TICNT for anyone using periodic interrupts */
632 	if (info->data->save_tick_cnt)
633 		info->data->save_tick_cnt(info);
634 
635 	if (info->data->disable)
636 		info->data->disable(info);
637 
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");
643 	}
644 
645 	return 0;
646 }
647 
s3c_rtc_resume(struct device * dev)648 static int s3c_rtc_resume(struct device *dev)
649 {
650 	struct s3c_rtc *info = dev_get_drvdata(dev);
651 
652 	if (info->data->enable)
653 		info->data->enable(info);
654 
655 	if (info->data->restore_tick_cnt)
656 		info->data->restore_tick_cnt(info);
657 
658 	s3c_rtc_disable_clk(info);
659 
660 	if (device_may_wakeup(dev) && info->wake_en) {
661 		disable_irq_wake(info->irq_alarm);
662 		info->wake_en = false;
663 	}
664 
665 	return 0;
666 }
667 #endif
668 static SIMPLE_DEV_PM_OPS(s3c_rtc_pm_ops, s3c_rtc_suspend, s3c_rtc_resume);
669 
s3c24xx_rtc_irq(struct s3c_rtc * info,int mask)670 static void s3c24xx_rtc_irq(struct s3c_rtc *info, int mask)
671 {
672 	rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
673 }
674 
s3c6410_rtc_irq(struct s3c_rtc * info,int mask)675 static void s3c6410_rtc_irq(struct s3c_rtc *info, int mask)
676 {
677 	rtc_update_irq(info->rtc, 1, RTC_AF | RTC_IRQF);
678 	writeb(mask, info->base + S3C2410_INTP);
679 }
680 
s3c2410_rtc_setfreq(struct s3c_rtc * info,int freq)681 static void s3c2410_rtc_setfreq(struct s3c_rtc *info, int freq)
682 {
683 	unsigned int tmp = 0;
684 	int val;
685 
686 	tmp = readb(info->base + S3C2410_TICNT);
687 	tmp &= S3C2410_TICNT_ENABLE;
688 
689 	val = (info->rtc->max_user_freq / freq) - 1;
690 	tmp |= val;
691 
692 	writel(tmp, info->base + S3C2410_TICNT);
693 }
694 
s3c2416_rtc_setfreq(struct s3c_rtc * info,int freq)695 static void s3c2416_rtc_setfreq(struct s3c_rtc *info, int freq)
696 {
697 	unsigned int tmp = 0;
698 	int val;
699 
700 	tmp = readb(info->base + S3C2410_TICNT);
701 	tmp &= S3C2410_TICNT_ENABLE;
702 
703 	val = (info->rtc->max_user_freq / freq) - 1;
704 
705 	tmp |= S3C2443_TICNT_PART(val);
706 	writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
707 
708 	writel(S3C2416_TICNT2_PART(val), info->base + S3C2416_TICNT2);
709 
710 	writel(tmp, info->base + S3C2410_TICNT);
711 }
712 
s3c2443_rtc_setfreq(struct s3c_rtc * info,int freq)713 static void s3c2443_rtc_setfreq(struct s3c_rtc *info, int freq)
714 {
715 	unsigned int tmp = 0;
716 	int val;
717 
718 	tmp = readb(info->base + S3C2410_TICNT);
719 	tmp &= S3C2410_TICNT_ENABLE;
720 
721 	val = (info->rtc->max_user_freq / freq) - 1;
722 
723 	tmp |= S3C2443_TICNT_PART(val);
724 	writel(S3C2443_TICNT1_PART(val), info->base + S3C2443_TICNT1);
725 
726 	writel(tmp, info->base + S3C2410_TICNT);
727 }
728 
s3c6410_rtc_setfreq(struct s3c_rtc * info,int freq)729 static void s3c6410_rtc_setfreq(struct s3c_rtc *info, int freq)
730 {
731 	int val;
732 
733 	val = (info->rtc->max_user_freq / freq) - 1;
734 	writel(val, info->base + S3C2410_TICNT);
735 }
736 
s3c24xx_rtc_enable_tick(struct s3c_rtc * info,struct seq_file * seq)737 static void s3c24xx_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
738 {
739 	unsigned int ticnt;
740 
741 	ticnt = readb(info->base + S3C2410_TICNT);
742 	ticnt &= S3C2410_TICNT_ENABLE;
743 
744 	seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt  ? "yes" : "no");
745 }
746 
s3c2416_rtc_select_tick_clk(struct s3c_rtc * info)747 static void s3c2416_rtc_select_tick_clk(struct s3c_rtc *info)
748 {
749 	unsigned int con;
750 
751 	con = readw(info->base + S3C2410_RTCCON);
752 	con |= S3C2443_RTCCON_TICSEL;
753 	writew(con, info->base + S3C2410_RTCCON);
754 }
755 
s3c6410_rtc_enable_tick(struct s3c_rtc * info,struct seq_file * seq)756 static void s3c6410_rtc_enable_tick(struct s3c_rtc *info, struct seq_file *seq)
757 {
758 	unsigned int ticnt;
759 
760 	ticnt = readw(info->base + S3C2410_RTCCON);
761 	ticnt &= S3C64XX_RTCCON_TICEN;
762 
763 	seq_printf(seq, "periodic_IRQ\t: %s\n", ticnt  ? "yes" : "no");
764 }
765 
s3c24xx_rtc_save_tick_cnt(struct s3c_rtc * info)766 static void s3c24xx_rtc_save_tick_cnt(struct s3c_rtc *info)
767 {
768 	info->ticnt_save = readb(info->base + S3C2410_TICNT);
769 }
770 
s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc * info)771 static void s3c24xx_rtc_restore_tick_cnt(struct s3c_rtc *info)
772 {
773 	writeb(info->ticnt_save, info->base + S3C2410_TICNT);
774 }
775 
s3c6410_rtc_save_tick_cnt(struct s3c_rtc * info)776 static void s3c6410_rtc_save_tick_cnt(struct s3c_rtc *info)
777 {
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);
781 }
782 
s3c6410_rtc_restore_tick_cnt(struct s3c_rtc * info)783 static void s3c6410_rtc_restore_tick_cnt(struct s3c_rtc *info)
784 {
785 	unsigned int con;
786 
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);
791 	}
792 }
793 
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,
803 };
804 
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,
815 };
816 
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,
827 };
828 
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,
839 };
840 
841 static const struct of_device_id s3c_rtc_dt_match[] = {
842 	{
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,
857 	},
858 	{ /* sentinel */ },
859 };
860 MODULE_DEVICE_TABLE(of, s3c_rtc_dt_match);
861 
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),
869 	},
870 };
871 module_platform_driver(s3c_rtc_driver);
872 
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");
877