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) 2022 Intel Corporation. All rights reserved.
9 #include <sound/pcm_params.h>
10 #include <sound/sof/ipc4/header.h>
11 #include "sof-audio.h"
13 #include "ipc4-priv.h"
14 #include "ipc4-topology.h"
16 int sof_ipc4_set_pipeline_state(struct snd_sof_dev *sdev, u32 id, u32 state)
18 struct sof_ipc4_msg msg = {{ 0 }};
21 dev_dbg(sdev->dev, "ipc4 set pipeline %d state %d", id, state);
24 primary |= SOF_IPC4_GLB_PIPE_STATE_ID(id);
25 primary |= SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_GLB_SET_PIPELINE_STATE);
26 primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
27 primary |= SOF_IPC4_MSG_TARGET(SOF_IPC4_FW_GEN_MSG);
29 msg.primary = primary;
31 return sof_ipc_tx_message(sdev->ipc, &msg, 0, NULL, 0);
33 EXPORT_SYMBOL(sof_ipc4_set_pipeline_state);
35 static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
36 struct snd_pcm_substream *substream, int state)
38 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
39 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
40 struct snd_sof_widget *pipeline_widget;
41 struct snd_soc_dapm_widget_list *list;
42 struct snd_soc_dapm_widget *widget;
43 struct sof_ipc4_pipeline *pipeline;
44 struct snd_sof_widget *swidget;
45 struct snd_sof_pcm *spcm;
49 spcm = snd_sof_find_spcm_dai(component, rtd);
53 list = spcm->stream[substream->stream].list;
55 for_each_dapm_widgets(list, num_widgets, widget) {
56 swidget = widget->dobj.private;
62 * set pipeline state for both FE and BE pipelines for RUNNING state.
63 * For PAUSE/RESET, set the pipeline state only for the FE pipeline.
66 case SOF_IPC4_PIPE_PAUSED:
67 case SOF_IPC4_PIPE_RESET:
68 if (!WIDGET_IS_AIF(swidget->id))
75 /* find pipeline widget for the pipeline that this widget belongs to */
76 pipeline_widget = swidget->pipe_widget;
77 pipeline = (struct sof_ipc4_pipeline *)pipeline_widget->private;
79 if (pipeline->state == state)
82 /* first set the pipeline to PAUSED state */
83 if (pipeline->state != SOF_IPC4_PIPE_PAUSED) {
84 ret = sof_ipc4_set_pipeline_state(sdev, swidget->pipeline_id,
85 SOF_IPC4_PIPE_PAUSED);
87 dev_err(sdev->dev, "failed to pause pipeline %d\n",
88 swidget->pipeline_id);
93 pipeline->state = SOF_IPC4_PIPE_PAUSED;
95 if (pipeline->state == state)
98 /* then set the final state */
99 ret = sof_ipc4_set_pipeline_state(sdev, swidget->pipeline_id, state);
101 dev_err(sdev->dev, "failed to set state %d for pipeline %d\n",
102 state, swidget->pipeline_id);
106 pipeline->state = state;
112 static int sof_ipc4_pcm_trigger(struct snd_soc_component *component,
113 struct snd_pcm_substream *substream, int cmd)
117 /* determine the pipeline state */
119 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
120 state = SOF_IPC4_PIPE_PAUSED;
122 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
123 case SNDRV_PCM_TRIGGER_RESUME:
124 case SNDRV_PCM_TRIGGER_START:
125 state = SOF_IPC4_PIPE_RUNNING;
127 case SNDRV_PCM_TRIGGER_SUSPEND:
128 case SNDRV_PCM_TRIGGER_STOP:
129 state = SOF_IPC4_PIPE_PAUSED;
132 dev_err(component->dev, "%s: unhandled trigger cmd %d\n", __func__, cmd);
136 /* set the pipeline state */
137 return sof_ipc4_trigger_pipelines(component, substream, state);
140 static int sof_ipc4_pcm_hw_free(struct snd_soc_component *component,
141 struct snd_pcm_substream *substream)
143 return sof_ipc4_trigger_pipelines(component, substream, SOF_IPC4_PIPE_RESET);
146 static void ipc4_ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
147 struct snd_pcm_hw_params *params)
149 struct snd_sof_dai_link *slink;
150 struct snd_sof_dai *dai;
151 bool dai_link_found = false;
154 list_for_each_entry(slink, &sdev->dai_link_list, list) {
155 if (!strcmp(slink->link->name, link_name)) {
156 dai_link_found = true;
164 for (i = 0; i < slink->num_hw_configs; i++) {
165 struct snd_soc_tplg_hw_config *hw_config = &slink->hw_configs[i];
167 if (params_rate(params) == le32_to_cpu(hw_config->fsync_rate)) {
168 /* set current config for all DAI's with matching name */
169 list_for_each_entry(dai, &sdev->dai_list, list)
170 if (!strcmp(slink->link->name, dai->name))
171 dai->current_config = le32_to_cpu(hw_config->id);
177 static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
178 struct snd_pcm_hw_params *params)
180 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
181 struct snd_sof_dai *dai = snd_sof_find_dai(component, rtd->dai_link->name);
182 struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
183 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
184 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
185 struct sof_ipc4_copier *ipc4_copier;
186 struct snd_soc_dpcm *dpcm;
189 dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
190 rtd->dai_link->name);
194 ipc4_copier = dai->private;
196 dev_err(component->dev, "%s: No private data found for DAI %s\n",
197 __func__, rtd->dai_link->name);
201 /* always set BE format to 32-bits for both playback and capture */
203 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
205 rate->min = ipc4_copier->available_fmt.base_config->audio_fmt.sampling_frequency;
206 rate->max = rate->min;
209 * Set trigger order for capture to SND_SOC_DPCM_TRIGGER_PRE. This is required
210 * to ensure that the BE DAI pipeline gets stopped/suspended before the FE DAI
211 * pipeline gets triggered and the pipeline widgets are freed.
213 for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_CAPTURE, dpcm) {
214 struct snd_soc_pcm_runtime *fe = dpcm->fe;
216 fe->dai_link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_PRE;
219 switch (ipc4_copier->dai_type) {
220 case SOF_DAI_INTEL_SSP:
221 ipc4_ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
230 const struct sof_ipc_pcm_ops ipc4_pcm_ops = {
231 .trigger = sof_ipc4_pcm_trigger,
232 .hw_free = sof_ipc4_pcm_hw_free,
233 .dai_link_fixup = sof_ipc4_pcm_dai_link_fixup,