1 // SPDX-License-Identifier: GPL-2.0
3 * RTC driver for Nuvoton MA35D1
5 * Copyright (C) 2023 Nuvoton Technology Corp.
10 #include <linux/delay.h>
11 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/rtc.h>
18 /* MA35D1 RTC Control Registers */
19 #define MA35_REG_RTC_INIT 0x00
20 #define MA35_REG_RTC_SINFASTS 0x04
21 #define MA35_REG_RTC_FREQADJ 0x08
22 #define MA35_REG_RTC_TIME 0x0c
23 #define MA35_REG_RTC_CAL 0x10
24 #define MA35_REG_RTC_CLKFMT 0x14
25 #define MA35_REG_RTC_WEEKDAY 0x18
26 #define MA35_REG_RTC_TALM 0x1c
27 #define MA35_REG_RTC_CALM 0x20
28 #define MA35_REG_RTC_LEAPYEAR 0x24
29 #define MA35_REG_RTC_INTEN 0x28
30 #define MA35_REG_RTC_INTSTS 0x2c
32 /* register MA35_REG_RTC_INIT */
33 #define RTC_INIT_ACTIVE BIT(0)
34 #define RTC_INIT_MAGIC_CODE 0xa5eb1357
36 /* register MA35_REG_RTC_CLKFMT */
37 #define RTC_CLKFMT_24HEN BIT(0)
38 #define RTC_CLKFMT_DCOMPEN BIT(16)
40 /* register MA35_REG_RTC_INTEN */
41 #define RTC_INTEN_ALMIEN BIT(0)
42 #define RTC_INTEN_UIEN BIT(1)
43 #define RTC_INTEN_CLKFIEN BIT(24)
44 #define RTC_INTEN_CLKSTIEN BIT(25)
46 /* register MA35_REG_RTC_INTSTS */
47 #define RTC_INTSTS_ALMIF BIT(0)
48 #define RTC_INTSTS_UIF BIT(1)
49 #define RTC_INTSTS_CLKFIF BIT(24)
50 #define RTC_INTSTS_CLKSTIF BIT(25)
52 #define RTC_INIT_TIMEOUT 250
56 void __iomem
*rtc_reg
;
57 struct rtc_device
*rtcdev
;
60 static u32
rtc_reg_read(struct ma35_rtc
*p
, u32 offset
)
62 return __raw_readl(p
->rtc_reg
+ offset
);
65 static inline void rtc_reg_write(struct ma35_rtc
*p
, u32 offset
, u32 value
)
67 __raw_writel(value
, p
->rtc_reg
+ offset
);
70 static irqreturn_t
ma35d1_rtc_interrupt(int irq
, void *data
)
72 struct ma35_rtc
*rtc
= (struct ma35_rtc
*)data
;
73 unsigned long events
= 0, rtc_irq
;
75 rtc_irq
= rtc_reg_read(rtc
, MA35_REG_RTC_INTSTS
);
77 if (rtc_irq
& RTC_INTSTS_ALMIF
) {
78 rtc_reg_write(rtc
, MA35_REG_RTC_INTSTS
, RTC_INTSTS_ALMIF
);
79 events
|= RTC_AF
| RTC_IRQF
;
82 rtc_update_irq(rtc
->rtcdev
, 1, events
);
87 static int ma35d1_rtc_init(struct ma35_rtc
*rtc
, u32 ms_timeout
)
89 const unsigned long timeout
= jiffies
+ msecs_to_jiffies(ms_timeout
);
92 if (rtc_reg_read(rtc
, MA35_REG_RTC_INIT
) & RTC_INIT_ACTIVE
)
95 rtc_reg_write(rtc
, MA35_REG_RTC_INIT
, RTC_INIT_MAGIC_CODE
);
99 } while (time_before(jiffies
, timeout
));
104 static int ma35d1_alarm_irq_enable(struct device
*dev
, u32 enabled
)
106 struct ma35_rtc
*rtc
= dev_get_drvdata(dev
);
109 reg_ien
= rtc_reg_read(rtc
, MA35_REG_RTC_INTEN
);
112 rtc_reg_write(rtc
, MA35_REG_RTC_INTEN
, reg_ien
| RTC_INTEN_ALMIEN
);
114 rtc_reg_write(rtc
, MA35_REG_RTC_INTEN
, reg_ien
& ~RTC_INTEN_ALMIEN
);
119 static int ma35d1_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
121 struct ma35_rtc
*rtc
= dev_get_drvdata(dev
);
125 time
= rtc_reg_read(rtc
, MA35_REG_RTC_TIME
);
126 cal
= rtc_reg_read(rtc
, MA35_REG_RTC_CAL
);
127 wday
= rtc_reg_read(rtc
, MA35_REG_RTC_WEEKDAY
);
128 } while (time
!= rtc_reg_read(rtc
, MA35_REG_RTC_TIME
) ||
129 cal
!= rtc_reg_read(rtc
, MA35_REG_RTC_CAL
));
131 tm
->tm_mday
= bcd2bin(cal
>> 0);
133 tm
->tm_mon
= bcd2bin(cal
>> 8);
134 tm
->tm_mon
= tm
->tm_mon
- 1;
135 tm
->tm_year
= bcd2bin(cal
>> 16) + 100;
137 tm
->tm_sec
= bcd2bin(time
>> 0);
138 tm
->tm_min
= bcd2bin(time
>> 8);
139 tm
->tm_hour
= bcd2bin(time
>> 16);
141 return rtc_valid_tm(tm
);
144 static int ma35d1_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
146 struct ma35_rtc
*rtc
= dev_get_drvdata(dev
);
149 val
= bin2bcd(tm
->tm_mday
) << 0 | bin2bcd(tm
->tm_mon
+ 1) << 8 |
150 bin2bcd(tm
->tm_year
- 100) << 16;
151 rtc_reg_write(rtc
, MA35_REG_RTC_CAL
, val
);
153 val
= bin2bcd(tm
->tm_sec
) << 0 | bin2bcd(tm
->tm_min
) << 8 |
154 bin2bcd(tm
->tm_hour
) << 16;
155 rtc_reg_write(rtc
, MA35_REG_RTC_TIME
, val
);
158 rtc_reg_write(rtc
, MA35_REG_RTC_WEEKDAY
, val
);
163 static int ma35d1_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
165 struct ma35_rtc
*rtc
= dev_get_drvdata(dev
);
168 talm
= rtc_reg_read(rtc
, MA35_REG_RTC_TALM
);
169 calm
= rtc_reg_read(rtc
, MA35_REG_RTC_CALM
);
171 alrm
->time
.tm_mday
= bcd2bin(calm
>> 0);
172 alrm
->time
.tm_mon
= bcd2bin(calm
>> 8);
173 alrm
->time
.tm_mon
= alrm
->time
.tm_mon
- 1;
175 alrm
->time
.tm_year
= bcd2bin(calm
>> 16) + 100;
177 alrm
->time
.tm_sec
= bcd2bin(talm
>> 0);
178 alrm
->time
.tm_min
= bcd2bin(talm
>> 8);
179 alrm
->time
.tm_hour
= bcd2bin(talm
>> 16);
181 return rtc_valid_tm(&alrm
->time
);
184 static int ma35d1_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
186 struct ma35_rtc
*rtc
= dev_get_drvdata(dev
);
189 val
= bin2bcd(alrm
->time
.tm_mday
) << 0 | bin2bcd(alrm
->time
.tm_mon
+ 1) << 8 |
190 bin2bcd(alrm
->time
.tm_year
- 100) << 16;
191 rtc_reg_write(rtc
, MA35_REG_RTC_CALM
, val
);
193 val
= bin2bcd(alrm
->time
.tm_sec
) << 0 | bin2bcd(alrm
->time
.tm_min
) << 8 |
194 bin2bcd(alrm
->time
.tm_hour
) << 16;
195 rtc_reg_write(rtc
, MA35_REG_RTC_TALM
, val
);
197 ma35d1_alarm_irq_enable(dev
, alrm
->enabled
);
202 static const struct rtc_class_ops ma35d1_rtc_ops
= {
203 .read_time
= ma35d1_rtc_read_time
,
204 .set_time
= ma35d1_rtc_set_time
,
205 .read_alarm
= ma35d1_rtc_read_alarm
,
206 .set_alarm
= ma35d1_rtc_set_alarm
,
207 .alarm_irq_enable
= ma35d1_alarm_irq_enable
,
210 static int ma35d1_rtc_probe(struct platform_device
*pdev
)
212 struct ma35_rtc
*rtc
;
216 rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*rtc
), GFP_KERNEL
);
220 rtc
->rtc_reg
= devm_platform_ioremap_resource(pdev
, 0);
221 if (IS_ERR(rtc
->rtc_reg
))
222 return PTR_ERR(rtc
->rtc_reg
);
224 clk
= of_clk_get(pdev
->dev
.of_node
, 0);
226 return dev_err_probe(&pdev
->dev
, PTR_ERR(clk
), "failed to find rtc clock\n");
228 ret
= clk_prepare_enable(clk
);
232 if (!(rtc_reg_read(rtc
, MA35_REG_RTC_INIT
) & RTC_INIT_ACTIVE
)) {
233 ret
= ma35d1_rtc_init(rtc
, RTC_INIT_TIMEOUT
);
235 return dev_err_probe(&pdev
->dev
, ret
, "rtc init failed\n");
238 rtc
->irq_num
= platform_get_irq(pdev
, 0);
240 ret
= devm_request_irq(&pdev
->dev
, rtc
->irq_num
, ma35d1_rtc_interrupt
,
241 IRQF_NO_SUSPEND
, "ma35d1rtc", rtc
);
243 return dev_err_probe(&pdev
->dev
, ret
, "Failed to request rtc irq\n");
245 platform_set_drvdata(pdev
, rtc
);
247 device_init_wakeup(&pdev
->dev
, true);
249 rtc
->rtcdev
= devm_rtc_allocate_device(&pdev
->dev
);
250 if (IS_ERR(rtc
->rtcdev
))
251 return PTR_ERR(rtc
->rtcdev
);
253 rtc
->rtcdev
->ops
= &ma35d1_rtc_ops
;
254 rtc
->rtcdev
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
255 rtc
->rtcdev
->range_max
= RTC_TIMESTAMP_END_2099
;
257 ret
= devm_rtc_register_device(rtc
->rtcdev
);
259 return dev_err_probe(&pdev
->dev
, ret
, "Failed to register rtc device\n");
264 static int ma35d1_rtc_suspend(struct platform_device
*pdev
, pm_message_t state
)
266 struct ma35_rtc
*rtc
= platform_get_drvdata(pdev
);
268 if (device_may_wakeup(&pdev
->dev
))
269 enable_irq_wake(rtc
->irq_num
);
274 static int ma35d1_rtc_resume(struct platform_device
*pdev
)
276 struct ma35_rtc
*rtc
= platform_get_drvdata(pdev
);
278 if (device_may_wakeup(&pdev
->dev
))
279 disable_irq_wake(rtc
->irq_num
);
284 static const struct of_device_id ma35d1_rtc_of_match
[] = {
285 { .compatible
= "nuvoton,ma35d1-rtc", },
288 MODULE_DEVICE_TABLE(of
, ma35d1_rtc_of_match
);
290 static struct platform_driver ma35d1_rtc_driver
= {
291 .suspend
= ma35d1_rtc_suspend
,
292 .resume
= ma35d1_rtc_resume
,
293 .probe
= ma35d1_rtc_probe
,
295 .name
= "rtc-ma35d1",
296 .of_match_table
= ma35d1_rtc_of_match
,
300 module_platform_driver(ma35d1_rtc_driver
);
302 MODULE_AUTHOR("Ming-Jen Chen <mjchen@nuvoton.com>");
303 MODULE_DESCRIPTION("MA35D1 RTC driver");
304 MODULE_LICENSE("GPL");