1 // SPDX-License-Identifier: GPL-2.0
3 * On-Chip RTC Support available on RZ/G3S SoC
5 * Copyright (C) 2024 Renesas Electronics Corp.
8 #include <linux/bitfield.h>
9 #include <linux/cleanup.h>
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/iopoll.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/reset.h>
20 #include <linux/rtc.h>
22 /* Counter registers. */
23 #define RTCA3_RSECCNT 0x2
24 #define RTCA3_RSECCNT_SEC GENMASK(6, 0)
25 #define RTCA3_RMINCNT 0x4
26 #define RTCA3_RMINCNT_MIN GENMASK(6, 0)
27 #define RTCA3_RHRCNT 0x6
28 #define RTCA3_RHRCNT_HR GENMASK(5, 0)
29 #define RTCA3_RHRCNT_PM BIT(6)
30 #define RTCA3_RWKCNT 0x8
31 #define RTCA3_RWKCNT_WK GENMASK(2, 0)
32 #define RTCA3_RDAYCNT 0xa
33 #define RTCA3_RDAYCNT_DAY GENMASK(5, 0)
34 #define RTCA3_RMONCNT 0xc
35 #define RTCA3_RMONCNT_MONTH GENMASK(4, 0)
36 #define RTCA3_RYRCNT 0xe
37 #define RTCA3_RYRCNT_YEAR GENMASK(7, 0)
39 /* Alarm registers. */
40 #define RTCA3_RSECAR 0x10
41 #define RTCA3_RSECAR_SEC GENMASK(6, 0)
42 #define RTCA3_RMINAR 0x12
43 #define RTCA3_RMINAR_MIN GENMASK(6, 0)
44 #define RTCA3_RHRAR 0x14
45 #define RTCA3_RHRAR_HR GENMASK(5, 0)
46 #define RTCA3_RHRAR_PM BIT(6)
47 #define RTCA3_RWKAR 0x16
48 #define RTCA3_RWKAR_DAYW GENMASK(2, 0)
49 #define RTCA3_RDAYAR 0x18
50 #define RTCA3_RDAYAR_DATE GENMASK(5, 0)
51 #define RTCA3_RMONAR 0x1a
52 #define RTCA3_RMONAR_MON GENMASK(4, 0)
53 #define RTCA3_RYRAR 0x1c
54 #define RTCA3_RYRAR_YR GENMASK(7, 0)
55 #define RTCA3_RYRAREN 0x1e
57 /* Alarm enable bit (for all alarm registers). */
58 #define RTCA3_AR_ENB BIT(7)
60 /* Control registers. */
61 #define RTCA3_RCR1 0x22
62 #define RTCA3_RCR1_AIE BIT(0)
63 #define RTCA3_RCR1_CIE BIT(1)
64 #define RTCA3_RCR1_PIE BIT(2)
65 #define RTCA3_RCR1_PES GENMASK(7, 4)
66 #define RTCA3_RCR1_PES_1_64_SEC 0x8
67 #define RTCA3_RCR2 0x24
68 #define RTCA3_RCR2_START BIT(0)
69 #define RTCA3_RCR2_RESET BIT(1)
70 #define RTCA3_RCR2_AADJE BIT(4)
71 #define RTCA3_RCR2_ADJP BIT(5)
72 #define RTCA3_RCR2_HR24 BIT(6)
73 #define RTCA3_RCR2_CNTMD BIT(7)
74 #define RTCA3_RSR 0x20
75 #define RTCA3_RSR_AF BIT(0)
76 #define RTCA3_RSR_CF BIT(1)
77 #define RTCA3_RSR_PF BIT(2)
78 #define RTCA3_RADJ 0x2e
79 #define RTCA3_RADJ_ADJ GENMASK(5, 0)
80 #define RTCA3_RADJ_ADJ_MAX 0x3f
81 #define RTCA3_RADJ_PMADJ GENMASK(7, 6)
82 #define RTCA3_RADJ_PMADJ_NONE 0
83 #define RTCA3_RADJ_PMADJ_ADD 1
84 #define RTCA3_RADJ_PMADJ_SUB 2
86 /* Polling operation timeouts. */
87 #define RTCA3_DEFAULT_TIMEOUT_US 150
88 #define RTCA3_IRQSET_TIMEOUT_US 5000
89 #define RTCA3_START_TIMEOUT_US 150000
90 #define RTCA3_RESET_TIMEOUT_US 200000
93 * enum rtca3_alrm_set_step - RTCA3 alarm set steps
94 * @RTCA3_ALRM_SSTEP_DONE: alarm setup done step
95 * @RTCA3_ALRM_SSTEP_IRQ: two 1/64 periodic IRQs were generated step
96 * @RTCA3_ALRM_SSTEP_INIT: alarm setup initialization step
98 enum rtca3_alrm_set_step
{
99 RTCA3_ALRM_SSTEP_DONE
= 0,
100 RTCA3_ALRM_SSTEP_IRQ
= 1,
101 RTCA3_ALRM_SSTEP_INIT
= 3,
105 * struct rtca3_ppb_per_cycle - PPB per cycle
106 * @ten_sec: PPB per cycle in 10 seconds adjutment mode
107 * @sixty_sec: PPB per cycle in 60 seconds adjustment mode
109 struct rtca3_ppb_per_cycle
{
115 * struct rtca3_priv - RTCA3 private data structure
116 * @base: base address
117 * @rtc_dev: RTC device
118 * @rstc: reset control
119 * @set_alarm_completion: alarm setup completion
120 * @alrm_sstep: alarm setup step (see enum rtca3_alrm_set_step)
122 * @ppb: ppb per cycle for each the available adjustment modes
123 * @wakeup_irq: wakeup IRQ
127 struct rtc_device
*rtc_dev
;
128 struct reset_control
*rstc
;
129 struct completion set_alarm_completion
;
132 struct rtca3_ppb_per_cycle ppb
;
136 static void rtca3_byte_update_bits(struct rtca3_priv
*priv
, u8 off
, u8 mask
, u8 val
)
140 tmp
= readb(priv
->base
+ off
);
143 writeb(tmp
, priv
->base
+ off
);
146 static u8
rtca3_alarm_handler_helper(struct rtca3_priv
*priv
)
150 val
= readb(priv
->base
+ RTCA3_RSR
);
151 pending
= val
& RTCA3_RSR_AF
;
152 writeb(val
& ~pending
, priv
->base
+ RTCA3_RSR
);
155 rtc_update_irq(priv
->rtc_dev
, 1, RTC_AF
| RTC_IRQF
);
160 static irqreturn_t
rtca3_alarm_handler(int irq
, void *dev_id
)
162 struct rtca3_priv
*priv
= dev_id
;
165 guard(spinlock
)(&priv
->lock
);
167 pending
= rtca3_alarm_handler_helper(priv
);
169 return IRQ_RETVAL(pending
);
172 static irqreturn_t
rtca3_periodic_handler(int irq
, void *dev_id
)
174 struct rtca3_priv
*priv
= dev_id
;
177 guard(spinlock
)(&priv
->lock
);
179 val
= readb(priv
->base
+ RTCA3_RSR
);
180 pending
= val
& RTCA3_RSR_PF
;
183 writeb(val
& ~pending
, priv
->base
+ RTCA3_RSR
);
185 if (atomic_read(&priv
->alrm_sstep
) > RTCA3_ALRM_SSTEP_IRQ
) {
186 /* Alarm setup in progress. */
187 atomic_dec(&priv
->alrm_sstep
);
189 if (atomic_read(&priv
->alrm_sstep
) == RTCA3_ALRM_SSTEP_IRQ
) {
191 * We got 2 * 1/64 periodic interrupts. Disable
192 * interrupt and let alarm setup continue.
194 rtca3_byte_update_bits(priv
, RTCA3_RCR1
,
196 readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR1
, val
,
197 !(val
& RTCA3_RCR1_PIE
),
198 10, RTCA3_DEFAULT_TIMEOUT_US
);
199 complete(&priv
->set_alarm_completion
);
204 return IRQ_RETVAL(pending
);
207 static void rtca3_prepare_cntalrm_regs_for_read(struct rtca3_priv
*priv
, bool cnt
)
209 /* Offset b/w time and alarm registers. */
210 u8 offset
= cnt
? 0 : 0xe;
213 * According to HW manual (section 22.6.4. Notes on writing to and
214 * reading from registers) after writing to count registers, alarm
215 * registers, year alarm enable register, bits RCR2.AADJE, AADJP,
216 * and HR24 register, we need to do 3 empty reads before being
217 * able to fetch the registers content.
219 for (u8 i
= 0; i
< 3; i
++) {
220 readb(priv
->base
+ RTCA3_RSECCNT
+ offset
);
221 readb(priv
->base
+ RTCA3_RMINCNT
+ offset
);
222 readb(priv
->base
+ RTCA3_RHRCNT
+ offset
);
223 readb(priv
->base
+ RTCA3_RWKCNT
+ offset
);
224 readb(priv
->base
+ RTCA3_RDAYCNT
+ offset
);
225 readw(priv
->base
+ RTCA3_RYRCNT
+ offset
);
227 readb(priv
->base
+ RTCA3_RYRAREN
);
231 static int rtca3_read_time(struct device
*dev
, struct rtc_time
*tm
)
233 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
234 u8 sec
, min
, hour
, wday
, mday
, month
, tmp
;
239 guard(spinlock_irqsave
)(&priv
->lock
);
241 tmp
= readb(priv
->base
+ RTCA3_RCR2
);
242 if (!(tmp
& RTCA3_RCR2_START
))
246 /* Clear carry interrupt. */
247 rtca3_byte_update_bits(priv
, RTCA3_RSR
, RTCA3_RSR_CF
, 0);
250 sec
= readb(priv
->base
+ RTCA3_RSECCNT
);
251 min
= readb(priv
->base
+ RTCA3_RMINCNT
);
252 hour
= readb(priv
->base
+ RTCA3_RHRCNT
);
253 wday
= readb(priv
->base
+ RTCA3_RWKCNT
);
254 mday
= readb(priv
->base
+ RTCA3_RDAYCNT
);
255 month
= readb(priv
->base
+ RTCA3_RMONCNT
);
256 year
= readw(priv
->base
+ RTCA3_RYRCNT
);
258 tmp
= readb(priv
->base
+ RTCA3_RSR
);
261 * We cannot generate carries due to reading 64Hz counter as
262 * the driver doesn't implement carry, thus, carries will be
263 * generated once per seconds. Add a timeout of 5 trials here
264 * to avoid infinite loop, if any.
266 } while ((tmp
& RTCA3_RSR_CF
) && ++trials
< 5);
271 tm
->tm_sec
= bcd2bin(FIELD_GET(RTCA3_RSECCNT_SEC
, sec
));
272 tm
->tm_min
= bcd2bin(FIELD_GET(RTCA3_RMINCNT_MIN
, min
));
273 tm
->tm_hour
= bcd2bin(FIELD_GET(RTCA3_RHRCNT_HR
, hour
));
274 tm
->tm_wday
= bcd2bin(FIELD_GET(RTCA3_RWKCNT_WK
, wday
));
275 tm
->tm_mday
= bcd2bin(FIELD_GET(RTCA3_RDAYCNT_DAY
, mday
));
276 tm
->tm_mon
= bcd2bin(FIELD_GET(RTCA3_RMONCNT_MONTH
, month
)) - 1;
277 year
= FIELD_GET(RTCA3_RYRCNT_YEAR
, year
);
278 year100
= bcd2bin((year
== 0x99) ? 0x19 : 0x20);
279 tm
->tm_year
= (year100
* 100 + bcd2bin(year
)) - 1900;
284 static int rtca3_set_time(struct device
*dev
, struct rtc_time
*tm
)
286 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
290 guard(spinlock_irqsave
)(&priv
->lock
);
293 rcr2
= readb(priv
->base
+ RTCA3_RCR2
);
294 writeb(rcr2
& ~RTCA3_RCR2_START
, priv
->base
+ RTCA3_RCR2
);
295 ret
= readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR2
, tmp
,
296 !(tmp
& RTCA3_RCR2_START
),
297 10, RTCA3_DEFAULT_TIMEOUT_US
);
302 writeb(bin2bcd(tm
->tm_sec
), priv
->base
+ RTCA3_RSECCNT
);
303 writeb(bin2bcd(tm
->tm_min
), priv
->base
+ RTCA3_RMINCNT
);
304 writeb(bin2bcd(tm
->tm_hour
), priv
->base
+ RTCA3_RHRCNT
);
305 writeb(bin2bcd(tm
->tm_wday
), priv
->base
+ RTCA3_RWKCNT
);
306 writeb(bin2bcd(tm
->tm_mday
), priv
->base
+ RTCA3_RDAYCNT
);
307 writeb(bin2bcd(tm
->tm_mon
+ 1), priv
->base
+ RTCA3_RMONCNT
);
308 writew(bin2bcd(tm
->tm_year
% 100), priv
->base
+ RTCA3_RYRCNT
);
310 /* Make sure we can read back the counters. */
311 rtca3_prepare_cntalrm_regs_for_read(priv
, true);
314 writeb(rcr2
| RTCA3_RCR2_START
, priv
->base
+ RTCA3_RCR2
);
315 return readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR2
, tmp
,
316 (tmp
& RTCA3_RCR2_START
),
317 10, RTCA3_DEFAULT_TIMEOUT_US
);
320 static int rtca3_alarm_irq_set_helper(struct rtca3_priv
*priv
,
322 unsigned int enabled
)
328 * AIE, CIE, PIE bit indexes in RSR corresponds with
329 * those on RCR1. Same interrupts mask can be used.
331 rtca3_byte_update_bits(priv
, RTCA3_RSR
, interrupts
, 0);
337 rtca3_byte_update_bits(priv
, RTCA3_RCR1
, interrupts
, val
);
338 return readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR1
, tmp
,
339 ((tmp
& interrupts
) == val
),
340 10, RTCA3_IRQSET_TIMEOUT_US
);
343 static int rtca3_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
345 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
347 guard(spinlock_irqsave
)(&priv
->lock
);
349 return rtca3_alarm_irq_set_helper(priv
, RTCA3_RCR1_AIE
, enabled
);
352 static int rtca3_read_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
354 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
355 u8 sec
, min
, hour
, wday
, mday
, month
;
356 struct rtc_time
*tm
= &wkalrm
->time
;
360 guard(spinlock_irqsave
)(&priv
->lock
);
362 sec
= readb(priv
->base
+ RTCA3_RSECAR
);
363 min
= readb(priv
->base
+ RTCA3_RMINAR
);
364 hour
= readb(priv
->base
+ RTCA3_RHRAR
);
365 wday
= readb(priv
->base
+ RTCA3_RWKAR
);
366 mday
= readb(priv
->base
+ RTCA3_RDAYAR
);
367 month
= readb(priv
->base
+ RTCA3_RMONAR
);
368 year
= readw(priv
->base
+ RTCA3_RYRAR
);
370 tm
->tm_sec
= bcd2bin(FIELD_GET(RTCA3_RSECAR_SEC
, sec
));
371 tm
->tm_min
= bcd2bin(FIELD_GET(RTCA3_RMINAR_MIN
, min
));
372 tm
->tm_hour
= bcd2bin(FIELD_GET(RTCA3_RHRAR_HR
, hour
));
373 tm
->tm_wday
= bcd2bin(FIELD_GET(RTCA3_RWKAR_DAYW
, wday
));
374 tm
->tm_mday
= bcd2bin(FIELD_GET(RTCA3_RDAYAR_DATE
, mday
));
375 tm
->tm_mon
= bcd2bin(FIELD_GET(RTCA3_RMONAR_MON
, month
)) - 1;
376 year
= FIELD_GET(RTCA3_RYRAR_YR
, year
);
377 year100
= bcd2bin((year
== 0x99) ? 0x19 : 0x20);
378 tm
->tm_year
= (year100
* 100 + bcd2bin(year
)) - 1900;
380 wkalrm
->enabled
= !!(readb(priv
->base
+ RTCA3_RCR1
) & RTCA3_RCR1_AIE
);
385 static int rtca3_set_alarm(struct device
*dev
, struct rtc_wkalrm
*wkalrm
)
387 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
388 struct rtc_time
*tm
= &wkalrm
->time
;
392 scoped_guard(spinlock_irqsave
, &priv
->lock
) {
393 tmp
= readb(priv
->base
+ RTCA3_RCR2
);
394 if (!(tmp
& RTCA3_RCR2_START
))
397 /* Disable AIE to prevent false interrupts. */
398 rcr1
= readb(priv
->base
+ RTCA3_RCR1
);
399 rcr1
&= ~RTCA3_RCR1_AIE
;
400 writeb(rcr1
, priv
->base
+ RTCA3_RCR1
);
401 ret
= readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR1
, tmp
,
402 !(tmp
& RTCA3_RCR1_AIE
),
403 10, RTCA3_DEFAULT_TIMEOUT_US
);
407 /* Set the time and enable the alarm. */
408 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_sec
), priv
->base
+ RTCA3_RSECAR
);
409 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_min
), priv
->base
+ RTCA3_RMINAR
);
410 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_hour
), priv
->base
+ RTCA3_RHRAR
);
411 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_wday
), priv
->base
+ RTCA3_RWKAR
);
412 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_mday
), priv
->base
+ RTCA3_RDAYAR
);
413 writeb(RTCA3_AR_ENB
| bin2bcd(tm
->tm_mon
+ 1), priv
->base
+ RTCA3_RMONAR
);
415 writew(bin2bcd(tm
->tm_year
% 100), priv
->base
+ RTCA3_RYRAR
);
416 writeb(RTCA3_AR_ENB
, priv
->base
+ RTCA3_RYRAREN
);
418 /* Make sure we can read back the counters. */
419 rtca3_prepare_cntalrm_regs_for_read(priv
, false);
421 /* Need to wait for 2 * 1/64 periodic interrupts to be generated. */
422 atomic_set(&priv
->alrm_sstep
, RTCA3_ALRM_SSTEP_INIT
);
423 reinit_completion(&priv
->set_alarm_completion
);
425 /* Enable periodic interrupt. */
426 rcr1
|= RTCA3_RCR1_PIE
;
427 writeb(rcr1
, priv
->base
+ RTCA3_RCR1
);
428 ret
= readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR1
, tmp
,
429 (tmp
& RTCA3_RCR1_PIE
),
430 10, RTCA3_IRQSET_TIMEOUT_US
);
436 /* Wait for the 2 * 1/64 periodic interrupts. */
437 ret
= wait_for_completion_interruptible_timeout(&priv
->set_alarm_completion
,
438 msecs_to_jiffies(500));
444 scoped_guard(spinlock_irqsave
, &priv
->lock
) {
445 ret
= rtca3_alarm_irq_set_helper(priv
, RTCA3_RCR1_AIE
, wkalrm
->enabled
);
446 atomic_set(&priv
->alrm_sstep
, RTCA3_ALRM_SSTEP_DONE
);
452 scoped_guard(spinlock_irqsave
, &priv
->lock
) {
454 * Disable PIE to avoid interrupt storm in case HW needed more than
455 * specified timeout for setup.
457 writeb(rcr1
& ~RTCA3_RCR1_PIE
, priv
->base
+ RTCA3_RCR1
);
458 readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR1
, tmp
, !(tmp
& ~RTCA3_RCR1_PIE
),
459 10, RTCA3_DEFAULT_TIMEOUT_US
);
460 atomic_set(&priv
->alrm_sstep
, RTCA3_ALRM_SSTEP_DONE
);
466 static int rtca3_read_offset(struct device
*dev
, long *offset
)
468 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
469 u8 val
, radj
, cycles
;
472 scoped_guard(spinlock_irqsave
, &priv
->lock
) {
473 radj
= readb(priv
->base
+ RTCA3_RADJ
);
474 val
= readb(priv
->base
+ RTCA3_RCR2
);
477 cycles
= FIELD_GET(RTCA3_RADJ_ADJ
, radj
);
484 if (val
& RTCA3_RCR2_ADJP
)
485 ppb_per_cycle
= priv
->ppb
.ten_sec
;
487 ppb_per_cycle
= priv
->ppb
.sixty_sec
;
489 *offset
= cycles
* ppb_per_cycle
;
490 val
= FIELD_GET(RTCA3_RADJ_PMADJ
, radj
);
491 if (val
== RTCA3_RADJ_PMADJ_SUB
)
492 *offset
= -(*offset
);
497 static int rtca3_set_offset(struct device
*dev
, long offset
)
499 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
500 int cycles
, cycles10
, cycles60
;
505 * Automatic time error adjustment could be set at intervals of 10
508 cycles10
= DIV_ROUND_CLOSEST(offset
, priv
->ppb
.ten_sec
);
509 cycles60
= DIV_ROUND_CLOSEST(offset
, priv
->ppb
.sixty_sec
);
511 /* We can set b/w 1 and 63 clock cycles. */
512 if (cycles60
>= -RTCA3_RADJ_ADJ_MAX
&&
513 cycles60
<= RTCA3_RADJ_ADJ_MAX
) {
516 } else if (cycles10
>= -RTCA3_RADJ_ADJ_MAX
&&
517 cycles10
<= RTCA3_RADJ_ADJ_MAX
) {
519 adjp
= RTCA3_RCR2_ADJP
;
524 radj
= FIELD_PREP(RTCA3_RADJ_ADJ
, abs(cycles
));
526 radj
|= FIELD_PREP(RTCA3_RADJ_PMADJ
, RTCA3_RADJ_PMADJ_NONE
);
528 radj
|= FIELD_PREP(RTCA3_RADJ_PMADJ
, RTCA3_RADJ_PMADJ_ADD
);
530 radj
|= FIELD_PREP(RTCA3_RADJ_PMADJ
, RTCA3_RADJ_PMADJ_SUB
);
532 guard(spinlock_irqsave
)(&priv
->lock
);
534 tmp
= readb(priv
->base
+ RTCA3_RCR2
);
536 if ((tmp
& RTCA3_RCR2_ADJP
) != adjp
) {
537 /* RADJ.PMADJ need to be set to zero before setting RCR2.ADJP. */
538 writeb(0, priv
->base
+ RTCA3_RADJ
);
539 ret
= readb_poll_timeout_atomic(priv
->base
+ RTCA3_RADJ
, tmp
, !tmp
,
540 10, RTCA3_DEFAULT_TIMEOUT_US
);
544 rtca3_byte_update_bits(priv
, RTCA3_RCR2
, RTCA3_RCR2_ADJP
, adjp
);
545 ret
= readb_poll_timeout_atomic(priv
->base
+ RTCA3_RCR2
, tmp
,
546 ((tmp
& RTCA3_RCR2_ADJP
) == adjp
),
547 10, RTCA3_DEFAULT_TIMEOUT_US
);
552 writeb(radj
, priv
->base
+ RTCA3_RADJ
);
553 return readb_poll_timeout_atomic(priv
->base
+ RTCA3_RADJ
, tmp
, (tmp
== radj
),
554 10, RTCA3_DEFAULT_TIMEOUT_US
);
557 static const struct rtc_class_ops rtca3_ops
= {
558 .read_time
= rtca3_read_time
,
559 .set_time
= rtca3_set_time
,
560 .read_alarm
= rtca3_read_alarm
,
561 .set_alarm
= rtca3_set_alarm
,
562 .alarm_irq_enable
= rtca3_alarm_irq_enable
,
563 .set_offset
= rtca3_set_offset
,
564 .read_offset
= rtca3_read_offset
,
567 static int rtca3_initial_setup(struct clk
*clk
, struct rtca3_priv
*priv
)
569 unsigned long osc32k_rate
;
574 osc32k_rate
= clk_get_rate(clk
);
578 sleep_us
= DIV_ROUND_UP_ULL(1000000ULL, osc32k_rate
) * 6;
580 priv
->ppb
.ten_sec
= DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate
* 10));
581 priv
->ppb
.sixty_sec
= DIV_ROUND_CLOSEST_ULL(1000000000ULL, (osc32k_rate
* 60));
584 * According to HW manual (section 22.4.2. Clock and count mode setting procedure)
585 * we need to wait at least 6 cycles of the 32KHz clock after clock was enabled.
587 usleep_range(sleep_us
, sleep_us
+ 10);
589 /* Disable all interrupts. */
590 mask
= RTCA3_RCR1_AIE
| RTCA3_RCR1_CIE
| RTCA3_RCR1_PIE
;
591 ret
= rtca3_alarm_irq_set_helper(priv
, mask
, 0);
595 mask
= RTCA3_RCR2_START
| RTCA3_RCR2_HR24
;
596 val
= readb(priv
->base
+ RTCA3_RCR2
);
597 /* Nothing to do if already started in 24 hours and calendar count mode. */
598 if ((val
& mask
) == mask
)
601 /* Reconfigure the RTC in 24 hours and calendar count mode. */
602 mask
= RTCA3_RCR2_START
| RTCA3_RCR2_CNTMD
;
603 writeb(0, priv
->base
+ RTCA3_RCR2
);
604 ret
= readb_poll_timeout(priv
->base
+ RTCA3_RCR2
, tmp
, !(tmp
& mask
),
605 10, RTCA3_DEFAULT_TIMEOUT_US
);
610 * Set 24 hours mode. According to HW manual (section 22.3.19. RTC Control
611 * Register 2) this needs to be done separate from stop operation.
613 mask
= RTCA3_RCR2_HR24
;
614 val
= RTCA3_RCR2_HR24
;
615 writeb(val
, priv
->base
+ RTCA3_RCR2
);
616 ret
= readb_poll_timeout(priv
->base
+ RTCA3_RCR2
, tmp
, (tmp
& mask
),
617 10, RTCA3_DEFAULT_TIMEOUT_US
);
622 mask
= RTCA3_RCR2_RESET
;
623 writeb(val
| RTCA3_RCR2_RESET
, priv
->base
+ RTCA3_RCR2
);
624 ret
= readb_poll_timeout(priv
->base
+ RTCA3_RCR2
, tmp
, !(tmp
& mask
),
625 10, RTCA3_RESET_TIMEOUT_US
);
630 * According to HW manual (section 22.6.3. Notes on writing to and reading
631 * from registers) after reset we need to wait 6 clock cycles before
632 * writing to RTC registers.
634 usleep_range(sleep_us
, sleep_us
+ 10);
636 /* Set no adjustment. */
637 writeb(0, priv
->base
+ RTCA3_RADJ
);
638 ret
= readb_poll_timeout(priv
->base
+ RTCA3_RADJ
, tmp
, !tmp
, 10,
639 RTCA3_DEFAULT_TIMEOUT_US
);
641 /* Start the RTC and enable automatic time error adjustment. */
642 mask
= RTCA3_RCR2_START
| RTCA3_RCR2_AADJE
;
643 val
|= RTCA3_RCR2_START
| RTCA3_RCR2_AADJE
;
644 writeb(val
, priv
->base
+ RTCA3_RCR2
);
645 ret
= readb_poll_timeout(priv
->base
+ RTCA3_RCR2
, tmp
, ((tmp
& mask
) == mask
),
646 10, RTCA3_START_TIMEOUT_US
);
651 * According to HW manual (section 22.6.4. Notes on writing to and reading
652 * from registers) we need to wait 1/128 seconds while the clock is operating
653 * (RCR2.START bit = 1) to be able to read the counters after a return from
656 usleep_range(8000, 9000);
658 /* Set period interrupt to 1/64 seconds. It is necessary for alarm setup. */
659 val
= FIELD_PREP(RTCA3_RCR1_PES
, RTCA3_RCR1_PES_1_64_SEC
);
660 rtca3_byte_update_bits(priv
, RTCA3_RCR1
, RTCA3_RCR1_PES
, val
);
661 return readb_poll_timeout(priv
->base
+ RTCA3_RCR1
, tmp
, ((tmp
& RTCA3_RCR1_PES
) == val
),
662 10, RTCA3_DEFAULT_TIMEOUT_US
);
665 static int rtca3_request_irqs(struct platform_device
*pdev
, struct rtca3_priv
*priv
)
667 struct device
*dev
= &pdev
->dev
;
670 irq
= platform_get_irq_byname(pdev
, "alarm");
672 return dev_err_probe(dev
, irq
, "Failed to get alarm IRQ!\n");
674 ret
= devm_request_irq(dev
, irq
, rtca3_alarm_handler
, 0, "rtca3-alarm", priv
);
676 return dev_err_probe(dev
, ret
, "Failed to request alarm IRQ!\n");
677 priv
->wakeup_irq
= irq
;
679 irq
= platform_get_irq_byname(pdev
, "period");
681 return dev_err_probe(dev
, irq
, "Failed to get period IRQ!\n");
683 ret
= devm_request_irq(dev
, irq
, rtca3_periodic_handler
, 0, "rtca3-period", priv
);
685 return dev_err_probe(dev
, ret
, "Failed to request period IRQ!\n");
688 * Driver doesn't implement carry handler. Just get the IRQ here
689 * for backward compatibility, in case carry support will be added later.
691 irq
= platform_get_irq_byname(pdev
, "carry");
693 return dev_err_probe(dev
, irq
, "Failed to get carry IRQ!\n");
698 static void rtca3_action(void *data
)
700 struct device
*dev
= data
;
701 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
704 ret
= reset_control_assert(priv
->rstc
);
706 dev_err(dev
, "Failed to de-assert reset!");
708 ret
= pm_runtime_put_sync(dev
);
710 dev_err(dev
, "Failed to runtime suspend!");
713 static int rtca3_probe(struct platform_device
*pdev
)
715 struct device
*dev
= &pdev
->dev
;
716 struct rtca3_priv
*priv
;
720 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
724 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
725 if (IS_ERR(priv
->base
))
726 return PTR_ERR(priv
->base
);
728 ret
= devm_pm_runtime_enable(dev
);
732 priv
->rstc
= devm_reset_control_get_shared(dev
, NULL
);
733 if (IS_ERR(priv
->rstc
))
734 return PTR_ERR(priv
->rstc
);
736 ret
= pm_runtime_resume_and_get(dev
);
740 ret
= reset_control_deassert(priv
->rstc
);
742 pm_runtime_put_sync(dev
);
746 dev_set_drvdata(dev
, priv
);
747 ret
= devm_add_action_or_reset(dev
, rtca3_action
, dev
);
752 * This must be an always-on clock to keep the RTC running even after
753 * driver is unbinded.
755 clk
= devm_clk_get_enabled(dev
, "counter");
759 spin_lock_init(&priv
->lock
);
760 atomic_set(&priv
->alrm_sstep
, RTCA3_ALRM_SSTEP_DONE
);
761 init_completion(&priv
->set_alarm_completion
);
763 ret
= rtca3_initial_setup(clk
, priv
);
765 return dev_err_probe(dev
, ret
, "Failed to setup the RTC!\n");
767 ret
= rtca3_request_irqs(pdev
, priv
);
771 device_init_wakeup(&pdev
->dev
, 1);
773 priv
->rtc_dev
= devm_rtc_allocate_device(&pdev
->dev
);
774 if (IS_ERR(priv
->rtc_dev
))
775 return PTR_ERR(priv
->rtc_dev
);
777 priv
->rtc_dev
->ops
= &rtca3_ops
;
778 priv
->rtc_dev
->max_user_freq
= 256;
779 priv
->rtc_dev
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
780 priv
->rtc_dev
->range_max
= RTC_TIMESTAMP_END_2099
;
782 return devm_rtc_register_device(priv
->rtc_dev
);
785 static void rtca3_remove(struct platform_device
*pdev
)
787 struct rtca3_priv
*priv
= platform_get_drvdata(pdev
);
789 guard(spinlock_irqsave
)(&priv
->lock
);
792 * Disable alarm, periodic interrupts. The RTC device cannot
793 * power up the system.
795 rtca3_alarm_irq_set_helper(priv
, RTCA3_RCR1_AIE
| RTCA3_RCR1_PIE
, 0);
798 static int rtca3_suspend(struct device
*dev
)
800 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
802 if (!device_may_wakeup(dev
))
805 /* Alarm setup in progress. */
806 if (atomic_read(&priv
->alrm_sstep
) != RTCA3_ALRM_SSTEP_DONE
)
809 enable_irq_wake(priv
->wakeup_irq
);
814 static int rtca3_clean_alarm(struct rtca3_priv
*priv
)
816 struct rtc_device
*rtc_dev
= priv
->rtc_dev
;
817 time64_t alarm_time
, now
;
818 struct rtc_wkalrm alarm
;
823 ret
= rtc_read_alarm(rtc_dev
, &alarm
);
830 ret
= rtc_read_time(rtc_dev
, &tm
);
834 alarm_time
= rtc_tm_to_time64(&alarm
.time
);
835 now
= rtc_tm_to_time64(&tm
);
836 if (alarm_time
>= now
)
840 * Heuristically, it has been determined that when returning from deep
841 * sleep state the RTCA3_RSR.AF is zero even though the alarm expired.
842 * Call again the rtc_update_irq() if alarm helper detects this.
845 guard(spinlock_irqsave
)(&priv
->lock
);
847 pending
= rtca3_alarm_handler_helper(priv
);
849 rtc_update_irq(priv
->rtc_dev
, 1, RTC_AF
| RTC_IRQF
);
854 static int rtca3_resume(struct device
*dev
)
856 struct rtca3_priv
*priv
= dev_get_drvdata(dev
);
858 if (!device_may_wakeup(dev
))
861 disable_irq_wake(priv
->wakeup_irq
);
864 * According to the HW manual (section 22.6.4 Notes on writing to
865 * and reading from registers) we need to wait 1/128 seconds while
866 * RCR2.START = 1 to be able to read the counters after a return from low
867 * power consumption state.
872 * The alarm cannot wake the system from deep sleep states. In case
873 * we return from deep sleep states and the alarm expired we need
874 * to disable it to avoid failures when setting another alarm.
876 return rtca3_clean_alarm(priv
);
879 static DEFINE_SIMPLE_DEV_PM_OPS(rtca3_pm_ops
, rtca3_suspend
, rtca3_resume
);
881 static const struct of_device_id rtca3_of_match
[] = {
882 { .compatible
= "renesas,rz-rtca3", },
885 MODULE_DEVICE_TABLE(of
, rtca3_of_match
);
887 static struct platform_driver rtca3_platform_driver
= {
890 .pm
= pm_ptr(&rtca3_pm_ops
),
891 .of_match_table
= rtca3_of_match
,
893 .probe
= rtca3_probe
,
894 .remove
= rtca3_remove
,
896 module_platform_driver(rtca3_platform_driver
);
898 MODULE_DESCRIPTION("Renesas RTCA-3 RTC driver");
899 MODULE_AUTHOR("Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com>");
900 MODULE_LICENSE("GPL");