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.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
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},
436 static enum sof_ipc_process_type find_process(const char *name)
440 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
441 if (strcmp(name, sof_process[i].name) == 0)
442 return sof_process[i].type;
445 return SOF_PROCESS_NONE;
448 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
452 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
453 if (sof_process[i].type == type)
454 return sof_process[i].comp_type;
457 return SOF_COMP_NONE;
461 * Topology Token Parsing.
462 * New tokens should be added to headers and parsing tables below.
465 struct sof_topology_token {
468 int (*get_token)(void *elem, void *object, u32 offset, u32 size);
473 static int get_token_u32(void *elem, void *object, u32 offset, u32 size)
475 struct snd_soc_tplg_vendor_value_elem *velem = elem;
476 u32 *val = (u32 *)((u8 *)object + offset);
478 *val = le32_to_cpu(velem->value);
482 static int get_token_u16(void *elem, void *object, u32 offset, u32 size)
484 struct snd_soc_tplg_vendor_value_elem *velem = elem;
485 u16 *val = (u16 *)((u8 *)object + offset);
487 *val = (u16)le32_to_cpu(velem->value);
491 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size)
493 struct snd_soc_tplg_vendor_string_elem *velem = elem;
494 u32 *val = (u32 *)((u8 *)object + offset);
496 *val = find_format(velem->string);
500 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size)
502 struct snd_soc_tplg_vendor_string_elem *velem = elem;
503 u32 *val = (u32 *)((u8 *)object + offset);
505 *val = find_dai(velem->string);
509 static int get_token_process_type(void *elem, void *object, u32 offset,
512 struct snd_soc_tplg_vendor_string_elem *velem = elem;
513 u32 *val = (u32 *)((u8 *)object + offset);
515 *val = find_process(velem->string);
520 static const struct sof_topology_token buffer_tokens[] = {
521 {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
522 offsetof(struct sof_ipc_buffer, size), 0},
523 {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
524 offsetof(struct sof_ipc_buffer, caps), 0},
528 static const struct sof_topology_token dai_tokens[] = {
529 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
530 offsetof(struct sof_ipc_comp_dai, type), 0},
531 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
532 offsetof(struct sof_ipc_comp_dai, dai_index), 0},
533 {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
534 offsetof(struct sof_ipc_comp_dai, direction), 0},
538 static const struct sof_topology_token dai_link_tokens[] = {
539 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
540 offsetof(struct sof_ipc_dai_config, type), 0},
541 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
542 offsetof(struct sof_ipc_dai_config, dai_index), 0},
546 static const struct sof_topology_token sched_tokens[] = {
547 {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
548 offsetof(struct sof_ipc_pipe_new, period), 0},
549 {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
550 offsetof(struct sof_ipc_pipe_new, priority), 0},
551 {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
552 offsetof(struct sof_ipc_pipe_new, period_mips), 0},
553 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
554 offsetof(struct sof_ipc_pipe_new, core), 0},
555 {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
556 offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0},
557 {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
558 offsetof(struct sof_ipc_pipe_new, time_domain), 0},
562 static const struct sof_topology_token volume_tokens[] = {
563 {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
564 get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0},
565 {SOF_TKN_VOLUME_RAMP_STEP_MS,
566 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
567 offsetof(struct sof_ipc_comp_volume, initial_ramp), 0},
571 static const struct sof_topology_token src_tokens[] = {
572 {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
573 offsetof(struct sof_ipc_comp_src, source_rate), 0},
574 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
575 offsetof(struct sof_ipc_comp_src, sink_rate), 0},
579 static const struct sof_topology_token asrc_tokens[] = {
580 {SOF_TKN_ASRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
581 offsetof(struct sof_ipc_comp_asrc, source_rate), 0},
582 {SOF_TKN_ASRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
583 offsetof(struct sof_ipc_comp_asrc, sink_rate), 0},
584 {SOF_TKN_ASRC_ASYNCHRONOUS_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
586 offsetof(struct sof_ipc_comp_asrc, asynchronous_mode), 0},
587 {SOF_TKN_ASRC_OPERATION_MODE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
589 offsetof(struct sof_ipc_comp_asrc, operation_mode), 0},
593 static const struct sof_topology_token tone_tokens[] = {
597 static const struct sof_topology_token process_tokens[] = {
598 {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
599 get_token_process_type,
600 offsetof(struct sof_ipc_comp_process, type), 0},
604 static const struct sof_topology_token pcm_tokens[] = {
605 {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
606 offsetof(struct sof_ipc_comp_host, dmac_config), 0},
610 static const struct sof_topology_token stream_tokens[] = {
611 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
612 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
613 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible), 0},
614 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
615 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
616 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible), 0},
619 /* Generic components */
620 static const struct sof_topology_token comp_tokens[] = {
621 {SOF_TKN_COMP_PERIOD_SINK_COUNT,
622 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
623 offsetof(struct sof_ipc_comp_config, periods_sink), 0},
624 {SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
625 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
626 offsetof(struct sof_ipc_comp_config, periods_source), 0},
627 {SOF_TKN_COMP_FORMAT,
628 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
629 offsetof(struct sof_ipc_comp_config, frame_fmt), 0},
633 static const struct sof_topology_token ssp_tokens[] = {
634 {SOF_TKN_INTEL_SSP_CLKS_CONTROL,
635 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
636 offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0},
637 {SOF_TKN_INTEL_SSP_MCLK_ID,
638 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
639 offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0},
640 {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
642 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0},
643 {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
645 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0},
646 {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
648 offsetof(struct sof_ipc_dai_ssp_params, quirks), 0},
649 {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
651 offsetof(struct sof_ipc_dai_ssp_params,
652 tdm_per_slot_padding_flag), 0},
653 {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
655 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0},
660 static const struct sof_topology_token alh_tokens[] = {
661 {SOF_TKN_INTEL_ALH_RATE,
662 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
663 offsetof(struct sof_ipc_dai_alh_params, rate), 0},
664 {SOF_TKN_INTEL_ALH_CH,
665 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
666 offsetof(struct sof_ipc_dai_alh_params, channels), 0},
670 static const struct sof_topology_token dmic_tokens[] = {
671 {SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
672 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
673 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version),
675 {SOF_TKN_INTEL_DMIC_CLK_MIN,
676 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
677 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0},
678 {SOF_TKN_INTEL_DMIC_CLK_MAX,
679 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
680 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0},
681 {SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
682 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
683 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0},
684 {SOF_TKN_INTEL_DMIC_DUTY_MIN,
685 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
686 offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0},
687 {SOF_TKN_INTEL_DMIC_DUTY_MAX,
688 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
689 offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0},
690 {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
691 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
692 offsetof(struct sof_ipc_dai_dmic_params,
694 {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
695 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
696 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0},
697 {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
698 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
699 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0},
704 static const struct sof_topology_token esai_tokens[] = {
705 {SOF_TKN_IMX_ESAI_MCLK_ID,
706 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
707 offsetof(struct sof_ipc_dai_esai_params, mclk_id), 0},
711 static const struct sof_topology_token sai_tokens[] = {
712 {SOF_TKN_IMX_SAI_MCLK_ID,
713 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
714 offsetof(struct sof_ipc_dai_sai_params, mclk_id), 0},
719 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
720 * as it increments the index while parsing the array of pdm tokens
721 * and determines the correct offset
723 static const struct sof_topology_token dmic_pdm_tokens[] = {
724 {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
725 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
726 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),
728 {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
729 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
730 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a),
732 {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
733 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
734 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b),
736 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
737 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
738 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a),
740 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
741 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
742 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b),
744 {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
745 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
746 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge),
748 {SOF_TKN_INTEL_DMIC_PDM_SKEW,
749 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
750 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew),
755 static const struct sof_topology_token hda_tokens[] = {
756 {SOF_TKN_INTEL_HDA_RATE,
757 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
758 offsetof(struct sof_ipc_dai_hda_params, rate), 0},
759 {SOF_TKN_INTEL_HDA_CH,
760 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
761 offsetof(struct sof_ipc_dai_hda_params, channels), 0},
765 static const struct sof_topology_token led_tokens[] = {
766 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
767 offsetof(struct snd_sof_led_control, use_led), 0},
768 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
769 get_token_u32, offsetof(struct snd_sof_led_control, direction), 0},
772 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
774 const struct sof_topology_token *tokens,
776 struct snd_soc_tplg_vendor_array *array,
779 struct snd_soc_tplg_vendor_uuid_elem *elem;
783 /* parse element by element */
784 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
785 elem = &array->uuid[i];
787 /* search for token */
788 for (j = 0; j < count; j++) {
789 /* match token type */
790 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
794 if (tokens[j].token != le32_to_cpu(elem->token))
797 /* matched - now load token */
798 tokens[j].get_token(elem, object,
799 offset + tokens[j].offset,
809 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
811 const struct sof_topology_token *tokens,
813 struct snd_soc_tplg_vendor_array *array,
816 struct snd_soc_tplg_vendor_string_elem *elem;
820 /* parse element by element */
821 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
822 elem = &array->string[i];
824 /* search for token */
825 for (j = 0; j < count; j++) {
826 /* match token type */
827 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
831 if (tokens[j].token != le32_to_cpu(elem->token))
834 /* matched - now load token */
835 tokens[j].get_token(elem, object,
836 offset + tokens[j].offset,
846 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
848 const struct sof_topology_token *tokens,
850 struct snd_soc_tplg_vendor_array *array,
853 struct snd_soc_tplg_vendor_value_elem *elem;
857 /* parse element by element */
858 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
859 elem = &array->value[i];
861 /* search for token */
862 for (j = 0; j < count; j++) {
863 /* match token type */
864 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
865 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
866 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
867 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
871 if (tokens[j].token != le32_to_cpu(elem->token))
875 tokens[j].get_token(elem, object,
876 offset + tokens[j].offset,
887 * sof_parse_token_sets - Parse multiple sets of tokens
888 * @scomp: pointer to soc component
889 * @object: target ipc struct for parsed values
890 * @tokens: token definition array describing what tokens to parse
891 * @count: number of tokens in definition array
892 * @array: source pointer to consecutive vendor arrays to be parsed
893 * @priv_size: total size of the consecutive source arrays
894 * @sets: number of similar token sets to be parsed, 1 set has count elements
895 * @object_size: offset to next target ipc struct with multiple sets
897 * This function parses multiple sets of tokens in vendor arrays into
898 * consecutive ipc structs.
900 static int sof_parse_token_sets(struct snd_soc_component *scomp,
902 const struct sof_topology_token *tokens,
904 struct snd_soc_tplg_vendor_array *array,
905 int priv_size, int sets, size_t object_size)
912 while (priv_size > 0 && total < count * sets) {
913 asize = le32_to_cpu(array->size);
916 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
917 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
922 /* make sure there is enough data before parsing */
925 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
930 /* call correct parser depending on type */
931 switch (le32_to_cpu(array->type)) {
932 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
933 found += sof_parse_uuid_tokens(scomp, object, tokens,
934 count, array, offset);
936 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
937 found += sof_parse_string_tokens(scomp, object, tokens,
938 count, array, offset);
940 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
941 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
942 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
943 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
944 found += sof_parse_word_tokens(scomp, object, tokens,
945 count, array, offset);
948 dev_err(scomp->dev, "error: unknown token type %d\n",
954 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
957 /* move to next target struct */
958 if (found >= count) {
959 offset += object_size;
968 static int sof_parse_tokens(struct snd_soc_component *scomp,
970 const struct sof_topology_token *tokens,
972 struct snd_soc_tplg_vendor_array *array,
976 * sof_parse_tokens is used when topology contains only a single set of
977 * identical tuples arrays. So additional parameters to
978 * sof_parse_token_sets are sets = 1 (only 1 set) and
979 * object_size = 0 (irrelevant).
981 return sof_parse_token_sets(scomp, object, tokens, count, array,
985 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
986 struct sof_ipc_comp_config *config)
988 dev_dbg(scomp->dev, " config: periods snk %d src %d fmt %d\n",
989 config->periods_sink, config->periods_source,
994 * Standard Kcontrols.
997 static int sof_control_load_volume(struct snd_soc_component *scomp,
998 struct snd_sof_control *scontrol,
999 struct snd_kcontrol_new *kc,
1000 struct snd_soc_tplg_ctl_hdr *hdr)
1002 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1003 struct snd_soc_tplg_mixer_control *mc =
1004 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
1005 struct sof_ipc_ctrl_data *cdata;
1010 /* validate topology data */
1011 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) {
1016 /* init the volume get/put data */
1017 scontrol->size = struct_size(scontrol->control_data, chanv,
1018 le32_to_cpu(mc->num_channels));
1019 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1020 if (!scontrol->control_data) {
1025 scontrol->comp_id = sdev->next_comp_id;
1026 scontrol->min_volume_step = le32_to_cpu(mc->min);
1027 scontrol->max_volume_step = le32_to_cpu(mc->max);
1028 scontrol->num_channels = le32_to_cpu(mc->num_channels);
1030 /* set cmd for mixer control */
1031 if (le32_to_cpu(mc->max) == 1) {
1032 scontrol->cmd = SOF_CTRL_CMD_SWITCH;
1036 scontrol->cmd = SOF_CTRL_CMD_VOLUME;
1038 /* extract tlv data */
1039 if (get_tlv_data(kc->tlv.p, tlv) < 0) {
1040 dev_err(scomp->dev, "error: invalid TLV data\n");
1045 /* set up volume table */
1046 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
1048 dev_err(scomp->dev, "error: setting up volume table\n");
1052 /* set default volume values to 0dB in control */
1053 cdata = scontrol->control_data;
1054 for (i = 0; i < scontrol->num_channels; i++) {
1055 cdata->chanv[i].channel = i;
1056 cdata->chanv[i].value = VOL_ZERO_DB;
1060 /* set up possible led control from mixer private data */
1061 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
1062 ARRAY_SIZE(led_tokens), mc->priv.array,
1063 le32_to_cpu(mc->priv.size));
1065 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
1066 le32_to_cpu(mc->priv.size));
1067 goto out_free_table;
1070 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1071 scontrol->comp_id, scontrol->num_channels);
1076 if (le32_to_cpu(mc->max) > 1)
1077 kfree(scontrol->volume_table);
1079 kfree(scontrol->control_data);
1084 static int sof_control_load_enum(struct snd_soc_component *scomp,
1085 struct snd_sof_control *scontrol,
1086 struct snd_kcontrol_new *kc,
1087 struct snd_soc_tplg_ctl_hdr *hdr)
1089 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1090 struct snd_soc_tplg_enum_control *ec =
1091 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1093 /* validate topology data */
1094 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1097 /* init the enum get/put data */
1098 scontrol->size = struct_size(scontrol->control_data, chanv,
1099 le32_to_cpu(ec->num_channels));
1100 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1101 if (!scontrol->control_data)
1104 scontrol->comp_id = sdev->next_comp_id;
1105 scontrol->num_channels = le32_to_cpu(ec->num_channels);
1107 scontrol->cmd = SOF_CTRL_CMD_ENUM;
1109 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1110 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1115 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1116 struct snd_sof_control *scontrol,
1117 struct snd_kcontrol_new *kc,
1118 struct snd_soc_tplg_ctl_hdr *hdr)
1120 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1121 struct sof_ipc_ctrl_data *cdata;
1122 struct snd_soc_tplg_bytes_control *control =
1123 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1124 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1125 int max_size = sbe->max;
1128 /* init the get/put bytes data */
1129 scontrol->size = sizeof(struct sof_ipc_ctrl_data) +
1130 le32_to_cpu(control->priv.size);
1132 if (scontrol->size > max_size) {
1133 dev_err(scomp->dev, "err: bytes data size %d exceeds max %d.\n",
1134 scontrol->size, max_size);
1139 scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1140 cdata = scontrol->control_data;
1141 if (!scontrol->control_data) {
1146 scontrol->comp_id = sdev->next_comp_id;
1147 scontrol->cmd = SOF_CTRL_CMD_BINARY;
1149 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
1150 scontrol->comp_id, scontrol->num_channels);
1152 if (le32_to_cpu(control->priv.size) > 0) {
1153 memcpy(cdata->data, control->priv.data,
1154 le32_to_cpu(control->priv.size));
1156 if (cdata->data->magic != SOF_ABI_MAGIC) {
1157 dev_err(scomp->dev, "error: Wrong ABI magic 0x%08x.\n",
1158 cdata->data->magic);
1162 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1163 cdata->data->abi)) {
1165 "error: Incompatible ABI version 0x%08x.\n",
1170 if (cdata->data->size + sizeof(const struct sof_abi_hdr) !=
1171 le32_to_cpu(control->priv.size)) {
1173 "error: Conflict in bytes vs. priv size.\n");
1182 kfree(scontrol->control_data);
1187 /* external kcontrol init - used for any driver specific init */
1188 static int sof_control_load(struct snd_soc_component *scomp, int index,
1189 struct snd_kcontrol_new *kc,
1190 struct snd_soc_tplg_ctl_hdr *hdr)
1192 struct soc_mixer_control *sm;
1193 struct soc_bytes_ext *sbe;
1194 struct soc_enum *se;
1195 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1196 struct snd_soc_dobj *dobj;
1197 struct snd_sof_control *scontrol;
1200 dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
1201 hdr->type, hdr->name);
1203 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1207 scontrol->scomp = scomp;
1209 switch (le32_to_cpu(hdr->ops.info)) {
1210 case SND_SOC_TPLG_CTL_VOLSW:
1211 case SND_SOC_TPLG_CTL_VOLSW_SX:
1212 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1213 sm = (struct soc_mixer_control *)kc->private_value;
1215 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1217 case SND_SOC_TPLG_CTL_BYTES:
1218 sbe = (struct soc_bytes_ext *)kc->private_value;
1220 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1222 case SND_SOC_TPLG_CTL_ENUM:
1223 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1224 se = (struct soc_enum *)kc->private_value;
1226 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1228 case SND_SOC_TPLG_CTL_RANGE:
1229 case SND_SOC_TPLG_CTL_STROBE:
1230 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1231 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1232 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1233 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1234 case SND_SOC_TPLG_DAPM_CTL_PIN:
1236 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1237 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1247 dobj->private = scontrol;
1248 list_add(&scontrol->list, &sdev->kcontrol_list);
1252 static int sof_control_unload(struct snd_soc_component *scomp,
1253 struct snd_soc_dobj *dobj)
1255 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1256 struct sof_ipc_free fcomp;
1257 struct snd_sof_control *scontrol = dobj->private;
1259 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scomp->name);
1261 fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1262 fcomp.hdr.size = sizeof(fcomp);
1263 fcomp.id = scontrol->comp_id;
1265 kfree(scontrol->control_data);
1266 list_del(&scontrol->list);
1268 /* send IPC to the DSP */
1269 return sof_ipc_tx_message(sdev->ipc,
1270 fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1278 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1279 struct snd_soc_dapm_widget *w,
1280 struct snd_soc_tplg_dapm_widget *tw,
1281 struct snd_sof_dai *dai)
1283 struct snd_soc_card *card = scomp->card;
1284 struct snd_soc_pcm_runtime *rtd;
1285 struct snd_soc_dai *cpu_dai;
1288 list_for_each_entry(rtd, &card->rtd_list, list) {
1289 dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1290 w->name, w->sname, rtd->dai_link->stream_name);
1292 if (!w->sname || !rtd->dai_link->stream_name)
1295 /* does stream match DAI link ? */
1296 if (strcmp(w->sname, rtd->dai_link->stream_name))
1300 case snd_soc_dapm_dai_out:
1301 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1303 * Please create DAI widget in the right order
1304 * to ensure BE will connect to the right DAI
1307 if (!cpu_dai->capture_widget) {
1308 cpu_dai->capture_widget = w;
1312 if (i == rtd->num_cpus) {
1313 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1318 dai->name = rtd->dai_link->name;
1319 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1320 w->name, rtd->dai_link->name);
1322 case snd_soc_dapm_dai_in:
1323 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1325 * Please create DAI widget in the right order
1326 * to ensure BE will connect to the right DAI
1329 if (!cpu_dai->playback_widget) {
1330 cpu_dai->playback_widget = w;
1334 if (i == rtd->num_cpus) {
1335 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1340 dai->name = rtd->dai_link->name;
1341 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1342 w->name, rtd->dai_link->name);
1349 /* check we have a connection */
1351 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1359 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1360 struct snd_sof_widget *swidget,
1361 struct snd_soc_tplg_dapm_widget *tw,
1362 struct sof_ipc_comp_reply *r,
1363 struct snd_sof_dai *dai)
1365 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1366 struct snd_soc_tplg_private *private = &tw->priv;
1367 struct sof_ipc_comp_dai comp_dai;
1370 /* configure dai IPC message */
1371 memset(&comp_dai, 0, sizeof(comp_dai));
1372 comp_dai.comp.hdr.size = sizeof(comp_dai);
1373 comp_dai.comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1374 comp_dai.comp.id = swidget->comp_id;
1375 comp_dai.comp.type = SOF_COMP_DAI;
1376 comp_dai.comp.pipeline_id = index;
1377 comp_dai.config.hdr.size = sizeof(comp_dai.config);
1379 ret = sof_parse_tokens(scomp, &comp_dai, dai_tokens,
1380 ARRAY_SIZE(dai_tokens), private->array,
1381 le32_to_cpu(private->size));
1383 dev_err(scomp->dev, "error: parse dai tokens failed %d\n",
1384 le32_to_cpu(private->size));
1388 ret = sof_parse_tokens(scomp, &comp_dai.config, comp_tokens,
1389 ARRAY_SIZE(comp_tokens), private->array,
1390 le32_to_cpu(private->size));
1392 dev_err(scomp->dev, "error: parse dai.cfg tokens failed %d\n",
1397 dev_dbg(scomp->dev, "dai %s: type %d index %d\n",
1398 swidget->widget->name, comp_dai.type, comp_dai.dai_index);
1399 sof_dbg_comp_config(scomp, &comp_dai.config);
1401 ret = sof_ipc_tx_message(sdev->ipc, comp_dai.comp.hdr.cmd,
1402 &comp_dai, sizeof(comp_dai), r, sizeof(*r));
1404 if (ret == 0 && dai) {
1406 memcpy(&dai->comp_dai, &comp_dai, sizeof(comp_dai));
1416 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index,
1417 struct snd_sof_widget *swidget,
1418 struct snd_soc_tplg_dapm_widget *tw,
1419 struct sof_ipc_comp_reply *r)
1421 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1422 struct snd_soc_tplg_private *private = &tw->priv;
1423 struct sof_ipc_buffer *buffer;
1426 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1430 /* configure dai IPC message */
1431 buffer->comp.hdr.size = sizeof(*buffer);
1432 buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
1433 buffer->comp.id = swidget->comp_id;
1434 buffer->comp.type = SOF_COMP_BUFFER;
1435 buffer->comp.pipeline_id = index;
1437 ret = sof_parse_tokens(scomp, buffer, buffer_tokens,
1438 ARRAY_SIZE(buffer_tokens), private->array,
1439 le32_to_cpu(private->size));
1441 dev_err(scomp->dev, "error: parse buffer tokens failed %d\n",
1447 dev_dbg(scomp->dev, "buffer %s: size %d caps 0x%x\n",
1448 swidget->widget->name, buffer->size, buffer->caps);
1450 swidget->private = buffer;
1452 ret = sof_ipc_tx_message(sdev->ipc, buffer->comp.hdr.cmd, buffer,
1453 sizeof(*buffer), r, sizeof(*r));
1455 dev_err(scomp->dev, "error: buffer %s load failed\n",
1456 swidget->widget->name);
1463 /* bind PCM ID to host component ID */
1464 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1467 struct snd_sof_widget *host_widget;
1469 host_widget = snd_sof_find_swidget_sname(scomp,
1470 spcm->pcm.caps[dir].name,
1473 dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1477 spcm->stream[dir].comp_id = host_widget->comp_id;
1486 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index,
1487 struct snd_sof_widget *swidget,
1488 enum sof_ipc_stream_direction dir,
1489 struct snd_soc_tplg_dapm_widget *tw,
1490 struct sof_ipc_comp_reply *r)
1492 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1493 struct snd_soc_tplg_private *private = &tw->priv;
1494 struct sof_ipc_comp_host *host;
1497 host = kzalloc(sizeof(*host), GFP_KERNEL);
1501 /* configure host comp IPC message */
1502 host->comp.hdr.size = sizeof(*host);
1503 host->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1504 host->comp.id = swidget->comp_id;
1505 host->comp.type = SOF_COMP_HOST;
1506 host->comp.pipeline_id = index;
1507 host->direction = dir;
1508 host->config.hdr.size = sizeof(host->config);
1510 ret = sof_parse_tokens(scomp, host, pcm_tokens,
1511 ARRAY_SIZE(pcm_tokens), private->array,
1512 le32_to_cpu(private->size));
1514 dev_err(scomp->dev, "error: parse host tokens failed %d\n",
1519 ret = sof_parse_tokens(scomp, &host->config, comp_tokens,
1520 ARRAY_SIZE(comp_tokens), private->array,
1521 le32_to_cpu(private->size));
1523 dev_err(scomp->dev, "error: parse host.cfg tokens failed %d\n",
1524 le32_to_cpu(private->size));
1528 dev_dbg(scomp->dev, "loaded host %s\n", swidget->widget->name);
1529 sof_dbg_comp_config(scomp, &host->config);
1531 swidget->private = host;
1533 ret = sof_ipc_tx_message(sdev->ipc, host->comp.hdr.cmd, host,
1534 sizeof(*host), r, sizeof(*r));
1545 int sof_load_pipeline_ipc(struct device *dev,
1546 struct sof_ipc_pipe_new *pipeline,
1547 struct sof_ipc_comp_reply *r)
1549 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
1550 struct sof_ipc_pm_core_config pm_core_config;
1553 ret = sof_ipc_tx_message(sdev->ipc, pipeline->hdr.cmd, pipeline,
1554 sizeof(*pipeline), r, sizeof(*r));
1556 dev_err(dev, "error: load pipeline ipc failure\n");
1560 /* power up the core that this pipeline is scheduled on */
1561 ret = snd_sof_dsp_core_power_up(sdev, 1 << pipeline->core);
1563 dev_err(dev, "error: powering up pipeline schedule core %d\n",
1568 /* update enabled cores mask */
1569 sdev->enabled_cores_mask |= 1 << pipeline->core;
1572 * Now notify DSP that the core that this pipeline is scheduled on
1573 * has been powered up
1575 memset(&pm_core_config, 0, sizeof(pm_core_config));
1576 pm_core_config.enable_mask = sdev->enabled_cores_mask;
1578 /* configure CORE_ENABLE ipc message */
1579 pm_core_config.hdr.size = sizeof(pm_core_config);
1580 pm_core_config.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE;
1583 ret = sof_ipc_tx_message(sdev->ipc, pm_core_config.hdr.cmd,
1584 &pm_core_config, sizeof(pm_core_config),
1585 &pm_core_config, sizeof(pm_core_config));
1587 dev_err(dev, "error: core enable ipc failure\n");
1592 static int sof_widget_load_pipeline(struct snd_soc_component *scomp,
1593 int index, struct snd_sof_widget *swidget,
1594 struct snd_soc_tplg_dapm_widget *tw,
1595 struct sof_ipc_comp_reply *r)
1597 struct snd_soc_tplg_private *private = &tw->priv;
1598 struct sof_ipc_pipe_new *pipeline;
1599 struct snd_sof_widget *comp_swidget;
1602 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
1606 /* configure dai IPC message */
1607 pipeline->hdr.size = sizeof(*pipeline);
1608 pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
1609 pipeline->pipeline_id = index;
1610 pipeline->comp_id = swidget->comp_id;
1612 /* component at start of pipeline is our stream id */
1613 comp_swidget = snd_sof_find_swidget(scomp, tw->sname);
1614 if (!comp_swidget) {
1615 dev_err(scomp->dev, "error: widget %s refers to non existent widget %s\n",
1616 tw->name, tw->sname);
1621 pipeline->sched_id = comp_swidget->comp_id;
1623 dev_dbg(scomp->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n",
1624 pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id);
1626 ret = sof_parse_tokens(scomp, pipeline, sched_tokens,
1627 ARRAY_SIZE(sched_tokens), private->array,
1628 le32_to_cpu(private->size));
1630 dev_err(scomp->dev, "error: parse pipeline tokens failed %d\n",
1635 dev_dbg(scomp->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d\n",
1636 swidget->widget->name, pipeline->period, pipeline->priority,
1637 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched);
1639 swidget->private = pipeline;
1641 /* send ipc's to create pipeline comp and power up schedule core */
1642 ret = sof_load_pipeline_ipc(scomp->dev, pipeline, r);
1654 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index,
1655 struct snd_sof_widget *swidget,
1656 struct snd_soc_tplg_dapm_widget *tw,
1657 struct sof_ipc_comp_reply *r)
1659 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1660 struct snd_soc_tplg_private *private = &tw->priv;
1661 struct sof_ipc_comp_mixer *mixer;
1664 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
1668 /* configure mixer IPC message */
1669 mixer->comp.hdr.size = sizeof(*mixer);
1670 mixer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1671 mixer->comp.id = swidget->comp_id;
1672 mixer->comp.type = SOF_COMP_MIXER;
1673 mixer->comp.pipeline_id = index;
1674 mixer->config.hdr.size = sizeof(mixer->config);
1676 ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens,
1677 ARRAY_SIZE(comp_tokens), private->array,
1678 le32_to_cpu(private->size));
1680 dev_err(scomp->dev, "error: parse mixer.cfg tokens failed %d\n",
1686 sof_dbg_comp_config(scomp, &mixer->config);
1688 swidget->private = mixer;
1690 ret = sof_ipc_tx_message(sdev->ipc, mixer->comp.hdr.cmd, mixer,
1691 sizeof(*mixer), r, sizeof(*r));
1701 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1702 struct snd_sof_widget *swidget,
1703 struct snd_soc_tplg_dapm_widget *tw,
1704 struct sof_ipc_comp_reply *r)
1706 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1707 struct snd_soc_tplg_private *private = &tw->priv;
1708 struct sof_ipc_comp_mux *mux;
1711 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
1715 /* configure mux IPC message */
1716 mux->comp.hdr.size = sizeof(*mux);
1717 mux->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1718 mux->comp.id = swidget->comp_id;
1719 mux->comp.type = SOF_COMP_MUX;
1720 mux->comp.pipeline_id = index;
1721 mux->config.hdr.size = sizeof(mux->config);
1723 ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1724 ARRAY_SIZE(comp_tokens), private->array,
1725 le32_to_cpu(private->size));
1727 dev_err(scomp->dev, "error: parse mux.cfg tokens failed %d\n",
1733 sof_dbg_comp_config(scomp, &mux->config);
1735 swidget->private = mux;
1737 ret = sof_ipc_tx_message(sdev->ipc, mux->comp.hdr.cmd, mux,
1738 sizeof(*mux), r, sizeof(*r));
1749 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index,
1750 struct snd_sof_widget *swidget,
1751 struct snd_soc_tplg_dapm_widget *tw,
1752 struct sof_ipc_comp_reply *r)
1754 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1755 struct snd_soc_tplg_private *private = &tw->priv;
1756 struct sof_ipc_comp_volume *volume;
1757 struct snd_sof_control *scontrol;
1762 volume = kzalloc(sizeof(*volume), GFP_KERNEL);
1766 if (!le32_to_cpu(tw->num_kcontrols)) {
1767 dev_err(scomp->dev, "error: invalid kcontrol count %d for volume\n",
1773 /* configure volume IPC message */
1774 volume->comp.hdr.size = sizeof(*volume);
1775 volume->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1776 volume->comp.id = swidget->comp_id;
1777 volume->comp.type = SOF_COMP_VOLUME;
1778 volume->comp.pipeline_id = index;
1779 volume->config.hdr.size = sizeof(volume->config);
1781 ret = sof_parse_tokens(scomp, volume, volume_tokens,
1782 ARRAY_SIZE(volume_tokens), private->array,
1783 le32_to_cpu(private->size));
1785 dev_err(scomp->dev, "error: parse volume tokens failed %d\n",
1789 ret = sof_parse_tokens(scomp, &volume->config, comp_tokens,
1790 ARRAY_SIZE(comp_tokens), private->array,
1791 le32_to_cpu(private->size));
1793 dev_err(scomp->dev, "error: parse volume.cfg tokens failed %d\n",
1794 le32_to_cpu(private->size));
1798 sof_dbg_comp_config(scomp, &volume->config);
1800 swidget->private = volume;
1802 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1803 if (scontrol->comp_id == swidget->comp_id &&
1804 scontrol->volume_table) {
1805 min_step = scontrol->min_volume_step;
1806 max_step = scontrol->max_volume_step;
1807 volume->min_value = scontrol->volume_table[min_step];
1808 volume->max_value = scontrol->volume_table[max_step];
1809 volume->channels = scontrol->num_channels;
1814 ret = sof_ipc_tx_message(sdev->ipc, volume->comp.hdr.cmd, volume,
1815 sizeof(*volume), r, sizeof(*r));
1827 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1828 struct snd_sof_widget *swidget,
1829 struct snd_soc_tplg_dapm_widget *tw,
1830 struct sof_ipc_comp_reply *r)
1832 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1833 struct snd_soc_tplg_private *private = &tw->priv;
1834 struct sof_ipc_comp_src *src;
1837 src = kzalloc(sizeof(*src), GFP_KERNEL);
1841 /* configure src IPC message */
1842 src->comp.hdr.size = sizeof(*src);
1843 src->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1844 src->comp.id = swidget->comp_id;
1845 src->comp.type = SOF_COMP_SRC;
1846 src->comp.pipeline_id = index;
1847 src->config.hdr.size = sizeof(src->config);
1849 ret = sof_parse_tokens(scomp, src, src_tokens,
1850 ARRAY_SIZE(src_tokens), private->array,
1851 le32_to_cpu(private->size));
1853 dev_err(scomp->dev, "error: parse src tokens failed %d\n",
1858 ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1859 ARRAY_SIZE(comp_tokens), private->array,
1860 le32_to_cpu(private->size));
1862 dev_err(scomp->dev, "error: parse src.cfg tokens failed %d\n",
1863 le32_to_cpu(private->size));
1867 dev_dbg(scomp->dev, "src %s: source rate %d sink rate %d\n",
1868 swidget->widget->name, src->source_rate, src->sink_rate);
1869 sof_dbg_comp_config(scomp, &src->config);
1871 swidget->private = src;
1873 ret = sof_ipc_tx_message(sdev->ipc, src->comp.hdr.cmd, src,
1874 sizeof(*src), r, sizeof(*r));
1886 static int sof_widget_load_asrc(struct snd_soc_component *scomp, int index,
1887 struct snd_sof_widget *swidget,
1888 struct snd_soc_tplg_dapm_widget *tw,
1889 struct sof_ipc_comp_reply *r)
1891 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1892 struct snd_soc_tplg_private *private = &tw->priv;
1893 struct sof_ipc_comp_asrc *asrc;
1896 asrc = kzalloc(sizeof(*asrc), GFP_KERNEL);
1900 /* configure ASRC IPC message */
1901 asrc->comp.hdr.size = sizeof(*asrc);
1902 asrc->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1903 asrc->comp.id = swidget->comp_id;
1904 asrc->comp.type = SOF_COMP_ASRC;
1905 asrc->comp.pipeline_id = index;
1906 asrc->config.hdr.size = sizeof(asrc->config);
1908 ret = sof_parse_tokens(scomp, asrc, asrc_tokens,
1909 ARRAY_SIZE(asrc_tokens), private->array,
1910 le32_to_cpu(private->size));
1912 dev_err(scomp->dev, "error: parse asrc tokens failed %d\n",
1917 ret = sof_parse_tokens(scomp, &asrc->config, comp_tokens,
1918 ARRAY_SIZE(comp_tokens), private->array,
1919 le32_to_cpu(private->size));
1921 dev_err(scomp->dev, "error: parse asrc.cfg tokens failed %d\n",
1922 le32_to_cpu(private->size));
1926 dev_dbg(scomp->dev, "asrc %s: source rate %d sink rate %d "
1927 "asynch %d operation %d\n",
1928 swidget->widget->name, asrc->source_rate, asrc->sink_rate,
1929 asrc->asynchronous_mode, asrc->operation_mode);
1930 sof_dbg_comp_config(scomp, &asrc->config);
1932 swidget->private = asrc;
1934 ret = sof_ipc_tx_message(sdev->ipc, asrc->comp.hdr.cmd, asrc,
1935 sizeof(*asrc), r, sizeof(*r));
1944 * Signal Generator Topology
1947 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1948 struct snd_sof_widget *swidget,
1949 struct snd_soc_tplg_dapm_widget *tw,
1950 struct sof_ipc_comp_reply *r)
1952 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1953 struct snd_soc_tplg_private *private = &tw->priv;
1954 struct sof_ipc_comp_tone *tone;
1957 tone = kzalloc(sizeof(*tone), GFP_KERNEL);
1961 /* configure siggen IPC message */
1962 tone->comp.hdr.size = sizeof(*tone);
1963 tone->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1964 tone->comp.id = swidget->comp_id;
1965 tone->comp.type = SOF_COMP_TONE;
1966 tone->comp.pipeline_id = index;
1967 tone->config.hdr.size = sizeof(tone->config);
1969 ret = sof_parse_tokens(scomp, tone, tone_tokens,
1970 ARRAY_SIZE(tone_tokens), private->array,
1971 le32_to_cpu(private->size));
1973 dev_err(scomp->dev, "error: parse tone tokens failed %d\n",
1974 le32_to_cpu(private->size));
1978 ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1979 ARRAY_SIZE(comp_tokens), private->array,
1980 le32_to_cpu(private->size));
1982 dev_err(scomp->dev, "error: parse tone.cfg tokens failed %d\n",
1983 le32_to_cpu(private->size));
1987 dev_dbg(scomp->dev, "tone %s: frequency %d amplitude %d\n",
1988 swidget->widget->name, tone->frequency, tone->amplitude);
1989 sof_dbg_comp_config(scomp, &tone->config);
1991 swidget->private = tone;
1993 ret = sof_ipc_tx_message(sdev->ipc, tone->comp.hdr.cmd, tone,
1994 sizeof(*tone), r, sizeof(*r));
2002 static int sof_get_control_data(struct snd_soc_component *scomp,
2003 struct snd_soc_dapm_widget *widget,
2004 struct sof_widget_data *wdata,
2007 const struct snd_kcontrol_new *kc;
2008 struct soc_mixer_control *sm;
2009 struct soc_bytes_ext *sbe;
2010 struct soc_enum *se;
2015 for (i = 0; i < widget->num_kcontrols; i++) {
2016 kc = &widget->kcontrol_news[i];
2018 switch (widget->dobj.widget.kcontrol_type) {
2019 case SND_SOC_TPLG_TYPE_MIXER:
2020 sm = (struct soc_mixer_control *)kc->private_value;
2021 wdata[i].control = sm->dobj.private;
2023 case SND_SOC_TPLG_TYPE_BYTES:
2024 sbe = (struct soc_bytes_ext *)kc->private_value;
2025 wdata[i].control = sbe->dobj.private;
2027 case SND_SOC_TPLG_TYPE_ENUM:
2028 se = (struct soc_enum *)kc->private_value;
2029 wdata[i].control = se->dobj.private;
2032 dev_err(scomp->dev, "error: unknown kcontrol type %d in widget %s\n",
2033 widget->dobj.widget.kcontrol_type,
2038 if (!wdata[i].control) {
2039 dev_err(scomp->dev, "error: no scontrol for widget %s\n",
2044 wdata[i].pdata = wdata[i].control->control_data->data;
2045 if (!wdata[i].pdata)
2048 /* make sure data is valid - data can be updated at runtime */
2049 if (wdata[i].pdata->magic != SOF_ABI_MAGIC)
2052 *size += wdata[i].pdata->size;
2055 switch (wdata[i].control->cmd) {
2056 case SOF_CTRL_CMD_VOLUME:
2057 case SOF_CTRL_CMD_ENUM:
2058 case SOF_CTRL_CMD_SWITCH:
2059 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
2060 wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
2062 case SOF_CTRL_CMD_BINARY:
2063 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
2064 wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
2074 static int sof_process_load(struct snd_soc_component *scomp, int index,
2075 struct snd_sof_widget *swidget,
2076 struct snd_soc_tplg_dapm_widget *tw,
2077 struct sof_ipc_comp_reply *r,
2080 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2081 struct snd_soc_dapm_widget *widget = swidget->widget;
2082 struct snd_soc_tplg_private *private = &tw->priv;
2083 struct sof_ipc_comp_process *process = NULL;
2084 struct sof_widget_data *wdata = NULL;
2085 size_t ipc_data_size = 0;
2091 if (type == SOF_COMP_NONE) {
2092 dev_err(scomp->dev, "error: invalid process comp type %d\n",
2097 /* allocate struct for widget control data sizes and types */
2098 if (widget->num_kcontrols) {
2099 wdata = kcalloc(widget->num_kcontrols,
2106 /* get possible component controls and get size of all pdata */
2107 ret = sof_get_control_data(scomp, widget, wdata,
2114 ipc_size = sizeof(struct sof_ipc_comp_process) +
2115 le32_to_cpu(private->size) +
2118 /* we are exceeding max ipc size, config needs to be sent separately */
2119 if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
2120 ipc_size -= ipc_data_size;
2124 process = kzalloc(ipc_size, GFP_KERNEL);
2130 /* configure iir IPC message */
2131 process->comp.hdr.size = ipc_size;
2132 process->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
2133 process->comp.id = swidget->comp_id;
2134 process->comp.type = type;
2135 process->comp.pipeline_id = index;
2136 process->config.hdr.size = sizeof(process->config);
2138 ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
2139 ARRAY_SIZE(comp_tokens), private->array,
2140 le32_to_cpu(private->size));
2142 dev_err(scomp->dev, "error: parse process.cfg tokens failed %d\n",
2143 le32_to_cpu(private->size));
2147 sof_dbg_comp_config(scomp, &process->config);
2150 * found private data in control, so copy it.
2151 * get possible component controls - get size of all pdata,
2152 * then memcpy with headers
2154 if (ipc_data_size) {
2155 for (i = 0; i < widget->num_kcontrols; i++) {
2156 memcpy(&process->data + offset,
2157 wdata[i].pdata->data,
2158 wdata[i].pdata->size);
2159 offset += wdata[i].pdata->size;
2163 process->size = ipc_data_size;
2164 swidget->private = process;
2166 ret = sof_ipc_tx_message(sdev->ipc, process->comp.hdr.cmd, process,
2167 ipc_size, r, sizeof(*r));
2170 dev_err(scomp->dev, "error: create process failed\n");
2174 /* we sent the data in single message so return */
2178 /* send control data with large message supported method */
2179 for (i = 0; i < widget->num_kcontrols; i++) {
2180 wdata[i].control->readback_offset = 0;
2181 ret = snd_sof_ipc_set_get_comp_data(wdata[i].control,
2184 wdata[i].control->cmd,
2187 dev_err(scomp->dev, "error: send control failed\n");
2201 * Processing Component Topology - can be "effect", "codec", or general
2205 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2206 struct snd_sof_widget *swidget,
2207 struct snd_soc_tplg_dapm_widget *tw,
2208 struct sof_ipc_comp_reply *r)
2210 struct snd_soc_tplg_private *private = &tw->priv;
2211 struct sof_ipc_comp_process config;
2214 /* check we have some tokens - we need at least process type */
2215 if (le32_to_cpu(private->size) == 0) {
2216 dev_err(scomp->dev, "error: process tokens not found\n");
2220 memset(&config, 0, sizeof(config));
2222 /* get the process token */
2223 ret = sof_parse_tokens(scomp, &config, process_tokens,
2224 ARRAY_SIZE(process_tokens), private->array,
2225 le32_to_cpu(private->size));
2227 dev_err(scomp->dev, "error: parse process tokens failed %d\n",
2228 le32_to_cpu(private->size));
2232 /* now load process specific data and send IPC */
2233 ret = sof_process_load(scomp, index, swidget, tw, r,
2234 find_process_comp_type(config.type));
2236 dev_err(scomp->dev, "error: process loading failed\n");
2243 static int sof_widget_bind_event(struct snd_soc_component *scomp,
2244 struct snd_sof_widget *swidget,
2247 struct sof_ipc_comp *ipc_comp;
2249 /* validate widget event type */
2250 switch (event_type) {
2251 case SOF_KEYWORD_DETECT_DAPM_EVENT:
2252 /* only KEYWORD_DETECT comps should handle this */
2253 if (swidget->id != snd_soc_dapm_effect)
2256 ipc_comp = swidget->private;
2257 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2260 /* bind event to keyword detect comp */
2261 return snd_soc_tplg_widget_bind_event(swidget->widget,
2263 ARRAY_SIZE(sof_kwd_events),
2270 "error: invalid event type %d for widget %s\n",
2271 event_type, swidget->widget->name);
2275 /* external widget init - used for any driver specific init */
2276 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2277 struct snd_soc_dapm_widget *w,
2278 struct snd_soc_tplg_dapm_widget *tw)
2280 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2281 struct snd_sof_widget *swidget;
2282 struct snd_sof_dai *dai;
2283 struct sof_ipc_comp_reply reply;
2284 struct snd_sof_control *scontrol;
2287 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2291 swidget->scomp = scomp;
2292 swidget->widget = w;
2293 swidget->comp_id = sdev->next_comp_id++;
2294 swidget->complete = 0;
2295 swidget->id = w->id;
2296 swidget->pipeline_id = index;
2297 swidget->private = NULL;
2298 memset(&reply, 0, sizeof(reply));
2300 dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2301 swidget->comp_id, index, swidget->id, tw->name,
2302 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2303 ? tw->sname : "none");
2305 /* handle any special case widgets */
2307 case snd_soc_dapm_dai_in:
2308 case snd_soc_dapm_dai_out:
2309 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2315 ret = sof_widget_load_dai(scomp, index, swidget, tw, &reply,
2318 sof_connect_dai_widget(scomp, w, tw, dai);
2319 list_add(&dai->list, &sdev->dai_list);
2320 swidget->private = dai;
2325 case snd_soc_dapm_mixer:
2326 ret = sof_widget_load_mixer(scomp, index, swidget, tw, &reply);
2328 case snd_soc_dapm_pga:
2329 ret = sof_widget_load_pga(scomp, index, swidget, tw, &reply);
2330 /* Find scontrol for this pga and set readback offset*/
2331 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2332 if (scontrol->comp_id == swidget->comp_id) {
2333 scontrol->readback_offset = reply.offset;
2338 case snd_soc_dapm_buffer:
2339 ret = sof_widget_load_buffer(scomp, index, swidget, tw, &reply);
2341 case snd_soc_dapm_scheduler:
2342 ret = sof_widget_load_pipeline(scomp, index, swidget, tw,
2345 case snd_soc_dapm_aif_out:
2346 ret = sof_widget_load_pcm(scomp, index, swidget,
2347 SOF_IPC_STREAM_CAPTURE, tw, &reply);
2349 case snd_soc_dapm_aif_in:
2350 ret = sof_widget_load_pcm(scomp, index, swidget,
2351 SOF_IPC_STREAM_PLAYBACK, tw, &reply);
2353 case snd_soc_dapm_src:
2354 ret = sof_widget_load_src(scomp, index, swidget, tw, &reply);
2356 case snd_soc_dapm_asrc:
2357 ret = sof_widget_load_asrc(scomp, index, swidget, tw, &reply);
2359 case snd_soc_dapm_siggen:
2360 ret = sof_widget_load_siggen(scomp, index, swidget, tw, &reply);
2362 case snd_soc_dapm_effect:
2363 ret = sof_widget_load_process(scomp, index, swidget, tw,
2366 case snd_soc_dapm_mux:
2367 case snd_soc_dapm_demux:
2368 ret = sof_widget_load_mux(scomp, index, swidget, tw, &reply);
2370 case snd_soc_dapm_switch:
2371 case snd_soc_dapm_dai_link:
2372 case snd_soc_dapm_kcontrol:
2374 dev_warn(scomp->dev, "warning: widget type %d name %s not handled\n",
2375 swidget->id, tw->name);
2379 /* check IPC reply */
2380 if (ret < 0 || reply.rhdr.error < 0) {
2382 "error: DSP failed to add widget id %d type %d name : %s stream %s reply %d\n",
2383 tw->shift, swidget->id, tw->name,
2384 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2385 ? tw->sname : "none", reply.rhdr.error);
2390 /* bind widget to external event */
2391 if (tw->event_type) {
2392 ret = sof_widget_bind_event(scomp, swidget,
2393 le16_to_cpu(tw->event_type));
2395 dev_err(scomp->dev, "error: widget event binding failed\n");
2396 kfree(swidget->private);
2402 w->dobj.private = swidget;
2403 list_add(&swidget->list, &sdev->widget_list);
2407 static int sof_route_unload(struct snd_soc_component *scomp,
2408 struct snd_soc_dobj *dobj)
2410 struct snd_sof_route *sroute;
2412 sroute = dobj->private;
2416 /* free sroute and its private data */
2417 kfree(sroute->private);
2418 list_del(&sroute->list);
2424 static int sof_widget_unload(struct snd_soc_component *scomp,
2425 struct snd_soc_dobj *dobj)
2427 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2428 const struct snd_kcontrol_new *kc;
2429 struct snd_soc_dapm_widget *widget;
2430 struct sof_ipc_pipe_new *pipeline;
2431 struct snd_sof_control *scontrol;
2432 struct snd_sof_widget *swidget;
2433 struct soc_mixer_control *sm;
2434 struct soc_bytes_ext *sbe;
2435 struct snd_sof_dai *dai;
2436 struct soc_enum *se;
2440 swidget = dobj->private;
2444 widget = swidget->widget;
2446 switch (swidget->id) {
2447 case snd_soc_dapm_dai_in:
2448 case snd_soc_dapm_dai_out:
2449 dai = swidget->private;
2452 /* free dai config */
2453 kfree(dai->dai_config);
2454 list_del(&dai->list);
2457 case snd_soc_dapm_scheduler:
2459 /* power down the pipeline schedule core */
2460 pipeline = swidget->private;
2461 ret = snd_sof_dsp_core_power_down(sdev, 1 << pipeline->core);
2463 dev_err(scomp->dev, "error: powering down pipeline schedule core %d\n",
2466 /* update enabled cores mask */
2467 sdev->enabled_cores_mask &= ~(1 << pipeline->core);
2473 for (i = 0; i < widget->num_kcontrols; i++) {
2474 kc = &widget->kcontrol_news[i];
2475 switch (dobj->widget.kcontrol_type) {
2476 case SND_SOC_TPLG_TYPE_MIXER:
2477 sm = (struct soc_mixer_control *)kc->private_value;
2478 scontrol = sm->dobj.private;
2480 kfree(scontrol->volume_table);
2482 case SND_SOC_TPLG_TYPE_ENUM:
2483 se = (struct soc_enum *)kc->private_value;
2484 scontrol = se->dobj.private;
2486 case SND_SOC_TPLG_TYPE_BYTES:
2487 sbe = (struct soc_bytes_ext *)kc->private_value;
2488 scontrol = sbe->dobj.private;
2491 dev_warn(scomp->dev, "unsupported kcontrol_type\n");
2494 kfree(scontrol->control_data);
2495 list_del(&scontrol->list);
2500 /* free private value */
2501 kfree(swidget->private);
2503 /* remove and free swidget object */
2504 list_del(&swidget->list);
2511 * DAI HW configuration.
2514 /* FE DAI - used for any driver specific init */
2515 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2516 struct snd_soc_dai_driver *dai_drv,
2517 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2519 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2520 struct snd_soc_tplg_stream_caps *caps;
2521 struct snd_soc_tplg_private *private = &pcm->priv;
2522 struct snd_sof_pcm *spcm;
2526 /* nothing to do for BEs atm */
2530 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2534 spcm->scomp = scomp;
2536 for_each_pcm_streams(stream) {
2537 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
2538 INIT_WORK(&spcm->stream[stream].period_elapsed_work,
2539 snd_sof_pcm_period_elapsed_work);
2543 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
2545 dai_drv->dobj.private = spcm;
2546 list_add(&spcm->list, &sdev->pcm_list);
2548 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2549 ARRAY_SIZE(stream_tokens), private->array,
2550 le32_to_cpu(private->size));
2552 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
2553 le32_to_cpu(private->size));
2557 /* do we need to allocate playback PCM DMA pages */
2558 if (!spcm->pcm.playback)
2561 stream = SNDRV_PCM_STREAM_PLAYBACK;
2563 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2564 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2566 caps = &spcm->pcm.caps[stream];
2568 /* allocate playback page table buffer */
2569 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2570 PAGE_SIZE, &spcm->stream[stream].page_table);
2572 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2578 /* bind pcm to host comp */
2579 ret = spcm_bind(scomp, spcm, stream);
2582 "error: can't bind pcm to host\n");
2583 goto free_playback_tables;
2587 stream = SNDRV_PCM_STREAM_CAPTURE;
2589 /* do we need to allocate capture PCM DMA pages */
2590 if (!spcm->pcm.capture)
2593 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2594 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
2596 caps = &spcm->pcm.caps[stream];
2598 /* allocate capture page table buffer */
2599 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2600 PAGE_SIZE, &spcm->stream[stream].page_table);
2602 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
2604 goto free_playback_tables;
2607 /* bind pcm to host comp */
2608 ret = spcm_bind(scomp, spcm, stream);
2611 "error: can't bind pcm to host\n");
2612 snd_dma_free_pages(&spcm->stream[stream].page_table);
2613 goto free_playback_tables;
2618 free_playback_tables:
2619 if (spcm->pcm.playback)
2620 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2625 static int sof_dai_unload(struct snd_soc_component *scomp,
2626 struct snd_soc_dobj *dobj)
2628 struct snd_sof_pcm *spcm = dobj->private;
2630 /* free PCM DMA pages */
2631 if (spcm->pcm.playback)
2632 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2634 if (spcm->pcm.capture)
2635 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2637 /* remove from list and free spcm */
2638 list_del(&spcm->list);
2644 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2645 struct sof_ipc_dai_config *config)
2647 /* clock directions wrt codec */
2648 if (hw_config->bclk_master == SND_SOC_TPLG_BCLK_CM) {
2649 /* codec is bclk master */
2650 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2651 config->format |= SOF_DAI_FMT_CBM_CFM;
2653 config->format |= SOF_DAI_FMT_CBM_CFS;
2655 /* codec is bclk slave */
2656 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2657 config->format |= SOF_DAI_FMT_CBS_CFM;
2659 config->format |= SOF_DAI_FMT_CBS_CFS;
2662 /* inverted clocks ? */
2663 if (hw_config->invert_bclk) {
2664 if (hw_config->invert_fsync)
2665 config->format |= SOF_DAI_FMT_IB_IF;
2667 config->format |= SOF_DAI_FMT_IB_NF;
2669 if (hw_config->invert_fsync)
2670 config->format |= SOF_DAI_FMT_NB_IF;
2672 config->format |= SOF_DAI_FMT_NB_NF;
2677 * Send IPC and set the same config for all DAIs with name matching the link
2678 * name. Note that the function can only be used for the case that all DAIs
2679 * have a common DAI config for now.
2681 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2682 struct snd_soc_dai_link *link,
2683 struct sof_ipc_dai_config *config)
2685 struct snd_sof_dai *dai;
2688 list_for_each_entry(dai, &sdev->dai_list, list) {
2692 if (strcmp(link->name, dai->name) == 0) {
2693 struct sof_ipc_reply reply;
2697 * the same dai config will be applied to all DAIs in
2698 * the same dai link. We have to ensure that the ipc
2699 * dai config's dai_index match to the component's
2702 config->dai_index = dai->comp_dai.dai_index;
2704 /* send message to DSP */
2705 ret = sof_ipc_tx_message(sdev->ipc,
2706 config->hdr.cmd, config, size,
2707 &reply, sizeof(reply));
2710 dev_err(sdev->dev, "error: failed to set DAI config for %s index %d\n",
2711 dai->name, config->dai_index);
2714 dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2715 if (!dai->dai_config)
2718 /* set cpu_dai_name */
2719 dai->cpu_dai_name = link->cpus->dai_name;
2726 * machine driver may define a dai link with playback and capture
2727 * dai enabled, but the dai link in topology would support both, one
2728 * or none of them. Here print a warning message to notify user
2731 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2738 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2739 struct snd_soc_dai_link *link,
2740 struct snd_soc_tplg_link_config *cfg,
2741 struct snd_soc_tplg_hw_config *hw_config,
2742 struct sof_ipc_dai_config *config)
2744 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2745 struct snd_soc_tplg_private *private = &cfg->priv;
2746 u32 size = sizeof(*config);
2749 /* handle master/slave and inverted clocks */
2750 sof_dai_set_format(hw_config, config);
2753 memset(&config->ssp, 0, sizeof(struct sof_ipc_dai_ssp_params));
2754 config->hdr.size = size;
2756 ret = sof_parse_tokens(scomp, &config->ssp, ssp_tokens,
2757 ARRAY_SIZE(ssp_tokens), private->array,
2758 le32_to_cpu(private->size));
2760 dev_err(scomp->dev, "error: parse ssp tokens failed %d\n",
2761 le32_to_cpu(private->size));
2765 config->ssp.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2766 config->ssp.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2767 config->ssp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2768 config->ssp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2769 config->ssp.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2770 config->ssp.mclk_direction = hw_config->mclk_direction;
2771 config->ssp.rx_slots = le32_to_cpu(hw_config->rx_slots);
2772 config->ssp.tx_slots = le32_to_cpu(hw_config->tx_slots);
2774 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",
2775 config->dai_index, config->format,
2776 config->ssp.mclk_rate, config->ssp.bclk_rate,
2777 config->ssp.fsync_rate, config->ssp.sample_valid_bits,
2778 config->ssp.tdm_slot_width, config->ssp.tdm_slots,
2779 config->ssp.mclk_id, config->ssp.quirks);
2781 /* validate SSP fsync rate and channel count */
2782 if (config->ssp.fsync_rate < 8000 || config->ssp.fsync_rate > 192000) {
2783 dev_err(scomp->dev, "error: invalid fsync rate for SSP%d\n",
2788 if (config->ssp.tdm_slots < 1 || config->ssp.tdm_slots > 8) {
2789 dev_err(scomp->dev, "error: invalid channel count for SSP%d\n",
2794 /* set config for all DAI's with name matching the link name */
2795 ret = sof_set_dai_config(sdev, size, link, config);
2797 dev_err(scomp->dev, "error: failed to save DAI config for SSP%d\n",
2803 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2804 struct snd_soc_dai_link *link,
2805 struct snd_soc_tplg_link_config *cfg,
2806 struct snd_soc_tplg_hw_config *hw_config,
2807 struct sof_ipc_dai_config *config)
2809 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2810 struct snd_soc_tplg_private *private = &cfg->priv;
2811 u32 size = sizeof(*config);
2814 /* handle master/slave and inverted clocks */
2815 sof_dai_set_format(hw_config, config);
2818 memset(&config->sai, 0, sizeof(struct sof_ipc_dai_sai_params));
2819 config->hdr.size = size;
2821 ret = sof_parse_tokens(scomp, &config->sai, sai_tokens,
2822 ARRAY_SIZE(sai_tokens), private->array,
2823 le32_to_cpu(private->size));
2825 dev_err(scomp->dev, "error: parse sai tokens failed %d\n",
2826 le32_to_cpu(private->size));
2830 config->sai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2831 config->sai.mclk_direction = hw_config->mclk_direction;
2833 config->sai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2834 config->sai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2835 config->sai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2836 config->sai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2838 dev_info(scomp->dev,
2839 "tplg: config SAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2840 config->dai_index, config->format,
2841 config->sai.mclk_rate, config->sai.tdm_slot_width,
2842 config->sai.tdm_slots, config->sai.mclk_id);
2844 if (config->sai.tdm_slots < 1 || config->sai.tdm_slots > 8) {
2845 dev_err(scomp->dev, "error: invalid channel count for SAI%d\n",
2850 /* set config for all DAI's with name matching the link name */
2851 ret = sof_set_dai_config(sdev, size, link, config);
2853 dev_err(scomp->dev, "error: failed to save DAI config for SAI%d\n",
2859 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2860 struct snd_soc_dai_link *link,
2861 struct snd_soc_tplg_link_config *cfg,
2862 struct snd_soc_tplg_hw_config *hw_config,
2863 struct sof_ipc_dai_config *config)
2865 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2866 struct snd_soc_tplg_private *private = &cfg->priv;
2867 u32 size = sizeof(*config);
2870 /* handle master/slave and inverted clocks */
2871 sof_dai_set_format(hw_config, config);
2874 memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2875 config->hdr.size = size;
2877 ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2878 ARRAY_SIZE(esai_tokens), private->array,
2879 le32_to_cpu(private->size));
2881 dev_err(scomp->dev, "error: parse esai tokens failed %d\n",
2882 le32_to_cpu(private->size));
2886 config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2887 config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2888 config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2889 config->esai.mclk_direction = hw_config->mclk_direction;
2890 config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2891 config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2892 config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2893 config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2895 dev_info(scomp->dev,
2896 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2897 config->dai_index, config->format,
2898 config->esai.mclk_rate, config->esai.tdm_slot_width,
2899 config->esai.tdm_slots, config->esai.mclk_id);
2901 if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2902 dev_err(scomp->dev, "error: invalid channel count for ESAI%d\n",
2907 /* set config for all DAI's with name matching the link name */
2908 ret = sof_set_dai_config(sdev, size, link, config);
2910 dev_err(scomp->dev, "error: failed to save DAI config for ESAI%d\n",
2916 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
2917 struct snd_soc_dai_link *link,
2918 struct snd_soc_tplg_link_config *cfg,
2919 struct snd_soc_tplg_hw_config *hw_config,
2920 struct sof_ipc_dai_config *config)
2922 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2923 struct snd_soc_tplg_private *private = &cfg->priv;
2924 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
2925 struct sof_ipc_fw_version *v = &ready->version;
2926 size_t size = sizeof(*config);
2929 /* Ensure the entire DMIC config struct is zeros */
2930 memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
2932 /* get DMIC tokens */
2933 ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
2934 ARRAY_SIZE(dmic_tokens), private->array,
2935 le32_to_cpu(private->size));
2937 dev_err(scomp->dev, "error: parse dmic tokens failed %d\n",
2938 le32_to_cpu(private->size));
2943 * alloc memory for private member
2944 * Used to track the pdm config array index currently being parsed
2946 sdev->private = kzalloc(sizeof(u32), GFP_KERNEL);
2950 /* get DMIC PDM tokens */
2951 ret = sof_parse_token_sets(scomp, &config->dmic.pdm[0], dmic_pdm_tokens,
2952 ARRAY_SIZE(dmic_pdm_tokens), private->array,
2953 le32_to_cpu(private->size),
2954 config->dmic.num_pdm_active,
2955 sizeof(struct sof_ipc_dai_dmic_pdm_ctrl));
2958 dev_err(scomp->dev, "error: parse dmic pdm tokens failed %d\n",
2959 le32_to_cpu(private->size));
2963 /* set IPC header size */
2964 config->hdr.size = size;
2966 /* debug messages */
2967 dev_dbg(scomp->dev, "tplg: config DMIC%d driver version %d\n",
2968 config->dai_index, config->dmic.driver_ipc_version);
2969 dev_dbg(scomp->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
2970 config->dmic.pdmclk_min, config->dmic.pdmclk_max,
2971 config->dmic.duty_min);
2972 dev_dbg(scomp->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
2973 config->dmic.duty_max, config->dmic.fifo_fs,
2974 config->dmic.num_pdm_active);
2975 dev_dbg(scomp->dev, "fifo word length %hd\n", config->dmic.fifo_bits);
2977 for (j = 0; j < config->dmic.num_pdm_active; j++) {
2978 dev_dbg(scomp->dev, "pdm %hd mic a %hd mic b %hd\n",
2979 config->dmic.pdm[j].id,
2980 config->dmic.pdm[j].enable_mic_a,
2981 config->dmic.pdm[j].enable_mic_b);
2982 dev_dbg(scomp->dev, "pdm %hd polarity a %hd polarity b %hd\n",
2983 config->dmic.pdm[j].id,
2984 config->dmic.pdm[j].polarity_mic_a,
2985 config->dmic.pdm[j].polarity_mic_b);
2986 dev_dbg(scomp->dev, "pdm %hd clk_edge %hd skew %hd\n",
2987 config->dmic.pdm[j].id,
2988 config->dmic.pdm[j].clk_edge,
2989 config->dmic.pdm[j].skew);
2993 * this takes care of backwards compatible handling of fifo_bits_b.
2994 * It is deprecated since firmware ABI version 3.0.1.
2996 if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1))
2997 config->dmic.fifo_bits_b = config->dmic.fifo_bits;
2999 /* set config for all DAI's with name matching the link name */
3000 ret = sof_set_dai_config(sdev, size, link, config);
3002 dev_err(scomp->dev, "error: failed to save DAI config for DMIC%d\n",
3006 kfree(sdev->private);
3011 static int sof_link_hda_load(struct snd_soc_component *scomp, int index,
3012 struct snd_soc_dai_link *link,
3013 struct snd_soc_tplg_link_config *cfg,
3014 struct snd_soc_tplg_hw_config *hw_config,
3015 struct sof_ipc_dai_config *config)
3017 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3018 struct snd_soc_tplg_private *private = &cfg->priv;
3019 struct snd_soc_dai *dai;
3020 u32 size = sizeof(*config);
3024 memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
3025 config->hdr.size = size;
3027 /* get any bespoke DAI tokens */
3028 ret = sof_parse_tokens(scomp, &config->hda, hda_tokens,
3029 ARRAY_SIZE(hda_tokens), private->array,
3030 le32_to_cpu(private->size));
3032 dev_err(scomp->dev, "error: parse hda tokens failed %d\n",
3033 le32_to_cpu(private->size));
3037 dev_dbg(scomp->dev, "HDA config rate %d channels %d\n",
3038 config->hda.rate, config->hda.channels);
3040 dai = snd_soc_find_dai(link->cpus);
3042 dev_err(scomp->dev, "error: failed to find dai %s in %s",
3043 link->cpus->dai_name, __func__);
3047 config->hda.link_dma_ch = DMA_CHAN_INVALID;
3049 ret = sof_set_dai_config(sdev, size, link, config);
3051 dev_err(scomp->dev, "error: failed to process hda dai link %s",
3057 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
3058 struct snd_soc_dai_link *link,
3059 struct snd_soc_tplg_link_config *cfg,
3060 struct snd_soc_tplg_hw_config *hw_config,
3061 struct sof_ipc_dai_config *config)
3063 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3064 struct snd_soc_tplg_private *private = &cfg->priv;
3065 u32 size = sizeof(*config);
3068 ret = sof_parse_tokens(scomp, &config->alh, alh_tokens,
3069 ARRAY_SIZE(alh_tokens), private->array,
3070 le32_to_cpu(private->size));
3072 dev_err(scomp->dev, "error: parse alh tokens failed %d\n",
3073 le32_to_cpu(private->size));
3078 config->hdr.size = size;
3080 /* set config for all DAI's with name matching the link name */
3081 ret = sof_set_dai_config(sdev, size, link, config);
3083 dev_err(scomp->dev, "error: failed to save DAI config for ALH %d\n",
3089 /* DAI link - used for any driver specific init */
3090 static int sof_link_load(struct snd_soc_component *scomp, int index,
3091 struct snd_soc_dai_link *link,
3092 struct snd_soc_tplg_link_config *cfg)
3094 struct snd_soc_tplg_private *private = &cfg->priv;
3095 struct sof_ipc_dai_config config;
3096 struct snd_soc_tplg_hw_config *hw_config;
3101 if (!link->platforms) {
3102 dev_err(scomp->dev, "error: no platforms\n");
3105 link->platforms->name = dev_name(scomp->dev);
3108 * Set nonatomic property for FE dai links as their trigger action
3111 if (!link->no_pcm) {
3112 link->nonatomic = true;
3115 * set default trigger order for all links. Exceptions to
3116 * the rule will be handled in sof_pcm_dai_link_fixup()
3117 * For playback, the sequence is the following: start FE,
3118 * start BE, stop BE, stop FE; for Capture the sequence is
3119 * inverted start BE, start FE, stop FE, stop BE
3121 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
3122 SND_SOC_DPCM_TRIGGER_PRE;
3123 link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
3124 SND_SOC_DPCM_TRIGGER_POST;
3126 /* nothing more to do for FE dai links */
3130 /* check we have some tokens - we need at least DAI type */
3131 if (le32_to_cpu(private->size) == 0) {
3132 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
3136 /* Send BE DAI link configurations to DSP */
3137 memset(&config, 0, sizeof(config));
3139 /* get any common DAI tokens */
3140 ret = sof_parse_tokens(scomp, &config, dai_link_tokens,
3141 ARRAY_SIZE(dai_link_tokens), private->array,
3142 le32_to_cpu(private->size));
3144 dev_err(scomp->dev, "error: parse link tokens failed %d\n",
3145 le32_to_cpu(private->size));
3150 * DAI links are expected to have at least 1 hw_config.
3151 * But some older topologies might have no hw_config for HDA dai links.
3153 num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
3154 if (!num_hw_configs) {
3155 if (config.type != SOF_DAI_INTEL_HDA) {
3156 dev_err(scomp->dev, "error: unexpected DAI config count %d!\n",
3157 le32_to_cpu(cfg->num_hw_configs));
3161 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d!\n",
3162 cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3164 for (i = 0; i < num_hw_configs; i++) {
3165 if (cfg->hw_config[i].id == cfg->default_hw_config_id)
3169 if (i == num_hw_configs) {
3170 dev_err(scomp->dev, "error: default hw_config id: %d not found!\n",
3171 le32_to_cpu(cfg->default_hw_config_id));
3176 /* configure dai IPC message */
3177 hw_config = &cfg->hw_config[i];
3179 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3180 config.format = le32_to_cpu(hw_config->fmt);
3182 /* now load DAI specific data and send IPC - type comes from token */
3183 switch (config.type) {
3184 case SOF_DAI_INTEL_SSP:
3185 ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config,
3188 case SOF_DAI_INTEL_DMIC:
3189 ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config,
3192 case SOF_DAI_INTEL_HDA:
3193 ret = sof_link_hda_load(scomp, index, link, cfg, hw_config,
3196 case SOF_DAI_INTEL_ALH:
3197 ret = sof_link_alh_load(scomp, index, link, cfg, hw_config,
3200 case SOF_DAI_IMX_SAI:
3201 ret = sof_link_sai_load(scomp, index, link, cfg, hw_config,
3204 case SOF_DAI_IMX_ESAI:
3205 ret = sof_link_esai_load(scomp, index, link, cfg, hw_config,
3209 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3220 static int sof_link_hda_unload(struct snd_sof_dev *sdev,
3221 struct snd_soc_dai_link *link)
3223 struct snd_soc_dai *dai;
3226 dai = snd_soc_find_dai(link->cpus);
3228 dev_err(sdev->dev, "error: failed to find dai %s in %s",
3229 link->cpus->dai_name, __func__);
3236 static int sof_link_unload(struct snd_soc_component *scomp,
3237 struct snd_soc_dobj *dobj)
3239 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3240 struct snd_soc_dai_link *link =
3241 container_of(dobj, struct snd_soc_dai_link, dobj);
3243 struct snd_sof_dai *sof_dai;
3246 /* only BE link is loaded by sof */
3250 list_for_each_entry(sof_dai, &sdev->dai_list, list) {
3254 if (strcmp(link->name, sof_dai->name) == 0)
3258 dev_err(scomp->dev, "error: failed to find dai %s in %s",
3259 link->name, __func__);
3263 switch (sof_dai->dai_config->type) {
3264 case SOF_DAI_INTEL_SSP:
3265 case SOF_DAI_INTEL_DMIC:
3266 case SOF_DAI_INTEL_ALH:
3267 case SOF_DAI_IMX_SAI:
3268 case SOF_DAI_IMX_ESAI:
3269 /* no resource needs to be released for all cases above */
3271 case SOF_DAI_INTEL_HDA:
3272 ret = sof_link_hda_unload(sdev, link);
3275 dev_err(scomp->dev, "error: invalid DAI type %d\n",
3276 sof_dai->dai_config->type);
3284 /* DAI link - used for any driver specific init */
3285 static int sof_route_load(struct snd_soc_component *scomp, int index,
3286 struct snd_soc_dapm_route *route)
3288 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3289 struct sof_ipc_pipe_comp_connect *connect;
3290 struct snd_sof_widget *source_swidget, *sink_swidget;
3291 struct snd_soc_dobj *dobj = &route->dobj;
3292 struct snd_sof_route *sroute;
3293 struct sof_ipc_reply reply;
3296 /* allocate memory for sroute and connect */
3297 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3301 sroute->scomp = scomp;
3303 connect = kzalloc(sizeof(*connect), GFP_KERNEL);
3309 connect->hdr.size = sizeof(*connect);
3310 connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
3312 dev_dbg(scomp->dev, "sink %s control %s source %s\n",
3313 route->sink, route->control ? route->control : "none",
3316 /* source component */
3317 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
3318 if (!source_swidget) {
3319 dev_err(scomp->dev, "error: source %s not found\n",
3326 * Virtual widgets of type output/out_drv may be added in topology
3327 * for compatibility. These are not handled by the FW.
3328 * So, don't send routes whose source/sink widget is of such types
3331 if (source_swidget->id == snd_soc_dapm_out_drv ||
3332 source_swidget->id == snd_soc_dapm_output)
3335 connect->source_id = source_swidget->comp_id;
3337 /* sink component */
3338 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
3339 if (!sink_swidget) {
3340 dev_err(scomp->dev, "error: sink %s not found\n",
3347 * Don't send routes whose sink widget is of type
3348 * output or out_drv to the DSP
3350 if (sink_swidget->id == snd_soc_dapm_out_drv ||
3351 sink_swidget->id == snd_soc_dapm_output)
3354 connect->sink_id = sink_swidget->comp_id;
3357 * For virtual routes, both sink and source are not
3358 * buffer. Since only buffer linked to component is supported by
3359 * FW, others are reported as error, add check in route function,
3360 * do not send it to FW when both source and sink are not buffer
3362 if (source_swidget->id != snd_soc_dapm_buffer &&
3363 sink_swidget->id != snd_soc_dapm_buffer) {
3364 dev_dbg(scomp->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3365 route->source, route->sink);
3369 ret = sof_ipc_tx_message(sdev->ipc,
3371 connect, sizeof(*connect),
3372 &reply, sizeof(reply));
3374 /* check IPC return value */
3376 dev_err(scomp->dev, "error: failed to add route sink %s control %s source %s\n",
3378 route->control ? route->control : "none",
3383 /* check IPC reply */
3384 if (reply.error < 0) {
3385 dev_err(scomp->dev, "error: DSP failed to add route sink %s control %s source %s result %d\n",
3387 route->control ? route->control : "none",
3388 route->source, reply.error);
3393 sroute->route = route;
3394 dobj->private = sroute;
3395 sroute->private = connect;
3397 /* add route to route list */
3398 list_add(&sroute->list, &sdev->route_list);
3409 /* Function to set the initial value of SOF kcontrols.
3410 * The value will be stored in scontrol->control_data
3412 static int snd_sof_cache_kcontrol_val(struct snd_soc_component *scomp)
3414 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3415 struct snd_sof_control *scontrol = NULL;
3416 int ipc_cmd, ctrl_type;
3419 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
3421 /* notify DSP of kcontrol values */
3422 switch (scontrol->cmd) {
3423 case SOF_CTRL_CMD_VOLUME:
3424 case SOF_CTRL_CMD_ENUM:
3425 case SOF_CTRL_CMD_SWITCH:
3426 ipc_cmd = SOF_IPC_COMP_GET_VALUE;
3427 ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_GET;
3429 case SOF_CTRL_CMD_BINARY:
3430 ipc_cmd = SOF_IPC_COMP_GET_DATA;
3431 ctrl_type = SOF_CTRL_TYPE_DATA_GET;
3435 "error: Invalid scontrol->cmd: %d\n",
3439 ret = snd_sof_ipc_set_get_comp_data(scontrol,
3444 dev_warn(scomp->dev,
3445 "error: kcontrol value get for widget: %d\n",
3453 int snd_sof_complete_pipeline(struct device *dev,
3454 struct snd_sof_widget *swidget)
3456 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
3457 struct sof_ipc_pipe_ready ready;
3458 struct sof_ipc_reply reply;
3461 dev_dbg(dev, "tplg: complete pipeline %s id %d\n",
3462 swidget->widget->name, swidget->comp_id);
3464 memset(&ready, 0, sizeof(ready));
3465 ready.hdr.size = sizeof(ready);
3466 ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3467 ready.comp_id = swidget->comp_id;
3469 ret = sof_ipc_tx_message(sdev->ipc,
3470 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3477 /* completion - called at completion of firmware loading */
3478 static void sof_complete(struct snd_soc_component *scomp)
3480 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3481 struct snd_sof_widget *swidget;
3483 /* some widget types require completion notificattion */
3484 list_for_each_entry(swidget, &sdev->widget_list, list) {
3485 if (swidget->complete)
3488 switch (swidget->id) {
3489 case snd_soc_dapm_scheduler:
3491 snd_sof_complete_pipeline(scomp->dev, swidget);
3498 * cache initial values of SOF kcontrols by reading DSP value over
3499 * IPC. It may be overwritten by alsa-mixer after booting up
3501 snd_sof_cache_kcontrol_val(scomp);
3504 /* manifest - optional to inform component of manifest */
3505 static int sof_manifest(struct snd_soc_component *scomp, int index,
3506 struct snd_soc_tplg_manifest *man)
3511 size = le32_to_cpu(man->priv.size);
3513 /* backward compatible with tplg without ABI info */
3515 dev_dbg(scomp->dev, "No topology ABI info\n");
3519 if (size != SOF_TPLG_ABI_SIZE) {
3520 dev_err(scomp->dev, "error: invalid topology ABI size\n");
3524 dev_info(scomp->dev,
3525 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3526 man->priv.data[0], man->priv.data[1],
3527 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3530 abi_version = SOF_ABI_VER(man->priv.data[0],
3534 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3535 dev_err(scomp->dev, "error: incompatible topology ABI version\n");
3539 if (abi_version > SOF_ABI_VERSION) {
3540 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3541 dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
3543 dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
3551 /* vendor specific kcontrol handlers available for binding */
3552 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3553 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3554 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3555 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3556 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3559 /* vendor specific bytes ext handlers available for binding */
3560 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3561 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3564 static struct snd_soc_tplg_ops sof_tplg_ops = {
3565 /* external kcontrol init - used for any driver specific init */
3566 .control_load = sof_control_load,
3567 .control_unload = sof_control_unload,
3569 /* external kcontrol init - used for any driver specific init */
3570 .dapm_route_load = sof_route_load,
3571 .dapm_route_unload = sof_route_unload,
3573 /* external widget init - used for any driver specific init */
3574 /* .widget_load is not currently used */
3575 .widget_ready = sof_widget_ready,
3576 .widget_unload = sof_widget_unload,
3578 /* FE DAI - used for any driver specific init */
3579 .dai_load = sof_dai_load,
3580 .dai_unload = sof_dai_unload,
3582 /* DAI link - used for any driver specific init */
3583 .link_load = sof_link_load,
3584 .link_unload = sof_link_unload,
3586 /* completion - called at completion of firmware loading */
3587 .complete = sof_complete,
3589 /* manifest - optional to inform component of manifest */
3590 .manifest = sof_manifest,
3592 /* vendor specific kcontrol handlers available for binding */
3593 .io_ops = sof_io_ops,
3594 .io_ops_count = ARRAY_SIZE(sof_io_ops),
3596 /* vendor specific bytes ext handlers available for binding */
3597 .bytes_ext_ops = sof_bytes_ext_ops,
3598 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
3601 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
3603 const struct firmware *fw;
3606 dev_dbg(scomp->dev, "loading topology:%s\n", file);
3608 ret = request_firmware(&fw, file, scomp->dev);
3610 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
3615 ret = snd_soc_tplg_component_load(scomp,
3617 SND_SOC_TPLG_INDEX_ALL);
3619 dev_err(scomp->dev, "error: tplg component load failed %d\n",
3624 release_firmware(fw);
3627 EXPORT_SYMBOL(snd_sof_load_topology);