xref: /linux/sound/soc/sof/ipc3-pcm.c (revision 320fefa9e2edc67011e235ea1d50f0d00ddfe004)
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) 2021 Intel Corporation. All rights reserved.
7 //
8 //
9 
10 #include <sound/pcm_params.h>
11 #include "ipc3-priv.h"
12 #include "ops.h"
13 #include "sof-priv.h"
14 #include "sof-audio.h"
15 
16 static int sof_ipc3_pcm_hw_free(struct snd_soc_component *component,
17 				struct snd_pcm_substream *substream)
18 {
19 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
20 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
21 	struct sof_ipc_stream stream;
22 	struct sof_ipc_reply reply;
23 	struct snd_sof_pcm *spcm;
24 
25 	spcm = snd_sof_find_spcm_dai(component, rtd);
26 	if (!spcm)
27 		return -EINVAL;
28 
29 	if (!spcm->prepared[substream->stream])
30 		return 0;
31 
32 	stream.hdr.size = sizeof(stream);
33 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
34 	stream.comp_id = spcm->stream[substream->stream].comp_id;
35 
36 	/* send IPC to the DSP */
37 	return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
38 }
39 
40 static int sof_ipc3_pcm_hw_params(struct snd_soc_component *component,
41 				  struct snd_pcm_substream *substream,
42 				  struct snd_pcm_hw_params *params,
43 				  struct snd_sof_platform_stream_params *platform_params)
44 {
45 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
46 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
47 	struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
48 	struct snd_pcm_runtime *runtime = substream->runtime;
49 	struct sof_ipc_pcm_params_reply ipc_params_reply;
50 	struct sof_ipc_pcm_params pcm;
51 	struct snd_sof_pcm *spcm;
52 	int ret;
53 
54 	spcm = snd_sof_find_spcm_dai(component, rtd);
55 	if (!spcm)
56 		return -EINVAL;
57 
58 	memset(&pcm, 0, sizeof(pcm));
59 
60 	/* number of pages should be rounded up */
61 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
62 
63 	/* set IPC PCM parameters */
64 	pcm.hdr.size = sizeof(pcm);
65 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
66 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
67 	pcm.params.hdr.size = sizeof(pcm.params);
68 	pcm.params.buffer.phy_addr = spcm->stream[substream->stream].page_table.addr;
69 	pcm.params.buffer.size = runtime->dma_bytes;
70 	pcm.params.direction = substream->stream;
71 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
72 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
73 	pcm.params.rate = params_rate(params);
74 	pcm.params.channels = params_channels(params);
75 	pcm.params.host_period_bytes = params_period_bytes(params);
76 
77 	/* container size */
78 	ret = snd_pcm_format_physical_width(params_format(params));
79 	if (ret < 0)
80 		return ret;
81 	pcm.params.sample_container_bytes = ret >> 3;
82 
83 	/* format */
84 	switch (params_format(params)) {
85 	case SNDRV_PCM_FORMAT_S16:
86 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
87 		break;
88 	case SNDRV_PCM_FORMAT_S24:
89 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
90 		break;
91 	case SNDRV_PCM_FORMAT_S32:
92 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
93 		break;
94 	case SNDRV_PCM_FORMAT_FLOAT:
95 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
96 		break;
97 	default:
98 		return -EINVAL;
99 	}
100 
101 	/* Update the IPC message with information from the platform */
102 	pcm.params.stream_tag = platform_params->stream_tag;
103 
104 	if (platform_params->use_phy_address)
105 		pcm.params.buffer.phy_addr = platform_params->phy_addr;
106 
107 	if (platform_params->no_ipc_position) {
108 		/* For older ABIs set host_period_bytes to zero to inform
109 		 * FW we don't want position updates. Newer versions use
110 		 * no_stream_position for this purpose.
111 		 */
112 		if (v->abi_version < SOF_ABI_VER(3, 10, 0))
113 			pcm.params.host_period_bytes = 0;
114 		else
115 			pcm.params.no_stream_position = 1;
116 	}
117 
118 	if (platform_params->cont_update_posn)
119 		pcm.params.cont_update_posn = 1;
120 
121 	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
122 
123 	/* send hw_params IPC to the DSP */
124 	ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm),
125 				 &ipc_params_reply, sizeof(ipc_params_reply));
126 	if (ret < 0) {
127 		dev_err(component->dev, "HW params ipc failed for stream %d\n",
128 			pcm.params.stream_tag);
129 		return ret;
130 	}
131 
132 	ret = snd_sof_set_stream_data_offset(sdev, substream, ipc_params_reply.posn_offset);
133 	if (ret < 0) {
134 		dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
135 			__func__, spcm->pcm.pcm_id);
136 		return ret;
137 	}
138 
139 	return ret;
140 }
141 
142 static int sof_ipc3_pcm_trigger(struct snd_soc_component *component,
143 				struct snd_pcm_substream *substream, int cmd)
144 {
145 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
146 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
147 	struct sof_ipc_stream stream;
148 	struct sof_ipc_reply reply;
149 	struct snd_sof_pcm *spcm;
150 
151 	spcm = snd_sof_find_spcm_dai(component, rtd);
152 	if (!spcm)
153 		return -EINVAL;
154 
155 	stream.hdr.size = sizeof(stream);
156 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
157 	stream.comp_id = spcm->stream[substream->stream].comp_id;
158 
159 	switch (cmd) {
160 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
161 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
162 		break;
163 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
164 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
165 		break;
166 	case SNDRV_PCM_TRIGGER_START:
167 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
168 		break;
169 	case SNDRV_PCM_TRIGGER_SUSPEND:
170 		fallthrough;
171 	case SNDRV_PCM_TRIGGER_STOP:
172 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
173 		break;
174 	default:
175 		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
176 		return -EINVAL;
177 	}
178 
179 	/* send IPC to the DSP */
180 	return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
181 }
182 
183 static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
184 					    struct snd_pcm_hw_params *params)
185 {
186 	struct sof_ipc_dai_config *config;
187 	struct snd_sof_dai *dai;
188 	int i;
189 
190 	/*
191 	 * Search for all matching DAIs as we can have both playback and capture DAI
192 	 * associated with the same link.
193 	 */
194 	list_for_each_entry(dai, &sdev->dai_list, list) {
195 		if (!dai->name || strcmp(link_name, dai->name))
196 			continue;
197 		for (i = 0; i < dai->number_configs; i++) {
198 			struct sof_dai_private_data *private = dai->private;
199 
200 			config = &private->dai_config[i];
201 			if (config->ssp.fsync_rate == params_rate(params)) {
202 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
203 				dai->current_config = i;
204 				break;
205 			}
206 		}
207 	}
208 }
209 
210 static int sof_ipc3_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
211 				       struct snd_pcm_hw_params *params)
212 {
213 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
214 	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
215 	struct snd_sof_dai *dai = snd_sof_find_dai(component, (char *)rtd->dai_link->name);
216 	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
217 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
218 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
219 	struct sof_dai_private_data *private;
220 	struct snd_soc_dpcm *dpcm;
221 
222 	if (!dai) {
223 		dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
224 			rtd->dai_link->name);
225 		return -EINVAL;
226 	}
227 
228 	private = dai->private;
229 	if (!private) {
230 		dev_err(component->dev, "%s: No private data found for DAI %s\n", __func__,
231 			rtd->dai_link->name);
232 		return -EINVAL;
233 	}
234 
235 	/* read format from topology */
236 	snd_mask_none(fmt);
237 
238 	switch (private->comp_dai->config.frame_fmt) {
239 	case SOF_IPC_FRAME_S16_LE:
240 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
241 		break;
242 	case SOF_IPC_FRAME_S24_4LE:
243 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
244 		break;
245 	case SOF_IPC_FRAME_S32_LE:
246 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
247 		break;
248 	default:
249 		dev_err(component->dev, "No available DAI format!\n");
250 		return -EINVAL;
251 	}
252 
253 	/* read rate and channels from topology */
254 	switch (private->dai_config->type) {
255 	case SOF_DAI_INTEL_SSP:
256 		/* search for config to pcm params match, if not found use default */
257 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
258 
259 		rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
260 		rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
261 		channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
262 		channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;
263 
264 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
265 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
266 			channels->min, channels->max);
267 
268 		break;
269 	case SOF_DAI_INTEL_DMIC:
270 		/* DMIC only supports 16 or 32 bit formats */
271 		if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
272 			dev_err(component->dev, "Invalid fmt %d for DAI type %d\n",
273 				private->comp_dai->config.frame_fmt,
274 				private->dai_config->type);
275 		}
276 		break;
277 	case SOF_DAI_INTEL_HDA:
278 		/*
279 		 * HDAudio does not follow the default trigger
280 		 * sequence due to firmware implementation
281 		 */
282 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
283 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
284 
285 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
286 				SND_SOC_DPCM_TRIGGER_POST;
287 		}
288 		break;
289 	case SOF_DAI_INTEL_ALH:
290 		/*
291 		 * Dai could run with different channel count compared with
292 		 * front end, so get dai channel count from topology
293 		 */
294 		channels->min = private->dai_config->alh.channels;
295 		channels->max = private->dai_config->alh.channels;
296 		break;
297 	case SOF_DAI_IMX_ESAI:
298 		rate->min = private->dai_config->esai.fsync_rate;
299 		rate->max = private->dai_config->esai.fsync_rate;
300 		channels->min = private->dai_config->esai.tdm_slots;
301 		channels->max = private->dai_config->esai.tdm_slots;
302 
303 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
304 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
305 			channels->min, channels->max);
306 		break;
307 	case SOF_DAI_MEDIATEK_AFE:
308 		rate->min = private->dai_config->afe.rate;
309 		rate->max = private->dai_config->afe.rate;
310 		channels->min = private->dai_config->afe.channels;
311 		channels->max = private->dai_config->afe.channels;
312 
313 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
314 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
315 			channels->min, channels->max);
316 		break;
317 	case SOF_DAI_IMX_SAI:
318 		rate->min = private->dai_config->sai.fsync_rate;
319 		rate->max = private->dai_config->sai.fsync_rate;
320 		channels->min = private->dai_config->sai.tdm_slots;
321 		channels->max = private->dai_config->sai.tdm_slots;
322 
323 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
324 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
325 			channels->min, channels->max);
326 		break;
327 	case SOF_DAI_AMD_BT:
328 		rate->min = private->dai_config->acpbt.fsync_rate;
329 		rate->max = private->dai_config->acpbt.fsync_rate;
330 		channels->min = private->dai_config->acpbt.tdm_slots;
331 		channels->max = private->dai_config->acpbt.tdm_slots;
332 
333 		dev_dbg(component->dev,
334 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
335 		dev_dbg(component->dev, "AMD_BT channels_min: %d channels_max: %d\n",
336 			channels->min, channels->max);
337 		break;
338 	case SOF_DAI_AMD_SP:
339 		rate->min = private->dai_config->acpsp.fsync_rate;
340 		rate->max = private->dai_config->acpsp.fsync_rate;
341 		channels->min = private->dai_config->acpsp.tdm_slots;
342 		channels->max = private->dai_config->acpsp.tdm_slots;
343 
344 		dev_dbg(component->dev,
345 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
346 		dev_dbg(component->dev, "AMD_SP channels_min: %d channels_max: %d\n",
347 			channels->min, channels->max);
348 		break;
349 	case SOF_DAI_AMD_HS:
350 		rate->min = private->dai_config->acphs.fsync_rate;
351 		rate->max = private->dai_config->acphs.fsync_rate;
352 		channels->min = private->dai_config->acphs.tdm_slots;
353 		channels->max = private->dai_config->acphs.tdm_slots;
354 
355 		dev_dbg(component->dev,
356 			"AMD_HS channel_max: %d rate_max: %d\n", channels->max, rate->max);
357 		break;
358 	case SOF_DAI_AMD_DMIC:
359 		rate->min = private->dai_config->acpdmic.pdm_rate;
360 		rate->max = private->dai_config->acpdmic.pdm_rate;
361 		channels->min = private->dai_config->acpdmic.pdm_ch;
362 		channels->max = private->dai_config->acpdmic.pdm_ch;
363 
364 		dev_dbg(component->dev,
365 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
366 		dev_dbg(component->dev, "AMD_DMIC channels_min: %d channels_max: %d\n",
367 			channels->min, channels->max);
368 		break;
369 	default:
370 		dev_err(component->dev, "Invalid DAI type %d\n", private->dai_config->type);
371 		break;
372 	}
373 
374 	return 0;
375 }
376 
377 const struct sof_ipc_pcm_ops ipc3_pcm_ops = {
378 	.hw_params = sof_ipc3_pcm_hw_params,
379 	.hw_free = sof_ipc3_pcm_hw_free,
380 	.trigger = sof_ipc3_pcm_trigger,
381 	.dai_link_fixup = sof_ipc3_pcm_dai_link_fixup,
382 };
383