xref: /linux/drivers/firmware/mtk-adsp-ipc.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 MediaTek Corporation. All rights reserved.
4  * Author: Allen-KH Cheng <allen-kh.cheng@mediatek.com>
5  */
6 
7 #include <linux/firmware/mediatek/mtk-adsp-ipc.h>
8 #include <linux/kernel.h>
9 #include <linux/mailbox_client.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 
15 /*
16  * mtk_adsp_ipc_send - send ipc cmd to MTK ADSP
17  *
18  * @ipc: ADSP IPC handle
19  * @idx: index of the mailbox channel
20  * @msg: IPC cmd (reply or request)
21  *
22  * Returns zero for success from mbox_send_message
23  * negative value for error
24  */
25 int mtk_adsp_ipc_send(struct mtk_adsp_ipc *ipc, unsigned int idx, uint32_t msg)
26 {
27 	struct mtk_adsp_chan *adsp_chan;
28 	int ret;
29 
30 	if (idx >= MTK_ADSP_MBOX_NUM)
31 		return -EINVAL;
32 
33 	adsp_chan = &ipc->chans[idx];
34 	ret = mbox_send_message(adsp_chan->ch, &msg);
35 	if (ret < 0)
36 		return ret;
37 
38 	return 0;
39 }
40 EXPORT_SYMBOL_GPL(mtk_adsp_ipc_send);
41 
42 /*
43  * mtk_adsp_ipc_recv - recv callback used by MTK ADSP mailbox
44  *
45  * @c: mbox client
46  * @msg: message received
47  *
48  * Users of ADSP IPC will need to privde handle_reply and handle_request
49  * callbacks.
50  */
51 static void mtk_adsp_ipc_recv(struct mbox_client *c, void *msg)
52 {
53 	struct mtk_adsp_chan *chan = container_of(c, struct mtk_adsp_chan, cl);
54 	struct device *dev = c->dev;
55 
56 	switch (chan->idx) {
57 	case MTK_ADSP_MBOX_REPLY:
58 		chan->ipc->ops->handle_reply(chan->ipc);
59 		break;
60 	case MTK_ADSP_MBOX_REQUEST:
61 		chan->ipc->ops->handle_request(chan->ipc);
62 		break;
63 	default:
64 		dev_err(dev, "wrong mbox chan %d\n", chan->idx);
65 		break;
66 	}
67 }
68 
69 static int mtk_adsp_ipc_probe(struct platform_device *pdev)
70 {
71 	struct device *dev = &pdev->dev;
72 	struct mtk_adsp_ipc *adsp_ipc;
73 	struct mtk_adsp_chan *adsp_chan;
74 	struct mbox_client *cl;
75 	char *chan_name;
76 	int ret;
77 	int i, j;
78 
79 	device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
80 
81 	adsp_ipc = devm_kzalloc(dev, sizeof(*adsp_ipc), GFP_KERNEL);
82 	if (!adsp_ipc)
83 		return -ENOMEM;
84 
85 	for (i = 0; i < MTK_ADSP_MBOX_NUM; i++) {
86 		chan_name = kasprintf(GFP_KERNEL, "mbox%d", i);
87 		if (!chan_name) {
88 			ret = -ENOMEM;
89 			goto out;
90 		}
91 
92 		adsp_chan = &adsp_ipc->chans[i];
93 		cl = &adsp_chan->cl;
94 		cl->dev = dev->parent;
95 		cl->tx_block = false;
96 		cl->knows_txdone = false;
97 		cl->tx_prepare = NULL;
98 		cl->rx_callback = mtk_adsp_ipc_recv;
99 
100 		adsp_chan->ipc = adsp_ipc;
101 		adsp_chan->idx = i;
102 		adsp_chan->ch = mbox_request_channel_byname(cl, chan_name);
103 		if (IS_ERR(adsp_chan->ch)) {
104 			ret = PTR_ERR(adsp_chan->ch);
105 			if (ret != -EPROBE_DEFER)
106 				dev_err(dev, "Failed to request mbox chan %d ret %d\n",
107 					i, ret);
108 			goto out_free;
109 		}
110 
111 		dev_dbg(dev, "request mbox chan %s\n", chan_name);
112 		kfree(chan_name);
113 	}
114 
115 	adsp_ipc->dev = dev;
116 	dev_set_drvdata(dev, adsp_ipc);
117 	dev_dbg(dev, "MTK ADSP IPC initialized\n");
118 
119 	return 0;
120 
121 out_free:
122 	kfree(chan_name);
123 out:
124 	for (j = 0; j < i; j++) {
125 		adsp_chan = &adsp_ipc->chans[j];
126 		mbox_free_channel(adsp_chan->ch);
127 	}
128 
129 	return ret;
130 }
131 
132 static int mtk_adsp_ipc_remove(struct platform_device *pdev)
133 {
134 	struct mtk_adsp_ipc *adsp_ipc = dev_get_drvdata(&pdev->dev);
135 	struct mtk_adsp_chan *adsp_chan;
136 	int i;
137 
138 	for (i = 0; i < MTK_ADSP_MBOX_NUM; i++) {
139 		adsp_chan = &adsp_ipc->chans[i];
140 		mbox_free_channel(adsp_chan->ch);
141 	}
142 
143 	return 0;
144 }
145 
146 static struct platform_driver mtk_adsp_ipc_driver = {
147 	.driver = {
148 		.name = "mtk-adsp-ipc",
149 	},
150 	.probe = mtk_adsp_ipc_probe,
151 	.remove = mtk_adsp_ipc_remove,
152 };
153 builtin_platform_driver(mtk_adsp_ipc_driver);
154 
155 MODULE_AUTHOR("Allen-KH Cheng <allen-kh.cheng@mediatek.com>");
156 MODULE_DESCRIPTION("MTK ADSP IPC Driver");
157 MODULE_LICENSE("GPL");
158