ASoC: SOF: Add support for DSPless mode
[linux-block.git] / sound / soc / sof / pcm.c
CommitLineData
e149ca29 1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
868bd00f
LG
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 <liam.r.girdwood@linux.intel.com>
9//
10// PCM Layer, interface between ALSA and IPC.
11//
12
13#include <linux/pm_runtime.h>
14#include <sound/pcm_params.h>
15#include <sound/sof.h>
bcd2cc35 16#include <trace/events/sof.h>
6ace85b9 17#include "sof-of-dev.h"
868bd00f 18#include "sof-priv.h"
ee1e79b7 19#include "sof-audio.h"
ee844305 20#include "sof-utils.h"
3dc0d709 21#include "ops.h"
868bd00f 22
868bd00f 23/* Create DMA buffer page table for DSP */
1c91d77e
KM
24static int create_page_table(struct snd_soc_component *component,
25 struct snd_pcm_substream *substream,
868bd00f
LG
26 unsigned char *dma_area, size_t size)
27{
1205300a 28 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f
LG
29 struct snd_sof_pcm *spcm;
30 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
31 int stream = substream->stream;
32
ee1e79b7 33 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
34 if (!spcm)
35 return -EINVAL;
36
ee1e79b7 37 return snd_sof_create_page_table(component->dev, dmab,
868bd00f
LG
38 spcm->stream[stream].page_table.area, size);
39}
40
e2803e61
KJ
41/*
42 * sof pcm period elapse work
43 */
858f7a5c 44static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
e2803e61
KJ
45{
46 struct snd_sof_pcm_stream *sps =
47 container_of(work, struct snd_sof_pcm_stream,
48 period_elapsed_work);
49
50 snd_pcm_period_elapsed(sps->substream);
51}
52
858f7a5c
DB
53void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
54{
55 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
56}
57
e2803e61
KJ
58/*
59 * sof pcm period elapse, this could be called at irq thread context.
60 */
61void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
62{
1205300a 63 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
e2803e61 64 struct snd_soc_component *component =
ee1e79b7 65 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
e2803e61
KJ
66 struct snd_sof_pcm *spcm;
67
ee1e79b7 68 spcm = snd_sof_find_spcm_dai(component, rtd);
e2803e61 69 if (!spcm) {
ee1e79b7 70 dev_err(component->dev,
e2803e61
KJ
71 "error: period elapsed for unknown stream!\n");
72 return;
73 }
74
75 /*
76 * snd_pcm_period_elapsed() can be called in interrupt context
77 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
78 * when the PCM is done draining or xrun happened, a STOP IPC will
79 * then be sent and this IPC will hit IPC timeout.
80 * To avoid sending IPC before the previous IPC is handled, we
81 * schedule delayed work here to call the snd_pcm_period_elapsed().
82 */
83 schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
84}
85EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
86
f18ad9ca
RS
87static int
88sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
66344c6d
RS
89 struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params,
90 struct snd_sof_platform_stream_params *platform_params, int dir)
5fcdbb2d
RS
91{
92 struct snd_soc_dai *dai;
93 int ret, j;
94
95 /* query DAPM for list of connected widgets and set them up */
96 for_each_rtd_cpu_dais(rtd, j, dai) {
97 struct snd_soc_dapm_widget_list *list;
98
99 ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
100 dpcm_end_walk_at_be);
101 if (ret < 0) {
102 dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
103 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
104 return ret;
105 }
106
107 spcm->stream[dir].list = list;
108
66344c6d 109 ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir);
5fcdbb2d
RS
110 if (ret < 0) {
111 dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n",
112 spcm->pcm.pcm_id, dir);
113 spcm->stream[dir].list = NULL;
114 snd_soc_dapm_dai_free_widgets(&list);
115 return ret;
116 }
117 }
118
119 return 0;
120}
121
1c91d77e
KM
122static int sof_pcm_hw_params(struct snd_soc_component *component,
123 struct snd_pcm_substream *substream,
868bd00f
LG
124 struct snd_pcm_hw_params *params)
125{
868bd00f 126 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
621fd48c 127 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148dd6a2 128 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
31f60a0c 129 struct snd_sof_platform_stream_params platform_params = { 0 };
621fd48c 130 struct snd_pcm_runtime *runtime = substream->runtime;
868bd00f 131 struct snd_sof_pcm *spcm;
868bd00f
LG
132 int ret;
133
134 /* nothing to do for BE */
135 if (rtd->dai_link->no_pcm)
136 return 0;
137
ee1e79b7 138 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
139 if (!spcm)
140 return -EINVAL;
141
cfe8191b
KV
142 /*
143 * Handle repeated calls to hw_params() without free_pcm() in
144 * between. At least ALSA OSS emulation depends on this.
145 */
148dd6a2 146 if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) {
4123c24b
RS
147 ret = pcm_ops->hw_free(component, substream);
148 if (ret < 0)
149 return ret;
150
151 spcm->prepared[substream->stream] = false;
152 }
cfe8191b 153
ee1e79b7 154 dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
868bd00f
LG
155 spcm->pcm.pcm_id, substream->stream);
156
f0d31dbb
RS
157 ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params);
158 if (ret < 0) {
159 dev_err(component->dev, "platform hw params failed\n");
160 return ret;
161 }
162
621fd48c
RS
163 /* if this is a repeated hw_params without hw_free, skip setting up widgets */
164 if (!spcm->stream[substream->stream].list) {
66344c6d
RS
165 ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params,
166 substream->stream);
621fd48c
RS
167 if (ret < 0)
168 return ret;
169 }
868bd00f 170
57e960f0
TI
171 /* create compressed page table for audio firmware */
172 if (runtime->buffer_changed) {
1c91d77e 173 ret = create_page_table(component, substream, runtime->dma_area,
868bd00f 174 runtime->dma_bytes);
621fd48c 175
868bd00f
LG
176 if (ret < 0)
177 return ret;
178 }
179
148dd6a2 180 if (pcm_ops && pcm_ops->hw_params) {
621fd48c 181 ret = pcm_ops->hw_params(component, substream, params, &platform_params);
5fcdbb2d
RS
182 if (ret < 0)
183 return ret;
184 }
185
04c80277
KV
186 spcm->prepared[substream->stream] = true;
187
868bd00f
LG
188 /* save pcm hw_params */
189 memcpy(&spcm->params[substream->stream], params, sizeof(*params));
190
621fd48c 191 return 0;
868bd00f
LG
192}
193
1c91d77e
KM
194static int sof_pcm_hw_free(struct snd_soc_component *component,
195 struct snd_pcm_substream *substream)
868bd00f 196{
1205300a 197 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f 198 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
148dd6a2 199 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
868bd00f 200 struct snd_sof_pcm *spcm;
e66e52c5 201 int ret, err = 0;
868bd00f
LG
202
203 /* nothing to do for BE */
204 if (rtd->dai_link->no_pcm)
205 return 0;
206
ee1e79b7 207 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
208 if (!spcm)
209 return -EINVAL;
210
ee1e79b7
RS
211 dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
212 spcm->pcm.pcm_id, substream->stream);
868bd00f 213
0b639dcd 214 /* free PCM in the DSP */
148dd6a2 215 if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) {
4123c24b
RS
216 ret = pcm_ops->hw_free(component, substream);
217 if (ret < 0)
218 err = ret;
868bd00f 219
4123c24b
RS
220 spcm->prepared[substream->stream] = false;
221 }
e2803e61 222
0b639dcd 223 /* stop DMA */
93146bc2 224 ret = snd_sof_pcm_platform_hw_free(sdev, substream);
e66e52c5 225 if (ret < 0) {
ee1e79b7 226 dev_err(component->dev, "error: platform hw free failed\n");
e66e52c5
KV
227 err = ret;
228 }
93146bc2 229
0b639dcd
RS
230 /* free the DAPM widget list */
231 ret = sof_widget_list_free(sdev, spcm, substream->stream);
232 if (ret < 0)
233 err = ret;
234
235 cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
236
e66e52c5 237 return err;
868bd00f
LG
238}
239
1c91d77e
KM
240static int sof_pcm_prepare(struct snd_soc_component *component,
241 struct snd_pcm_substream *substream)
868bd00f 242{
1205300a 243 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f
LG
244 struct snd_sof_pcm *spcm;
245 int ret;
246
247 /* nothing to do for BE */
248 if (rtd->dai_link->no_pcm)
249 return 0;
250
ee1e79b7 251 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
252 if (!spcm)
253 return -EINVAL;
254
04c80277 255 if (spcm->prepared[substream->stream])
868bd00f
LG
256 return 0;
257
ee1e79b7
RS
258 dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
259 spcm->pcm.pcm_id, substream->stream);
868bd00f
LG
260
261 /* set hw_params */
1c91d77e
KM
262 ret = sof_pcm_hw_params(component,
263 substream, &spcm->params[substream->stream]);
868bd00f 264 if (ret < 0) {
ee1e79b7
RS
265 dev_err(component->dev,
266 "error: set pcm hw_params after resume\n");
868bd00f
LG
267 return ret;
268 }
269
270 return 0;
271}
272
273/*
274 * FE dai link trigger actions are always executed in non-atomic context because
275 * they involve IPC's.
276 */
1c91d77e
KM
277static int sof_pcm_trigger(struct snd_soc_component *component,
278 struct snd_pcm_substream *substream, int cmd)
868bd00f 279{
1205300a 280 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f 281 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
148dd6a2 282 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
868bd00f 283 struct snd_sof_pcm *spcm;
04c80277 284 bool reset_hw_params = false;
0a1b0834 285 bool ipc_first = false;
beac3f4c 286 int ret = 0;
868bd00f
LG
287
288 /* nothing to do for BE */
289 if (rtd->dai_link->no_pcm)
290 return 0;
291
ee1e79b7 292 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
293 if (!spcm)
294 return -EINVAL;
295
ee1e79b7 296 dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
868bd00f
LG
297 spcm->pcm.pcm_id, substream->stream, cmd);
298
868bd00f
LG
299 switch (cmd) {
300 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0a1b0834 301 ipc_first = true;
868bd00f
LG
302 break;
303 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
51ce3e6e
RS
304 if (pcm_ops && pcm_ops->ipc_first_on_start)
305 ipc_first = true;
868bd00f 306 break;
868bd00f 307 case SNDRV_PCM_TRIGGER_START:
ac8c046f
KJ
308 if (spcm->stream[substream->stream].suspend_ignored) {
309 /*
310 * This case will be triggered when INFO_RESUME is
311 * not supported, no need to re-start streams that
312 * remained enabled in D0ix.
313 */
314 spcm->stream[substream->stream].suspend_ignored = false;
315 return 0;
316 }
51ce3e6e
RS
317
318 if (pcm_ops && pcm_ops->ipc_first_on_start)
319 ipc_first = true;
868bd00f
LG
320 break;
321 case SNDRV_PCM_TRIGGER_SUSPEND:
043ae13b 322 if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
ac8c046f
KJ
323 spcm->stream[substream->stream].d0i3_compatible) {
324 /*
325 * trap the event, not sending trigger stop to
326 * prevent the FW pipelines from being stopped,
327 * and mark the flag to ignore the upcoming DAPM
328 * PM events.
329 */
330 spcm->stream[substream->stream].suspend_ignored = true;
331 return 0;
332 }
28d40e7a
PU
333
334 /* On suspend the DMA must be stopped in DSPless mode */
335 if (sdev->dspless_mode_selected)
336 reset_hw_params = true;
337
df561f66 338 fallthrough;
868bd00f 339 case SNDRV_PCM_TRIGGER_STOP:
0a1b0834 340 ipc_first = true;
7d6f623c
RS
341 if (pcm_ops && pcm_ops->reset_hw_params_during_stop)
342 reset_hw_params = true;
868bd00f
LG
343 break;
344 default:
beac3f4c 345 dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
868bd00f
LG
346 return -EINVAL;
347 }
348
0a1b0834
PX
349 if (!ipc_first)
350 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
868bd00f 351
148dd6a2 352 if (pcm_ops && pcm_ops->trigger)
beac3f4c 353 ret = pcm_ops->trigger(component, substream, cmd);
868bd00f 354
51ce3e6e
RS
355 switch (cmd) {
356 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
357 case SNDRV_PCM_TRIGGER_START:
358 /* invoke platform trigger to start DMA only if pcm_ops is successful */
359 if (ipc_first && !ret)
360 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
361 break;
362 case SNDRV_PCM_TRIGGER_SUSPEND:
363 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
364 case SNDRV_PCM_TRIGGER_STOP:
365 /* invoke platform trigger to stop DMA even if pcm_ops failed */
0a1b0834 366 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
51ce3e6e
RS
367 break;
368 default:
369 break;
370 }
0a1b0834
PX
371
372 /* free PCM if reset_hw_params is set and the STOP IPC is successful */
9e116f5a 373 if (!ret && reset_hw_params)
82b18242 374 ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false);
868bd00f 375
a49b6871 376 return ret;
868bd00f
LG
377}
378
1c91d77e
KM
379static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
380 struct snd_pcm_substream *substream)
868bd00f 381{
1205300a 382 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f
LG
383 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
384 struct snd_sof_pcm *spcm;
385 snd_pcm_uframes_t host, dai;
386
387 /* nothing to do for BE */
388 if (rtd->dai_link->no_pcm)
389 return 0;
390
391 /* use dsp ops pointer callback directly if set */
392 if (sof_ops(sdev)->pcm_pointer)
393 return sof_ops(sdev)->pcm_pointer(sdev, substream);
394
ee1e79b7 395 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
396 if (!spcm)
397 return -EINVAL;
398
399 /* read position from DSP */
400 host = bytes_to_frames(substream->runtime,
401 spcm->stream[substream->stream].posn.host_posn);
402 dai = bytes_to_frames(substream->runtime,
403 spcm->stream[substream->stream].posn.dai_posn);
404
bcd2cc35 405 trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
868bd00f
LG
406
407 return host;
408}
409
1c91d77e
KM
410static int sof_pcm_open(struct snd_soc_component *component,
411 struct snd_pcm_substream *substream)
868bd00f 412{
1205300a 413 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f 414 struct snd_pcm_runtime *runtime = substream->runtime;
868bd00f 415 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
856601e5 416 struct snd_sof_dsp_ops *ops = sof_ops(sdev);
868bd00f
LG
417 struct snd_sof_pcm *spcm;
418 struct snd_soc_tplg_stream_caps *caps;
419 int ret;
868bd00f
LG
420
421 /* nothing to do for BE */
422 if (rtd->dai_link->no_pcm)
423 return 0;
424
ee1e79b7 425 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
426 if (!spcm)
427 return -EINVAL;
428
ee1e79b7
RS
429 dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
430 spcm->pcm.pcm_id, substream->stream);
868bd00f 431
868bd00f
LG
432
433 caps = &spcm->pcm.caps[substream->stream];
434
868bd00f 435 /* set runtime config */
27e322fa
PLB
436 runtime->hw.info = ops->hw_info; /* platform-specific */
437
9983ac49 438 /* set any runtime constraints based on topology */
868bd00f
LG
439 runtime->hw.formats = le64_to_cpu(caps->formats);
440 runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
441 runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
442 runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
443 runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
444
445 /*
446 * caps->buffer_size_min is not used since the
447 * snd_pcm_hardware structure only defines buffer_bytes_max
448 */
449 runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
450
ee1e79b7 451 dev_dbg(component->dev, "period min %zd max %zd bytes\n",
868bd00f
LG
452 runtime->hw.period_bytes_min,
453 runtime->hw.period_bytes_max);
ee1e79b7 454 dev_dbg(component->dev, "period count %d max %d\n",
868bd00f
LG
455 runtime->hw.periods_min,
456 runtime->hw.periods_max);
ee1e79b7 457 dev_dbg(component->dev, "buffer max %zd bytes\n",
868bd00f
LG
458 runtime->hw.buffer_bytes_max);
459
460 /* set wait time - TODO: come from topology */
461 substream->wait_time = 500;
462
463 spcm->stream[substream->stream].posn.host_posn = 0;
464 spcm->stream[substream->stream].posn.dai_posn = 0;
465 spcm->stream[substream->stream].substream = substream;
04c80277 466 spcm->prepared[substream->stream] = false;
868bd00f
LG
467
468 ret = snd_sof_pcm_platform_open(sdev, substream);
14a2212d 469 if (ret < 0)
ee1e79b7 470 dev_err(component->dev, "error: pcm open failed %d\n", ret);
868bd00f
LG
471
472 return ret;
473}
474
1c91d77e
KM
475static int sof_pcm_close(struct snd_soc_component *component,
476 struct snd_pcm_substream *substream)
868bd00f 477{
1205300a 478 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
868bd00f
LG
479 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
480 struct snd_sof_pcm *spcm;
481 int err;
482
483 /* nothing to do for BE */
484 if (rtd->dai_link->no_pcm)
485 return 0;
486
ee1e79b7 487 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f
LG
488 if (!spcm)
489 return -EINVAL;
490
ee1e79b7
RS
491 dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
492 spcm->pcm.pcm_id, substream->stream);
868bd00f
LG
493
494 err = snd_sof_pcm_platform_close(sdev, substream);
495 if (err < 0) {
ee1e79b7 496 dev_err(component->dev, "error: pcm close failed %d\n",
868bd00f
LG
497 err);
498 /*
499 * keep going, no point in preventing the close
500 * from happening
501 */
502 }
503
868bd00f
LG
504 return 0;
505}
506
868bd00f
LG
507/*
508 * Pre-allocate playback/capture audio buffer pages.
509 * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
510 * snd_pcm_lib_preallocate_free_for_all() is called by the core.
511 */
1c91d77e
KM
512static int sof_pcm_new(struct snd_soc_component *component,
513 struct snd_soc_pcm_runtime *rtd)
868bd00f 514{
868bd00f
LG
515 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
516 struct snd_sof_pcm *spcm;
517 struct snd_pcm *pcm = rtd->pcm;
518 struct snd_soc_tplg_stream_caps *caps;
519 int stream = SNDRV_PCM_STREAM_PLAYBACK;
520
521 /* find SOF PCM for this RTD */
ee1e79b7 522 spcm = snd_sof_find_spcm_dai(component, rtd);
868bd00f 523 if (!spcm) {
ee1e79b7 524 dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
868bd00f
LG
525 rtd->dai_link->id);
526 return 0;
527 }
528
ee1e79b7 529 dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
868bd00f
LG
530
531 /* do we need to pre-allocate playback audio buffer pages */
532 if (!spcm->pcm.playback)
533 goto capture;
534
535 caps = &spcm->pcm.caps[stream];
536
537 /* pre-allocate playback audio buffer pages */
ee1e79b7
RS
538 dev_dbg(component->dev,
539 "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
868bd00f
LG
540 caps->name, caps->buffer_size_min, caps->buffer_size_max);
541
4f7f9564
GL
542 if (!pcm->streams[stream].substream) {
543 dev_err(component->dev, "error: NULL playback substream!\n");
544 return -EINVAL;
545 }
546
57e960f0
TI
547 snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
548 SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
e582f483 549 0, le32_to_cpu(caps->buffer_size_max));
868bd00f
LG
550capture:
551 stream = SNDRV_PCM_STREAM_CAPTURE;
552
553 /* do we need to pre-allocate capture audio buffer pages */
554 if (!spcm->pcm.capture)
555 return 0;
556
557 caps = &spcm->pcm.caps[stream];
558
559 /* pre-allocate capture audio buffer pages */
ee1e79b7
RS
560 dev_dbg(component->dev,
561 "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
868bd00f
LG
562 caps->name, caps->buffer_size_min, caps->buffer_size_max);
563
4f7f9564
GL
564 if (!pcm->streams[stream].substream) {
565 dev_err(component->dev, "error: NULL capture substream!\n");
566 return -EINVAL;
567 }
568
57e960f0
TI
569 snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
570 SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
e582f483 571 0, le32_to_cpu(caps->buffer_size_max));
868bd00f
LG
572
573 return 0;
574}
575
576/* fixup the BE DAI link to match any values from topology */
f805e7e0 577int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
868bd00f
LG
578{
579 struct snd_interval *rate = hw_param_interval(params,
580 SNDRV_PCM_HW_PARAM_RATE);
581 struct snd_interval *channels = hw_param_interval(params,
582 SNDRV_PCM_HW_PARAM_CHANNELS);
583 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
584 struct snd_soc_component *component =
ee1e79b7 585 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
868bd00f 586 struct snd_sof_dai *dai =
ee1e79b7 587 snd_sof_find_dai(component, (char *)rtd->dai_link->name);
c943a586 588 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
148dd6a2 589 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
868bd00f
LG
590
591 /* no topology exists for this BE, try a common configuration */
592 if (!dai) {
ee1e79b7
RS
593 dev_warn(component->dev,
594 "warning: no topology found for BE DAI %s config\n",
868bd00f
LG
595 rtd->dai_link->name);
596
597 /* set 48k, stereo, 16bits by default */
598 rate->min = 48000;
599 rate->max = 48000;
600
601 channels->min = 2;
602 channels->max = 2;
603
604 snd_mask_none(fmt);
605 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
606
607 return 0;
608 }
609
148dd6a2 610 if (pcm_ops && pcm_ops->dai_link_fixup)
b243b437 611 return pcm_ops->dai_link_fixup(rtd, params);
868bd00f
LG
612
613 return 0;
614}
f3f3af17 615EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
868bd00f
LG
616
617static int sof_pcm_probe(struct snd_soc_component *component)
618{
619 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
620 struct snd_sof_pdata *plat_data = sdev->pdata;
621 const char *tplg_filename;
622 int ret;
623
4ea3bfd1
PLB
624 /*
625 * make sure the device is pm_runtime_active before loading the
626 * topology and initiating IPC or bus transactions
627 */
628 ret = pm_runtime_resume_and_get(component->dev);
629 if (ret < 0 && ret != -EACCES)
630 return ret;
631
868bd00f
LG
632 /* load the default topology */
633 sdev->component = component;
634
635 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
636 "%s/%s",
637 plat_data->tplg_filename_prefix,
638 plat_data->tplg_filename);
639 if (!tplg_filename)
640 return -ENOMEM;
641
ee1e79b7 642 ret = snd_sof_load_topology(component, tplg_filename);
868bd00f 643 if (ret < 0) {
ee1e79b7 644 dev_err(component->dev, "error: failed to load DSP topology %d\n",
868bd00f
LG
645 ret);
646 return ret;
647 }
648
4ea3bfd1
PLB
649 pm_runtime_mark_last_busy(component->dev);
650 pm_runtime_put_autosuspend(component->dev);
651
868bd00f
LG
652 return ret;
653}
654
655static void sof_pcm_remove(struct snd_soc_component *component)
656{
657 /* remove topology */
a5b8f71c 658 snd_soc_tplg_component_remove(component);
868bd00f
LG
659}
660
4a39ea3f
RS
661static int sof_pcm_ack(struct snd_soc_component *component,
662 struct snd_pcm_substream *substream)
663{
664 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
665
666 return snd_sof_pcm_platform_ack(sdev, substream);
667}
668
27c2100b
RW
669static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
670 struct snd_pcm_substream *substream)
671{
672 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
673 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
674
675 if (pcm_ops && pcm_ops->delay)
676 return pcm_ops->delay(component, substream);
677
678 return 0;
679}
680
868bd00f
LG
681void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
682{
683 struct snd_soc_component_driver *pd = &sdev->plat_drv;
684 struct snd_sof_pdata *plat_data = sdev->pdata;
685 const char *drv_name;
686
6ace85b9
CL
687 if (plat_data->machine)
688 drv_name = plat_data->machine->drv_name;
689 else if (plat_data->of_machine)
690 drv_name = plat_data->of_machine->drv_name;
691 else
692 drv_name = NULL;
868bd00f
LG
693
694 pd->name = "sof-audio-component";
695 pd->probe = sof_pcm_probe;
696 pd->remove = sof_pcm_remove;
1c91d77e
KM
697 pd->open = sof_pcm_open;
698 pd->close = sof_pcm_close;
1c91d77e
KM
699 pd->hw_params = sof_pcm_hw_params;
700 pd->prepare = sof_pcm_prepare;
701 pd->hw_free = sof_pcm_hw_free;
702 pd->trigger = sof_pcm_trigger;
703 pd->pointer = sof_pcm_pointer;
4a39ea3f 704 pd->ack = sof_pcm_ack;
27c2100b 705 pd->delay = sof_pcm_delay;
1c91d77e 706
76cdd90b
DB
707#if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
708 pd->compress_ops = &sof_compressed_ops;
709#endif
710
1c91d77e 711 pd->pcm_construct = sof_pcm_new;
868bd00f 712 pd->ignore_machine = drv_name;
868bd00f
LG
713 pd->be_pcm_base = SOF_BE_PCM_BASE;
714 pd->use_dai_pcm_id = true;
715 pd->topology_name_prefix = "sof";
716
717 /* increment module refcount when a pcm is opened */
718 pd->module_get_upon_open = 1;
a718ba30
CK
719
720 pd->legacy_dai_naming = 1;
28d40e7a
PU
721
722 /*
723 * The fixup is only needed when the DSP is in use as with the DSPless
724 * mode we are directly using the audio interface
725 */
726 if (!sdev->dspless_mode_selected)
727 pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
868bd00f 728}