1 // SPDX-License-Identifier: GPL-2.0+
6 #include <dt-bindings/firmware/imx/rsrc.h>
7 #include <linux/arm-smccc.h>
8 #include <linux/firmware/imx/sci.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/rtc.h>
14 #define IMX_SC_TIMER_FUNC_GET_RTC_SEC1970 9
15 #define IMX_SC_TIMER_FUNC_SET_RTC_ALARM 8
16 #define IMX_SC_TIMER_FUNC_SET_RTC_TIME 6
18 #define IMX_SIP_SRTC 0xC2000002
19 #define IMX_SIP_SRTC_SET_TIME 0x0
21 #define SC_IRQ_GROUP_RTC 2
24 static struct imx_sc_ipc
*rtc_ipc_handle
;
25 static struct rtc_device
*imx_sc_rtc
;
27 struct imx_sc_msg_timer_get_rtc_time
{
28 struct imx_sc_rpc_msg hdr
;
32 struct imx_sc_msg_timer_rtc_set_alarm
{
33 struct imx_sc_rpc_msg hdr
;
40 } __packed
__aligned(4);
42 static int imx_sc_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
44 struct imx_sc_msg_timer_get_rtc_time msg
;
45 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
48 hdr
->ver
= IMX_SC_RPC_VERSION
;
49 hdr
->svc
= IMX_SC_RPC_SVC_TIMER
;
50 hdr
->func
= IMX_SC_TIMER_FUNC_GET_RTC_SEC1970
;
53 ret
= imx_scu_call_rpc(rtc_ipc_handle
, &msg
, true);
55 dev_err(dev
, "read rtc time failed, ret %d\n", ret
);
59 rtc_time64_to_tm(msg
.time
, tm
);
64 static int imx_sc_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
66 struct arm_smccc_res res
;
68 /* pack 2 time parameters into 1 register, 16 bits for each */
69 arm_smccc_smc(IMX_SIP_SRTC
, IMX_SIP_SRTC_SET_TIME
,
70 ((tm
->tm_year
+ 1900) << 16) | (tm
->tm_mon
+ 1),
71 (tm
->tm_mday
<< 16) | tm
->tm_hour
,
72 (tm
->tm_min
<< 16) | tm
->tm_sec
,
78 static int imx_sc_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
80 return imx_scu_irq_group_enable(SC_IRQ_GROUP_RTC
, SC_IRQ_RTC
, enable
);
83 static int imx_sc_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
86 * SCU firmware does NOT provide read alarm API, but .read_alarm
87 * callback is required by RTC framework to support alarm function,
88 * so just return here.
93 static int imx_sc_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
95 struct imx_sc_msg_timer_rtc_set_alarm msg
;
96 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
98 struct rtc_time
*alrm_tm
= &alrm
->time
;
100 hdr
->ver
= IMX_SC_RPC_VERSION
;
101 hdr
->svc
= IMX_SC_RPC_SVC_TIMER
;
102 hdr
->func
= IMX_SC_TIMER_FUNC_SET_RTC_ALARM
;
105 msg
.year
= alrm_tm
->tm_year
+ 1900;
106 msg
.mon
= alrm_tm
->tm_mon
+ 1;
107 msg
.day
= alrm_tm
->tm_mday
;
108 msg
.hour
= alrm_tm
->tm_hour
;
109 msg
.min
= alrm_tm
->tm_min
;
110 msg
.sec
= alrm_tm
->tm_sec
;
112 ret
= imx_scu_call_rpc(rtc_ipc_handle
, &msg
, true);
114 dev_err(dev
, "set rtc alarm failed, ret %d\n", ret
);
118 ret
= imx_sc_rtc_alarm_irq_enable(dev
, alrm
->enabled
);
120 dev_err(dev
, "enable rtc alarm failed, ret %d\n", ret
);
127 static const struct rtc_class_ops imx_sc_rtc_ops
= {
128 .read_time
= imx_sc_rtc_read_time
,
129 .set_time
= imx_sc_rtc_set_time
,
130 .read_alarm
= imx_sc_rtc_read_alarm
,
131 .set_alarm
= imx_sc_rtc_set_alarm
,
132 .alarm_irq_enable
= imx_sc_rtc_alarm_irq_enable
,
135 static int imx_sc_rtc_alarm_notify(struct notifier_block
*nb
,
136 unsigned long event
, void *group
)
138 /* ignore non-rtc irq */
139 if (!((event
& SC_IRQ_RTC
) && (*(u8
*)group
== SC_IRQ_GROUP_RTC
)))
142 rtc_update_irq(imx_sc_rtc
, 1, RTC_IRQF
| RTC_AF
);
147 static struct notifier_block imx_sc_rtc_alarm_sc_notifier
= {
148 .notifier_call
= imx_sc_rtc_alarm_notify
,
151 static int imx_sc_rtc_probe(struct platform_device
*pdev
)
155 ret
= imx_scu_get_handle(&rtc_ipc_handle
);
159 device_init_wakeup(&pdev
->dev
, true);
161 imx_sc_rtc
= devm_rtc_allocate_device(&pdev
->dev
);
162 if (IS_ERR(imx_sc_rtc
))
163 return PTR_ERR(imx_sc_rtc
);
165 imx_sc_rtc
->ops
= &imx_sc_rtc_ops
;
166 imx_sc_rtc
->range_min
= 0;
167 imx_sc_rtc
->range_max
= U32_MAX
;
169 ret
= devm_rtc_register_device(imx_sc_rtc
);
173 imx_scu_irq_register_notifier(&imx_sc_rtc_alarm_sc_notifier
);
178 static const struct of_device_id imx_sc_dt_ids
[] = {
179 { .compatible
= "fsl,imx8qxp-sc-rtc", },
182 MODULE_DEVICE_TABLE(of
, imx_sc_dt_ids
);
184 static struct platform_driver imx_sc_rtc_driver
= {
186 .name
= "imx-sc-rtc",
187 .of_match_table
= imx_sc_dt_ids
,
189 .probe
= imx_sc_rtc_probe
,
191 module_platform_driver(imx_sc_rtc_driver
);
193 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
194 MODULE_DESCRIPTION("NXP i.MX System Controller RTC Driver");
195 MODULE_LICENSE("GPL");