^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2019 NXP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Daniel Baluta <daniel.baluta@nxp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Implementation of the DSP IPC interface (host side)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/firmware/imx/dsp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mailbox_client.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * imx_dsp_ring_doorbell - triggers an interrupt on the other side (DSP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @dsp: DSP IPC handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @chan_idx: index of the channel where to trigger the interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Returns non-negative value for success, negative value for error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int imx_dsp_ring_doorbell(struct imx_dsp_ipc *ipc, unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct imx_dsp_chan *dsp_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (idx >= DSP_MU_CHAN_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) dsp_chan = &ipc->chans[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ret = mbox_send_message(dsp_chan->ch, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) EXPORT_SYMBOL(imx_dsp_ring_doorbell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * imx_dsp_handle_rx - rx callback used by imx mailbox
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @c: mbox client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @msg: message received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Users of DSP IPC will need to privde handle_reply and handle_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void imx_dsp_handle_rx(struct mbox_client *c, void *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct imx_dsp_chan *chan = container_of(c, struct imx_dsp_chan, cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (chan->idx == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) chan->ipc->ops->handle_reply(chan->ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) chan->ipc->ops->handle_request(chan->ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) imx_dsp_ring_doorbell(chan->ipc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int imx_dsp_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct imx_dsp_ipc *dsp_ipc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct imx_dsp_chan *dsp_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct mbox_client *cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) char *chan_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dsp_ipc = devm_kzalloc(dev, sizeof(*dsp_ipc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!dsp_ipc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i = 0; i < DSP_MU_CHAN_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (i < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) chan_name = kasprintf(GFP_KERNEL, "txdb%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) chan_name = kasprintf(GFP_KERNEL, "rxdb%d", i - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!chan_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dsp_chan = &dsp_ipc->chans[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) cl = &dsp_chan->cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) cl->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cl->tx_block = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cl->knows_txdone = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cl->rx_callback = imx_dsp_handle_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dsp_chan->ipc = dsp_ipc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dsp_chan->idx = i % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) dsp_chan->ch = mbox_request_channel_byname(cl, chan_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (IS_ERR(dsp_chan->ch)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = PTR_ERR(dsp_chan->ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_err(dev, "Failed to request mbox chan %s ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) chan_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_dbg(dev, "request mbox chan %s\n", chan_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* chan_name is not used anymore by framework */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kfree(chan_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dsp_ipc->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dev_set_drvdata(dev, dsp_ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev_info(dev, "NXP i.MX DSP IPC initialized\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) kfree(chan_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (j = 0; j < i; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) dsp_chan = &dsp_ipc->chans[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) mbox_free_channel(dsp_chan->ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int imx_dsp_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct imx_dsp_chan *dsp_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct imx_dsp_ipc *dsp_ipc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dsp_ipc = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) for (i = 0; i < DSP_MU_CHAN_NUM; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dsp_chan = &dsp_ipc->chans[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) mbox_free_channel(dsp_chan->ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static struct platform_driver imx_dsp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .name = "imx-dsp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .probe = imx_dsp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .remove = imx_dsp_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) builtin_platform_driver(imx_dsp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_AUTHOR("Daniel Baluta <daniel.baluta@nxp.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_DESCRIPTION("IMX DSP IPC protocol driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) MODULE_LICENSE("GPL v2");