2 * Copyright (C) 2017 Spreadtrum Communications Inc.
4 * SPDX-License-Identifier: GPL-2.0
7 #include <linux/bitops.h>
8 #include <linux/delay.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
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
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)
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)
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)
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)
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)
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)
89 /* SPG values definition for SPRD_RTC_SPG_UPD register */
90 #define SPRD_RTC_POWEROFF_ALM_FLAG BIT(8)
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)
99 /* timeout of synchronizing time and alarm registers (us) */
100 #define SPRD_RTC_POLL_TIMEOUT 200000
101 #define SPRD_RTC_POLL_DELAY_US 20000
104 struct rtc_device
*rtc
;
105 struct regmap
*regmap
;
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.
120 enum sprd_rtc_reg_types
{
126 static int sprd_rtc_clear_alarm_ints(struct sprd_rtc
*rtc
)
128 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
129 SPRD_RTC_ALM_INT_MASK
);
132 static int sprd_rtc_disable_ints(struct sprd_rtc
*rtc
)
136 ret
= regmap_update_bits(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
,
137 SPRD_RTC_INT_MASK
, 0);
141 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
145 static int sprd_rtc_lock_alarm(struct sprd_rtc
*rtc
, bool lock
)
150 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_SPG_VALUE
, &val
);
154 val
&= ~(SPRD_RTC_ALMLOCK_MASK
| SPRD_RTC_POWEROFF_ALM_FLAG
);
156 val
|= SPRD_RTC_ALM_LOCK
;
158 val
|= SPRD_RTC_ALM_UNLOCK
| SPRD_RTC_POWEROFF_ALM_FLAG
;
160 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_SPG_UPD
, val
);
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
);
171 dev_err(rtc
->dev
, "failed to update SPG value:%d\n", ret
);
178 static int sprd_rtc_get_secs(struct sprd_rtc
*rtc
, enum sprd_rtc_reg_types type
,
181 u32 sec_reg
, min_reg
, hour_reg
, day_reg
;
182 u32 val
, sec
, min
, hour
, day
;
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
;
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
;
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
;
208 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ sec_reg
, &val
);
212 sec
= val
& SPRD_RTC_SEC_MASK
;
214 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ min_reg
, &val
);
218 min
= val
& SPRD_RTC_MIN_MASK
;
220 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ hour_reg
, &val
);
224 hour
= val
& SPRD_RTC_HOUR_MASK
;
226 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ day_reg
, &val
);
230 day
= val
& SPRD_RTC_DAY_MASK
;
231 *secs
= (((time64_t
)(day
* 24) + hour
) * 60 + min
) * 60 + sec
;
235 static int sprd_rtc_set_secs(struct sprd_rtc
*rtc
, enum sprd_rtc_reg_types type
,
238 u32 sec_reg
, min_reg
, hour_reg
, day_reg
, sts_mask
;
239 u32 sec
, min
, hour
, day
, val
;
242 /* convert seconds to RTC time format */
243 day
= div_s64_rem(secs
, 86400, &rem
);
247 sec
= rem
- min
* 60;
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
;
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
;
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
;
275 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ sec_reg
, sec
);
279 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ min_reg
, min
);
283 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ hour_reg
, hour
);
287 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ day_reg
, day
);
291 if (type
== SPRD_RTC_AUX_ALARM
)
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.
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
);
306 dev_err(rtc
->dev
, "set time/alarm values timeout\n");
310 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
314 static int sprd_rtc_read_aux_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
316 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
321 ret
= sprd_rtc_get_secs(rtc
, SPRD_RTC_AUX_ALARM
, &secs
);
325 rtc_time64_to_tm(secs
, &alrm
->time
);
327 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
, &val
);
331 alrm
->enabled
= !!(val
& SPRD_RTC_AUXALM_EN
);
333 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_RAW_STS
, &val
);
337 alrm
->pending
= !!(val
& SPRD_RTC_AUXALM_EN
);
341 static int sprd_rtc_set_aux_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
343 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
344 time64_t secs
= rtc_tm_to_time64(&alrm
->time
);
347 /* clear the auxiliary alarm interrupt status */
348 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
353 ret
= sprd_rtc_set_secs(rtc
, SPRD_RTC_AUX_ALARM
, secs
);
358 ret
= regmap_update_bits(rtc
->regmap
,
359 rtc
->base
+ SPRD_RTC_INT_EN
,
363 ret
= regmap_update_bits(rtc
->regmap
,
364 rtc
->base
+ SPRD_RTC_INT_EN
,
365 SPRD_RTC_AUXALM_EN
, 0);
371 static int sprd_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
373 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
378 dev_warn(dev
, "RTC values are invalid\n");
382 ret
= sprd_rtc_get_secs(rtc
, SPRD_RTC_TIME
, &secs
);
386 rtc_time64_to_tm(secs
, tm
);
390 static int sprd_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
392 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
393 time64_t secs
= rtc_tm_to_time64(tm
);
396 ret
= sprd_rtc_set_secs(rtc
, SPRD_RTC_TIME
, secs
);
401 /* Clear RTC power status firstly */
402 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_PWR_CTRL
,
403 SPRD_RTC_POWER_STS_CLEAR
);
408 * Set RTC power status to indicate now RTC has valid time
411 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_PWR_CTRL
,
412 SPRD_RTC_POWER_STS_VALID
);
422 static int sprd_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
424 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
430 * If aie_timer is enabled, we should get the normal alarm time.
431 * Otherwise we should get auxiliary alarm time.
433 if (rtc
->rtc
&& rtc
->rtc
->aie_timer
.enabled
== 0)
434 return sprd_rtc_read_aux_alarm(dev
, alrm
);
436 ret
= sprd_rtc_get_secs(rtc
, SPRD_RTC_ALARM
, &secs
);
440 rtc_time64_to_tm(secs
, &alrm
->time
);
442 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
, &val
);
446 alrm
->enabled
= !!(val
& SPRD_RTC_ALARM_EN
);
448 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_RAW_STS
, &val
);
452 alrm
->pending
= !!(val
& SPRD_RTC_ALARM_EN
);
456 static int sprd_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
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
);
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.
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.
477 if (!rtc
->rtc
->aie_timer
.enabled
|| rtc_tm_sub(&aie_time
, &alrm
->time
))
478 return sprd_rtc_set_aux_alarm(dev
, alrm
);
480 /* clear the alarm interrupt status firstly */
481 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
486 ret
= sprd_rtc_set_secs(rtc
, SPRD_RTC_ALARM
, secs
);
491 ret
= regmap_update_bits(rtc
->regmap
,
492 rtc
->base
+ SPRD_RTC_INT_EN
,
498 /* unlock the alarm to enable the alarm function. */
499 ret
= sprd_rtc_lock_alarm(rtc
, false);
501 regmap_update_bits(rtc
->regmap
,
502 rtc
->base
+ SPRD_RTC_INT_EN
,
503 SPRD_RTC_ALARM_EN
, 0);
506 * Lock the alarm function in case fake alarm event will power
509 ret
= sprd_rtc_lock_alarm(rtc
, true);
515 static int sprd_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
517 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
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
);
528 ret
= sprd_rtc_lock_alarm(rtc
, false);
530 regmap_update_bits(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
,
531 SPRD_RTC_ALARM_EN
| SPRD_RTC_AUXALM_EN
, 0);
533 ret
= sprd_rtc_lock_alarm(rtc
, true);
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
,
547 static irqreturn_t
sprd_rtc_handler(int irq
, void *dev_id
)
549 struct sprd_rtc
*rtc
= dev_id
;
552 ret
= sprd_rtc_clear_alarm_ints(rtc
);
554 return IRQ_RETVAL(ret
);
556 rtc_update_irq(rtc
->rtc
, 1, RTC_AF
| RTC_IRQF
);
560 static int sprd_rtc_check_power_down(struct sprd_rtc
*rtc
)
565 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_PWR_STS
, &val
);
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
574 rtc
->valid
= val
== SPRD_RTC_POWER_RESET_VALUE
? false : true;
578 static int sprd_rtc_probe(struct platform_device
*pdev
)
580 struct device_node
*node
= pdev
->dev
.of_node
;
581 struct sprd_rtc
*rtc
;
584 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
588 rtc
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
592 ret
= of_property_read_u32(node
, "reg", &rtc
->base
);
594 dev_err(&pdev
->dev
, "failed to get RTC base address\n");
598 rtc
->irq
= platform_get_irq(pdev
, 0);
600 dev_err(&pdev
->dev
, "failed to get RTC irq number\n");
604 rtc
->rtc
= devm_rtc_allocate_device(&pdev
->dev
);
605 if (IS_ERR(rtc
->rtc
))
606 return PTR_ERR(rtc
->rtc
);
608 rtc
->dev
= &pdev
->dev
;
609 platform_set_drvdata(pdev
, rtc
);
611 /* clear all RTC interrupts and disable all RTC interrupts */
612 ret
= sprd_rtc_disable_ints(rtc
);
614 dev_err(&pdev
->dev
, "failed to disable RTC interrupts\n");
618 /* check if RTC time values are valid */
619 ret
= sprd_rtc_check_power_down(rtc
);
621 dev_err(&pdev
->dev
, "failed to check RTC time values\n");
625 ret
= devm_request_threaded_irq(&pdev
->dev
, rtc
->irq
, NULL
,
627 IRQF_ONESHOT
| IRQF_EARLY_RESUME
,
630 dev_err(&pdev
->dev
, "failed to request RTC irq\n");
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
);
639 dev_err(&pdev
->dev
, "failed to register rtc device\n");
643 device_init_wakeup(&pdev
->dev
, 1);
647 static int sprd_rtc_remove(struct platform_device
*pdev
)
649 device_init_wakeup(&pdev
->dev
, 0);
653 static const struct of_device_id sprd_rtc_of_match
[] = {
654 { .compatible
= "sprd,sc2731-rtc", },
657 MODULE_DEVICE_TABLE(of
, sprd_rtc_of_match
);
659 static struct platform_driver sprd_rtc_driver
= {
662 .of_match_table
= sprd_rtc_of_match
,
664 .probe
= sprd_rtc_probe
,
665 .remove
= sprd_rtc_remove
,
667 module_platform_driver(sprd_rtc_driver
);
669 MODULE_LICENSE("GPL v2");
670 MODULE_DESCRIPTION("Spreadtrum RTC Device Driver");
671 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");