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