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) 2021 Intel Corporation. All rights reserved.
10 #include <sound/pcm_params.h>
11 #include "ipc3-priv.h"
14 #include "sof-audio.h"
16 static int sof_ipc3_pcm_hw_free(struct snd_soc_component *component,
17 struct snd_pcm_substream *substream)
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;
25 spcm = snd_sof_find_spcm_dai(component, rtd);
29 if (!spcm->prepared[substream->stream])
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;
36 /* send IPC to the DSP */
37 return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
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)
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;
54 spcm = snd_sof_find_spcm_dai(component, rtd);
58 memset(&pcm, 0, sizeof(pcm));
60 /* number of pages should be rounded up */
61 pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
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);
78 ret = snd_pcm_format_physical_width(params_format(params));
81 pcm.params.sample_container_bytes = ret >> 3;
84 switch (params_format(params)) {
85 case SNDRV_PCM_FORMAT_S16:
86 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
88 case SNDRV_PCM_FORMAT_S24:
89 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
91 case SNDRV_PCM_FORMAT_S32:
92 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
94 case SNDRV_PCM_FORMAT_FLOAT:
95 pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
101 /* Update the IPC message with information from the platform */
102 pcm.params.stream_tag = platform_params->stream_tag;
104 if (platform_params->use_phy_address)
105 pcm.params.buffer.phy_addr = platform_params->phy_addr;
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.
112 if (v->abi_version < SOF_ABI_VER(3, 10, 0))
113 pcm.params.host_period_bytes = 0;
115 pcm.params.no_stream_position = 1;
118 if (platform_params->cont_update_posn)
119 pcm.params.cont_update_posn = 1;
121 dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
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));
127 dev_err(component->dev, "HW params ipc failed for stream %d\n",
128 pcm.params.stream_tag);
132 ret = snd_sof_set_stream_data_offset(sdev, substream, ipc_params_reply.posn_offset);
134 dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
135 __func__, spcm->pcm.pcm_id);
142 static int sof_ipc3_pcm_trigger(struct snd_soc_component *component,
143 struct snd_pcm_substream *substream, int cmd)
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;
151 spcm = snd_sof_find_spcm_dai(component, rtd);
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;
160 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
161 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
163 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
164 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
166 case SNDRV_PCM_TRIGGER_START:
167 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
169 case SNDRV_PCM_TRIGGER_SUSPEND:
171 case SNDRV_PCM_TRIGGER_STOP:
172 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
175 dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
179 /* send IPC to the DSP */
180 return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
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)
186 struct sof_ipc_dai_config *config;
187 struct snd_sof_dai *dai;
191 * Search for all matching DAIs as we can have both playback and capture DAI
192 * associated with the same link.
194 list_for_each_entry(dai, &sdev->dai_list, list) {
195 if (!dai->name || strcmp(link_name, dai->name))
197 for (i = 0; i < dai->number_configs; i++) {
198 struct sof_dai_private_data *private = dai->private;
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;
210 static int sof_ipc3_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
211 struct snd_pcm_hw_params *params)
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;
223 dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
224 rtd->dai_link->name);
228 private = dai->private;
230 dev_err(component->dev, "%s: No private data found for DAI %s\n", __func__,
231 rtd->dai_link->name);
235 /* read format from topology */
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);
242 case SOF_IPC_FRAME_S24_4LE:
243 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
245 case SOF_IPC_FRAME_S32_LE:
246 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
249 dev_err(component->dev, "No available DAI format!\n");
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);
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;
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);
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);
277 case SOF_DAI_INTEL_HDA:
279 * HDAudio does not follow the default trigger
280 * sequence due to firmware implementation
282 for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
283 struct snd_soc_pcm_runtime *fe = dpcm->fe;
285 fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
286 SND_SOC_DPCM_TRIGGER_POST;
289 case SOF_DAI_INTEL_ALH:
291 * Dai could run with different channel count compared with
292 * front end, so get dai channel count from topology
294 channels->min = private->dai_config->alh.channels;
295 channels->max = private->dai_config->alh.channels;
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;
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);
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;
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);
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;
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);
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;
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);
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;
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);
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;
355 dev_dbg(component->dev,
356 "AMD_HS channel_max: %d rate_max: %d\n", channels->max, rate->max);
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;
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);
370 dev_err(component->dev, "Invalid DAI type %d\n", private->dai_config->type);
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,