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