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/interrupt.h>
9 #include <linux/iopoll.h>
10 #include <linux/kernel.h>
11 #include <linux/mailbox_controller.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/slab.h>
16 struct mtk_adsp_mbox_priv {
18 struct mbox_controller mbox;
19 void __iomem *va_mboxreg;
20 const struct mtk_adsp_mbox_cfg *cfg;
23 struct mtk_adsp_mbox_cfg {
30 static inline struct mtk_adsp_mbox_priv *get_mtk_adsp_mbox_priv(struct mbox_controller *mbox)
32 return container_of(mbox, struct mtk_adsp_mbox_priv, mbox);
35 static irqreturn_t mtk_adsp_mbox_irq(int irq, void *data)
37 struct mbox_chan *chan = data;
38 struct mtk_adsp_mbox_priv *priv = get_mtk_adsp_mbox_priv(chan->mbox);
39 u32 op = readl(priv->va_mboxreg + priv->cfg->set_out);
41 writel(op, priv->va_mboxreg + priv->cfg->clr_out);
43 return IRQ_WAKE_THREAD;
46 static irqreturn_t mtk_adsp_mbox_isr(int irq, void *data)
48 struct mbox_chan *chan = data;
50 mbox_chan_received_data(chan, NULL);
55 static struct mbox_chan *mtk_adsp_mbox_xlate(struct mbox_controller *mbox,
56 const struct of_phandle_args *sp)
61 static int mtk_adsp_mbox_startup(struct mbox_chan *chan)
63 struct mtk_adsp_mbox_priv *priv = get_mtk_adsp_mbox_priv(chan->mbox);
65 /* Clear ADSP mbox command */
66 writel(0xFFFFFFFF, priv->va_mboxreg + priv->cfg->clr_in);
67 writel(0xFFFFFFFF, priv->va_mboxreg + priv->cfg->clr_out);
72 static void mtk_adsp_mbox_shutdown(struct mbox_chan *chan)
74 struct mtk_adsp_mbox_priv *priv = get_mtk_adsp_mbox_priv(chan->mbox);
76 /* Clear ADSP mbox command */
77 writel(0xFFFFFFFF, priv->va_mboxreg + priv->cfg->clr_in);
78 writel(0xFFFFFFFF, priv->va_mboxreg + priv->cfg->clr_out);
81 static int mtk_adsp_mbox_send_data(struct mbox_chan *chan, void *data)
83 struct mtk_adsp_mbox_priv *priv = get_mtk_adsp_mbox_priv(chan->mbox);
86 writel(*msg, priv->va_mboxreg + priv->cfg->set_in);
91 static bool mtk_adsp_mbox_last_tx_done(struct mbox_chan *chan)
93 struct mtk_adsp_mbox_priv *priv = get_mtk_adsp_mbox_priv(chan->mbox);
95 return readl(priv->va_mboxreg + priv->cfg->set_in) == 0;
98 static const struct mbox_chan_ops mtk_adsp_mbox_chan_ops = {
99 .send_data = mtk_adsp_mbox_send_data,
100 .startup = mtk_adsp_mbox_startup,
101 .shutdown = mtk_adsp_mbox_shutdown,
102 .last_tx_done = mtk_adsp_mbox_last_tx_done,
105 static int mtk_adsp_mbox_probe(struct platform_device *pdev)
107 struct device *dev = &pdev->dev;
108 struct mtk_adsp_mbox_priv *priv;
109 const struct mtk_adsp_mbox_cfg *cfg;
110 struct mbox_controller *mbox;
113 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
119 mbox->ops = &mtk_adsp_mbox_chan_ops;
120 mbox->txdone_irq = false;
121 mbox->txdone_poll = true;
122 mbox->of_xlate = mtk_adsp_mbox_xlate;
124 mbox->chans = devm_kzalloc(dev, sizeof(*mbox->chans), GFP_KERNEL);
128 priv->va_mboxreg = devm_platform_ioremap_resource(pdev, 0);
129 if (IS_ERR(priv->va_mboxreg))
130 return PTR_ERR(priv->va_mboxreg);
132 cfg = of_device_get_match_data(dev);
137 irq = platform_get_irq(pdev, 0);
141 ret = devm_request_threaded_irq(dev, irq, mtk_adsp_mbox_irq,
142 mtk_adsp_mbox_isr, IRQF_TRIGGER_NONE,
143 dev_name(dev), mbox->chans);
147 platform_set_drvdata(pdev, priv);
149 return devm_mbox_controller_register(dev, &priv->mbox);
152 static const struct mtk_adsp_mbox_cfg mt8186_adsp_mbox_cfg = {
159 static const struct mtk_adsp_mbox_cfg mt8195_adsp_mbox_cfg = {
166 static const struct of_device_id mtk_adsp_mbox_of_match[] = {
167 { .compatible = "mediatek,mt8186-adsp-mbox", .data = &mt8186_adsp_mbox_cfg },
168 { .compatible = "mediatek,mt8195-adsp-mbox", .data = &mt8195_adsp_mbox_cfg },
171 MODULE_DEVICE_TABLE(of, mtk_adsp_mbox_of_match);
173 static struct platform_driver mtk_adsp_mbox_driver = {
174 .probe = mtk_adsp_mbox_probe,
176 .name = "mtk_adsp_mbox",
177 .of_match_table = mtk_adsp_mbox_of_match,
180 module_platform_driver(mtk_adsp_mbox_driver);
182 MODULE_AUTHOR("Allen-KH Cheng <Allen-KH.Cheng@mediatek.com>");
183 MODULE_DESCRIPTION("MTK ADSP Mailbox Controller");
184 MODULE_LICENSE("GPL v2");