mtk-adsp-ipc.c 3.4 KB

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