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>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
17 struct mtk_adsp_mbox_priv
{
19 struct mbox_controller mbox
;
20 void __iomem
*va_mboxreg
;
21 const struct mtk_adsp_mbox_cfg
*cfg
;
24 struct mtk_adsp_mbox_cfg
{
31 static inline struct mtk_adsp_mbox_priv
*get_mtk_adsp_mbox_priv(struct mbox_controller
*mbox
)
33 return container_of(mbox
, struct mtk_adsp_mbox_priv
, mbox
);
36 static irqreturn_t
mtk_adsp_mbox_irq(int irq
, void *data
)
38 struct mbox_chan
*chan
= data
;
39 struct mtk_adsp_mbox_priv
*priv
= get_mtk_adsp_mbox_priv(chan
->mbox
);
40 u32 op
= readl(priv
->va_mboxreg
+ priv
->cfg
->set_out
);
42 writel(op
, priv
->va_mboxreg
+ priv
->cfg
->clr_out
);
44 return IRQ_WAKE_THREAD
;
47 static irqreturn_t
mtk_adsp_mbox_isr(int irq
, void *data
)
49 struct mbox_chan
*chan
= data
;
51 mbox_chan_received_data(chan
, NULL
);
56 static struct mbox_chan
*mtk_adsp_mbox_xlate(struct mbox_controller
*mbox
,
57 const struct of_phandle_args
*sp
)
62 static int mtk_adsp_mbox_startup(struct mbox_chan
*chan
)
64 struct mtk_adsp_mbox_priv
*priv
= get_mtk_adsp_mbox_priv(chan
->mbox
);
66 /* Clear ADSP mbox command */
67 writel(0xFFFFFFFF, priv
->va_mboxreg
+ priv
->cfg
->clr_in
);
68 writel(0xFFFFFFFF, priv
->va_mboxreg
+ priv
->cfg
->clr_out
);
73 static void mtk_adsp_mbox_shutdown(struct mbox_chan
*chan
)
75 struct mtk_adsp_mbox_priv
*priv
= get_mtk_adsp_mbox_priv(chan
->mbox
);
77 /* Clear ADSP mbox command */
78 writel(0xFFFFFFFF, priv
->va_mboxreg
+ priv
->cfg
->clr_in
);
79 writel(0xFFFFFFFF, priv
->va_mboxreg
+ priv
->cfg
->clr_out
);
82 static int mtk_adsp_mbox_send_data(struct mbox_chan
*chan
, void *data
)
84 struct mtk_adsp_mbox_priv
*priv
= get_mtk_adsp_mbox_priv(chan
->mbox
);
87 writel(*msg
, priv
->va_mboxreg
+ priv
->cfg
->set_in
);
92 static bool mtk_adsp_mbox_last_tx_done(struct mbox_chan
*chan
)
94 struct mtk_adsp_mbox_priv
*priv
= get_mtk_adsp_mbox_priv(chan
->mbox
);
96 return readl(priv
->va_mboxreg
+ priv
->cfg
->set_in
) == 0;
99 static const struct mbox_chan_ops mtk_adsp_mbox_chan_ops
= {
100 .send_data
= mtk_adsp_mbox_send_data
,
101 .startup
= mtk_adsp_mbox_startup
,
102 .shutdown
= mtk_adsp_mbox_shutdown
,
103 .last_tx_done
= mtk_adsp_mbox_last_tx_done
,
106 static int mtk_adsp_mbox_probe(struct platform_device
*pdev
)
108 struct device
*dev
= &pdev
->dev
;
109 struct mtk_adsp_mbox_priv
*priv
;
110 const struct mtk_adsp_mbox_cfg
*cfg
;
111 struct mbox_controller
*mbox
;
114 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
120 mbox
->ops
= &mtk_adsp_mbox_chan_ops
;
121 mbox
->txdone_irq
= false;
122 mbox
->txdone_poll
= true;
123 mbox
->of_xlate
= mtk_adsp_mbox_xlate
;
125 mbox
->chans
= devm_kzalloc(dev
, sizeof(*mbox
->chans
), GFP_KERNEL
);
129 priv
->va_mboxreg
= devm_platform_ioremap_resource(pdev
, 0);
130 if (IS_ERR(priv
->va_mboxreg
))
131 return PTR_ERR(priv
->va_mboxreg
);
133 cfg
= of_device_get_match_data(dev
);
138 irq
= platform_get_irq(pdev
, 0);
142 ret
= devm_request_threaded_irq(dev
, irq
, mtk_adsp_mbox_irq
,
143 mtk_adsp_mbox_isr
, IRQF_TRIGGER_NONE
,
144 dev_name(dev
), mbox
->chans
);
148 platform_set_drvdata(pdev
, priv
);
150 return devm_mbox_controller_register(dev
, &priv
->mbox
);
153 static const struct mtk_adsp_mbox_cfg mt8186_adsp_mbox_cfg
= {
160 static const struct mtk_adsp_mbox_cfg mt8195_adsp_mbox_cfg
= {
167 static const struct of_device_id mtk_adsp_mbox_of_match
[] = {
168 { .compatible
= "mediatek,mt8186-adsp-mbox", .data
= &mt8186_adsp_mbox_cfg
},
169 { .compatible
= "mediatek,mt8195-adsp-mbox", .data
= &mt8195_adsp_mbox_cfg
},
172 MODULE_DEVICE_TABLE(of
, mtk_adsp_mbox_of_match
);
174 static struct platform_driver mtk_adsp_mbox_driver
= {
175 .probe
= mtk_adsp_mbox_probe
,
177 .name
= "mtk_adsp_mbox",
178 .of_match_table
= mtk_adsp_mbox_of_match
,
181 module_platform_driver(mtk_adsp_mbox_driver
);
183 MODULE_AUTHOR("Allen-KH Cheng <Allen-KH.Cheng@mediatek.com>");
184 MODULE_DESCRIPTION("MTK ADSP Mailbox Controller");
185 MODULE_LICENSE("GPL v2");