xref: /openbmc/linux/drivers/rtc/rtc-sc27xx.c (revision 9be08a27)
1 /*
2  * Copyright (C) 2017 Spreadtrum Communications Inc.
3  *
4  * SPDX-License-Identifier: GPL-2.0
5  */
6 
7 #include <linux/bitops.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
15 
16 #define SPRD_RTC_SEC_CNT_VALUE		0x0
17 #define SPRD_RTC_MIN_CNT_VALUE		0x4
18 #define SPRD_RTC_HOUR_CNT_VALUE		0x8
19 #define SPRD_RTC_DAY_CNT_VALUE		0xc
20 #define SPRD_RTC_SEC_CNT_UPD		0x10
21 #define SPRD_RTC_MIN_CNT_UPD		0x14
22 #define SPRD_RTC_HOUR_CNT_UPD		0x18
23 #define SPRD_RTC_DAY_CNT_UPD		0x1c
24 #define SPRD_RTC_SEC_ALM_UPD		0x20
25 #define SPRD_RTC_MIN_ALM_UPD		0x24
26 #define SPRD_RTC_HOUR_ALM_UPD		0x28
27 #define SPRD_RTC_DAY_ALM_UPD		0x2c
28 #define SPRD_RTC_INT_EN			0x30
29 #define SPRD_RTC_INT_RAW_STS		0x34
30 #define SPRD_RTC_INT_CLR		0x38
31 #define SPRD_RTC_INT_MASK_STS		0x3C
32 #define SPRD_RTC_SEC_ALM_VALUE		0x40
33 #define SPRD_RTC_MIN_ALM_VALUE		0x44
34 #define SPRD_RTC_HOUR_ALM_VALUE		0x48
35 #define SPRD_RTC_DAY_ALM_VALUE		0x4c
36 #define SPRD_RTC_SPG_VALUE		0x50
37 #define SPRD_RTC_SPG_UPD		0x54
38 #define SPRD_RTC_PWR_CTRL		0x58
39 #define SPRD_RTC_PWR_STS		0x5c
40 #define SPRD_RTC_SEC_AUXALM_UPD		0x60
41 #define SPRD_RTC_MIN_AUXALM_UPD		0x64
42 #define SPRD_RTC_HOUR_AUXALM_UPD	0x68
43 #define SPRD_RTC_DAY_AUXALM_UPD		0x6c
44 
45 /* BIT & MASK definition for SPRD_RTC_INT_* registers */
46 #define SPRD_RTC_SEC_EN			BIT(0)
47 #define SPRD_RTC_MIN_EN			BIT(1)
48 #define SPRD_RTC_HOUR_EN		BIT(2)
49 #define SPRD_RTC_DAY_EN			BIT(3)
50 #define SPRD_RTC_ALARM_EN		BIT(4)
51 #define SPRD_RTC_HRS_FORMAT_EN		BIT(5)
52 #define SPRD_RTC_AUXALM_EN		BIT(6)
53 #define SPRD_RTC_SPG_UPD_EN		BIT(7)
54 #define SPRD_RTC_SEC_UPD_EN		BIT(8)
55 #define SPRD_RTC_MIN_UPD_EN		BIT(9)
56 #define SPRD_RTC_HOUR_UPD_EN		BIT(10)
57 #define SPRD_RTC_DAY_UPD_EN		BIT(11)
58 #define SPRD_RTC_ALMSEC_UPD_EN		BIT(12)
59 #define SPRD_RTC_ALMMIN_UPD_EN		BIT(13)
60 #define SPRD_RTC_ALMHOUR_UPD_EN		BIT(14)
61 #define SPRD_RTC_ALMDAY_UPD_EN		BIT(15)
62 #define SPRD_RTC_INT_MASK		GENMASK(15, 0)
63 
64 #define SPRD_RTC_TIME_INT_MASK				\
65 	(SPRD_RTC_SEC_UPD_EN | SPRD_RTC_MIN_UPD_EN |	\
66 	 SPRD_RTC_HOUR_UPD_EN | SPRD_RTC_DAY_UPD_EN)
67 
68 #define SPRD_RTC_ALMTIME_INT_MASK				\
69 	(SPRD_RTC_ALMSEC_UPD_EN | SPRD_RTC_ALMMIN_UPD_EN |	\
70 	 SPRD_RTC_ALMHOUR_UPD_EN | SPRD_RTC_ALMDAY_UPD_EN)
71 
72 #define SPRD_RTC_ALM_INT_MASK			\
73 	(SPRD_RTC_SEC_EN | SPRD_RTC_MIN_EN |	\
74 	 SPRD_RTC_HOUR_EN | SPRD_RTC_DAY_EN |	\
75 	 SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN)
76 
77 /* second/minute/hour/day values mask definition */
78 #define SPRD_RTC_SEC_MASK		GENMASK(5, 0)
79 #define SPRD_RTC_MIN_MASK		GENMASK(5, 0)
80 #define SPRD_RTC_HOUR_MASK		GENMASK(4, 0)
81 #define SPRD_RTC_DAY_MASK		GENMASK(15, 0)
82 
83 /* alarm lock definition for SPRD_RTC_SPG_UPD register */
84 #define SPRD_RTC_ALMLOCK_MASK		GENMASK(7, 0)
85 #define SPRD_RTC_ALM_UNLOCK		0xa5
86 #define SPRD_RTC_ALM_LOCK		(~SPRD_RTC_ALM_UNLOCK &	\
87 					 SPRD_RTC_ALMLOCK_MASK)
88 
89 /* SPG values definition for SPRD_RTC_SPG_UPD register */
90 #define SPRD_RTC_POWEROFF_ALM_FLAG	BIT(8)
91 
92 /* power control/status definition */
93 #define SPRD_RTC_POWER_RESET_VALUE	0x96
94 #define SPRD_RTC_POWER_STS_CLEAR	GENMASK(7, 0)
95 #define SPRD_RTC_POWER_STS_SHIFT	8
96 #define SPRD_RTC_POWER_STS_VALID	\
97 	(~SPRD_RTC_POWER_RESET_VALUE << SPRD_RTC_POWER_STS_SHIFT)
98 
99 /* timeout of synchronizing time and alarm registers (us) */
100 #define SPRD_RTC_POLL_TIMEOUT		200000
101 #define SPRD_RTC_POLL_DELAY_US		20000
102 
103 struct sprd_rtc {
104 	struct rtc_device	*rtc;
105 	struct regmap		*regmap;
106 	struct device		*dev;
107 	u32			base;
108 	int			irq;
109 	bool			valid;
110 };
111 
112 /*
113  * The Spreadtrum RTC controller has 3 groups registers, including time, normal
114  * alarm and auxiliary alarm. The time group registers are used to set RTC time,
115  * the normal alarm registers are used to set normal alarm, and the auxiliary
116  * alarm registers are used to set auxiliary alarm. Both alarm event and
117  * auxiliary alarm event can wake up system from deep sleep, but only alarm
118  * event can power up system from power down status.
119  */
120 enum sprd_rtc_reg_types {
121 	SPRD_RTC_TIME,
122 	SPRD_RTC_ALARM,
123 	SPRD_RTC_AUX_ALARM,
124 };
125 
126 static int sprd_rtc_clear_alarm_ints(struct sprd_rtc *rtc)
127 {
128 	return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
129 			    SPRD_RTC_ALM_INT_MASK);
130 }
131 
132 static int sprd_rtc_disable_ints(struct sprd_rtc *rtc)
133 {
134 	int ret;
135 
136 	ret = regmap_update_bits(rtc->regmap, rtc->base + SPRD_RTC_INT_EN,
137 				 SPRD_RTC_INT_MASK, 0);
138 	if (ret)
139 		return ret;
140 
141 	return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
142 			    SPRD_RTC_INT_MASK);
143 }
144 
145 static int sprd_rtc_lock_alarm(struct sprd_rtc *rtc, bool lock)
146 {
147 	int ret;
148 	u32 val;
149 
150 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_SPG_VALUE, &val);
151 	if (ret)
152 		return ret;
153 
154 	val &= ~(SPRD_RTC_ALMLOCK_MASK | SPRD_RTC_POWEROFF_ALM_FLAG);
155 	if (lock)
156 		val |= SPRD_RTC_ALM_LOCK;
157 	else
158 		val |= SPRD_RTC_ALM_UNLOCK | SPRD_RTC_POWEROFF_ALM_FLAG;
159 
160 	ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_SPG_UPD, val);
161 	if (ret)
162 		return ret;
163 
164 	/* wait until the SPG value is updated successfully */
165 	ret = regmap_read_poll_timeout(rtc->regmap,
166 				       rtc->base + SPRD_RTC_INT_RAW_STS, val,
167 				       (val & SPRD_RTC_SPG_UPD_EN),
168 				       SPRD_RTC_POLL_DELAY_US,
169 				       SPRD_RTC_POLL_TIMEOUT);
170 	if (ret) {
171 		dev_err(rtc->dev, "failed to update SPG value:%d\n", ret);
172 		return ret;
173 	}
174 
175 	return 0;
176 }
177 
178 static int sprd_rtc_get_secs(struct sprd_rtc *rtc, enum sprd_rtc_reg_types type,
179 			     time64_t *secs)
180 {
181 	u32 sec_reg, min_reg, hour_reg, day_reg;
182 	u32 val, sec, min, hour, day;
183 	int ret;
184 
185 	switch (type) {
186 	case SPRD_RTC_TIME:
187 		sec_reg = SPRD_RTC_SEC_CNT_VALUE;
188 		min_reg = SPRD_RTC_MIN_CNT_VALUE;
189 		hour_reg = SPRD_RTC_HOUR_CNT_VALUE;
190 		day_reg = SPRD_RTC_DAY_CNT_VALUE;
191 		break;
192 	case SPRD_RTC_ALARM:
193 		sec_reg = SPRD_RTC_SEC_ALM_VALUE;
194 		min_reg = SPRD_RTC_MIN_ALM_VALUE;
195 		hour_reg = SPRD_RTC_HOUR_ALM_VALUE;
196 		day_reg = SPRD_RTC_DAY_ALM_VALUE;
197 		break;
198 	case SPRD_RTC_AUX_ALARM:
199 		sec_reg = SPRD_RTC_SEC_AUXALM_UPD;
200 		min_reg = SPRD_RTC_MIN_AUXALM_UPD;
201 		hour_reg = SPRD_RTC_HOUR_AUXALM_UPD;
202 		day_reg = SPRD_RTC_DAY_AUXALM_UPD;
203 		break;
204 	default:
205 		return -EINVAL;
206 	}
207 
208 	ret = regmap_read(rtc->regmap, rtc->base + sec_reg, &val);
209 	if (ret)
210 		return ret;
211 
212 	sec = val & SPRD_RTC_SEC_MASK;
213 
214 	ret = regmap_read(rtc->regmap, rtc->base + min_reg, &val);
215 	if (ret)
216 		return ret;
217 
218 	min = val & SPRD_RTC_MIN_MASK;
219 
220 	ret = regmap_read(rtc->regmap, rtc->base + hour_reg, &val);
221 	if (ret)
222 		return ret;
223 
224 	hour = val & SPRD_RTC_HOUR_MASK;
225 
226 	ret = regmap_read(rtc->regmap, rtc->base + day_reg, &val);
227 	if (ret)
228 		return ret;
229 
230 	day = val & SPRD_RTC_DAY_MASK;
231 	*secs = (((time64_t)(day * 24) + hour) * 60 + min) * 60 + sec;
232 	return 0;
233 }
234 
235 static int sprd_rtc_set_secs(struct sprd_rtc *rtc, enum sprd_rtc_reg_types type,
236 			     time64_t secs)
237 {
238 	u32 sec_reg, min_reg, hour_reg, day_reg, sts_mask;
239 	u32 sec, min, hour, day, val;
240 	int ret, rem;
241 
242 	/* convert seconds to RTC time format */
243 	day = div_s64_rem(secs, 86400, &rem);
244 	hour = rem / 3600;
245 	rem -= hour * 3600;
246 	min = rem / 60;
247 	sec = rem - min * 60;
248 
249 	switch (type) {
250 	case SPRD_RTC_TIME:
251 		sec_reg = SPRD_RTC_SEC_CNT_UPD;
252 		min_reg = SPRD_RTC_MIN_CNT_UPD;
253 		hour_reg = SPRD_RTC_HOUR_CNT_UPD;
254 		day_reg = SPRD_RTC_DAY_CNT_UPD;
255 		sts_mask = SPRD_RTC_TIME_INT_MASK;
256 		break;
257 	case SPRD_RTC_ALARM:
258 		sec_reg = SPRD_RTC_SEC_ALM_UPD;
259 		min_reg = SPRD_RTC_MIN_ALM_UPD;
260 		hour_reg = SPRD_RTC_HOUR_ALM_UPD;
261 		day_reg = SPRD_RTC_DAY_ALM_UPD;
262 		sts_mask = SPRD_RTC_ALMTIME_INT_MASK;
263 		break;
264 	case SPRD_RTC_AUX_ALARM:
265 		sec_reg = SPRD_RTC_SEC_AUXALM_UPD;
266 		min_reg = SPRD_RTC_MIN_AUXALM_UPD;
267 		hour_reg = SPRD_RTC_HOUR_AUXALM_UPD;
268 		day_reg = SPRD_RTC_DAY_AUXALM_UPD;
269 		sts_mask = 0;
270 		break;
271 	default:
272 		return -EINVAL;
273 	}
274 
275 	ret = regmap_write(rtc->regmap, rtc->base + sec_reg, sec);
276 	if (ret)
277 		return ret;
278 
279 	ret = regmap_write(rtc->regmap, rtc->base + min_reg, min);
280 	if (ret)
281 		return ret;
282 
283 	ret = regmap_write(rtc->regmap, rtc->base + hour_reg, hour);
284 	if (ret)
285 		return ret;
286 
287 	ret = regmap_write(rtc->regmap, rtc->base + day_reg, day);
288 	if (ret)
289 		return ret;
290 
291 	if (type == SPRD_RTC_AUX_ALARM)
292 		return 0;
293 
294 	/*
295 	 * Since the time and normal alarm registers are put in always-power-on
296 	 * region supplied by VDDRTC, then these registers changing time will
297 	 * be very long, about 125ms. Thus here we should wait until all
298 	 * values are updated successfully.
299 	 */
300 	ret = regmap_read_poll_timeout(rtc->regmap,
301 				       rtc->base + SPRD_RTC_INT_RAW_STS, val,
302 				       ((val & sts_mask) == sts_mask),
303 				       SPRD_RTC_POLL_DELAY_US,
304 				       SPRD_RTC_POLL_TIMEOUT);
305 	if (ret < 0) {
306 		dev_err(rtc->dev, "set time/alarm values timeout\n");
307 		return ret;
308 	}
309 
310 	return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
311 			    sts_mask);
312 }
313 
314 static int sprd_rtc_read_aux_alarm(struct device *dev, struct rtc_wkalrm *alrm)
315 {
316 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
317 	time64_t secs;
318 	u32 val;
319 	int ret;
320 
321 	ret = sprd_rtc_get_secs(rtc, SPRD_RTC_AUX_ALARM, &secs);
322 	if (ret)
323 		return ret;
324 
325 	rtc_time64_to_tm(secs, &alrm->time);
326 
327 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_EN, &val);
328 	if (ret)
329 		return ret;
330 
331 	alrm->enabled = !!(val & SPRD_RTC_AUXALM_EN);
332 
333 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_RAW_STS, &val);
334 	if (ret)
335 		return ret;
336 
337 	alrm->pending = !!(val & SPRD_RTC_AUXALM_EN);
338 	return 0;
339 }
340 
341 static int sprd_rtc_set_aux_alarm(struct device *dev, struct rtc_wkalrm *alrm)
342 {
343 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
344 	time64_t secs = rtc_tm_to_time64(&alrm->time);
345 	int ret;
346 
347 	/* clear the auxiliary alarm interrupt status */
348 	ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
349 			   SPRD_RTC_AUXALM_EN);
350 	if (ret)
351 		return ret;
352 
353 	ret = sprd_rtc_set_secs(rtc, SPRD_RTC_AUX_ALARM, secs);
354 	if (ret)
355 		return ret;
356 
357 	if (alrm->enabled) {
358 		ret = regmap_update_bits(rtc->regmap,
359 					 rtc->base + SPRD_RTC_INT_EN,
360 					 SPRD_RTC_AUXALM_EN,
361 					 SPRD_RTC_AUXALM_EN);
362 	} else {
363 		ret = regmap_update_bits(rtc->regmap,
364 					 rtc->base + SPRD_RTC_INT_EN,
365 					 SPRD_RTC_AUXALM_EN, 0);
366 	}
367 
368 	return ret;
369 }
370 
371 static int sprd_rtc_read_time(struct device *dev, struct rtc_time *tm)
372 {
373 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
374 	time64_t secs;
375 	int ret;
376 
377 	if (!rtc->valid) {
378 		dev_warn(dev, "RTC values are invalid\n");
379 		return -EINVAL;
380 	}
381 
382 	ret = sprd_rtc_get_secs(rtc, SPRD_RTC_TIME, &secs);
383 	if (ret)
384 		return ret;
385 
386 	rtc_time64_to_tm(secs, tm);
387 	return 0;
388 }
389 
390 static int sprd_rtc_set_time(struct device *dev, struct rtc_time *tm)
391 {
392 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
393 	time64_t secs = rtc_tm_to_time64(tm);
394 	int ret;
395 
396 	ret = sprd_rtc_set_secs(rtc, SPRD_RTC_TIME, secs);
397 	if (ret)
398 		return ret;
399 
400 	if (!rtc->valid) {
401 		/* Clear RTC power status firstly */
402 		ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_PWR_CTRL,
403 				   SPRD_RTC_POWER_STS_CLEAR);
404 		if (ret)
405 			return ret;
406 
407 		/*
408 		 * Set RTC power status to indicate now RTC has valid time
409 		 * values.
410 		 */
411 		ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_PWR_CTRL,
412 				   SPRD_RTC_POWER_STS_VALID);
413 		if (ret)
414 			return ret;
415 
416 		rtc->valid = true;
417 	}
418 
419 	return 0;
420 }
421 
422 static int sprd_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
423 {
424 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
425 	time64_t secs;
426 	int ret;
427 	u32 val;
428 
429 	/*
430 	 * If aie_timer is enabled, we should get the normal alarm time.
431 	 * Otherwise we should get auxiliary alarm time.
432 	 */
433 	if (rtc->rtc && rtc->rtc->aie_timer.enabled == 0)
434 		return sprd_rtc_read_aux_alarm(dev, alrm);
435 
436 	ret = sprd_rtc_get_secs(rtc, SPRD_RTC_ALARM, &secs);
437 	if (ret)
438 		return ret;
439 
440 	rtc_time64_to_tm(secs, &alrm->time);
441 
442 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_EN, &val);
443 	if (ret)
444 		return ret;
445 
446 	alrm->enabled = !!(val & SPRD_RTC_ALARM_EN);
447 
448 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_RAW_STS, &val);
449 	if (ret)
450 		return ret;
451 
452 	alrm->pending = !!(val & SPRD_RTC_ALARM_EN);
453 	return 0;
454 }
455 
456 static int sprd_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
457 {
458 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
459 	time64_t secs = rtc_tm_to_time64(&alrm->time);
460 	struct rtc_time aie_time =
461 		rtc_ktime_to_tm(rtc->rtc->aie_timer.node.expires);
462 	int ret;
463 
464 	/*
465 	 * We have 2 groups alarms: normal alarm and auxiliary alarm. Since
466 	 * both normal alarm event and auxiliary alarm event can wake up system
467 	 * from deep sleep, but only alarm event can power up system from power
468 	 * down status. Moreover we do not need to poll about 125ms when
469 	 * updating auxiliary alarm registers. Thus we usually set auxiliary
470 	 * alarm when wake up system from deep sleep, and for other scenarios,
471 	 * we should set normal alarm with polling status.
472 	 *
473 	 * So here we check if the alarm time is set by aie_timer, if yes, we
474 	 * should set normal alarm, if not, we should set auxiliary alarm which
475 	 * means it is just a wake event.
476 	 */
477 	if (!rtc->rtc->aie_timer.enabled || rtc_tm_sub(&aie_time, &alrm->time))
478 		return sprd_rtc_set_aux_alarm(dev, alrm);
479 
480 	/* clear the alarm interrupt status firstly */
481 	ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
482 			   SPRD_RTC_ALARM_EN);
483 	if (ret)
484 		return ret;
485 
486 	ret = sprd_rtc_set_secs(rtc, SPRD_RTC_ALARM, secs);
487 	if (ret)
488 		return ret;
489 
490 	if (alrm->enabled) {
491 		ret = regmap_update_bits(rtc->regmap,
492 					 rtc->base + SPRD_RTC_INT_EN,
493 					 SPRD_RTC_ALARM_EN,
494 					 SPRD_RTC_ALARM_EN);
495 		if (ret)
496 			return ret;
497 
498 		/* unlock the alarm to enable the alarm function. */
499 		ret = sprd_rtc_lock_alarm(rtc, false);
500 	} else {
501 		regmap_update_bits(rtc->regmap,
502 				   rtc->base + SPRD_RTC_INT_EN,
503 				   SPRD_RTC_ALARM_EN, 0);
504 
505 		/*
506 		 * Lock the alarm function in case fake alarm event will power
507 		 * up systems.
508 		 */
509 		ret = sprd_rtc_lock_alarm(rtc, true);
510 	}
511 
512 	return ret;
513 }
514 
515 static int sprd_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
516 {
517 	struct sprd_rtc *rtc = dev_get_drvdata(dev);
518 	int ret;
519 
520 	if (enabled) {
521 		ret = regmap_update_bits(rtc->regmap,
522 					 rtc->base + SPRD_RTC_INT_EN,
523 					 SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN,
524 					 SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN);
525 		if (ret)
526 			return ret;
527 
528 		ret = sprd_rtc_lock_alarm(rtc, false);
529 	} else {
530 		regmap_update_bits(rtc->regmap, rtc->base + SPRD_RTC_INT_EN,
531 				   SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN, 0);
532 
533 		ret = sprd_rtc_lock_alarm(rtc, true);
534 	}
535 
536 	return ret;
537 }
538 
539 static const struct rtc_class_ops sprd_rtc_ops = {
540 	.read_time = sprd_rtc_read_time,
541 	.set_time = sprd_rtc_set_time,
542 	.read_alarm = sprd_rtc_read_alarm,
543 	.set_alarm = sprd_rtc_set_alarm,
544 	.alarm_irq_enable = sprd_rtc_alarm_irq_enable,
545 };
546 
547 static irqreturn_t sprd_rtc_handler(int irq, void *dev_id)
548 {
549 	struct sprd_rtc *rtc = dev_id;
550 	int ret;
551 
552 	ret = sprd_rtc_clear_alarm_ints(rtc);
553 	if (ret)
554 		return IRQ_RETVAL(ret);
555 
556 	rtc_update_irq(rtc->rtc, 1, RTC_AF | RTC_IRQF);
557 	return IRQ_HANDLED;
558 }
559 
560 static int sprd_rtc_check_power_down(struct sprd_rtc *rtc)
561 {
562 	u32 val;
563 	int ret;
564 
565 	ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_PWR_STS, &val);
566 	if (ret)
567 		return ret;
568 
569 	/*
570 	 * If the RTC power status value is SPRD_RTC_POWER_RESET_VALUE, which
571 	 * means the RTC has been powered down, so the RTC time values are
572 	 * invalid.
573 	 */
574 	rtc->valid = val == SPRD_RTC_POWER_RESET_VALUE ? false : true;
575 	return 0;
576 }
577 
578 static int sprd_rtc_probe(struct platform_device *pdev)
579 {
580 	struct device_node *node = pdev->dev.of_node;
581 	struct sprd_rtc *rtc;
582 	int ret;
583 
584 	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
585 	if (!rtc)
586 		return -ENOMEM;
587 
588 	rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
589 	if (!rtc->regmap)
590 		return -ENODEV;
591 
592 	ret = of_property_read_u32(node, "reg", &rtc->base);
593 	if (ret) {
594 		dev_err(&pdev->dev, "failed to get RTC base address\n");
595 		return ret;
596 	}
597 
598 	rtc->irq = platform_get_irq(pdev, 0);
599 	if (rtc->irq < 0) {
600 		dev_err(&pdev->dev, "failed to get RTC irq number\n");
601 		return rtc->irq;
602 	}
603 
604 	rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
605 	if (IS_ERR(rtc->rtc))
606 		return PTR_ERR(rtc->rtc);
607 
608 	rtc->dev = &pdev->dev;
609 	platform_set_drvdata(pdev, rtc);
610 
611 	/* clear all RTC interrupts and disable all RTC interrupts */
612 	ret = sprd_rtc_disable_ints(rtc);
613 	if (ret) {
614 		dev_err(&pdev->dev, "failed to disable RTC interrupts\n");
615 		return ret;
616 	}
617 
618 	/* check if RTC time values are valid */
619 	ret = sprd_rtc_check_power_down(rtc);
620 	if (ret) {
621 		dev_err(&pdev->dev, "failed to check RTC time values\n");
622 		return ret;
623 	}
624 
625 	ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
626 					sprd_rtc_handler,
627 					IRQF_ONESHOT | IRQF_EARLY_RESUME,
628 					pdev->name, rtc);
629 	if (ret < 0) {
630 		dev_err(&pdev->dev, "failed to request RTC irq\n");
631 		return ret;
632 	}
633 
634 	rtc->rtc->ops = &sprd_rtc_ops;
635 	rtc->rtc->range_min = 0;
636 	rtc->rtc->range_max = 5662310399LL;
637 	ret = rtc_register_device(rtc->rtc);
638 	if (ret) {
639 		dev_err(&pdev->dev, "failed to register rtc device\n");
640 		return ret;
641 	}
642 
643 	device_init_wakeup(&pdev->dev, 1);
644 	return 0;
645 }
646 
647 static int sprd_rtc_remove(struct platform_device *pdev)
648 {
649 	device_init_wakeup(&pdev->dev, 0);
650 	return 0;
651 }
652 
653 static const struct of_device_id sprd_rtc_of_match[] = {
654 	{ .compatible = "sprd,sc2731-rtc", },
655 	{ },
656 };
657 MODULE_DEVICE_TABLE(of, sprd_rtc_of_match);
658 
659 static struct platform_driver sprd_rtc_driver = {
660 	.driver = {
661 		.name = "sprd-rtc",
662 		.of_match_table = sprd_rtc_of_match,
663 	},
664 	.probe	= sprd_rtc_probe,
665 	.remove = sprd_rtc_remove,
666 };
667 module_platform_driver(sprd_rtc_driver);
668 
669 MODULE_LICENSE("GPL v2");
670 MODULE_DESCRIPTION("Spreadtrum RTC Device Driver");
671 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
672