1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2021 Intel Corporation. All rights reserved.
10 #include <uapi/sound/sof/tokens.h>
11 #include <sound/pcm_params.h>
13 #include "sof-audio.h"
14 #include "ipc3-priv.h"
17 /* Full volume for default values */
18 #define VOL_ZERO_DB BIT(VOLUME_FWL)
20 /* size of tplg ABI in bytes */
21 #define SOF_IPC3_TPLG_ABI_SIZE 3
23 struct sof_widget_data {
28 struct snd_sof_control *control;
31 struct sof_process_types {
33 enum sof_ipc_process_type type;
34 enum sof_comp_type comp_type;
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},
49 static enum sof_ipc_process_type find_process(const char *name)
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;
58 return SOF_PROCESS_NONE;
61 static int get_token_process_type(void *elem, void *object, u32 offset)
63 u32 *val = (u32 *)((u8 *)object + offset);
65 *val = find_process((const char *)elem);
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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
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)},
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)},
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)},
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)},
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)},
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)},
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)},
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
321 * @index: ID of the pipeline the component belongs to
323 * Return: The pointer to the new allocated component, NULL if failed.
325 static void *sof_comp_alloc(struct snd_sof_widget *swidget, size_t *ipc_size,
328 struct sof_ipc_comp *comp;
329 size_t total_size = *ipc_size;
330 size_t ext_size = sizeof(swidget->uuid);
332 /* only non-zero UUID is valid */
333 if (!guid_is_null(&swidget->uuid))
334 total_size += ext_size;
336 comp = kzalloc(total_size, GFP_KERNEL);
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;
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;
354 /* update ipc_size and return */
355 *ipc_size = total_size;
359 static void sof_dbg_comp_config(struct snd_soc_component *scomp, struct sof_ipc_comp_config *config)
361 dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
362 config->periods_sink, config->periods_source,
366 static int sof_ipc3_widget_setup_comp_host(struct snd_sof_widget *swidget)
368 struct snd_soc_component *scomp = swidget->scomp;
369 struct sof_ipc_comp_host *host;
370 size_t ipc_size = sizeof(*host);
373 host = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
376 swidget->private = host;
378 /* configure host comp IPC message */
379 host->comp.type = SOF_COMP_HOST;
380 host->config.hdr.size = sizeof(host->config);
382 if (swidget->id == snd_soc_dapm_aif_out)
383 host->direction = SOF_IPC_STREAM_CAPTURE;
385 host->direction = SOF_IPC_STREAM_PLAYBACK;
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);
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);
399 dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
400 sof_dbg_comp_config(scomp, &host->config);
404 kfree(swidget->private);
405 swidget->private = NULL;
410 static void sof_ipc3_widget_free_comp(struct snd_sof_widget *swidget)
412 kfree(swidget->private);
415 static int sof_ipc3_widget_setup_comp_tone(struct snd_sof_widget *swidget)
417 struct snd_soc_component *scomp = swidget->scomp;
418 struct sof_ipc_comp_tone *tone;
419 size_t ipc_size = sizeof(*tone);
422 tone = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
426 swidget->private = tone;
428 /* configure siggen IPC message */
429 tone->comp.type = SOF_COMP_TONE;
430 tone->config.hdr.size = sizeof(tone->config);
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);
436 kfree(swidget->private);
437 swidget->private = NULL;
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);
448 static int sof_ipc3_widget_setup_comp_mixer(struct snd_sof_widget *swidget)
450 struct snd_soc_component *scomp = swidget->scomp;
451 struct sof_ipc_comp_mixer *mixer;
452 size_t ipc_size = sizeof(*mixer);
455 mixer = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
459 swidget->private = mixer;
461 /* configure mixer IPC message */
462 mixer->comp.type = SOF_COMP_MIXER;
463 mixer->config.hdr.size = sizeof(mixer->config);
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);
470 kfree(swidget->private);
471 swidget->private = NULL;
476 dev_dbg(scomp->dev, "loaded mixer %s\n", swidget->widget->name);
477 sof_dbg_comp_config(scomp, &mixer->config);
482 static int sof_ipc3_widget_setup_comp_pipeline(struct snd_sof_widget *swidget)
484 struct snd_soc_component *scomp = swidget->scomp;
485 struct sof_ipc_pipe_new *pipeline;
486 struct snd_sof_widget *comp_swidget;
489 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
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;
499 swidget->private = pipeline;
501 /* component at start of pipeline is our stream id */
502 comp_swidget = snd_sof_find_swidget(scomp, swidget->widget->sname);
504 dev_err(scomp->dev, "scheduler %s refers to non existent widget %s\n",
505 swidget->widget->name, swidget->widget->sname);
510 pipeline->sched_id = comp_swidget->comp_id;
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);
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);
524 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE))
525 pipeline->core = SOF_DSP_PRIMARY_CORE;
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);
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);
536 swidget->core = pipeline->core;
541 kfree(swidget->private);
542 swidget->private = NULL;
547 static int sof_ipc3_widget_setup_comp_buffer(struct snd_sof_widget *swidget)
549 struct snd_soc_component *scomp = swidget->scomp;
550 struct sof_ipc_buffer *buffer;
553 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
557 swidget->private = buffer;
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;
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);
571 kfree(swidget->private);
572 swidget->private = NULL;
576 dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
577 swidget->widget->name, buffer->size, buffer->caps);
582 static int sof_ipc3_widget_setup_comp_src(struct snd_sof_widget *swidget)
584 struct snd_soc_component *scomp = swidget->scomp;
585 struct sof_ipc_comp_src *src;
586 size_t ipc_size = sizeof(*src);
589 src = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
593 swidget->private = src;
595 /* configure src IPC message */
596 src->comp.type = SOF_COMP_SRC;
597 src->config.hdr.size = sizeof(src->config);
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);
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);
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);
617 kfree(swidget->private);
618 swidget->private = NULL;
623 static int sof_ipc3_widget_setup_comp_asrc(struct snd_sof_widget *swidget)
625 struct snd_soc_component *scomp = swidget->scomp;
626 struct sof_ipc_comp_asrc *asrc;
627 size_t ipc_size = sizeof(*asrc);
630 asrc = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
634 swidget->private = asrc;
636 /* configure ASRC IPC message */
637 asrc->comp.type = SOF_COMP_ASRC;
638 asrc->config.hdr.size = sizeof(asrc->config);
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);
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);
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);
656 sof_dbg_comp_config(scomp, &asrc->config);
660 kfree(swidget->private);
661 swidget->private = NULL;
669 static int sof_ipc3_widget_setup_comp_mux(struct snd_sof_widget *swidget)
671 struct snd_soc_component *scomp = swidget->scomp;
672 struct sof_ipc_comp_mux *mux;
673 size_t ipc_size = sizeof(*mux);
676 mux = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
680 swidget->private = mux;
682 /* configure mux IPC message */
683 mux->comp.type = SOF_COMP_MUX;
684 mux->config.hdr.size = sizeof(mux->config);
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);
690 kfree(swidget->private);
691 swidget->private = NULL;
695 dev_dbg(scomp->dev, "loaded mux %s\n", swidget->widget->name);
696 sof_dbg_comp_config(scomp, &mux->config);
705 static int sof_ipc3_widget_setup_comp_pga(struct snd_sof_widget *swidget)
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;
715 volume = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
719 swidget->private = volume;
721 /* configure volume IPC message */
722 volume->comp.type = SOF_COMP_VOLUME;
723 volume->config.hdr.size = sizeof(volume->config);
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);
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);
738 dev_dbg(scomp->dev, "loaded PGA %s\n", swidget->widget->name);
739 sof_dbg_comp_config(scomp, &volume->config);
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;
755 kfree(swidget->private);
756 swidget->private = NULL;
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)
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;
774 for (i = 0; i < widget->num_kcontrols; i++) {
775 kc = &widget->kcontrol_news[i];
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;
782 case SND_SOC_TPLG_TYPE_BYTES:
783 sbe = (struct soc_bytes_ext *)kc->private_value;
784 wdata[i].control = sbe->dobj.private;
786 case SND_SOC_TPLG_TYPE_ENUM:
787 se = (struct soc_enum *)kc->private_value;
788 wdata[i].control = se->dobj.private;
791 dev_err(scomp->dev, "Unknown kcontrol type %u in widget %s\n",
792 widget->dobj.widget.kcontrol_type[i], widget->name);
796 if (!wdata[i].control) {
797 dev_err(scomp->dev, "No scontrol for widget %s\n", widget->name);
801 cdata = wdata[i].control->ipc_control_data;
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)
808 wdata[i].pdata = cdata->data->data;
809 wdata[i].pdata_size = cdata->data->size;
811 /* points to the control data union */
812 wdata[i].pdata = cdata->chanv;
814 * wdata[i].control->size is calculated with struct_size
815 * and includes the size of struct sof_ipc_ctrl_data
817 wdata[i].pdata_size = wdata[i].control->size -
818 sizeof(struct sof_ipc_ctrl_data);
821 *size += wdata[i].pdata_size;
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;
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;
843 static int sof_process_load(struct snd_soc_component *scomp,
844 struct snd_sof_widget *swidget, int type)
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;
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);
861 /* get possible component controls and get size of all pdata */
862 ret = sof_get_control_data(scomp, widget, wdata, &ipc_data_size);
867 ipc_size = sizeof(struct sof_ipc_comp_process) + ipc_data_size;
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;
875 process = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
881 swidget->private = process;
883 /* configure iir IPC message */
884 process->comp.type = type;
885 process->config.hdr.size = sizeof(process->config);
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);
894 dev_dbg(scomp->dev, "loaded process %s\n", swidget->widget->name);
895 sof_dbg_comp_config(scomp, &process->config);
898 * found private data in control, so copy it.
899 * get possible component controls - get size of all pdata,
900 * then memcpy with headers
903 for (i = 0; i < widget->num_kcontrols; i++) {
904 if (!wdata[i].pdata_size)
907 memcpy(&process->data[offset], wdata[i].pdata,
908 wdata[i].pdata_size);
909 offset += wdata[i].pdata_size;
913 process->size = ipc_data_size;
919 kfree(swidget->private);
920 swidget->private = NULL;
926 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
930 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
931 if (sof_process[i].type == type)
932 return sof_process[i].comp_type;
935 return SOF_COMP_NONE;
939 * Processing Component Topology - can be "effect", "codec", or general
943 static int sof_widget_update_ipc_comp_process(struct snd_sof_widget *swidget)
945 struct snd_soc_component *scomp = swidget->scomp;
946 struct sof_ipc_comp_process config;
949 memset(&config, 0, sizeof(config));
950 config.comp.core = swidget->core;
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);
958 /* now load process specific data and send IPC */
959 return sof_process_load(scomp, swidget, find_process_comp_type(config.type));
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)
965 struct sof_dai_private_data *private = dai->private;
966 u32 size = sizeof(*config);
970 memset(&config->hda, 0, sizeof(config->hda));
971 config->hdr.size = size;
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);
979 dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
980 config->hda.rate, config->hda.channels);
982 config->hda.link_dma_ch = DMA_CHAN_INVALID;
984 dai->number_configs = 1;
985 dai->current_config = 0;
986 private->dai_config = kmemdup(config, size, GFP_KERNEL);
987 if (!private->dai_config)
993 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
994 struct sof_ipc_dai_config *config)
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;
1003 config->format |= SOF_DAI_FMT_CBP_CFC;
1005 /* codec is bclk consumer */
1006 if (hw_config->fsync_provider == SND_SOC_TPLG_FSYNC_CP)
1007 config->format |= SOF_DAI_FMT_CBC_CFP;
1009 config->format |= SOF_DAI_FMT_CBC_CFC;
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;
1018 config->format |= SOF_DAI_FMT_IB_NF;
1020 if (hw_config->invert_fsync)
1021 config->format |= SOF_DAI_FMT_NB_IF;
1023 config->format |= SOF_DAI_FMT_NB_NF;
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)
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);
1035 /* handle master/slave and inverted clocks */
1036 sof_dai_set_format(hw_config, config);
1039 memset(&config->sai, 0, sizeof(config->sai));
1040 config->hdr.size = size;
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);
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;
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);
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);
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);
1069 dai->number_configs = 1;
1070 dai->current_config = 0;
1071 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1072 if (!private->dai_config)
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)
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);
1086 /* handle master/slave and inverted clocks */
1087 sof_dai_set_format(hw_config, config);
1090 memset(&config->esai, 0, sizeof(config->esai));
1091 config->hdr.size = size;
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);
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);
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);
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);
1119 dai->number_configs = 1;
1120 dai->current_config = 0;
1121 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1122 if (!private->dai_config)
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)
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);
1136 /* handle master/slave and inverted clocks */
1137 sof_dai_set_format(hw_config, config);
1139 config->hdr.size = size;
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);
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);
1151 dai->number_configs = 1;
1152 dai->current_config = 0;
1153 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1154 if (!private->dai_config)
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)
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);
1167 /* handle master/slave and inverted clocks */
1168 sof_dai_set_format(hw_config, config);
1171 memset(&config->acpbt, 0, sizeof(config->acpbt));
1172 config->hdr.size = size;
1174 config->acpbt.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1175 config->acpbt.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
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);
1181 dai->number_configs = 1;
1182 dai->current_config = 0;
1183 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1184 if (!private->dai_config)
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)
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);
1197 /* handle master/slave and inverted clocks */
1198 sof_dai_set_format(hw_config, config);
1201 memset(&config->acpsp, 0, sizeof(config->acpsp));
1202 config->hdr.size = size;
1204 config->acpsp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1205 config->acpsp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
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);
1211 dai->number_configs = 1;
1212 dai->current_config = 0;
1213 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1214 if (!private->dai_config)
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)
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);
1227 /* Configures the DAI hardware format and inverted clocks */
1228 sof_dai_set_format(hw_config, config);
1231 memset(&config->acphs, 0, sizeof(config->acphs));
1232 config->hdr.size = size;
1234 config->acphs.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
1235 config->acphs.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
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);
1241 dai->number_configs = 1;
1242 dai->current_config = 0;
1243 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1244 if (!private->dai_config)
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)
1253 struct sof_dai_private_data *private = dai->private;
1254 u32 size = sizeof(*config);
1257 config->hdr.size = size;
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);
1265 dev_dbg(scomp->dev, "AFE config rate %d channels %d format:%d\n",
1266 config->afe.rate, config->afe.channels, config->afe.format);
1268 config->afe.stream_id = DMA_CHAN_INVALID;
1270 dai->number_configs = 1;
1271 dai->current_config = 0;
1272 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1273 if (!private->dai_config)
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)
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;
1290 * Parse common data, we should have 1 common data per hw_config.
1292 ret = sof_update_ipc_object(scomp, &config->ssp, SOF_SSP_TOKENS, slink->tuples,
1293 slink->num_tuples, size, slink->num_hw_configs);
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)
1302 /* handle master/slave and inverted clocks */
1303 sof_dai_set_format(&hw_config[i], &config[i]);
1305 config[i].hdr.size = size;
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;
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);
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);
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);
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);
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)
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)
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);
1362 /* Ensure the entire DMIC config struct is zeros */
1363 memset(&config->dmic, 0, sizeof(config->dmic));
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);
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);
1379 /* set IPC header size */
1380 config->hdr.size = size;
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);
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);
1409 * this takes care of backwards compatible handling of fifo_bits_b.
1410 * It is deprecated since firmware ABI version 3.0.1.
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;
1415 dai->number_configs = 1;
1416 dai->current_config = 0;
1417 private->dai_config = kmemdup(config, size, GFP_KERNEL);
1418 if (!private->dai_config)
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)
1427 struct sof_dai_private_data *private = dai->private;
1428 u32 size = sizeof(*config);
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);
1438 config->hdr.size = size;
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)
1450 static int sof_ipc3_widget_setup_comp_dai(struct snd_sof_widget *swidget)
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;
1462 private = kzalloc(sizeof(*private), GFP_KERNEL);
1466 dai->private = private;
1468 private->comp_dai = sof_comp_alloc(swidget, &ipc_size, swidget->pipeline_id);
1469 if (!private->comp_dai) {
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);
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);
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);
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);
1496 /* now update DAI config */
1497 list_for_each_entry(slink, &sdev->dai_link_list, list) {
1498 struct sof_ipc_dai_config common_config;
1501 if (strcmp(slink->link->name, dai->name))
1504 /* Reserve memory for all hw configs, eventually freed by widget */
1505 config = kcalloc(slink->num_hw_configs, sizeof(*config), GFP_KERNEL);
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);
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;
1525 switch (common_config.type) {
1526 case SOF_DAI_INTEL_SSP:
1527 ret = sof_link_ssp_load(scomp, slink, config, dai);
1529 case SOF_DAI_INTEL_DMIC:
1530 ret = sof_link_dmic_load(scomp, slink, config, dai);
1532 case SOF_DAI_INTEL_HDA:
1533 ret = sof_link_hda_load(scomp, slink, config, dai);
1535 case SOF_DAI_INTEL_ALH:
1536 ret = sof_link_alh_load(scomp, slink, config, dai);
1538 case SOF_DAI_IMX_SAI:
1539 ret = sof_link_sai_load(scomp, slink, config, dai);
1541 case SOF_DAI_IMX_ESAI:
1542 ret = sof_link_esai_load(scomp, slink, config, dai);
1544 case SOF_DAI_AMD_BT:
1545 ret = sof_link_acp_bt_load(scomp, slink, config, dai);
1547 case SOF_DAI_AMD_SP:
1548 ret = sof_link_acp_sp_load(scomp, slink, config, dai);
1550 case SOF_DAI_AMD_HS:
1551 ret = sof_link_acp_hs_load(scomp, slink, config, dai);
1553 case SOF_DAI_AMD_DMIC:
1554 ret = sof_link_acp_dmic_load(scomp, slink, config, dai);
1556 case SOF_DAI_MEDIATEK_AFE:
1557 ret = sof_link_afe_load(scomp, slink, config, dai);
1563 dev_err(scomp->dev, "failed to load config for dai %s\n", dai->name);
1577 dai->private = NULL;
1581 static void sof_ipc3_widget_free_comp_dai(struct snd_sof_widget *swidget)
1583 switch (swidget->id) {
1584 case snd_soc_dapm_dai_in:
1585 case snd_soc_dapm_dai_out:
1587 struct snd_sof_dai *dai = swidget->private;
1588 struct sof_dai_private_data *dai_data;
1593 dai_data = dai->private;
1595 kfree(dai_data->comp_dai);
1596 kfree(dai_data->dai_config);
1607 static int sof_ipc3_route_setup(struct snd_sof_dev *sdev, struct snd_sof_route *sroute)
1609 struct sof_ipc_pipe_comp_connect connect;
1610 struct sof_ipc_reply reply;
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;
1618 dev_dbg(sdev->dev, "setting up route %s -> %s\n",
1619 sroute->src_widget->widget->name,
1620 sroute->sink_widget->widget->name);
1623 ret = sof_ipc_tx_message(sdev->ipc, &connect, sizeof(connect), &reply, sizeof(reply));
1625 dev_err(sdev->dev, "%s: route %s -> %s failed\n", __func__,
1626 sroute->src_widget->widget->name, sroute->sink_widget->widget->name);
1631 static int sof_ipc3_control_load_bytes(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1633 struct sof_ipc_ctrl_data *cdata;
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);
1642 if (scontrol->priv_size > scontrol->max_size - sizeof(*cdata)) {
1644 "%s: bytes data size %zu exceeds max %zu.\n", __func__,
1645 scontrol->priv_size, scontrol->max_size - sizeof(*cdata));
1649 scontrol->ipc_control_data = kzalloc(scontrol->max_size, GFP_KERNEL);
1650 if (!scontrol->ipc_control_data)
1653 scontrol->size = sizeof(struct sof_ipc_ctrl_data) + scontrol->priv_size;
1655 cdata = scontrol->ipc_control_data;
1656 cdata->cmd = SOF_CTRL_CMD_BINARY;
1657 cdata->index = scontrol->index;
1659 if (scontrol->priv_size > 0) {
1660 memcpy(cdata->data, scontrol->priv, scontrol->priv_size);
1661 kfree(scontrol->priv);
1662 scontrol->priv = NULL;
1664 if (cdata->data->magic != SOF_ABI_MAGIC) {
1665 dev_err(sdev->dev, "Wrong ABI magic 0x%08x.\n", cdata->data->magic);
1670 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
1671 dev_err(sdev->dev, "Incompatible ABI version 0x%08x.\n",
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");
1686 kfree(scontrol->ipc_control_data);
1687 scontrol->ipc_control_data = NULL;
1691 static int sof_ipc3_control_load_volume(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1693 struct sof_ipc_ctrl_data *cdata;
1696 /* init the volume get/put data */
1697 scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1699 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1700 if (!scontrol->ipc_control_data)
1703 cdata = scontrol->ipc_control_data;
1704 cdata->index = scontrol->index;
1706 /* set cmd for mixer control */
1707 if (scontrol->max == 1) {
1708 cdata->cmd = SOF_CTRL_CMD_SWITCH;
1712 cdata->cmd = SOF_CTRL_CMD_VOLUME;
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;
1723 static int sof_ipc3_control_load_enum(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1725 struct sof_ipc_ctrl_data *cdata;
1727 /* init the enum get/put data */
1728 scontrol->size = struct_size(cdata, chanv, scontrol->num_channels);
1730 scontrol->ipc_control_data = kzalloc(scontrol->size, GFP_KERNEL);
1731 if (!scontrol->ipc_control_data)
1734 cdata = scontrol->ipc_control_data;
1735 cdata->index = scontrol->index;
1736 cdata->cmd = SOF_CTRL_CMD_ENUM;
1741 static int sof_ipc3_control_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
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);
1760 static int sof_ipc3_control_free(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
1762 struct sof_ipc_free fcomp;
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;
1768 /* send IPC to the DSP */
1769 return sof_ipc_tx_message(sdev->ipc, &fcomp, sizeof(fcomp), NULL, 0);
1772 /* send pcm params ipc */
1773 static int sof_ipc3_keyword_detect_pcm_params(struct snd_sof_widget *swidget, int dir)
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;
1783 /* get runtime PCM params using widget's stream name */
1784 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1786 dev_err(scomp->dev, "Cannot find PCM for %s\n", swidget->widget->name);
1790 params = &spcm->params[dir];
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);
1806 switch (params_format(params)) {
1807 case SNDRV_PCM_FORMAT_S16:
1808 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
1810 case SNDRV_PCM_FORMAT_S24:
1811 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
1813 case SNDRV_PCM_FORMAT_S32:
1814 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
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));
1824 dev_err(scomp->dev, "%s: PCM params failed for %s\n", __func__,
1825 swidget->widget->name);
1830 /* send stream trigger ipc */
1831 static int sof_ipc3_keyword_detect_trigger(struct snd_sof_widget *swidget, int cmd)
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;
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;
1844 /* send IPC to the DSP */
1845 ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
1847 dev_err(scomp->dev, "%s: Failed to trigger %s\n", __func__, swidget->widget->name);
1852 static int sof_ipc3_keyword_dapm_event(struct snd_soc_dapm_widget *w,
1853 struct snd_kcontrol *k, int event)
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;
1864 scomp = swidget->scomp;
1866 dev_dbg(scomp->dev, "received event %d for widget %s\n",
1869 /* get runtime PCM params using widget's stream name */
1870 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
1872 dev_err(scomp->dev, "%s: Cannot find PCM for %s\n", __func__,
1873 swidget->widget->name);
1877 /* process events */
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");
1885 /* set pcm params */
1886 ret = sof_ipc3_keyword_detect_pcm_params(swidget, stream);
1888 dev_err(scomp->dev, "%s: Failed to set pcm params for widget %s\n",
1889 __func__, swidget->widget->name);
1894 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
1896 dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1897 swidget->widget->name);
1899 case SND_SOC_DAPM_POST_PMD:
1900 if (spcm->stream[stream].suspend_ignored) {
1902 "POST_PMD event ignored, KWD pipeline will remain RUNNING\n");
1907 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
1909 dev_err(scomp->dev, "%s: Failed to trigger widget %s\n", __func__,
1910 swidget->widget->name);
1913 ret = sof_ipc3_keyword_detect_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
1915 dev_err(scomp->dev, "%s: Failed to free PCM for widget %s\n", __func__,
1916 swidget->widget->name);
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},
1930 static int sof_ipc3_widget_bind_event(struct snd_soc_component *scomp,
1931 struct snd_sof_widget *swidget, u16 event_type)
1933 struct sof_ipc_comp *ipc_comp;
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)
1942 ipc_comp = swidget->private;
1943 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
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);
1953 dev_err(scomp->dev, "Invalid event type %d for widget %s\n", event_type,
1954 swidget->widget->name);
1959 static int sof_ipc3_complete_pipeline(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1961 struct sof_ipc_pipe_ready ready;
1962 struct sof_ipc_reply reply;
1965 dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
1966 swidget->widget->name, swidget->comp_id);
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;
1973 ret = sof_ipc_tx_message(sdev->ipc, &ready, sizeof(ready), &reply, sizeof(reply));
1980 static int sof_ipc3_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
1982 struct sof_ipc_free ipc_free = {
1984 .size = sizeof(ipc_free),
1985 .cmd = SOF_IPC_GLB_TPLG_MSG,
1987 .id = swidget->comp_id,
1989 struct sof_ipc_reply reply;
1992 if (!swidget->private)
1995 switch (swidget->id) {
1996 case snd_soc_dapm_scheduler:
1998 ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE;
2001 case snd_soc_dapm_buffer:
2002 ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE;
2005 ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE;
2009 ret = sof_ipc_tx_message(sdev->ipc, &ipc_free, sizeof(ipc_free),
2010 &reply, sizeof(reply));
2012 dev_err(sdev->dev, "failed to free widget %s\n", swidget->widget->name);
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)
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;
2027 if (!dai || !dai->private) {
2028 dev_err(sdev->dev, "No private data for DAI %s\n", swidget->widget->name);
2032 private = dai->private;
2033 if (!private->dai_config) {
2034 dev_err(sdev->dev, "No config for DAI %s\n", dai->name);
2038 config = &private->dai_config[dai->current_config];
2040 dev_err(sdev->dev, "Invalid current config for DAI %s\n", dai->name);
2044 switch (config->type) {
2045 case SOF_DAI_INTEL_SSP:
2047 * DAI_CONFIG IPC during hw_params/hw_free for SSP DAI's is not supported in older
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)))
2055 case SOF_DAI_INTEL_HDA:
2057 config->hda.link_dma_ch = data->dai_data;
2059 case SOF_DAI_INTEL_ALH:
2061 config->dai_index = data->dai_index;
2062 config->alh.stream_id = data->dai_data;
2069 config->flags = flags;
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));
2076 dev_err(sdev->dev, "Failed to set dai config for %s\n", dai->name);
2082 static int sof_ipc3_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
2084 struct sof_ipc_comp_reply reply;
2087 if (!swidget->private)
2090 switch (swidget->id) {
2091 case snd_soc_dapm_dai_in:
2092 case snd_soc_dapm_dai_out:
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;
2098 ret = sof_ipc_tx_message(sdev->ipc, dai_data->comp_dai,
2099 comp->hdr.size, &reply, sizeof(reply));
2102 case snd_soc_dapm_scheduler:
2104 struct sof_ipc_pipe_new *pipeline;
2106 pipeline = swidget->private;
2107 ret = sof_ipc_tx_message(sdev->ipc, pipeline, sizeof(*pipeline),
2108 &reply, sizeof(reply));
2113 struct sof_ipc_cmd_hdr *hdr;
2115 hdr = swidget->private;
2116 ret = sof_ipc_tx_message(sdev->ipc, swidget->private, hdr->size,
2117 &reply, sizeof(reply));
2122 dev_err(sdev->dev, "Failed to setup widget %s\n", swidget->widget->name);
2127 static int sof_ipc3_set_up_all_pipelines(struct snd_sof_dev *sdev, bool verify)
2129 struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
2130 struct snd_sof_widget *swidget;
2131 struct snd_sof_route *sroute;
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)
2141 * For older firmware, skip scheduler widgets in this loop,
2142 * sof_widget_setup() will be called in the 'complete pipeline' loop
2144 if (v->abi_version < SOF_ABI_VER(3, 19, 0) &&
2145 swidget->id == snd_soc_dapm_scheduler)
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;
2154 if (!dai || !dai->private)
2156 private = dai->private;
2157 if (!private->dai_config)
2160 config = private->dai_config;
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.
2167 if (config->type == SOF_DAI_INTEL_HDA)
2168 config->hda.link_dma_ch = DMA_CHAN_INVALID;
2171 ret = sof_widget_setup(sdev, swidget);
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))
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.
2187 if (sroute->src_widget->id != snd_soc_dapm_buffer &&
2188 sroute->sink_widget->id != snd_soc_dapm_buffer)
2191 ret = sof_route_setup(sdev, sroute->src_widget->widget,
2192 sroute->sink_widget->widget);
2194 dev_err(sdev->dev, "%s: route set up failed\n", __func__);
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)
2207 if (v->abi_version < SOF_ABI_VER(3, 19, 0)) {
2208 ret = sof_widget_setup(sdev, swidget);
2213 swidget->complete = sof_ipc3_complete_pipeline(sdev, swidget);
2214 if (swidget->complete < 0)
2215 return swidget->complete;
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.
2229 static int sof_tear_down_left_over_pipelines(struct snd_sof_dev *sdev)
2231 struct snd_sof_widget *swidget;
2232 struct snd_sof_pcm *spcm;
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.
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;
2244 if (!substream || !substream->runtime)
2247 if (spcm->stream[dir].list) {
2248 ret = sof_pcm_stream_free(sdev, substream, spcm, dir, true);
2256 * free any left over DAI widgets. This is equivalent to the handling of suspend trigger
2257 * for the BE DAI for running streams.
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);
2270 * For older firmware, this function doesn't free widgets for static pipelines during suspend.
2271 * It only resets use_count for all widgets.
2273 static int sof_ipc3_tear_down_all_pipelines(struct snd_sof_dev *sdev, bool verify)
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;
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.
2287 list_for_each_entry(swidget, &sdev->widget_list, list) {
2288 if (swidget->dynamic_pipeline_widget) {
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;
2301 ret = sof_widget_free(sdev, swidget);
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).
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);
2316 dev_err(sdev->dev, "failed to tear down paused pipelines\n");
2321 list_for_each_entry(sroute, &sdev->route_list, list)
2322 sroute->setup = false;
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
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);
2339 static int sof_ipc3_dai_get_clk(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type)
2341 struct sof_dai_private_data *private = dai->private;
2343 if (!private || !private->dai_config)
2346 switch (private->dai_config->type) {
2347 case SOF_DAI_INTEL_SSP:
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;
2356 dev_err(sdev->dev, "fail to get SSP clk %d rate\n", clk_type);
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);
2367 static int sof_ipc3_parse_manifest(struct snd_soc_component *scomp, int index,
2368 struct snd_soc_tplg_manifest *man)
2370 u32 size = le32_to_cpu(man->priv.size);
2373 /* backward compatible with tplg without ABI info */
2375 dev_dbg(scomp->dev, "No topology ABI info\n");
2379 if (size != SOF_IPC3_TPLG_ABI_SIZE) {
2380 dev_err(scomp->dev, "%s: Invalid topology ABI size: %u\n",
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);
2390 abi_version = SOF_ABI_VER(man->priv.data[0], man->priv.data[1], man->priv.data[2]);
2392 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
2393 dev_err(scomp->dev, "%s: Incompatible topology ABI version\n", __func__);
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__);
2406 /* token list for each topology object */
2407 static enum sof_tokens host_token_list[] = {
2409 SOF_COMP_EXT_TOKENS,
2414 static enum sof_tokens comp_generic_token_list[] = {
2416 SOF_COMP_EXT_TOKENS,
2420 static enum sof_tokens buffer_token_list[] = {
2424 static enum sof_tokens pipeline_token_list[] = {
2426 SOF_COMP_EXT_TOKENS,
2427 SOF_PIPELINE_TOKENS,
2431 static enum sof_tokens asrc_token_list[] = {
2433 SOF_COMP_EXT_TOKENS,
2438 static enum sof_tokens src_token_list[] = {
2440 SOF_COMP_EXT_TOKENS,
2445 static enum sof_tokens pga_token_list[] = {
2447 SOF_COMP_EXT_TOKENS,
2452 static enum sof_tokens dai_token_list[] = {
2454 SOF_COMP_EXT_TOKENS,
2459 static enum sof_tokens process_token_list[] = {
2461 SOF_COMP_EXT_TOKENS,
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},
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),
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),
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),
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},
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,