pcm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  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 = snd_soc_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 = snd_soc_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. snd_pcm_direction_name(dir));
  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 = snd_soc_substream_to_rtd(substream);
  111. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  112. struct snd_sof_platform_stream_params platform_params = { 0 };
  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 && 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 && 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 = snd_soc_substream_to_rtd(substream);
  167. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  168. struct snd_sof_pcm *spcm;
  169. int ret;
  170. /* nothing to do for BE */
  171. if (rtd->dai_link->no_pcm)
  172. return 0;
  173. spcm = snd_sof_find_spcm_dai(component, rtd);
  174. if (!spcm)
  175. return -EINVAL;
  176. dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
  177. spcm->pcm.pcm_id, substream->stream);
  178. ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true);
  179. cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
  180. return ret;
  181. }
  182. static int sof_pcm_prepare(struct snd_soc_component *component,
  183. struct snd_pcm_substream *substream)
  184. {
  185. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  186. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  187. struct snd_sof_pcm *spcm;
  188. int ret;
  189. /* nothing to do for BE */
  190. if (rtd->dai_link->no_pcm)
  191. return 0;
  192. spcm = snd_sof_find_spcm_dai(component, rtd);
  193. if (!spcm)
  194. return -EINVAL;
  195. if (spcm->prepared[substream->stream]) {
  196. if (!spcm->pending_stop[substream->stream])
  197. return 0;
  198. /*
  199. * this case should be reached in case of xruns where we absolutely
  200. * want to free-up and reset all PCM/DMA resources
  201. */
  202. ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true);
  203. if (ret < 0)
  204. return ret;
  205. }
  206. dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
  207. spcm->pcm.pcm_id, substream->stream);
  208. /* set hw_params */
  209. ret = sof_pcm_hw_params(component,
  210. substream, &spcm->params[substream->stream]);
  211. if (ret < 0) {
  212. dev_err(component->dev,
  213. "error: set pcm hw_params after resume\n");
  214. return ret;
  215. }
  216. return 0;
  217. }
  218. /*
  219. * FE dai link trigger actions are always executed in non-atomic context because
  220. * they involve IPC's.
  221. */
  222. static int sof_pcm_trigger(struct snd_soc_component *component,
  223. struct snd_pcm_substream *substream, int cmd)
  224. {
  225. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  226. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  227. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  228. struct snd_sof_pcm *spcm;
  229. bool reset_hw_params = false;
  230. bool ipc_first = false;
  231. int ret = 0;
  232. /* nothing to do for BE */
  233. if (rtd->dai_link->no_pcm)
  234. return 0;
  235. spcm = snd_sof_find_spcm_dai(component, rtd);
  236. if (!spcm)
  237. return -EINVAL;
  238. dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
  239. spcm->pcm.pcm_id, substream->stream, cmd);
  240. spcm->pending_stop[substream->stream] = false;
  241. switch (cmd) {
  242. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  243. ipc_first = true;
  244. break;
  245. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  246. if (pcm_ops && pcm_ops->ipc_first_on_start)
  247. ipc_first = true;
  248. break;
  249. case SNDRV_PCM_TRIGGER_START:
  250. if (spcm->stream[substream->stream].suspend_ignored) {
  251. /*
  252. * This case will be triggered when INFO_RESUME is
  253. * not supported, no need to re-start streams that
  254. * remained enabled in D0ix.
  255. */
  256. spcm->stream[substream->stream].suspend_ignored = false;
  257. return 0;
  258. }
  259. if (pcm_ops && pcm_ops->ipc_first_on_start)
  260. ipc_first = true;
  261. break;
  262. case SNDRV_PCM_TRIGGER_SUSPEND:
  263. /*
  264. * If DSP D0I3 is allowed during S0iX, set the suspend_ignored flag for
  265. * D0I3-compatible streams to keep the firmware pipeline running
  266. */
  267. if (pcm_ops && pcm_ops->d0i3_supported_in_s0ix &&
  268. sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
  269. spcm->stream[substream->stream].d0i3_compatible) {
  270. spcm->stream[substream->stream].suspend_ignored = true;
  271. return 0;
  272. }
  273. /* On suspend the DMA must be stopped in DSPless mode */
  274. if (sdev->dspless_mode_selected)
  275. reset_hw_params = true;
  276. fallthrough;
  277. case SNDRV_PCM_TRIGGER_STOP:
  278. ipc_first = true;
  279. if (pcm_ops && pcm_ops->reset_hw_params_during_stop)
  280. reset_hw_params = true;
  281. break;
  282. default:
  283. dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
  284. return -EINVAL;
  285. }
  286. if (!ipc_first)
  287. snd_sof_pcm_platform_trigger(sdev, substream, cmd);
  288. if (pcm_ops && pcm_ops->trigger)
  289. ret = pcm_ops->trigger(component, substream, cmd);
  290. switch (cmd) {
  291. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  292. case SNDRV_PCM_TRIGGER_START:
  293. /* invoke platform trigger to start DMA only if pcm_ops is successful */
  294. if (ipc_first && !ret)
  295. snd_sof_pcm_platform_trigger(sdev, substream, cmd);
  296. break;
  297. case SNDRV_PCM_TRIGGER_SUSPEND:
  298. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  299. case SNDRV_PCM_TRIGGER_STOP:
  300. /* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */
  301. if (!pcm_ops || !pcm_ops->platform_stop_during_hw_free)
  302. snd_sof_pcm_platform_trigger(sdev, substream, cmd);
  303. /*
  304. * set the pending_stop flag to indicate that pipeline stop has been delayed.
  305. * This will be used later to stop the pipelines during prepare when recovering
  306. * from xruns.
  307. */
  308. if (pcm_ops && pcm_ops->platform_stop_during_hw_free &&
  309. cmd == SNDRV_PCM_TRIGGER_STOP)
  310. spcm->pending_stop[substream->stream] = true;
  311. break;
  312. default:
  313. break;
  314. }
  315. /* free PCM if reset_hw_params is set and the STOP IPC is successful */
  316. if (!ret && reset_hw_params)
  317. ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false);
  318. return ret;
  319. }
  320. static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
  321. struct snd_pcm_substream *substream)
  322. {
  323. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  324. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  325. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  326. struct snd_sof_pcm *spcm;
  327. snd_pcm_uframes_t host, dai;
  328. int ret = -EOPNOTSUPP;
  329. /* nothing to do for BE */
  330. if (rtd->dai_link->no_pcm)
  331. return 0;
  332. if (pcm_ops && pcm_ops->pointer)
  333. ret = pcm_ops->pointer(component, substream, &host);
  334. if (ret != -EOPNOTSUPP)
  335. return ret ? ret : host;
  336. /* use dsp ops pointer callback directly if set */
  337. if (sof_ops(sdev)->pcm_pointer)
  338. return sof_ops(sdev)->pcm_pointer(sdev, substream);
  339. spcm = snd_sof_find_spcm_dai(component, rtd);
  340. if (!spcm)
  341. return -EINVAL;
  342. /* read position from DSP */
  343. host = bytes_to_frames(substream->runtime,
  344. spcm->stream[substream->stream].posn.host_posn);
  345. dai = bytes_to_frames(substream->runtime,
  346. spcm->stream[substream->stream].posn.dai_posn);
  347. trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
  348. return host;
  349. }
  350. static int sof_pcm_open(struct snd_soc_component *component,
  351. struct snd_pcm_substream *substream)
  352. {
  353. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  354. struct snd_pcm_runtime *runtime = substream->runtime;
  355. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  356. const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
  357. struct snd_sof_pcm *spcm;
  358. struct snd_soc_tplg_stream_caps *caps;
  359. int ret;
  360. /* nothing to do for BE */
  361. if (rtd->dai_link->no_pcm)
  362. return 0;
  363. spcm = snd_sof_find_spcm_dai(component, rtd);
  364. if (!spcm)
  365. return -EINVAL;
  366. dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
  367. spcm->pcm.pcm_id, substream->stream);
  368. caps = &spcm->pcm.caps[substream->stream];
  369. /* set runtime config */
  370. runtime->hw.info = ops->hw_info; /* platform-specific */
  371. /* set any runtime constraints based on topology */
  372. runtime->hw.formats = le64_to_cpu(caps->formats);
  373. runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
  374. runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
  375. runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
  376. runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
  377. /*
  378. * caps->buffer_size_min is not used since the
  379. * snd_pcm_hardware structure only defines buffer_bytes_max
  380. */
  381. runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
  382. dev_dbg(component->dev, "period min %zd max %zd bytes\n",
  383. runtime->hw.period_bytes_min,
  384. runtime->hw.period_bytes_max);
  385. dev_dbg(component->dev, "period count %d max %d\n",
  386. runtime->hw.periods_min,
  387. runtime->hw.periods_max);
  388. dev_dbg(component->dev, "buffer max %zd bytes\n",
  389. runtime->hw.buffer_bytes_max);
  390. /* set wait time - TODO: come from topology */
  391. substream->wait_time = 500;
  392. spcm->stream[substream->stream].posn.host_posn = 0;
  393. spcm->stream[substream->stream].posn.dai_posn = 0;
  394. spcm->stream[substream->stream].substream = substream;
  395. spcm->prepared[substream->stream] = false;
  396. ret = snd_sof_pcm_platform_open(sdev, substream);
  397. if (ret < 0)
  398. dev_err(component->dev, "error: pcm open failed %d\n", ret);
  399. return ret;
  400. }
  401. static int sof_pcm_close(struct snd_soc_component *component,
  402. struct snd_pcm_substream *substream)
  403. {
  404. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  405. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  406. struct snd_sof_pcm *spcm;
  407. int err;
  408. /* nothing to do for BE */
  409. if (rtd->dai_link->no_pcm)
  410. return 0;
  411. spcm = snd_sof_find_spcm_dai(component, rtd);
  412. if (!spcm)
  413. return -EINVAL;
  414. dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
  415. spcm->pcm.pcm_id, substream->stream);
  416. err = snd_sof_pcm_platform_close(sdev, substream);
  417. if (err < 0) {
  418. dev_err(component->dev, "error: pcm close failed %d\n",
  419. err);
  420. /*
  421. * keep going, no point in preventing the close
  422. * from happening
  423. */
  424. }
  425. spcm->stream[substream->stream].substream = NULL;
  426. return 0;
  427. }
  428. /*
  429. * Pre-allocate playback/capture audio buffer pages.
  430. * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
  431. * snd_pcm_lib_preallocate_free_for_all() is called by the core.
  432. */
  433. static int sof_pcm_new(struct snd_soc_component *component,
  434. struct snd_soc_pcm_runtime *rtd)
  435. {
  436. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  437. struct snd_sof_pcm *spcm;
  438. struct snd_pcm *pcm = rtd->pcm;
  439. struct snd_soc_tplg_stream_caps *caps;
  440. int stream = SNDRV_PCM_STREAM_PLAYBACK;
  441. /* find SOF PCM for this RTD */
  442. spcm = snd_sof_find_spcm_dai(component, rtd);
  443. if (!spcm) {
  444. dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
  445. rtd->dai_link->id);
  446. return 0;
  447. }
  448. dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
  449. /* do we need to pre-allocate playback audio buffer pages */
  450. if (!spcm->pcm.playback)
  451. goto capture;
  452. caps = &spcm->pcm.caps[stream];
  453. /* pre-allocate playback audio buffer pages */
  454. dev_dbg(component->dev,
  455. "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
  456. caps->name, caps->buffer_size_min, caps->buffer_size_max);
  457. if (!pcm->streams[stream].substream) {
  458. dev_err(component->dev, "error: NULL playback substream!\n");
  459. return -EINVAL;
  460. }
  461. snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
  462. SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
  463. 0, le32_to_cpu(caps->buffer_size_max));
  464. capture:
  465. stream = SNDRV_PCM_STREAM_CAPTURE;
  466. /* do we need to pre-allocate capture audio buffer pages */
  467. if (!spcm->pcm.capture)
  468. return 0;
  469. caps = &spcm->pcm.caps[stream];
  470. /* pre-allocate capture audio buffer pages */
  471. dev_dbg(component->dev,
  472. "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
  473. caps->name, caps->buffer_size_min, caps->buffer_size_max);
  474. if (!pcm->streams[stream].substream) {
  475. dev_err(component->dev, "error: NULL capture substream!\n");
  476. return -EINVAL;
  477. }
  478. snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
  479. SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
  480. 0, le32_to_cpu(caps->buffer_size_max));
  481. return 0;
  482. }
  483. /* fixup the BE DAI link to match any values from topology */
  484. int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
  485. {
  486. struct snd_interval *rate = hw_param_interval(params,
  487. SNDRV_PCM_HW_PARAM_RATE);
  488. struct snd_interval *channels = hw_param_interval(params,
  489. SNDRV_PCM_HW_PARAM_CHANNELS);
  490. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  491. struct snd_soc_component *component =
  492. snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  493. struct snd_sof_dai *dai =
  494. snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  495. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  496. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  497. /* no topology exists for this BE, try a common configuration */
  498. if (!dai) {
  499. dev_warn(component->dev,
  500. "warning: no topology found for BE DAI %s config\n",
  501. rtd->dai_link->name);
  502. /* set 48k, stereo, 16bits by default */
  503. rate->min = 48000;
  504. rate->max = 48000;
  505. channels->min = 2;
  506. channels->max = 2;
  507. snd_mask_none(fmt);
  508. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  509. return 0;
  510. }
  511. if (pcm_ops && pcm_ops->dai_link_fixup)
  512. return pcm_ops->dai_link_fixup(rtd, params);
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
  516. static int sof_pcm_probe(struct snd_soc_component *component)
  517. {
  518. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  519. struct snd_sof_pdata *plat_data = sdev->pdata;
  520. const char *tplg_filename;
  521. int ret;
  522. /*
  523. * make sure the device is pm_runtime_active before loading the
  524. * topology and initiating IPC or bus transactions
  525. */
  526. ret = pm_runtime_resume_and_get(component->dev);
  527. if (ret < 0 && ret != -EACCES)
  528. return ret;
  529. /* load the default topology */
  530. sdev->component = component;
  531. tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
  532. "%s/%s",
  533. plat_data->tplg_filename_prefix,
  534. plat_data->tplg_filename);
  535. if (!tplg_filename) {
  536. ret = -ENOMEM;
  537. goto pm_error;
  538. }
  539. ret = snd_sof_load_topology(component, tplg_filename);
  540. if (ret < 0)
  541. dev_err(component->dev, "error: failed to load DSP topology %d\n",
  542. ret);
  543. pm_error:
  544. pm_runtime_mark_last_busy(component->dev);
  545. pm_runtime_put_autosuspend(component->dev);
  546. return ret;
  547. }
  548. static void sof_pcm_remove(struct snd_soc_component *component)
  549. {
  550. /* remove topology */
  551. snd_soc_tplg_component_remove(component);
  552. }
  553. static int sof_pcm_ack(struct snd_soc_component *component,
  554. struct snd_pcm_substream *substream)
  555. {
  556. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  557. return snd_sof_pcm_platform_ack(sdev, substream);
  558. }
  559. static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
  560. struct snd_pcm_substream *substream)
  561. {
  562. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  563. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  564. if (pcm_ops && pcm_ops->delay)
  565. return pcm_ops->delay(component, substream);
  566. return 0;
  567. }
  568. void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
  569. {
  570. struct snd_soc_component_driver *pd = &sdev->plat_drv;
  571. struct snd_sof_pdata *plat_data = sdev->pdata;
  572. const char *drv_name;
  573. if (plat_data->machine)
  574. drv_name = plat_data->machine->drv_name;
  575. else if (plat_data->of_machine)
  576. drv_name = plat_data->of_machine->drv_name;
  577. else
  578. drv_name = NULL;
  579. pd->name = "sof-audio-component";
  580. pd->probe = sof_pcm_probe;
  581. pd->remove = sof_pcm_remove;
  582. pd->open = sof_pcm_open;
  583. pd->close = sof_pcm_close;
  584. pd->hw_params = sof_pcm_hw_params;
  585. pd->prepare = sof_pcm_prepare;
  586. pd->hw_free = sof_pcm_hw_free;
  587. pd->trigger = sof_pcm_trigger;
  588. pd->pointer = sof_pcm_pointer;
  589. pd->ack = sof_pcm_ack;
  590. pd->delay = sof_pcm_delay;
  591. #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
  592. pd->compress_ops = &sof_compressed_ops;
  593. #endif
  594. pd->pcm_construct = sof_pcm_new;
  595. pd->ignore_machine = drv_name;
  596. pd->be_pcm_base = SOF_BE_PCM_BASE;
  597. pd->use_dai_pcm_id = true;
  598. pd->topology_name_prefix = "sof";
  599. /* increment module refcount when a pcm is opened */
  600. pd->module_get_upon_open = 1;
  601. pd->legacy_dai_naming = 1;
  602. /*
  603. * The fixup is only needed when the DSP is in use as with the DSPless
  604. * mode we are directly using the audio interface
  605. */
  606. if (!sdev->dspless_mode_selected)
  607. pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
  608. }