Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
0002 //
0003 // This file is provided under a dual BSD/GPLv2 license.  When using or
0004 // redistributing this file, you may do so under either license.
0005 //
0006 // Copyright(c) 2021 Intel Corporation. All rights reserved.
0007 //
0008 //
0009 
0010 #include <uapi/sound/sof/tokens.h>
0011 #include <sound/pcm_params.h>
0012 #include "sof-priv.h"
0013 #include "sof-audio.h"
0014 #include "ipc3-priv.h"
0015 #include "ops.h"
0016 
0017 /* Full volume for default values */
0018 #define VOL_ZERO_DB BIT(VOLUME_FWL)
0019 
0020 /* size of tplg ABI in bytes */
0021 #define SOF_IPC3_TPLG_ABI_SIZE 3
0022 
0023 struct sof_widget_data {
0024     int ctrl_type;
0025     int ipc_cmd;
0026     void *pdata;
0027     size_t pdata_size;
0028     struct snd_sof_control *control;
0029 };
0030 
0031 struct sof_process_types {
0032     const char *name;
0033     enum sof_ipc_process_type type;
0034     enum sof_comp_type comp_type;
0035 };
0036 
0037 static const struct sof_process_types sof_process[] = {
0038     {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
0039     {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
0040     {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
0041     {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
0042     {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
0043     {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
0044     {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
0045     {"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK},
0046     {"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP},
0047 };
0048 
0049 static enum sof_ipc_process_type find_process(const char *name)
0050 {
0051     int i;
0052 
0053     for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
0054         if (strcmp(name, sof_process[i].name) == 0)
0055             return sof_process[i].type;
0056     }
0057 
0058     return SOF_PROCESS_NONE;
0059 }
0060 
0061 static int get_token_process_type(void *elem, void *object, u32 offset)
0062 {
0063     u32 *val = (u32 *)((u8 *)object + offset);
0064 
0065     *val = find_process((const char *)elem);
0066     return 0;
0067 }
0068 
0069 /* Buffers */
0070 static const struct sof_topology_token buffer_tokens[] = {
0071     {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0072         offsetof(struct sof_ipc_buffer, size)},
0073     {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0074         offsetof(struct sof_ipc_buffer, caps)},
0075 };
0076 
0077 /* DAI */
0078 static const struct sof_topology_token dai_tokens[] = {
0079     {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
0080         offsetof(struct sof_ipc_comp_dai, type)},
0081     {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0082         offsetof(struct sof_ipc_comp_dai, dai_index)},
0083     {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0084         offsetof(struct sof_ipc_comp_dai, direction)},
0085 };
0086 
0087 /* BE DAI link */
0088 static const struct sof_topology_token dai_link_tokens[] = {
0089     {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
0090         offsetof(struct sof_ipc_dai_config, type)},
0091     {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0092         offsetof(struct sof_ipc_dai_config, dai_index)},
0093 };
0094 
0095 /* scheduling */
0096 static const struct sof_topology_token sched_tokens[] = {
0097     {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0098         offsetof(struct sof_ipc_pipe_new, period)},
0099     {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0100         offsetof(struct sof_ipc_pipe_new, priority)},
0101     {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0102         offsetof(struct sof_ipc_pipe_new, period_mips)},
0103     {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0104         offsetof(struct sof_ipc_pipe_new, core)},
0105     {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0106         offsetof(struct sof_ipc_pipe_new, frames_per_sched)},
0107     {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0108         offsetof(struct sof_ipc_pipe_new, time_domain)},
0109 };
0110 
0111 static const struct sof_topology_token pipeline_tokens[] = {
0112     {SOF_TKN_SCHED_DYNAMIC_PIPELINE, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
0113         offsetof(struct snd_sof_widget, dynamic_pipeline_widget)},
0114 
0115 };
0116 
0117 /* volume */
0118 static const struct sof_topology_token volume_tokens[] = {
0119     {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0120         offsetof(struct sof_ipc_comp_volume, ramp)},
0121     {SOF_TKN_VOLUME_RAMP_STEP_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0122         offsetof(struct sof_ipc_comp_volume, initial_ramp)},
0123 };
0124 
0125 /* SRC */
0126 static const struct sof_topology_token src_tokens[] = {
0127     {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0128         offsetof(struct sof_ipc_comp_src, source_rate)},
0129     {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0130         offsetof(struct sof_ipc_comp_src, sink_rate)},
0131 };
0132 
0133 /* ASRC */
0134 static const struct sof_topology_token asrc_tokens[] = {
0135     {SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0136         offsetof(struct sof_ipc_comp_asrc, source_rate)},
0137     {SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0138         offsetof(struct sof_ipc_comp_asrc, sink_rate)},
0139     {SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0140         offsetof(struct sof_ipc_comp_asrc, asynchronous_mode)},
0141     {SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0142         offsetof(struct sof_ipc_comp_asrc, operation_mode)},
0143 };
0144 
0145 /* EFFECT */
0146 static const struct sof_topology_token process_tokens[] = {
0147     {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_process_type,
0148         offsetof(struct sof_ipc_comp_process, type)},
0149 };
0150 
0151 /* PCM */
0152 static const struct sof_topology_token pcm_tokens[] = {
0153     {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0154         offsetof(struct sof_ipc_comp_host, dmac_config)},
0155 };
0156 
0157 /* Generic components */
0158 static const struct sof_topology_token comp_tokens[] = {
0159     {SOF_TKN_COMP_PERIOD_SINK_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0160         offsetof(struct sof_ipc_comp_config, periods_sink)},
0161     {SOF_TKN_COMP_PERIOD_SOURCE_COUNT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0162         offsetof(struct sof_ipc_comp_config, periods_source)},
0163     {SOF_TKN_COMP_FORMAT,
0164         SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
0165         offsetof(struct sof_ipc_comp_config, frame_fmt)},
0166 };
0167 
0168 /* SSP */
0169 static const struct sof_topology_token ssp_tokens[] = {
0170     {SOF_TKN_INTEL_SSP_CLKS_CONTROL, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0171         offsetof(struct sof_ipc_dai_ssp_params, clks_control)},
0172     {SOF_TKN_INTEL_SSP_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0173         offsetof(struct sof_ipc_dai_ssp_params, mclk_id)},
0174     {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0175         offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits)},
0176     {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,    get_token_u16,
0177         offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width)},
0178     {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0179         offsetof(struct sof_ipc_dai_ssp_params, quirks)},
0180     {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
0181         offsetof(struct sof_ipc_dai_ssp_params, tdm_per_slot_padding_flag)},
0182     {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0183         offsetof(struct sof_ipc_dai_ssp_params, bclk_delay)},
0184 };
0185 
0186 /* ALH */
0187 static const struct sof_topology_token alh_tokens[] = {
0188     {SOF_TKN_INTEL_ALH_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0189         offsetof(struct sof_ipc_dai_alh_params, rate)},
0190     {SOF_TKN_INTEL_ALH_CH,  SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0191         offsetof(struct sof_ipc_dai_alh_params, channels)},
0192 };
0193 
0194 /* DMIC */
0195 static const struct sof_topology_token dmic_tokens[] = {
0196     {SOF_TKN_INTEL_DMIC_DRIVER_VERSION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0197         offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version)},
0198     {SOF_TKN_INTEL_DMIC_CLK_MIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0199         offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min)},
0200     {SOF_TKN_INTEL_DMIC_CLK_MAX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0201         offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max)},
0202     {SOF_TKN_INTEL_DMIC_SAMPLE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0203         offsetof(struct sof_ipc_dai_dmic_params, fifo_fs)},
0204     {SOF_TKN_INTEL_DMIC_DUTY_MIN, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0205         offsetof(struct sof_ipc_dai_dmic_params, duty_min)},
0206     {SOF_TKN_INTEL_DMIC_DUTY_MAX, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0207         offsetof(struct sof_ipc_dai_dmic_params, duty_max)},
0208     {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0209         offsetof(struct sof_ipc_dai_dmic_params, num_pdm_active)},
0210     {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0211         offsetof(struct sof_ipc_dai_dmic_params, fifo_bits)},
0212     {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0213         offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time)},
0214 };
0215 
0216 /* ESAI */
0217 static const struct sof_topology_token esai_tokens[] = {
0218     {SOF_TKN_IMX_ESAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0219         offsetof(struct sof_ipc_dai_esai_params, mclk_id)},
0220 };
0221 
0222 /* SAI */
0223 static const struct sof_topology_token sai_tokens[] = {
0224     {SOF_TKN_IMX_SAI_MCLK_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0225         offsetof(struct sof_ipc_dai_sai_params, mclk_id)},
0226 };
0227 
0228 /*
0229  * DMIC PDM Tokens
0230  * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
0231  * as it increments the index while parsing the array of pdm tokens
0232  * and determines the correct offset
0233  */
0234 static const struct sof_topology_token dmic_pdm_tokens[] = {
0235     {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0236         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id)},
0237     {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0238         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a)},
0239     {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0240         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b)},
0241     {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0242         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a)},
0243     {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0244         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b)},
0245     {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0246         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge)},
0247     {SOF_TKN_INTEL_DMIC_PDM_SKEW, SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
0248         offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew)},
0249 };
0250 
0251 /* HDA */
0252 static const struct sof_topology_token hda_tokens[] = {
0253     {SOF_TKN_INTEL_HDA_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0254         offsetof(struct sof_ipc_dai_hda_params, rate)},
0255     {SOF_TKN_INTEL_HDA_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0256         offsetof(struct sof_ipc_dai_hda_params, channels)},
0257 };
0258 
0259 /* AFE */
0260 static const struct sof_topology_token afe_tokens[] = {
0261     {SOF_TKN_MEDIATEK_AFE_RATE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0262         offsetof(struct sof_ipc_dai_mtk_afe_params, rate)},
0263     {SOF_TKN_MEDIATEK_AFE_CH, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0264         offsetof(struct sof_ipc_dai_mtk_afe_params, channels)},
0265     {SOF_TKN_MEDIATEK_AFE_FORMAT, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
0266         offsetof(struct sof_ipc_dai_mtk_afe_params, format)},
0267 };
0268 
0269 /* ACPDMIC */
0270 static const struct sof_topology_token acpdmic_tokens[] = {
0271     {SOF_TKN_AMD_ACPDMIC_RATE,
0272         SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0273         offsetof(struct sof_ipc_dai_acpdmic_params, pdm_rate)},
0274     {SOF_TKN_AMD_ACPDMIC_CH,
0275         SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0276         offsetof(struct sof_ipc_dai_acpdmic_params, pdm_ch)},
0277 };
0278 
0279 /* Core tokens */
0280 static const struct sof_topology_token core_tokens[] = {
0281     {SOF_TKN_COMP_CORE_ID, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
0282         offsetof(struct sof_ipc_comp, core)},
0283 };
0284 
0285 /* Component extended tokens */
0286 static const struct sof_topology_token comp_ext_tokens[] = {
0287     {SOF_TKN_COMP_UUID, SND_SOC_TPLG_TUPLE_TYPE_UUID, get_token_uuid,
0288         offsetof(struct snd_sof_widget, uuid)},
0289 };
0290 
0291 static const struct sof_token_info ipc3_token_list[SOF_TOKEN_COUNT] = {
0292     [SOF_PCM_TOKENS] = {"PCM tokens", pcm_tokens, ARRAY_SIZE(pcm_tokens)},
0293     [SOF_PIPELINE_TOKENS] = {"Pipeline tokens", pipeline_tokens, ARRAY_SIZE(pipeline_tokens)},
0294     [SOF_SCHED_TOKENS] = {"Scheduler tokens", sched_tokens, ARRAY_SIZE(sched_tokens)},
0295     [SOF_COMP_TOKENS] = {"Comp tokens", comp_tokens, ARRAY_SIZE(comp_tokens)},
0296     [SOF_CORE_TOKENS] = {"Core tokens", core_tokens, ARRAY_SIZE(core_tokens)},
0297     [SOF_COMP_EXT_TOKENS] = {"AFE tokens", comp_ext_tokens, ARRAY_SIZE(comp_ext_tokens)},
0298     [SOF_BUFFER_TOKENS] = {"Buffer tokens", buffer_tokens, ARRAY_SIZE(buffer_tokens)},
0299     [SOF_VOLUME_TOKENS] = {"Volume tokens", volume_tokens, ARRAY_SIZE(volume_tokens)},
0300     [SOF_SRC_TOKENS] = {"SRC tokens", src_tokens, ARRAY_SIZE(src_tokens)},
0301     [SOF_ASRC_TOKENS] = {"ASRC tokens", asrc_tokens, ARRAY_SIZE(asrc_tokens)},
0302     [SOF_PROCESS_TOKENS] = {"Process tokens", process_tokens, ARRAY_SIZE(process_tokens)},
0303     [SOF_DAI_TOKENS] = {"DAI tokens", dai_tokens, ARRAY_SIZE(dai_tokens)},
0304     [SOF_DAI_LINK_TOKENS] = {"DAI link tokens", dai_link_tokens, ARRAY_SIZE(dai_link_tokens)},
0305     [SOF_HDA_TOKENS] = {"HDA tokens", hda_tokens, ARRAY_SIZE(hda_tokens)},
0306     [SOF_SSP_TOKENS] = {"SSP tokens", ssp_tokens, ARRAY_SIZE(ssp_tokens)},
0307     [SOF_ALH_TOKENS] = {"ALH tokens", alh_tokens, ARRAY_SIZE(alh_tokens)},
0308     [SOF_DMIC_TOKENS] = {"DMIC tokens", dmic_tokens, ARRAY_SIZE(dmic_tokens)},
0309     [SOF_DMIC_PDM_TOKENS] = {"DMIC PDM tokens", dmic_pdm_tokens, ARRAY_SIZE(dmic_pdm_tokens)},
0310     [SOF_ESAI_TOKENS] = {"ESAI tokens", esai_tokens, ARRAY_SIZE(esai_tokens)},
0311     [SOF_SAI_TOKENS] = {"SAI tokens", sai_tokens, ARRAY_SIZE(sai_tokens)},
0312     [SOF_AFE_TOKENS] = {"AFE tokens", afe_tokens, ARRAY_SIZE(afe_tokens)},
0313     [SOF_ACPDMIC_TOKENS] = {"ACPDMIC tokens", acpdmic_tokens, ARRAY_SIZE(acpdmic_tokens)},
0314 };
0315 
0316 /**
0317  * sof_comp_alloc - allocate and initialize buffer for a new component
0318  * @swidget: pointer to struct snd_sof_widget containing extended data
0319  * @ipc_size: IPC payload size that will be updated depending on valid
0320  *  extended data.
0321  * @index: ID of the pipeline the component belongs to
0322  *
0323  * Return: The pointer to the new allocated component, NULL if failed.
0324  */
0325 static void *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size,
0326                 int index)
0327 {
0328     struct sof_ipc_comp *comp;
0329     size_t total_size = *ipc_size;
0330     size_t ext_size = sizeof(swidget->uuid);
0331 
0332     /* only non-zero UUID is valid */
0333     if (!guid_is_null(&swidget->uuid))
0334         total_size += ext_size;
0335 
0336     comp = kzalloc(total_size, GFP_KERNEL);
0337     if (!comp)
0338         return NULL;
0339 
0340     /* configure comp new IPC message */
0341     comp->hdr.size = total_size;
0342     comp->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
0343     comp->id = swidget->comp_id;
0344     comp->pipeline_id = index;
0345     comp->core = swidget->core;
0346 
0347     /* handle the extended data if needed */
0348     if (total_size > *ipc_size) {
0349         /* append extended data to the end of the component */
0350         memcpy((u8 *)comp + *ipc_size, &swidget->uuid, ext_size);
0351         comp->ext_data_length = ext_size;
0352     }
0353 
0354     /* update ipc_size and return */
0355     *ipc_size = total_size;
0356     return comp;
0357 }
0358 
0359 static void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config)
0360 {
0361     dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
0362         config->periods_sink, config->periods_source,
0363         config->frame_fmt);
0364 }
0365 
0366 static int sof_ipc3_widget_setup_comp_host(struct snd_sof_widget *swidget)
0367 {
0368     struct snd_soc_component *scomp = swidget->scomp;
0369     struct sof_ipc_comp_host *host;
0370     size_t ipc_size = sizeof(*host);
0371     int ret;
0372 
0373     host = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0374     if (!host)
0375         return -ENOMEM;
0376     swidget->private = host;
0377 
0378     /* configure host comp IPC message */
0379     host->comp.type = SOF_COMP_HOST;
0380     host->config.hdr.size = sizeof(host->config);
0381 
0382     if (swidget->id == snd_soc_dapm_aif_out)
0383         host->direction = SOF_IPC_STREAM_CAPTURE;
0384     else
0385         host->direction = SOF_IPC_STREAM_PLAYBACK;
0386 
0387     /* parse one set of pcm_tokens */
0388     ret = sof_update_ipc_object(scomp, host, SOF_PCM_TOKENS, swidget->tuples,
0389                     swidget->num_tuples, sizeof(*host), 1);
0390     if (ret < 0)
0391         goto err;
0392 
0393     /* parse one set of comp_tokens */
0394     ret = sof_update_ipc_object(scomp, &host->config, SOF_COMP_TOKENS, swidget->tuples,
0395                     swidget->num_tuples, sizeof(host->config), 1);
0396     if (ret < 0)
0397         goto err;
0398 
0399     dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
0400     sof_dbg_comp_config(scomp, &host->config);
0401 
0402     return 0;
0403 err:
0404     kfree(swidget->private);
0405     swidget->private = NULL;
0406 
0407     return ret;
0408 }
0409 
0410 static void sof_ipc3_widget_free_comp(struct snd_sof_widget *swidget)
0411 {
0412     kfree(swidget->private);
0413 }
0414 
0415 static int sof_ipc3_widget_setup_comp_tone(struct snd_sof_widget *swidget)
0416 {
0417     struct snd_soc_component *scomp = swidget->scomp;
0418     struct sof_ipc_comp_tone *tone;
0419     size_t ipc_size = sizeof(*tone);
0420     int ret;
0421 
0422     tone = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0423     if (!tone)
0424         return -ENOMEM;
0425 
0426     swidget->private = tone;
0427 
0428     /* configure siggen IPC message */
0429     tone->comp.type = SOF_COMP_TONE;
0430     tone->config.hdr.size = sizeof(tone->config);
0431 
0432     /* parse one set of comp tokens */
0433     ret = sof_update_ipc_object(scomp, &tone->config, SOF_COMP_TOKENS, swidget->tuples,
0434                     swidget->num_tuples, sizeof(tone->config), 1);
0435     if (ret < 0) {
0436         kfree(swidget->private);
0437         swidget->private = NULL;
0438         return ret;
0439     }
0440 
0441     dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
0442         swidget->widget->name, tone->frequency, tone->amplitude);
0443     sof_dbg_comp_config(scomp, &tone->config);
0444 
0445     return 0;
0446 }
0447 
0448 static int sof_ipc3_widget_setup_comp_mixer(struct snd_sof_widget *swidget)
0449 {
0450     struct snd_soc_component *scomp = swidget->scomp;
0451     struct sof_ipc_comp_mixer *mixer;
0452     size_t ipc_size = sizeof(*mixer);
0453     int ret;
0454 
0455     mixer = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0456     if (!mixer)
0457         return -ENOMEM;
0458 
0459     swidget->private = mixer;
0460 
0461     /* configure mixer IPC message */
0462     mixer->comp.type = SOF_COMP_MIXER;
0463     mixer->config.hdr.size = sizeof(mixer->config);
0464 
0465     /* parse one set of comp tokens */
0466     ret = sof_update_ipc_object(scomp, &mixer->config, SOF_COMP_TOKENS,
0467                     swidget->tuples, swidget->num_tuples,
0468                     sizeof(mixer->config), 1);
0469     if (ret < 0) {
0470         kfree(swidget->private);
0471         swidget->private = NULL;
0472 
0473         return ret;
0474     }
0475 
0476     dev_dbg(scomp->dev, "loaded mixer %s\n", swidget->widget->name);
0477     sof_dbg_comp_config(scomp, &mixer->config);
0478 
0479     return 0;
0480 }
0481 
0482 static int sof_ipc3_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)
0483 {
0484     struct snd_soc_component *scomp = swidget->scomp;
0485     struct sof_ipc_pipe_new *pipeline;
0486     struct snd_sof_widget *comp_swidget;
0487     int ret;
0488 
0489     pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
0490     if (!pipeline)
0491         return -ENOMEM;
0492 
0493     /* configure pipeline IPC message */
0494     pipeline->hdr.size = sizeof(*pipeline);
0495     pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
0496     pipeline->pipeline_id = swidget->pipeline_id;
0497     pipeline->comp_id = swidget->comp_id;
0498 
0499     swidget->private = pipeline;
0500 
0501     /* component at start of pipeline is our stream id */
0502     comp_swidget = snd_sof_find_swidget(scomp, swidget->widget->sname);
0503     if (!comp_swidget) {
0504         dev_err(scomp->dev, "scheduler %s refers to non existent widget %s\n",
0505             swidget->widget->name, swidget->widget->sname);
0506         ret = -EINVAL;
0507         goto err;
0508     }
0509 
0510     pipeline->sched_id = comp_swidget->comp_id;
0511 
0512     /* parse one set of scheduler tokens */
0513     ret = sof_update_ipc_object(scomp, pipeline, SOF_SCHED_TOKENS, swidget->tuples,
0514                     swidget->num_tuples, sizeof(*pipeline), 1);
0515     if (ret < 0)
0516         goto err;
0517 
0518     /* parse one set of pipeline tokens */
0519     ret = sof_update_ipc_object(scomp, swidget, SOF_PIPELINE_TOKENS, swidget->tuples,
0520                     swidget->num_tuples, sizeof(*swidget), 1);
0521     if (ret < 0)
0522         goto err;
0523 
0524     if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
0525         pipeline->core = SOF_DSP_PRIMARY_CORE;
0526 
0527     if (sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE))
0528         swidget->dynamic_pipeline_widget =
0529             sof_debug_check_flag(SOF_DBG_DYNAMIC_PIPELINES_ENABLE);
0530 
0531     dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d dynamic %d\n",
0532         swidget->widget->name, pipeline->period, pipeline->priority,
0533         pipeline->period_mips, pipeline->core, pipeline->frames_per_sched,
0534         swidget->dynamic_pipeline_widget);
0535 
0536     swidget->core = pipeline->core;
0537 
0538     return 0;
0539 
0540 err:
0541     kfree(swidget->private);
0542     swidget->private = NULL;
0543 
0544     return ret;
0545 }
0546 
0547 static int sof_ipc3_widget_setup_comp_buffer(struct snd_sof_widget *swidget)
0548 {
0549     struct snd_soc_component *scomp = swidget->scomp;
0550     struct sof_ipc_buffer *buffer;
0551     int ret;
0552 
0553     buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
0554     if (!buffer)
0555         return -ENOMEM;
0556 
0557     swidget->private = buffer;
0558 
0559     /* configure dai IPC message */
0560     buffer->comp.hdr.size = sizeof(*buffer);
0561     buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
0562     buffer->comp.id = swidget->comp_id;
0563     buffer->comp.type = SOF_COMP_BUFFER;
0564     buffer->comp.pipeline_id = swidget->pipeline_id;
0565     buffer->comp.core = swidget->core;
0566 
0567     /* parse one set of buffer tokens */
0568     ret = sof_update_ipc_object(scomp, buffer, SOF_BUFFER_TOKENS, swidget->tuples,
0569                     swidget->num_tuples, sizeof(*buffer), 1);
0570     if (ret < 0) {
0571         kfree(swidget->private);
0572         swidget->private = NULL;
0573         return ret;
0574     }
0575 
0576     dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
0577         swidget->widget->name, buffer->size, buffer->caps);
0578 
0579     return 0;
0580 }
0581 
0582 static int sof_ipc3_widget_setup_comp_src(struct snd_sof_widget *swidget)
0583 {
0584     struct snd_soc_component *scomp = swidget->scomp;
0585     struct sof_ipc_comp_src *src;
0586     size_t ipc_size = sizeof(*src);
0587     int ret;
0588 
0589     src = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0590     if (!src)
0591         return -ENOMEM;
0592 
0593     swidget->private = src;
0594 
0595     /* configure src IPC message */
0596     src->comp.type = SOF_COMP_SRC;
0597     src->config.hdr.size = sizeof(src->config);
0598 
0599     /* parse one set of src tokens */
0600     ret = sof_update_ipc_object(scomp, src, SOF_SRC_TOKENS, swidget->tuples,
0601                     swidget->num_tuples, sizeof(*src), 1);
0602     if (ret < 0)
0603         goto err;
0604 
0605     /* parse one set of comp tokens */
0606     ret = sof_update_ipc_object(scomp, &src->config, SOF_COMP_TOKENS,
0607                     swidget->tuples, swidget->num_tuples, sizeof(src->config), 1);
0608     if (ret < 0)
0609         goto err;
0610 
0611     dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
0612         swidget->widget->name, src->source_rate, src->sink_rate);
0613     sof_dbg_comp_config(scomp, &src->config);
0614 
0615     return 0;
0616 err:
0617     kfree(swidget->private);
0618     swidget->private = NULL;
0619 
0620     return ret;
0621 }
0622 
0623 static int sof_ipc3_widget_setup_comp_asrc(struct snd_sof_widget *swidget)
0624 {
0625     struct snd_soc_component *scomp = swidget->scomp;
0626     struct sof_ipc_comp_asrc *asrc;
0627     size_t ipc_size = sizeof(*asrc);
0628     int ret;
0629 
0630     asrc = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0631     if (!asrc)
0632         return -ENOMEM;
0633 
0634     swidget->private = asrc;
0635 
0636     /* configure ASRC IPC message */
0637     asrc->comp.type = SOF_COMP_ASRC;
0638     asrc->config.hdr.size = sizeof(asrc->config);
0639 
0640     /* parse one set of asrc tokens */
0641     ret = sof_update_ipc_object(scomp, asrc, SOF_ASRC_TOKENS, swidget->tuples,
0642                     swidget->num_tuples, sizeof(*asrc), 1);
0643     if (ret < 0)
0644         goto err;
0645 
0646     /* parse one set of comp tokens */
0647     ret = sof_update_ipc_object(scomp, &asrc->config, SOF_COMP_TOKENS,
0648                     swidget->tuples, swidget->num_tuples, sizeof(asrc->config), 1);
0649     if (ret < 0)
0650         goto err;
0651 
0652     dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d asynch %d operation %d\n",
0653         swidget->widget->name, asrc->source_rate, asrc->sink_rate,
0654         asrc->asynchronous_mode, asrc->operation_mode);
0655 
0656     sof_dbg_comp_config(scomp, &asrc->config);
0657 
0658     return 0;
0659 err:
0660     kfree(swidget->private);
0661     swidget->private = NULL;
0662 
0663     return ret;
0664 }
0665 
0666 /*
0667  * Mux topology
0668  */
0669 static int sof_ipc3_widget_setup_comp_mux(struct snd_sof_widget *swidget)
0670 {
0671     struct snd_soc_component *scomp = swidget->scomp;
0672     struct sof_ipc_comp_mux *mux;
0673     size_t ipc_size = sizeof(*mux);
0674     int ret;
0675 
0676     mux = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0677     if (!mux)
0678         return -ENOMEM;
0679 
0680     swidget->private = mux;
0681 
0682     /* configure mux IPC message */
0683     mux->comp.type = SOF_COMP_MUX;
0684     mux->config.hdr.size = sizeof(mux->config);
0685 
0686     /* parse one set of comp tokens */
0687     ret = sof_update_ipc_object(scomp, &mux->config, SOF_COMP_TOKENS,
0688                     swidget->tuples, swidget->num_tuples, sizeof(mux->config), 1);
0689     if (ret < 0) {
0690         kfree(swidget->private);
0691         swidget->private = NULL;
0692         return ret;
0693     }
0694 
0695     dev_dbg(scomp->dev, "loaded mux %s\n", swidget->widget->name);
0696     sof_dbg_comp_config(scomp, &mux->config);
0697 
0698     return 0;
0699 }
0700 
0701 /*
0702  * PGA Topology
0703  */
0704 
0705 static int sof_ipc3_widget_setup_comp_pga(struct snd_sof_widget *swidget)
0706 {
0707     struct snd_soc_component *scomp = swidget->scomp;
0708     struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
0709     struct sof_ipc_comp_volume *volume;
0710     struct snd_sof_control *scontrol;
0711     size_t ipc_size = sizeof(*volume);
0712     int min_step, max_step;
0713     int ret;
0714 
0715     volume = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0716     if (!volume)
0717         return -ENOMEM;
0718 
0719     swidget->private = volume;
0720 
0721     /* configure volume IPC message */
0722     volume->comp.type = SOF_COMP_VOLUME;
0723     volume->config.hdr.size = sizeof(volume->config);
0724 
0725     /* parse one set of volume tokens */
0726     ret = sof_update_ipc_object(scomp, volume, SOF_VOLUME_TOKENS, swidget->tuples,
0727                     swidget->num_tuples, sizeof(*volume), 1);
0728     if (ret < 0)
0729         goto err;
0730 
0731     /* parse one set of comp tokens */
0732     ret = sof_update_ipc_object(scomp, &volume->config, SOF_COMP_TOKENS,
0733                     swidget->tuples, swidget->num_tuples,
0734                     sizeof(volume->config), 1);
0735     if (ret < 0)
0736         goto err;
0737 
0738     dev_dbg(scomp->dev, "loaded PGA %s\n", swidget->widget->name);
0739     sof_dbg_comp_config(scomp, &volume->config);
0740 
0741     list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
0742         if (scontrol->comp_id == swidget->comp_id &&
0743             scontrol->volume_table) {
0744             min_step = scontrol->min_volume_step;
0745             max_step = scontrol->max_volume_step;
0746             volume->min_value = scontrol->volume_table[min_step];
0747             volume->max_value = scontrol->volume_table[max_step];
0748             volume->channels = scontrol->num_channels;
0749             break;
0750         }
0751     }
0752 
0753     return 0;
0754 err:
0755     kfree(swidget->private);
0756     swidget->private = NULL;
0757 
0758     return ret;
0759 }
0760 
0761 static int sof_get_control_data(struct snd_soc_component *scomp,
0762                 struct snd_soc_dapm_widget *widget,
0763                 struct sof_widget_data *wdata, size_t *size)
0764 {
0765     const struct snd_kcontrol_new *kc;
0766     struct sof_ipc_ctrl_data *cdata;
0767     struct soc_mixer_control *sm;
0768     struct soc_bytes_ext *sbe;
0769     struct soc_enum *se;
0770     int i;
0771 
0772     *size = 0;
0773 
0774     for (i = 0; i < widget->num_kcontrols; i++) {
0775         kc = &widget->kcontrol_news[i];
0776 
0777         switch (widget->dobj.widget.kcontrol_type[i]) {
0778         case SND_SOC_TPLG_TYPE_MIXER:
0779             sm = (struct soc_mixer_control *)kc->private_value;
0780             wdata[i].control = sm->dobj.private;
0781             break;
0782         case SND_SOC_TPLG_TYPE_BYTES:
0783             sbe = (struct soc_bytes_ext *)kc->private_value;
0784             wdata[i].control = sbe->dobj.private;
0785             break;
0786         case SND_SOC_TPLG_TYPE_ENUM:
0787             se = (struct soc_enum *)kc->private_value;
0788             wdata[i].control = se->dobj.private;
0789             break;
0790         default:
0791             dev_err(scomp->dev, "Unknown kcontrol type %u in widget %s\n",
0792                 widget->dobj.widget.kcontrol_type[i], widget->name);
0793             return -EINVAL;
0794         }
0795 
0796         if (!wdata[i].control) {
0797             dev_err(scomp->dev, "No scontrol for widget %s\n", widget->name);
0798             return -EINVAL;
0799         }
0800 
0801         cdata = wdata[i].control->ipc_control_data;
0802 
0803         if (widget->dobj.widget.kcontrol_type[i] == SND_SOC_TPLG_TYPE_BYTES) {
0804             /* make sure data is valid - data can be updated at runtime */
0805             if (cdata->data->magic != SOF_ABI_MAGIC)
0806                 return -EINVAL;
0807 
0808             wdata[i].pdata = cdata->data->data;
0809             wdata[i].pdata_size = cdata->data->size;
0810         } else {
0811             /* points to the control data union */
0812             wdata[i].pdata = cdata->chanv;
0813             /*
0814              * wdata[i].control->size is calculated with struct_size
0815              * and includes the size of struct sof_ipc_ctrl_data
0816              */
0817             wdata[i].pdata_size = wdata[i].control->size -
0818                           sizeof(struct sof_ipc_ctrl_data);
0819         }
0820 
0821         *size += wdata[i].pdata_size;
0822 
0823         /* get data type */
0824         switch (cdata->cmd) {
0825         case SOF_CTRL_CMD_VOLUME:
0826         case SOF_CTRL_CMD_ENUM:
0827         case SOF_CTRL_CMD_SWITCH:
0828             wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
0829             wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
0830             break;
0831         case SOF_CTRL_CMD_BINARY:
0832             wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
0833             wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
0834             break;
0835         default:
0836             break;
0837         }
0838     }
0839 
0840     return 0;
0841 }
0842 
0843 static int sof_process_load(struct snd_soc_component *scomp,
0844                 struct snd_sof_widget *swidget, int type)
0845 {
0846     struct snd_soc_dapm_widget *widget = swidget->widget;
0847     struct sof_ipc_comp_process *process;
0848     struct sof_widget_data *wdata = NULL;
0849     size_t ipc_data_size = 0;
0850     size_t ipc_size;
0851     int offset = 0;
0852     int ret;
0853     int i;
0854 
0855     /* allocate struct for widget control data sizes and types */
0856     if (widget->num_kcontrols) {
0857         wdata = kcalloc(widget->num_kcontrols, sizeof(*wdata), GFP_KERNEL);
0858         if (!wdata)
0859             return -ENOMEM;
0860 
0861         /* get possible component controls and get size of all pdata */
0862         ret = sof_get_control_data(scomp, widget, wdata, &ipc_data_size);
0863         if (ret < 0)
0864             goto out;
0865     }
0866 
0867     ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
0868 
0869     /* we are exceeding max ipc size, config needs to be sent separately */
0870     if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
0871         ipc_size -= ipc_data_size;
0872         ipc_data_size = 0;
0873     }
0874 
0875     process = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
0876     if (!process) {
0877         ret = -ENOMEM;
0878         goto out;
0879     }
0880 
0881     swidget->private = process;
0882 
0883     /* configure iir IPC message */
0884     process->comp.type = type;
0885     process->config.hdr.size = sizeof(process->config);
0886 
0887     /* parse one set of comp tokens */
0888     ret = sof_update_ipc_object(scomp, &process->config, SOF_COMP_TOKENS,
0889                     swidget->tuples, swidget->num_tuples,
0890                     sizeof(process->config), 1);
0891     if (ret < 0)
0892         goto err;
0893 
0894     dev_dbg(scomp->dev, "loaded process %s\n", swidget->widget->name);
0895     sof_dbg_comp_config(scomp, &process->config);
0896 
0897     /*
0898      * found private data in control, so copy it.
0899      * get possible component controls - get size of all pdata,
0900      * then memcpy with headers
0901      */
0902     if (ipc_data_size) {
0903         for (i = 0; i < widget->num_kcontrols; i++) {
0904             if (!wdata[i].pdata_size)
0905                 continue;
0906 
0907             memcpy(&process->data[offset], wdata[i].pdata,
0908                    wdata[i].pdata_size);
0909             offset += wdata[i].pdata_size;
0910         }
0911     }
0912 
0913     process->size = ipc_data_size;
0914 
0915     kfree(wdata);
0916 
0917     return 0;
0918 err:
0919     kfree(swidget->private);
0920     swidget->private = NULL;
0921 out:
0922     kfree(wdata);
0923     return ret;
0924 }
0925 
0926 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
0927 {
0928     int i;
0929 
0930     for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
0931         if (sof_process[i].type == type)
0932             return sof_process[i].comp_type;
0933     }
0934 
0935     return SOF_COMP_NONE;
0936 }
0937 
0938 /*
0939  * Processing Component Topology - can be "effect", "codec", or general
0940  * "processing".
0941  */
0942 
0943 static int sof_widget_update_ipc_comp_process(struct snd_sof_widget *swidget)
0944 {
0945     struct snd_soc_component *scomp = swidget->scomp;
0946     struct sof_ipc_comp_process config;
0947     int ret;
0948 
0949     memset(&config, 0, sizeof(config));
0950     config.comp.core = swidget->core;
0951 
0952     /* parse one set of process tokens */
0953     ret = sof_update_ipc_object(scomp, &config, SOF_PROCESS_TOKENS, swidget->tuples,
0954                     swidget->num_tuples, sizeof(config), 1);
0955     if (ret < 0)
0956         return ret;
0957 
0958     /* now load process specific data and send IPC */
0959     return sof_process_load(scomp, swidget, find_process_comp_type(config.type));
0960 }
0961 
0962 static int sof_link_hda_load(struct snd_soc_component *scomp, struct snd_sof_dai_link *slink,
0963                  struct sof_ipc_dai_config *config, struct snd_sof_dai *dai)
0964 {
0965     struct sof_dai_private_data *private = dai->private;
0966     u32 size = sizeof(*config);
0967     int ret;
0968 
0969     /* init IPC */
0970     memset(&config->hda, 0, sizeof(config->hda));
0971     config->hdr.size = size;
0972 
0973     /* parse one set of HDA tokens */
0974     ret = sof_update_ipc_object(scomp, &config->hda, SOF_HDA_TOKENS, slink->tuples,
0975                     slink->num_tuples, size, 1);
0976     if (ret < 0)
0977         return ret;
0978 
0979     dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
0980         config->hda.rate, config->hda.channels);
0981 
0982     config->hda.link_dma_ch = DMA_CHAN_INVALID;
0983 
0984     dai->number_configs = 1;
0985     dai->current_config = 0;
0986     private->dai_config = kmemdup(config, size, GFP_KERNEL);
0987     if (!private->dai_config)
0988         return -ENOMEM;
0989 
0990     return 0;
0991 }
0992 
0993 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
0994                    struct sof_ipc_dai_config *config)
0995 {
0996     /* clock directions wrt codec */
0997     config->format &= ~SOF_DAI_FMT_CLOCK_PROVIDER_MASK;
0998     if (hw_config->bclk_provider == SND_SOC_TPLG_BCLK_CP) {
0999         /* 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     scontrol->ipc_control_data = NULL;
1648     return ret;
1649 }
1650 
1651 static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1652 {
1653     struct sof_ipc_ctrl_data *cdata;
1654     int i;
1655 
1656     /* init the volume get/put data */
1657     scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1658 
1659     scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1660     if (!scontrol->ipc_control_data)
1661         return -ENOMEM;
1662 
1663     cdata = scontrol->ipc_control_data;
1664     cdata->index = scontrol->index;
1665 
1666     /* set cmd for mixer control */
1667     if (scontrol->max == 1) {
1668         cdata->cmd = SOF_CTRL_CMD_SWITCH;
1669         return 0;
1670     }
1671 
1672     cdata->cmd = SOF_CTRL_CMD_VOLUME;
1673 
1674     /* set default volume values to 0dB in control */
1675     for (i = 0; i < scontrol->num_channels; i++) {
1676         cdata->chanv[i].channel = i;
1677         cdata->chanv[i].value = VOL_ZERO_DB;
1678     }
1679 
1680     return 0;
1681 }
1682 
1683 static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1684 {
1685     struct sof_ipc_ctrl_data *cdata;
1686 
1687     /* init the enum get/put data */
1688     scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1689 
1690     scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1691     if (!scontrol->ipc_control_data)
1692         return -ENOMEM;
1693 
1694     cdata = scontrol->ipc_control_data;
1695     cdata->index = scontrol->index;
1696     cdata->cmd = SOF_CTRL_CMD_ENUM;
1697 
1698     return 0;
1699 }
1700 
1701 static int sof_ipc3_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1702 {
1703     switch (scontrol->info_type) {
1704     case SND_SOC_TPLG_CTL_VOLSW:
1705     case SND_SOC_TPLG_CTL_VOLSW_SX:
1706     case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1707         return sof_ipc3_control_load_volume(sdev, scontrol);
1708     case SND_SOC_TPLG_CTL_BYTES:
1709         return sof_ipc3_control_load_bytes(sdev, scontrol);
1710     case SND_SOC_TPLG_CTL_ENUM:
1711     case SND_SOC_TPLG_CTL_ENUM_VALUE:
1712         return sof_ipc3_control_load_enum(sdev, scontrol);
1713     default:
1714         break;
1715     }
1716 
1717     return 0;
1718 }
1719 
1720 static int sof_ipc3_control_free(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1721 {
1722     struct sof_ipc_free fcomp;
1723 
1724     fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1725     fcomp.hdr.size = sizeof(fcomp);
1726     fcomp.id = scontrol->comp_id;
1727 
1728     /* send IPC to the DSP */
1729     return sof_ipc_tx_message(sdev->ipc, &fcomp, sizeof(fcomp), NULL, 0);
1730 }
1731 
1732 /* send pcm params ipc */
1733 static int sof_ipc3_keyword_detect_pcm_params(struct snd_sof_widget *swidget, int dir)
1734 {
1735     struct snd_soc_component *scomp = swidget->scomp;
1736     struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1737     struct sof_ipc_pcm_params_reply ipc_params_reply;
1738     struct snd_pcm_hw_params *params;
1739     struct sof_ipc_pcm_params pcm;
1740     struct snd_sof_pcm *spcm;
1741     int ret;
1742 
1743     /* get runtime PCM params using widget's stream name */
1744     spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1745     if (!spcm) {
1746         dev_err(scomp->dev, "Cannot find PCM for %s\n", swidget->widget->name);
1747         return -EINVAL;
1748     }
1749 
1750     params = &spcm->params[dir];
1751 
1752     /* set IPC PCM params */
1753     memset(&pcm, 0, sizeof(pcm));
1754     pcm.hdr.size = sizeof(pcm);
1755     pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
1756     pcm.comp_id = swidget->comp_id;
1757     pcm.params.hdr.size = sizeof(pcm.params);
1758     pcm.params.direction = dir;
1759     pcm.params.sample_valid_bytes = params_width(params) >> 3;
1760     pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
1761     pcm.params.rate = params_rate(params);
1762     pcm.params.channels = params_channels(params);
1763     pcm.params.host_period_bytes = params_period_bytes(params);
1764 
1765     /* set format */
1766     switch (params_format(params)) {
1767     case SNDRV_PCM_FORMAT_S16:
1768         pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
1769         break;
1770     case SNDRV_PCM_FORMAT_S24:
1771         pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
1772         break;
1773     case SNDRV_PCM_FORMAT_S32:
1774         pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
1775         break;
1776     default:
1777         return -EINVAL;
1778     }
1779 
1780     /* send IPC to the DSP */
1781     ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm),
1782                  &ipc_params_reply, sizeof(ipc_params_reply));
1783     if (ret < 0)
1784         dev_err(scomp->dev, "%s: PCM params failed for %s\n", __func__,
1785             swidget->widget->name);
1786 
1787     return ret;
1788 }
1789 
1790  /* send stream trigger ipc */
1791 static int sof_ipc3_keyword_detect_trigger(struct snd_sof_widget *swidget, int cmd)
1792 {
1793     struct snd_soc_component *scomp = swidget->scomp;
1794     struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1795     struct sof_ipc_stream stream;
1796     struct sof_ipc_reply reply;
1797     int ret;
1798 
1799     /* set IPC stream params */
1800     stream.hdr.size = sizeof(stream);
1801     stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
1802     stream.comp_id = swidget->comp_id;
1803 
1804     /* send IPC to the DSP */
1805     ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
1806     if (ret < 0)
1807         dev_err(scomp->dev, "%s: Failed to trigger %s\n", __func__, swidget->widget->name);
1808 
1809     return ret;
1810 }
1811 
1812 static int sof_ipc3_keyword_dapm_event(struct snd_soc_dapm_widget *w,
1813                        struct snd_kcontrol *k, int event)
1814 {
1815     struct snd_sof_widget *swidget = w->dobj.private;
1816     struct snd_soc_component *scomp;
1817     int stream = SNDRV_PCM_STREAM_CAPTURE;
1818     struct snd_sof_pcm *spcm;
1819     int ret = 0;
1820 
1821     if (!swidget)
1822         return 0;
1823 
1824     scomp = swidget->scomp;
1825 
1826     dev_dbg(scomp->dev, "received event %d for widget %s\n",
1827         event, w->name);
1828 
1829     /* get runtime PCM params using widget's stream name */
1830     spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1831     if (!spcm) {
1832         dev_err(scomp->dev, "%s: Cannot find PCM for %s\n", __func__,
1833             swidget->widget->name);
1834         return -EINVAL;
1835     }
1836 
1837     /* process events */
1838     switch (event) {
1839     case SND_SOC_DAPM_PRE_PMU:
1840         if (spcm->stream[stream].suspend_ignored) {
1841             dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
1842             return 0;
1843         }
1844 
1845         /* set pcm params */
1846         ret = sof_ipc3_keyword_detect_pcm_params(swidget, stream);
1847         if (ret < 0) {
1848             dev_err(scomp->dev, "%s: Failed to set pcm params for widget %s\n",
1849                 __func__, swidget->widget->name);
1850             break;
1851         }
1852 
1853         /* start trigger */
1854         ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
1855         if (ret < 0)
1856             dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1857                 swidget->widget->name);
1858         break;
1859     case SND_SOC_DAPM_POST_PMD:
1860         if (spcm->stream[stream].suspend_ignored) {
1861             dev_dbg(scomp->dev,
1862                 "POST_PMD event ignored, KWD pipeline will remain RUNNING\n");
1863             return 0;
1864         }
1865 
1866         /* stop trigger */
1867         ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
1868         if (ret < 0)
1869             dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1870                 swidget->widget->name);
1871 
1872         /* pcm free */
1873         ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
1874         if (ret < 0)
1875             dev_err(scomp->dev, "%s: Failed to free PCM for widget %s\n", __func__,
1876                 swidget->widget->name);
1877         break;
1878     default:
1879         break;
1880     }
1881 
1882     return ret;
1883 }
1884 
1885 /* event handlers for keyword detect component */
1886 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
1887     {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_ipc3_keyword_dapm_event},
1888 };
1889 
1890 static int sof_ipc3_widget_bind_event(struct snd_soc_component *scomp,
1891                       struct snd_sof_widget *swidget, u16 event_type)
1892 {
1893     struct sof_ipc_comp *ipc_comp;
1894 
1895     /* validate widget event type */
1896     switch (event_type) {
1897     case SOF_KEYWORD_DETECT_DAPM_EVENT:
1898         /* only KEYWORD_DETECT comps should handle this */
1899         if (swidget->id != snd_soc_dapm_effect)
1900             break;
1901 
1902         ipc_comp = swidget->private;
1903         if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
1904             break;
1905 
1906         /* bind event to keyword detect comp */
1907         return snd_soc_tplg_widget_bind_event(swidget->widget, sof_kwd_events,
1908                               ARRAY_SIZE(sof_kwd_events), event_type);
1909     default:
1910         break;
1911     }
1912 
1913     dev_err(scomp->dev, "Invalid event type %d for widget %s\n", event_type,
1914         swidget->widget->name);
1915 
1916     return -EINVAL;
1917 }
1918 
1919 static int sof_ipc3_complete_pipeline(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1920 {
1921     struct sof_ipc_pipe_ready ready;
1922     struct sof_ipc_reply reply;
1923     int ret;
1924 
1925     dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
1926         swidget->widget->name, swidget->comp_id);
1927 
1928     memset(&ready, 0, sizeof(ready));
1929     ready.hdr.size = sizeof(ready);
1930     ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
1931     ready.comp_id = swidget->comp_id;
1932 
1933     ret = sof_ipc_tx_message(sdev->ipc, &ready, sizeof(ready), &reply, sizeof(reply));
1934     if (ret < 0)
1935         return ret;
1936 
1937     return 1;
1938 }
1939 
1940 static int sof_ipc3_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1941 {
1942     struct sof_ipc_free ipc_free = {
1943         .hdr = {
1944             .size = sizeof(ipc_free),
1945             .cmd = SOF_IPC_GLB_TPLG_MSG,
1946         },
1947         .id = swidget->comp_id,
1948     };
1949     struct sof_ipc_reply reply;
1950     int ret;
1951 
1952     if (!swidget->private)
1953         return 0;
1954 
1955     switch (swidget->id) {
1956     case snd_soc_dapm_scheduler:
1957     {
1958         ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE;
1959         break;
1960     }
1961     case snd_soc_dapm_buffer:
1962         ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE;
1963         break;
1964     default:
1965         ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE;
1966         break;
1967     }
1968 
1969     ret = sof_ipc_tx_message(sdev->ipc, &ipc_free, sizeof(ipc_free),
1970                  &reply, sizeof(reply));
1971     if (ret < 0)
1972         dev_err(sdev->dev, "failed to free widget %s\n", swidget->widget->name);
1973 
1974     return ret;
1975 }
1976 
1977 static int sof_ipc3_dai_config(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
1978                    unsigned int flags, struct snd_sof_dai_config_data *data)
1979 {
1980     struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
1981     struct snd_sof_dai *dai = swidget->private;
1982     struct sof_dai_private_data *private;
1983     struct sof_ipc_dai_config *config;
1984     struct sof_ipc_reply reply;
1985     int ret = 0;
1986 
1987     if (!dai || !dai->private) {
1988         dev_err(sdev->dev, "No private data for DAI %s\n", swidget->widget->name);
1989         return -EINVAL;
1990     }
1991 
1992     private = dai->private;
1993     if (!private->dai_config) {
1994         dev_err(sdev->dev, "No config for DAI %s\n", dai->name);
1995         return -EINVAL;
1996     }
1997 
1998     config = &private->dai_config[dai->current_config];
1999     if (!config) {
2000         dev_err(sdev->dev, "Invalid current config for DAI %s\n", dai->name);
2001         return -EINVAL;
2002     }
2003 
2004     switch (config->type) {
2005     case SOF_DAI_INTEL_SSP:
2006         /*
2007          * DAI_CONFIG IPC during hw_params/hw_free for SSP DAI's is not supported in older
2008          * firmware
2009          */
2010         if (v->abi_version < SOF_ABI_VER(3, 18, 0) &&
2011             ((flags & SOF_DAI_CONFIG_FLAGS_HW_PARAMS) ||
2012              (flags & SOF_DAI_CONFIG_FLAGS_HW_FREE)))
2013             return 0;
2014         break;
2015     case SOF_DAI_INTEL_HDA:
2016         if (data)
2017             config->hda.link_dma_ch = data->dai_data;
2018         break;
2019     case SOF_DAI_INTEL_ALH:
2020         if (data) {
2021             config->dai_index = data->dai_index;
2022             config->alh.stream_id = data->dai_data;
2023         }
2024         break;
2025     default:
2026         break;
2027     }
2028 
2029     config->flags = flags;
2030 
2031     /* only send the IPC if the widget is set up in the DSP */
2032     if (swidget->use_count > 0) {
2033         ret = sof_ipc_tx_message(sdev->ipc, config, config->hdr.size,
2034                      &reply, sizeof(reply));
2035         if (ret < 0)
2036             dev_err(sdev->dev, "Failed to set dai config for %s\n", dai->name);
2037     }
2038 
2039     return ret;
2040 }
2041 
2042 static int sof_ipc3_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2043 {
2044     struct sof_ipc_comp_reply reply;
2045     int ret;
2046 
2047     if (!swidget->private)
2048         return 0;
2049 
2050     switch (swidget->id) {
2051     case snd_soc_dapm_dai_in:
2052     case snd_soc_dapm_dai_out:
2053     {
2054         struct snd_sof_dai *dai = swidget->private;
2055         struct sof_dai_private_data *dai_data = dai->private;
2056         struct sof_ipc_comp *comp = &dai_data->comp_dai->comp;
2057 
2058         ret = sof_ipc_tx_message(sdev->ipc, dai_data->comp_dai,
2059                      comp->hdr.size, &reply, sizeof(reply));
2060         break;
2061     }
2062     case snd_soc_dapm_scheduler:
2063     {
2064         struct sof_ipc_pipe_new *pipeline;
2065 
2066         pipeline = swidget->private;
2067         ret = sof_ipc_tx_message(sdev->ipc, pipeline, sizeof(*pipeline),
2068                      &reply, sizeof(reply));
2069         break;
2070     }
2071     default:
2072     {
2073         struct sof_ipc_cmd_hdr *hdr;
2074 
2075         hdr = swidget->private;
2076         ret = sof_ipc_tx_message(sdev->ipc, swidget->private, hdr->size,
2077                      &reply, sizeof(reply));
2078         break;
2079     }
2080     }
2081     if (ret < 0)
2082         dev_err(sdev->dev, "Failed to setup widget %s\n", swidget->widget->name);
2083 
2084     return ret;
2085 }
2086 
2087 static int sof_ipc3_set_up_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2088 {
2089     struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2090     struct snd_sof_widget *swidget;
2091     struct snd_sof_route *sroute;
2092     int ret;
2093 
2094     /* restore pipeline components */
2095     list_for_each_entry(swidget, &sdev->widget_list, list) {
2096         /* only set up the widgets belonging to static pipelines */
2097         if (!verify && swidget->dynamic_pipeline_widget)
2098             continue;
2099 
2100         /*
2101          * For older firmware, skip scheduler widgets in this loop,
2102          * sof_widget_setup() will be called in the 'complete pipeline' loop
2103          */
2104         if (v->abi_version < SOF_ABI_VER(3, 19, 0) &&
2105             swidget->id == snd_soc_dapm_scheduler)
2106             continue;
2107 
2108         /* update DAI config. The IPC will be sent in sof_widget_setup() */
2109         if (WIDGET_IS_DAI(swidget->id)) {
2110             struct snd_sof_dai *dai = swidget->private;
2111             struct sof_dai_private_data *private;
2112             struct sof_ipc_dai_config *config;
2113 
2114             if (!dai || !dai->private)
2115                 continue;
2116             private = dai->private;
2117             if (!private->dai_config)
2118                 continue;
2119 
2120             config = private->dai_config;
2121             /*
2122              * The link DMA channel would be invalidated for running
2123              * streams but not for streams that were in the PAUSED
2124              * state during suspend. So invalidate it here before setting
2125              * the dai config in the DSP.
2126              */
2127             if (config->type == SOF_DAI_INTEL_HDA)
2128                 config->hda.link_dma_ch = DMA_CHAN_INVALID;
2129         }
2130 
2131         ret = sof_widget_setup(sdev, swidget);
2132         if (ret < 0)
2133             return ret;
2134     }
2135 
2136     /* restore pipeline connections */
2137     list_for_each_entry(sroute, &sdev->route_list, list) {
2138         /* only set up routes belonging to static pipelines */
2139         if (!verify && (sroute->src_widget->dynamic_pipeline_widget ||
2140                 sroute->sink_widget->dynamic_pipeline_widget))
2141             continue;
2142 
2143         /*
2144          * For virtual routes, both sink and source are not buffer. IPC3 only supports
2145          * connections between a buffer and a component. Ignore the rest.
2146          */
2147         if (sroute->src_widget->id != snd_soc_dapm_buffer &&
2148             sroute->sink_widget->id != snd_soc_dapm_buffer)
2149             continue;
2150 
2151         ret = sof_route_setup(sdev, sroute->src_widget->widget,
2152                       sroute->sink_widget->widget);
2153         if (ret < 0) {
2154             dev_err(sdev->dev, "%s: route set up failed\n", __func__);
2155             return ret;
2156         }
2157     }
2158 
2159     /* complete pipeline */
2160     list_for_each_entry(swidget, &sdev->widget_list, list) {
2161         switch (swidget->id) {
2162         case snd_soc_dapm_scheduler:
2163             /* only complete static pipelines */
2164             if (!verify && swidget->dynamic_pipeline_widget)
2165                 continue;
2166 
2167             if (v->abi_version < SOF_ABI_VER(3, 19, 0)) {
2168                 ret = sof_widget_setup(sdev, swidget);
2169                 if (ret < 0)
2170                     return ret;
2171             }
2172 
2173             swidget->complete = sof_ipc3_complete_pipeline(sdev, swidget);
2174             if (swidget->complete < 0)
2175                 return swidget->complete;
2176             break;
2177         default:
2178             break;
2179         }
2180     }
2181 
2182     return 0;
2183 }
2184 
2185 /*
2186  * Free the PCM, its associated widgets and set the prepared flag to false for all PCMs that
2187  * did not get suspended(ex: paused streams) so the widgets can be set up again during resume.
2188  */
2189 static int sof_tear_down_left_over_pipelines(struct snd_sof_dev *sdev)
2190 {
2191     struct snd_sof_widget *swidget;
2192     struct snd_sof_pcm *spcm;
2193     int dir, ret;
2194 
2195     /*
2196      * free all PCMs and their associated DAPM widgets if their connected DAPM widget
2197      * list is not NULL. This should only be true for paused streams at this point.
2198      * This is equivalent to the handling of FE DAI suspend trigger for running streams.
2199      */
2200     list_for_each_entry(spcm, &sdev->pcm_list, list) {
2201         for_each_pcm_streams(dir) {
2202             struct snd_pcm_substream *substream = spcm->stream[dir].substream;
2203 
2204             if (!substream || !substream->runtime)
2205                 continue;
2206 
2207             if (spcm->stream[dir].list) {
2208                 ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true);
2209                 if (ret < 0)
2210                     return ret;
2211             }
2212         }
2213     }
2214 
2215     /*
2216      * free any left over DAI widgets. This is equivalent to the handling of suspend trigger
2217      * for the BE DAI for running streams.
2218      */
2219     list_for_each_entry(swidget, &sdev->widget_list, list)
2220         if (WIDGET_IS_DAI(swidget->id) && swidget->use_count == 1) {
2221             ret = sof_widget_free(sdev, swidget);
2222             if (ret < 0)
2223                 return ret;
2224         }
2225 
2226     return 0;
2227 }
2228 
2229 /*
2230  * For older firmware, this function doesn't free widgets for static pipelines during suspend.
2231  * It only resets use_count for all widgets.
2232  */
2233 static int sof_ipc3_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2234 {
2235     struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2236     struct snd_sof_widget *swidget;
2237     struct snd_sof_route *sroute;
2238     int ret;
2239 
2240     /*
2241      * This function is called during suspend and for one-time topology verification during
2242      * first boot. In both cases, there is no need to protect swidget->use_count and
2243      * sroute->setup because during suspend all running streams are suspended and during
2244      * topology loading the sound card unavailable to open PCMs.
2245      */
2246     list_for_each_entry(swidget, &sdev->widget_list, list) {
2247         if (swidget->dynamic_pipeline_widget)
2248             continue;
2249 
2250         /* Do not free widgets for static pipelines with FW ABI older than 3.19 */
2251         if (!verify && !swidget->dynamic_pipeline_widget &&
2252             v->abi_version < SOF_ABI_VER(3, 19, 0)) {
2253             swidget->use_count = 0;
2254             swidget->complete = 0;
2255             continue;
2256         }
2257 
2258         ret = sof_widget_free(sdev, swidget);
2259         if (ret < 0)
2260             return ret;
2261     }
2262 
2263     /*
2264      * Tear down all pipelines associated with PCMs that did not get suspended
2265      * and unset the prepare flag so that they can be set up again during resume.
2266      * Skip this step for older firmware.
2267      */
2268     if (!verify && v->abi_version >= SOF_ABI_VER(3, 19, 0)) {
2269         ret = sof_tear_down_left_over_pipelines(sdev);
2270         if (ret < 0) {
2271             dev_err(sdev->dev, "failed to tear down paused pipelines\n");
2272             return ret;
2273         }
2274     }
2275 
2276     list_for_each_entry(sroute, &sdev->route_list, list)
2277         sroute->setup = false;
2278 
2279     /*
2280      * before suspending, make sure the refcounts are all zeroed out. There's no way
2281      * to recover at this point but this will help root cause bad sequences leading to
2282      * more issues on resume
2283      */
2284     list_for_each_entry(swidget, &sdev->widget_list, list) {
2285         if (swidget->use_count != 0) {
2286             dev_err(sdev->dev, "%s: widget %s is still in use: count %d\n",
2287                 __func__, swidget->widget->name, swidget->use_count);
2288         }
2289     }
2290 
2291     return 0;
2292 }
2293 
2294 static int sof_ipc3_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type)
2295 {
2296     struct sof_dai_private_data *private = dai->private;
2297 
2298     if (!private || !private->dai_config)
2299         return 0;
2300 
2301     switch (private->dai_config->type) {
2302     case SOF_DAI_INTEL_SSP:
2303         switch (clk_type) {
2304         case SOF_DAI_CLK_INTEL_SSP_MCLK:
2305             return private->dai_config->ssp.mclk_rate;
2306         case SOF_DAI_CLK_INTEL_SSP_BCLK:
2307             return private->dai_config->ssp.bclk_rate;
2308         default:
2309             break;
2310         }
2311         dev_err(sdev->dev, "fail to get SSP clk %d rate\n", clk_type);
2312         break;
2313     default:
2314         /* not yet implemented for platforms other than the above */
2315         dev_err(sdev->dev, "DAI type %d not supported yet!\n", private->dai_config->type);
2316         break;
2317     }
2318 
2319     return -EINVAL;
2320 }
2321 
2322 static int sof_ipc3_parse_manifest(struct snd_soc_component *scomp, int index,
2323                    struct snd_soc_tplg_manifest *man)
2324 {
2325     u32 size = le32_to_cpu(man->priv.size);
2326     u32 abi_version;
2327 
2328     /* backward compatible with tplg without ABI info */
2329     if (!size) {
2330         dev_dbg(scomp->dev, "No topology ABI info\n");
2331         return 0;
2332     }
2333 
2334     if (size != SOF_IPC3_TPLG_ABI_SIZE) {
2335         dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n",
2336             __func__, size);
2337         return -EINVAL;
2338     }
2339 
2340     dev_info(scomp->dev,
2341          "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
2342          man->priv.data[0], man->priv.data[1], man->priv.data[2],
2343          SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
2344 
2345     abi_version = SOF_ABI_VER(man->priv.data[0], man->priv.data[1], man->priv.data[2]);
2346 
2347     if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
2348         dev_err(scomp->dev, "%s: Incompatible topology ABI version\n", __func__);
2349         return -EINVAL;
2350     }
2351 
2352     if (IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS) &&
2353         SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
2354         dev_err(scomp->dev, "%s: Topology ABI is more recent than kernel\n", __func__);
2355         return -EINVAL;
2356     }
2357 
2358     return 0;
2359 }
2360 
2361 /* token list for each topology object */
2362 static enum sof_tokens host_token_list[] = {
2363     SOF_CORE_TOKENS,
2364     SOF_COMP_EXT_TOKENS,
2365     SOF_PCM_TOKENS,
2366     SOF_COMP_TOKENS,
2367 };
2368 
2369 static enum sof_tokens comp_generic_token_list[] = {
2370     SOF_CORE_TOKENS,
2371     SOF_COMP_EXT_TOKENS,
2372     SOF_COMP_TOKENS,
2373 };
2374 
2375 static enum sof_tokens buffer_token_list[] = {
2376     SOF_BUFFER_TOKENS,
2377 };
2378 
2379 static enum sof_tokens pipeline_token_list[] = {
2380     SOF_CORE_TOKENS,
2381     SOF_COMP_EXT_TOKENS,
2382     SOF_PIPELINE_TOKENS,
2383     SOF_SCHED_TOKENS,
2384 };
2385 
2386 static enum sof_tokens asrc_token_list[] = {
2387     SOF_CORE_TOKENS,
2388     SOF_COMP_EXT_TOKENS,
2389     SOF_ASRC_TOKENS,
2390     SOF_COMP_TOKENS,
2391 };
2392 
2393 static enum sof_tokens src_token_list[] = {
2394     SOF_CORE_TOKENS,
2395     SOF_COMP_EXT_TOKENS,
2396     SOF_SRC_TOKENS,
2397     SOF_COMP_TOKENS
2398 };
2399 
2400 static enum sof_tokens pga_token_list[] = {
2401     SOF_CORE_TOKENS,
2402     SOF_COMP_EXT_TOKENS,
2403     SOF_VOLUME_TOKENS,
2404     SOF_COMP_TOKENS,
2405 };
2406 
2407 static enum sof_tokens dai_token_list[] = {
2408     SOF_CORE_TOKENS,
2409     SOF_COMP_EXT_TOKENS,
2410     SOF_DAI_TOKENS,
2411     SOF_COMP_TOKENS,
2412 };
2413 
2414 static enum sof_tokens process_token_list[] = {
2415     SOF_CORE_TOKENS,
2416     SOF_COMP_EXT_TOKENS,
2417     SOF_PROCESS_TOKENS,
2418     SOF_COMP_TOKENS,
2419 };
2420 
2421 static const struct sof_ipc_tplg_widget_ops tplg_ipc3_widget_ops[SND_SOC_DAPM_TYPE_COUNT] = {
2422     [snd_soc_dapm_aif_in] =  {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2423                   host_token_list, ARRAY_SIZE(host_token_list), NULL},
2424     [snd_soc_dapm_aif_out] = {sof_ipc3_widget_setup_comp_host, sof_ipc3_widget_free_comp,
2425                   host_token_list, ARRAY_SIZE(host_token_list), NULL},
2426 
2427     [snd_soc_dapm_dai_in] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2428                  dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2429     [snd_soc_dapm_dai_out] = {sof_ipc3_widget_setup_comp_dai, sof_ipc3_widget_free_comp_dai,
2430                   dai_token_list, ARRAY_SIZE(dai_token_list), NULL},
2431     [snd_soc_dapm_buffer] = {sof_ipc3_widget_setup_comp_buffer, sof_ipc3_widget_free_comp,
2432                  buffer_token_list, ARRAY_SIZE(buffer_token_list), NULL},
2433     [snd_soc_dapm_mixer] = {sof_ipc3_widget_setup_comp_mixer, sof_ipc3_widget_free_comp,
2434                 comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2435                 NULL},
2436     [snd_soc_dapm_src] = {sof_ipc3_widget_setup_comp_src, sof_ipc3_widget_free_comp,
2437                   src_token_list, ARRAY_SIZE(src_token_list), NULL},
2438     [snd_soc_dapm_asrc] = {sof_ipc3_widget_setup_comp_asrc, sof_ipc3_widget_free_comp,
2439                    asrc_token_list, ARRAY_SIZE(asrc_token_list), NULL},
2440     [snd_soc_dapm_siggen] = {sof_ipc3_widget_setup_comp_tone, sof_ipc3_widget_free_comp,
2441                  comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2442                  NULL},
2443     [snd_soc_dapm_scheduler] = {sof_ipc3_widget_setup_comp_pipeline, sof_ipc3_widget_free_comp,
2444                     pipeline_token_list, ARRAY_SIZE(pipeline_token_list), NULL},
2445     [snd_soc_dapm_pga] = {sof_ipc3_widget_setup_comp_pga, sof_ipc3_widget_free_comp,
2446                   pga_token_list, ARRAY_SIZE(pga_token_list), NULL},
2447     [snd_soc_dapm_mux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2448                   comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list), NULL},
2449     [snd_soc_dapm_demux] = {sof_ipc3_widget_setup_comp_mux, sof_ipc3_widget_free_comp,
2450                  comp_generic_token_list, ARRAY_SIZE(comp_generic_token_list),
2451                  NULL},
2452     [snd_soc_dapm_effect] = {sof_widget_update_ipc_comp_process, sof_ipc3_widget_free_comp,
2453                  process_token_list, ARRAY_SIZE(process_token_list),
2454                  sof_ipc3_widget_bind_event},
2455 };
2456 
2457 const struct sof_ipc_tplg_ops ipc3_tplg_ops = {
2458     .widget = tplg_ipc3_widget_ops,
2459     .control = &tplg_ipc3_control_ops,
2460     .route_setup = sof_ipc3_route_setup,
2461     .control_setup = sof_ipc3_control_setup,
2462     .control_free = sof_ipc3_control_free,
2463     .pipeline_complete = sof_ipc3_complete_pipeline,
2464     .token_list = ipc3_token_list,
2465     .widget_free = sof_ipc3_widget_free,
2466     .widget_setup = sof_ipc3_widget_setup,
2467     .dai_config = sof_ipc3_dai_config,
2468     .dai_get_clk = sof_ipc3_dai_get_clk,
2469     .set_up_all_pipelines = sof_ipc3_set_up_all_pipelines,
2470     .tear_down_all_pipelines = sof_ipc3_tear_down_all_pipelines,
2471     .parse_manifest = sof_ipc3_parse_manifest,
2472 };