1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 MediaTek Corporation. All rights reserved.
4 * Author: Allen-KH Cheng <allen-kh.cheng@mediatek.com>
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>
16 * mtk_adsp_ipc_send - send ipc cmd to MTK ADSP
18 * @ipc: ADSP IPC handle
19 * @idx: index of the mailbox channel
20 * @msg: IPC cmd (reply or request)
22 * Returns zero for success from mbox_send_message
23 * negative value for error
25 int mtk_adsp_ipc_send(struct mtk_adsp_ipc *ipc, unsigned int idx, uint32_t msg)
27 struct mtk_adsp_chan *adsp_chan;
30 if (idx >= MTK_ADSP_MBOX_NUM)
33 adsp_chan = &ipc->chans[idx];
34 ret = mbox_send_message(adsp_chan->ch, &msg);
40 EXPORT_SYMBOL_GPL(mtk_adsp_ipc_send);
43 * mtk_adsp_ipc_recv - recv callback used by MTK ADSP mailbox
46 * @msg: message received
48 * Users of ADSP IPC will need to privde handle_reply and handle_request
51 static void mtk_adsp_ipc_recv(struct mbox_client *c, void *msg)
53 struct mtk_adsp_chan *chan = container_of(c, struct mtk_adsp_chan, cl);
54 struct device *dev = c->dev;
57 case MTK_ADSP_MBOX_REPLY:
58 chan->ipc->ops->handle_reply(chan->ipc);
60 case MTK_ADSP_MBOX_REQUEST:
61 chan->ipc->ops->handle_request(chan->ipc);
64 dev_err(dev, "wrong mbox chan %d\n", chan->idx);
69 static int mtk_adsp_ipc_probe(struct platform_device *pdev)
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;
79 device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
81 adsp_ipc = devm_kzalloc(dev, sizeof(*adsp_ipc), GFP_KERNEL);
85 for (i = 0; i < MTK_ADSP_MBOX_NUM; i++) {
86 chan_name = kasprintf(GFP_KERNEL, "mbox%d", i);
92 adsp_chan = &adsp_ipc->chans[i];
94 cl->dev = dev->parent;
96 cl->knows_txdone = false;
97 cl->tx_prepare = NULL;
98 cl->rx_callback = mtk_adsp_ipc_recv;
100 adsp_chan->ipc = adsp_ipc;
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",
111 dev_dbg(dev, "request mbox chan %s\n", chan_name);
116 dev_set_drvdata(dev, adsp_ipc);
117 dev_dbg(dev, "MTK ADSP IPC initialized\n");
124 for (j = 0; j < i; j++) {
125 adsp_chan = &adsp_ipc->chans[j];
126 mbox_free_channel(adsp_chan->ch);
132 static int mtk_adsp_ipc_remove(struct platform_device *pdev)
134 struct mtk_adsp_ipc *adsp_ipc = dev_get_drvdata(&pdev->dev);
135 struct mtk_adsp_chan *adsp_chan;
138 for (i = 0; i < MTK_ADSP_MBOX_NUM; i++) {
139 adsp_chan = &adsp_ipc->chans[i];
140 mbox_free_channel(adsp_chan->ch);
146 static struct platform_driver mtk_adsp_ipc_driver = {
148 .name = "mtk-adsp-ipc",
150 .probe = mtk_adsp_ipc_probe,
151 .remove = mtk_adsp_ipc_remove,
153 builtin_platform_driver(mtk_adsp_ipc_driver);
155 MODULE_AUTHOR("Allen-KH Cheng <allen-kh.cheng@mediatek.com>");
156 MODULE_DESCRIPTION("MTK ADSP IPC Driver");
157 MODULE_LICENSE("GPL");