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_SEC_AUXALM_UPD 0x60
39 #define SPRD_RTC_MIN_AUXALM_UPD 0x64
40 #define SPRD_RTC_HOUR_AUXALM_UPD 0x68
41 #define SPRD_RTC_DAY_AUXALM_UPD 0x6c
43 /* BIT & MASK definition for SPRD_RTC_INT_* registers */
44 #define SPRD_RTC_SEC_EN BIT(0)
45 #define SPRD_RTC_MIN_EN BIT(1)
46 #define SPRD_RTC_HOUR_EN BIT(2)
47 #define SPRD_RTC_DAY_EN BIT(3)
48 #define SPRD_RTC_ALARM_EN BIT(4)
49 #define SPRD_RTC_HRS_FORMAT_EN BIT(5)
50 #define SPRD_RTC_AUXALM_EN BIT(6)
51 #define SPRD_RTC_SPG_UPD_EN BIT(7)
52 #define SPRD_RTC_SEC_UPD_EN BIT(8)
53 #define SPRD_RTC_MIN_UPD_EN BIT(9)
54 #define SPRD_RTC_HOUR_UPD_EN BIT(10)
55 #define SPRD_RTC_DAY_UPD_EN BIT(11)
56 #define SPRD_RTC_ALMSEC_UPD_EN BIT(12)
57 #define SPRD_RTC_ALMMIN_UPD_EN BIT(13)
58 #define SPRD_RTC_ALMHOUR_UPD_EN BIT(14)
59 #define SPRD_RTC_ALMDAY_UPD_EN BIT(15)
60 #define SPRD_RTC_INT_MASK GENMASK(15, 0)
62 #define SPRD_RTC_TIME_INT_MASK \
63 (SPRD_RTC_SEC_UPD_EN | SPRD_RTC_MIN_UPD_EN | \
64 SPRD_RTC_HOUR_UPD_EN | SPRD_RTC_DAY_UPD_EN)
66 #define SPRD_RTC_ALMTIME_INT_MASK \
67 (SPRD_RTC_ALMSEC_UPD_EN | SPRD_RTC_ALMMIN_UPD_EN | \
68 SPRD_RTC_ALMHOUR_UPD_EN | SPRD_RTC_ALMDAY_UPD_EN)
70 #define SPRD_RTC_ALM_INT_MASK \
71 (SPRD_RTC_SEC_EN | SPRD_RTC_MIN_EN | \
72 SPRD_RTC_HOUR_EN | SPRD_RTC_DAY_EN | \
73 SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN)
75 /* second/minute/hour/day values mask definition */
76 #define SPRD_RTC_SEC_MASK GENMASK(5, 0)
77 #define SPRD_RTC_MIN_MASK GENMASK(5, 0)
78 #define SPRD_RTC_HOUR_MASK GENMASK(4, 0)
79 #define SPRD_RTC_DAY_MASK GENMASK(15, 0)
81 /* alarm lock definition for SPRD_RTC_SPG_UPD register */
82 #define SPRD_RTC_ALMLOCK_MASK GENMASK(7, 0)
83 #define SPRD_RTC_ALM_UNLOCK 0xa5
84 #define SPRD_RTC_ALM_LOCK (~SPRD_RTC_ALM_UNLOCK & \
85 SPRD_RTC_ALMLOCK_MASK)
87 /* SPG values definition for SPRD_RTC_SPG_UPD register */
88 #define SPRD_RTC_POWEROFF_ALM_FLAG BIT(8)
89 #define SPRD_RTC_POWER_RESET_FLAG BIT(9)
91 /* timeout of synchronizing time and alarm registers (us) */
92 #define SPRD_RTC_POLL_TIMEOUT 200000
93 #define SPRD_RTC_POLL_DELAY_US 20000
96 struct rtc_device
*rtc
;
97 struct regmap
*regmap
;
105 * The Spreadtrum RTC controller has 3 groups registers, including time, normal
106 * alarm and auxiliary alarm. The time group registers are used to set RTC time,
107 * the normal alarm registers are used to set normal alarm, and the auxiliary
108 * alarm registers are used to set auxiliary alarm. Both alarm event and
109 * auxiliary alarm event can wake up system from deep sleep, but only alarm
110 * event can power up system from power down status.
112 enum sprd_rtc_reg_types
{
118 static int sprd_rtc_clear_alarm_ints(struct sprd_rtc
*rtc
)
120 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
121 SPRD_RTC_ALM_INT_MASK
);
124 static int sprd_rtc_disable_ints(struct sprd_rtc
*rtc
)
128 ret
= regmap_update_bits(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
,
129 SPRD_RTC_INT_MASK
, 0);
133 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
137 static int sprd_rtc_lock_alarm(struct sprd_rtc
*rtc
, bool lock
)
142 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_SPG_VALUE
, &val
);
146 val
&= ~(SPRD_RTC_ALMLOCK_MASK
| SPRD_RTC_POWEROFF_ALM_FLAG
);
148 val
|= SPRD_RTC_ALM_LOCK
;
150 val
|= SPRD_RTC_ALM_UNLOCK
| SPRD_RTC_POWEROFF_ALM_FLAG
;
152 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_SPG_UPD
, val
);
156 /* wait until the SPG value is updated successfully */
157 ret
= regmap_read_poll_timeout(rtc
->regmap
,
158 rtc
->base
+ SPRD_RTC_INT_RAW_STS
, val
,
159 (val
& SPRD_RTC_SPG_UPD_EN
),
160 SPRD_RTC_POLL_DELAY_US
,
161 SPRD_RTC_POLL_TIMEOUT
);
163 dev_err(rtc
->dev
, "failed to update SPG value:%d\n", ret
);
170 static int sprd_rtc_get_secs(struct sprd_rtc
*rtc
, enum sprd_rtc_reg_types type
,
173 u32 sec_reg
, min_reg
, hour_reg
, day_reg
;
174 u32 val
, sec
, min
, hour
, day
;
179 sec_reg
= SPRD_RTC_SEC_CNT_VALUE
;
180 min_reg
= SPRD_RTC_MIN_CNT_VALUE
;
181 hour_reg
= SPRD_RTC_HOUR_CNT_VALUE
;
182 day_reg
= SPRD_RTC_DAY_CNT_VALUE
;
185 sec_reg
= SPRD_RTC_SEC_ALM_VALUE
;
186 min_reg
= SPRD_RTC_MIN_ALM_VALUE
;
187 hour_reg
= SPRD_RTC_HOUR_ALM_VALUE
;
188 day_reg
= SPRD_RTC_DAY_ALM_VALUE
;
190 case SPRD_RTC_AUX_ALARM
:
191 sec_reg
= SPRD_RTC_SEC_AUXALM_UPD
;
192 min_reg
= SPRD_RTC_MIN_AUXALM_UPD
;
193 hour_reg
= SPRD_RTC_HOUR_AUXALM_UPD
;
194 day_reg
= SPRD_RTC_DAY_AUXALM_UPD
;
200 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ sec_reg
, &val
);
204 sec
= val
& SPRD_RTC_SEC_MASK
;
206 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ min_reg
, &val
);
210 min
= val
& SPRD_RTC_MIN_MASK
;
212 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ hour_reg
, &val
);
216 hour
= val
& SPRD_RTC_HOUR_MASK
;
218 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ day_reg
, &val
);
222 day
= val
& SPRD_RTC_DAY_MASK
;
223 *secs
= (((time64_t
)(day
* 24) + hour
) * 60 + min
) * 60 + sec
;
227 static int sprd_rtc_set_secs(struct sprd_rtc
*rtc
, enum sprd_rtc_reg_types type
,
230 u32 sec_reg
, min_reg
, hour_reg
, day_reg
, sts_mask
;
231 u32 sec
, min
, hour
, day
, val
;
234 /* convert seconds to RTC time format */
235 day
= div_s64_rem(secs
, 86400, &rem
);
239 sec
= rem
- min
* 60;
243 sec_reg
= SPRD_RTC_SEC_CNT_UPD
;
244 min_reg
= SPRD_RTC_MIN_CNT_UPD
;
245 hour_reg
= SPRD_RTC_HOUR_CNT_UPD
;
246 day_reg
= SPRD_RTC_DAY_CNT_UPD
;
247 sts_mask
= SPRD_RTC_TIME_INT_MASK
;
250 sec_reg
= SPRD_RTC_SEC_ALM_UPD
;
251 min_reg
= SPRD_RTC_MIN_ALM_UPD
;
252 hour_reg
= SPRD_RTC_HOUR_ALM_UPD
;
253 day_reg
= SPRD_RTC_DAY_ALM_UPD
;
254 sts_mask
= SPRD_RTC_ALMTIME_INT_MASK
;
256 case SPRD_RTC_AUX_ALARM
:
257 sec_reg
= SPRD_RTC_SEC_AUXALM_UPD
;
258 min_reg
= SPRD_RTC_MIN_AUXALM_UPD
;
259 hour_reg
= SPRD_RTC_HOUR_AUXALM_UPD
;
260 day_reg
= SPRD_RTC_DAY_AUXALM_UPD
;
267 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ sec_reg
, sec
);
271 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ min_reg
, min
);
275 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ hour_reg
, hour
);
279 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ day_reg
, day
);
283 if (type
== SPRD_RTC_AUX_ALARM
)
287 * Since the time and normal alarm registers are put in always-power-on
288 * region supplied by VDDRTC, then these registers changing time will
289 * be very long, about 125ms. Thus here we should wait until all
290 * values are updated successfully.
292 ret
= regmap_read_poll_timeout(rtc
->regmap
,
293 rtc
->base
+ SPRD_RTC_INT_RAW_STS
, val
,
294 ((val
& sts_mask
) == sts_mask
),
295 SPRD_RTC_POLL_DELAY_US
,
296 SPRD_RTC_POLL_TIMEOUT
);
298 dev_err(rtc
->dev
, "set time/alarm values timeout\n");
302 return regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
306 static int sprd_rtc_read_aux_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
308 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
313 ret
= sprd_rtc_get_secs(rtc
, SPRD_RTC_AUX_ALARM
, &secs
);
317 rtc_time64_to_tm(secs
, &alrm
->time
);
319 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_EN
, &val
);
323 alrm
->enabled
= !!(val
& SPRD_RTC_AUXALM_EN
);
325 ret
= regmap_read(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_RAW_STS
, &val
);
329 alrm
->pending
= !!(val
& SPRD_RTC_AUXALM_EN
);
333 static int sprd_rtc_set_aux_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
335 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
336 time64_t secs
= rtc_tm_to_time64(&alrm
->time
);
339 /* clear the auxiliary alarm interrupt status */
340 ret
= regmap_write(rtc
->regmap
, rtc
->base
+ SPRD_RTC_INT_CLR
,
345 ret
= sprd_rtc_set_secs(rtc
, SPRD_RTC_AUX_ALARM
, secs
);
350 ret
= regmap_update_bits(rtc
->regmap
,
351 rtc
->base
+ SPRD_RTC_INT_EN
,
355 ret
= regmap_update_bits(rtc
->regmap
,
356 rtc
->base
+ SPRD_RTC_INT_EN
,
357 SPRD_RTC_AUXALM_EN
, 0);
363 static int sprd_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
365 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
370 dev_warn(dev
, "RTC values are invalid\n");
374 ret
= sprd_rtc_get_secs(rtc
, SPRD_RTC_TIME
, &secs
);
378 rtc_time64_to_tm(secs
, tm
);
382 static int sprd_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
384 struct sprd_rtc
*rtc
= dev_get_drvdata(dev
);
385 time64_t secs
= rtc_tm_to_time64(tm
);
389 ret
= sprd_rtc_set_secs(rtc
, SPRD_RTC_TIME
, secs
);
395 * Set SPRD_RTC_POWER_RESET_FLAG to indicate now RTC has valid
398 ret
= regmap_update_bits(rtc
->regmap
,
399 rtc
->base
+ SPRD_RTC_SPG_UPD
,
400 SPRD_RTC_POWER_RESET_FLAG
,
401 SPRD_RTC_POWER_RESET_FLAG
);
405 ret
= regmap_read_poll_timeout(rtc
->regmap
,
406 rtc
->base
+ SPRD_RTC_INT_RAW_STS
,
407 val
, (val
& SPRD_RTC_SPG_UPD_EN
),
408 SPRD_RTC_POLL_DELAY_US
,
409 SPRD_RTC_POLL_TIMEOUT
);
411 dev_err(rtc
->dev
, "failed to update SPG value:%d\n",
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_SPG_VALUE
, &val
);
570 * If the SPRD_RTC_POWER_RESET_FLAG was not set, which means the RTC has
571 * been powered down, so the RTC time values are invalid.
573 rtc
->valid
= (val
& SPRD_RTC_POWER_RESET_FLAG
) ? true : false;
577 static int sprd_rtc_probe(struct platform_device
*pdev
)
579 struct device_node
*node
= pdev
->dev
.of_node
;
580 struct sprd_rtc
*rtc
;
583 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
587 rtc
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
591 ret
= of_property_read_u32(node
, "reg", &rtc
->base
);
593 dev_err(&pdev
->dev
, "failed to get RTC base address\n");
597 rtc
->irq
= platform_get_irq(pdev
, 0);
599 dev_err(&pdev
->dev
, "failed to get RTC irq number\n");
603 rtc
->dev
= &pdev
->dev
;
604 platform_set_drvdata(pdev
, rtc
);
606 /* clear all RTC interrupts and disable all RTC interrupts */
607 ret
= sprd_rtc_disable_ints(rtc
);
609 dev_err(&pdev
->dev
, "failed to disable RTC interrupts\n");
613 /* check if RTC time values are valid */
614 ret
= sprd_rtc_check_power_down(rtc
);
616 dev_err(&pdev
->dev
, "failed to check RTC time values\n");
620 ret
= devm_request_threaded_irq(&pdev
->dev
, rtc
->irq
, NULL
,
622 IRQF_ONESHOT
| IRQF_EARLY_RESUME
,
625 dev_err(&pdev
->dev
, "failed to request RTC irq\n");
629 rtc
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
630 &sprd_rtc_ops
, THIS_MODULE
);
631 if (IS_ERR(rtc
->rtc
))
632 return PTR_ERR(rtc
->rtc
);
634 device_init_wakeup(&pdev
->dev
, 1);
638 static int sprd_rtc_remove(struct platform_device
*pdev
)
640 device_init_wakeup(&pdev
->dev
, 0);
644 static const struct of_device_id sprd_rtc_of_match
[] = {
645 { .compatible
= "sprd,sc2731-rtc", },
648 MODULE_DEVICE_TABLE(of
, sprd_rtc_of_match
);
650 static struct platform_driver sprd_rtc_driver
= {
653 .of_match_table
= sprd_rtc_of_match
,
655 .probe
= sprd_rtc_probe
,
656 .remove
= sprd_rtc_remove
,
658 module_platform_driver(sprd_rtc_driver
);
660 MODULE_LICENSE("GPL v2");
661 MODULE_DESCRIPTION("Spreadtrum RTC Device Driver");
662 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");