xref: /openbmc/linux/sound/soc/sof/ipc4-pcm.c (revision 34b7074d)
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) 2022 Intel Corporation. All rights reserved.
7 //
8 
9 #include <sound/pcm_params.h>
10 #include <sound/sof/ipc4/header.h>
11 #include "sof-audio.h"
12 #include "sof-priv.h"
13 #include "ipc4-priv.h"
14 #include "ipc4-topology.h"
15 
16 int sof_ipc4_set_pipeline_state(struct snd_sof_dev *sdev, u32 id, u32 state)
17 {
18 	struct sof_ipc4_msg msg = {{ 0 }};
19 	u32 primary;
20 
21 	dev_dbg(sdev->dev, "ipc4 set pipeline %d state %d", id, state);
22 
23 	primary = 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);
28 
29 	msg.primary = primary;
30 
31 	return sof_ipc_tx_message(sdev->ipc, &msg, 0, NULL, 0);
32 }
33 EXPORT_SYMBOL(sof_ipc4_set_pipeline_state);
34 
35 static int sof_ipc4_trigger_pipelines(struct snd_soc_component *component,
36 				      struct snd_pcm_substream *substream, int state)
37 {
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;
46 	int ret = 0;
47 	int num_widgets;
48 
49 	spcm = snd_sof_find_spcm_dai(component, rtd);
50 	if (!spcm)
51 		return -EINVAL;
52 
53 	list = spcm->stream[substream->stream].list;
54 
55 	for_each_dapm_widgets(list, num_widgets, widget) {
56 		swidget = widget->dobj.private;
57 
58 		if (!swidget)
59 			continue;
60 
61 		/*
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.
64 		 */
65 		switch (state) {
66 		case SOF_IPC4_PIPE_PAUSED:
67 		case SOF_IPC4_PIPE_RESET:
68 			if (!WIDGET_IS_AIF(swidget->id))
69 				continue;
70 			break;
71 		default:
72 			break;
73 		}
74 
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;
78 
79 		if (pipeline->state == state)
80 			continue;
81 
82 		/* first set the pipeline to PAUSED state */
83 		if (pipeline->state != SOF_IPC4_PIPE_PAUSED) {
84 			ret = sof_ipc4_set_pipeline_state(sdev, pipeline_widget->instance_id,
85 							  SOF_IPC4_PIPE_PAUSED);
86 			if (ret < 0) {
87 				dev_err(sdev->dev, "failed to pause pipeline %d\n",
88 					swidget->pipeline_id);
89 				return ret;
90 			}
91 		}
92 
93 		pipeline->state = SOF_IPC4_PIPE_PAUSED;
94 
95 		if (pipeline->state == state)
96 			continue;
97 
98 		/* then set the final state */
99 		ret = sof_ipc4_set_pipeline_state(sdev, pipeline_widget->instance_id, state);
100 		if (ret < 0) {
101 			dev_err(sdev->dev, "failed to set state %d for pipeline %d\n",
102 				state, swidget->pipeline_id);
103 			break;
104 		}
105 
106 		pipeline->state = state;
107 	}
108 
109 	return ret;
110 }
111 
112 static int sof_ipc4_pcm_trigger(struct snd_soc_component *component,
113 				struct snd_pcm_substream *substream, int cmd)
114 {
115 	int state;
116 
117 	/* determine the pipeline state */
118 	switch (cmd) {
119 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
120 		state = SOF_IPC4_PIPE_PAUSED;
121 		break;
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;
126 		break;
127 	case SNDRV_PCM_TRIGGER_SUSPEND:
128 	case SNDRV_PCM_TRIGGER_STOP:
129 		state = SOF_IPC4_PIPE_PAUSED;
130 		break;
131 	default:
132 		dev_err(component->dev, "%s: unhandled trigger cmd %d\n", __func__, cmd);
133 		return -EINVAL;
134 	}
135 
136 	/* set the pipeline state */
137 	return sof_ipc4_trigger_pipelines(component, substream, state);
138 }
139 
140 static int sof_ipc4_pcm_hw_free(struct snd_soc_component *component,
141 				struct snd_pcm_substream *substream)
142 {
143 	return sof_ipc4_trigger_pipelines(component, substream, SOF_IPC4_PIPE_RESET);
144 }
145 
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)
148 {
149 	struct snd_sof_dai_link *slink;
150 	struct snd_sof_dai *dai;
151 	bool dai_link_found = false;
152 	int i;
153 
154 	list_for_each_entry(slink, &sdev->dai_link_list, list) {
155 		if (!strcmp(slink->link->name, link_name)) {
156 			dai_link_found = true;
157 			break;
158 		}
159 	}
160 
161 	if (!dai_link_found)
162 		return;
163 
164 	for (i = 0; i < slink->num_hw_configs; i++) {
165 		struct snd_soc_tplg_hw_config *hw_config = &slink->hw_configs[i];
166 
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);
172 			break;
173 		}
174 	}
175 }
176 
177 static int sof_ipc4_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
178 				       struct snd_pcm_hw_params *params)
179 {
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;
187 
188 	if (!dai) {
189 		dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
190 			rtd->dai_link->name);
191 		return -EINVAL;
192 	}
193 
194 	ipc4_copier = dai->private;
195 	if (!ipc4_copier) {
196 		dev_err(component->dev, "%s: No private data found for DAI %s\n",
197 			__func__, rtd->dai_link->name);
198 		return -EINVAL;
199 	}
200 
201 	/* always set BE format to 32-bits for both playback and capture */
202 	snd_mask_none(fmt);
203 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
204 
205 	rate->min = ipc4_copier->available_fmt.base_config->audio_fmt.sampling_frequency;
206 	rate->max = rate->min;
207 
208 	/*
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.
212 	 */
213 	for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_CAPTURE, dpcm) {
214 		struct snd_soc_pcm_runtime *fe = dpcm->fe;
215 
216 		fe->dai_link->trigger[SNDRV_PCM_STREAM_CAPTURE] = SND_SOC_DPCM_TRIGGER_PRE;
217 	}
218 
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);
222 		break;
223 	default:
224 		break;
225 	}
226 
227 	return 0;
228 }
229 
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,
234 };
235