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