imx8m.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // Copyright 2020 NXP
  4. //
  5. // Author: Daniel Baluta <daniel.baluta@nxp.com>
  6. //
  7. // Hardware interface for audio DSP on i.MX8M
  8. #include <linux/bits.h>
  9. #include <linux/firmware.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/regmap.h>
  15. #include <linux/module.h>
  16. #include <sound/sof.h>
  17. #include <sound/sof/xtensa.h>
  18. #include <linux/firmware/imx/dsp.h>
  19. #include "../ops.h"
  20. #include "../sof-of-dev.h"
  21. #include "imx-common.h"
  22. #define MBOX_OFFSET 0x800000
  23. #define MBOX_SIZE 0x1000
  24. /* DAP registers */
  25. #define IMX8M_DAP_DEBUG 0x28800000
  26. #define IMX8M_DAP_DEBUG_SIZE (64 * 1024)
  27. #define IMX8M_DAP_PWRCTL (0x4000 + 0x3020)
  28. #define IMX8M_PWRCTL_CORERESET BIT(16)
  29. /* DSP audio mix registers */
  30. #define AudioDSP_REG0 0x100
  31. #define AudioDSP_REG1 0x104
  32. #define AudioDSP_REG2 0x108
  33. #define AudioDSP_REG3 0x10c
  34. #define AudioDSP_REG2_RUNSTALL BIT(5)
  35. struct imx8m_priv {
  36. struct device *dev;
  37. struct snd_sof_dev *sdev;
  38. /* DSP IPC handler */
  39. struct imx_dsp_ipc *dsp_ipc;
  40. struct platform_device *ipc_dev;
  41. struct clk_bulk_data *clks;
  42. int clk_num;
  43. void __iomem *dap;
  44. struct regmap *regmap;
  45. };
  46. static int imx8m_get_mailbox_offset(struct snd_sof_dev *sdev)
  47. {
  48. return MBOX_OFFSET;
  49. }
  50. static int imx8m_get_window_offset(struct snd_sof_dev *sdev, u32 id)
  51. {
  52. return MBOX_OFFSET;
  53. }
  54. static void imx8m_dsp_handle_reply(struct imx_dsp_ipc *ipc)
  55. {
  56. struct imx8m_priv *priv = imx_dsp_get_data(ipc);
  57. unsigned long flags;
  58. spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
  59. snd_sof_ipc_process_reply(priv->sdev, 0);
  60. spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
  61. }
  62. static void imx8m_dsp_handle_request(struct imx_dsp_ipc *ipc)
  63. {
  64. struct imx8m_priv *priv = imx_dsp_get_data(ipc);
  65. u32 p; /* Panic code */
  66. /* Read the message from the debug box. */
  67. sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
  68. /* Check to see if the message is a panic code (0x0dead***) */
  69. if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
  70. snd_sof_dsp_panic(priv->sdev, p, true);
  71. else
  72. snd_sof_ipc_msgs_rx(priv->sdev);
  73. }
  74. static struct imx_dsp_ops imx8m_dsp_ops = {
  75. .handle_reply = imx8m_dsp_handle_reply,
  76. .handle_request = imx8m_dsp_handle_request,
  77. };
  78. static int imx8m_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
  79. {
  80. struct imx8m_priv *priv = sdev->pdata->hw_pdata;
  81. sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
  82. msg->msg_size);
  83. imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
  84. return 0;
  85. }
  86. /*
  87. * DSP control.
  88. */
  89. static int imx8m_run(struct snd_sof_dev *sdev)
  90. {
  91. struct imx8m_priv *priv = (struct imx8m_priv *)sdev->pdata->hw_pdata;
  92. regmap_update_bits(priv->regmap, AudioDSP_REG2, AudioDSP_REG2_RUNSTALL, 0);
  93. return 0;
  94. }
  95. static int imx8m_reset(struct snd_sof_dev *sdev)
  96. {
  97. struct imx8m_priv *priv = (struct imx8m_priv *)sdev->pdata->hw_pdata;
  98. u32 pwrctl;
  99. /* put DSP into reset and stall */
  100. pwrctl = readl(priv->dap + IMX8M_DAP_PWRCTL);
  101. pwrctl |= IMX8M_PWRCTL_CORERESET;
  102. writel(pwrctl, priv->dap + IMX8M_DAP_PWRCTL);
  103. /* keep reset asserted for 10 cycles */
  104. usleep_range(1, 2);
  105. regmap_update_bits(priv->regmap, AudioDSP_REG2,
  106. AudioDSP_REG2_RUNSTALL, AudioDSP_REG2_RUNSTALL);
  107. /* take the DSP out of reset and keep stalled for FW loading */
  108. pwrctl = readl(priv->dap + IMX8M_DAP_PWRCTL);
  109. pwrctl &= ~IMX8M_PWRCTL_CORERESET;
  110. writel(pwrctl, priv->dap + IMX8M_DAP_PWRCTL);
  111. return 0;
  112. }
  113. static int imx8m_probe(struct snd_sof_dev *sdev)
  114. {
  115. struct platform_device *pdev =
  116. container_of(sdev->dev, struct platform_device, dev);
  117. struct device_node *np = pdev->dev.of_node;
  118. struct device_node *res_node;
  119. struct resource *mmio;
  120. struct imx8m_priv *priv;
  121. struct resource res;
  122. u32 base, size;
  123. int ret = 0;
  124. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv)
  126. return -ENOMEM;
  127. sdev->num_cores = 1;
  128. sdev->pdata->hw_pdata = priv;
  129. priv->dev = sdev->dev;
  130. priv->sdev = sdev;
  131. priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
  132. PLATFORM_DEVID_NONE,
  133. pdev, sizeof(*pdev));
  134. if (IS_ERR(priv->ipc_dev))
  135. return PTR_ERR(priv->ipc_dev);
  136. priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
  137. if (!priv->dsp_ipc) {
  138. /* DSP IPC driver not probed yet, try later */
  139. ret = -EPROBE_DEFER;
  140. dev_err(sdev->dev, "Failed to get drvdata\n");
  141. goto exit_pdev_unregister;
  142. }
  143. imx_dsp_set_data(priv->dsp_ipc, priv);
  144. priv->dsp_ipc->ops = &imx8m_dsp_ops;
  145. /* DSP base */
  146. mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. if (mmio) {
  148. base = mmio->start;
  149. size = resource_size(mmio);
  150. } else {
  151. dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
  152. ret = -EINVAL;
  153. goto exit_pdev_unregister;
  154. }
  155. priv->dap = devm_ioremap(sdev->dev, IMX8M_DAP_DEBUG, IMX8M_DAP_DEBUG_SIZE);
  156. if (!priv->dap) {
  157. dev_err(sdev->dev, "error: failed to map DAP debug memory area");
  158. ret = -ENODEV;
  159. goto exit_pdev_unregister;
  160. }
  161. sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
  162. if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
  163. dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
  164. base, size);
  165. ret = -ENODEV;
  166. goto exit_pdev_unregister;
  167. }
  168. sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
  169. res_node = of_parse_phandle(np, "memory-region", 0);
  170. if (!res_node) {
  171. dev_err(&pdev->dev, "failed to get memory region node\n");
  172. ret = -ENODEV;
  173. goto exit_pdev_unregister;
  174. }
  175. ret = of_address_to_resource(res_node, 0, &res);
  176. of_node_put(res_node);
  177. if (ret) {
  178. dev_err(&pdev->dev, "failed to get reserved region address\n");
  179. goto exit_pdev_unregister;
  180. }
  181. sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
  182. resource_size(&res));
  183. if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
  184. dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
  185. base, size);
  186. ret = -ENOMEM;
  187. goto exit_pdev_unregister;
  188. }
  189. sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
  190. /* set default mailbox offset for FW ready message */
  191. sdev->dsp_box.offset = MBOX_OFFSET;
  192. priv->regmap = syscon_regmap_lookup_by_phandle(np, "fsl,dsp-ctrl");
  193. if (IS_ERR(priv->regmap)) {
  194. dev_err(sdev->dev, "cannot find dsp-ctrl registers");
  195. ret = PTR_ERR(priv->regmap);
  196. goto exit_pdev_unregister;
  197. }
  198. ret = devm_clk_bulk_get_all(sdev->dev, &priv->clks);
  199. if (ret < 0) {
  200. dev_err(sdev->dev, "failed to fetch clocks: %d\n", ret);
  201. goto exit_pdev_unregister;
  202. }
  203. priv->clk_num = ret;
  204. ret = clk_bulk_prepare_enable(priv->clk_num, priv->clks);
  205. if (ret < 0) {
  206. dev_err(sdev->dev, "failed to enable clocks: %d\n", ret);
  207. goto exit_pdev_unregister;
  208. }
  209. return 0;
  210. exit_pdev_unregister:
  211. platform_device_unregister(priv->ipc_dev);
  212. return ret;
  213. }
  214. static void imx8m_remove(struct snd_sof_dev *sdev)
  215. {
  216. struct imx8m_priv *priv = sdev->pdata->hw_pdata;
  217. clk_bulk_disable_unprepare(priv->clk_num, priv->clks);
  218. platform_device_unregister(priv->ipc_dev);
  219. }
  220. /* on i.MX8 there is 1 to 1 match between type and BAR idx */
  221. static int imx8m_get_bar_index(struct snd_sof_dev *sdev, u32 type)
  222. {
  223. /* Only IRAM and SRAM bars are valid */
  224. switch (type) {
  225. case SOF_FW_BLK_TYPE_IRAM:
  226. case SOF_FW_BLK_TYPE_SRAM:
  227. return type;
  228. default:
  229. return -EINVAL;
  230. }
  231. }
  232. static struct snd_soc_dai_driver imx8m_dai[] = {
  233. {
  234. .name = "sai1",
  235. .playback = {
  236. .channels_min = 1,
  237. .channels_max = 32,
  238. },
  239. .capture = {
  240. .channels_min = 1,
  241. .channels_max = 32,
  242. },
  243. },
  244. {
  245. .name = "sai3",
  246. .playback = {
  247. .channels_min = 1,
  248. .channels_max = 32,
  249. },
  250. .capture = {
  251. .channels_min = 1,
  252. .channels_max = 32,
  253. },
  254. },
  255. {
  256. .name = "micfil",
  257. .capture = {
  258. .channels_min = 1,
  259. .channels_max = 8,
  260. },
  261. },
  262. };
  263. static int imx8m_dsp_set_power_state(struct snd_sof_dev *sdev,
  264. const struct sof_dsp_power_state *target_state)
  265. {
  266. sdev->dsp_power_state = *target_state;
  267. return 0;
  268. }
  269. static int imx8m_resume(struct snd_sof_dev *sdev)
  270. {
  271. struct imx8m_priv *priv = (struct imx8m_priv *)sdev->pdata->hw_pdata;
  272. int ret;
  273. int i;
  274. ret = clk_bulk_prepare_enable(priv->clk_num, priv->clks);
  275. if (ret < 0) {
  276. dev_err(sdev->dev, "failed to enable clocks: %d\n", ret);
  277. return ret;
  278. }
  279. for (i = 0; i < DSP_MU_CHAN_NUM; i++)
  280. imx_dsp_request_channel(priv->dsp_ipc, i);
  281. return 0;
  282. }
  283. static void imx8m_suspend(struct snd_sof_dev *sdev)
  284. {
  285. struct imx8m_priv *priv = (struct imx8m_priv *)sdev->pdata->hw_pdata;
  286. int i;
  287. for (i = 0; i < DSP_MU_CHAN_NUM; i++)
  288. imx_dsp_free_channel(priv->dsp_ipc, i);
  289. clk_bulk_disable_unprepare(priv->clk_num, priv->clks);
  290. }
  291. static int imx8m_dsp_runtime_resume(struct snd_sof_dev *sdev)
  292. {
  293. int ret;
  294. const struct sof_dsp_power_state target_dsp_state = {
  295. .state = SOF_DSP_PM_D0,
  296. };
  297. ret = imx8m_resume(sdev);
  298. if (ret < 0)
  299. return ret;
  300. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  301. }
  302. static int imx8m_dsp_runtime_suspend(struct snd_sof_dev *sdev)
  303. {
  304. const struct sof_dsp_power_state target_dsp_state = {
  305. .state = SOF_DSP_PM_D3,
  306. };
  307. imx8m_suspend(sdev);
  308. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  309. }
  310. static int imx8m_dsp_resume(struct snd_sof_dev *sdev)
  311. {
  312. int ret;
  313. const struct sof_dsp_power_state target_dsp_state = {
  314. .state = SOF_DSP_PM_D0,
  315. };
  316. ret = imx8m_resume(sdev);
  317. if (ret < 0)
  318. return ret;
  319. if (pm_runtime_suspended(sdev->dev)) {
  320. pm_runtime_disable(sdev->dev);
  321. pm_runtime_set_active(sdev->dev);
  322. pm_runtime_mark_last_busy(sdev->dev);
  323. pm_runtime_enable(sdev->dev);
  324. pm_runtime_idle(sdev->dev);
  325. }
  326. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  327. }
  328. static int imx8m_dsp_suspend(struct snd_sof_dev *sdev, unsigned int target_state)
  329. {
  330. const struct sof_dsp_power_state target_dsp_state = {
  331. .state = target_state,
  332. };
  333. if (!pm_runtime_suspended(sdev->dev))
  334. imx8m_suspend(sdev);
  335. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  336. }
  337. /* i.MX8 ops */
  338. static const struct snd_sof_dsp_ops sof_imx8m_ops = {
  339. /* probe and remove */
  340. .probe = imx8m_probe,
  341. .remove = imx8m_remove,
  342. /* DSP core boot */
  343. .run = imx8m_run,
  344. .reset = imx8m_reset,
  345. /* Block IO */
  346. .block_read = sof_block_read,
  347. .block_write = sof_block_write,
  348. /* Mailbox IO */
  349. .mailbox_read = sof_mailbox_read,
  350. .mailbox_write = sof_mailbox_write,
  351. /* ipc */
  352. .send_msg = imx8m_send_msg,
  353. .get_mailbox_offset = imx8m_get_mailbox_offset,
  354. .get_window_offset = imx8m_get_window_offset,
  355. .ipc_msg_data = sof_ipc_msg_data,
  356. .set_stream_data_offset = sof_set_stream_data_offset,
  357. .get_bar_index = imx8m_get_bar_index,
  358. /* firmware loading */
  359. .load_firmware = snd_sof_load_firmware_memcpy,
  360. /* Debug information */
  361. .dbg_dump = imx8_dump,
  362. .debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
  363. /* stream callbacks */
  364. .pcm_open = sof_stream_pcm_open,
  365. .pcm_close = sof_stream_pcm_close,
  366. /* Firmware ops */
  367. .dsp_arch_ops = &sof_xtensa_arch_ops,
  368. /* DAI drivers */
  369. .drv = imx8m_dai,
  370. .num_drv = ARRAY_SIZE(imx8m_dai),
  371. .suspend = imx8m_dsp_suspend,
  372. .resume = imx8m_dsp_resume,
  373. .runtime_suspend = imx8m_dsp_runtime_suspend,
  374. .runtime_resume = imx8m_dsp_runtime_resume,
  375. .set_power_state = imx8m_dsp_set_power_state,
  376. .hw_info = SNDRV_PCM_INFO_MMAP |
  377. SNDRV_PCM_INFO_MMAP_VALID |
  378. SNDRV_PCM_INFO_INTERLEAVED |
  379. SNDRV_PCM_INFO_PAUSE |
  380. SNDRV_PCM_INFO_BATCH |
  381. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  382. };
  383. static struct snd_sof_of_mach sof_imx8mp_machs[] = {
  384. {
  385. .compatible = "fsl,imx8mp-evk",
  386. .sof_tplg_filename = "sof-imx8mp-wm8960.tplg",
  387. .drv_name = "asoc-audio-graph-card2",
  388. },
  389. {}
  390. };
  391. static struct sof_dev_desc sof_of_imx8mp_desc = {
  392. .of_machines = sof_imx8mp_machs,
  393. .ipc_supported_mask = BIT(SOF_IPC_TYPE_3),
  394. .ipc_default = SOF_IPC_TYPE_3,
  395. .default_fw_path = {
  396. [SOF_IPC_TYPE_3] = "imx/sof",
  397. },
  398. .default_tplg_path = {
  399. [SOF_IPC_TYPE_3] = "imx/sof-tplg",
  400. },
  401. .default_fw_filename = {
  402. [SOF_IPC_TYPE_3] = "sof-imx8m.ri",
  403. },
  404. .nocodec_tplg_filename = "sof-imx8-nocodec.tplg",
  405. .ops = &sof_imx8m_ops,
  406. };
  407. static const struct of_device_id sof_of_imx8m_ids[] = {
  408. { .compatible = "fsl,imx8mp-dsp", .data = &sof_of_imx8mp_desc},
  409. { }
  410. };
  411. MODULE_DEVICE_TABLE(of, sof_of_imx8m_ids);
  412. /* DT driver definition */
  413. static struct platform_driver snd_sof_of_imx8m_driver = {
  414. .probe = sof_of_probe,
  415. .remove = sof_of_remove,
  416. .driver = {
  417. .name = "sof-audio-of-imx8m",
  418. .pm = &sof_of_pm,
  419. .of_match_table = sof_of_imx8m_ids,
  420. },
  421. };
  422. module_platform_driver(snd_sof_of_imx8m_driver);
  423. MODULE_LICENSE("Dual BSD/GPL");
  424. MODULE_DESCRIPTION("SOF support for IMX8M platforms");
  425. MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);