mtk-adsp-ipc.c 3.4 KB

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