xref: /openbmc/linux/sound/soc/sof/ipc3-pcm.c (revision d47a97bd)
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, &spcm->stream[substream->stream],
133 					     ipc_params_reply.posn_offset);
134 	if (ret < 0) {
135 		dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
136 			__func__, spcm->pcm.pcm_id);
137 		return ret;
138 	}
139 
140 	return ret;
141 }
142 
143 static int sof_ipc3_pcm_trigger(struct snd_soc_component *component,
144 				struct snd_pcm_substream *substream, int cmd)
145 {
146 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
147 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
148 	struct sof_ipc_stream stream;
149 	struct sof_ipc_reply reply;
150 	struct snd_sof_pcm *spcm;
151 
152 	spcm = snd_sof_find_spcm_dai(component, rtd);
153 	if (!spcm)
154 		return -EINVAL;
155 
156 	stream.hdr.size = sizeof(stream);
157 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
158 	stream.comp_id = spcm->stream[substream->stream].comp_id;
159 
160 	switch (cmd) {
161 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
162 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
163 		break;
164 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
165 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
166 		break;
167 	case SNDRV_PCM_TRIGGER_START:
168 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
169 		break;
170 	case SNDRV_PCM_TRIGGER_SUSPEND:
171 		fallthrough;
172 	case SNDRV_PCM_TRIGGER_STOP:
173 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
174 		break;
175 	default:
176 		dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
177 		return -EINVAL;
178 	}
179 
180 	/* send IPC to the DSP */
181 	return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
182 }
183 
184 static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
185 					    struct snd_pcm_hw_params *params)
186 {
187 	struct sof_ipc_dai_config *config;
188 	struct snd_sof_dai *dai;
189 	int i;
190 
191 	/*
192 	 * Search for all matching DAIs as we can have both playback and capture DAI
193 	 * associated with the same link.
194 	 */
195 	list_for_each_entry(dai, &sdev->dai_list, list) {
196 		if (!dai->name || strcmp(link_name, dai->name))
197 			continue;
198 		for (i = 0; i < dai->number_configs; i++) {
199 			struct sof_dai_private_data *private = dai->private;
200 
201 			config = &private->dai_config[i];
202 			if (config->ssp.fsync_rate == params_rate(params)) {
203 				dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
204 				dai->current_config = i;
205 				break;
206 			}
207 		}
208 	}
209 }
210 
211 static int sof_ipc3_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
212 				       struct snd_pcm_hw_params *params)
213 {
214 	struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
215 	struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
216 	struct snd_sof_dai *dai = snd_sof_find_dai(component, (char *)rtd->dai_link->name);
217 	struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
218 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
219 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
220 	struct sof_dai_private_data *private;
221 	struct snd_soc_dpcm *dpcm;
222 
223 	if (!dai) {
224 		dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
225 			rtd->dai_link->name);
226 		return -EINVAL;
227 	}
228 
229 	private = dai->private;
230 	if (!private) {
231 		dev_err(component->dev, "%s: No private data found for DAI %s\n", __func__,
232 			rtd->dai_link->name);
233 		return -EINVAL;
234 	}
235 
236 	/* read format from topology */
237 	snd_mask_none(fmt);
238 
239 	switch (private->comp_dai->config.frame_fmt) {
240 	case SOF_IPC_FRAME_S16_LE:
241 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
242 		break;
243 	case SOF_IPC_FRAME_S24_4LE:
244 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
245 		break;
246 	case SOF_IPC_FRAME_S32_LE:
247 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
248 		break;
249 	default:
250 		dev_err(component->dev, "No available DAI format!\n");
251 		return -EINVAL;
252 	}
253 
254 	/* read rate and channels from topology */
255 	switch (private->dai_config->type) {
256 	case SOF_DAI_INTEL_SSP:
257 		/* search for config to pcm params match, if not found use default */
258 		ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
259 
260 		rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
261 		rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
262 		channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
263 		channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;
264 
265 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
266 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
267 			channels->min, channels->max);
268 
269 		break;
270 	case SOF_DAI_INTEL_DMIC:
271 		/* DMIC only supports 16 or 32 bit formats */
272 		if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
273 			dev_err(component->dev, "Invalid fmt %d for DAI type %d\n",
274 				private->comp_dai->config.frame_fmt,
275 				private->dai_config->type);
276 		}
277 		break;
278 	case SOF_DAI_INTEL_HDA:
279 		/*
280 		 * HDAudio does not follow the default trigger
281 		 * sequence due to firmware implementation
282 		 */
283 		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
284 			struct snd_soc_pcm_runtime *fe = dpcm->fe;
285 
286 			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
287 				SND_SOC_DPCM_TRIGGER_POST;
288 		}
289 		break;
290 	case SOF_DAI_INTEL_ALH:
291 		/*
292 		 * Dai could run with different channel count compared with
293 		 * front end, so get dai channel count from topology
294 		 */
295 		channels->min = private->dai_config->alh.channels;
296 		channels->max = private->dai_config->alh.channels;
297 		break;
298 	case SOF_DAI_IMX_ESAI:
299 		rate->min = private->dai_config->esai.fsync_rate;
300 		rate->max = private->dai_config->esai.fsync_rate;
301 		channels->min = private->dai_config->esai.tdm_slots;
302 		channels->max = private->dai_config->esai.tdm_slots;
303 
304 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
305 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
306 			channels->min, channels->max);
307 		break;
308 	case SOF_DAI_MEDIATEK_AFE:
309 		rate->min = private->dai_config->afe.rate;
310 		rate->max = private->dai_config->afe.rate;
311 		channels->min = private->dai_config->afe.channels;
312 		channels->max = private->dai_config->afe.channels;
313 
314 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
315 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
316 			channels->min, channels->max);
317 		break;
318 	case SOF_DAI_IMX_SAI:
319 		rate->min = private->dai_config->sai.fsync_rate;
320 		rate->max = private->dai_config->sai.fsync_rate;
321 		channels->min = private->dai_config->sai.tdm_slots;
322 		channels->max = private->dai_config->sai.tdm_slots;
323 
324 		dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
325 		dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
326 			channels->min, channels->max);
327 		break;
328 	case SOF_DAI_AMD_BT:
329 		rate->min = private->dai_config->acpbt.fsync_rate;
330 		rate->max = private->dai_config->acpbt.fsync_rate;
331 		channels->min = private->dai_config->acpbt.tdm_slots;
332 		channels->max = private->dai_config->acpbt.tdm_slots;
333 
334 		dev_dbg(component->dev,
335 			"AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
336 		dev_dbg(component->dev, "AMD_BT channels_min: %d channels_max: %d\n",
337 			channels->min, channels->max);
338 		break;
339 	case SOF_DAI_AMD_SP:
340 	case SOF_DAI_AMD_SP_VIRTUAL:
341 		rate->min = private->dai_config->acpsp.fsync_rate;
342 		rate->max = private->dai_config->acpsp.fsync_rate;
343 		channels->min = private->dai_config->acpsp.tdm_slots;
344 		channels->max = private->dai_config->acpsp.tdm_slots;
345 
346 		dev_dbg(component->dev,
347 			"AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
348 		dev_dbg(component->dev, "AMD_SP channels_min: %d channels_max: %d\n",
349 			channels->min, channels->max);
350 		break;
351 	case SOF_DAI_AMD_HS:
352 	case SOF_DAI_AMD_HS_VIRTUAL:
353 		rate->min = private->dai_config->acphs.fsync_rate;
354 		rate->max = private->dai_config->acphs.fsync_rate;
355 		channels->min = private->dai_config->acphs.tdm_slots;
356 		channels->max = private->dai_config->acphs.tdm_slots;
357 
358 		dev_dbg(component->dev,
359 			"AMD_HS channel_max: %d rate_max: %d\n", channels->max, rate->max);
360 		break;
361 	case SOF_DAI_AMD_DMIC:
362 		rate->min = private->dai_config->acpdmic.pdm_rate;
363 		rate->max = private->dai_config->acpdmic.pdm_rate;
364 		channels->min = private->dai_config->acpdmic.pdm_ch;
365 		channels->max = private->dai_config->acpdmic.pdm_ch;
366 
367 		dev_dbg(component->dev,
368 			"AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
369 		dev_dbg(component->dev, "AMD_DMIC channels_min: %d channels_max: %d\n",
370 			channels->min, channels->max);
371 		break;
372 	default:
373 		dev_err(component->dev, "Invalid DAI type %d\n", private->dai_config->type);
374 		break;
375 	}
376 
377 	return 0;
378 }
379 
380 const struct sof_ipc_pcm_ops ipc3_pcm_ops = {
381 	.hw_params = sof_ipc3_pcm_hw_params,
382 	.hw_free = sof_ipc3_pcm_hw_free,
383 	.trigger = sof_ipc3_pcm_trigger,
384 	.dai_link_fixup = sof_ipc3_pcm_dai_link_fixup,
385 };
386