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) 2018 Intel Corporation. All rights reserved.
11 #include <linux/firmware.h>
12 #include <linux/workqueue.h>
13 #include <sound/tlv.h>
14 #include <sound/pcm_params.h>
15 #include <uapi/sound/sof/tokens.h>
17 #include "sof-audio.h"
20 #define COMP_ID_UNASSIGNED 0xffffffff
22 * Constants used in the computation of linear volume gain
23 * from dB gain 20th root of 10 in Q1.16 fixed-point notation
25 #define VOL_TWENTIETH_ROOT_OF_TEN 73533
26 /* 40th root of 10 in Q1.16 fixed-point notation*/
27 #define VOL_FORTIETH_ROOT_OF_TEN 69419
29 * Volume fractional word length define to 16 sets
30 * the volume linear gain value to use Qx.16 format
33 /* 0.5 dB step value in topology TLV */
34 #define VOL_HALF_DB_STEP 50
35 /* Full volume for default values */
36 #define VOL_ZERO_DB BIT(VOLUME_FWL)
44 /* size of tplg abi in byte */
45 #define SOF_TPLG_ABI_SIZE 3
47 struct sof_widget_data {
50 struct sof_abi_hdr *pdata;
51 struct snd_sof_control *control;
54 /* send pcm params ipc */
55 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir)
57 struct sof_ipc_pcm_params_reply ipc_params_reply;
58 struct snd_soc_component *scomp = swidget->scomp;
59 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
60 struct sof_ipc_pcm_params pcm;
61 struct snd_pcm_hw_params *params;
62 struct snd_sof_pcm *spcm;
65 memset(&pcm, 0, sizeof(pcm));
67 /* get runtime PCM params using widget's stream name */
68 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
70 dev_err(scomp->dev, "error: cannot find PCM for %s\n",
71 swidget->widget->name);
75 params = &spcm->params[dir];
77 /* set IPC PCM params */
78 pcm.hdr.size = sizeof(pcm);
79 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
80 pcm.comp_id = swidget->comp_id;
81 pcm.params.hdr.size = sizeof(pcm.params);
82 pcm.params.direction = dir;
83 pcm.params.sample_valid_bytes = params_width(params) >> 3;
84 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
85 pcm.params.rate = params_rate(params);
86 pcm.params.channels = params_channels(params);
87 pcm.params.host_period_bytes = params_period_bytes(params);
90 switch (params_format(params)) {
91 case SNDRV_PCM_FORMAT_S16:
92 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
94 case SNDRV_PCM_FORMAT_S24:
95 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
97 case SNDRV_PCM_FORMAT_S32:
98 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
104 /* send IPC to the DSP */
105 ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
106 &ipc_params_reply, sizeof(ipc_params_reply));
108 dev_err(scomp->dev, "error: pcm params failed for %s\n",
109 swidget->widget->name);
114 /* send stream trigger ipc */
115 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd)
117 struct snd_soc_component *scomp = swidget->scomp;
118 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
119 struct sof_ipc_stream stream;
120 struct sof_ipc_reply reply;
123 /* set IPC stream params */
124 stream.hdr.size = sizeof(stream);
125 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
126 stream.comp_id = swidget->comp_id;
128 /* send IPC to the DSP */
129 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
130 sizeof(stream), &reply, sizeof(reply));
132 dev_err(scomp->dev, "error: failed to trigger %s\n",
133 swidget->widget->name);
138 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w,
139 struct snd_kcontrol *k, int event)
141 struct snd_sof_widget *swidget = w->dobj.private;
142 struct snd_soc_component *scomp;
143 int stream = SNDRV_PCM_STREAM_CAPTURE;
144 struct snd_sof_pcm *spcm;
150 scomp = swidget->scomp;
152 dev_dbg(scomp->dev, "received event %d for widget %s\n",
155 /* get runtime PCM params using widget's stream name */
156 spcm = snd_sof_find_spcm_name(scomp, swidget->widget->sname);
158 dev_err(scomp->dev, "error: cannot find PCM for %s\n",
159 swidget->widget->name);
165 case SND_SOC_DAPM_PRE_PMU:
166 if (spcm->stream[stream].suspend_ignored) {
167 dev_dbg(scomp->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
172 ret = ipc_pcm_params(swidget, stream);
175 "error: failed to set pcm params for widget %s\n",
176 swidget->widget->name);
181 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
184 "error: failed to trigger widget %s\n",
185 swidget->widget->name);
187 case SND_SOC_DAPM_POST_PMD:
188 if (spcm->stream[stream].suspend_ignored) {
189 dev_dbg(scomp->dev, "POST_PMD even ignored, KWD pipeline will remain RUNNING\n");
194 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
197 "error: failed to trigger widget %s\n",
198 swidget->widget->name);
201 ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
204 "error: failed to trigger widget %s\n",
205 swidget->widget->name);
214 /* event handlers for keyword detect component */
215 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
216 {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event},
219 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS])
221 /* we only support dB scale TLV type at the moment */
222 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
225 /* min value in topology tlv data is multiplied by 100 */
226 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
229 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
233 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
234 TLV_DB_SCALE_MUTE) == 0)
243 * Function to truncate an unsigned 64-bit number
244 * by x bits and return 32-bit unsigned number. This
245 * function also takes care of rounding while truncating
247 static inline u32 vol_shift_64(u64 i, u32 x)
249 /* do not truncate more than 32 bits */
256 return (u32)(((i >> (x - 1)) + 1) >> 1);
260 * Function to compute a ^ exp where,
261 * a is a fractional number represented by a fixed-point
262 * integer with a fractional world length of "fwl"
264 * fwl is the fractional word length
265 * Return value is a fractional number represented by a
266 * fixed-point integer with a fractional word length of "fwl"
268 static u32 vol_pow32(u32 a, int exp, u32 fwl)
271 u32 power = 1 << fwl;
274 /* if exponent is 0, return 1 */
278 /* determine the number of iterations based on the exponent */
284 /* mutiply a "iter" times to compute power */
285 for (i = 0; i < iter; i++) {
287 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
288 * Truncate product back to fwl fractional bits with rounding
290 power = vol_shift_64((u64)power * a, fwl);
294 /* if exp is positive, return the result */
298 /* if exp is negative, return the multiplicative inverse */
299 numerator = (u64)1 << (fwl << 1);
300 do_div(numerator, power);
302 return (u32)numerator;
306 * Function to calculate volume gain from TLV data.
307 * This function can only handle gain steps that are multiples of 0.5 dB
309 static u32 vol_compute_gain(u32 value, int *tlv)
316 if (value == 0 && tlv[TLV_MUTE])
320 * compute dB gain from tlv. tlv_step
321 * in topology is multiplied by 100
323 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
326 * compute linear gain represented by fixed-point
327 * int with VOLUME_FWL fractional bits
329 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
331 /* extract the fractional part of volume step */
332 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
334 /* if volume step is an odd multiple of 0.5 dB */
335 if (f_step == VOL_HALF_DB_STEP && (value & 1))
336 linear_gain = vol_shift_64((u64)linear_gain *
337 VOL_FORTIETH_ROOT_OF_TEN,
344 * Set up volume table for kcontrols from tlv data
345 * "size" specifies the number of entries in the table
347 static int set_up_volume_table(struct snd_sof_control *scontrol,
348 int tlv[TLV_ITEMS], int size)
352 /* init the volume table */
353 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
354 if (!scontrol->volume_table)
357 /* populate the volume table */
358 for (j = 0; j < size ; j++)
359 scontrol->volume_table[j] = vol_compute_gain(j, tlv);
364 struct sof_dai_types {
366 enum sof_ipc_dai_type type;
369 static const struct sof_dai_types sof_dais[] = {
370 {"SSP", SOF_DAI_INTEL_SSP},
371 {"HDA", SOF_DAI_INTEL_HDA},
372 {"DMIC", SOF_DAI_INTEL_DMIC},
373 {"ALH", SOF_DAI_INTEL_ALH},
374 {"SAI", SOF_DAI_IMX_SAI},
375 {"ESAI", SOF_DAI_IMX_ESAI},
378 static enum sof_ipc_dai_type find_dai(const char *name)
382 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
383 if (strcmp(name, sof_dais[i].name) == 0)
384 return sof_dais[i].type;
387 return SOF_DAI_INTEL_NONE;
391 * Supported Frame format types and lookup, add new ones to end of list.
394 struct sof_frame_types {
396 enum sof_ipc_frame frame;
399 static const struct sof_frame_types sof_frames[] = {
400 {"s16le", SOF_IPC_FRAME_S16_LE},
401 {"s24le", SOF_IPC_FRAME_S24_4LE},
402 {"s32le", SOF_IPC_FRAME_S32_LE},
403 {"float", SOF_IPC_FRAME_FLOAT},
406 static enum sof_ipc_frame find_format(const char *name)
410 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
411 if (strcmp(name, sof_frames[i].name) == 0)
412 return sof_frames[i].frame;
415 /* use s32le if nothing is specified */
416 return SOF_IPC_FRAME_S32_LE;
419 struct sof_process_types {
421 enum sof_ipc_process_type type;
422 enum sof_comp_type comp_type;
425 static const struct sof_process_types sof_process[] = {
426 {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
427 {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
428 {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
429 {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
430 {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
431 {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
432 {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
433 {"DCBLOCK", SOF_PROCESS_DCBLOCK, SOF_COMP_DCBLOCK},
434 {"SMART_AMP", SOF_PROCESS_SMART_AMP, SOF_COMP_SMART_AMP},
437 static enum sof_ipc_process_type find_process(const char *name)
441 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
442 if (strcmp(name, sof_process[i].name) == 0)
443 return sof_process[i].type;
446 return SOF_PROCESS_NONE;
449 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
453 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
454 if (sof_process[i].type == type)
455 return sof_process[i].comp_type;
458 return SOF_COMP_NONE;
462 * Topology Token Parsing.
463 * New tokens should be added to headers and parsing tables below.
466 struct sof_topology_token {
469 int (*get_token)(void *elem, void *object, u32 offset, u32 size);
474 static int get_token_u32(void *elem, void *object, u32 offset, u32 size)
476 struct snd_soc_tplg_vendor_value_elem *velem = elem;
477 u32 *val = (u32 *)((u8 *)object + offset);
479 *val = le32_to_cpu(velem->value);
483 static int get_token_u16(void *elem, void *object, u32 offset, u32 size)
485 struct snd_soc_tplg_vendor_value_elem *velem = elem;
486 u16 *val = (u16 *)((u8 *)object + offset);
488 *val = (u16)le32_to_cpu(velem->value);
492 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size)
494 struct snd_soc_tplg_vendor_string_elem *velem = elem;
495 u32 *val = (u32 *)((u8 *)object + offset);
497 *val = find_format(velem->string);
501 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size)
503 struct snd_soc_tplg_vendor_string_elem *velem = elem;
504 u32 *val = (u32 *)((u8 *)object + offset);
506 *val = find_dai(velem->string);
510 static int get_token_process_type(void *elem, void *object, u32 offset,
513 struct snd_soc_tplg_vendor_string_elem *velem = elem;
514 u32 *val = (u32 *)((u8 *)object + offset);
516 *val = find_process(velem->string);
521 static const struct sof_topology_token buffer_tokens[] = {
522 {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
523 offsetof(struct sof_ipc_buffer, size), 0},
524 {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
525 offsetof(struct sof_ipc_buffer, caps), 0},
529 static const struct sof_topology_token dai_tokens[] = {
530 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
531 offsetof(struct sof_ipc_comp_dai, type), 0},
532 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
533 offsetof(struct sof_ipc_comp_dai, dai_index), 0},
534 {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
535 offsetof(struct sof_ipc_comp_dai, direction), 0},
539 static const struct sof_topology_token dai_link_tokens[] = {
540 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
541 offsetof(struct sof_ipc_dai_config, type), 0},
542 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
543 offsetof(struct sof_ipc_dai_config, dai_index), 0},
547 static const struct sof_topology_token sched_tokens[] = {
548 {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
549 offsetof(struct sof_ipc_pipe_new, period), 0},
550 {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
551 offsetof(struct sof_ipc_pipe_new, priority), 0},
552 {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
553 offsetof(struct sof_ipc_pipe_new, period_mips), 0},
554 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
555 offsetof(struct sof_ipc_pipe_new, core), 0},
556 {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
557 offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0},
558 {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
559 offsetof(struct sof_ipc_pipe_new, time_domain), 0},
563 static const struct sof_topology_token volume_tokens[] = {
564 {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
565 get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0},
566 {SOF_TKN_VOLUME_RAMP_STEP_MS,
567 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
568 offsetof(struct sof_ipc_comp_volume, initial_ramp), 0},
572 static const struct sof_topology_token src_tokens[] = {
573 {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
574 offsetof(struct sof_ipc_comp_src, source_rate), 0},
575 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
576 offsetof(struct sof_ipc_comp_src, sink_rate), 0},
580 static const struct sof_topology_token asrc_tokens[] = {
581 {SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
582 offsetof(struct sof_ipc_comp_asrc, source_rate), 0},
583 {SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
584 offsetof(struct sof_ipc_comp_asrc, sink_rate), 0},
585 {SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
587 offsetof(struct sof_ipc_comp_asrc, asynchronous_mode), 0},
588 {SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
590 offsetof(struct sof_ipc_comp_asrc, operation_mode), 0},
594 static const struct sof_topology_token tone_tokens[] = {
598 static const struct sof_topology_token process_tokens[] = {
599 {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
600 get_token_process_type,
601 offsetof(struct sof_ipc_comp_process, type), 0},
605 static const struct sof_topology_token pcm_tokens[] = {
606 {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
607 offsetof(struct sof_ipc_comp_host, dmac_config), 0},
611 static const struct sof_topology_token stream_tokens[] = {
612 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
613 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
614 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible), 0},
615 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
616 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
617 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible), 0},
620 /* Generic components */
621 static const struct sof_topology_token comp_tokens[] = {
622 {SOF_TKN_COMP_PERIOD_SINK_COUNT,
623 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
624 offsetof(struct sof_ipc_comp_config, periods_sink), 0},
625 {SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
626 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
627 offsetof(struct sof_ipc_comp_config, periods_source), 0},
628 {SOF_TKN_COMP_FORMAT,
629 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
630 offsetof(struct sof_ipc_comp_config, frame_fmt), 0},
634 static const struct sof_topology_token ssp_tokens[] = {
635 {SOF_TKN_INTEL_SSP_CLKS_CONTROL,
636 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
637 offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0},
638 {SOF_TKN_INTEL_SSP_MCLK_ID,
639 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
640 offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0},
641 {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
643 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0},
644 {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
646 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0},
647 {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
649 offsetof(struct sof_ipc_dai_ssp_params, quirks), 0},
650 {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
652 offsetof(struct sof_ipc_dai_ssp_params,
653 tdm_per_slot_padding_flag), 0},
654 {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
656 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0},
661 static const struct sof_topology_token alh_tokens[] = {
662 {SOF_TKN_INTEL_ALH_RATE,
663 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
664 offsetof(struct sof_ipc_dai_alh_params, rate), 0},
665 {SOF_TKN_INTEL_ALH_CH,
666 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
667 offsetof(struct sof_ipc_dai_alh_params, channels), 0},
671 static const struct sof_topology_token dmic_tokens[] = {
672 {SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
673 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
674 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version),
676 {SOF_TKN_INTEL_DMIC_CLK_MIN,
677 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
678 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0},
679 {SOF_TKN_INTEL_DMIC_CLK_MAX,
680 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
681 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0},
682 {SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
683 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
684 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0},
685 {SOF_TKN_INTEL_DMIC_DUTY_MIN,
686 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
687 offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0},
688 {SOF_TKN_INTEL_DMIC_DUTY_MAX,
689 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
690 offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0},
691 {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
692 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
693 offsetof(struct sof_ipc_dai_dmic_params,
695 {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
696 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
697 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0},
698 {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
699 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
700 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0},
705 static const struct sof_topology_token esai_tokens[] = {
706 {SOF_TKN_IMX_ESAI_MCLK_ID,
707 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
708 offsetof(struct sof_ipc_dai_esai_params, mclk_id), 0},
712 static const struct sof_topology_token sai_tokens[] = {
713 {SOF_TKN_IMX_SAI_MCLK_ID,
714 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
715 offsetof(struct sof_ipc_dai_sai_params, mclk_id), 0},
720 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
721 * as it increments the index while parsing the array of pdm tokens
722 * and determines the correct offset
724 static const struct sof_topology_token dmic_pdm_tokens[] = {
725 {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
726 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
727 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),
729 {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
730 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
731 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a),
733 {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
734 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
735 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b),
737 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
738 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
739 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a),
741 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
742 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
743 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b),
745 {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
746 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
747 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge),
749 {SOF_TKN_INTEL_DMIC_PDM_SKEW,
750 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
751 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew),
756 static const struct sof_topology_token hda_tokens[] = {
757 {SOF_TKN_INTEL_HDA_RATE,
758 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
759 offsetof(struct sof_ipc_dai_hda_params, rate), 0},
760 {SOF_TKN_INTEL_HDA_CH,
761 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
762 offsetof(struct sof_ipc_dai_hda_params, channels), 0},
766 static const struct sof_topology_token led_tokens[] = {
767 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
768 offsetof(struct snd_sof_led_control, use_led), 0},
769 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
770 get_token_u32, offsetof(struct snd_sof_led_control, direction), 0},
773 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
775 const struct sof_topology_token *tokens,
777 struct snd_soc_tplg_vendor_array *array,
780 struct snd_soc_tplg_vendor_uuid_elem *elem;
784 /* parse element by element */
785 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
786 elem = &array->uuid[i];
788 /* search for token */
789 for (j = 0; j < count; j++) {
790 /* match token type */
791 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
795 if (tokens[j].token != le32_to_cpu(elem->token))
798 /* matched - now load token */
799 tokens[j].get_token(elem, object,
800 offset + tokens[j].offset,
810 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
812 const struct sof_topology_token *tokens,
814 struct snd_soc_tplg_vendor_array *array,
817 struct snd_soc_tplg_vendor_string_elem *elem;
821 /* parse element by element */
822 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
823 elem = &array->string[i];
825 /* search for token */
826 for (j = 0; j < count; j++) {
827 /* match token type */
828 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
832 if (tokens[j].token != le32_to_cpu(elem->token))
835 /* matched - now load token */
836 tokens[j].get_token(elem, object,
837 offset + tokens[j].offset,
847 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
849 const struct sof_topology_token *tokens,
851 struct snd_soc_tplg_vendor_array *array,
854 struct snd_soc_tplg_vendor_value_elem *elem;
858 /* parse element by element */
859 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
860 elem = &array->value[i];
862 /* search for token */
863 for (j = 0; j < count; j++) {
864 /* match token type */
865 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
866 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
867 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
868 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
872 if (tokens[j].token != le32_to_cpu(elem->token))
876 tokens[j].get_token(elem, object,
877 offset + tokens[j].offset,
888 * sof_parse_token_sets - Parse multiple sets of tokens
889 * @scomp: pointer to soc component
890 * @object: target ipc struct for parsed values
891 * @tokens: token definition array describing what tokens to parse
892 * @count: number of tokens in definition array
893 * @array: source pointer to consecutive vendor arrays to be parsed
894 * @priv_size: total size of the consecutive source arrays
895 * @sets: number of similar token sets to be parsed, 1 set has count elements
896 * @object_size: offset to next target ipc struct with multiple sets
898 * This function parses multiple sets of tokens in vendor arrays into
899 * consecutive ipc structs.
901 static int sof_parse_token_sets(struct snd_soc_component *scomp,
903 const struct sof_topology_token *tokens,
905 struct snd_soc_tplg_vendor_array *array,
906 int priv_size, int sets, size_t object_size)
913 while (priv_size > 0 && total < count * sets) {
914 asize = le32_to_cpu(array->size);
917 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
918 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
923 /* make sure there is enough data before parsing */
926 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
931 /* call correct parser depending on type */
932 switch (le32_to_cpu(array->type)) {
933 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
934 found += sof_parse_uuid_tokens(scomp, object, tokens,
935 count, array, offset);
937 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
938 found += sof_parse_string_tokens(scomp, object, tokens,
939 count, array, offset);
941 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
942 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
943 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
944 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
945 found += sof_parse_word_tokens(scomp, object, tokens,
946 count, array, offset);
949 dev_err(scomp->dev, "error: unknown token type %d\n",
955 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
958 /* move to next target struct */
959 if (found >= count) {
960 offset += object_size;
969 static int sof_parse_tokens(struct snd_soc_component *scomp,
971 const struct sof_topology_token *tokens,
973 struct snd_soc_tplg_vendor_array *array,
977 * sof_parse_tokens is used when topology contains only a single set of
978 * identical tuples arrays. So additional parameters to
979 * sof_parse_token_sets are sets = 1 (only 1 set) and
980 * object_size = 0 (irrelevant).
982 return sof_parse_token_sets(scomp, object, tokens, count, array,
986 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
987 struct sof_ipc_comp_config *config)
989 dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
990 config->periods_sink, config->periods_source,
995 * Standard Kcontrols.
998 static int sof_control_load_volume(struct snd_soc_component *scomp,
999 struct snd_sof_control *scontrol,
1000 struct snd_kcontrol_new *kc,
1001 struct snd_soc_tplg_ctl_hdr *hdr)
1003 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1004 struct snd_soc_tplg_mixer_control *mc =
1005 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
1006 struct sof_ipc_ctrl_data *cdata;
1011 /* validate topology data */
1012 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) {
1017 /* init the volume get/put data */
1018 scontrol->size = struct_size(scontrol->control_data, chanv,
1019 le32_to_cpu(mc->num_channels));
1020 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1021 if (!scontrol->control_data) {
1026 scontrol->comp_id = sdev->next_comp_id;
1027 scontrol->min_volume_step = le32_to_cpu(mc->min);
1028 scontrol->max_volume_step = le32_to_cpu(mc->max);
1029 scontrol->num_channels = le32_to_cpu(mc->num_channels);
1031 /* set cmd for mixer control */
1032 if (le32_to_cpu(mc->max) == 1) {
1033 scontrol->cmd = SOF_CTRL_CMD_SWITCH;
1037 scontrol->cmd = SOF_CTRL_CMD_VOLUME;
1039 /* extract tlv data */
1040 if (get_tlv_data(kc->tlv.p, tlv) < 0) {
1041 dev_err(scomp->dev, "error: invalid TLV data\n");
1046 /* set up volume table */
1047 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
1049 dev_err(scomp->dev, "error: setting up volume table\n");
1053 /* set default volume values to 0dB in control */
1054 cdata = scontrol->control_data;
1055 for (i = 0; i < scontrol->num_channels; i++) {
1056 cdata->chanv[i].channel = i;
1057 cdata->chanv[i].value = VOL_ZERO_DB;
1061 /* set up possible led control from mixer private data */
1062 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
1063 ARRAY_SIZE(led_tokens), mc->priv.array,
1064 le32_to_cpu(mc->priv.size));
1066 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
1067 le32_to_cpu(mc->priv.size));
1068 goto out_free_table;
1071 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1072 scontrol->comp_id, scontrol->num_channels);
1077 if (le32_to_cpu(mc->max) > 1)
1078 kfree(scontrol->volume_table);
1080 kfree(scontrol->control_data);
1085 static int sof_control_load_enum(struct snd_soc_component *scomp,
1086 struct snd_sof_control *scontrol,
1087 struct snd_kcontrol_new *kc,
1088 struct snd_soc_tplg_ctl_hdr *hdr)
1090 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1091 struct snd_soc_tplg_enum_control *ec =
1092 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1094 /* validate topology data */
1095 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1098 /* init the enum get/put data */
1099 scontrol->size = struct_size(scontrol->control_data, chanv,
1100 le32_to_cpu(ec->num_channels));
1101 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1102 if (!scontrol->control_data)
1105 scontrol->comp_id = sdev->next_comp_id;
1106 scontrol->num_channels = le32_to_cpu(ec->num_channels);
1108 scontrol->cmd = SOF_CTRL_CMD_ENUM;
1110 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1111 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1116 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1117 struct snd_sof_control *scontrol,
1118 struct snd_kcontrol_new *kc,
1119 struct snd_soc_tplg_ctl_hdr *hdr)
1121 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1122 struct sof_ipc_ctrl_data *cdata;
1123 struct snd_soc_tplg_bytes_control *control =
1124 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1125 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1126 int max_size = sbe->max;
1129 /* init the get/put bytes data */
1130 scontrol->size = sizeof(struct sof_ipc_ctrl_data) +
1131 le32_to_cpu(control->priv.size);
1133 if (scontrol->size > max_size) {
1134 dev_err(scomp->dev, "err: bytes data size %d exceeds max %d.\n",
1135 scontrol->size, max_size);
1140 scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1141 cdata = scontrol->control_data;
1142 if (!scontrol->control_data) {
1147 scontrol->comp_id = sdev->next_comp_id;
1148 scontrol->cmd = SOF_CTRL_CMD_BINARY;
1150 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1151 scontrol->comp_id, scontrol->num_channels);
1153 if (le32_to_cpu(control->priv.size) > 0) {
1154 memcpy(cdata->data, control->priv.data,
1155 le32_to_cpu(control->priv.size));
1157 if (cdata->data->magic != SOF_ABI_MAGIC) {
1158 dev_err(scomp->dev, "error: Wrong ABI magic 0x%08x.\n",
1159 cdata->data->magic);
1163 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1164 cdata->data->abi)) {
1166 "error: Incompatible ABI version 0x%08x.\n",
1171 if (cdata->data->size + sizeof(const struct sof_abi_hdr) !=
1172 le32_to_cpu(control->priv.size)) {
1174 "error: Conflict in bytes vs. priv size.\n");
1183 kfree(scontrol->control_data);
1188 /* external kcontrol init - used for any driver specific init */
1189 static int sof_control_load(struct snd_soc_component *scomp, int index,
1190 struct snd_kcontrol_new *kc,
1191 struct snd_soc_tplg_ctl_hdr *hdr)
1193 struct soc_mixer_control *sm;
1194 struct soc_bytes_ext *sbe;
1195 struct soc_enum *se;
1196 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1197 struct snd_soc_dobj *dobj;
1198 struct snd_sof_control *scontrol;
1201 dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
1202 hdr->type, hdr->name);
1204 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1208 scontrol->scomp = scomp;
1210 switch (le32_to_cpu(hdr->ops.info)) {
1211 case SND_SOC_TPLG_CTL_VOLSW:
1212 case SND_SOC_TPLG_CTL_VOLSW_SX:
1213 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1214 sm = (struct soc_mixer_control *)kc->private_value;
1216 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1218 case SND_SOC_TPLG_CTL_BYTES:
1219 sbe = (struct soc_bytes_ext *)kc->private_value;
1221 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1223 case SND_SOC_TPLG_CTL_ENUM:
1224 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1225 se = (struct soc_enum *)kc->private_value;
1227 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1229 case SND_SOC_TPLG_CTL_RANGE:
1230 case SND_SOC_TPLG_CTL_STROBE:
1231 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1232 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1233 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1234 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1235 case SND_SOC_TPLG_DAPM_CTL_PIN:
1237 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1238 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1248 scontrol->led_ctl.led_value = -1;
1250 dobj->private = scontrol;
1251 list_add(&scontrol->list, &sdev->kcontrol_list);
1255 static int sof_control_unload(struct snd_soc_component *scomp,
1256 struct snd_soc_dobj *dobj)
1258 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1259 struct sof_ipc_free fcomp;
1260 struct snd_sof_control *scontrol = dobj->private;
1262 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scomp->name);
1264 fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1265 fcomp.hdr.size = sizeof(fcomp);
1266 fcomp.id = scontrol->comp_id;
1268 kfree(scontrol->control_data);
1269 list_del(&scontrol->list);
1271 /* send IPC to the DSP */
1272 return sof_ipc_tx_message(sdev->ipc,
1273 fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1281 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1282 struct snd_soc_dapm_widget *w,
1283 struct snd_soc_tplg_dapm_widget *tw,
1284 struct snd_sof_dai *dai)
1286 struct snd_soc_card *card = scomp->card;
1287 struct snd_soc_pcm_runtime *rtd;
1288 struct snd_soc_dai *cpu_dai;
1291 list_for_each_entry(rtd, &card->rtd_list, list) {
1292 dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1293 w->name, w->sname, rtd->dai_link->stream_name);
1295 if (!w->sname || !rtd->dai_link->stream_name)
1298 /* does stream match DAI link ? */
1299 if (strcmp(w->sname, rtd->dai_link->stream_name))
1303 case snd_soc_dapm_dai_out:
1304 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1306 * Please create DAI widget in the right order
1307 * to ensure BE will connect to the right DAI
1310 if (!cpu_dai->capture_widget) {
1311 cpu_dai->capture_widget = w;
1315 if (i == rtd->num_cpus) {
1316 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1321 dai->name = rtd->dai_link->name;
1322 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1323 w->name, rtd->dai_link->name);
1325 case snd_soc_dapm_dai_in:
1326 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1328 * Please create DAI widget in the right order
1329 * to ensure BE will connect to the right DAI
1332 if (!cpu_dai->playback_widget) {
1333 cpu_dai->playback_widget = w;
1337 if (i == rtd->num_cpus) {
1338 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1343 dai->name = rtd->dai_link->name;
1344 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1345 w->name, rtd->dai_link->name);
1352 /* check we have a connection */
1354 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1362 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1363 struct snd_sof_widget *swidget,
1364 struct snd_soc_tplg_dapm_widget *tw,
1365 struct sof_ipc_comp_reply *r,
1366 struct snd_sof_dai *dai)
1368 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1369 struct snd_soc_tplg_private *private = &tw->priv;
1370 struct sof_ipc_comp_dai comp_dai;
1373 /* configure dai IPC message */
1374 memset(&comp_dai, 0, sizeof(comp_dai));
1375 comp_dai.comp.hdr.size = sizeof(comp_dai);
1376 comp_dai.comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1377 comp_dai.comp.id = swidget->comp_id;
1378 comp_dai.comp.type = SOF_COMP_DAI;
1379 comp_dai.comp.pipeline_id = index;
1380 comp_dai.config.hdr.size = sizeof(comp_dai.config);
1382 ret = sof_parse_tokens(scomp, &comp_dai, dai_tokens,
1383 ARRAY_SIZE(dai_tokens), private->array,
1384 le32_to_cpu(private->size));
1386 dev_err(scomp->dev, "error: parse dai tokens failed %d\n",
1387 le32_to_cpu(private->size));
1391 ret = sof_parse_tokens(scomp, &comp_dai.config, comp_tokens,
1392 ARRAY_SIZE(comp_tokens), private->array,
1393 le32_to_cpu(private->size));
1395 dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n",
1400 dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1401 swidget->widget->name, comp_dai.type, comp_dai.dai_index);
1402 sof_dbg_comp_config(scomp, &comp_dai.config);
1404 ret = sof_ipc_tx_message(sdev->ipc, comp_dai.comp.hdr.cmd,
1405 &comp_dai, sizeof(comp_dai), r, sizeof(*r));
1407 if (ret == 0 && dai) {
1409 memcpy(&dai->comp_dai, &comp_dai, sizeof(comp_dai));
1419 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index,
1420 struct snd_sof_widget *swidget,
1421 struct snd_soc_tplg_dapm_widget *tw,
1422 struct sof_ipc_comp_reply *r)
1424 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1425 struct snd_soc_tplg_private *private = &tw->priv;
1426 struct sof_ipc_buffer *buffer;
1429 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1433 /* configure dai IPC message */
1434 buffer->comp.hdr.size = sizeof(*buffer);
1435 buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
1436 buffer->comp.id = swidget->comp_id;
1437 buffer->comp.type = SOF_COMP_BUFFER;
1438 buffer->comp.pipeline_id = index;
1440 ret = sof_parse_tokens(scomp, buffer, buffer_tokens,
1441 ARRAY_SIZE(buffer_tokens), private->array,
1442 le32_to_cpu(private->size));
1444 dev_err(scomp->dev, "error: parse buffer tokens failed %d\n",
1450 dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
1451 swidget->widget->name, buffer->size, buffer->caps);
1453 swidget->private = buffer;
1455 ret = sof_ipc_tx_message(sdev->ipc, buffer->comp.hdr.cmd, buffer,
1456 sizeof(*buffer), r, sizeof(*r));
1458 dev_err(scomp->dev, "error: buffer %s load failed\n",
1459 swidget->widget->name);
1466 /* bind PCM ID to host component ID */
1467 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1470 struct snd_sof_widget *host_widget;
1472 host_widget = snd_sof_find_swidget_sname(scomp,
1473 spcm->pcm.caps[dir].name,
1476 dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1480 spcm->stream[dir].comp_id = host_widget->comp_id;
1489 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index,
1490 struct snd_sof_widget *swidget,
1491 enum sof_ipc_stream_direction dir,
1492 struct snd_soc_tplg_dapm_widget *tw,
1493 struct sof_ipc_comp_reply *r)
1495 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1496 struct snd_soc_tplg_private *private = &tw->priv;
1497 struct sof_ipc_comp_host *host;
1500 host = kzalloc(sizeof(*host), GFP_KERNEL);
1504 /* configure host comp IPC message */
1505 host->comp.hdr.size = sizeof(*host);
1506 host->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1507 host->comp.id = swidget->comp_id;
1508 host->comp.type = SOF_COMP_HOST;
1509 host->comp.pipeline_id = index;
1510 host->direction = dir;
1511 host->config.hdr.size = sizeof(host->config);
1513 ret = sof_parse_tokens(scomp, host, pcm_tokens,
1514 ARRAY_SIZE(pcm_tokens), private->array,
1515 le32_to_cpu(private->size));
1517 dev_err(scomp->dev, "error: parse host tokens failed %d\n",
1522 ret = sof_parse_tokens(scomp, &host->config, comp_tokens,
1523 ARRAY_SIZE(comp_tokens), private->array,
1524 le32_to_cpu(private->size));
1526 dev_err(scomp->dev, "error: parse host.cfg tokens failed %d\n",
1527 le32_to_cpu(private->size));
1531 dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
1532 sof_dbg_comp_config(scomp, &host->config);
1534 swidget->private = host;
1536 ret = sof_ipc_tx_message(sdev->ipc, host->comp.hdr.cmd, host,
1537 sizeof(*host), r, sizeof(*r));
1548 int sof_load_pipeline_ipc(struct device *dev,
1549 struct sof_ipc_pipe_new *pipeline,
1550 struct sof_ipc_comp_reply *r)
1552 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
1553 struct sof_ipc_pm_core_config pm_core_config;
1556 ret = sof_ipc_tx_message(sdev->ipc, pipeline->hdr.cmd, pipeline,
1557 sizeof(*pipeline), r, sizeof(*r));
1559 dev_err(dev, "error: load pipeline ipc failure\n");
1563 /* power up the core that this pipeline is scheduled on */
1564 ret = snd_sof_dsp_core_power_up(sdev, 1 << pipeline->core);
1566 dev_err(dev, "error: powering up pipeline schedule core %d\n",
1571 /* update enabled cores mask */
1572 sdev->enabled_cores_mask |= 1 << pipeline->core;
1575 * Now notify DSP that the core that this pipeline is scheduled on
1576 * has been powered up
1578 memset(&pm_core_config, 0, sizeof(pm_core_config));
1579 pm_core_config.enable_mask = sdev->enabled_cores_mask;
1581 /* configure CORE_ENABLE ipc message */
1582 pm_core_config.hdr.size = sizeof(pm_core_config);
1583 pm_core_config.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE;
1586 ret = sof_ipc_tx_message(sdev->ipc, pm_core_config.hdr.cmd,
1587 &pm_core_config, sizeof(pm_core_config),
1588 &pm_core_config, sizeof(pm_core_config));
1590 dev_err(dev, "error: core enable ipc failure\n");
1595 static int sof_widget_load_pipeline(struct snd_soc_component *scomp,
1596 int index, struct snd_sof_widget *swidget,
1597 struct snd_soc_tplg_dapm_widget *tw,
1598 struct sof_ipc_comp_reply *r)
1600 struct snd_soc_tplg_private *private = &tw->priv;
1601 struct sof_ipc_pipe_new *pipeline;
1602 struct snd_sof_widget *comp_swidget;
1605 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
1609 /* configure dai IPC message */
1610 pipeline->hdr.size = sizeof(*pipeline);
1611 pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
1612 pipeline->pipeline_id = index;
1613 pipeline->comp_id = swidget->comp_id;
1615 /* component at start of pipeline is our stream id */
1616 comp_swidget = snd_sof_find_swidget(scomp, tw->sname);
1617 if (!comp_swidget) {
1618 dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n",
1619 tw->name, tw->sname);
1624 pipeline->sched_id = comp_swidget->comp_id;
1626 dev_dbg(scomp->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n",
1627 pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id);
1629 ret = sof_parse_tokens(scomp, pipeline, sched_tokens,
1630 ARRAY_SIZE(sched_tokens), private->array,
1631 le32_to_cpu(private->size));
1633 dev_err(scomp->dev, "error: parse pipeline tokens failed %d\n",
1638 dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d\n",
1639 swidget->widget->name, pipeline->period, pipeline->priority,
1640 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched);
1642 swidget->private = pipeline;
1644 /* send ipc's to create pipeline comp and power up schedule core */
1645 ret = sof_load_pipeline_ipc(scomp->dev, pipeline, r);
1657 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index,
1658 struct snd_sof_widget *swidget,
1659 struct snd_soc_tplg_dapm_widget *tw,
1660 struct sof_ipc_comp_reply *r)
1662 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1663 struct snd_soc_tplg_private *private = &tw->priv;
1664 struct sof_ipc_comp_mixer *mixer;
1667 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
1671 /* configure mixer IPC message */
1672 mixer->comp.hdr.size = sizeof(*mixer);
1673 mixer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1674 mixer->comp.id = swidget->comp_id;
1675 mixer->comp.type = SOF_COMP_MIXER;
1676 mixer->comp.pipeline_id = index;
1677 mixer->config.hdr.size = sizeof(mixer->config);
1679 ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens,
1680 ARRAY_SIZE(comp_tokens), private->array,
1681 le32_to_cpu(private->size));
1683 dev_err(scomp->dev, "error: parse mixer.cfg tokens failed %d\n",
1689 sof_dbg_comp_config(scomp, &mixer->config);
1691 swidget->private = mixer;
1693 ret = sof_ipc_tx_message(sdev->ipc, mixer->comp.hdr.cmd, mixer,
1694 sizeof(*mixer), r, sizeof(*r));
1704 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1705 struct snd_sof_widget *swidget,
1706 struct snd_soc_tplg_dapm_widget *tw,
1707 struct sof_ipc_comp_reply *r)
1709 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1710 struct snd_soc_tplg_private *private = &tw->priv;
1711 struct sof_ipc_comp_mux *mux;
1714 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
1718 /* configure mux IPC message */
1719 mux->comp.hdr.size = sizeof(*mux);
1720 mux->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1721 mux->comp.id = swidget->comp_id;
1722 mux->comp.type = SOF_COMP_MUX;
1723 mux->comp.pipeline_id = index;
1724 mux->config.hdr.size = sizeof(mux->config);
1726 ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1727 ARRAY_SIZE(comp_tokens), private->array,
1728 le32_to_cpu(private->size));
1730 dev_err(scomp->dev, "error: parse mux.cfg tokens failed %d\n",
1736 sof_dbg_comp_config(scomp, &mux->config);
1738 swidget->private = mux;
1740 ret = sof_ipc_tx_message(sdev->ipc, mux->comp.hdr.cmd, mux,
1741 sizeof(*mux), r, sizeof(*r));
1752 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index,
1753 struct snd_sof_widget *swidget,
1754 struct snd_soc_tplg_dapm_widget *tw,
1755 struct sof_ipc_comp_reply *r)
1757 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1758 struct snd_soc_tplg_private *private = &tw->priv;
1759 struct sof_ipc_comp_volume *volume;
1760 struct snd_sof_control *scontrol;
1765 volume = kzalloc(sizeof(*volume), GFP_KERNEL);
1769 if (!le32_to_cpu(tw->num_kcontrols)) {
1770 dev_err(scomp->dev, "error: invalid kcontrol count %d for volume\n",
1776 /* configure volume IPC message */
1777 volume->comp.hdr.size = sizeof(*volume);
1778 volume->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1779 volume->comp.id = swidget->comp_id;
1780 volume->comp.type = SOF_COMP_VOLUME;
1781 volume->comp.pipeline_id = index;
1782 volume->config.hdr.size = sizeof(volume->config);
1784 ret = sof_parse_tokens(scomp, volume, volume_tokens,
1785 ARRAY_SIZE(volume_tokens), private->array,
1786 le32_to_cpu(private->size));
1788 dev_err(scomp->dev, "error: parse volume tokens failed %d\n",
1792 ret = sof_parse_tokens(scomp, &volume->config, comp_tokens,
1793 ARRAY_SIZE(comp_tokens), private->array,
1794 le32_to_cpu(private->size));
1796 dev_err(scomp->dev, "error: parse volume.cfg tokens failed %d\n",
1797 le32_to_cpu(private->size));
1801 sof_dbg_comp_config(scomp, &volume->config);
1803 swidget->private = volume;
1805 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1806 if (scontrol->comp_id == swidget->comp_id &&
1807 scontrol->volume_table) {
1808 min_step = scontrol->min_volume_step;
1809 max_step = scontrol->max_volume_step;
1810 volume->min_value = scontrol->volume_table[min_step];
1811 volume->max_value = scontrol->volume_table[max_step];
1812 volume->channels = scontrol->num_channels;
1817 ret = sof_ipc_tx_message(sdev->ipc, volume->comp.hdr.cmd, volume,
1818 sizeof(*volume), r, sizeof(*r));
1830 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1831 struct snd_sof_widget *swidget,
1832 struct snd_soc_tplg_dapm_widget *tw,
1833 struct sof_ipc_comp_reply *r)
1835 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1836 struct snd_soc_tplg_private *private = &tw->priv;
1837 struct sof_ipc_comp_src *src;
1840 src = kzalloc(sizeof(*src), GFP_KERNEL);
1844 /* configure src IPC message */
1845 src->comp.hdr.size = sizeof(*src);
1846 src->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1847 src->comp.id = swidget->comp_id;
1848 src->comp.type = SOF_COMP_SRC;
1849 src->comp.pipeline_id = index;
1850 src->config.hdr.size = sizeof(src->config);
1852 ret = sof_parse_tokens(scomp, src, src_tokens,
1853 ARRAY_SIZE(src_tokens), private->array,
1854 le32_to_cpu(private->size));
1856 dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1861 ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1862 ARRAY_SIZE(comp_tokens), private->array,
1863 le32_to_cpu(private->size));
1865 dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1866 le32_to_cpu(private->size));
1870 dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1871 swidget->widget->name, src->source_rate, src->sink_rate);
1872 sof_dbg_comp_config(scomp, &src->config);
1874 swidget->private = src;
1876 ret = sof_ipc_tx_message(sdev->ipc, src->comp.hdr.cmd, src,
1877 sizeof(*src), r, sizeof(*r));
1889 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1890 struct snd_sof_widget *swidget,
1891 struct snd_soc_tplg_dapm_widget *tw,
1892 struct sof_ipc_comp_reply *r)
1894 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1895 struct snd_soc_tplg_private *private = &tw->priv;
1896 struct sof_ipc_comp_asrc *asrc;
1899 asrc = kzalloc(sizeof(*asrc), GFP_KERNEL);
1903 /* configure ASRC IPC message */
1904 asrc->comp.hdr.size = sizeof(*asrc);
1905 asrc->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1906 asrc->comp.id = swidget->comp_id;
1907 asrc->comp.type = SOF_COMP_ASRC;
1908 asrc->comp.pipeline_id = index;
1909 asrc->config.hdr.size = sizeof(asrc->config);
1911 ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1912 ARRAY_SIZE(asrc_tokens), private->array,
1913 le32_to_cpu(private->size));
1915 dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1920 ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1921 ARRAY_SIZE(comp_tokens), private->array,
1922 le32_to_cpu(private->size));
1924 dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1925 le32_to_cpu(private->size));
1929 dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1930 "asynch %d operation %d\n",
1931 swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1932 asrc->asynchronous_mode, asrc->operation_mode);
1933 sof_dbg_comp_config(scomp, &asrc->config);
1935 swidget->private = asrc;
1937 ret = sof_ipc_tx_message(sdev->ipc, asrc->comp.hdr.cmd, asrc,
1938 sizeof(*asrc), r, sizeof(*r));
1947 * Signal Generator Topology
1950 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1951 struct snd_sof_widget *swidget,
1952 struct snd_soc_tplg_dapm_widget *tw,
1953 struct sof_ipc_comp_reply *r)
1955 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1956 struct snd_soc_tplg_private *private = &tw->priv;
1957 struct sof_ipc_comp_tone *tone;
1960 tone = kzalloc(sizeof(*tone), GFP_KERNEL);
1964 /* configure siggen IPC message */
1965 tone->comp.hdr.size = sizeof(*tone);
1966 tone->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1967 tone->comp.id = swidget->comp_id;
1968 tone->comp.type = SOF_COMP_TONE;
1969 tone->comp.pipeline_id = index;
1970 tone->config.hdr.size = sizeof(tone->config);
1972 ret = sof_parse_tokens(scomp, tone, tone_tokens,
1973 ARRAY_SIZE(tone_tokens), private->array,
1974 le32_to_cpu(private->size));
1976 dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
1977 le32_to_cpu(private->size));
1981 ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1982 ARRAY_SIZE(comp_tokens), private->array,
1983 le32_to_cpu(private->size));
1985 dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
1986 le32_to_cpu(private->size));
1990 dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
1991 swidget->widget->name, tone->frequency, tone->amplitude);
1992 sof_dbg_comp_config(scomp, &tone->config);
1994 swidget->private = tone;
1996 ret = sof_ipc_tx_message(sdev->ipc, tone->comp.hdr.cmd, tone,
1997 sizeof(*tone), r, sizeof(*r));
2005 static int sof_get_control_data(struct snd_soc_component *scomp,
2006 struct snd_soc_dapm_widget *widget,
2007 struct sof_widget_data *wdata,
2010 const struct snd_kcontrol_new *kc;
2011 struct soc_mixer_control *sm;
2012 struct soc_bytes_ext *sbe;
2013 struct soc_enum *se;
2018 for (i = 0; i < widget->num_kcontrols; i++) {
2019 kc = &widget->kcontrol_news[i];
2021 switch (widget->dobj.widget.kcontrol_type) {
2022 case SND_SOC_TPLG_TYPE_MIXER:
2023 sm = (struct soc_mixer_control *)kc->private_value;
2024 wdata[i].control = sm->dobj.private;
2026 case SND_SOC_TPLG_TYPE_BYTES:
2027 sbe = (struct soc_bytes_ext *)kc->private_value;
2028 wdata[i].control = sbe->dobj.private;
2030 case SND_SOC_TPLG_TYPE_ENUM:
2031 se = (struct soc_enum *)kc->private_value;
2032 wdata[i].control = se->dobj.private;
2035 dev_err(scomp->dev, "error: unknown kcontrol type %d in widget %s\n",
2036 widget->dobj.widget.kcontrol_type,
2041 if (!wdata[i].control) {
2042 dev_err(scomp->dev, "error: no scontrol for widget %s\n",
2047 wdata[i].pdata = wdata[i].control->control_data->data;
2048 if (!wdata[i].pdata)
2051 /* make sure data is valid - data can be updated at runtime */
2052 if (wdata[i].pdata->magic != SOF_ABI_MAGIC)
2055 *size += wdata[i].pdata->size;
2058 switch (wdata[i].control->cmd) {
2059 case SOF_CTRL_CMD_VOLUME:
2060 case SOF_CTRL_CMD_ENUM:
2061 case SOF_CTRL_CMD_SWITCH:
2062 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
2063 wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
2065 case SOF_CTRL_CMD_BINARY:
2066 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
2067 wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
2077 static int sof_process_load(struct snd_soc_component *scomp, int index,
2078 struct snd_sof_widget *swidget,
2079 struct snd_soc_tplg_dapm_widget *tw,
2080 struct sof_ipc_comp_reply *r,
2083 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2084 struct snd_soc_dapm_widget *widget = swidget->widget;
2085 struct snd_soc_tplg_private *private = &tw->priv;
2086 struct sof_ipc_comp_process *process = NULL;
2087 struct sof_widget_data *wdata = NULL;
2088 size_t ipc_data_size = 0;
2094 if (type == SOF_COMP_NONE) {
2095 dev_err(scomp->dev, "error: invalid process comp type %d\n",
2100 /* allocate struct for widget control data sizes and types */
2101 if (widget->num_kcontrols) {
2102 wdata = kcalloc(widget->num_kcontrols,
2109 /* get possible component controls and get size of all pdata */
2110 ret = sof_get_control_data(scomp, widget, wdata,
2117 ipc_size = sizeof(struct sof_ipc_comp_process) +
2118 le32_to_cpu(private->size) +
2121 /* we are exceeding max ipc size, config needs to be sent separately */
2122 if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2123 ipc_size -= ipc_data_size;
2127 process = kzalloc(ipc_size, GFP_KERNEL);
2133 /* configure iir IPC message */
2134 process->comp.hdr.size = ipc_size;
2135 process->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
2136 process->comp.id = swidget->comp_id;
2137 process->comp.type = type;
2138 process->comp.pipeline_id = index;
2139 process->config.hdr.size = sizeof(process->config);
2141 ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2142 ARRAY_SIZE(comp_tokens), private->array,
2143 le32_to_cpu(private->size));
2145 dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2146 le32_to_cpu(private->size));
2150 sof_dbg_comp_config(scomp, &process->config);
2153 * found private data in control, so copy it.
2154 * get possible component controls - get size of all pdata,
2155 * then memcpy with headers
2157 if (ipc_data_size) {
2158 for (i = 0; i < widget->num_kcontrols; i++) {
2159 memcpy(&process->data + offset,
2160 wdata[i].pdata->data,
2161 wdata[i].pdata->size);
2162 offset += wdata[i].pdata->size;
2166 process->size = ipc_data_size;
2167 swidget->private = process;
2169 ret = sof_ipc_tx_message(sdev->ipc, process->comp.hdr.cmd, process,
2170 ipc_size, r, sizeof(*r));
2173 dev_err(scomp->dev, "error: create process failed\n");
2177 /* we sent the data in single message so return */
2181 /* send control data with large message supported method */
2182 for (i = 0; i < widget->num_kcontrols; i++) {
2183 wdata[i].control->readback_offset = 0;
2184 ret = snd_sof_ipc_set_get_comp_data(wdata[i].control,
2187 wdata[i].control->cmd,
2190 dev_err(scomp->dev, "error: send control failed\n");
2204 * Processing Component Topology - can be "effect", "codec", or general
2208 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2209 struct snd_sof_widget *swidget,
2210 struct snd_soc_tplg_dapm_widget *tw,
2211 struct sof_ipc_comp_reply *r)
2213 struct snd_soc_tplg_private *private = &tw->priv;
2214 struct sof_ipc_comp_process config;
2217 /* check we have some tokens - we need at least process type */
2218 if (le32_to_cpu(private->size) == 0) {
2219 dev_err(scomp->dev, "error: process tokens not found\n");
2223 memset(&config, 0, sizeof(config));
2225 /* get the process token */
2226 ret = sof_parse_tokens(scomp, &config, process_tokens,
2227 ARRAY_SIZE(process_tokens), private->array,
2228 le32_to_cpu(private->size));
2230 dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2231 le32_to_cpu(private->size));
2235 /* now load process specific data and send IPC */
2236 ret = sof_process_load(scomp, index, swidget, tw, r,
2237 find_process_comp_type(config.type));
2239 dev_err(scomp->dev, "error: process loading failed\n");
2246 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2247 struct snd_sof_widget *swidget,
2250 struct sof_ipc_comp *ipc_comp;
2252 /* validate widget event type */
2253 switch (event_type) {
2254 case SOF_KEYWORD_DETECT_DAPM_EVENT:
2255 /* only KEYWORD_DETECT comps should handle this */
2256 if (swidget->id != snd_soc_dapm_effect)
2259 ipc_comp = swidget->private;
2260 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2263 /* bind event to keyword detect comp */
2264 return snd_soc_tplg_widget_bind_event(swidget->widget,
2266 ARRAY_SIZE(sof_kwd_events),
2273 "error: invalid event type %d for widget %s\n",
2274 event_type, swidget->widget->name);
2278 /* external widget init - used for any driver specific init */
2279 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2280 struct snd_soc_dapm_widget *w,
2281 struct snd_soc_tplg_dapm_widget *tw)
2283 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2284 struct snd_sof_widget *swidget;
2285 struct snd_sof_dai *dai;
2286 struct sof_ipc_comp_reply reply;
2287 struct snd_sof_control *scontrol;
2290 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2294 swidget->scomp = scomp;
2295 swidget->widget = w;
2296 swidget->comp_id = sdev->next_comp_id++;
2297 swidget->complete = 0;
2298 swidget->id = w->id;
2299 swidget->pipeline_id = index;
2300 swidget->private = NULL;
2301 memset(&reply, 0, sizeof(reply));
2303 dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2304 swidget->comp_id, index, swidget->id, tw->name,
2305 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2306 ? tw->sname : "none");
2308 /* handle any special case widgets */
2310 case snd_soc_dapm_dai_in:
2311 case snd_soc_dapm_dai_out:
2312 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2318 ret = sof_widget_load_dai(scomp, index, swidget, tw, &reply,
2321 sof_connect_dai_widget(scomp, w, tw, dai);
2322 list_add(&dai->list, &sdev->dai_list);
2323 swidget->private = dai;
2328 case snd_soc_dapm_mixer:
2329 ret = sof_widget_load_mixer(scomp, index, swidget, tw, &reply);
2331 case snd_soc_dapm_pga:
2332 ret = sof_widget_load_pga(scomp, index, swidget, tw, &reply);
2333 /* Find scontrol for this pga and set readback offset*/
2334 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2335 if (scontrol->comp_id == swidget->comp_id) {
2336 scontrol->readback_offset = reply.offset;
2341 case snd_soc_dapm_buffer:
2342 ret = sof_widget_load_buffer(scomp, index, swidget, tw, &reply);
2344 case snd_soc_dapm_scheduler:
2345 ret = sof_widget_load_pipeline(scomp, index, swidget, tw,
2348 case snd_soc_dapm_aif_out:
2349 ret = sof_widget_load_pcm(scomp, index, swidget,
2350 SOF_IPC_STREAM_CAPTURE, tw, &reply);
2352 case snd_soc_dapm_aif_in:
2353 ret = sof_widget_load_pcm(scomp, index, swidget,
2354 SOF_IPC_STREAM_PLAYBACK, tw, &reply);
2356 case snd_soc_dapm_src:
2357 ret = sof_widget_load_src(scomp, index, swidget, tw, &reply);
2359 case snd_soc_dapm_asrc:
2360 ret = sof_widget_load_asrc(scomp, index, swidget, tw, &reply);
2362 case snd_soc_dapm_siggen:
2363 ret = sof_widget_load_siggen(scomp, index, swidget, tw, &reply);
2365 case snd_soc_dapm_effect:
2366 ret = sof_widget_load_process(scomp, index, swidget, tw,
2369 case snd_soc_dapm_mux:
2370 case snd_soc_dapm_demux:
2371 ret = sof_widget_load_mux(scomp, index, swidget, tw, &reply);
2373 case snd_soc_dapm_switch:
2374 case snd_soc_dapm_dai_link:
2375 case snd_soc_dapm_kcontrol:
2377 dev_warn(scomp->dev, "warning: widget type %d name %s not handled\n",
2378 swidget->id, tw->name);
2382 /* check IPC reply */
2383 if (ret < 0 || reply.rhdr.error < 0) {
2385 "error: DSP failed to add widget id %d type %d name : %s stream %s reply %d\n",
2386 tw->shift, swidget->id, tw->name,
2387 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2388 ? tw->sname : "none", reply.rhdr.error);
2393 /* bind widget to external event */
2394 if (tw->event_type) {
2395 ret = sof_widget_bind_event(scomp, swidget,
2396 le16_to_cpu(tw->event_type));
2398 dev_err(scomp->dev, "error: widget event binding failed\n");
2399 kfree(swidget->private);
2405 w->dobj.private = swidget;
2406 list_add(&swidget->list, &sdev->widget_list);
2410 static int sof_route_unload(struct snd_soc_component *scomp,
2411 struct snd_soc_dobj *dobj)
2413 struct snd_sof_route *sroute;
2415 sroute = dobj->private;
2419 /* free sroute and its private data */
2420 kfree(sroute->private);
2421 list_del(&sroute->list);
2427 static int sof_widget_unload(struct snd_soc_component *scomp,
2428 struct snd_soc_dobj *dobj)
2430 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2431 const struct snd_kcontrol_new *kc;
2432 struct snd_soc_dapm_widget *widget;
2433 struct sof_ipc_pipe_new *pipeline;
2434 struct snd_sof_control *scontrol;
2435 struct snd_sof_widget *swidget;
2436 struct soc_mixer_control *sm;
2437 struct soc_bytes_ext *sbe;
2438 struct snd_sof_dai *dai;
2439 struct soc_enum *se;
2443 swidget = dobj->private;
2447 widget = swidget->widget;
2449 switch (swidget->id) {
2450 case snd_soc_dapm_dai_in:
2451 case snd_soc_dapm_dai_out:
2452 dai = swidget->private;
2455 /* free dai config */
2456 kfree(dai->dai_config);
2457 list_del(&dai->list);
2460 case snd_soc_dapm_scheduler:
2462 /* power down the pipeline schedule core */
2463 pipeline = swidget->private;
2464 ret = snd_sof_dsp_core_power_down(sdev, 1 << pipeline->core);
2466 dev_err(scomp->dev, "error: powering down pipeline schedule core %d\n",
2469 /* update enabled cores mask */
2470 sdev->enabled_cores_mask &= ~(1 << pipeline->core);
2476 for (i = 0; i < widget->num_kcontrols; i++) {
2477 kc = &widget->kcontrol_news[i];
2478 switch (dobj->widget.kcontrol_type) {
2479 case SND_SOC_TPLG_TYPE_MIXER:
2480 sm = (struct soc_mixer_control *)kc->private_value;
2481 scontrol = sm->dobj.private;
2483 kfree(scontrol->volume_table);
2485 case SND_SOC_TPLG_TYPE_ENUM:
2486 se = (struct soc_enum *)kc->private_value;
2487 scontrol = se->dobj.private;
2489 case SND_SOC_TPLG_TYPE_BYTES:
2490 sbe = (struct soc_bytes_ext *)kc->private_value;
2491 scontrol = sbe->dobj.private;
2494 dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2497 kfree(scontrol->control_data);
2498 list_del(&scontrol->list);
2503 /* free private value */
2504 kfree(swidget->private);
2506 /* remove and free swidget object */
2507 list_del(&swidget->list);
2514 * DAI HW configuration.
2517 /* FE DAI - used for any driver specific init */
2518 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2519 struct snd_soc_dai_driver *dai_drv,
2520 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2522 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2523 struct snd_soc_tplg_stream_caps *caps;
2524 struct snd_soc_tplg_private *private = &pcm->priv;
2525 struct snd_sof_pcm *spcm;
2529 /* nothing to do for BEs atm */
2533 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2537 spcm->scomp = scomp;
2539 for_each_pcm_streams(stream) {
2540 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
2541 INIT_WORK(&spcm->stream[stream].period_elapsed_work,
2542 snd_sof_pcm_period_elapsed_work);
2546 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2548 dai_drv->dobj.private = spcm;
2549 list_add(&spcm->list, &sdev->pcm_list);
2551 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2552 ARRAY_SIZE(stream_tokens), private->array,
2553 le32_to_cpu(private->size));
2555 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2556 le32_to_cpu(private->size));
2560 /* do we need to allocate playback PCM DMA pages */
2561 if (!spcm->pcm.playback)
2564 stream = SNDRV_PCM_STREAM_PLAYBACK;
2566 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2567 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2569 caps = &spcm->pcm.caps[stream];
2571 /* allocate playback page table buffer */
2572 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2573 PAGE_SIZE, &spcm->stream[stream].page_table);
2575 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2581 /* bind pcm to host comp */
2582 ret = spcm_bind(scomp, spcm, stream);
2585 "error: can't bind pcm to host\n");
2586 goto free_playback_tables;
2590 stream = SNDRV_PCM_STREAM_CAPTURE;
2592 /* do we need to allocate capture PCM DMA pages */
2593 if (!spcm->pcm.capture)
2596 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2597 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2599 caps = &spcm->pcm.caps[stream];
2601 /* allocate capture page table buffer */
2602 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2603 PAGE_SIZE, &spcm->stream[stream].page_table);
2605 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2607 goto free_playback_tables;
2610 /* bind pcm to host comp */
2611 ret = spcm_bind(scomp, spcm, stream);
2614 "error: can't bind pcm to host\n");
2615 snd_dma_free_pages(&spcm->stream[stream].page_table);
2616 goto free_playback_tables;
2621 free_playback_tables:
2622 if (spcm->pcm.playback)
2623 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2628 static int sof_dai_unload(struct snd_soc_component *scomp,
2629 struct snd_soc_dobj *dobj)
2631 struct snd_sof_pcm *spcm = dobj->private;
2633 /* free PCM DMA pages */
2634 if (spcm->pcm.playback)
2635 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2637 if (spcm->pcm.capture)
2638 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2640 /* remove from list and free spcm */
2641 list_del(&spcm->list);
2647 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2648 struct sof_ipc_dai_config *config)
2650 /* clock directions wrt codec */
2651 if (hw_config->bclk_master == SND_SOC_TPLG_BCLK_CM) {
2652 /* codec is bclk master */
2653 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2654 config->format |= SOF_DAI_FMT_CBM_CFM;
2656 config->format |= SOF_DAI_FMT_CBM_CFS;
2658 /* codec is bclk slave */
2659 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2660 config->format |= SOF_DAI_FMT_CBS_CFM;
2662 config->format |= SOF_DAI_FMT_CBS_CFS;
2665 /* inverted clocks ? */
2666 if (hw_config->invert_bclk) {
2667 if (hw_config->invert_fsync)
2668 config->format |= SOF_DAI_FMT_IB_IF;
2670 config->format |= SOF_DAI_FMT_IB_NF;
2672 if (hw_config->invert_fsync)
2673 config->format |= SOF_DAI_FMT_NB_IF;
2675 config->format |= SOF_DAI_FMT_NB_NF;
2680 * Send IPC and set the same config for all DAIs with name matching the link
2681 * name. Note that the function can only be used for the case that all DAIs
2682 * have a common DAI config for now.
2684 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2685 struct snd_soc_dai_link *link,
2686 struct sof_ipc_dai_config *config)
2688 struct snd_sof_dai *dai;
2691 list_for_each_entry(dai, &sdev->dai_list, list) {
2695 if (strcmp(link->name, dai->name) == 0) {
2696 struct sof_ipc_reply reply;
2700 * the same dai config will be applied to all DAIs in
2701 * the same dai link. We have to ensure that the ipc
2702 * dai config's dai_index match to the component's
2705 config->dai_index = dai->comp_dai.dai_index;
2707 /* send message to DSP */
2708 ret = sof_ipc_tx_message(sdev->ipc,
2709 config->hdr.cmd, config, size,
2710 &reply, sizeof(reply));
2713 dev_err(sdev->dev, "error: failed to set DAI config for %s index %d\n",
2714 dai->name, config->dai_index);
2717 dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2718 if (!dai->dai_config)
2721 /* set cpu_dai_name */
2722 dai->cpu_dai_name = link->cpus->dai_name;
2729 * machine driver may define a dai link with playback and capture
2730 * dai enabled, but the dai link in topology would support both, one
2731 * or none of them. Here print a warning message to notify user
2734 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2741 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2742 struct snd_soc_dai_link *link,
2743 struct snd_soc_tplg_link_config *cfg,
2744 struct snd_soc_tplg_hw_config *hw_config,
2745 struct sof_ipc_dai_config *config)
2747 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2748 struct snd_soc_tplg_private *private = &cfg->priv;
2749 u32 size = sizeof(*config);
2752 /* handle master/slave and inverted clocks */
2753 sof_dai_set_format(hw_config, config);
2756 memset(&config->ssp, 0, sizeof(struct sof_ipc_dai_ssp_params));
2757 config->hdr.size = size;
2759 ret = sof_parse_tokens(scomp, &config->ssp, ssp_tokens,
2760 ARRAY_SIZE(ssp_tokens), private->array,
2761 le32_to_cpu(private->size));
2763 dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2764 le32_to_cpu(private->size));
2768 config->ssp.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2769 config->ssp.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2770 config->ssp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2771 config->ssp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2772 config->ssp.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2773 config->ssp.mclk_direction = hw_config->mclk_direction;
2774 config->ssp.rx_slots = le32_to_cpu(hw_config->rx_slots);
2775 config->ssp.tx_slots = le32_to_cpu(hw_config->tx_slots);
2777 dev_dbg(scomp->dev, "tplg: config SSP%d fmt 0x%x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d\n",
2778 config->dai_index, config->format,
2779 config->ssp.mclk_rate, config->ssp.bclk_rate,
2780 config->ssp.fsync_rate, config->ssp.sample_valid_bits,
2781 config->ssp.tdm_slot_width, config->ssp.tdm_slots,
2782 config->ssp.mclk_id, config->ssp.quirks);
2784 /* validate SSP fsync rate and channel count */
2785 if (config->ssp.fsync_rate < 8000 || config->ssp.fsync_rate > 192000) {
2786 dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2791 if (config->ssp.tdm_slots < 1 || config->ssp.tdm_slots > 8) {
2792 dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2797 /* set config for all DAI's with name matching the link name */
2798 ret = sof_set_dai_config(sdev, size, link, config);
2800 dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2806 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2807 struct snd_soc_dai_link *link,
2808 struct snd_soc_tplg_link_config *cfg,
2809 struct snd_soc_tplg_hw_config *hw_config,
2810 struct sof_ipc_dai_config *config)
2812 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2813 struct snd_soc_tplg_private *private = &cfg->priv;
2814 u32 size = sizeof(*config);
2817 /* handle master/slave and inverted clocks */
2818 sof_dai_set_format(hw_config, config);
2821 memset(&config->sai, 0, sizeof(struct sof_ipc_dai_sai_params));
2822 config->hdr.size = size;
2824 ret = sof_parse_tokens(scomp, &config->sai, sai_tokens,
2825 ARRAY_SIZE(sai_tokens), private->array,
2826 le32_to_cpu(private->size));
2828 dev_err(scomp->dev, "error: parse sai tokens failed %d\n",
2829 le32_to_cpu(private->size));
2833 config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2834 config->sai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2835 config->sai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2836 config->sai.mclk_direction = hw_config->mclk_direction;
2838 config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2839 config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2840 config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2841 config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2843 dev_info(scomp->dev,
2844 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2845 config->dai_index, config->format,
2846 config->sai.mclk_rate, config->sai.tdm_slot_width,
2847 config->sai.tdm_slots, config->sai.mclk_id);
2849 if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
2850 dev_err(scomp->dev, "error: invalid channel count for SAI%d\n",
2855 /* set config for all DAI's with name matching the link name */
2856 ret = sof_set_dai_config(sdev, size, link, config);
2858 dev_err(scomp->dev, "error: failed to save DAI config for SAI%d\n",
2864 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2865 struct snd_soc_dai_link *link,
2866 struct snd_soc_tplg_link_config *cfg,
2867 struct snd_soc_tplg_hw_config *hw_config,
2868 struct sof_ipc_dai_config *config)
2870 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2871 struct snd_soc_tplg_private *private = &cfg->priv;
2872 u32 size = sizeof(*config);
2875 /* handle master/slave and inverted clocks */
2876 sof_dai_set_format(hw_config, config);
2879 memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2880 config->hdr.size = size;
2882 ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2883 ARRAY_SIZE(esai_tokens), private->array,
2884 le32_to_cpu(private->size));
2886 dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2887 le32_to_cpu(private->size));
2891 config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2892 config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2893 config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2894 config->esai.mclk_direction = hw_config->mclk_direction;
2895 config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2896 config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2897 config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2898 config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2900 dev_info(scomp->dev,
2901 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2902 config->dai_index, config->format,
2903 config->esai.mclk_rate, config->esai.tdm_slot_width,
2904 config->esai.tdm_slots, config->esai.mclk_id);
2906 if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2907 dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2912 /* set config for all DAI's with name matching the link name */
2913 ret = sof_set_dai_config(sdev, size, link, config);
2915 dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2921 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
2922 struct snd_soc_dai_link *link,
2923 struct snd_soc_tplg_link_config *cfg,
2924 struct snd_soc_tplg_hw_config *hw_config,
2925 struct sof_ipc_dai_config *config)
2927 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2928 struct snd_soc_tplg_private *private = &cfg->priv;
2929 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
2930 struct sof_ipc_fw_version *v = &ready->version;
2931 size_t size = sizeof(*config);
2934 /* Ensure the entire DMIC config struct is zeros */
2935 memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
2937 /* get DMIC tokens */
2938 ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
2939 ARRAY_SIZE(dmic_tokens), private->array,
2940 le32_to_cpu(private->size));
2942 dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
2943 le32_to_cpu(private->size));
2948 * alloc memory for private member
2949 * Used to track the pdm config array index currently being parsed
2951 sdev->private = kzalloc(sizeof(u32), GFP_KERNEL);
2955 /* get DMIC PDM tokens */
2956 ret = sof_parse_token_sets(scomp, &config->dmic.pdm[0], dmic_pdm_tokens,
2957 ARRAY_SIZE(dmic_pdm_tokens), private->array,
2958 le32_to_cpu(private->size),
2959 config->dmic.num_pdm_active,
2960 sizeof(struct sof_ipc_dai_dmic_pdm_ctrl));
2963 dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
2964 le32_to_cpu(private->size));
2968 /* set IPC header size */
2969 config->hdr.size = size;
2971 /* debug messages */
2972 dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
2973 config->dai_index, config->dmic.driver_ipc_version);
2974 dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
2975 config->dmic.pdmclk_min, config->dmic.pdmclk_max,
2976 config->dmic.duty_min);
2977 dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
2978 config->dmic.duty_max, config->dmic.fifo_fs,
2979 config->dmic.num_pdm_active);
2980 dev_dbg(scomp->dev, "fifo word length %hd\n", config->dmic.fifo_bits);
2982 for (j = 0; j < config->dmic.num_pdm_active; j++) {
2983 dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
2984 config->dmic.pdm[j].id,
2985 config->dmic.pdm[j].enable_mic_a,
2986 config->dmic.pdm[j].enable_mic_b);
2987 dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
2988 config->dmic.pdm[j].id,
2989 config->dmic.pdm[j].polarity_mic_a,
2990 config->dmic.pdm[j].polarity_mic_b);
2991 dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
2992 config->dmic.pdm[j].id,
2993 config->dmic.pdm[j].clk_edge,
2994 config->dmic.pdm[j].skew);
2998 * this takes care of backwards compatible handling of fifo_bits_b.
2999 * It is deprecated since firmware ABI version 3.0.1.
3001 if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
3002 config->dmic.fifo_bits_b = config->dmic.fifo_bits;
3004 /* set config for all DAI's with name matching the link name */
3005 ret = sof_set_dai_config(sdev, size, link, config);
3007 dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
3011 kfree(sdev->private);
3016 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
3017 struct snd_soc_dai_link *link,
3018 struct snd_soc_tplg_link_config *cfg,
3019 struct snd_soc_tplg_hw_config *hw_config,
3020 struct sof_ipc_dai_config *config)
3022 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3023 struct snd_soc_tplg_private *private = &cfg->priv;
3024 struct snd_soc_dai *dai;
3025 u32 size = sizeof(*config);
3029 memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
3030 config->hdr.size = size;
3032 /* get any bespoke DAI tokens */
3033 ret = sof_parse_tokens(scomp, &config->hda, hda_tokens,
3034 ARRAY_SIZE(hda_tokens), private->array,
3035 le32_to_cpu(private->size));
3037 dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
3038 le32_to_cpu(private->size));
3042 dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
3043 config->hda.rate, config->hda.channels);
3045 dai = snd_soc_find_dai(link->cpus);
3047 dev_err(scomp->dev, "error: failed to find dai %s in %s",
3048 link->cpus->dai_name, __func__);
3052 config->hda.link_dma_ch = DMA_CHAN_INVALID;
3054 ret = sof_set_dai_config(sdev, size, link, config);
3056 dev_err(scomp->dev, "error: failed to process hda dai link %s",
3062 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3063 struct snd_soc_dai_link *link,
3064 struct snd_soc_tplg_link_config *cfg,
3065 struct snd_soc_tplg_hw_config *hw_config,
3066 struct sof_ipc_dai_config *config)
3068 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3069 struct snd_soc_tplg_private *private = &cfg->priv;
3070 u32 size = sizeof(*config);
3073 ret = sof_parse_tokens(scomp, &config->alh, alh_tokens,
3074 ARRAY_SIZE(alh_tokens), private->array,
3075 le32_to_cpu(private->size));
3077 dev_err(scomp->dev, "error: parse alh tokens failed %d\n",
3078 le32_to_cpu(private->size));
3083 config->hdr.size = size;
3085 /* set config for all DAI's with name matching the link name */
3086 ret = sof_set_dai_config(sdev, size, link, config);
3088 dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3094 /* DAI link - used for any driver specific init */
3095 static int sof_link_load(struct snd_soc_component *scomp, int index,
3096 struct snd_soc_dai_link *link,
3097 struct snd_soc_tplg_link_config *cfg)
3099 struct snd_soc_tplg_private *private = &cfg->priv;
3100 struct sof_ipc_dai_config config;
3101 struct snd_soc_tplg_hw_config *hw_config;
3106 if (!link->platforms) {
3107 dev_err(scomp->dev, "error: no platforms\n");
3110 link->platforms->name = dev_name(scomp->dev);
3113 * Set nonatomic property for FE dai links as their trigger action
3116 if (!link->no_pcm) {
3117 link->nonatomic = true;
3120 * set default trigger order for all links. Exceptions to
3121 * the rule will be handled in sof_pcm_dai_link_fixup()
3122 * For playback, the sequence is the following: start FE,
3123 * start BE, stop BE, stop FE; for Capture the sequence is
3124 * inverted start BE, start FE, stop FE, stop BE
3126 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
3127 SND_SOC_DPCM_TRIGGER_PRE;
3128 link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
3129 SND_SOC_DPCM_TRIGGER_POST;
3131 /* nothing more to do for FE dai links */
3135 /* check we have some tokens - we need at least DAI type */
3136 if (le32_to_cpu(private->size) == 0) {
3137 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3141 /* Send BE DAI link configurations to DSP */
3142 memset(&config, 0, sizeof(config));
3144 /* get any common DAI tokens */
3145 ret = sof_parse_tokens(scomp, &config, dai_link_tokens,
3146 ARRAY_SIZE(dai_link_tokens), private->array,
3147 le32_to_cpu(private->size));
3149 dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3150 le32_to_cpu(private->size));
3155 * DAI links are expected to have at least 1 hw_config.
3156 * But some older topologies might have no hw_config for HDA dai links.
3158 num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
3159 if (!num_hw_configs) {
3160 if (config.type != SOF_DAI_INTEL_HDA) {
3161 dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3162 le32_to_cpu(cfg->num_hw_configs));
3166 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3167 cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3169 for (i = 0; i < num_hw_configs; i++) {
3170 if (cfg->hw_config[i].id == cfg->default_hw_config_id)
3174 if (i == num_hw_configs) {
3175 dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3176 le32_to_cpu(cfg->default_hw_config_id));
3181 /* configure dai IPC message */
3182 hw_config = &cfg->hw_config[i];
3184 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3185 config.format = le32_to_cpu(hw_config->fmt);
3187 /* now load DAI specific data and send IPC - type comes from token */
3188 switch (config.type) {
3189 case SOF_DAI_INTEL_SSP:
3190 ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config,
3193 case SOF_DAI_INTEL_DMIC:
3194 ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config,
3197 case SOF_DAI_INTEL_HDA:
3198 ret = sof_link_hda_load(scomp, index, link, cfg, hw_config,
3201 case SOF_DAI_INTEL_ALH:
3202 ret = sof_link_alh_load(scomp, index, link, cfg, hw_config,
3205 case SOF_DAI_IMX_SAI:
3206 ret = sof_link_sai_load(scomp, index, link, cfg, hw_config,
3209 case SOF_DAI_IMX_ESAI:
3210 ret = sof_link_esai_load(scomp, index, link, cfg, hw_config,
3214 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3225 static int sof_link_hda_unload(struct snd_sof_dev *sdev,
3226 struct snd_soc_dai_link *link)
3228 struct snd_soc_dai *dai;
3231 dai = snd_soc_find_dai(link->cpus);
3233 dev_err(sdev->dev, "error: failed to find dai %s in %s",
3234 link->cpus->dai_name, __func__);
3241 static int sof_link_unload(struct snd_soc_component *scomp,
3242 struct snd_soc_dobj *dobj)
3244 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3245 struct snd_soc_dai_link *link =
3246 container_of(dobj, struct snd_soc_dai_link, dobj);
3248 struct snd_sof_dai *sof_dai;
3251 /* only BE link is loaded by sof */
3255 list_for_each_entry(sof_dai, &sdev->dai_list, list) {
3259 if (strcmp(link->name, sof_dai->name) == 0)
3263 dev_err(scomp->dev, "error: failed to find dai %s in %s",
3264 link->name, __func__);
3268 switch (sof_dai->dai_config->type) {
3269 case SOF_DAI_INTEL_SSP:
3270 case SOF_DAI_INTEL_DMIC:
3271 case SOF_DAI_INTEL_ALH:
3272 case SOF_DAI_IMX_SAI:
3273 case SOF_DAI_IMX_ESAI:
3274 /* no resource needs to be released for all cases above */
3276 case SOF_DAI_INTEL_HDA:
3277 ret = sof_link_hda_unload(sdev, link);
3280 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3281 sof_dai->dai_config->type);
3289 /* DAI link - used for any driver specific init */
3290 static int sof_route_load(struct snd_soc_component *scomp, int index,
3291 struct snd_soc_dapm_route *route)
3293 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3294 struct sof_ipc_pipe_comp_connect *connect;
3295 struct snd_sof_widget *source_swidget, *sink_swidget;
3296 struct snd_soc_dobj *dobj = &route->dobj;
3297 struct snd_sof_route *sroute;
3298 struct sof_ipc_reply reply;
3301 /* allocate memory for sroute and connect */
3302 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3306 sroute->scomp = scomp;
3308 connect = kzalloc(sizeof(*connect), GFP_KERNEL);
3314 connect->hdr.size = sizeof(*connect);
3315 connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
3317 dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3318 route->sink, route->control ? route->control : "none",
3321 /* source component */
3322 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3323 if (!source_swidget) {
3324 dev_err(scomp->dev, "error: source %s not found\n",
3331 * Virtual widgets of type output/out_drv may be added in topology
3332 * for compatibility. These are not handled by the FW.
3333 * So, don't send routes whose source/sink widget is of such types
3336 if (source_swidget->id == snd_soc_dapm_out_drv ||
3337 source_swidget->id == snd_soc_dapm_output)
3340 connect->source_id = source_swidget->comp_id;
3342 /* sink component */
3343 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3344 if (!sink_swidget) {
3345 dev_err(scomp->dev, "error: sink %s not found\n",
3352 * Don't send routes whose sink widget is of type
3353 * output or out_drv to the DSP
3355 if (sink_swidget->id == snd_soc_dapm_out_drv ||
3356 sink_swidget->id == snd_soc_dapm_output)
3359 connect->sink_id = sink_swidget->comp_id;
3362 * For virtual routes, both sink and source are not
3363 * buffer. Since only buffer linked to component is supported by
3364 * FW, others are reported as error, add check in route function,
3365 * do not send it to FW when both source and sink are not buffer
3367 if (source_swidget->id != snd_soc_dapm_buffer &&
3368 sink_swidget->id != snd_soc_dapm_buffer) {
3369 dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3370 route->source, route->sink);
3374 ret = sof_ipc_tx_message(sdev->ipc,
3376 connect, sizeof(*connect),
3377 &reply, sizeof(reply));
3379 /* check IPC return value */
3381 dev_err(scomp->dev, "error: failed to add route sink %s control %s source %s\n",
3383 route->control ? route->control : "none",
3388 /* check IPC reply */
3389 if (reply.error < 0) {
3390 dev_err(scomp->dev, "error: DSP failed to add route sink %s control %s source %s result %d\n",
3392 route->control ? route->control : "none",
3393 route->source, reply.error);
3398 sroute->route = route;
3399 dobj->private = sroute;
3400 sroute->private = connect;
3402 /* add route to route list */
3403 list_add(&sroute->list, &sdev->route_list);
3414 /* Function to set the initial value of SOF kcontrols.
3415 * The value will be stored in scontrol->control_data
3417 static int snd_sof_cache_kcontrol_val(struct snd_soc_component *scomp)
3419 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3420 struct snd_sof_control *scontrol = NULL;
3421 int ipc_cmd, ctrl_type;
3424 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
3426 /* notify DSP of kcontrol values */
3427 switch (scontrol->cmd) {
3428 case SOF_CTRL_CMD_VOLUME:
3429 case SOF_CTRL_CMD_ENUM:
3430 case SOF_CTRL_CMD_SWITCH:
3431 ipc_cmd = SOF_IPC_COMP_GET_VALUE;
3432 ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_GET;
3434 case SOF_CTRL_CMD_BINARY:
3435 ipc_cmd = SOF_IPC_COMP_GET_DATA;
3436 ctrl_type = SOF_CTRL_TYPE_DATA_GET;
3440 "error: Invalid scontrol->cmd: %d\n",
3444 ret = snd_sof_ipc_set_get_comp_data(scontrol,
3449 dev_warn(scomp->dev,
3450 "error: kcontrol value get for widget: %d\n",
3458 int snd_sof_complete_pipeline(struct device *dev,
3459 struct snd_sof_widget *swidget)
3461 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
3462 struct sof_ipc_pipe_ready ready;
3463 struct sof_ipc_reply reply;
3466 dev_dbg(dev, "tplg: complete pipeline %s id %d\n",
3467 swidget->widget->name, swidget->comp_id);
3469 memset(&ready, 0, sizeof(ready));
3470 ready.hdr.size = sizeof(ready);
3471 ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3472 ready.comp_id = swidget->comp_id;
3474 ret = sof_ipc_tx_message(sdev->ipc,
3475 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3482 /* completion - called at completion of firmware loading */
3483 static void sof_complete(struct snd_soc_component *scomp)
3485 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3486 struct snd_sof_widget *swidget;
3488 /* some widget types require completion notificattion */
3489 list_for_each_entry(swidget, &sdev->widget_list, list) {
3490 if (swidget->complete)
3493 switch (swidget->id) {
3494 case snd_soc_dapm_scheduler:
3496 snd_sof_complete_pipeline(scomp->dev, swidget);
3503 * cache initial values of SOF kcontrols by reading DSP value over
3504 * IPC. It may be overwritten by alsa-mixer after booting up
3506 snd_sof_cache_kcontrol_val(scomp);
3509 /* manifest - optional to inform component of manifest */
3510 static int sof_manifest(struct snd_soc_component *scomp, int index,
3511 struct snd_soc_tplg_manifest *man)
3516 size = le32_to_cpu(man->priv.size);
3518 /* backward compatible with tplg without ABI info */
3520 dev_dbg(scomp->dev, "No topology ABI info\n");
3524 if (size != SOF_TPLG_ABI_SIZE) {
3525 dev_err(scomp->dev, "error: invalid topology ABI size\n");
3529 dev_info(scomp->dev,
3530 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3531 man->priv.data[0], man->priv.data[1],
3532 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3535 abi_version = SOF_ABI_VER(man->priv.data[0],
3539 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3540 dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3544 if (abi_version > SOF_ABI_VERSION) {
3545 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3546 dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3548 dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3556 /* vendor specific kcontrol handlers available for binding */
3557 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3558 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3559 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3560 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3561 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3564 /* vendor specific bytes ext handlers available for binding */
3565 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3566 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3569 static struct snd_soc_tplg_ops sof_tplg_ops = {
3570 /* external kcontrol init - used for any driver specific init */
3571 .control_load = sof_control_load,
3572 .control_unload = sof_control_unload,
3574 /* external kcontrol init - used for any driver specific init */
3575 .dapm_route_load = sof_route_load,
3576 .dapm_route_unload = sof_route_unload,
3578 /* external widget init - used for any driver specific init */
3579 /* .widget_load is not currently used */
3580 .widget_ready = sof_widget_ready,
3581 .widget_unload = sof_widget_unload,
3583 /* FE DAI - used for any driver specific init */
3584 .dai_load = sof_dai_load,
3585 .dai_unload = sof_dai_unload,
3587 /* DAI link - used for any driver specific init */
3588 .link_load = sof_link_load,
3589 .link_unload = sof_link_unload,
3591 /* completion - called at completion of firmware loading */
3592 .complete = sof_complete,
3594 /* manifest - optional to inform component of manifest */
3595 .manifest = sof_manifest,
3597 /* vendor specific kcontrol handlers available for binding */
3598 .io_ops = sof_io_ops,
3599 .io_ops_count = ARRAY_SIZE(sof_io_ops),
3601 /* vendor specific bytes ext handlers available for binding */
3602 .bytes_ext_ops = sof_bytes_ext_ops,
3603 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
3606 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3608 const struct firmware *fw;
3611 dev_dbg(scomp->dev, "loading topology:%s\n", file);
3613 ret = request_firmware(&fw, file, scomp->dev);
3615 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3620 ret = snd_soc_tplg_component_load(scomp,
3622 SND_SOC_TPLG_INDEX_ALL);
3624 dev_err(scomp->dev, "error: tplg component load failed %d\n",
3629 release_firmware(fw);
3632 EXPORT_SYMBOL(snd_sof_load_topology);