Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / firmware / imx / imx-scu-irq.c
blob6125cccc9ba79cd3445a720935b5c0b276c83d73
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2019,2023 NXP
5 * Implementation of the SCU IRQ functions using MU.
7 */
9 #include <dt-bindings/firmware/imx/rsrc.h>
10 #include <linux/firmware/imx/ipc.h>
11 #include <linux/firmware/imx/sci.h>
12 #include <linux/kobject.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/of.h>
15 #include <linux/suspend.h>
16 #include <linux/sysfs.h>
18 #define IMX_SC_IRQ_FUNC_ENABLE 1
19 #define IMX_SC_IRQ_FUNC_STATUS 2
20 #define IMX_SC_IRQ_NUM_GROUP 9
22 static u32 mu_resource_id;
24 struct imx_sc_msg_irq_get_status {
25 struct imx_sc_rpc_msg hdr;
26 union {
27 struct {
28 u16 resource;
29 u8 group;
30 u8 reserved;
31 } __packed req;
32 struct {
33 u32 status;
34 } resp;
35 } data;
38 struct imx_sc_msg_irq_enable {
39 struct imx_sc_rpc_msg hdr;
40 u32 mask;
41 u16 resource;
42 u8 group;
43 u8 enable;
44 } __packed;
46 struct scu_wakeup {
47 u32 mask;
48 u32 wakeup_src;
49 bool valid;
52 /* Sysfs functions */
53 static struct kobject *wakeup_obj;
54 static ssize_t wakeup_source_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf);
55 static struct kobj_attribute wakeup_source_attr =
56 __ATTR(wakeup_src, 0660, wakeup_source_show, NULL);
58 static struct scu_wakeup scu_irq_wakeup[IMX_SC_IRQ_NUM_GROUP];
60 static struct imx_sc_ipc *imx_sc_irq_ipc_handle;
61 static struct work_struct imx_sc_irq_work;
62 static BLOCKING_NOTIFIER_HEAD(imx_scu_irq_notifier_chain);
64 int imx_scu_irq_register_notifier(struct notifier_block *nb)
66 return blocking_notifier_chain_register(
67 &imx_scu_irq_notifier_chain, nb);
69 EXPORT_SYMBOL(imx_scu_irq_register_notifier);
71 int imx_scu_irq_unregister_notifier(struct notifier_block *nb)
73 return blocking_notifier_chain_unregister(
74 &imx_scu_irq_notifier_chain, nb);
76 EXPORT_SYMBOL(imx_scu_irq_unregister_notifier);
78 static int imx_scu_irq_notifier_call_chain(unsigned long status, u8 *group)
80 return blocking_notifier_call_chain(&imx_scu_irq_notifier_chain,
81 status, (void *)group);
84 static void imx_scu_irq_work_handler(struct work_struct *work)
86 u32 irq_status;
87 int ret;
88 u8 i;
90 for (i = 0; i < IMX_SC_IRQ_NUM_GROUP; i++) {
91 if (scu_irq_wakeup[i].mask) {
92 scu_irq_wakeup[i].valid = false;
93 scu_irq_wakeup[i].wakeup_src = 0;
96 ret = imx_scu_irq_get_status(i, &irq_status);
97 if (ret) {
98 pr_err("get irq group %d status failed, ret %d\n",
99 i, ret);
100 return;
103 if (!irq_status)
104 continue;
105 if (scu_irq_wakeup[i].mask & irq_status) {
106 scu_irq_wakeup[i].valid = true;
107 scu_irq_wakeup[i].wakeup_src = irq_status & scu_irq_wakeup[i].mask;
108 } else {
109 scu_irq_wakeup[i].wakeup_src = irq_status;
112 pm_system_wakeup();
113 imx_scu_irq_notifier_call_chain(irq_status, &i);
117 int imx_scu_irq_get_status(u8 group, u32 *irq_status)
119 struct imx_sc_msg_irq_get_status msg;
120 struct imx_sc_rpc_msg *hdr = &msg.hdr;
121 int ret;
123 hdr->ver = IMX_SC_RPC_VERSION;
124 hdr->svc = IMX_SC_RPC_SVC_IRQ;
125 hdr->func = IMX_SC_IRQ_FUNC_STATUS;
126 hdr->size = 2;
128 msg.data.req.resource = mu_resource_id;
129 msg.data.req.group = group;
131 ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
132 if (ret)
133 return ret;
135 if (irq_status)
136 *irq_status = msg.data.resp.status;
138 return 0;
140 EXPORT_SYMBOL(imx_scu_irq_get_status);
142 int imx_scu_irq_group_enable(u8 group, u32 mask, u8 enable)
144 struct imx_sc_msg_irq_enable msg;
145 struct imx_sc_rpc_msg *hdr = &msg.hdr;
146 int ret;
148 if (!imx_sc_irq_ipc_handle)
149 return -EPROBE_DEFER;
151 hdr->ver = IMX_SC_RPC_VERSION;
152 hdr->svc = IMX_SC_RPC_SVC_IRQ;
153 hdr->func = IMX_SC_IRQ_FUNC_ENABLE;
154 hdr->size = 3;
156 msg.resource = mu_resource_id;
157 msg.group = group;
158 msg.mask = mask;
159 msg.enable = enable;
161 ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
162 if (ret)
163 pr_err("enable irq failed, group %d, mask %d, ret %d\n",
164 group, mask, ret);
166 if (enable)
167 scu_irq_wakeup[group].mask |= mask;
168 else
169 scu_irq_wakeup[group].mask &= ~mask;
171 return ret;
173 EXPORT_SYMBOL(imx_scu_irq_group_enable);
175 static void imx_scu_irq_callback(struct mbox_client *c, void *msg)
177 schedule_work(&imx_sc_irq_work);
180 static ssize_t wakeup_source_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
182 int i;
184 for (i = 0; i < IMX_SC_IRQ_NUM_GROUP; i++) {
185 if (!scu_irq_wakeup[i].wakeup_src)
186 continue;
188 if (scu_irq_wakeup[i].valid)
189 sprintf(buf, "Wakeup source group = %d, irq = 0x%x\n",
190 i, scu_irq_wakeup[i].wakeup_src);
191 else
192 sprintf(buf, "Spurious SCU wakeup, group = %d, irq = 0x%x\n",
193 i, scu_irq_wakeup[i].wakeup_src);
196 return strlen(buf);
199 int imx_scu_enable_general_irq_channel(struct device *dev)
201 struct of_phandle_args spec;
202 struct mbox_client *cl;
203 struct mbox_chan *ch;
204 int ret = 0, i = 0;
206 ret = imx_scu_get_handle(&imx_sc_irq_ipc_handle);
207 if (ret)
208 return ret;
210 cl = devm_kzalloc(dev, sizeof(*cl), GFP_KERNEL);
211 if (!cl)
212 return -ENOMEM;
214 cl->dev = dev;
215 cl->rx_callback = imx_scu_irq_callback;
217 /* SCU general IRQ uses general interrupt channel 3 */
218 ch = mbox_request_channel_byname(cl, "gip3");
219 if (IS_ERR(ch)) {
220 ret = PTR_ERR(ch);
221 dev_err(dev, "failed to request mbox chan gip3, ret %d\n", ret);
222 devm_kfree(dev, cl);
223 return ret;
226 INIT_WORK(&imx_sc_irq_work, imx_scu_irq_work_handler);
228 if (!of_parse_phandle_with_args(dev->of_node, "mboxes",
229 "#mbox-cells", 0, &spec))
230 i = of_alias_get_id(spec.np, "mu");
232 /* use mu1 as general mu irq channel if failed */
233 if (i < 0)
234 i = 1;
236 mu_resource_id = IMX_SC_R_MU_0A + i;
238 /* Create directory under /sysfs/firmware */
239 wakeup_obj = kobject_create_and_add("scu_wakeup_source", firmware_kobj);
240 if (!wakeup_obj) {
241 ret = -ENOMEM;
242 goto free_ch;
245 ret = sysfs_create_file(wakeup_obj, &wakeup_source_attr.attr);
246 if (ret) {
247 dev_err(dev, "Cannot create wakeup source src file......\n");
248 kobject_put(wakeup_obj);
249 goto free_ch;
252 return 0;
254 free_ch:
255 mbox_free_channel(ch);
257 return ret;
259 EXPORT_SYMBOL(imx_scu_enable_general_irq_channel);