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