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 <uapi/sound/sof/tokens.h> 11 #include <sound/pcm_params.h> 12 #include "sof-priv.h" 13 #include "sof-audio.h" 14 #include "ipc3-priv.h" 15 #include "ops.h" 16 17 /* Full volume for default values */ 18 #define VOL_ZERO_DB BIT(VOLUME_FWL) 19 20 /* size of tplg ABI in bytes */ 21 #define SOF_IPC3_TPLG_ABI_SIZE 3 22 23 struct sof_widget_data { 24 int ctrl_type; 25 int ipc_cmd; 26 void *pdata; 27 size_t pdata_size; 28 struct snd_sof_control *control; 29 }; 30 31 struct sof_process_types { 32 const char *name; 33 enum sof_ipc_process_type type; 34 enum sof_comp_type comp_type; 35 }; 36 37 static const struct sof_process_types sof_process[] = { 38 {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR}, 39 {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR}, 40 {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT}, 41 {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB}, 42 {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR}, 43 {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX}, 44 {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX}, 45 {"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK}, 46 {"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP}, 47 }; 48 49 static enum sof_ipc_process_type find_process(const char *name) 50 { 51 int i; 52 53 for (i = 0; i < ARRAY_SIZE(sof_process); i++) { 54 if (strcmp(name, sof_process[i].name) == 0) 55 return sof_process[i].type; 56 } 57 58 return SOF_PROCESS_NONE; 59 } 60 61 static int get_token_process_type(void *elem, void *object, u32 offset) 62 { 63 u32 *val = (u32 *)((u8 *)object + offset); 64 65 *val = find_process((const char *)elem); 66 return 0; 67 } 68 69 /* Buffers */ 70 static const struct sof_topology_token buffer_tokens[] = { 71 {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 72 offsetof(struct sof_ipc_buffer, size)}, 73 {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 74 offsetof(struct sof_ipc_buffer, caps)}, 75 }; 76 77 /* DAI */ 78 static const struct sof_topology_token dai_tokens[] = { 79 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 80 offsetof(struct sof_ipc_comp_dai, type)}, 81 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 82 offsetof(struct sof_ipc_comp_dai, dai_index)}, 83 {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 84 offsetof(struct sof_ipc_comp_dai, direction)}, 85 }; 86 87 /* BE DAI link */ 88 static const struct sof_topology_token dai_link_tokens[] = { 89 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 90 offsetof(struct sof_ipc_dai_config, type)}, 91 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 92 offsetof(struct sof_ipc_dai_config, dai_index)}, 93 }; 94 95 /* scheduling */ 96 static const struct sof_topology_token sched_tokens[] = { 97 {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 98 offsetof(struct sof_ipc_pipe_new, period)}, 99 {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 100 offsetof(struct sof_ipc_pipe_new, priority)}, 101 {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 102 offsetof(struct sof_ipc_pipe_new, period_mips)}, 103 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 104 offsetof(struct sof_ipc_pipe_new, core)}, 105 {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 106 offsetof(struct sof_ipc_pipe_new, frames_per_sched)}, 107 {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 108 offsetof(struct sof_ipc_pipe_new, time_domain)}, 109 }; 110 111 static const struct sof_topology_token pipeline_tokens[] = { 112 {SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 113 offsetof(struct snd_sof_widget, dynamic_pipeline_widget)}, 114 115 }; 116 117 /* volume */ 118 static const struct sof_topology_token volume_tokens[] = { 119 {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 120 offsetof(struct sof_ipc_comp_volume, ramp)}, 121 {SOF_TKN_VOLUME_RAMP_STEP_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 122 offsetof(struct sof_ipc_comp_volume, initial_ramp)}, 123 }; 124 125 /* SRC */ 126 static const struct sof_topology_token src_tokens[] = { 127 {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 128 offsetof(struct sof_ipc_comp_src, source_rate)}, 129 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 130 offsetof(struct sof_ipc_comp_src, sink_rate)}, 131 }; 132 133 /* ASRC */ 134 static const struct sof_topology_token asrc_tokens[] = { 135 {SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 136 offsetof(struct sof_ipc_comp_asrc, source_rate)}, 137 {SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 138 offsetof(struct sof_ipc_comp_asrc, sink_rate)}, 139 {SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 140 offsetof(struct sof_ipc_comp_asrc, asynchronous_mode)}, 141 {SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 142 offsetof(struct sof_ipc_comp_asrc, operation_mode)}, 143 }; 144 145 /* EFFECT */ 146 static const struct sof_topology_token process_tokens[] = { 147 {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_process_type, 148 offsetof(struct sof_ipc_comp_process, type)}, 149 }; 150 151 /* PCM */ 152 static const struct sof_topology_token pcm_tokens[] = { 153 {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 154 offsetof(struct sof_ipc_comp_host, dmac_config)}, 155 }; 156 157 /* Generic components */ 158 static const struct sof_topology_token comp_tokens[] = { 159 {SOF_TKN_COMP_PERIOD_SINK_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 160 offsetof(struct sof_ipc_comp_config, periods_sink)}, 161 {SOF_TKN_COMP_PERIOD_SOURCE_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 162 offsetof(struct sof_ipc_comp_config, periods_source)}, 163 {SOF_TKN_COMP_FORMAT, 164 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format, 165 offsetof(struct sof_ipc_comp_config, frame_fmt)}, 166 }; 167 168 /* SSP */ 169 static const struct sof_topology_token ssp_tokens[] = { 170 {SOF_TKN_INTEL_SSP_CLKS_CONTROL, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 171 offsetof(struct sof_ipc_dai_ssp_params, clks_control)}, 172 {SOF_TKN_INTEL_SSP_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 173 offsetof(struct sof_ipc_dai_ssp_params, mclk_id)}, 174 {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 175 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits)}, 176 {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 177 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width)}, 178 {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 179 offsetof(struct sof_ipc_dai_ssp_params, quirks)}, 180 {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 181 offsetof(struct sof_ipc_dai_ssp_params, tdm_per_slot_padding_flag)}, 182 {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 183 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay)}, 184 }; 185 186 /* ALH */ 187 static const struct sof_topology_token alh_tokens[] = { 188 {SOF_TKN_INTEL_ALH_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 189 offsetof(struct sof_ipc_dai_alh_params, rate)}, 190 {SOF_TKN_INTEL_ALH_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 191 offsetof(struct sof_ipc_dai_alh_params, channels)}, 192 }; 193 194 /* DMIC */ 195 static const struct sof_topology_token dmic_tokens[] = { 196 {SOF_TKN_INTEL_DMIC_DRIVER_VERSION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 197 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version)}, 198 {SOF_TKN_INTEL_DMIC_CLK_MIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 199 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min)}, 200 {SOF_TKN_INTEL_DMIC_CLK_MAX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 201 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max)}, 202 {SOF_TKN_INTEL_DMIC_SAMPLE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 203 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs)}, 204 {SOF_TKN_INTEL_DMIC_DUTY_MIN, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 205 offsetof(struct sof_ipc_dai_dmic_params, duty_min)}, 206 {SOF_TKN_INTEL_DMIC_DUTY_MAX, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 207 offsetof(struct sof_ipc_dai_dmic_params, duty_max)}, 208 {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 209 offsetof(struct sof_ipc_dai_dmic_params, num_pdm_active)}, 210 {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 211 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits)}, 212 {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 213 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time)}, 214 }; 215 216 /* ESAI */ 217 static const struct sof_topology_token esai_tokens[] = { 218 {SOF_TKN_IMX_ESAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 219 offsetof(struct sof_ipc_dai_esai_params, mclk_id)}, 220 }; 221 222 /* SAI */ 223 static const struct sof_topology_token sai_tokens[] = { 224 {SOF_TKN_IMX_SAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 225 offsetof(struct sof_ipc_dai_sai_params, mclk_id)}, 226 }; 227 228 /* 229 * DMIC PDM Tokens 230 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token 231 * as it increments the index while parsing the array of pdm tokens 232 * and determines the correct offset 233 */ 234 static const struct sof_topology_token dmic_pdm_tokens[] = { 235 {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 236 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id)}, 237 {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 238 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a)}, 239 {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 240 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b)}, 241 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 242 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a)}, 243 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 244 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b)}, 245 {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 246 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge)}, 247 {SOF_TKN_INTEL_DMIC_PDM_SKEW, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 248 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew)}, 249 }; 250 251 /* HDA */ 252 static const struct sof_topology_token hda_tokens[] = { 253 {SOF_TKN_INTEL_HDA_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 254 offsetof(struct sof_ipc_dai_hda_params, rate)}, 255 {SOF_TKN_INTEL_HDA_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 256 offsetof(struct sof_ipc_dai_hda_params, channels)}, 257 }; 258 259 /* AFE */ 260 static const struct sof_topology_token afe_tokens[] = { 261 {SOF_TKN_MEDIATEK_AFE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 262 offsetof(struct sof_ipc_dai_mtk_afe_params, rate)}, 263 {SOF_TKN_MEDIATEK_AFE_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 264 offsetof(struct sof_ipc_dai_mtk_afe_params, channels)}, 265 {SOF_TKN_MEDIATEK_AFE_FORMAT, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format, 266 offsetof(struct sof_ipc_dai_mtk_afe_params, format)}, 267 }; 268 269 /* ACPDMIC */ 270 static const struct sof_topology_token acpdmic_tokens[] = { 271 {SOF_TKN_AMD_ACPDMIC_RATE, 272 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 273 offsetof(struct sof_ipc_dai_acpdmic_params, pdm_rate)}, 274 {SOF_TKN_AMD_ACPDMIC_CH, 275 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 276 offsetof(struct sof_ipc_dai_acpdmic_params, pdm_ch)}, 277 }; 278 279 /* ACPI2S */ 280 static const struct sof_topology_token acpi2s_tokens[] = { 281 {SOF_TKN_AMD_ACPI2S_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 282 offsetof(struct sof_ipc_dai_acp_params, fsync_rate)}, 283 {SOF_TKN_AMD_ACPI2S_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 284 offsetof(struct sof_ipc_dai_acp_params, tdm_slots)}, 285 {SOF_TKN_AMD_ACPI2S_TDM_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 286 offsetof(struct sof_ipc_dai_acp_params, tdm_mode)}, 287 }; 288 289 /* Core tokens */ 290 static const struct sof_topology_token core_tokens[] = { 291 {SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 292 offsetof(struct sof_ipc_comp, core)}, 293 }; 294 295 /* Component extended tokens */ 296 static const struct sof_topology_token comp_ext_tokens[] = { 297 {SOF_TKN_COMP_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid, 298 offsetof(struct snd_sof_widget, uuid)}, 299 }; 300 301 static const struct sof_token_info ipc3_token_list[SOF_TOKEN_COUNT] = { 302 [SOF_PCM_TOKENS] = {"PCM tokens", pcm_tokens, ARRAY_SIZE(pcm_tokens)}, 303 [SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)}, 304 [SOF_SCHED_TOKENS] = {"Scheduler tokens", sched_tokens, ARRAY_SIZE(sched_tokens)}, 305 [SOF_COMP_TOKENS] = {"Comp tokens", comp_tokens, ARRAY_SIZE(comp_tokens)}, 306 [SOF_CORE_TOKENS] = {"Core tokens", core_tokens, ARRAY_SIZE(core_tokens)}, 307 [SOF_COMP_EXT_TOKENS] = {"AFE tokens", comp_ext_tokens, ARRAY_SIZE(comp_ext_tokens)}, 308 [SOF_BUFFER_TOKENS] = {"Buffer tokens", buffer_tokens, ARRAY_SIZE(buffer_tokens)}, 309 [SOF_VOLUME_TOKENS] = {"Volume tokens", volume_tokens, ARRAY_SIZE(volume_tokens)}, 310 [SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)}, 311 [SOF_ASRC_TOKENS] = {"ASRC tokens", asrc_tokens, ARRAY_SIZE(asrc_tokens)}, 312 [SOF_PROCESS_TOKENS] = {"Process tokens", process_tokens, ARRAY_SIZE(process_tokens)}, 313 [SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)}, 314 [SOF_DAI_LINK_TOKENS] = {"DAI link tokens", dai_link_tokens, ARRAY_SIZE(dai_link_tokens)}, 315 [SOF_HDA_TOKENS] = {"HDA tokens", hda_tokens, ARRAY_SIZE(hda_tokens)}, 316 [SOF_SSP_TOKENS] = {"SSP tokens", ssp_tokens, ARRAY_SIZE(ssp_tokens)}, 317 [SOF_ALH_TOKENS] = {"ALH tokens", alh_tokens, ARRAY_SIZE(alh_tokens)}, 318 [SOF_DMIC_TOKENS] = {"DMIC tokens", dmic_tokens, ARRAY_SIZE(dmic_tokens)}, 319 [SOF_DMIC_PDM_TOKENS] = {"DMIC PDM tokens", dmic_pdm_tokens, ARRAY_SIZE(dmic_pdm_tokens)}, 320 [SOF_ESAI_TOKENS] = {"ESAI tokens", esai_tokens, ARRAY_SIZE(esai_tokens)}, 321 [SOF_SAI_TOKENS] = {"SAI tokens", sai_tokens, ARRAY_SIZE(sai_tokens)}, 322 [SOF_AFE_TOKENS] = {"AFE tokens", afe_tokens, ARRAY_SIZE(afe_tokens)}, 323 [SOF_ACPDMIC_TOKENS] = {"ACPDMIC tokens", acpdmic_tokens, ARRAY_SIZE(acpdmic_tokens)}, 324 [SOF_ACPI2S_TOKENS] = {"ACPI2S tokens", acpi2s_tokens, ARRAY_SIZE(acpi2s_tokens)}, 325 }; 326 327 /** 328 * sof_comp_alloc - allocate and initialize buffer for a new component 329 * @swidget: pointer to struct snd_sof_widget containing extended data 330 * @ipc_size: IPC payload size that will be updated depending on valid 331 * extended data. 332 * @index: ID of the pipeline the component belongs to 333 * 334 * Return: The pointer to the new allocated component, NULL if failed. 335 */ 336 static void *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size, 337 int index) 338 { 339 struct sof_ipc_comp *comp; 340 size_t total_size = *ipc_size; 341 size_t ext_size = sizeof(swidget->uuid); 342 343 /* only non-zero UUID is valid */ 344 if (!guid_is_null(&swidget->uuid)) 345 total_size += ext_size; 346 347 comp = kzalloc(total_size, GFP_KERNEL); 348 if (!comp) 349 return NULL; 350 351 /* configure comp new IPC message */ 352 comp->hdr.size = total_size; 353 comp->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 354 comp->id = swidget->comp_id; 355 comp->pipeline_id = index; 356 comp->core = swidget->core; 357 358 /* handle the extended data if needed */ 359 if (total_size > *ipc_size) { 360 /* append extended data to the end of the component */ 361 memcpy((u8 *)comp + *ipc_size, &swidget->uuid, ext_size); 362 comp->ext_data_length = ext_size; 363 } 364 365 /* update ipc_size and return */ 366 *ipc_size = total_size; 367 return comp; 368 } 369 370 static void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config) 371 { 372 dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n", 373 config->periods_sink, config->periods_source, 374 config->frame_fmt); 375 } 376 377 static int sof_ipc3_widget_setup_comp_host(struct snd_sof_widget *swidget) 378 { 379 struct snd_soc_component *scomp = swidget->scomp; 380 struct sof_ipc_comp_host *host; 381 size_t ipc_size = sizeof(*host); 382 int ret; 383 384 host = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 385 if (!host) 386 return -ENOMEM; 387 swidget->private = host; 388 389 /* configure host comp IPC message */ 390 host->comp.type = SOF_COMP_HOST; 391 host->config.hdr.size = sizeof(host->config); 392 393 if (swidget->id == snd_soc_dapm_aif_out) 394 host->direction = SOF_IPC_STREAM_CAPTURE; 395 else 396 host->direction = SOF_IPC_STREAM_PLAYBACK; 397 398 /* parse one set of pcm_tokens */ 399 ret = sof_update_ipc_object(scomp, host, SOF_PCM_TOKENS, swidget->tuples, 400 swidget->num_tuples, sizeof(*host), 1); 401 if (ret < 0) 402 goto err; 403 404 /* parse one set of comp_tokens */ 405 ret = sof_update_ipc_object(scomp, &host->config, SOF_COMP_TOKENS, swidget->tuples, 406 swidget->num_tuples, sizeof(host->config), 1); 407 if (ret < 0) 408 goto err; 409 410 dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name); 411 sof_dbg_comp_config(scomp, &host->config); 412 413 return 0; 414 err: 415 kfree(swidget->private); 416 swidget->private = NULL; 417 418 return ret; 419 } 420 421 static void sof_ipc3_widget_free_comp(struct snd_sof_widget *swidget) 422 { 423 kfree(swidget->private); 424 } 425 426 static int sof_ipc3_widget_setup_comp_tone(struct snd_sof_widget *swidget) 427 { 428 struct snd_soc_component *scomp = swidget->scomp; 429 struct sof_ipc_comp_tone *tone; 430 size_t ipc_size = sizeof(*tone); 431 int ret; 432 433 tone = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 434 if (!tone) 435 return -ENOMEM; 436 437 swidget->private = tone; 438 439 /* configure siggen IPC message */ 440 tone->comp.type = SOF_COMP_TONE; 441 tone->config.hdr.size = sizeof(tone->config); 442 443 /* parse one set of comp tokens */ 444 ret = sof_update_ipc_object(scomp, &tone->config, SOF_COMP_TOKENS, swidget->tuples, 445 swidget->num_tuples, sizeof(tone->config), 1); 446 if (ret < 0) { 447 kfree(swidget->private); 448 swidget->private = NULL; 449 return ret; 450 } 451 452 dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n", 453 swidget->widget->name, tone->frequency, tone->amplitude); 454 sof_dbg_comp_config(scomp, &tone->config); 455 456 return 0; 457 } 458 459 static int sof_ipc3_widget_setup_comp_mixer(struct snd_sof_widget *swidget) 460 { 461 struct snd_soc_component *scomp = swidget->scomp; 462 struct sof_ipc_comp_mixer *mixer; 463 size_t ipc_size = sizeof(*mixer); 464 int ret; 465 466 mixer = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 467 if (!mixer) 468 return -ENOMEM; 469 470 swidget->private = mixer; 471 472 /* configure mixer IPC message */ 473 mixer->comp.type = SOF_COMP_MIXER; 474 mixer->config.hdr.size = sizeof(mixer->config); 475 476 /* parse one set of comp tokens */ 477 ret = sof_update_ipc_object(scomp, &mixer->config, SOF_COMP_TOKENS, 478 swidget->tuples, swidget->num_tuples, 479 sizeof(mixer->config), 1); 480 if (ret < 0) { 481 kfree(swidget->private); 482 swidget->private = NULL; 483 484 return ret; 485 } 486 487 dev_dbg(scomp->dev, "loaded mixer %s\n", swidget->widget->name); 488 sof_dbg_comp_config(scomp, &mixer->config); 489 490 return 0; 491 } 492 493 static int sof_ipc3_widget_setup_comp_pipeline(struct snd_sof_widget *swidget) 494 { 495 struct snd_soc_component *scomp = swidget->scomp; 496 struct sof_ipc_pipe_new *pipeline; 497 struct snd_sof_widget *comp_swidget; 498 int ret; 499 500 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL); 501 if (!pipeline) 502 return -ENOMEM; 503 504 /* configure pipeline IPC message */ 505 pipeline->hdr.size = sizeof(*pipeline); 506 pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW; 507 pipeline->pipeline_id = swidget->pipeline_id; 508 pipeline->comp_id = swidget->comp_id; 509 510 swidget->private = pipeline; 511 512 /* component at start of pipeline is our stream id */ 513 comp_swidget = snd_sof_find_swidget(scomp, swidget->widget->sname); 514 if (!comp_swidget) { 515 dev_err(scomp->dev, "scheduler %s refers to non existent widget %s\n", 516 swidget->widget->name, swidget->widget->sname); 517 ret = -EINVAL; 518 goto err; 519 } 520 521 pipeline->sched_id = comp_swidget->comp_id; 522 523 /* parse one set of scheduler tokens */ 524 ret = sof_update_ipc_object(scomp, pipeline, SOF_SCHED_TOKENS, swidget->tuples, 525 swidget->num_tuples, sizeof(*pipeline), 1); 526 if (ret < 0) 527 goto err; 528 529 /* parse one set of pipeline tokens */ 530 ret = sof_update_ipc_object(scomp, swidget, SOF_PIPELINE_TOKENS, swidget->tuples, 531 swidget->num_tuples, sizeof(*swidget), 1); 532 if (ret < 0) 533 goto err; 534 535 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) 536 pipeline->core = SOF_DSP_PRIMARY_CORE; 537 538 if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE)) 539 swidget->dynamic_pipeline_widget = 540 sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE); 541 542 dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n", 543 swidget->widget->name, pipeline->period, pipeline->priority, 544 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched, 545 swidget->dynamic_pipeline_widget); 546 547 swidget->core = pipeline->core; 548 549 return 0; 550 551 err: 552 kfree(swidget->private); 553 swidget->private = NULL; 554 555 return ret; 556 } 557 558 static int sof_ipc3_widget_setup_comp_buffer(struct snd_sof_widget *swidget) 559 { 560 struct snd_soc_component *scomp = swidget->scomp; 561 struct sof_ipc_buffer *buffer; 562 int ret; 563 564 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 565 if (!buffer) 566 return -ENOMEM; 567 568 swidget->private = buffer; 569 570 /* configure dai IPC message */ 571 buffer->comp.hdr.size = sizeof(*buffer); 572 buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW; 573 buffer->comp.id = swidget->comp_id; 574 buffer->comp.type = SOF_COMP_BUFFER; 575 buffer->comp.pipeline_id = swidget->pipeline_id; 576 buffer->comp.core = swidget->core; 577 578 /* parse one set of buffer tokens */ 579 ret = sof_update_ipc_object(scomp, buffer, SOF_BUFFER_TOKENS, swidget->tuples, 580 swidget->num_tuples, sizeof(*buffer), 1); 581 if (ret < 0) { 582 kfree(swidget->private); 583 swidget->private = NULL; 584 return ret; 585 } 586 587 dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n", 588 swidget->widget->name, buffer->size, buffer->caps); 589 590 return 0; 591 } 592 593 static int sof_ipc3_widget_setup_comp_src(struct snd_sof_widget *swidget) 594 { 595 struct snd_soc_component *scomp = swidget->scomp; 596 struct sof_ipc_comp_src *src; 597 size_t ipc_size = sizeof(*src); 598 int ret; 599 600 src = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 601 if (!src) 602 return -ENOMEM; 603 604 swidget->private = src; 605 606 /* configure src IPC message */ 607 src->comp.type = SOF_COMP_SRC; 608 src->config.hdr.size = sizeof(src->config); 609 610 /* parse one set of src tokens */ 611 ret = sof_update_ipc_object(scomp, src, SOF_SRC_TOKENS, swidget->tuples, 612 swidget->num_tuples, sizeof(*src), 1); 613 if (ret < 0) 614 goto err; 615 616 /* parse one set of comp tokens */ 617 ret = sof_update_ipc_object(scomp, &src->config, SOF_COMP_TOKENS, 618 swidget->tuples, swidget->num_tuples, sizeof(src->config), 1); 619 if (ret < 0) 620 goto err; 621 622 dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n", 623 swidget->widget->name, src->source_rate, src->sink_rate); 624 sof_dbg_comp_config(scomp, &src->config); 625 626 return 0; 627 err: 628 kfree(swidget->private); 629 swidget->private = NULL; 630 631 return ret; 632 } 633 634 static int sof_ipc3_widget_setup_comp_asrc(struct snd_sof_widget *swidget) 635 { 636 struct snd_soc_component *scomp = swidget->scomp; 637 struct sof_ipc_comp_asrc *asrc; 638 size_t ipc_size = sizeof(*asrc); 639 int ret; 640 641 asrc = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 642 if (!asrc) 643 return -ENOMEM; 644 645 swidget->private = asrc; 646 647 /* configure ASRC IPC message */ 648 asrc->comp.type = SOF_COMP_ASRC; 649 asrc->config.hdr.size = sizeof(asrc->config); 650 651 /* parse one set of asrc tokens */ 652 ret = sof_update_ipc_object(scomp, asrc, SOF_ASRC_TOKENS, swidget->tuples, 653 swidget->num_tuples, sizeof(*asrc), 1); 654 if (ret < 0) 655 goto err; 656 657 /* parse one set of comp tokens */ 658 ret = sof_update_ipc_object(scomp, &asrc->config, SOF_COMP_TOKENS, 659 swidget->tuples, swidget->num_tuples, sizeof(asrc->config), 1); 660 if (ret < 0) 661 goto err; 662 663 dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d asynch %d operation %d\n", 664 swidget->widget->name, asrc->source_rate, asrc->sink_rate, 665 asrc->asynchronous_mode, asrc->operation_mode); 666 667 sof_dbg_comp_config(scomp, &asrc->config); 668 669 return 0; 670 err: 671 kfree(swidget->private); 672 swidget->private = NULL; 673 674 return ret; 675 } 676 677 /* 678 * Mux topology 679 */ 680 static int sof_ipc3_widget_setup_comp_mux(struct snd_sof_widget *swidget) 681 { 682 struct snd_soc_component *scomp = swidget->scomp; 683 struct sof_ipc_comp_mux *mux; 684 size_t ipc_size = sizeof(*mux); 685 int ret; 686 687 mux = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 688 if (!mux) 689 return -ENOMEM; 690 691 swidget->private = mux; 692 693 /* configure mux IPC message */ 694 mux->comp.type = SOF_COMP_MUX; 695 mux->config.hdr.size = sizeof(mux->config); 696 697 /* parse one set of comp tokens */ 698 ret = sof_update_ipc_object(scomp, &mux->config, SOF_COMP_TOKENS, 699 swidget->tuples, swidget->num_tuples, sizeof(mux->config), 1); 700 if (ret < 0) { 701 kfree(swidget->private); 702 swidget->private = NULL; 703 return ret; 704 } 705 706 dev_dbg(scomp->dev, "loaded mux %s\n", swidget->widget->name); 707 sof_dbg_comp_config(scomp, &mux->config); 708 709 return 0; 710 } 711 712 /* 713 * PGA Topology 714 */ 715 716 static int sof_ipc3_widget_setup_comp_pga(struct snd_sof_widget *swidget) 717 { 718 struct snd_soc_component *scomp = swidget->scomp; 719 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 720 struct sof_ipc_comp_volume *volume; 721 struct snd_sof_control *scontrol; 722 size_t ipc_size = sizeof(*volume); 723 int min_step, max_step; 724 int ret; 725 726 volume = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 727 if (!volume) 728 return -ENOMEM; 729 730 swidget->private = volume; 731 732 /* configure volume IPC message */ 733 volume->comp.type = SOF_COMP_VOLUME; 734 volume->config.hdr.size = sizeof(volume->config); 735 736 /* parse one set of volume tokens */ 737 ret = sof_update_ipc_object(scomp, volume, SOF_VOLUME_TOKENS, swidget->tuples, 738 swidget->num_tuples, sizeof(*volume), 1); 739 if (ret < 0) 740 goto err; 741 742 /* parse one set of comp tokens */ 743 ret = sof_update_ipc_object(scomp, &volume->config, SOF_COMP_TOKENS, 744 swidget->tuples, swidget->num_tuples, 745 sizeof(volume->config), 1); 746 if (ret < 0) 747 goto err; 748 749 dev_dbg(scomp->dev, "loaded PGA %s\n", swidget->widget->name); 750 sof_dbg_comp_config(scomp, &volume->config); 751 752 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 753 if (scontrol->comp_id == swidget->comp_id && 754 scontrol->volume_table) { 755 min_step = scontrol->min_volume_step; 756 max_step = scontrol->max_volume_step; 757 volume->min_value = scontrol->volume_table[min_step]; 758 volume->max_value = scontrol->volume_table[max_step]; 759 volume->channels = scontrol->num_channels; 760 break; 761 } 762 } 763 764 return 0; 765 err: 766 kfree(swidget->private); 767 swidget->private = NULL; 768 769 return ret; 770 } 771 772 static int sof_get_control_data(struct snd_soc_component *scomp, 773 struct snd_soc_dapm_widget *widget, 774 struct sof_widget_data *wdata, size_t *size) 775 { 776 const struct snd_kcontrol_new *kc; 777 struct sof_ipc_ctrl_data *cdata; 778 struct soc_mixer_control *sm; 779 struct soc_bytes_ext *sbe; 780 struct soc_enum *se; 781 int i; 782 783 *size = 0; 784 785 for (i = 0; i < widget->num_kcontrols; i++) { 786 kc = &widget->kcontrol_news[i]; 787 788 switch (widget->dobj.widget.kcontrol_type[i]) { 789 case SND_SOC_TPLG_TYPE_MIXER: 790 sm = (struct soc_mixer_control *)kc->private_value; 791 wdata[i].control = sm->dobj.private; 792 break; 793 case SND_SOC_TPLG_TYPE_BYTES: 794 sbe = (struct soc_bytes_ext *)kc->private_value; 795 wdata[i].control = sbe->dobj.private; 796 break; 797 case SND_SOC_TPLG_TYPE_ENUM: 798 se = (struct soc_enum *)kc->private_value; 799 wdata[i].control = se->dobj.private; 800 break; 801 default: 802 dev_err(scomp->dev, "Unknown kcontrol type %u in widget %s\n", 803 widget->dobj.widget.kcontrol_type[i], widget->name); 804 return -EINVAL; 805 } 806 807 if (!wdata[i].control) { 808 dev_err(scomp->dev, "No scontrol for widget %s\n", widget->name); 809 return -EINVAL; 810 } 811 812 cdata = wdata[i].control->ipc_control_data; 813 814 if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) { 815 /* make sure data is valid - data can be updated at runtime */ 816 if (cdata->data->magic != SOF_ABI_MAGIC) 817 return -EINVAL; 818 819 wdata[i].pdata = cdata->data->data; 820 wdata[i].pdata_size = cdata->data->size; 821 } else { 822 /* points to the control data union */ 823 wdata[i].pdata = cdata->chanv; 824 /* 825 * wdata[i].control->size is calculated with struct_size 826 * and includes the size of struct sof_ipc_ctrl_data 827 */ 828 wdata[i].pdata_size = wdata[i].control->size - 829 sizeof(struct sof_ipc_ctrl_data); 830 } 831 832 *size += wdata[i].pdata_size; 833 834 /* get data type */ 835 switch (cdata->cmd) { 836 case SOF_CTRL_CMD_VOLUME: 837 case SOF_CTRL_CMD_ENUM: 838 case SOF_CTRL_CMD_SWITCH: 839 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE; 840 wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET; 841 break; 842 case SOF_CTRL_CMD_BINARY: 843 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA; 844 wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET; 845 break; 846 default: 847 break; 848 } 849 } 850 851 return 0; 852 } 853 854 static int sof_process_load(struct snd_soc_component *scomp, 855 struct snd_sof_widget *swidget, int type) 856 { 857 struct snd_soc_dapm_widget *widget = swidget->widget; 858 struct sof_ipc_comp_process *process; 859 struct sof_widget_data *wdata = NULL; 860 size_t ipc_data_size = 0; 861 size_t ipc_size; 862 int offset = 0; 863 int ret; 864 int i; 865 866 /* allocate struct for widget control data sizes and types */ 867 if (widget->num_kcontrols) { 868 wdata = kcalloc(widget->num_kcontrols, sizeof(*wdata), GFP_KERNEL); 869 if (!wdata) 870 return -ENOMEM; 871 872 /* get possible component controls and get size of all pdata */ 873 ret = sof_get_control_data(scomp, widget, wdata, &ipc_data_size); 874 if (ret < 0) 875 goto out; 876 } 877 878 ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size; 879 880 /* we are exceeding max ipc size, config needs to be sent separately */ 881 if (ipc_size > SOF_IPC_MSG_MAX_SIZE) { 882 ipc_size -= ipc_data_size; 883 ipc_data_size = 0; 884 } 885 886 process = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 887 if (!process) { 888 ret = -ENOMEM; 889 goto out; 890 } 891 892 swidget->private = process; 893 894 /* configure iir IPC message */ 895 process->comp.type = type; 896 process->config.hdr.size = sizeof(process->config); 897 898 /* parse one set of comp tokens */ 899 ret = sof_update_ipc_object(scomp, &process->config, SOF_COMP_TOKENS, 900 swidget->tuples, swidget->num_tuples, 901 sizeof(process->config), 1); 902 if (ret < 0) 903 goto err; 904 905 dev_dbg(scomp->dev, "loaded process %s\n", swidget->widget->name); 906 sof_dbg_comp_config(scomp, &process->config); 907 908 /* 909 * found private data in control, so copy it. 910 * get possible component controls - get size of all pdata, 911 * then memcpy with headers 912 */ 913 if (ipc_data_size) { 914 for (i = 0; i < widget->num_kcontrols; i++) { 915 if (!wdata[i].pdata_size) 916 continue; 917 918 memcpy(&process->data[offset], wdata[i].pdata, 919 wdata[i].pdata_size); 920 offset += wdata[i].pdata_size; 921 } 922 } 923 924 process->size = ipc_data_size; 925 926 kfree(wdata); 927 928 return 0; 929 err: 930 kfree(swidget->private); 931 swidget->private = NULL; 932 out: 933 kfree(wdata); 934 return ret; 935 } 936 937 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type) 938 { 939 int i; 940 941 for (i = 0; i < ARRAY_SIZE(sof_process); i++) { 942 if (sof_process[i].type == type) 943 return sof_process[i].comp_type; 944 } 945 946 return SOF_COMP_NONE; 947 } 948 949 /* 950 * Processing Component Topology - can be "effect", "codec", or general 951 * "processing". 952 */ 953 954 static int sof_widget_update_ipc_comp_process(struct snd_sof_widget *swidget) 955 { 956 struct snd_soc_component *scomp = swidget->scomp; 957 struct sof_ipc_comp_process config; 958 int ret; 959 960 memset(&config, 0, sizeof(config)); 961 config.comp.core = swidget->core; 962 963 /* parse one set of process tokens */ 964 ret = sof_update_ipc_object(scomp, &config, SOF_PROCESS_TOKENS, swidget->tuples, 965 swidget->num_tuples, sizeof(config), 1); 966 if (ret < 0) 967 return ret; 968 969 /* now load process specific data and send IPC */ 970 return sof_process_load(scomp, swidget, find_process_comp_type(config.type)); 971 } 972 973 static int sof_link_hda_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 974 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 975 { 976 struct sof_dai_private_data *private = dai->private; 977 u32 size = sizeof(*config); 978 int ret; 979 980 /* init IPC */ 981 memset(&config->hda, 0, sizeof(config->hda)); 982 config->hdr.size = size; 983 984 /* parse one set of HDA tokens */ 985 ret = sof_update_ipc_object(scomp, &config->hda, SOF_HDA_TOKENS, slink->tuples, 986 slink->num_tuples, size, 1); 987 if (ret < 0) 988 return ret; 989 990 dev_dbg(scomp->dev, "HDA config rate %d channels %d\n", 991 config->hda.rate, config->hda.channels); 992 993 config->hda.link_dma_ch = DMA_CHAN_INVALID; 994 995 dai->number_configs = 1; 996 dai->current_config = 0; 997 private->dai_config = kmemdup(config, size, GFP_KERNEL); 998 if (!private->dai_config) 999 return -ENOMEM; 1000 1001 return 0; 1002 } 1003 1004 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config, 1005 struct sof_ipc_dai_config *config) 1006 { 1007 /* clock directions wrt codec */ 1008 config->format &= ~SOF_DAI_FMT_CLOCK_PROVIDER_MASK; 1009 if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) { 1010 /* codec is bclk provider */ 1011 if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP) 1012 config->format |= SOF_DAI_FMT_CBP_CFP; 1013 else 1014 config->format |= SOF_DAI_FMT_CBP_CFC; 1015 } else { 1016 /* codec is bclk consumer */ 1017 if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP) 1018 config->format |= SOF_DAI_FMT_CBC_CFP; 1019 else 1020 config->format |= SOF_DAI_FMT_CBC_CFC; 1021 } 1022 1023 /* inverted clocks ? */ 1024 config->format &= ~SOF_DAI_FMT_INV_MASK; 1025 if (hw_config->invert_bclk) { 1026 if (hw_config->invert_fsync) 1027 config->format |= SOF_DAI_FMT_IB_IF; 1028 else 1029 config->format |= SOF_DAI_FMT_IB_NF; 1030 } else { 1031 if (hw_config->invert_fsync) 1032 config->format |= SOF_DAI_FMT_NB_IF; 1033 else 1034 config->format |= SOF_DAI_FMT_NB_NF; 1035 } 1036 } 1037 1038 static int sof_link_sai_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1039 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1040 { 1041 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1042 struct sof_dai_private_data *private = dai->private; 1043 u32 size = sizeof(*config); 1044 int ret; 1045 1046 /* handle master/slave and inverted clocks */ 1047 sof_dai_set_format(hw_config, config); 1048 1049 /* init IPC */ 1050 memset(&config->sai, 0, sizeof(config->sai)); 1051 config->hdr.size = size; 1052 1053 /* parse one set of SAI tokens */ 1054 ret = sof_update_ipc_object(scomp, &config->sai, SOF_SAI_TOKENS, slink->tuples, 1055 slink->num_tuples, size, 1); 1056 if (ret < 0) 1057 return ret; 1058 1059 config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate); 1060 config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate); 1061 config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate); 1062 config->sai.mclk_direction = hw_config->mclk_direction; 1063 1064 config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots); 1065 config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width); 1066 config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots); 1067 config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots); 1068 1069 dev_info(scomp->dev, 1070 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n", 1071 config->dai_index, config->format, 1072 config->sai.mclk_rate, config->sai.tdm_slot_width, 1073 config->sai.tdm_slots, config->sai.mclk_id); 1074 1075 if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) { 1076 dev_err(scomp->dev, "Invalid channel count for SAI%d\n", config->dai_index); 1077 return -EINVAL; 1078 } 1079 1080 dai->number_configs = 1; 1081 dai->current_config = 0; 1082 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1083 if (!private->dai_config) 1084 return -ENOMEM; 1085 1086 return 0; 1087 } 1088 1089 static int sof_link_esai_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1090 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1091 { 1092 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1093 struct sof_dai_private_data *private = dai->private; 1094 u32 size = sizeof(*config); 1095 int ret; 1096 1097 /* handle master/slave and inverted clocks */ 1098 sof_dai_set_format(hw_config, config); 1099 1100 /* init IPC */ 1101 memset(&config->esai, 0, sizeof(config->esai)); 1102 config->hdr.size = size; 1103 1104 /* parse one set of ESAI tokens */ 1105 ret = sof_update_ipc_object(scomp, &config->esai, SOF_ESAI_TOKENS, slink->tuples, 1106 slink->num_tuples, size, 1); 1107 if (ret < 0) 1108 return ret; 1109 1110 config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate); 1111 config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate); 1112 config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate); 1113 config->esai.mclk_direction = hw_config->mclk_direction; 1114 config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots); 1115 config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width); 1116 config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots); 1117 config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots); 1118 1119 dev_info(scomp->dev, 1120 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n", 1121 config->dai_index, config->format, 1122 config->esai.mclk_rate, config->esai.tdm_slot_width, 1123 config->esai.tdm_slots, config->esai.mclk_id); 1124 1125 if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) { 1126 dev_err(scomp->dev, "Invalid channel count for ESAI%d\n", config->dai_index); 1127 return -EINVAL; 1128 } 1129 1130 dai->number_configs = 1; 1131 dai->current_config = 0; 1132 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1133 if (!private->dai_config) 1134 return -ENOMEM; 1135 1136 return 0; 1137 } 1138 1139 static int sof_link_acp_dmic_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1140 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1141 { 1142 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1143 struct sof_dai_private_data *private = dai->private; 1144 u32 size = sizeof(*config); 1145 int ret; 1146 1147 /* handle master/slave and inverted clocks */ 1148 sof_dai_set_format(hw_config, config); 1149 1150 config->hdr.size = size; 1151 1152 /* parse the required set of ACPDMIC tokens based on num_hw_cfgs */ 1153 ret = sof_update_ipc_object(scomp, &config->acpdmic, SOF_ACPDMIC_TOKENS, slink->tuples, 1154 slink->num_tuples, size, slink->num_hw_configs); 1155 if (ret < 0) 1156 return ret; 1157 1158 dev_info(scomp->dev, "ACP_DMIC config ACP%d channel %d rate %d\n", 1159 config->dai_index, config->acpdmic.pdm_ch, 1160 config->acpdmic.pdm_rate); 1161 1162 dai->number_configs = 1; 1163 dai->current_config = 0; 1164 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1165 if (!private->dai_config) 1166 return -ENOMEM; 1167 1168 return 0; 1169 } 1170 1171 static int sof_link_acp_bt_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1172 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1173 { 1174 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1175 struct sof_dai_private_data *private = dai->private; 1176 u32 size = sizeof(*config); 1177 1178 /* handle master/slave and inverted clocks */ 1179 sof_dai_set_format(hw_config, config); 1180 1181 /* init IPC */ 1182 memset(&config->acpbt, 0, sizeof(config->acpbt)); 1183 config->hdr.size = size; 1184 1185 config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate); 1186 config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots); 1187 1188 dev_info(scomp->dev, "ACP_BT config ACP%d channel %d rate %d\n", 1189 config->dai_index, config->acpbt.tdm_slots, 1190 config->acpbt.fsync_rate); 1191 1192 dai->number_configs = 1; 1193 dai->current_config = 0; 1194 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1195 if (!private->dai_config) 1196 return -ENOMEM; 1197 1198 return 0; 1199 } 1200 1201 static int sof_link_acp_sp_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1202 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1203 { 1204 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1205 struct sof_dai_private_data *private = dai->private; 1206 u32 size = sizeof(*config); 1207 int ret; 1208 1209 /* handle master/slave and inverted clocks */ 1210 sof_dai_set_format(hw_config, config); 1211 1212 /* init IPC */ 1213 memset(&config->acpsp, 0, sizeof(config->acpsp)); 1214 config->hdr.size = size; 1215 1216 ret = sof_update_ipc_object(scomp, &config->acpsp, SOF_ACPI2S_TOKENS, slink->tuples, 1217 slink->num_tuples, size, slink->num_hw_configs); 1218 if (ret < 0) 1219 return ret; 1220 1221 1222 dev_info(scomp->dev, "ACP_SP config ACP%d channel %d rate %d tdm_mode %d\n", 1223 config->dai_index, config->acpsp.tdm_slots, 1224 config->acpsp.fsync_rate, config->acpsp.tdm_mode); 1225 1226 dai->number_configs = 1; 1227 dai->current_config = 0; 1228 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1229 if (!private->dai_config) 1230 return -ENOMEM; 1231 1232 return 0; 1233 } 1234 1235 static int sof_link_acp_hs_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1236 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1237 { 1238 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1239 struct sof_dai_private_data *private = dai->private; 1240 u32 size = sizeof(*config); 1241 int ret; 1242 1243 /* Configures the DAI hardware format and inverted clocks */ 1244 sof_dai_set_format(hw_config, config); 1245 1246 /* init IPC */ 1247 memset(&config->acphs, 0, sizeof(config->acphs)); 1248 config->hdr.size = size; 1249 1250 ret = sof_update_ipc_object(scomp, &config->acphs, SOF_ACPI2S_TOKENS, slink->tuples, 1251 slink->num_tuples, size, slink->num_hw_configs); 1252 if (ret < 0) 1253 return ret; 1254 1255 dev_info(scomp->dev, "ACP_HS config ACP%d channel %d rate %d tdm_mode %d\n", 1256 config->dai_index, config->acphs.tdm_slots, 1257 config->acphs.fsync_rate, config->acphs.tdm_mode); 1258 1259 dai->number_configs = 1; 1260 dai->current_config = 0; 1261 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1262 if (!private->dai_config) 1263 return -ENOMEM; 1264 1265 return 0; 1266 } 1267 1268 static int sof_link_afe_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1269 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1270 { 1271 struct sof_dai_private_data *private = dai->private; 1272 u32 size = sizeof(*config); 1273 int ret; 1274 1275 config->hdr.size = size; 1276 1277 /* parse the required set of AFE tokens based on num_hw_cfgs */ 1278 ret = sof_update_ipc_object(scomp, &config->afe, SOF_AFE_TOKENS, slink->tuples, 1279 slink->num_tuples, size, slink->num_hw_configs); 1280 if (ret < 0) 1281 return ret; 1282 1283 dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n", 1284 config->afe.rate, config->afe.channels, config->afe.format); 1285 1286 config->afe.stream_id = DMA_CHAN_INVALID; 1287 1288 dai->number_configs = 1; 1289 dai->current_config = 0; 1290 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1291 if (!private->dai_config) 1292 return -ENOMEM; 1293 1294 return 0; 1295 } 1296 1297 static int sof_link_ssp_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1298 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1299 { 1300 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1301 struct snd_soc_tplg_hw_config *hw_config = slink->hw_configs; 1302 struct sof_dai_private_data *private = dai->private; 1303 u32 size = sizeof(*config); 1304 int current_config = 0; 1305 int i, ret; 1306 1307 /* 1308 * Parse common data, we should have 1 common data per hw_config. 1309 */ 1310 ret = sof_update_ipc_object(scomp, &config->ssp, SOF_SSP_TOKENS, slink->tuples, 1311 slink->num_tuples, size, slink->num_hw_configs); 1312 if (ret < 0) 1313 return ret; 1314 1315 /* process all possible hw configs */ 1316 for (i = 0; i < slink->num_hw_configs; i++) { 1317 if (le32_to_cpu(hw_config[i].id) == slink->default_hw_cfg_id) 1318 current_config = i; 1319 1320 /* handle master/slave and inverted clocks */ 1321 sof_dai_set_format(&hw_config[i], &config[i]); 1322 1323 config[i].hdr.size = size; 1324 1325 if (sdev->mclk_id_override) { 1326 dev_dbg(scomp->dev, "tplg: overriding topology mclk_id %d by quirk %d\n", 1327 config[i].ssp.mclk_id, sdev->mclk_id_quirk); 1328 config[i].ssp.mclk_id = sdev->mclk_id_quirk; 1329 } 1330 1331 /* copy differentiating hw configs to ipc structs */ 1332 config[i].ssp.mclk_rate = le32_to_cpu(hw_config[i].mclk_rate); 1333 config[i].ssp.bclk_rate = le32_to_cpu(hw_config[i].bclk_rate); 1334 config[i].ssp.fsync_rate = le32_to_cpu(hw_config[i].fsync_rate); 1335 config[i].ssp.tdm_slots = le32_to_cpu(hw_config[i].tdm_slots); 1336 config[i].ssp.tdm_slot_width = le32_to_cpu(hw_config[i].tdm_slot_width); 1337 config[i].ssp.mclk_direction = hw_config[i].mclk_direction; 1338 config[i].ssp.rx_slots = le32_to_cpu(hw_config[i].rx_slots); 1339 config[i].ssp.tx_slots = le32_to_cpu(hw_config[i].tx_slots); 1340 1341 dev_dbg(scomp->dev, "tplg: config SSP%d fmt %#x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d clks_control %#x\n", 1342 config[i].dai_index, config[i].format, 1343 config[i].ssp.mclk_rate, config[i].ssp.bclk_rate, 1344 config[i].ssp.fsync_rate, config[i].ssp.sample_valid_bits, 1345 config[i].ssp.tdm_slot_width, config[i].ssp.tdm_slots, 1346 config[i].ssp.mclk_id, config[i].ssp.quirks, config[i].ssp.clks_control); 1347 1348 /* validate SSP fsync rate and channel count */ 1349 if (config[i].ssp.fsync_rate < 8000 || config[i].ssp.fsync_rate > 192000) { 1350 dev_err(scomp->dev, "Invalid fsync rate for SSP%d\n", config[i].dai_index); 1351 return -EINVAL; 1352 } 1353 1354 if (config[i].ssp.tdm_slots < 1 || config[i].ssp.tdm_slots > 8) { 1355 dev_err(scomp->dev, "Invalid channel count for SSP%d\n", 1356 config[i].dai_index); 1357 return -EINVAL; 1358 } 1359 } 1360 1361 dai->number_configs = slink->num_hw_configs; 1362 dai->current_config = current_config; 1363 private->dai_config = kmemdup(config, size * slink->num_hw_configs, GFP_KERNEL); 1364 if (!private->dai_config) 1365 return -ENOMEM; 1366 1367 return 0; 1368 } 1369 1370 static int sof_link_dmic_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1371 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1372 { 1373 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1374 struct sof_dai_private_data *private = dai->private; 1375 struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 1376 struct sof_ipc_fw_version *v = &ready->version; 1377 size_t size = sizeof(*config); 1378 int i, ret; 1379 1380 /* Ensure the entire DMIC config struct is zeros */ 1381 memset(&config->dmic, 0, sizeof(config->dmic)); 1382 1383 /* parse the required set of DMIC tokens based on num_hw_cfgs */ 1384 ret = sof_update_ipc_object(scomp, &config->dmic, SOF_DMIC_TOKENS, slink->tuples, 1385 slink->num_tuples, size, slink->num_hw_configs); 1386 if (ret < 0) 1387 return ret; 1388 1389 /* parse the required set of DMIC PDM tokens based on number of active PDM's */ 1390 ret = sof_update_ipc_object(scomp, &config->dmic.pdm[0], SOF_DMIC_PDM_TOKENS, 1391 slink->tuples, slink->num_tuples, 1392 sizeof(struct sof_ipc_dai_dmic_pdm_ctrl), 1393 config->dmic.num_pdm_active); 1394 if (ret < 0) 1395 return ret; 1396 1397 /* set IPC header size */ 1398 config->hdr.size = size; 1399 1400 /* debug messages */ 1401 dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n", 1402 config->dai_index, config->dmic.driver_ipc_version); 1403 dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %d\n", 1404 config->dmic.pdmclk_min, config->dmic.pdmclk_max, 1405 config->dmic.duty_min); 1406 dev_dbg(scomp->dev, "duty_max %d fifo_fs %d num_pdms active %d\n", 1407 config->dmic.duty_max, config->dmic.fifo_fs, 1408 config->dmic.num_pdm_active); 1409 dev_dbg(scomp->dev, "fifo word length %d\n", config->dmic.fifo_bits); 1410 1411 for (i = 0; i < config->dmic.num_pdm_active; i++) { 1412 dev_dbg(scomp->dev, "pdm %d mic a %d mic b %d\n", 1413 config->dmic.pdm[i].id, 1414 config->dmic.pdm[i].enable_mic_a, 1415 config->dmic.pdm[i].enable_mic_b); 1416 dev_dbg(scomp->dev, "pdm %d polarity a %d polarity b %d\n", 1417 config->dmic.pdm[i].id, 1418 config->dmic.pdm[i].polarity_mic_a, 1419 config->dmic.pdm[i].polarity_mic_b); 1420 dev_dbg(scomp->dev, "pdm %d clk_edge %d skew %d\n", 1421 config->dmic.pdm[i].id, 1422 config->dmic.pdm[i].clk_edge, 1423 config->dmic.pdm[i].skew); 1424 } 1425 1426 /* 1427 * this takes care of backwards compatible handling of fifo_bits_b. 1428 * It is deprecated since firmware ABI version 3.0.1. 1429 */ 1430 if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1)) 1431 config->dmic.fifo_bits_b = config->dmic.fifo_bits; 1432 1433 dai->number_configs = 1; 1434 dai->current_config = 0; 1435 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1436 if (!private->dai_config) 1437 return -ENOMEM; 1438 1439 return 0; 1440 } 1441 1442 static int sof_link_alh_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink, 1443 struct sof_ipc_dai_config *config, struct snd_sof_dai *dai) 1444 { 1445 struct sof_dai_private_data *private = dai->private; 1446 u32 size = sizeof(*config); 1447 int ret; 1448 1449 /* parse the required set of ALH tokens based on num_hw_cfgs */ 1450 ret = sof_update_ipc_object(scomp, &config->alh, SOF_ALH_TOKENS, slink->tuples, 1451 slink->num_tuples, size, slink->num_hw_configs); 1452 if (ret < 0) 1453 return ret; 1454 1455 /* init IPC */ 1456 config->hdr.size = size; 1457 1458 /* set config for all DAI's with name matching the link name */ 1459 dai->number_configs = 1; 1460 dai->current_config = 0; 1461 private->dai_config = kmemdup(config, size, GFP_KERNEL); 1462 if (!private->dai_config) 1463 return -ENOMEM; 1464 1465 return 0; 1466 } 1467 1468 static int sof_ipc3_widget_setup_comp_dai(struct snd_sof_widget *swidget) 1469 { 1470 struct snd_soc_component *scomp = swidget->scomp; 1471 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1472 struct snd_sof_dai *dai = swidget->private; 1473 struct sof_dai_private_data *private; 1474 struct sof_ipc_comp_dai *comp_dai; 1475 size_t ipc_size = sizeof(*comp_dai); 1476 struct sof_ipc_dai_config *config; 1477 struct snd_sof_dai_link *slink; 1478 int ret; 1479 1480 private = kzalloc(sizeof(*private), GFP_KERNEL); 1481 if (!private) 1482 return -ENOMEM; 1483 1484 dai->private = private; 1485 1486 private->comp_dai = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id); 1487 if (!private->comp_dai) { 1488 ret = -ENOMEM; 1489 goto free; 1490 } 1491 1492 /* configure dai IPC message */ 1493 comp_dai = private->comp_dai; 1494 comp_dai->comp.type = SOF_COMP_DAI; 1495 comp_dai->config.hdr.size = sizeof(comp_dai->config); 1496 1497 /* parse one set of DAI tokens */ 1498 ret = sof_update_ipc_object(scomp, comp_dai, SOF_DAI_TOKENS, swidget->tuples, 1499 swidget->num_tuples, sizeof(*comp_dai), 1); 1500 if (ret < 0) 1501 goto free; 1502 1503 /* update comp_tokens */ 1504 ret = sof_update_ipc_object(scomp, &comp_dai->config, SOF_COMP_TOKENS, 1505 swidget->tuples, swidget->num_tuples, 1506 sizeof(comp_dai->config), 1); 1507 if (ret < 0) 1508 goto free; 1509 1510 dev_dbg(scomp->dev, "dai %s: type %d index %d\n", 1511 swidget->widget->name, comp_dai->type, comp_dai->dai_index); 1512 sof_dbg_comp_config(scomp, &comp_dai->config); 1513 1514 /* now update DAI config */ 1515 list_for_each_entry(slink, &sdev->dai_link_list, list) { 1516 struct sof_ipc_dai_config common_config; 1517 int i; 1518 1519 if (strcmp(slink->link->name, dai->name)) 1520 continue; 1521 1522 /* Reserve memory for all hw configs, eventually freed by widget */ 1523 config = kcalloc(slink->num_hw_configs, sizeof(*config), GFP_KERNEL); 1524 if (!config) { 1525 ret = -ENOMEM; 1526 goto free_comp; 1527 } 1528 1529 /* parse one set of DAI link tokens */ 1530 ret = sof_update_ipc_object(scomp, &common_config, SOF_DAI_LINK_TOKENS, 1531 slink->tuples, slink->num_tuples, 1532 sizeof(common_config), 1); 1533 if (ret < 0) 1534 goto free_config; 1535 1536 for (i = 0; i < slink->num_hw_configs; i++) { 1537 config[i].hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG; 1538 config[i].format = le32_to_cpu(slink->hw_configs[i].fmt); 1539 config[i].type = common_config.type; 1540 config[i].dai_index = comp_dai->dai_index; 1541 } 1542 1543 switch (common_config.type) { 1544 case SOF_DAI_INTEL_SSP: 1545 ret = sof_link_ssp_load(scomp, slink, config, dai); 1546 break; 1547 case SOF_DAI_INTEL_DMIC: 1548 ret = sof_link_dmic_load(scomp, slink, config, dai); 1549 break; 1550 case SOF_DAI_INTEL_HDA: 1551 ret = sof_link_hda_load(scomp, slink, config, dai); 1552 break; 1553 case SOF_DAI_INTEL_ALH: 1554 ret = sof_link_alh_load(scomp, slink, config, dai); 1555 break; 1556 case SOF_DAI_IMX_SAI: 1557 ret = sof_link_sai_load(scomp, slink, config, dai); 1558 break; 1559 case SOF_DAI_IMX_ESAI: 1560 ret = sof_link_esai_load(scomp, slink, config, dai); 1561 break; 1562 case SOF_DAI_AMD_BT: 1563 ret = sof_link_acp_bt_load(scomp, slink, config, dai); 1564 break; 1565 case SOF_DAI_AMD_SP: 1566 case SOF_DAI_AMD_SP_VIRTUAL: 1567 ret = sof_link_acp_sp_load(scomp, slink, config, dai); 1568 break; 1569 case SOF_DAI_AMD_HS: 1570 case SOF_DAI_AMD_HS_VIRTUAL: 1571 ret = sof_link_acp_hs_load(scomp, slink, config, dai); 1572 break; 1573 case SOF_DAI_AMD_DMIC: 1574 ret = sof_link_acp_dmic_load(scomp, slink, config, dai); 1575 break; 1576 case SOF_DAI_MEDIATEK_AFE: 1577 ret = sof_link_afe_load(scomp, slink, config, dai); 1578 break; 1579 default: 1580 break; 1581 } 1582 if (ret < 0) { 1583 dev_err(scomp->dev, "failed to load config for dai %s\n", dai->name); 1584 goto free_config; 1585 } 1586 1587 kfree(config); 1588 } 1589 1590 return 0; 1591 free_config: 1592 kfree(config); 1593 free_comp: 1594 kfree(comp_dai); 1595 free: 1596 kfree(private); 1597 dai->private = NULL; 1598 return ret; 1599 } 1600 1601 static void sof_ipc3_widget_free_comp_dai(struct snd_sof_widget *swidget) 1602 { 1603 switch (swidget->id) { 1604 case snd_soc_dapm_dai_in: 1605 case snd_soc_dapm_dai_out: 1606 { 1607 struct snd_sof_dai *dai = swidget->private; 1608 struct sof_dai_private_data *dai_data; 1609 1610 if (!dai) 1611 return; 1612 1613 dai_data = dai->private; 1614 if (dai_data) { 1615 kfree(dai_data->comp_dai); 1616 kfree(dai_data->dai_config); 1617 kfree(dai_data); 1618 } 1619 kfree(dai); 1620 break; 1621 } 1622 default: 1623 break; 1624 } 1625 } 1626 1627 static int sof_ipc3_route_setup(struct snd_sof_dev *sdev, struct snd_sof_route *sroute) 1628 { 1629 struct sof_ipc_pipe_comp_connect connect; 1630 struct sof_ipc_reply reply; 1631 int ret; 1632 1633 connect.hdr.size = sizeof(connect); 1634 connect.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT; 1635 connect.source_id = sroute->src_widget->comp_id; 1636 connect.sink_id = sroute->sink_widget->comp_id; 1637 1638 dev_dbg(sdev->dev, "setting up route %s -> %s\n", 1639 sroute->src_widget->widget->name, 1640 sroute->sink_widget->widget->name); 1641 1642 /* send ipc */ 1643 ret = sof_ipc_tx_message(sdev->ipc, &connect, sizeof(connect), &reply, sizeof(reply)); 1644 if (ret < 0) 1645 dev_err(sdev->dev, "%s: route %s -> %s failed\n", __func__, 1646 sroute->src_widget->widget->name, sroute->sink_widget->widget->name); 1647 1648 return ret; 1649 } 1650 1651 static int sof_ipc3_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 1652 { 1653 struct sof_ipc_ctrl_data *cdata; 1654 int ret; 1655 1656 if (scontrol->max_size < (sizeof(*cdata) + sizeof(struct sof_abi_hdr))) { 1657 dev_err(sdev->dev, "%s: insufficient size for a bytes control: %zu.\n", 1658 __func__, scontrol->max_size); 1659 return -EINVAL; 1660 } 1661 1662 if (scontrol->priv_size > scontrol->max_size - sizeof(*cdata)) { 1663 dev_err(sdev->dev, 1664 "%s: bytes data size %zu exceeds max %zu.\n", __func__, 1665 scontrol->priv_size, scontrol->max_size - sizeof(*cdata)); 1666 return -EINVAL; 1667 } 1668 1669 scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL); 1670 if (!scontrol->ipc_control_data) 1671 return -ENOMEM; 1672 1673 scontrol->size = sizeof(struct sof_ipc_ctrl_data) + scontrol->priv_size; 1674 1675 cdata = scontrol->ipc_control_data; 1676 cdata->cmd = SOF_CTRL_CMD_BINARY; 1677 cdata->index = scontrol->index; 1678 1679 if (scontrol->priv_size > 0) { 1680 memcpy(cdata->data, scontrol->priv, scontrol->priv_size); 1681 kfree(scontrol->priv); 1682 scontrol->priv = NULL; 1683 1684 if (cdata->data->magic != SOF_ABI_MAGIC) { 1685 dev_err(sdev->dev, "Wrong ABI magic 0x%08x.\n", cdata->data->magic); 1686 ret = -EINVAL; 1687 goto err; 1688 } 1689 1690 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) { 1691 dev_err(sdev->dev, "Incompatible ABI version 0x%08x.\n", 1692 cdata->data->abi); 1693 ret = -EINVAL; 1694 goto err; 1695 } 1696 1697 if (cdata->data->size + sizeof(struct sof_abi_hdr) != scontrol->priv_size) { 1698 dev_err(sdev->dev, "Conflict in bytes vs. priv size.\n"); 1699 ret = -EINVAL; 1700 goto err; 1701 } 1702 } 1703 1704 return 0; 1705 err: 1706 kfree(scontrol->ipc_control_data); 1707 scontrol->ipc_control_data = NULL; 1708 return ret; 1709 } 1710 1711 static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 1712 { 1713 struct sof_ipc_ctrl_data *cdata; 1714 int i; 1715 1716 /* init the volume get/put data */ 1717 scontrol->size = struct_size(cdata, chanv, scontrol->num_channels); 1718 1719 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL); 1720 if (!scontrol->ipc_control_data) 1721 return -ENOMEM; 1722 1723 cdata = scontrol->ipc_control_data; 1724 cdata->index = scontrol->index; 1725 1726 /* set cmd for mixer control */ 1727 if (scontrol->max == 1) { 1728 cdata->cmd = SOF_CTRL_CMD_SWITCH; 1729 return 0; 1730 } 1731 1732 cdata->cmd = SOF_CTRL_CMD_VOLUME; 1733 1734 /* set default volume values to 0dB in control */ 1735 for (i = 0; i < scontrol->num_channels; i++) { 1736 cdata->chanv[i].channel = i; 1737 cdata->chanv[i].value = VOL_ZERO_DB; 1738 } 1739 1740 return 0; 1741 } 1742 1743 static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 1744 { 1745 struct sof_ipc_ctrl_data *cdata; 1746 1747 /* init the enum get/put data */ 1748 scontrol->size = struct_size(cdata, chanv, scontrol->num_channels); 1749 1750 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL); 1751 if (!scontrol->ipc_control_data) 1752 return -ENOMEM; 1753 1754 cdata = scontrol->ipc_control_data; 1755 cdata->index = scontrol->index; 1756 cdata->cmd = SOF_CTRL_CMD_ENUM; 1757 1758 return 0; 1759 } 1760 1761 static int sof_ipc3_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 1762 { 1763 switch (scontrol->info_type) { 1764 case SND_SOC_TPLG_CTL_VOLSW: 1765 case SND_SOC_TPLG_CTL_VOLSW_SX: 1766 case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 1767 return sof_ipc3_control_load_volume(sdev, scontrol); 1768 case SND_SOC_TPLG_CTL_BYTES: 1769 return sof_ipc3_control_load_bytes(sdev, scontrol); 1770 case SND_SOC_TPLG_CTL_ENUM: 1771 case SND_SOC_TPLG_CTL_ENUM_VALUE: 1772 return sof_ipc3_control_load_enum(sdev, scontrol); 1773 default: 1774 break; 1775 } 1776 1777 return 0; 1778 } 1779 1780 static int sof_ipc3_control_free(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol) 1781 { 1782 struct sof_ipc_free fcomp; 1783 1784 fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE; 1785 fcomp.hdr.size = sizeof(fcomp); 1786 fcomp.id = scontrol->comp_id; 1787 1788 /* send IPC to the DSP */ 1789 return sof_ipc_tx_message(sdev->ipc, &fcomp, sizeof(fcomp), NULL, 0); 1790 } 1791 1792 /* send pcm params ipc */ 1793 static int sof_ipc3_keyword_detect_pcm_params(struct snd_sof_widget *swidget, int dir) 1794 { 1795 struct snd_soc_component *scomp = swidget->scomp; 1796 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1797 struct sof_ipc_pcm_params_reply ipc_params_reply; 1798 struct snd_pcm_hw_params *params; 1799 struct sof_ipc_pcm_params pcm; 1800 struct snd_sof_pcm *spcm; 1801 int ret; 1802 1803 /* get runtime PCM params using widget's stream name */ 1804 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname); 1805 if (!spcm) { 1806 dev_err(scomp->dev, "Cannot find PCM for %s\n", swidget->widget->name); 1807 return -EINVAL; 1808 } 1809 1810 params = &spcm->params[dir]; 1811 1812 /* set IPC PCM params */ 1813 memset(&pcm, 0, sizeof(pcm)); 1814 pcm.hdr.size = sizeof(pcm); 1815 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 1816 pcm.comp_id = swidget->comp_id; 1817 pcm.params.hdr.size = sizeof(pcm.params); 1818 pcm.params.direction = dir; 1819 pcm.params.sample_valid_bytes = params_width(params) >> 3; 1820 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 1821 pcm.params.rate = params_rate(params); 1822 pcm.params.channels = params_channels(params); 1823 pcm.params.host_period_bytes = params_period_bytes(params); 1824 1825 /* set format */ 1826 switch (params_format(params)) { 1827 case SNDRV_PCM_FORMAT_S16: 1828 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE; 1829 break; 1830 case SNDRV_PCM_FORMAT_S24: 1831 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE; 1832 break; 1833 case SNDRV_PCM_FORMAT_S32: 1834 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 1835 break; 1836 default: 1837 return -EINVAL; 1838 } 1839 1840 /* send IPC to the DSP */ 1841 ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm), 1842 &ipc_params_reply, sizeof(ipc_params_reply)); 1843 if (ret < 0) 1844 dev_err(scomp->dev, "%s: PCM params failed for %s\n", __func__, 1845 swidget->widget->name); 1846 1847 return ret; 1848 } 1849 1850 /* send stream trigger ipc */ 1851 static int sof_ipc3_keyword_detect_trigger(struct snd_sof_widget *swidget, int cmd) 1852 { 1853 struct snd_soc_component *scomp = swidget->scomp; 1854 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1855 struct sof_ipc_stream stream; 1856 struct sof_ipc_reply reply; 1857 int ret; 1858 1859 /* set IPC stream params */ 1860 stream.hdr.size = sizeof(stream); 1861 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd; 1862 stream.comp_id = swidget->comp_id; 1863 1864 /* send IPC to the DSP */ 1865 ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply)); 1866 if (ret < 0) 1867 dev_err(scomp->dev, "%s: Failed to trigger %s\n", __func__, swidget->widget->name); 1868 1869 return ret; 1870 } 1871 1872 static int sof_ipc3_keyword_dapm_event(struct snd_soc_dapm_widget *w, 1873 struct snd_kcontrol *k, int event) 1874 { 1875 struct snd_sof_widget *swidget = w->dobj.private; 1876 struct snd_soc_component *scomp; 1877 int stream = SNDRV_PCM_STREAM_CAPTURE; 1878 struct snd_sof_pcm *spcm; 1879 int ret = 0; 1880 1881 if (!swidget) 1882 return 0; 1883 1884 scomp = swidget->scomp; 1885 1886 dev_dbg(scomp->dev, "received event %d for widget %s\n", 1887 event, w->name); 1888 1889 /* get runtime PCM params using widget's stream name */ 1890 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname); 1891 if (!spcm) { 1892 dev_err(scomp->dev, "%s: Cannot find PCM for %s\n", __func__, 1893 swidget->widget->name); 1894 return -EINVAL; 1895 } 1896 1897 /* process events */ 1898 switch (event) { 1899 case SND_SOC_DAPM_PRE_PMU: 1900 if (spcm->stream[stream].suspend_ignored) { 1901 dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n"); 1902 return 0; 1903 } 1904 1905 /* set pcm params */ 1906 ret = sof_ipc3_keyword_detect_pcm_params(swidget, stream); 1907 if (ret < 0) { 1908 dev_err(scomp->dev, "%s: Failed to set pcm params for widget %s\n", 1909 __func__, swidget->widget->name); 1910 break; 1911 } 1912 1913 /* start trigger */ 1914 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_START); 1915 if (ret < 0) 1916 dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__, 1917 swidget->widget->name); 1918 break; 1919 case SND_SOC_DAPM_POST_PMD: 1920 if (spcm->stream[stream].suspend_ignored) { 1921 dev_dbg(scomp->dev, 1922 "POST_PMD event ignored, KWD pipeline will remain RUNNING\n"); 1923 return 0; 1924 } 1925 1926 /* stop trigger */ 1927 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP); 1928 if (ret < 0) 1929 dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__, 1930 swidget->widget->name); 1931 1932 /* pcm free */ 1933 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_PCM_FREE); 1934 if (ret < 0) 1935 dev_err(scomp->dev, "%s: Failed to free PCM for widget %s\n", __func__, 1936 swidget->widget->name); 1937 break; 1938 default: 1939 break; 1940 } 1941 1942 return ret; 1943 } 1944 1945 /* event handlers for keyword detect component */ 1946 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = { 1947 {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_ipc3_keyword_dapm_event}, 1948 }; 1949 1950 static int sof_ipc3_widget_bind_event(struct snd_soc_component *scomp, 1951 struct snd_sof_widget *swidget, u16 event_type) 1952 { 1953 struct sof_ipc_comp *ipc_comp; 1954 1955 /* validate widget event type */ 1956 switch (event_type) { 1957 case SOF_KEYWORD_DETECT_DAPM_EVENT: 1958 /* only KEYWORD_DETECT comps should handle this */ 1959 if (swidget->id != snd_soc_dapm_effect) 1960 break; 1961 1962 ipc_comp = swidget->private; 1963 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT) 1964 break; 1965 1966 /* bind event to keyword detect comp */ 1967 return snd_soc_tplg_widget_bind_event(swidget->widget, sof_kwd_events, 1968 ARRAY_SIZE(sof_kwd_events), event_type); 1969 default: 1970 break; 1971 } 1972 1973 dev_err(scomp->dev, "Invalid event type %d for widget %s\n", event_type, 1974 swidget->widget->name); 1975 1976 return -EINVAL; 1977 } 1978 1979 static int sof_ipc3_complete_pipeline(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 1980 { 1981 struct sof_ipc_pipe_ready ready; 1982 struct sof_ipc_reply reply; 1983 int ret; 1984 1985 dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n", 1986 swidget->widget->name, swidget->comp_id); 1987 1988 memset(&ready, 0, sizeof(ready)); 1989 ready.hdr.size = sizeof(ready); 1990 ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE; 1991 ready.comp_id = swidget->comp_id; 1992 1993 ret = sof_ipc_tx_message(sdev->ipc, &ready, sizeof(ready), &reply, sizeof(reply)); 1994 if (ret < 0) 1995 return ret; 1996 1997 return 1; 1998 } 1999 2000 static int sof_ipc3_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 2001 { 2002 struct sof_ipc_free ipc_free = { 2003 .hdr = { 2004 .size = sizeof(ipc_free), 2005 .cmd = SOF_IPC_GLB_TPLG_MSG, 2006 }, 2007 .id = swidget->comp_id, 2008 }; 2009 struct sof_ipc_reply reply; 2010 int ret; 2011 2012 if (!swidget->private) 2013 return 0; 2014 2015 switch (swidget->id) { 2016 case snd_soc_dapm_scheduler: 2017 { 2018 ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE; 2019 break; 2020 } 2021 case snd_soc_dapm_buffer: 2022 ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE; 2023 break; 2024 default: 2025 ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE; 2026 break; 2027 } 2028 2029 ret = sof_ipc_tx_message(sdev->ipc, &ipc_free, sizeof(ipc_free), 2030 &reply, sizeof(reply)); 2031 if (ret < 0) 2032 dev_err(sdev->dev, "failed to free widget %s\n", swidget->widget->name); 2033 2034 return ret; 2035 } 2036 2037 static int sof_ipc3_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, 2038 unsigned int flags, struct snd_sof_dai_config_data *data) 2039 { 2040 struct sof_ipc_fw_version *v = &sdev->fw_ready.version; 2041 struct snd_sof_dai *dai = swidget->private; 2042 struct sof_dai_private_data *private; 2043 struct sof_ipc_dai_config *config; 2044 struct sof_ipc_reply reply; 2045 int ret = 0; 2046 2047 if (!dai || !dai->private) { 2048 dev_err(sdev->dev, "No private data for DAI %s\n", swidget->widget->name); 2049 return -EINVAL; 2050 } 2051 2052 private = dai->private; 2053 if (!private->dai_config) { 2054 dev_err(sdev->dev, "No config for DAI %s\n", dai->name); 2055 return -EINVAL; 2056 } 2057 2058 config = &private->dai_config[dai->current_config]; 2059 if (!config) { 2060 dev_err(sdev->dev, "Invalid current config for DAI %s\n", dai->name); 2061 return -EINVAL; 2062 } 2063 2064 switch (config->type) { 2065 case SOF_DAI_INTEL_SSP: 2066 /* 2067 * DAI_CONFIG IPC during hw_params/hw_free for SSP DAI's is not supported in older 2068 * firmware 2069 */ 2070 if (v->abi_version < SOF_ABI_VER(3, 18, 0) && 2071 ((flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) || 2072 (flags & SOF_DAI_CONFIG_FLAGS_HW_FREE))) 2073 return 0; 2074 break; 2075 case SOF_DAI_INTEL_HDA: 2076 if (data) 2077 config->hda.link_dma_ch = data->dai_data; 2078 break; 2079 case SOF_DAI_INTEL_ALH: 2080 if (data) { 2081 config->dai_index = data->dai_index; 2082 config->alh.stream_id = data->dai_data; 2083 } 2084 break; 2085 default: 2086 break; 2087 } 2088 2089 config->flags = flags; 2090 2091 /* only send the IPC if the widget is set up in the DSP */ 2092 if (swidget->use_count > 0) { 2093 ret = sof_ipc_tx_message(sdev->ipc, config, config->hdr.size, 2094 &reply, sizeof(reply)); 2095 if (ret < 0) 2096 dev_err(sdev->dev, "Failed to set dai config for %s\n", dai->name); 2097 } 2098 2099 return ret; 2100 } 2101 2102 static int sof_ipc3_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 2103 { 2104 struct sof_ipc_comp_reply reply; 2105 int ret; 2106 2107 if (!swidget->private) 2108 return 0; 2109 2110 switch (swidget->id) { 2111 case snd_soc_dapm_dai_in: 2112 case snd_soc_dapm_dai_out: 2113 { 2114 struct snd_sof_dai *dai = swidget->private; 2115 struct sof_dai_private_data *dai_data = dai->private; 2116 struct sof_ipc_comp *comp = &dai_data->comp_dai->comp; 2117 2118 ret = sof_ipc_tx_message(sdev->ipc, dai_data->comp_dai, 2119 comp->hdr.size, &reply, sizeof(reply)); 2120 break; 2121 } 2122 case snd_soc_dapm_scheduler: 2123 { 2124 struct sof_ipc_pipe_new *pipeline; 2125 2126 pipeline = swidget->private; 2127 ret = sof_ipc_tx_message(sdev->ipc, pipeline, sizeof(*pipeline), 2128 &reply, sizeof(reply)); 2129 break; 2130 } 2131 default: 2132 { 2133 struct sof_ipc_cmd_hdr *hdr; 2134 2135 hdr = swidget->private; 2136 ret = sof_ipc_tx_message(sdev->ipc, swidget->private, hdr->size, 2137 &reply, sizeof(reply)); 2138 break; 2139 } 2140 } 2141 if (ret < 0) 2142 dev_err(sdev->dev, "Failed to setup widget %s\n", swidget->widget->name); 2143 2144 return ret; 2145 } 2146 2147 static int sof_ipc3_set_up_all_pipelines(struct snd_sof_dev *sdev, bool verify) 2148 { 2149 struct sof_ipc_fw_version *v = &sdev->fw_ready.version; 2150 struct snd_sof_widget *swidget; 2151 struct snd_sof_route *sroute; 2152 int ret; 2153 2154 /* restore pipeline components */ 2155 list_for_each_entry(swidget, &sdev->widget_list, list) { 2156 /* only set up the widgets belonging to static pipelines */ 2157 if (!verify && swidget->dynamic_pipeline_widget) 2158 continue; 2159 2160 /* 2161 * For older firmware, skip scheduler widgets in this loop, 2162 * sof_widget_setup() will be called in the 'complete pipeline' loop 2163 */ 2164 if (v->abi_version < SOF_ABI_VER(3, 19, 0) && 2165 swidget->id == snd_soc_dapm_scheduler) 2166 continue; 2167 2168 /* update DAI config. The IPC will be sent in sof_widget_setup() */ 2169 if (WIDGET_IS_DAI(swidget->id)) { 2170 struct snd_sof_dai *dai = swidget->private; 2171 struct sof_dai_private_data *private; 2172 struct sof_ipc_dai_config *config; 2173 2174 if (!dai || !dai->private) 2175 continue; 2176 private = dai->private; 2177 if (!private->dai_config) 2178 continue; 2179 2180 config = private->dai_config; 2181 /* 2182 * The link DMA channel would be invalidated for running 2183 * streams but not for streams that were in the PAUSED 2184 * state during suspend. So invalidate it here before setting 2185 * the dai config in the DSP. 2186 */ 2187 if (config->type == SOF_DAI_INTEL_HDA) 2188 config->hda.link_dma_ch = DMA_CHAN_INVALID; 2189 } 2190 2191 ret = sof_widget_setup(sdev, swidget); 2192 if (ret < 0) 2193 return ret; 2194 } 2195 2196 /* restore pipeline connections */ 2197 list_for_each_entry(sroute, &sdev->route_list, list) { 2198 /* only set up routes belonging to static pipelines */ 2199 if (!verify && (sroute->src_widget->dynamic_pipeline_widget || 2200 sroute->sink_widget->dynamic_pipeline_widget)) 2201 continue; 2202 2203 /* 2204 * For virtual routes, both sink and source are not buffer. IPC3 only supports 2205 * connections between a buffer and a component. Ignore the rest. 2206 */ 2207 if (sroute->src_widget->id != snd_soc_dapm_buffer && 2208 sroute->sink_widget->id != snd_soc_dapm_buffer) 2209 continue; 2210 2211 ret = sof_route_setup(sdev, sroute->src_widget->widget, 2212 sroute->sink_widget->widget); 2213 if (ret < 0) { 2214 dev_err(sdev->dev, "%s: route set up failed\n", __func__); 2215 return ret; 2216 } 2217 } 2218 2219 /* complete pipeline */ 2220 list_for_each_entry(swidget, &sdev->widget_list, list) { 2221 switch (swidget->id) { 2222 case snd_soc_dapm_scheduler: 2223 /* only complete static pipelines */ 2224 if (!verify && swidget->dynamic_pipeline_widget) 2225 continue; 2226 2227 if (v->abi_version < SOF_ABI_VER(3, 19, 0)) { 2228 ret = sof_widget_setup(sdev, swidget); 2229 if (ret < 0) 2230 return ret; 2231 } 2232 2233 swidget->complete = sof_ipc3_complete_pipeline(sdev, swidget); 2234 if (swidget->complete < 0) 2235 return swidget->complete; 2236 break; 2237 default: 2238 break; 2239 } 2240 } 2241 2242 return 0; 2243 } 2244 2245 /* 2246 * Free the PCM, its associated widgets and set the prepared flag to false for all PCMs that 2247 * did not get suspended(ex: paused streams) so the widgets can be set up again during resume. 2248 */ 2249 static int sof_tear_down_left_over_pipelines(struct snd_sof_dev *sdev) 2250 { 2251 struct snd_sof_widget *swidget; 2252 struct snd_sof_pcm *spcm; 2253 int dir, ret; 2254 2255 /* 2256 * free all PCMs and their associated DAPM widgets if their connected DAPM widget 2257 * list is not NULL. This should only be true for paused streams at this point. 2258 * This is equivalent to the handling of FE DAI suspend trigger for running streams. 2259 */ 2260 list_for_each_entry(spcm, &sdev->pcm_list, list) { 2261 for_each_pcm_streams(dir) { 2262 struct snd_pcm_substream *substream = spcm->stream[dir].substream; 2263 2264 if (!substream || !substream->runtime) 2265 continue; 2266 2267 if (spcm->stream[dir].list) { 2268 ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true); 2269 if (ret < 0) 2270 return ret; 2271 } 2272 } 2273 } 2274 2275 /* 2276 * free any left over DAI widgets. This is equivalent to the handling of suspend trigger 2277 * for the BE DAI for running streams. 2278 */ 2279 list_for_each_entry(swidget, &sdev->widget_list, list) 2280 if (WIDGET_IS_DAI(swidget->id) && swidget->use_count == 1) { 2281 ret = sof_widget_free(sdev, swidget); 2282 if (ret < 0) 2283 return ret; 2284 } 2285 2286 return 0; 2287 } 2288 2289 /* 2290 * For older firmware, this function doesn't free widgets for static pipelines during suspend. 2291 * It only resets use_count for all widgets. 2292 */ 2293 static int sof_ipc3_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify) 2294 { 2295 struct sof_ipc_fw_version *v = &sdev->fw_ready.version; 2296 struct snd_sof_widget *swidget; 2297 struct snd_sof_route *sroute; 2298 bool dyn_widgets = false; 2299 int ret; 2300 2301 /* 2302 * This function is called during suspend and for one-time topology verification during 2303 * first boot. In both cases, there is no need to protect swidget->use_count and 2304 * sroute->setup because during suspend all running streams are suspended and during 2305 * topology loading the sound card unavailable to open PCMs. 2306 */ 2307 list_for_each_entry(swidget, &sdev->widget_list, list) { 2308 if (swidget->dynamic_pipeline_widget) { 2309 dyn_widgets = true; 2310 continue; 2311 } 2312 2313 /* Do not free widgets for static pipelines with FW older than SOF2.2 */ 2314 if (!verify && !swidget->dynamic_pipeline_widget && 2315 SOF_FW_VER(v->major, v->minor, v->micro) < SOF_FW_VER(2, 2, 0)) { 2316 swidget->use_count = 0; 2317 swidget->complete = 0; 2318 continue; 2319 } 2320 2321 ret = sof_widget_free(sdev, swidget); 2322 if (ret < 0) 2323 return ret; 2324 } 2325 2326 /* 2327 * Tear down all pipelines associated with PCMs that did not get suspended 2328 * and unset the prepare flag so that they can be set up again during resume. 2329 * Skip this step for older firmware unless topology has any 2330 * dynamic pipeline (in which case the step is mandatory). 2331 */ 2332 if (!verify && (dyn_widgets || SOF_FW_VER(v->major, v->minor, v->micro) >= 2333 SOF_FW_VER(2, 2, 0))) { 2334 ret = sof_tear_down_left_over_pipelines(sdev); 2335 if (ret < 0) { 2336 dev_err(sdev->dev, "failed to tear down paused pipelines\n"); 2337 return ret; 2338 } 2339 } 2340 2341 list_for_each_entry(sroute, &sdev->route_list, list) 2342 sroute->setup = false; 2343 2344 /* 2345 * before suspending, make sure the refcounts are all zeroed out. There's no way 2346 * to recover at this point but this will help root cause bad sequences leading to 2347 * more issues on resume 2348 */ 2349 list_for_each_entry(swidget, &sdev->widget_list, list) { 2350 if (swidget->use_count != 0) { 2351 dev_err(sdev->dev, "%s: widget %s is still in use: count %d\n", 2352 __func__, swidget->widget->name, swidget->use_count); 2353 } 2354 } 2355 2356 return 0; 2357 } 2358 2359 static int sof_ipc3_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type) 2360 { 2361 struct sof_dai_private_data *private = dai->private; 2362 2363 if (!private || !private->dai_config) 2364 return 0; 2365 2366 switch (private->dai_config->type) { 2367 case SOF_DAI_INTEL_SSP: 2368 switch (clk_type) { 2369 case SOF_DAI_CLK_INTEL_SSP_MCLK: 2370 return private->dai_config->ssp.mclk_rate; 2371 case SOF_DAI_CLK_INTEL_SSP_BCLK: 2372 return private->dai_config->ssp.bclk_rate; 2373 default: 2374 break; 2375 } 2376 dev_err(sdev->dev, "fail to get SSP clk %d rate\n", clk_type); 2377 break; 2378 default: 2379 /* not yet implemented for platforms other than the above */ 2380 dev_err(sdev->dev, "DAI type %d not supported yet!\n", private->dai_config->type); 2381 break; 2382 } 2383 2384 return -EINVAL; 2385 } 2386 2387 static int sof_ipc3_parse_manifest(struct snd_soc_component *scomp, int index, 2388 struct snd_soc_tplg_manifest *man) 2389 { 2390 u32 size = le32_to_cpu(man->priv.size); 2391 u32 abi_version; 2392 2393 /* backward compatible with tplg without ABI info */ 2394 if (!size) { 2395 dev_dbg(scomp->dev, "No topology ABI info\n"); 2396 return 0; 2397 } 2398 2399 if (size != SOF_IPC3_TPLG_ABI_SIZE) { 2400 dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n", 2401 __func__, size); 2402 return -EINVAL; 2403 } 2404 2405 dev_info(scomp->dev, 2406 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n", 2407 man->priv.data[0], man->priv.data[1], man->priv.data[2], 2408 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH); 2409 2410 abi_version = SOF_ABI_VER(man->priv.data[0], man->priv.data[1], man->priv.data[2]); 2411 2412 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) { 2413 dev_err(scomp->dev, "%s: Incompatible topology ABI version\n", __func__); 2414 return -EINVAL; 2415 } 2416 2417 if (IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS) && 2418 SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) { 2419 dev_err(scomp->dev, "%s: Topology ABI is more recent than kernel\n", __func__); 2420 return -EINVAL; 2421 } 2422 2423 return 0; 2424 } 2425 2426 /* token list for each topology object */ 2427 static enum sof_tokens host_token_list[] = { 2428 SOF_CORE_TOKENS, 2429 SOF_COMP_EXT_TOKENS, 2430 SOF_PCM_TOKENS, 2431 SOF_COMP_TOKENS, 2432 }; 2433 2434 static enum sof_tokens comp_generic_token_list[] = { 2435 SOF_CORE_TOKENS, 2436 SOF_COMP_EXT_TOKENS, 2437 SOF_COMP_TOKENS, 2438 }; 2439 2440 static enum sof_tokens buffer_token_list[] = { 2441 SOF_BUFFER_TOKENS, 2442 }; 2443 2444 static enum sof_tokens pipeline_token_list[] = { 2445 SOF_CORE_TOKENS, 2446 SOF_COMP_EXT_TOKENS, 2447 SOF_PIPELINE_TOKENS, 2448 SOF_SCHED_TOKENS, 2449 }; 2450 2451 static enum sof_tokens asrc_token_list[] = { 2452 SOF_CORE_TOKENS, 2453 SOF_COMP_EXT_TOKENS, 2454 SOF_ASRC_TOKENS, 2455 SOF_COMP_TOKENS, 2456 }; 2457 2458 static enum sof_tokens src_token_list[] = { 2459 SOF_CORE_TOKENS, 2460 SOF_COMP_EXT_TOKENS, 2461 SOF_SRC_TOKENS, 2462 SOF_COMP_TOKENS 2463 }; 2464 2465 static enum sof_tokens pga_token_list[] = { 2466 SOF_CORE_TOKENS, 2467 SOF_COMP_EXT_TOKENS, 2468 SOF_VOLUME_TOKENS, 2469 SOF_COMP_TOKENS, 2470 }; 2471 2472 static enum sof_tokens dai_token_list[] = { 2473 SOF_CORE_TOKENS, 2474 SOF_COMP_EXT_TOKENS, 2475 SOF_DAI_TOKENS, 2476 SOF_COMP_TOKENS, 2477 }; 2478 2479 static enum sof_tokens process_token_list[] = { 2480 SOF_CORE_TOKENS, 2481 SOF_COMP_EXT_TOKENS, 2482 SOF_PROCESS_TOKENS, 2483 SOF_COMP_TOKENS, 2484 }; 2485 2486 static const struct sof_ipc_tplg_widget_ops tplg_ipc3_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = { 2487 [snd_soc_dapm_aif_in] = {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp, 2488 host_token_list, ARRAY_SIZE(host_token_list), NULL}, 2489 [snd_soc_dapm_aif_out] = {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp, 2490 host_token_list, ARRAY_SIZE(host_token_list), NULL}, 2491 2492 [snd_soc_dapm_dai_in] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai, 2493 dai_token_list, ARRAY_SIZE(dai_token_list), NULL}, 2494 [snd_soc_dapm_dai_out] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai, 2495 dai_token_list, ARRAY_SIZE(dai_token_list), NULL}, 2496 [snd_soc_dapm_buffer] = {sof_ipc3_widget_setup_comp_buffer, sof_ipc3_widget_free_comp, 2497 buffer_token_list, ARRAY_SIZE(buffer_token_list), NULL}, 2498 [snd_soc_dapm_mixer] = {sof_ipc3_widget_setup_comp_mixer, sof_ipc3_widget_free_comp, 2499 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), 2500 NULL}, 2501 [snd_soc_dapm_src] = {sof_ipc3_widget_setup_comp_src, sof_ipc3_widget_free_comp, 2502 src_token_list, ARRAY_SIZE(src_token_list), NULL}, 2503 [snd_soc_dapm_asrc] = {sof_ipc3_widget_setup_comp_asrc, sof_ipc3_widget_free_comp, 2504 asrc_token_list, ARRAY_SIZE(asrc_token_list), NULL}, 2505 [snd_soc_dapm_siggen] = {sof_ipc3_widget_setup_comp_tone, sof_ipc3_widget_free_comp, 2506 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), 2507 NULL}, 2508 [snd_soc_dapm_scheduler] = {sof_ipc3_widget_setup_comp_pipeline, sof_ipc3_widget_free_comp, 2509 pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL}, 2510 [snd_soc_dapm_pga] = {sof_ipc3_widget_setup_comp_pga, sof_ipc3_widget_free_comp, 2511 pga_token_list, ARRAY_SIZE(pga_token_list), NULL}, 2512 [snd_soc_dapm_mux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp, 2513 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), NULL}, 2514 [snd_soc_dapm_demux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp, 2515 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), 2516 NULL}, 2517 [snd_soc_dapm_effect] = {sof_widget_update_ipc_comp_process, sof_ipc3_widget_free_comp, 2518 process_token_list, ARRAY_SIZE(process_token_list), 2519 sof_ipc3_widget_bind_event}, 2520 }; 2521 2522 const struct sof_ipc_tplg_ops ipc3_tplg_ops = { 2523 .widget = tplg_ipc3_widget_ops, 2524 .control = &tplg_ipc3_control_ops, 2525 .route_setup = sof_ipc3_route_setup, 2526 .control_setup = sof_ipc3_control_setup, 2527 .control_free = sof_ipc3_control_free, 2528 .pipeline_complete = sof_ipc3_complete_pipeline, 2529 .token_list = ipc3_token_list, 2530 .widget_free = sof_ipc3_widget_free, 2531 .widget_setup = sof_ipc3_widget_setup, 2532 .dai_config = sof_ipc3_dai_config, 2533 .dai_get_clk = sof_ipc3_dai_get_clk, 2534 .set_up_all_pipelines = sof_ipc3_set_up_all_pipelines, 2535 .tear_down_all_pipelines = sof_ipc3_tear_down_all_pipelines, 2536 .parse_manifest = sof_ipc3_parse_manifest, 2537 }; 2538