1 // SPDX-License-Identifier: (GPL-2.0-only OR MIT)
3 * Copyright (C) 2024 Amlogic, Inc. All rights reserved
4 * Author: Yiting Deng <yiting.deng@amlogic.com>
7 #include <linux/bitfield.h>
9 #include <linux/clk-provider.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
15 #include <linux/time64.h>
17 /* rtc oscillator rate */
19 #define OSC_24M 24000000
21 #define RTC_CTRL (0x0 << 2) /* Control RTC */
22 #define RTC_ALRM0_EN BIT(0)
23 #define RTC_OSC_SEL BIT(8)
24 #define RTC_ENABLE BIT(12)
26 #define RTC_COUNTER_REG (0x1 << 2) /* Program RTC counter initial value */
28 #define RTC_ALARM0_REG (0x2 << 2) /* Program RTC alarm0 value */
30 #define RTC_SEC_ADJUST_REG (0x6 << 2) /* Control second-based timing adjustment */
31 #define RTC_MATCH_COUNTER GENMASK(18, 0)
32 #define RTC_SEC_ADJUST_CTRL GENMASK(20, 19)
33 #define RTC_ADJ_VALID BIT(23)
35 #define RTC_INT_MASK (0x8 << 2) /* RTC interrupt mask */
36 #define RTC_ALRM0_IRQ_MSK BIT(0)
38 #define RTC_INT_CLR (0x9 << 2) /* Clear RTC interrupt */
39 #define RTC_ALRM0_IRQ_CLR BIT(0)
41 #define RTC_OSCIN_CTRL0 (0xa << 2) /* Control RTC clk from 24M */
42 #define RTC_OSCIN_CTRL1 (0xb << 2) /* Control RTC clk from 24M */
43 #define RTC_OSCIN_IN_EN BIT(31)
44 #define RTC_OSCIN_OUT_CFG GENMASK(29, 28)
45 #define RTC_OSCIN_OUT_N0M0 GENMASK(11, 0)
46 #define RTC_OSCIN_OUT_N1M1 GENMASK(23, 12)
48 #define RTC_INT_STATUS (0xc << 2) /* RTC interrupt status */
49 #define RTC_ALRM0_IRQ_STATUS BIT(0)
51 #define RTC_REAL_TIME (0xd << 2) /* RTC time value */
53 #define RTC_OSCIN_OUT_32K_N0 0x2dc
54 #define RTC_OSCIN_OUT_32K_N1 0x2db
55 #define RTC_OSCIN_OUT_32K_M0 0x1
56 #define RTC_OSCIN_OUT_32K_M1 0x2
58 #define RTC_SWALLOW_SECOND 0x2
59 #define RTC_INSERT_SECOND 0x3
61 struct aml_rtc_config
{
67 struct rtc_device
*rtc_dev
;
72 const struct aml_rtc_config
*config
;
75 static const struct regmap_config aml_rtc_regmap_config
= {
79 .max_register
= RTC_REAL_TIME
,
82 static inline u32
gray_to_binary(u32 gray
)
85 int size
= sizeof(bcd
) * 8;
88 for (i
= 0; (1 << i
) < size
; i
++)
89 bcd
^= bcd
>> (1 << i
);
94 static inline u32
binary_to_gray(u32 bcd
)
96 return bcd
^ (bcd
>> 1);
99 static int aml_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
101 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
104 /* if RTC disabled, read time failed */
105 if (!rtc
->rtc_enabled
)
108 regmap_read(rtc
->map
, RTC_REAL_TIME
, &time_sec
);
109 if (rtc
->config
->gray_stored
)
110 time_sec
= gray_to_binary(time_sec
);
111 rtc_time64_to_tm(time_sec
, tm
);
112 dev_dbg(dev
, "%s: read time = %us\n", __func__
, time_sec
);
117 static int aml_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
119 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
122 /* if RTC disabled, first enable it */
123 if (!rtc
->rtc_enabled
) {
124 regmap_write_bits(rtc
->map
, RTC_CTRL
, RTC_ENABLE
, RTC_ENABLE
);
125 usleep_range(100, 200);
126 rtc
->rtc_enabled
= regmap_test_bits(rtc
->map
, RTC_CTRL
, RTC_ENABLE
);
127 if (!rtc
->rtc_enabled
)
131 time_sec
= rtc_tm_to_time64(tm
);
132 if (rtc
->config
->gray_stored
)
133 time_sec
= binary_to_gray(time_sec
);
134 regmap_write(rtc
->map
, RTC_COUNTER_REG
, time_sec
);
135 dev_dbg(dev
, "%s: set time = %us\n", __func__
, time_sec
);
140 static int aml_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
142 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
145 /* if RTC disabled, set alarm failed */
146 if (!rtc
->rtc_enabled
)
149 regmap_update_bits(rtc
->map
, RTC_CTRL
,
150 RTC_ALRM0_EN
, RTC_ALRM0_EN
);
151 regmap_update_bits(rtc
->map
, RTC_INT_MASK
,
152 RTC_ALRM0_IRQ_MSK
, 0);
154 alarm_sec
= rtc_tm_to_time64(&alarm
->time
);
155 if (rtc
->config
->gray_stored
)
156 alarm_sec
= binary_to_gray(alarm_sec
);
157 regmap_write(rtc
->map
, RTC_ALARM0_REG
, alarm_sec
);
159 dev_dbg(dev
, "%s: alarm->enabled=%d alarm_set=%llds\n", __func__
,
160 alarm
->enabled
, alarm_sec
);
165 static int aml_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
167 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
172 /* if RTC disabled, read alarm failed */
173 if (!rtc
->rtc_enabled
)
176 regmap_read(rtc
->map
, RTC_ALARM0_REG
, &alarm_sec
);
177 if (rtc
->config
->gray_stored
)
178 alarm_sec
= gray_to_binary(alarm_sec
);
179 rtc_time64_to_tm(alarm_sec
, &alarm
->time
);
181 alarm_enable
= regmap_test_bits(rtc
->map
, RTC_CTRL
, RTC_ALRM0_EN
);
182 alarm_mask
= regmap_test_bits(rtc
->map
, RTC_INT_MASK
, RTC_ALRM0_IRQ_MSK
);
183 alarm
->enabled
= (alarm_enable
&& !alarm_mask
) ? 1 : 0;
184 dev_dbg(dev
, "%s: alarm->enabled=%d alarm=%us\n", __func__
,
185 alarm
->enabled
, alarm_sec
);
190 static int aml_rtc_read_offset(struct device
*dev
, long *offset
)
192 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
195 int sign
, match_counter
, enable
;
197 /* if RTC disabled, read offset failed */
198 if (!rtc
->rtc_enabled
)
201 regmap_read(rtc
->map
, RTC_SEC_ADJUST_REG
, ®_val
);
202 enable
= FIELD_GET(RTC_ADJ_VALID
, reg_val
);
206 sign
= FIELD_GET(RTC_SEC_ADJUST_CTRL
, reg_val
);
207 match_counter
= FIELD_GET(RTC_MATCH_COUNTER
, reg_val
);
208 val
= 1000000000 / (match_counter
+ 1);
209 if (sign
== RTC_SWALLOW_SECOND
)
217 static int aml_rtc_set_offset(struct device
*dev
, long offset
)
219 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
221 int match_counter
= 0;
225 /* if RTC disabled, set offset failed */
226 if (!rtc
->rtc_enabled
)
231 sign
= offset
< 0 ? RTC_SWALLOW_SECOND
: RTC_INSERT_SECOND
;
232 match_counter
= 1000000000 / abs(offset
) - 1;
233 if (match_counter
< 0 || match_counter
> RTC_MATCH_COUNTER
)
237 reg_val
= FIELD_PREP(RTC_ADJ_VALID
, enable
) |
238 FIELD_PREP(RTC_SEC_ADJUST_CTRL
, sign
) |
239 FIELD_PREP(RTC_MATCH_COUNTER
, match_counter
);
240 regmap_write(rtc
->map
, RTC_SEC_ADJUST_REG
, reg_val
);
245 static int aml_rtc_alarm_enable(struct device
*dev
, unsigned int enabled
)
247 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
250 regmap_update_bits(rtc
->map
, RTC_CTRL
,
251 RTC_ALRM0_EN
, RTC_ALRM0_EN
);
252 regmap_update_bits(rtc
->map
, RTC_INT_MASK
,
253 RTC_ALRM0_IRQ_MSK
, 0);
255 regmap_update_bits(rtc
->map
, RTC_INT_MASK
,
256 RTC_ALRM0_IRQ_MSK
, RTC_ALRM0_IRQ_MSK
);
257 regmap_update_bits(rtc
->map
, RTC_CTRL
,
264 static const struct rtc_class_ops aml_rtc_ops
= {
265 .read_time
= aml_rtc_read_time
,
266 .set_time
= aml_rtc_set_time
,
267 .read_alarm
= aml_rtc_read_alarm
,
268 .set_alarm
= aml_rtc_set_alarm
,
269 .alarm_irq_enable
= aml_rtc_alarm_enable
,
270 .read_offset
= aml_rtc_read_offset
,
271 .set_offset
= aml_rtc_set_offset
,
274 static irqreturn_t
aml_rtc_handler(int irq
, void *data
)
276 struct aml_rtc_data
*rtc
= (struct aml_rtc_data
*)data
;
278 regmap_write(rtc
->map
, RTC_ALARM0_REG
, 0);
279 regmap_write(rtc
->map
, RTC_INT_CLR
, RTC_ALRM0_IRQ_STATUS
);
281 rtc_update_irq(rtc
->rtc_dev
, 1, RTC_AF
| RTC_IRQF
);
286 static void aml_rtc_init(struct aml_rtc_data
*rtc
)
290 rtc
->rtc_enabled
= regmap_test_bits(rtc
->map
, RTC_CTRL
, RTC_ENABLE
);
291 if (!rtc
->rtc_enabled
) {
292 if (clk_get_rate(rtc
->rtc_clk
) == OSC_24M
) {
293 /* select 24M oscillator */
294 regmap_write_bits(rtc
->map
, RTC_CTRL
, RTC_OSC_SEL
, RTC_OSC_SEL
);
297 * Set RTC oscillator to freq_out to freq_in/((N0*M0+N1*M1)/(M0+M1))
298 * Enable clock_in gate of oscillator 24MHz
299 * Set N0 to 733, N1 to 732
301 reg_val
= FIELD_PREP(RTC_OSCIN_IN_EN
, 1)
302 | FIELD_PREP(RTC_OSCIN_OUT_CFG
, 1)
303 | FIELD_PREP(RTC_OSCIN_OUT_N0M0
, RTC_OSCIN_OUT_32K_N0
)
304 | FIELD_PREP(RTC_OSCIN_OUT_N1M1
, RTC_OSCIN_OUT_32K_N1
);
305 regmap_write_bits(rtc
->map
, RTC_OSCIN_CTRL0
, RTC_OSCIN_IN_EN
306 | RTC_OSCIN_OUT_CFG
| RTC_OSCIN_OUT_N0M0
307 | RTC_OSCIN_OUT_N1M1
, reg_val
);
309 /* Set M0 to 2, M1 to 3, so freq_out = 32768 Hz*/
310 reg_val
= FIELD_PREP(RTC_OSCIN_OUT_N0M0
, RTC_OSCIN_OUT_32K_M0
)
311 | FIELD_PREP(RTC_OSCIN_OUT_N1M1
, RTC_OSCIN_OUT_32K_M1
);
312 regmap_write_bits(rtc
->map
, RTC_OSCIN_CTRL1
, RTC_OSCIN_OUT_N0M0
313 | RTC_OSCIN_OUT_N1M1
, reg_val
);
315 /* select 32K oscillator */
316 regmap_write_bits(rtc
->map
, RTC_CTRL
, RTC_OSC_SEL
, 0);
319 regmap_write_bits(rtc
->map
, RTC_INT_MASK
,
320 RTC_ALRM0_IRQ_MSK
, RTC_ALRM0_IRQ_MSK
);
321 regmap_write_bits(rtc
->map
, RTC_CTRL
, RTC_ALRM0_EN
, 0);
324 static int aml_rtc_probe(struct platform_device
*pdev
)
326 struct device
*dev
= &pdev
->dev
;
327 struct aml_rtc_data
*rtc
;
331 rtc
= devm_kzalloc(dev
, sizeof(*rtc
), GFP_KERNEL
);
335 rtc
->config
= of_device_get_match_data(dev
);
339 base
= devm_platform_ioremap_resource(pdev
, 0);
341 return dev_err_probe(dev
, PTR_ERR(base
), "resource ioremap failed\n");
343 rtc
->map
= devm_regmap_init_mmio(dev
, base
, &aml_rtc_regmap_config
);
344 if (IS_ERR(rtc
->map
))
345 return dev_err_probe(dev
, PTR_ERR(rtc
->map
), "regmap init failed\n");
347 rtc
->irq
= platform_get_irq(pdev
, 0);
351 rtc
->rtc_clk
= devm_clk_get(dev
, "osc");
352 if (IS_ERR(rtc
->rtc_clk
))
353 return dev_err_probe(dev
, PTR_ERR(rtc
->rtc_clk
),
354 "failed to find rtc clock\n");
355 if (clk_get_rate(rtc
->rtc_clk
) != OSC_32K
&& clk_get_rate(rtc
->rtc_clk
) != OSC_24M
)
356 return dev_err_probe(dev
, -EINVAL
, "Invalid clock configuration\n");
358 rtc
->sys_clk
= devm_clk_get_enabled(dev
, "sys");
359 if (IS_ERR(rtc
->sys_clk
))
360 return dev_err_probe(dev
, PTR_ERR(rtc
->sys_clk
),
361 "failed to get_enable rtc sys clk\n");
364 device_init_wakeup(dev
, 1);
365 platform_set_drvdata(pdev
, rtc
);
367 rtc
->rtc_dev
= devm_rtc_allocate_device(dev
);
368 if (IS_ERR(rtc
->rtc_dev
)) {
369 ret
= PTR_ERR(rtc
->rtc_dev
);
373 ret
= devm_request_irq(dev
, rtc
->irq
, aml_rtc_handler
,
374 IRQF_ONESHOT
, "aml-rtc alarm", rtc
);
376 dev_err_probe(dev
, ret
, "IRQ%d request failed, ret = %d\n",
381 rtc
->rtc_dev
->ops
= &aml_rtc_ops
;
382 rtc
->rtc_dev
->range_min
= 0;
383 rtc
->rtc_dev
->range_max
= U32_MAX
;
385 ret
= devm_rtc_register_device(rtc
->rtc_dev
);
387 dev_err_probe(&pdev
->dev
, ret
, "Failed to register RTC device: %d\n", ret
);
393 clk_disable_unprepare(rtc
->sys_clk
);
394 device_init_wakeup(dev
, 0);
399 #ifdef CONFIG_PM_SLEEP
400 static int aml_rtc_suspend(struct device
*dev
)
402 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
404 if (device_may_wakeup(dev
))
405 enable_irq_wake(rtc
->irq
);
410 static int aml_rtc_resume(struct device
*dev
)
412 struct aml_rtc_data
*rtc
= dev_get_drvdata(dev
);
414 if (device_may_wakeup(dev
))
415 disable_irq_wake(rtc
->irq
);
421 static SIMPLE_DEV_PM_OPS(aml_rtc_pm_ops
,
422 aml_rtc_suspend
, aml_rtc_resume
);
424 static void aml_rtc_remove(struct platform_device
*pdev
)
426 struct aml_rtc_data
*rtc
= dev_get_drvdata(&pdev
->dev
);
428 clk_disable_unprepare(rtc
->sys_clk
);
429 device_init_wakeup(&pdev
->dev
, 0);
432 static const struct aml_rtc_config a5_rtc_config
= {
435 static const struct aml_rtc_config a4_rtc_config
= {
439 static const struct of_device_id aml_rtc_device_id
[] = {
441 .compatible
= "amlogic,a4-rtc",
442 .data
= &a4_rtc_config
,
445 .compatible
= "amlogic,a5-rtc",
446 .data
= &a5_rtc_config
,
450 MODULE_DEVICE_TABLE(of
, aml_rtc_device_id
);
452 static struct platform_driver aml_rtc_driver
= {
453 .probe
= aml_rtc_probe
,
454 .remove
= aml_rtc_remove
,
457 .pm
= &aml_rtc_pm_ops
,
458 .of_match_table
= aml_rtc_device_id
,
462 module_platform_driver(aml_rtc_driver
);
463 MODULE_DESCRIPTION("Amlogic RTC driver");
464 MODULE_AUTHOR("Yiting Deng <yiting.deng@amlogic.com>");
465 MODULE_LICENSE("GPL");