pcm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <[email protected]>
  9. //
  10. // PCM Layer, interface between ALSA and IPC.
  11. //
  12. #include <linux/pm_runtime.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/sof.h>
  15. #include <trace/events/sof.h>
  16. #include "sof-of-dev.h"
  17. #include "sof-priv.h"
  18. #include "sof-audio.h"
  19. #include "sof-utils.h"
  20. #include "ops.h"
  21. /* Create DMA buffer page table for DSP */
  22. static int create_page_table(struct snd_soc_component *component,
  23. struct snd_pcm_substream *substream,
  24. unsigned char *dma_area, size_t size)
  25. {
  26. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  27. struct snd_sof_pcm *spcm;
  28. struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
  29. int stream = substream->stream;
  30. spcm = snd_sof_find_spcm_dai(component, rtd);
  31. if (!spcm)
  32. return -EINVAL;
  33. return snd_sof_create_page_table(component->dev, dmab,
  34. spcm->stream[stream].page_table.area, size);
  35. }
  36. /*
  37. * sof pcm period elapse work
  38. */
  39. static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
  40. {
  41. struct snd_sof_pcm_stream *sps =
  42. container_of(work, struct snd_sof_pcm_stream,
  43. period_elapsed_work);
  44. snd_pcm_period_elapsed(sps->substream);
  45. }
  46. void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
  47. {
  48. INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
  49. }
  50. /*
  51. * sof pcm period elapse, this could be called at irq thread context.
  52. */
  53. void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
  54. {
  55. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  56. struct snd_soc_component *component =
  57. snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  58. struct snd_sof_pcm *spcm;
  59. spcm = snd_sof_find_spcm_dai(component, rtd);
  60. if (!spcm) {
  61. dev_err(component->dev,
  62. "error: period elapsed for unknown stream!\n");
  63. return;
  64. }
  65. /*
  66. * snd_pcm_period_elapsed() can be called in interrupt context
  67. * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
  68. * when the PCM is done draining or xrun happened, a STOP IPC will
  69. * then be sent and this IPC will hit IPC timeout.
  70. * To avoid sending IPC before the previous IPC is handled, we
  71. * schedule delayed work here to call the snd_pcm_period_elapsed().
  72. */
  73. schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
  74. }
  75. EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
  76. static int
  77. sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
  78. struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params,
  79. struct snd_sof_platform_stream_params *platform_params, int dir)
  80. {
  81. struct snd_soc_dai *dai;
  82. int ret, j;
  83. /* query DAPM for list of connected widgets and set them up */
  84. for_each_rtd_cpu_dais(rtd, j, dai) {
  85. struct snd_soc_dapm_widget_list *list;
  86. ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
  87. dpcm_end_walk_at_be);
  88. if (ret < 0) {
  89. dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
  90. dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  91. return ret;
  92. }
  93. spcm->stream[dir].list = list;
  94. ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir);
  95. if (ret < 0) {
  96. dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
  97. spcm->pcm.pcm_id, dir);
  98. spcm->stream[dir].list = NULL;
  99. snd_soc_dapm_dai_free_widgets(&list);
  100. return ret;
  101. }
  102. }
  103. return 0;
  104. }
  105. static int sof_pcm_hw_params(struct snd_soc_component *component,
  106. struct snd_pcm_substream *substream,
  107. struct snd_pcm_hw_params *params)
  108. {
  109. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  110. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  111. struct snd_sof_platform_stream_params platform_params = { 0 };
  112. const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
  113. struct snd_pcm_runtime *runtime = substream->runtime;
  114. struct snd_sof_pcm *spcm;
  115. int ret;
  116. /* nothing to do for BE */
  117. if (rtd->dai_link->no_pcm)
  118. return 0;
  119. spcm = snd_sof_find_spcm_dai(component, rtd);
  120. if (!spcm)
  121. return -EINVAL;
  122. /*
  123. * Handle repeated calls to hw_params() without free_pcm() in
  124. * between. At least ALSA OSS emulation depends on this.
  125. */
  126. if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
  127. ret = pcm_ops->hw_free(component, substream);
  128. if (ret < 0)
  129. return ret;
  130. spcm->prepared[substream->stream] = false;
  131. }
  132. dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
  133. spcm->pcm.pcm_id, substream->stream);
  134. ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params);
  135. if (ret < 0) {
  136. dev_err(component->dev, "platform hw params failed\n");
  137. return ret;
  138. }
  139. /* if this is a repeated hw_params without hw_free, skip setting up widgets */
  140. if (!spcm->stream[substream->stream].list) {
  141. ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params,
  142. substream->stream);
  143. if (ret < 0)
  144. return ret;
  145. }
  146. /* create compressed page table for audio firmware */
  147. if (runtime->buffer_changed) {
  148. ret = create_page_table(component, substream, runtime->dma_area,
  149. runtime->dma_bytes);
  150. if (ret < 0)
  151. return ret;
  152. }
  153. if (pcm_ops->hw_params) {
  154. ret = pcm_ops->hw_params(component, substream, params, &platform_params);
  155. if (ret < 0)
  156. return ret;
  157. }
  158. spcm->prepared[substream->stream] = true;
  159. /* save pcm hw_params */
  160. memcpy(&spcm->params[substream->stream], params, sizeof(*params));
  161. return 0;
  162. }
  163. static int sof_pcm_hw_free(struct snd_soc_component *component,
  164. struct snd_pcm_substream *substream)
  165. {
  166. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  167. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  168. const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
  169. struct snd_sof_pcm *spcm;
  170. int ret, err = 0;
  171. /* nothing to do for BE */
  172. if (rtd->dai_link->no_pcm)
  173. return 0;
  174. spcm = snd_sof_find_spcm_dai(component, rtd);
  175. if (!spcm)
  176. return -EINVAL;
  177. dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
  178. spcm->pcm.pcm_id, substream->stream);
  179. /* free PCM in the DSP */
  180. if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
  181. ret = pcm_ops->hw_free(component, substream);
  182. if (ret < 0)
  183. err = ret;
  184. spcm->prepared[substream->stream] = false;
  185. }
  186. /* stop DMA */
  187. ret = snd_sof_pcm_platform_hw_free(sdev, substream);
  188. if (ret < 0) {
  189. dev_err(component->dev, "error: platform hw free failed\n");
  190. err = ret;
  191. }
  192. /* free the DAPM widget list */
  193. ret = sof_widget_list_free(sdev, spcm, substream->stream);
  194. if (ret < 0)
  195. err = ret;
  196. cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
  197. return err;
  198. }
  199. static int sof_pcm_prepare(struct snd_soc_component *component,
  200. struct snd_pcm_substream *substream)
  201. {
  202. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  203. struct snd_sof_pcm *spcm;
  204. int ret;
  205. /* nothing to do for BE */
  206. if (rtd->dai_link->no_pcm)
  207. return 0;
  208. spcm = snd_sof_find_spcm_dai(component, rtd);
  209. if (!spcm)
  210. return -EINVAL;
  211. if (spcm->prepared[substream->stream])
  212. return 0;
  213. dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
  214. spcm->pcm.pcm_id, substream->stream);
  215. /* set hw_params */
  216. ret = sof_pcm_hw_params(component,
  217. substream, &spcm->params[substream->stream]);
  218. if (ret < 0) {
  219. dev_err(component->dev,
  220. "error: set pcm hw_params after resume\n");
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. /*
  226. * FE dai link trigger actions are always executed in non-atomic context because
  227. * they involve IPC's.
  228. */
  229. static int sof_pcm_trigger(struct snd_soc_component *component,
  230. struct snd_pcm_substream *substream, int cmd)
  231. {
  232. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  233. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  234. const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
  235. struct snd_sof_pcm *spcm;
  236. bool reset_hw_params = false;
  237. bool free_widget_list = false;
  238. bool ipc_first = false;
  239. int ret = 0;
  240. /* nothing to do for BE */
  241. if (rtd->dai_link->no_pcm)
  242. return 0;
  243. spcm = snd_sof_find_spcm_dai(component, rtd);
  244. if (!spcm)
  245. return -EINVAL;
  246. dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
  247. spcm->pcm.pcm_id, substream->stream, cmd);
  248. switch (cmd) {
  249. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  250. ipc_first = true;
  251. break;
  252. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  253. break;
  254. case SNDRV_PCM_TRIGGER_START:
  255. if (spcm->stream[substream->stream].suspend_ignored) {
  256. /*
  257. * This case will be triggered when INFO_RESUME is
  258. * not supported, no need to re-start streams that
  259. * remained enabled in D0ix.
  260. */
  261. spcm->stream[substream->stream].suspend_ignored = false;
  262. return 0;
  263. }
  264. break;
  265. case SNDRV_PCM_TRIGGER_SUSPEND:
  266. if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
  267. spcm->stream[substream->stream].d0i3_compatible) {
  268. /*
  269. * trap the event, not sending trigger stop to
  270. * prevent the FW pipelines from being stopped,
  271. * and mark the flag to ignore the upcoming DAPM
  272. * PM events.
  273. */
  274. spcm->stream[substream->stream].suspend_ignored = true;
  275. return 0;
  276. }
  277. free_widget_list = true;
  278. fallthrough;
  279. case SNDRV_PCM_TRIGGER_STOP:
  280. ipc_first = true;
  281. reset_hw_params = true;
  282. break;
  283. default:
  284. dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
  285. return -EINVAL;
  286. }
  287. /*
  288. * DMA and IPC sequence is different for start and stop. Need to send
  289. * STOP IPC before stop DMA
  290. */
  291. if (!ipc_first)
  292. snd_sof_pcm_platform_trigger(sdev, substream, cmd);
  293. if (pcm_ops->trigger)
  294. ret = pcm_ops->trigger(component, substream, cmd);
  295. /* need to STOP DMA even if trigger IPC failed */
  296. if (ipc_first)
  297. snd_sof_pcm_platform_trigger(sdev, substream, cmd);
  298. /* free PCM if reset_hw_params is set and the STOP IPC is successful */
  299. if (!ret && reset_hw_params)
  300. ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream,
  301. free_widget_list);
  302. return ret;
  303. }
  304. static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
  305. struct snd_pcm_substream *substream)
  306. {
  307. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  308. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  309. struct snd_sof_pcm *spcm;
  310. snd_pcm_uframes_t host, dai;
  311. /* nothing to do for BE */
  312. if (rtd->dai_link->no_pcm)
  313. return 0;
  314. /* use dsp ops pointer callback directly if set */
  315. if (sof_ops(sdev)->pcm_pointer)
  316. return sof_ops(sdev)->pcm_pointer(sdev, substream);
  317. spcm = snd_sof_find_spcm_dai(component, rtd);
  318. if (!spcm)
  319. return -EINVAL;
  320. /* read position from DSP */
  321. host = bytes_to_frames(substream->runtime,
  322. spcm->stream[substream->stream].posn.host_posn);
  323. dai = bytes_to_frames(substream->runtime,
  324. spcm->stream[substream->stream].posn.dai_posn);
  325. trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
  326. return host;
  327. }
  328. static int sof_pcm_open(struct snd_soc_component *component,
  329. struct snd_pcm_substream *substream)
  330. {
  331. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  332. struct snd_pcm_runtime *runtime = substream->runtime;
  333. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  334. struct snd_sof_dsp_ops *ops = sof_ops(sdev);
  335. struct snd_sof_pcm *spcm;
  336. struct snd_soc_tplg_stream_caps *caps;
  337. int ret;
  338. /* nothing to do for BE */
  339. if (rtd->dai_link->no_pcm)
  340. return 0;
  341. spcm = snd_sof_find_spcm_dai(component, rtd);
  342. if (!spcm)
  343. return -EINVAL;
  344. dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
  345. spcm->pcm.pcm_id, substream->stream);
  346. caps = &spcm->pcm.caps[substream->stream];
  347. /* set runtime config */
  348. runtime->hw.info = ops->hw_info; /* platform-specific */
  349. /* set any runtime constraints based on topology */
  350. runtime->hw.formats = le64_to_cpu(caps->formats);
  351. runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
  352. runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
  353. runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
  354. runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
  355. /*
  356. * caps->buffer_size_min is not used since the
  357. * snd_pcm_hardware structure only defines buffer_bytes_max
  358. */
  359. runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
  360. dev_dbg(component->dev, "period min %zd max %zd bytes\n",
  361. runtime->hw.period_bytes_min,
  362. runtime->hw.period_bytes_max);
  363. dev_dbg(component->dev, "period count %d max %d\n",
  364. runtime->hw.periods_min,
  365. runtime->hw.periods_max);
  366. dev_dbg(component->dev, "buffer max %zd bytes\n",
  367. runtime->hw.buffer_bytes_max);
  368. /* set wait time - TODO: come from topology */
  369. substream->wait_time = 500;
  370. spcm->stream[substream->stream].posn.host_posn = 0;
  371. spcm->stream[substream->stream].posn.dai_posn = 0;
  372. spcm->stream[substream->stream].substream = substream;
  373. spcm->prepared[substream->stream] = false;
  374. ret = snd_sof_pcm_platform_open(sdev, substream);
  375. if (ret < 0)
  376. dev_err(component->dev, "error: pcm open failed %d\n", ret);
  377. return ret;
  378. }
  379. static int sof_pcm_close(struct snd_soc_component *component,
  380. struct snd_pcm_substream *substream)
  381. {
  382. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  383. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  384. struct snd_sof_pcm *spcm;
  385. int err;
  386. /* nothing to do for BE */
  387. if (rtd->dai_link->no_pcm)
  388. return 0;
  389. spcm = snd_sof_find_spcm_dai(component, rtd);
  390. if (!spcm)
  391. return -EINVAL;
  392. dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
  393. spcm->pcm.pcm_id, substream->stream);
  394. err = snd_sof_pcm_platform_close(sdev, substream);
  395. if (err < 0) {
  396. dev_err(component->dev, "error: pcm close failed %d\n",
  397. err);
  398. /*
  399. * keep going, no point in preventing the close
  400. * from happening
  401. */
  402. }
  403. return 0;
  404. }
  405. /*
  406. * Pre-allocate playback/capture audio buffer pages.
  407. * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
  408. * snd_pcm_lib_preallocate_free_for_all() is called by the core.
  409. */
  410. static int sof_pcm_new(struct snd_soc_component *component,
  411. struct snd_soc_pcm_runtime *rtd)
  412. {
  413. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  414. struct snd_sof_pcm *spcm;
  415. struct snd_pcm *pcm = rtd->pcm;
  416. struct snd_soc_tplg_stream_caps *caps;
  417. int stream = SNDRV_PCM_STREAM_PLAYBACK;
  418. /* find SOF PCM for this RTD */
  419. spcm = snd_sof_find_spcm_dai(component, rtd);
  420. if (!spcm) {
  421. dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
  422. rtd->dai_link->id);
  423. return 0;
  424. }
  425. dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
  426. /* do we need to pre-allocate playback audio buffer pages */
  427. if (!spcm->pcm.playback)
  428. goto capture;
  429. caps = &spcm->pcm.caps[stream];
  430. /* pre-allocate playback audio buffer pages */
  431. dev_dbg(component->dev,
  432. "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
  433. caps->name, caps->buffer_size_min, caps->buffer_size_max);
  434. if (!pcm->streams[stream].substream) {
  435. dev_err(component->dev, "error: NULL playback substream!\n");
  436. return -EINVAL;
  437. }
  438. snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
  439. SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
  440. 0, le32_to_cpu(caps->buffer_size_max));
  441. capture:
  442. stream = SNDRV_PCM_STREAM_CAPTURE;
  443. /* do we need to pre-allocate capture audio buffer pages */
  444. if (!spcm->pcm.capture)
  445. return 0;
  446. caps = &spcm->pcm.caps[stream];
  447. /* pre-allocate capture audio buffer pages */
  448. dev_dbg(component->dev,
  449. "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
  450. caps->name, caps->buffer_size_min, caps->buffer_size_max);
  451. if (!pcm->streams[stream].substream) {
  452. dev_err(component->dev, "error: NULL capture substream!\n");
  453. return -EINVAL;
  454. }
  455. snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
  456. SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
  457. 0, le32_to_cpu(caps->buffer_size_max));
  458. return 0;
  459. }
  460. /* fixup the BE DAI link to match any values from topology */
  461. int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
  462. {
  463. struct snd_interval *rate = hw_param_interval(params,
  464. SNDRV_PCM_HW_PARAM_RATE);
  465. struct snd_interval *channels = hw_param_interval(params,
  466. SNDRV_PCM_HW_PARAM_CHANNELS);
  467. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  468. struct snd_soc_component *component =
  469. snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  470. struct snd_sof_dai *dai =
  471. snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  472. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  473. const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
  474. /* no topology exists for this BE, try a common configuration */
  475. if (!dai) {
  476. dev_warn(component->dev,
  477. "warning: no topology found for BE DAI %s config\n",
  478. rtd->dai_link->name);
  479. /* set 48k, stereo, 16bits by default */
  480. rate->min = 48000;
  481. rate->max = 48000;
  482. channels->min = 2;
  483. channels->max = 2;
  484. snd_mask_none(fmt);
  485. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  486. return 0;
  487. }
  488. if (pcm_ops->dai_link_fixup)
  489. return pcm_ops->dai_link_fixup(rtd, params);
  490. return 0;
  491. }
  492. EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
  493. static int sof_pcm_probe(struct snd_soc_component *component)
  494. {
  495. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  496. struct snd_sof_pdata *plat_data = sdev->pdata;
  497. const char *tplg_filename;
  498. int ret;
  499. /*
  500. * make sure the device is pm_runtime_active before loading the
  501. * topology and initiating IPC or bus transactions
  502. */
  503. ret = pm_runtime_resume_and_get(component->dev);
  504. if (ret < 0 && ret != -EACCES)
  505. return ret;
  506. /* load the default topology */
  507. sdev->component = component;
  508. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  509. "%s/%s",
  510. plat_data->tplg_filename_prefix,
  511. plat_data->tplg_filename);
  512. if (!tplg_filename) {
  513. ret = -ENOMEM;
  514. goto pm_error;
  515. }
  516. ret = snd_sof_load_topology(component, tplg_filename);
  517. if (ret < 0)
  518. dev_err(component->dev, "error: failed to load DSP topology %d\n",
  519. ret);
  520. pm_error:
  521. pm_runtime_mark_last_busy(component->dev);
  522. pm_runtime_put_autosuspend(component->dev);
  523. return ret;
  524. }
  525. static void sof_pcm_remove(struct snd_soc_component *component)
  526. {
  527. /* remove topology */
  528. snd_soc_tplg_component_remove(component);
  529. }
  530. static int sof_pcm_ack(struct snd_soc_component *component,
  531. struct snd_pcm_substream *substream)
  532. {
  533. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  534. return snd_sof_pcm_platform_ack(sdev, substream);
  535. }
  536. void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
  537. {
  538. struct snd_soc_component_driver *pd = &sdev->plat_drv;
  539. struct snd_sof_pdata *plat_data = sdev->pdata;
  540. const char *drv_name;
  541. if (plat_data->machine)
  542. drv_name = plat_data->machine->drv_name;
  543. else if (plat_data->of_machine)
  544. drv_name = plat_data->of_machine->drv_name;
  545. else
  546. drv_name = NULL;
  547. pd->name = "sof-audio-component";
  548. pd->probe = sof_pcm_probe;
  549. pd->remove = sof_pcm_remove;
  550. pd->open = sof_pcm_open;
  551. pd->close = sof_pcm_close;
  552. pd->hw_params = sof_pcm_hw_params;
  553. pd->prepare = sof_pcm_prepare;
  554. pd->hw_free = sof_pcm_hw_free;
  555. pd->trigger = sof_pcm_trigger;
  556. pd->pointer = sof_pcm_pointer;
  557. pd->ack = sof_pcm_ack;
  558. #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
  559. pd->compress_ops = &sof_compressed_ops;
  560. #endif
  561. pd->pcm_construct = sof_pcm_new;
  562. pd->ignore_machine = drv_name;
  563. pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
  564. pd->be_pcm_base = SOF_BE_PCM_BASE;
  565. pd->use_dai_pcm_id = true;
  566. pd->topology_name_prefix = "sof";
  567. /* increment module refcount when a pcm is opened */
  568. pd->module_get_upon_open = 1;
  569. pd->legacy_dai_naming = 1;
  570. }