1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
10 // PCM Layer, interface between ALSA and IPC.
13 #include <linux/pm_runtime.h>
14 #include <sound/pcm_params.h>
15 #include <sound/sof.h>
16 #include <trace/events/sof.h>
17 #include "sof-of-dev.h"
19 #include "sof-audio.h"
20 #include "sof-utils.h"
23 /* Create DMA buffer page table for DSP */
24 static int create_page_table(struct snd_soc_component *component,
25 struct snd_pcm_substream *substream,
26 unsigned char *dma_area, size_t size)
28 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
29 struct snd_sof_pcm *spcm;
30 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
31 int stream = substream->stream;
33 spcm = snd_sof_find_spcm_dai(component, rtd);
37 return snd_sof_create_page_table(component->dev, dmab,
38 spcm->stream[stream].page_table.area, size);
42 * sof pcm period elapse work
44 static void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
46 struct snd_sof_pcm_stream *sps =
47 container_of(work, struct snd_sof_pcm_stream,
50 snd_pcm_period_elapsed(sps->substream);
53 void snd_sof_pcm_init_elapsed_work(struct work_struct *work)
55 INIT_WORK(work, snd_sof_pcm_period_elapsed_work);
59 * sof pcm period elapse, this could be called at irq thread context.
61 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
63 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
64 struct snd_soc_component *component =
65 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
66 struct snd_sof_pcm *spcm;
68 spcm = snd_sof_find_spcm_dai(component, rtd);
70 dev_err(component->dev,
71 "error: period elapsed for unknown stream!\n");
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().
83 schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
85 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
88 sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd,
89 struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params,
90 struct snd_sof_platform_stream_params *platform_params, int dir)
92 struct snd_soc_dai *dai;
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;
99 ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list,
100 dpcm_end_walk_at_be);
102 dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name,
103 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture");
107 spcm->stream[dir].list = list;
109 ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir);
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);
122 static int sof_pcm_hw_params(struct snd_soc_component *component,
123 struct snd_pcm_substream *substream,
124 struct snd_pcm_hw_params *params)
126 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
127 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
128 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
129 struct snd_sof_platform_stream_params platform_params = { 0 };
130 struct snd_pcm_runtime *runtime = substream->runtime;
131 struct snd_sof_pcm *spcm;
134 /* nothing to do for BE */
135 if (rtd->dai_link->no_pcm)
138 spcm = snd_sof_find_spcm_dai(component, rtd);
143 * Handle repeated calls to hw_params() without free_pcm() in
144 * between. At least ALSA OSS emulation depends on this.
146 if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) {
147 ret = pcm_ops->hw_free(component, substream);
151 spcm->prepared[substream->stream] = false;
154 dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
155 spcm->pcm.pcm_id, substream->stream);
157 ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params);
159 dev_err(component->dev, "platform hw params failed\n");
163 /* if this is a repeated hw_params without hw_free, skip setting up widgets */
164 if (!spcm->stream[substream->stream].list) {
165 ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params,
171 /* create compressed page table for audio firmware */
172 if (runtime->buffer_changed) {
173 ret = create_page_table(component, substream, runtime->dma_area,
180 if (pcm_ops && pcm_ops->hw_params) {
181 ret = pcm_ops->hw_params(component, substream, params, &platform_params);
186 spcm->prepared[substream->stream] = true;
188 /* save pcm hw_params */
189 memcpy(&spcm->params[substream->stream], params, sizeof(*params));
194 static int sof_pcm_hw_free(struct snd_soc_component *component,
195 struct snd_pcm_substream *substream)
197 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
198 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
199 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
200 struct snd_sof_pcm *spcm;
203 /* nothing to do for BE */
204 if (rtd->dai_link->no_pcm)
207 spcm = snd_sof_find_spcm_dai(component, rtd);
211 dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
212 spcm->pcm.pcm_id, substream->stream);
214 if (spcm->prepared[substream->stream]) {
215 /* stop DMA first if needed */
216 if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
217 snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP);
219 /* free PCM in the DSP */
220 if (pcm_ops && pcm_ops->hw_free) {
221 ret = pcm_ops->hw_free(component, substream);
226 spcm->prepared[substream->stream] = false;
230 ret = snd_sof_pcm_platform_hw_free(sdev, substream);
232 dev_err(component->dev, "error: platform hw free failed\n");
236 /* free the DAPM widget list */
237 ret = sof_widget_list_free(sdev, spcm, substream->stream);
241 cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
246 static int sof_pcm_prepare(struct snd_soc_component *component,
247 struct snd_pcm_substream *substream)
249 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
250 struct snd_sof_pcm *spcm;
253 /* nothing to do for BE */
254 if (rtd->dai_link->no_pcm)
257 spcm = snd_sof_find_spcm_dai(component, rtd);
261 if (spcm->prepared[substream->stream])
264 dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
265 spcm->pcm.pcm_id, substream->stream);
268 ret = sof_pcm_hw_params(component,
269 substream, &spcm->params[substream->stream]);
271 dev_err(component->dev,
272 "error: set pcm hw_params after resume\n");
280 * FE dai link trigger actions are always executed in non-atomic context because
281 * they involve IPC's.
283 static int sof_pcm_trigger(struct snd_soc_component *component,
284 struct snd_pcm_substream *substream, int cmd)
286 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
287 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
288 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
289 struct snd_sof_pcm *spcm;
290 bool reset_hw_params = false;
291 bool ipc_first = false;
294 /* nothing to do for BE */
295 if (rtd->dai_link->no_pcm)
298 spcm = snd_sof_find_spcm_dai(component, rtd);
302 dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
303 spcm->pcm.pcm_id, substream->stream, cmd);
306 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
309 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
310 if (pcm_ops && pcm_ops->ipc_first_on_start)
313 case SNDRV_PCM_TRIGGER_START:
314 if (spcm->stream[substream->stream].suspend_ignored) {
316 * This case will be triggered when INFO_RESUME is
317 * not supported, no need to re-start streams that
318 * remained enabled in D0ix.
320 spcm->stream[substream->stream].suspend_ignored = false;
324 if (pcm_ops && pcm_ops->ipc_first_on_start)
327 case SNDRV_PCM_TRIGGER_SUSPEND:
328 if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
329 spcm->stream[substream->stream].d0i3_compatible) {
331 * trap the event, not sending trigger stop to
332 * prevent the FW pipelines from being stopped,
333 * and mark the flag to ignore the upcoming DAPM
336 spcm->stream[substream->stream].suspend_ignored = true;
340 /* On suspend the DMA must be stopped in DSPless mode */
341 if (sdev->dspless_mode_selected)
342 reset_hw_params = true;
345 case SNDRV_PCM_TRIGGER_STOP:
347 if (pcm_ops && pcm_ops->reset_hw_params_during_stop)
348 reset_hw_params = true;
351 dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
356 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
358 if (pcm_ops && pcm_ops->trigger)
359 ret = pcm_ops->trigger(component, substream, cmd);
362 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
363 case SNDRV_PCM_TRIGGER_START:
364 /* invoke platform trigger to start DMA only if pcm_ops is successful */
365 if (ipc_first && !ret)
366 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
368 case SNDRV_PCM_TRIGGER_SUSPEND:
369 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
370 case SNDRV_PCM_TRIGGER_STOP:
371 /* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */
372 if (!pcm_ops || !pcm_ops->platform_stop_during_hw_free)
373 snd_sof_pcm_platform_trigger(sdev, substream, cmd);
379 /* free PCM if reset_hw_params is set and the STOP IPC is successful */
380 if (!ret && reset_hw_params)
381 ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false);
386 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
387 struct snd_pcm_substream *substream)
389 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
390 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
391 struct snd_sof_pcm *spcm;
392 snd_pcm_uframes_t host, dai;
394 /* nothing to do for BE */
395 if (rtd->dai_link->no_pcm)
398 /* use dsp ops pointer callback directly if set */
399 if (sof_ops(sdev)->pcm_pointer)
400 return sof_ops(sdev)->pcm_pointer(sdev, substream);
402 spcm = snd_sof_find_spcm_dai(component, rtd);
406 /* read position from DSP */
407 host = bytes_to_frames(substream->runtime,
408 spcm->stream[substream->stream].posn.host_posn);
409 dai = bytes_to_frames(substream->runtime,
410 spcm->stream[substream->stream].posn.dai_posn);
412 trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai);
417 static int sof_pcm_open(struct snd_soc_component *component,
418 struct snd_pcm_substream *substream)
420 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
423 struct snd_sof_dsp_ops *ops = sof_ops(sdev);
424 struct snd_sof_pcm *spcm;
425 struct snd_soc_tplg_stream_caps *caps;
428 /* nothing to do for BE */
429 if (rtd->dai_link->no_pcm)
432 spcm = snd_sof_find_spcm_dai(component, rtd);
436 dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
437 spcm->pcm.pcm_id, substream->stream);
440 caps = &spcm->pcm.caps[substream->stream];
442 /* set runtime config */
443 runtime->hw.info = ops->hw_info; /* platform-specific */
445 /* set any runtime constraints based on topology */
446 runtime->hw.formats = le64_to_cpu(caps->formats);
447 runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
448 runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
449 runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
450 runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
453 * caps->buffer_size_min is not used since the
454 * snd_pcm_hardware structure only defines buffer_bytes_max
456 runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
458 dev_dbg(component->dev, "period min %zd max %zd bytes\n",
459 runtime->hw.period_bytes_min,
460 runtime->hw.period_bytes_max);
461 dev_dbg(component->dev, "period count %d max %d\n",
462 runtime->hw.periods_min,
463 runtime->hw.periods_max);
464 dev_dbg(component->dev, "buffer max %zd bytes\n",
465 runtime->hw.buffer_bytes_max);
467 /* set wait time - TODO: come from topology */
468 substream->wait_time = 500;
470 spcm->stream[substream->stream].posn.host_posn = 0;
471 spcm->stream[substream->stream].posn.dai_posn = 0;
472 spcm->stream[substream->stream].substream = substream;
473 spcm->prepared[substream->stream] = false;
475 ret = snd_sof_pcm_platform_open(sdev, substream);
477 dev_err(component->dev, "error: pcm open failed %d\n", ret);
482 static int sof_pcm_close(struct snd_soc_component *component,
483 struct snd_pcm_substream *substream)
485 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
486 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
487 struct snd_sof_pcm *spcm;
490 /* nothing to do for BE */
491 if (rtd->dai_link->no_pcm)
494 spcm = snd_sof_find_spcm_dai(component, rtd);
498 dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
499 spcm->pcm.pcm_id, substream->stream);
501 err = snd_sof_pcm_platform_close(sdev, substream);
503 dev_err(component->dev, "error: pcm close failed %d\n",
506 * keep going, no point in preventing the close
515 * Pre-allocate playback/capture audio buffer pages.
516 * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
517 * snd_pcm_lib_preallocate_free_for_all() is called by the core.
519 static int sof_pcm_new(struct snd_soc_component *component,
520 struct snd_soc_pcm_runtime *rtd)
522 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
523 struct snd_sof_pcm *spcm;
524 struct snd_pcm *pcm = rtd->pcm;
525 struct snd_soc_tplg_stream_caps *caps;
526 int stream = SNDRV_PCM_STREAM_PLAYBACK;
528 /* find SOF PCM for this RTD */
529 spcm = snd_sof_find_spcm_dai(component, rtd);
531 dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
536 dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
538 /* do we need to pre-allocate playback audio buffer pages */
539 if (!spcm->pcm.playback)
542 caps = &spcm->pcm.caps[stream];
544 /* pre-allocate playback audio buffer pages */
545 dev_dbg(component->dev,
546 "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
547 caps->name, caps->buffer_size_min, caps->buffer_size_max);
549 if (!pcm->streams[stream].substream) {
550 dev_err(component->dev, "error: NULL playback substream!\n");
554 snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
555 SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
556 0, le32_to_cpu(caps->buffer_size_max));
558 stream = SNDRV_PCM_STREAM_CAPTURE;
560 /* do we need to pre-allocate capture audio buffer pages */
561 if (!spcm->pcm.capture)
564 caps = &spcm->pcm.caps[stream];
566 /* pre-allocate capture audio buffer pages */
567 dev_dbg(component->dev,
568 "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
569 caps->name, caps->buffer_size_min, caps->buffer_size_max);
571 if (!pcm->streams[stream].substream) {
572 dev_err(component->dev, "error: NULL capture substream!\n");
576 snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
577 SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
578 0, le32_to_cpu(caps->buffer_size_max));
583 /* fixup the BE DAI link to match any values from topology */
584 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params)
586 struct snd_interval *rate = hw_param_interval(params,
587 SNDRV_PCM_HW_PARAM_RATE);
588 struct snd_interval *channels = hw_param_interval(params,
589 SNDRV_PCM_HW_PARAM_CHANNELS);
590 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
591 struct snd_soc_component *component =
592 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
593 struct snd_sof_dai *dai =
594 snd_sof_find_dai(component, (char *)rtd->dai_link->name);
595 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
596 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
598 /* no topology exists for this BE, try a common configuration */
600 dev_warn(component->dev,
601 "warning: no topology found for BE DAI %s config\n",
602 rtd->dai_link->name);
604 /* set 48k, stereo, 16bits by default */
612 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
617 if (pcm_ops && pcm_ops->dai_link_fixup)
618 return pcm_ops->dai_link_fixup(rtd, params);
622 EXPORT_SYMBOL(sof_pcm_dai_link_fixup);
624 static int sof_pcm_probe(struct snd_soc_component *component)
626 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
627 struct snd_sof_pdata *plat_data = sdev->pdata;
628 const char *tplg_filename;
632 * make sure the device is pm_runtime_active before loading the
633 * topology and initiating IPC or bus transactions
635 ret = pm_runtime_resume_and_get(component->dev);
636 if (ret < 0 && ret != -EACCES)
639 /* load the default topology */
640 sdev->component = component;
642 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
644 plat_data->tplg_filename_prefix,
645 plat_data->tplg_filename);
646 if (!tplg_filename) {
651 ret = snd_sof_load_topology(component, tplg_filename);
653 dev_err(component->dev, "error: failed to load DSP topology %d\n",
657 pm_runtime_mark_last_busy(component->dev);
658 pm_runtime_put_autosuspend(component->dev);
663 static void sof_pcm_remove(struct snd_soc_component *component)
665 /* remove topology */
666 snd_soc_tplg_component_remove(component);
669 static int sof_pcm_ack(struct snd_soc_component *component,
670 struct snd_pcm_substream *substream)
672 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
674 return snd_sof_pcm_platform_ack(sdev, substream);
677 static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component,
678 struct snd_pcm_substream *substream)
680 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
681 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
683 if (pcm_ops && pcm_ops->delay)
684 return pcm_ops->delay(component, substream);
689 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
691 struct snd_soc_component_driver *pd = &sdev->plat_drv;
692 struct snd_sof_pdata *plat_data = sdev->pdata;
693 const char *drv_name;
695 if (plat_data->machine)
696 drv_name = plat_data->machine->drv_name;
697 else if (plat_data->of_machine)
698 drv_name = plat_data->of_machine->drv_name;
702 pd->name = "sof-audio-component";
703 pd->probe = sof_pcm_probe;
704 pd->remove = sof_pcm_remove;
705 pd->open = sof_pcm_open;
706 pd->close = sof_pcm_close;
707 pd->hw_params = sof_pcm_hw_params;
708 pd->prepare = sof_pcm_prepare;
709 pd->hw_free = sof_pcm_hw_free;
710 pd->trigger = sof_pcm_trigger;
711 pd->pointer = sof_pcm_pointer;
712 pd->ack = sof_pcm_ack;
713 pd->delay = sof_pcm_delay;
715 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
716 pd->compress_ops = &sof_compressed_ops;
719 pd->pcm_construct = sof_pcm_new;
720 pd->ignore_machine = drv_name;
721 pd->be_pcm_base = SOF_BE_PCM_BASE;
722 pd->use_dai_pcm_id = true;
723 pd->topology_name_prefix = "sof";
725 /* increment module refcount when a pcm is opened */
726 pd->module_get_upon_open = 1;
728 pd->legacy_dai_naming = 1;
731 * The fixup is only needed when the DSP is in use as with the DSPless
732 * mode we are directly using the audio interface
734 if (!sdev->dspless_mode_selected)
735 pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;