1 // SPDX-License-Identifier: (GPL-2.0 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 <sound/tlv.h>
13 #include <sound/pcm_params.h>
14 #include <uapi/sound/sof/tokens.h>
18 #define COMP_ID_UNASSIGNED 0xffffffff
20 * Constants used in the computation of linear volume gain
21 * from dB gain 20th root of 10 in Q1.16 fixed-point notation
23 #define VOL_TWENTIETH_ROOT_OF_TEN 73533
24 /* 40th root of 10 in Q1.16 fixed-point notation*/
25 #define VOL_FORTIETH_ROOT_OF_TEN 69419
27 * Volume fractional word length define to 16 sets
28 * the volume linear gain value to use Qx.16 format
31 /* 0.5 dB step value in topology TLV */
32 #define VOL_HALF_DB_STEP 50
33 /* Full volume for default values */
34 #define VOL_ZERO_DB BIT(VOLUME_FWL)
42 /* size of tplg abi in byte */
43 #define SOF_TPLG_ABI_SIZE 3
45 struct sof_widget_data {
48 struct sof_abi_hdr *pdata;
49 struct snd_sof_control *control;
52 /* send pcm params ipc */
53 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir)
55 struct sof_ipc_pcm_params_reply ipc_params_reply;
56 struct snd_sof_dev *sdev = swidget->sdev;
57 struct sof_ipc_pcm_params pcm;
58 struct snd_pcm_hw_params *params;
59 struct snd_sof_pcm *spcm;
62 memset(&pcm, 0, sizeof(pcm));
64 /* get runtime PCM params using widget's stream name */
65 spcm = snd_sof_find_spcm_name(sdev, swidget->widget->sname);
67 dev_err(sdev->dev, "error: cannot find PCM for %s\n",
68 swidget->widget->name);
72 params = &spcm->params[dir];
74 /* set IPC PCM params */
75 pcm.hdr.size = sizeof(pcm);
76 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
77 pcm.comp_id = swidget->comp_id;
78 pcm.params.hdr.size = sizeof(pcm.params);
79 pcm.params.direction = dir;
80 pcm.params.sample_valid_bytes = params_width(params) >> 3;
81 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
82 pcm.params.rate = params_rate(params);
83 pcm.params.channels = params_channels(params);
84 pcm.params.host_period_bytes = params_period_bytes(params);
87 switch (params_format(params)) {
88 case SNDRV_PCM_FORMAT_S16:
89 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
91 case SNDRV_PCM_FORMAT_S24:
92 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
94 case SNDRV_PCM_FORMAT_S32:
95 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
101 /* send IPC to the DSP */
102 ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
103 &ipc_params_reply, sizeof(ipc_params_reply));
105 dev_err(sdev->dev, "error: pcm params failed for %s\n",
106 swidget->widget->name);
111 /* send stream trigger ipc */
112 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd)
114 struct snd_sof_dev *sdev = swidget->sdev;
115 struct sof_ipc_stream stream;
116 struct sof_ipc_reply reply;
119 /* set IPC stream params */
120 stream.hdr.size = sizeof(stream);
121 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd;
122 stream.comp_id = swidget->comp_id;
124 /* send IPC to the DSP */
125 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
126 sizeof(stream), &reply, sizeof(reply));
128 dev_err(sdev->dev, "error: failed to trigger %s\n",
129 swidget->widget->name);
134 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w,
135 struct snd_kcontrol *k, int event)
137 struct snd_sof_widget *swidget = w->dobj.private;
138 int stream = SNDRV_PCM_STREAM_CAPTURE;
139 struct snd_sof_dev *sdev;
140 struct snd_sof_pcm *spcm;
146 sdev = swidget->sdev;
148 dev_dbg(sdev->dev, "received event %d for widget %s\n",
151 /* get runtime PCM params using widget's stream name */
152 spcm = snd_sof_find_spcm_name(sdev, swidget->widget->sname);
154 dev_err(sdev->dev, "error: cannot find PCM for %s\n",
155 swidget->widget->name);
161 case SND_SOC_DAPM_PRE_PMU:
162 if (spcm->stream[stream].suspend_ignored) {
163 dev_dbg(sdev->dev, "PRE_PMU event ignored, KWD pipeline is already RUNNING\n");
168 ret = ipc_pcm_params(swidget, stream);
171 "error: failed to set pcm params for widget %s\n",
172 swidget->widget->name);
177 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START);
180 "error: failed to trigger widget %s\n",
181 swidget->widget->name);
183 case SND_SOC_DAPM_POST_PMD:
184 if (spcm->stream[stream].suspend_ignored) {
185 dev_dbg(sdev->dev, "POST_PMD even ignored, KWD pipeline will remain RUNNING\n");
190 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP);
193 "error: failed to trigger widget %s\n",
194 swidget->widget->name);
197 ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE);
200 "error: failed to trigger widget %s\n",
201 swidget->widget->name);
210 /* event handlers for keyword detect component */
211 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = {
212 {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event},
215 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS])
217 /* we only support dB scale TLV type at the moment */
218 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
221 /* min value in topology tlv data is multiplied by 100 */
222 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
225 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
229 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
230 TLV_DB_SCALE_MUTE) == 0)
239 * Function to truncate an unsigned 64-bit number
240 * by x bits and return 32-bit unsigned number. This
241 * function also takes care of rounding while truncating
243 static inline u32 vol_shift_64(u64 i, u32 x)
245 /* do not truncate more than 32 bits */
252 return (u32)(((i >> (x - 1)) + 1) >> 1);
256 * Function to compute a ^ exp where,
257 * a is a fractional number represented by a fixed-point
258 * integer with a fractional world length of "fwl"
260 * fwl is the fractional word length
261 * Return value is a fractional number represented by a
262 * fixed-point integer with a fractional word length of "fwl"
264 static u32 vol_pow32(u32 a, int exp, u32 fwl)
267 u32 power = 1 << fwl;
270 /* if exponent is 0, return 1 */
274 /* determine the number of iterations based on the exponent */
280 /* mutiply a "iter" times to compute power */
281 for (i = 0; i < iter; i++) {
283 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
284 * Truncate product back to fwl fractional bits with rounding
286 power = vol_shift_64((u64)power * a, fwl);
290 /* if exp is positive, return the result */
294 /* if exp is negative, return the multiplicative inverse */
295 numerator = (u64)1 << (fwl << 1);
296 do_div(numerator, power);
298 return (u32)numerator;
302 * Function to calculate volume gain from TLV data.
303 * This function can only handle gain steps that are multiples of 0.5 dB
305 static u32 vol_compute_gain(u32 value, int *tlv)
312 if (value == 0 && tlv[TLV_MUTE])
316 * compute dB gain from tlv. tlv_step
317 * in topology is multiplied by 100
319 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
322 * compute linear gain represented by fixed-point
323 * int with VOLUME_FWL fractional bits
325 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
327 /* extract the fractional part of volume step */
328 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
330 /* if volume step is an odd multiple of 0.5 dB */
331 if (f_step == VOL_HALF_DB_STEP && (value & 1))
332 linear_gain = vol_shift_64((u64)linear_gain *
333 VOL_FORTIETH_ROOT_OF_TEN,
340 * Set up volume table for kcontrols from tlv data
341 * "size" specifies the number of entries in the table
343 static int set_up_volume_table(struct snd_sof_control *scontrol,
344 int tlv[TLV_ITEMS], int size)
348 /* init the volume table */
349 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
350 if (!scontrol->volume_table)
353 /* populate the volume table */
354 for (j = 0; j < size ; j++)
355 scontrol->volume_table[j] = vol_compute_gain(j, tlv);
360 struct sof_dai_types {
362 enum sof_ipc_dai_type type;
365 static const struct sof_dai_types sof_dais[] = {
366 {"SSP", SOF_DAI_INTEL_SSP},
367 {"HDA", SOF_DAI_INTEL_HDA},
368 {"DMIC", SOF_DAI_INTEL_DMIC},
369 {"ALH", SOF_DAI_INTEL_ALH},
370 {"SAI", SOF_DAI_IMX_SAI},
371 {"ESAI", SOF_DAI_IMX_ESAI},
374 static enum sof_ipc_dai_type find_dai(const char *name)
378 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
379 if (strcmp(name, sof_dais[i].name) == 0)
380 return sof_dais[i].type;
383 return SOF_DAI_INTEL_NONE;
387 * Supported Frame format types and lookup, add new ones to end of list.
390 struct sof_frame_types {
392 enum sof_ipc_frame frame;
395 static const struct sof_frame_types sof_frames[] = {
396 {"s16le", SOF_IPC_FRAME_S16_LE},
397 {"s24le", SOF_IPC_FRAME_S24_4LE},
398 {"s32le", SOF_IPC_FRAME_S32_LE},
399 {"float", SOF_IPC_FRAME_FLOAT},
402 static enum sof_ipc_frame find_format(const char *name)
406 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
407 if (strcmp(name, sof_frames[i].name) == 0)
408 return sof_frames[i].frame;
411 /* use s32le if nothing is specified */
412 return SOF_IPC_FRAME_S32_LE;
415 struct sof_process_types {
417 enum sof_ipc_process_type type;
418 enum sof_comp_type comp_type;
421 static const struct sof_process_types sof_process[] = {
422 {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR},
423 {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR},
424 {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT},
425 {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB},
426 {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR},
427 {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX},
428 {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX},
431 static enum sof_ipc_process_type find_process(const char *name)
435 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
436 if (strcmp(name, sof_process[i].name) == 0)
437 return sof_process[i].type;
440 return SOF_PROCESS_NONE;
443 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type)
447 for (i = 0; i < ARRAY_SIZE(sof_process); i++) {
448 if (sof_process[i].type == type)
449 return sof_process[i].comp_type;
452 return SOF_COMP_NONE;
456 * Topology Token Parsing.
457 * New tokens should be added to headers and parsing tables below.
460 struct sof_topology_token {
463 int (*get_token)(void *elem, void *object, u32 offset, u32 size);
468 static int get_token_u32(void *elem, void *object, u32 offset, u32 size)
470 struct snd_soc_tplg_vendor_value_elem *velem = elem;
471 u32 *val = (u32 *)((u8 *)object + offset);
473 *val = le32_to_cpu(velem->value);
477 static int get_token_u16(void *elem, void *object, u32 offset, u32 size)
479 struct snd_soc_tplg_vendor_value_elem *velem = elem;
480 u16 *val = (u16 *)((u8 *)object + offset);
482 *val = (u16)le32_to_cpu(velem->value);
486 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size)
488 struct snd_soc_tplg_vendor_string_elem *velem = elem;
489 u32 *val = (u32 *)((u8 *)object + offset);
491 *val = find_format(velem->string);
495 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size)
497 struct snd_soc_tplg_vendor_string_elem *velem = elem;
498 u32 *val = (u32 *)((u8 *)object + offset);
500 *val = find_dai(velem->string);
504 static int get_token_process_type(void *elem, void *object, u32 offset,
507 struct snd_soc_tplg_vendor_string_elem *velem = elem;
508 u32 *val = (u32 *)((u8 *)object + offset);
510 *val = find_process(velem->string);
515 static const struct sof_topology_token buffer_tokens[] = {
516 {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
517 offsetof(struct sof_ipc_buffer, size), 0},
518 {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
519 offsetof(struct sof_ipc_buffer, caps), 0},
523 static const struct sof_topology_token dai_tokens[] = {
524 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
525 offsetof(struct sof_ipc_comp_dai, type), 0},
526 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
527 offsetof(struct sof_ipc_comp_dai, dai_index), 0},
528 {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
529 offsetof(struct sof_ipc_comp_dai, direction), 0},
533 static const struct sof_topology_token dai_link_tokens[] = {
534 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
535 offsetof(struct sof_ipc_dai_config, type), 0},
536 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
537 offsetof(struct sof_ipc_dai_config, dai_index), 0},
541 static const struct sof_topology_token sched_tokens[] = {
542 {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
543 offsetof(struct sof_ipc_pipe_new, period), 0},
544 {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
545 offsetof(struct sof_ipc_pipe_new, priority), 0},
546 {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
547 offsetof(struct sof_ipc_pipe_new, period_mips), 0},
548 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
549 offsetof(struct sof_ipc_pipe_new, core), 0},
550 {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
551 offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0},
552 {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
553 offsetof(struct sof_ipc_pipe_new, time_domain), 0},
557 static const struct sof_topology_token volume_tokens[] = {
558 {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD,
559 get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0},
560 {SOF_TKN_VOLUME_RAMP_STEP_MS,
561 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
562 offsetof(struct sof_ipc_comp_volume, initial_ramp), 0},
566 static const struct sof_topology_token src_tokens[] = {
567 {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
568 offsetof(struct sof_ipc_comp_src, source_rate), 0},
569 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
570 offsetof(struct sof_ipc_comp_src, sink_rate), 0},
574 static const struct sof_topology_token tone_tokens[] = {
578 static const struct sof_topology_token process_tokens[] = {
579 {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING,
580 get_token_process_type,
581 offsetof(struct sof_ipc_comp_process, type), 0},
585 static const struct sof_topology_token pcm_tokens[] = {
586 {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
587 offsetof(struct sof_ipc_comp_host, dmac_config), 0},
591 static const struct sof_topology_token stream_tokens[] = {
592 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3,
593 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
594 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible), 0},
595 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3,
596 SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
597 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible), 0},
600 /* Generic components */
601 static const struct sof_topology_token comp_tokens[] = {
602 {SOF_TKN_COMP_PERIOD_SINK_COUNT,
603 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
604 offsetof(struct sof_ipc_comp_config, periods_sink), 0},
605 {SOF_TKN_COMP_PERIOD_SOURCE_COUNT,
606 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
607 offsetof(struct sof_ipc_comp_config, periods_source), 0},
608 {SOF_TKN_COMP_FORMAT,
609 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format,
610 offsetof(struct sof_ipc_comp_config, frame_fmt), 0},
614 static const struct sof_topology_token ssp_tokens[] = {
615 {SOF_TKN_INTEL_SSP_CLKS_CONTROL,
616 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
617 offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0},
618 {SOF_TKN_INTEL_SSP_MCLK_ID,
619 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
620 offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0},
621 {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
623 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0},
624 {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT,
626 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0},
627 {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD,
629 offsetof(struct sof_ipc_dai_ssp_params, quirks), 0},
630 {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL,
632 offsetof(struct sof_ipc_dai_ssp_params,
633 tdm_per_slot_padding_flag), 0},
634 {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD,
636 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0},
641 static const struct sof_topology_token dmic_tokens[] = {
642 {SOF_TKN_INTEL_DMIC_DRIVER_VERSION,
643 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
644 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version),
646 {SOF_TKN_INTEL_DMIC_CLK_MIN,
647 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
648 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0},
649 {SOF_TKN_INTEL_DMIC_CLK_MAX,
650 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
651 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0},
652 {SOF_TKN_INTEL_DMIC_SAMPLE_RATE,
653 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
654 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0},
655 {SOF_TKN_INTEL_DMIC_DUTY_MIN,
656 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
657 offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0},
658 {SOF_TKN_INTEL_DMIC_DUTY_MAX,
659 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
660 offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0},
661 {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
662 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
663 offsetof(struct sof_ipc_dai_dmic_params,
665 {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH,
666 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
667 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0},
668 {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS,
669 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
670 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0},
675 static const struct sof_topology_token esai_tokens[] = {
676 {SOF_TKN_IMX_ESAI_MCLK_ID,
677 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
678 offsetof(struct sof_ipc_dai_esai_params, mclk_id), 0},
683 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token
684 * as it increments the index while parsing the array of pdm tokens
685 * and determines the correct offset
687 static const struct sof_topology_token dmic_pdm_tokens[] = {
688 {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID,
689 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
690 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id),
692 {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable,
693 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
694 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a),
696 {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable,
697 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
698 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b),
700 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A,
701 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
702 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a),
704 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B,
705 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
706 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b),
708 {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE,
709 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
710 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge),
712 {SOF_TKN_INTEL_DMIC_PDM_SKEW,
713 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16,
714 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew),
719 static const struct sof_topology_token hda_tokens[] = {
723 static const struct sof_topology_token led_tokens[] = {
724 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
725 offsetof(struct snd_sof_led_control, use_led), 0},
726 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD,
727 get_token_u32, offsetof(struct snd_sof_led_control, direction), 0},
730 static void sof_parse_uuid_tokens(struct snd_soc_component *scomp,
732 const struct sof_topology_token *tokens,
734 struct snd_soc_tplg_vendor_array *array)
736 struct snd_soc_tplg_vendor_uuid_elem *elem;
739 /* parse element by element */
740 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
741 elem = &array->uuid[i];
743 /* search for token */
744 for (j = 0; j < count; j++) {
745 /* match token type */
746 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
750 if (tokens[j].token != le32_to_cpu(elem->token))
753 /* matched - now load token */
754 tokens[j].get_token(elem, object, tokens[j].offset,
760 static void sof_parse_string_tokens(struct snd_soc_component *scomp,
762 const struct sof_topology_token *tokens,
764 struct snd_soc_tplg_vendor_array *array)
766 struct snd_soc_tplg_vendor_string_elem *elem;
769 /* parse element by element */
770 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
771 elem = &array->string[i];
773 /* search for token */
774 for (j = 0; j < count; j++) {
775 /* match token type */
776 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
780 if (tokens[j].token != le32_to_cpu(elem->token))
783 /* matched - now load token */
784 tokens[j].get_token(elem, object, tokens[j].offset,
790 static void sof_parse_word_tokens(struct snd_soc_component *scomp,
792 const struct sof_topology_token *tokens,
794 struct snd_soc_tplg_vendor_array *array)
796 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
797 struct snd_soc_tplg_vendor_value_elem *elem;
798 size_t size = sizeof(struct sof_ipc_dai_dmic_pdm_ctrl);
803 /* parse element by element */
804 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
805 elem = &array->value[i];
807 /* search for token */
808 for (j = 0; j < count; j++) {
809 /* match token type */
810 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
811 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
812 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
813 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
817 if (tokens[j].token != le32_to_cpu(elem->token))
820 /* pdm config array index */
822 index = sdev->private;
824 /* matched - determine offset */
825 switch (tokens[j].token) {
826 case SOF_TKN_INTEL_DMIC_PDM_CTRL_ID:
828 /* inc number of pdm array index */
832 case SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable:
833 case SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable:
834 case SOF_TKN_INTEL_DMIC_PDM_POLARITY_A:
835 case SOF_TKN_INTEL_DMIC_PDM_POLARITY_B:
836 case SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE:
837 case SOF_TKN_INTEL_DMIC_PDM_SKEW:
839 /* check if array index is valid */
840 if (!index || *index == 0) {
842 "error: invalid array offset\n");
845 /* offset within the pdm config array */
846 offset = size * (*index - 1);
855 tokens[j].get_token(elem, object,
856 offset + tokens[j].offset,
862 static int sof_parse_tokens(struct snd_soc_component *scomp,
864 const struct sof_topology_token *tokens,
866 struct snd_soc_tplg_vendor_array *array,
869 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
872 while (priv_size > 0) {
873 asize = le32_to_cpu(array->size);
876 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
877 dev_err(sdev->dev, "error: invalid array size 0x%x\n",
882 /* make sure there is enough data before parsing */
885 dev_err(sdev->dev, "error: invalid array size 0x%x\n",
890 /* call correct parser depending on type */
891 switch (le32_to_cpu(array->type)) {
892 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
893 sof_parse_uuid_tokens(scomp, object, tokens, count,
896 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
897 sof_parse_string_tokens(scomp, object, tokens, count,
900 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
901 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
902 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
903 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
904 sof_parse_word_tokens(scomp, object, tokens, count,
908 dev_err(sdev->dev, "error: unknown token type %d\n",
914 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
920 static void sof_dbg_comp_config(struct snd_soc_component *scomp,
921 struct sof_ipc_comp_config *config)
923 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
925 dev_dbg(sdev->dev, " config: periods snk %d src %d fmt %d\n",
926 config->periods_sink, config->periods_source,
931 * Standard Kcontrols.
934 static int sof_control_load_volume(struct snd_soc_component *scomp,
935 struct snd_sof_control *scontrol,
936 struct snd_kcontrol_new *kc,
937 struct snd_soc_tplg_ctl_hdr *hdr)
939 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
940 struct snd_soc_tplg_mixer_control *mc =
941 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
942 struct sof_ipc_ctrl_data *cdata;
947 /* validate topology data */
948 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
951 /* init the volume get/put data */
952 scontrol->size = struct_size(scontrol->control_data, chanv,
953 le32_to_cpu(mc->num_channels));
954 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
955 if (!scontrol->control_data)
958 scontrol->comp_id = sdev->next_comp_id;
959 scontrol->min_volume_step = le32_to_cpu(mc->min);
960 scontrol->max_volume_step = le32_to_cpu(mc->max);
961 scontrol->num_channels = le32_to_cpu(mc->num_channels);
963 /* set cmd for mixer control */
964 if (le32_to_cpu(mc->max) == 1) {
965 scontrol->cmd = SOF_CTRL_CMD_SWITCH;
969 scontrol->cmd = SOF_CTRL_CMD_VOLUME;
971 /* extract tlv data */
972 if (get_tlv_data(kc->tlv.p, tlv) < 0) {
973 dev_err(sdev->dev, "error: invalid TLV data\n");
977 /* set up volume table */
978 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
980 dev_err(sdev->dev, "error: setting up volume table\n");
984 /* set default volume values to 0dB in control */
985 cdata = scontrol->control_data;
986 for (i = 0; i < scontrol->num_channels; i++) {
987 cdata->chanv[i].channel = i;
988 cdata->chanv[i].value = VOL_ZERO_DB;
992 /* set up possible led control from mixer private data */
993 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
994 ARRAY_SIZE(led_tokens), mc->priv.array,
995 le32_to_cpu(mc->priv.size));
997 dev_err(sdev->dev, "error: parse led tokens failed %d\n",
998 le32_to_cpu(mc->priv.size));
1002 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d\n",
1003 scontrol->comp_id, scontrol->num_channels);
1008 static int sof_control_load_enum(struct snd_soc_component *scomp,
1009 struct snd_sof_control *scontrol,
1010 struct snd_kcontrol_new *kc,
1011 struct snd_soc_tplg_ctl_hdr *hdr)
1013 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1014 struct snd_soc_tplg_enum_control *ec =
1015 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
1017 /* validate topology data */
1018 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
1021 /* init the enum get/put data */
1022 scontrol->size = struct_size(scontrol->control_data, chanv,
1023 le32_to_cpu(ec->num_channels));
1024 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL);
1025 if (!scontrol->control_data)
1028 scontrol->comp_id = sdev->next_comp_id;
1029 scontrol->num_channels = le32_to_cpu(ec->num_channels);
1031 scontrol->cmd = SOF_CTRL_CMD_ENUM;
1033 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
1034 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
1039 static int sof_control_load_bytes(struct snd_soc_component *scomp,
1040 struct snd_sof_control *scontrol,
1041 struct snd_kcontrol_new *kc,
1042 struct snd_soc_tplg_ctl_hdr *hdr)
1044 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1045 struct sof_ipc_ctrl_data *cdata;
1046 struct snd_soc_tplg_bytes_control *control =
1047 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
1048 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
1049 int max_size = sbe->max;
1051 /* init the get/put bytes data */
1052 scontrol->size = sizeof(struct sof_ipc_ctrl_data) +
1053 le32_to_cpu(control->priv.size);
1055 if (scontrol->size > max_size) {
1056 dev_err(sdev->dev, "err: bytes data size %d exceeds max %d.\n",
1057 scontrol->size, max_size);
1061 scontrol->control_data = kzalloc(max_size, GFP_KERNEL);
1062 cdata = scontrol->control_data;
1063 if (!scontrol->control_data)
1066 scontrol->comp_id = sdev->next_comp_id;
1067 scontrol->cmd = SOF_CTRL_CMD_BINARY;
1069 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d\n",
1070 scontrol->comp_id, scontrol->num_channels);
1072 if (le32_to_cpu(control->priv.size) > 0) {
1073 memcpy(cdata->data, control->priv.data,
1074 le32_to_cpu(control->priv.size));
1076 if (cdata->data->magic != SOF_ABI_MAGIC) {
1077 dev_err(sdev->dev, "error: Wrong ABI magic 0x%08x.\n",
1078 cdata->data->magic);
1081 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION,
1082 cdata->data->abi)) {
1084 "error: Incompatible ABI version 0x%08x.\n",
1088 if (cdata->data->size + sizeof(const struct sof_abi_hdr) !=
1089 le32_to_cpu(control->priv.size)) {
1091 "error: Conflict in bytes vs. priv size.\n");
1098 /* external kcontrol init - used for any driver specific init */
1099 static int sof_control_load(struct snd_soc_component *scomp, int index,
1100 struct snd_kcontrol_new *kc,
1101 struct snd_soc_tplg_ctl_hdr *hdr)
1103 struct soc_mixer_control *sm;
1104 struct soc_bytes_ext *sbe;
1105 struct soc_enum *se;
1106 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1107 struct snd_soc_dobj *dobj;
1108 struct snd_sof_control *scontrol;
1111 dev_dbg(sdev->dev, "tplg: load control type %d name : %s\n",
1112 hdr->type, hdr->name);
1114 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
1118 scontrol->sdev = sdev;
1120 switch (le32_to_cpu(hdr->ops.info)) {
1121 case SND_SOC_TPLG_CTL_VOLSW:
1122 case SND_SOC_TPLG_CTL_VOLSW_SX:
1123 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1124 sm = (struct soc_mixer_control *)kc->private_value;
1126 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
1128 case SND_SOC_TPLG_CTL_BYTES:
1129 sbe = (struct soc_bytes_ext *)kc->private_value;
1131 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
1133 case SND_SOC_TPLG_CTL_ENUM:
1134 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1135 se = (struct soc_enum *)kc->private_value;
1137 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
1139 case SND_SOC_TPLG_CTL_RANGE:
1140 case SND_SOC_TPLG_CTL_STROBE:
1141 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1142 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1143 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1144 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1145 case SND_SOC_TPLG_DAPM_CTL_PIN:
1147 dev_warn(sdev->dev, "control type not supported %d:%d:%d\n",
1148 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1153 dobj->private = scontrol;
1154 list_add(&scontrol->list, &sdev->kcontrol_list);
1158 static int sof_control_unload(struct snd_soc_component *scomp,
1159 struct snd_soc_dobj *dobj)
1161 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1162 struct sof_ipc_free fcomp;
1163 struct snd_sof_control *scontrol = dobj->private;
1165 dev_dbg(sdev->dev, "tplg: unload control name : %s\n", scomp->name);
1167 fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE;
1168 fcomp.hdr.size = sizeof(fcomp);
1169 fcomp.id = scontrol->comp_id;
1171 kfree(scontrol->control_data);
1172 list_del(&scontrol->list);
1174 /* send IPC to the DSP */
1175 return sof_ipc_tx_message(sdev->ipc,
1176 fcomp.hdr.cmd, &fcomp, sizeof(fcomp),
1184 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1185 struct snd_soc_dapm_widget *w,
1186 struct snd_soc_tplg_dapm_widget *tw,
1187 struct snd_sof_dai *dai)
1189 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1190 struct snd_soc_card *card = scomp->card;
1191 struct snd_soc_pcm_runtime *rtd;
1193 list_for_each_entry(rtd, &card->rtd_list, list) {
1194 dev_vdbg(sdev->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1195 w->name, w->sname, rtd->dai_link->stream_name);
1197 if (!w->sname || !rtd->dai_link->stream_name)
1200 /* does stream match DAI link ? */
1201 if (strcmp(w->sname, rtd->dai_link->stream_name))
1205 case snd_soc_dapm_dai_out:
1206 rtd->cpu_dai->capture_widget = w;
1207 dai->name = rtd->dai_link->name;
1208 dev_dbg(sdev->dev, "tplg: connected widget %s -> DAI link %s\n",
1209 w->name, rtd->dai_link->name);
1211 case snd_soc_dapm_dai_in:
1212 rtd->cpu_dai->playback_widget = w;
1213 dai->name = rtd->dai_link->name;
1214 dev_dbg(sdev->dev, "tplg: connected widget %s -> DAI link %s\n",
1215 w->name, rtd->dai_link->name);
1222 /* check we have a connection */
1224 dev_err(sdev->dev, "error: can't connect DAI %s stream %s\n",
1232 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index,
1233 struct snd_sof_widget *swidget,
1234 struct snd_soc_tplg_dapm_widget *tw,
1235 struct sof_ipc_comp_reply *r,
1236 struct snd_sof_dai *dai)
1238 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1239 struct snd_soc_tplg_private *private = &tw->priv;
1240 struct sof_ipc_comp_dai comp_dai;
1243 /* configure dai IPC message */
1244 memset(&comp_dai, 0, sizeof(comp_dai));
1245 comp_dai.comp.hdr.size = sizeof(comp_dai);
1246 comp_dai.comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1247 comp_dai.comp.id = swidget->comp_id;
1248 comp_dai.comp.type = SOF_COMP_DAI;
1249 comp_dai.comp.pipeline_id = index;
1250 comp_dai.config.hdr.size = sizeof(comp_dai.config);
1252 ret = sof_parse_tokens(scomp, &comp_dai, dai_tokens,
1253 ARRAY_SIZE(dai_tokens), private->array,
1254 le32_to_cpu(private->size));
1256 dev_err(sdev->dev, "error: parse dai tokens failed %d\n",
1257 le32_to_cpu(private->size));
1261 ret = sof_parse_tokens(scomp, &comp_dai.config, comp_tokens,
1262 ARRAY_SIZE(comp_tokens), private->array,
1263 le32_to_cpu(private->size));
1265 dev_err(sdev->dev, "error: parse dai.cfg tokens failed %d\n",
1270 dev_dbg(sdev->dev, "dai %s: type %d index %d\n",
1271 swidget->widget->name, comp_dai.type, comp_dai.dai_index);
1272 sof_dbg_comp_config(scomp, &comp_dai.config);
1274 ret = sof_ipc_tx_message(sdev->ipc, comp_dai.comp.hdr.cmd,
1275 &comp_dai, sizeof(comp_dai), r, sizeof(*r));
1277 if (ret == 0 && dai) {
1279 memcpy(&dai->comp_dai, &comp_dai, sizeof(comp_dai));
1289 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index,
1290 struct snd_sof_widget *swidget,
1291 struct snd_soc_tplg_dapm_widget *tw,
1292 struct sof_ipc_comp_reply *r)
1294 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1295 struct snd_soc_tplg_private *private = &tw->priv;
1296 struct sof_ipc_buffer *buffer;
1299 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1303 /* configure dai IPC message */
1304 buffer->comp.hdr.size = sizeof(*buffer);
1305 buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW;
1306 buffer->comp.id = swidget->comp_id;
1307 buffer->comp.type = SOF_COMP_BUFFER;
1308 buffer->comp.pipeline_id = index;
1310 ret = sof_parse_tokens(scomp, buffer, buffer_tokens,
1311 ARRAY_SIZE(buffer_tokens), private->array,
1312 le32_to_cpu(private->size));
1314 dev_err(sdev->dev, "error: parse buffer tokens failed %d\n",
1320 dev_dbg(sdev->dev, "buffer %s: size %d caps 0x%x\n",
1321 swidget->widget->name, buffer->size, buffer->caps);
1323 swidget->private = buffer;
1325 ret = sof_ipc_tx_message(sdev->ipc, buffer->comp.hdr.cmd, buffer,
1326 sizeof(*buffer), r, sizeof(*r));
1328 dev_err(sdev->dev, "error: buffer %s load failed\n",
1329 swidget->widget->name);
1336 /* bind PCM ID to host component ID */
1337 static int spcm_bind(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
1340 struct snd_sof_widget *host_widget;
1342 host_widget = snd_sof_find_swidget_sname(sdev,
1343 spcm->pcm.caps[dir].name,
1346 dev_err(sdev->dev, "can't find host comp to bind pcm\n");
1350 spcm->stream[dir].comp_id = host_widget->comp_id;
1359 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index,
1360 struct snd_sof_widget *swidget,
1361 enum sof_ipc_stream_direction dir,
1362 struct snd_soc_tplg_dapm_widget *tw,
1363 struct sof_ipc_comp_reply *r)
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_host *host;
1370 host = kzalloc(sizeof(*host), GFP_KERNEL);
1374 /* configure host comp IPC message */
1375 host->comp.hdr.size = sizeof(*host);
1376 host->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1377 host->comp.id = swidget->comp_id;
1378 host->comp.type = SOF_COMP_HOST;
1379 host->comp.pipeline_id = index;
1380 host->direction = dir;
1381 host->config.hdr.size = sizeof(host->config);
1383 ret = sof_parse_tokens(scomp, host, pcm_tokens,
1384 ARRAY_SIZE(pcm_tokens), private->array,
1385 le32_to_cpu(private->size));
1387 dev_err(sdev->dev, "error: parse host tokens failed %d\n",
1392 ret = sof_parse_tokens(scomp, &host->config, comp_tokens,
1393 ARRAY_SIZE(comp_tokens), private->array,
1394 le32_to_cpu(private->size));
1396 dev_err(sdev->dev, "error: parse host.cfg tokens failed %d\n",
1397 le32_to_cpu(private->size));
1401 dev_dbg(sdev->dev, "loaded host %s\n", swidget->widget->name);
1402 sof_dbg_comp_config(scomp, &host->config);
1404 swidget->private = host;
1406 ret = sof_ipc_tx_message(sdev->ipc, host->comp.hdr.cmd, host,
1407 sizeof(*host), r, sizeof(*r));
1418 int sof_load_pipeline_ipc(struct snd_sof_dev *sdev,
1419 struct sof_ipc_pipe_new *pipeline,
1420 struct sof_ipc_comp_reply *r)
1422 struct sof_ipc_pm_core_config pm_core_config;
1425 ret = sof_ipc_tx_message(sdev->ipc, pipeline->hdr.cmd, pipeline,
1426 sizeof(*pipeline), r, sizeof(*r));
1428 dev_err(sdev->dev, "error: load pipeline ipc failure\n");
1432 /* power up the core that this pipeline is scheduled on */
1433 ret = snd_sof_dsp_core_power_up(sdev, 1 << pipeline->core);
1435 dev_err(sdev->dev, "error: powering up pipeline schedule core %d\n",
1440 /* update enabled cores mask */
1441 sdev->enabled_cores_mask |= 1 << pipeline->core;
1444 * Now notify DSP that the core that this pipeline is scheduled on
1445 * has been powered up
1447 memset(&pm_core_config, 0, sizeof(pm_core_config));
1448 pm_core_config.enable_mask = sdev->enabled_cores_mask;
1450 /* configure CORE_ENABLE ipc message */
1451 pm_core_config.hdr.size = sizeof(pm_core_config);
1452 pm_core_config.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE;
1455 ret = sof_ipc_tx_message(sdev->ipc, pm_core_config.hdr.cmd,
1456 &pm_core_config, sizeof(pm_core_config),
1457 &pm_core_config, sizeof(pm_core_config));
1459 dev_err(sdev->dev, "error: core enable ipc failure\n");
1464 static int sof_widget_load_pipeline(struct snd_soc_component *scomp,
1465 int index, struct snd_sof_widget *swidget,
1466 struct snd_soc_tplg_dapm_widget *tw,
1467 struct sof_ipc_comp_reply *r)
1469 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1470 struct snd_soc_tplg_private *private = &tw->priv;
1471 struct sof_ipc_pipe_new *pipeline;
1472 struct snd_sof_widget *comp_swidget;
1475 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL);
1479 /* configure dai IPC message */
1480 pipeline->hdr.size = sizeof(*pipeline);
1481 pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW;
1482 pipeline->pipeline_id = index;
1483 pipeline->comp_id = swidget->comp_id;
1485 /* component at start of pipeline is our stream id */
1486 comp_swidget = snd_sof_find_swidget(sdev, tw->sname);
1487 if (!comp_swidget) {
1488 dev_err(sdev->dev, "error: widget %s refers to non existent widget %s\n",
1489 tw->name, tw->sname);
1494 pipeline->sched_id = comp_swidget->comp_id;
1496 dev_dbg(sdev->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n",
1497 pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id);
1499 ret = sof_parse_tokens(scomp, pipeline, sched_tokens,
1500 ARRAY_SIZE(sched_tokens), private->array,
1501 le32_to_cpu(private->size));
1503 dev_err(sdev->dev, "error: parse pipeline tokens failed %d\n",
1508 dev_dbg(sdev->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d\n",
1509 swidget->widget->name, pipeline->period, pipeline->priority,
1510 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched);
1512 swidget->private = pipeline;
1514 /* send ipc's to create pipeline comp and power up schedule core */
1515 ret = sof_load_pipeline_ipc(sdev, pipeline, r);
1527 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index,
1528 struct snd_sof_widget *swidget,
1529 struct snd_soc_tplg_dapm_widget *tw,
1530 struct sof_ipc_comp_reply *r)
1532 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1533 struct snd_soc_tplg_private *private = &tw->priv;
1534 struct sof_ipc_comp_mixer *mixer;
1537 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL);
1541 /* configure mixer IPC message */
1542 mixer->comp.hdr.size = sizeof(*mixer);
1543 mixer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1544 mixer->comp.id = swidget->comp_id;
1545 mixer->comp.type = SOF_COMP_MIXER;
1546 mixer->comp.pipeline_id = index;
1547 mixer->config.hdr.size = sizeof(mixer->config);
1549 ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens,
1550 ARRAY_SIZE(comp_tokens), private->array,
1551 le32_to_cpu(private->size));
1553 dev_err(sdev->dev, "error: parse mixer.cfg tokens failed %d\n",
1559 sof_dbg_comp_config(scomp, &mixer->config);
1561 swidget->private = mixer;
1563 ret = sof_ipc_tx_message(sdev->ipc, mixer->comp.hdr.cmd, mixer,
1564 sizeof(*mixer), r, sizeof(*r));
1574 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index,
1575 struct snd_sof_widget *swidget,
1576 struct snd_soc_tplg_dapm_widget *tw,
1577 struct sof_ipc_comp_reply *r)
1579 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1580 struct snd_soc_tplg_private *private = &tw->priv;
1581 struct sof_ipc_comp_mux *mux;
1584 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
1588 /* configure mux IPC message */
1589 mux->comp.hdr.size = sizeof(*mux);
1590 mux->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1591 mux->comp.id = swidget->comp_id;
1592 mux->comp.type = SOF_COMP_MUX;
1593 mux->comp.pipeline_id = index;
1594 mux->config.hdr.size = sizeof(mux->config);
1596 ret = sof_parse_tokens(scomp, &mux->config, comp_tokens,
1597 ARRAY_SIZE(comp_tokens), private->array,
1598 le32_to_cpu(private->size));
1600 dev_err(sdev->dev, "error: parse mux.cfg tokens failed %d\n",
1606 sof_dbg_comp_config(scomp, &mux->config);
1608 swidget->private = mux;
1610 ret = sof_ipc_tx_message(sdev->ipc, mux->comp.hdr.cmd, mux,
1611 sizeof(*mux), r, sizeof(*r));
1622 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index,
1623 struct snd_sof_widget *swidget,
1624 struct snd_soc_tplg_dapm_widget *tw,
1625 struct sof_ipc_comp_reply *r)
1627 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1628 struct snd_soc_tplg_private *private = &tw->priv;
1629 struct sof_ipc_comp_volume *volume;
1630 struct snd_sof_control *scontrol;
1635 volume = kzalloc(sizeof(*volume), GFP_KERNEL);
1639 if (!le32_to_cpu(tw->num_kcontrols)) {
1640 dev_err(sdev->dev, "error: invalid kcontrol count %d for volume\n",
1646 /* configure volume IPC message */
1647 volume->comp.hdr.size = sizeof(*volume);
1648 volume->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1649 volume->comp.id = swidget->comp_id;
1650 volume->comp.type = SOF_COMP_VOLUME;
1651 volume->comp.pipeline_id = index;
1652 volume->config.hdr.size = sizeof(volume->config);
1654 ret = sof_parse_tokens(scomp, volume, volume_tokens,
1655 ARRAY_SIZE(volume_tokens), private->array,
1656 le32_to_cpu(private->size));
1658 dev_err(sdev->dev, "error: parse volume tokens failed %d\n",
1662 ret = sof_parse_tokens(scomp, &volume->config, comp_tokens,
1663 ARRAY_SIZE(comp_tokens), private->array,
1664 le32_to_cpu(private->size));
1666 dev_err(sdev->dev, "error: parse volume.cfg tokens failed %d\n",
1667 le32_to_cpu(private->size));
1671 sof_dbg_comp_config(scomp, &volume->config);
1673 swidget->private = volume;
1675 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1676 if (scontrol->comp_id == swidget->comp_id &&
1677 scontrol->volume_table) {
1678 min_step = scontrol->min_volume_step;
1679 max_step = scontrol->max_volume_step;
1680 volume->min_value = scontrol->volume_table[min_step];
1681 volume->max_value = scontrol->volume_table[max_step];
1682 volume->channels = scontrol->num_channels;
1687 ret = sof_ipc_tx_message(sdev->ipc, volume->comp.hdr.cmd, volume,
1688 sizeof(*volume), r, sizeof(*r));
1700 static int sof_widget_load_src(struct snd_soc_component *scomp, int index,
1701 struct snd_sof_widget *swidget,
1702 struct snd_soc_tplg_dapm_widget *tw,
1703 struct sof_ipc_comp_reply *r)
1705 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1706 struct snd_soc_tplg_private *private = &tw->priv;
1707 struct sof_ipc_comp_src *src;
1710 src = kzalloc(sizeof(*src), GFP_KERNEL);
1714 /* configure src IPC message */
1715 src->comp.hdr.size = sizeof(*src);
1716 src->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1717 src->comp.id = swidget->comp_id;
1718 src->comp.type = SOF_COMP_SRC;
1719 src->comp.pipeline_id = index;
1720 src->config.hdr.size = sizeof(src->config);
1722 ret = sof_parse_tokens(scomp, src, src_tokens,
1723 ARRAY_SIZE(src_tokens), private->array,
1724 le32_to_cpu(private->size));
1726 dev_err(sdev->dev, "error: parse src tokens failed %d\n",
1731 ret = sof_parse_tokens(scomp, &src->config, comp_tokens,
1732 ARRAY_SIZE(comp_tokens), private->array,
1733 le32_to_cpu(private->size));
1735 dev_err(sdev->dev, "error: parse src.cfg tokens failed %d\n",
1736 le32_to_cpu(private->size));
1740 dev_dbg(sdev->dev, "src %s: source rate %d sink rate %d\n",
1741 swidget->widget->name, src->source_rate, src->sink_rate);
1742 sof_dbg_comp_config(scomp, &src->config);
1744 swidget->private = src;
1746 ret = sof_ipc_tx_message(sdev->ipc, src->comp.hdr.cmd, src,
1747 sizeof(*src), r, sizeof(*r));
1756 * Signal Generator Topology
1759 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index,
1760 struct snd_sof_widget *swidget,
1761 struct snd_soc_tplg_dapm_widget *tw,
1762 struct sof_ipc_comp_reply *r)
1764 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1765 struct snd_soc_tplg_private *private = &tw->priv;
1766 struct sof_ipc_comp_tone *tone;
1769 tone = kzalloc(sizeof(*tone), GFP_KERNEL);
1773 /* configure siggen IPC message */
1774 tone->comp.hdr.size = sizeof(*tone);
1775 tone->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1776 tone->comp.id = swidget->comp_id;
1777 tone->comp.type = SOF_COMP_TONE;
1778 tone->comp.pipeline_id = index;
1779 tone->config.hdr.size = sizeof(tone->config);
1781 ret = sof_parse_tokens(scomp, tone, tone_tokens,
1782 ARRAY_SIZE(tone_tokens), private->array,
1783 le32_to_cpu(private->size));
1785 dev_err(sdev->dev, "error: parse tone tokens failed %d\n",
1786 le32_to_cpu(private->size));
1790 ret = sof_parse_tokens(scomp, &tone->config, comp_tokens,
1791 ARRAY_SIZE(comp_tokens), private->array,
1792 le32_to_cpu(private->size));
1794 dev_err(sdev->dev, "error: parse tone.cfg tokens failed %d\n",
1795 le32_to_cpu(private->size));
1799 dev_dbg(sdev->dev, "tone %s: frequency %d amplitude %d\n",
1800 swidget->widget->name, tone->frequency, tone->amplitude);
1801 sof_dbg_comp_config(scomp, &tone->config);
1803 swidget->private = tone;
1805 ret = sof_ipc_tx_message(sdev->ipc, tone->comp.hdr.cmd, tone,
1806 sizeof(*tone), r, sizeof(*r));
1814 static int sof_get_control_data(struct snd_sof_dev *sdev,
1815 struct snd_soc_dapm_widget *widget,
1816 struct sof_widget_data *wdata,
1819 const struct snd_kcontrol_new *kc;
1820 struct soc_mixer_control *sm;
1821 struct soc_bytes_ext *sbe;
1822 struct soc_enum *se;
1827 for (i = 0; i < widget->num_kcontrols; i++) {
1828 kc = &widget->kcontrol_news[i];
1830 switch (widget->dobj.widget.kcontrol_type) {
1831 case SND_SOC_TPLG_TYPE_MIXER:
1832 sm = (struct soc_mixer_control *)kc->private_value;
1833 wdata[i].control = sm->dobj.private;
1835 case SND_SOC_TPLG_TYPE_BYTES:
1836 sbe = (struct soc_bytes_ext *)kc->private_value;
1837 wdata[i].control = sbe->dobj.private;
1839 case SND_SOC_TPLG_TYPE_ENUM:
1840 se = (struct soc_enum *)kc->private_value;
1841 wdata[i].control = se->dobj.private;
1844 dev_err(sdev->dev, "error: unknown kcontrol type %d in widget %s\n",
1845 widget->dobj.widget.kcontrol_type,
1850 if (!wdata[i].control) {
1851 dev_err(sdev->dev, "error: no scontrol for widget %s\n",
1856 wdata[i].pdata = wdata[i].control->control_data->data;
1857 if (!wdata[i].pdata)
1860 /* make sure data is valid - data can be updated at runtime */
1861 if (wdata[i].pdata->magic != SOF_ABI_MAGIC)
1864 *size += wdata[i].pdata->size;
1867 switch (wdata[i].control->cmd) {
1868 case SOF_CTRL_CMD_VOLUME:
1869 case SOF_CTRL_CMD_ENUM:
1870 case SOF_CTRL_CMD_SWITCH:
1871 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE;
1872 wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
1874 case SOF_CTRL_CMD_BINARY:
1875 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA;
1876 wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET;
1886 static int sof_process_load(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,
1892 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1893 struct snd_soc_dapm_widget *widget = swidget->widget;
1894 struct snd_soc_tplg_private *private = &tw->priv;
1895 struct sof_ipc_comp_process *process = NULL;
1896 struct sof_widget_data *wdata = NULL;
1897 size_t ipc_data_size = 0;
1903 if (type == SOF_COMP_NONE) {
1904 dev_err(sdev->dev, "error: invalid process comp type %d\n",
1909 /* allocate struct for widget control data sizes and types */
1910 if (widget->num_kcontrols) {
1911 wdata = kcalloc(widget->num_kcontrols,
1918 /* get possible component controls and get size of all pdata */
1919 ret = sof_get_control_data(sdev, widget, wdata,
1926 ipc_size = sizeof(struct sof_ipc_comp_process) +
1927 le32_to_cpu(private->size) +
1930 /* we are exceeding max ipc size, config needs to be sent separately */
1931 if (ipc_size > SOF_IPC_MSG_MAX_SIZE) {
1932 ipc_size -= ipc_data_size;
1936 process = kzalloc(ipc_size, GFP_KERNEL);
1942 /* configure iir IPC message */
1943 process->comp.hdr.size = ipc_size;
1944 process->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW;
1945 process->comp.id = swidget->comp_id;
1946 process->comp.type = type;
1947 process->comp.pipeline_id = index;
1948 process->config.hdr.size = sizeof(process->config);
1950 ret = sof_parse_tokens(scomp, &process->config, comp_tokens,
1951 ARRAY_SIZE(comp_tokens), private->array,
1952 le32_to_cpu(private->size));
1954 dev_err(sdev->dev, "error: parse process.cfg tokens failed %d\n",
1955 le32_to_cpu(private->size));
1959 sof_dbg_comp_config(scomp, &process->config);
1962 * found private data in control, so copy it.
1963 * get possible component controls - get size of all pdata,
1964 * then memcpy with headers
1966 if (ipc_data_size) {
1967 for (i = 0; i < widget->num_kcontrols; i++) {
1968 memcpy(&process->data + offset,
1969 wdata[i].pdata->data,
1970 wdata[i].pdata->size);
1971 offset += wdata[i].pdata->size;
1975 process->size = ipc_data_size;
1976 swidget->private = process;
1978 ret = sof_ipc_tx_message(sdev->ipc, process->comp.hdr.cmd, process,
1979 ipc_size, r, sizeof(*r));
1982 dev_err(sdev->dev, "error: create process failed\n");
1986 /* we sent the data in single message so return */
1990 /* send control data with large message supported method */
1991 for (i = 0; i < widget->num_kcontrols; i++) {
1992 wdata[i].control->readback_offset = 0;
1993 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, wdata[i].control,
1996 wdata[i].control->cmd,
1999 dev_err(sdev->dev, "error: send control failed\n");
2013 * Processing Component Topology - can be "effect", "codec", or general
2017 static int sof_widget_load_process(struct snd_soc_component *scomp, int index,
2018 struct snd_sof_widget *swidget,
2019 struct snd_soc_tplg_dapm_widget *tw,
2020 struct sof_ipc_comp_reply *r)
2022 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2023 struct snd_soc_tplg_private *private = &tw->priv;
2024 struct sof_ipc_comp_process config;
2027 /* check we have some tokens - we need at least process type */
2028 if (le32_to_cpu(private->size) == 0) {
2029 dev_err(sdev->dev, "error: process tokens not found\n");
2033 memset(&config, 0, sizeof(config));
2035 /* get the process token */
2036 ret = sof_parse_tokens(scomp, &config, process_tokens,
2037 ARRAY_SIZE(process_tokens), private->array,
2038 le32_to_cpu(private->size));
2040 dev_err(sdev->dev, "error: parse process tokens failed %d\n",
2041 le32_to_cpu(private->size));
2045 /* now load process specific data and send IPC */
2046 ret = sof_process_load(scomp, index, swidget, tw, r,
2047 find_process_comp_type(config.type));
2049 dev_err(sdev->dev, "error: process loading failed\n");
2056 static int sof_widget_bind_event(struct snd_sof_dev *sdev,
2057 struct snd_sof_widget *swidget,
2060 struct sof_ipc_comp *ipc_comp;
2062 /* validate widget event type */
2063 switch (event_type) {
2064 case SOF_KEYWORD_DETECT_DAPM_EVENT:
2065 /* only KEYWORD_DETECT comps should handle this */
2066 if (swidget->id != snd_soc_dapm_effect)
2069 ipc_comp = swidget->private;
2070 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT)
2073 /* bind event to keyword detect comp */
2074 return snd_soc_tplg_widget_bind_event(swidget->widget,
2076 ARRAY_SIZE(sof_kwd_events),
2083 "error: invalid event type %d for widget %s\n",
2084 event_type, swidget->widget->name);
2088 /* external widget init - used for any driver specific init */
2089 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
2090 struct snd_soc_dapm_widget *w,
2091 struct snd_soc_tplg_dapm_widget *tw)
2093 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2094 struct snd_sof_widget *swidget;
2095 struct snd_sof_dai *dai;
2096 struct sof_ipc_comp_reply reply;
2097 struct snd_sof_control *scontrol;
2100 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2104 swidget->sdev = sdev;
2105 swidget->widget = w;
2106 swidget->comp_id = sdev->next_comp_id++;
2107 swidget->complete = 0;
2108 swidget->id = w->id;
2109 swidget->pipeline_id = index;
2110 swidget->private = NULL;
2111 memset(&reply, 0, sizeof(reply));
2113 dev_dbg(sdev->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
2114 swidget->comp_id, index, swidget->id, tw->name,
2115 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2116 ? tw->sname : "none");
2118 /* handle any special case widgets */
2120 case snd_soc_dapm_dai_in:
2121 case snd_soc_dapm_dai_out:
2122 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
2128 ret = sof_widget_load_dai(scomp, index, swidget, tw, &reply,
2131 sof_connect_dai_widget(scomp, w, tw, dai);
2132 list_add(&dai->list, &sdev->dai_list);
2133 swidget->private = dai;
2138 case snd_soc_dapm_mixer:
2139 ret = sof_widget_load_mixer(scomp, index, swidget, tw, &reply);
2141 case snd_soc_dapm_pga:
2142 ret = sof_widget_load_pga(scomp, index, swidget, tw, &reply);
2143 /* Find scontrol for this pga and set readback offset*/
2144 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2145 if (scontrol->comp_id == swidget->comp_id) {
2146 scontrol->readback_offset = reply.offset;
2151 case snd_soc_dapm_buffer:
2152 ret = sof_widget_load_buffer(scomp, index, swidget, tw, &reply);
2154 case snd_soc_dapm_scheduler:
2155 ret = sof_widget_load_pipeline(scomp, index, swidget, tw,
2158 case snd_soc_dapm_aif_out:
2159 ret = sof_widget_load_pcm(scomp, index, swidget,
2160 SOF_IPC_STREAM_CAPTURE, tw, &reply);
2162 case snd_soc_dapm_aif_in:
2163 ret = sof_widget_load_pcm(scomp, index, swidget,
2164 SOF_IPC_STREAM_PLAYBACK, tw, &reply);
2166 case snd_soc_dapm_src:
2167 ret = sof_widget_load_src(scomp, index, swidget, tw, &reply);
2169 case snd_soc_dapm_siggen:
2170 ret = sof_widget_load_siggen(scomp, index, swidget, tw, &reply);
2172 case snd_soc_dapm_effect:
2173 ret = sof_widget_load_process(scomp, index, swidget, tw,
2176 case snd_soc_dapm_mux:
2177 case snd_soc_dapm_demux:
2178 ret = sof_widget_load_mux(scomp, index, swidget, tw, &reply);
2180 case snd_soc_dapm_switch:
2181 case snd_soc_dapm_dai_link:
2182 case snd_soc_dapm_kcontrol:
2184 dev_warn(sdev->dev, "warning: widget type %d name %s not handled\n",
2185 swidget->id, tw->name);
2189 /* check IPC reply */
2190 if (ret < 0 || reply.rhdr.error < 0) {
2192 "error: DSP failed to add widget id %d type %d name : %s stream %s reply %d\n",
2193 tw->shift, swidget->id, tw->name,
2194 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
2195 ? tw->sname : "none", reply.rhdr.error);
2200 /* bind widget to external event */
2201 if (tw->event_type) {
2202 ret = sof_widget_bind_event(sdev, swidget,
2203 le16_to_cpu(tw->event_type));
2205 dev_err(sdev->dev, "error: widget event binding failed\n");
2206 kfree(swidget->private);
2212 w->dobj.private = swidget;
2213 list_add(&swidget->list, &sdev->widget_list);
2217 static int sof_route_unload(struct snd_soc_component *scomp,
2218 struct snd_soc_dobj *dobj)
2220 struct snd_sof_route *sroute;
2222 sroute = dobj->private;
2226 /* free sroute and its private data */
2227 kfree(sroute->private);
2228 list_del(&sroute->list);
2234 static int sof_widget_unload(struct snd_soc_component *scomp,
2235 struct snd_soc_dobj *dobj)
2237 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2238 const struct snd_kcontrol_new *kc;
2239 struct snd_soc_dapm_widget *widget;
2240 struct sof_ipc_pipe_new *pipeline;
2241 struct snd_sof_control *scontrol;
2242 struct snd_sof_widget *swidget;
2243 struct soc_mixer_control *sm;
2244 struct soc_bytes_ext *sbe;
2245 struct snd_sof_dai *dai;
2246 struct soc_enum *se;
2250 swidget = dobj->private;
2254 widget = swidget->widget;
2256 switch (swidget->id) {
2257 case snd_soc_dapm_dai_in:
2258 case snd_soc_dapm_dai_out:
2259 dai = swidget->private;
2262 /* free dai config */
2263 kfree(dai->dai_config);
2264 list_del(&dai->list);
2267 case snd_soc_dapm_scheduler:
2269 /* power down the pipeline schedule core */
2270 pipeline = swidget->private;
2271 ret = snd_sof_dsp_core_power_down(sdev, 1 << pipeline->core);
2273 dev_err(sdev->dev, "error: powering down pipeline schedule core %d\n",
2276 /* update enabled cores mask */
2277 sdev->enabled_cores_mask &= ~(1 << pipeline->core);
2283 for (i = 0; i < widget->num_kcontrols; i++) {
2284 kc = &widget->kcontrol_news[i];
2285 switch (dobj->widget.kcontrol_type) {
2286 case SND_SOC_TPLG_TYPE_MIXER:
2287 sm = (struct soc_mixer_control *)kc->private_value;
2288 scontrol = sm->dobj.private;
2290 kfree(scontrol->volume_table);
2292 case SND_SOC_TPLG_TYPE_ENUM:
2293 se = (struct soc_enum *)kc->private_value;
2294 scontrol = se->dobj.private;
2296 case SND_SOC_TPLG_TYPE_BYTES:
2297 sbe = (struct soc_bytes_ext *)kc->private_value;
2298 scontrol = sbe->dobj.private;
2301 dev_warn(sdev->dev, "unsupported kcontrol_type\n");
2304 kfree(scontrol->control_data);
2305 list_del(&scontrol->list);
2310 /* free private value */
2311 kfree(swidget->private);
2313 /* remove and free swidget object */
2314 list_del(&swidget->list);
2321 * DAI HW configuration.
2324 /* FE DAI - used for any driver specific init */
2325 static int sof_dai_load(struct snd_soc_component *scomp, int index,
2326 struct snd_soc_dai_driver *dai_drv,
2327 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
2329 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2330 struct snd_soc_tplg_stream_caps *caps;
2331 struct snd_soc_tplg_private *private = &pcm->priv;
2332 struct snd_sof_pcm *spcm;
2333 int stream = SNDRV_PCM_STREAM_PLAYBACK;
2336 /* nothing to do for BEs atm */
2340 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
2345 spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].comp_id = COMP_ID_UNASSIGNED;
2346 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].comp_id = COMP_ID_UNASSIGNED;
2349 dev_dbg(sdev->dev, "tplg: load pcm %s\n", pcm->dai_name);
2351 dai_drv->dobj.private = spcm;
2352 list_add(&spcm->list, &sdev->pcm_list);
2354 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
2355 ARRAY_SIZE(stream_tokens), private->array,
2356 le32_to_cpu(private->size));
2358 dev_err(sdev->dev, "error: parse stream tokens failed %d\n",
2359 le32_to_cpu(private->size));
2363 /* do we need to allocate playback PCM DMA pages */
2364 if (!spcm->pcm.playback)
2367 dev_vdbg(sdev->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
2368 spcm->pcm.pcm_name, spcm->stream[0].d0i3_compatible);
2370 caps = &spcm->pcm.caps[stream];
2372 /* allocate playback page table buffer */
2373 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2374 PAGE_SIZE, &spcm->stream[stream].page_table);
2376 dev_err(sdev->dev, "error: can't alloc page table for %s %d\n",
2382 /* bind pcm to host comp */
2383 ret = spcm_bind(sdev, spcm, stream);
2386 "error: can't bind pcm to host\n");
2387 goto free_playback_tables;
2391 stream = SNDRV_PCM_STREAM_CAPTURE;
2393 /* do we need to allocate capture PCM DMA pages */
2394 if (!spcm->pcm.capture)
2397 dev_vdbg(sdev->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
2398 spcm->pcm.pcm_name, spcm->stream[1].d0i3_compatible);
2400 caps = &spcm->pcm.caps[stream];
2402 /* allocate capture page table buffer */
2403 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
2404 PAGE_SIZE, &spcm->stream[stream].page_table);
2406 dev_err(sdev->dev, "error: can't alloc page table for %s %d\n",
2408 goto free_playback_tables;
2411 /* bind pcm to host comp */
2412 ret = spcm_bind(sdev, spcm, stream);
2415 "error: can't bind pcm to host\n");
2416 snd_dma_free_pages(&spcm->stream[stream].page_table);
2417 goto free_playback_tables;
2422 free_playback_tables:
2423 if (spcm->pcm.playback)
2424 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2429 static int sof_dai_unload(struct snd_soc_component *scomp,
2430 struct snd_soc_dobj *dobj)
2432 struct snd_sof_pcm *spcm = dobj->private;
2434 /* free PCM DMA pages */
2435 if (spcm->pcm.playback)
2436 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
2438 if (spcm->pcm.capture)
2439 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
2441 /* remove from list and free spcm */
2442 list_del(&spcm->list);
2448 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config,
2449 struct sof_ipc_dai_config *config)
2451 /* clock directions wrt codec */
2452 if (hw_config->bclk_master == SND_SOC_TPLG_BCLK_CM) {
2453 /* codec is bclk master */
2454 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2455 config->format |= SOF_DAI_FMT_CBM_CFM;
2457 config->format |= SOF_DAI_FMT_CBM_CFS;
2459 /* codec is bclk slave */
2460 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM)
2461 config->format |= SOF_DAI_FMT_CBS_CFM;
2463 config->format |= SOF_DAI_FMT_CBS_CFS;
2466 /* inverted clocks ? */
2467 if (hw_config->invert_bclk) {
2468 if (hw_config->invert_fsync)
2469 config->format |= SOF_DAI_FMT_IB_IF;
2471 config->format |= SOF_DAI_FMT_IB_NF;
2473 if (hw_config->invert_fsync)
2474 config->format |= SOF_DAI_FMT_NB_IF;
2476 config->format |= SOF_DAI_FMT_NB_NF;
2480 /* set config for all DAI's with name matching the link name */
2481 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size,
2482 struct snd_soc_dai_link *link,
2483 struct sof_ipc_dai_config *config)
2485 struct snd_sof_dai *dai;
2488 list_for_each_entry(dai, &sdev->dai_list, list) {
2492 if (strcmp(link->name, dai->name) == 0) {
2493 dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2494 if (!dai->dai_config)
2497 /* set cpu_dai_name */
2498 dai->cpu_dai_name = link->cpus->dai_name;
2505 * machine driver may define a dai link with playback and capture
2506 * dai enabled, but the dai link in topology would support both, one
2507 * or none of them. Here print a warning message to notify user
2510 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2517 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index,
2518 struct snd_soc_dai_link *link,
2519 struct snd_soc_tplg_link_config *cfg,
2520 struct snd_soc_tplg_hw_config *hw_config,
2521 struct sof_ipc_dai_config *config)
2523 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2524 struct snd_soc_tplg_private *private = &cfg->priv;
2525 struct sof_ipc_reply reply;
2526 u32 size = sizeof(*config);
2529 /* handle master/slave and inverted clocks */
2530 sof_dai_set_format(hw_config, config);
2533 memset(&config->ssp, 0, sizeof(struct sof_ipc_dai_ssp_params));
2534 config->hdr.size = size;
2536 ret = sof_parse_tokens(scomp, &config->ssp, ssp_tokens,
2537 ARRAY_SIZE(ssp_tokens), private->array,
2538 le32_to_cpu(private->size));
2540 dev_err(sdev->dev, "error: parse ssp tokens failed %d\n",
2541 le32_to_cpu(private->size));
2545 config->ssp.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2546 config->ssp.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2547 config->ssp.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2548 config->ssp.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2549 config->ssp.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2550 config->ssp.mclk_direction = hw_config->mclk_direction;
2551 config->ssp.rx_slots = le32_to_cpu(hw_config->rx_slots);
2552 config->ssp.tx_slots = le32_to_cpu(hw_config->tx_slots);
2554 dev_dbg(sdev->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",
2555 config->dai_index, config->format,
2556 config->ssp.mclk_rate, config->ssp.bclk_rate,
2557 config->ssp.fsync_rate, config->ssp.sample_valid_bits,
2558 config->ssp.tdm_slot_width, config->ssp.tdm_slots,
2559 config->ssp.mclk_id, config->ssp.quirks);
2561 /* validate SSP fsync rate and channel count */
2562 if (config->ssp.fsync_rate < 8000 || config->ssp.fsync_rate > 192000) {
2563 dev_err(sdev->dev, "error: invalid fsync rate for SSP%d\n",
2568 if (config->ssp.tdm_slots < 1 || config->ssp.tdm_slots > 8) {
2569 dev_err(sdev->dev, "error: invalid channel count for SSP%d\n",
2574 /* send message to DSP */
2575 ret = sof_ipc_tx_message(sdev->ipc,
2576 config->hdr.cmd, config, size, &reply,
2580 dev_err(sdev->dev, "error: failed to set DAI config for SSP%d\n",
2585 /* set config for all DAI's with name matching the link name */
2586 ret = sof_set_dai_config(sdev, size, link, config);
2588 dev_err(sdev->dev, "error: failed to save DAI config for SSP%d\n",
2594 static int sof_link_sai_load(struct snd_soc_component *scomp, int index,
2595 struct snd_soc_dai_link *link,
2596 struct snd_soc_tplg_link_config *cfg,
2597 struct snd_soc_tplg_hw_config *hw_config,
2598 struct sof_ipc_dai_config *config)
2600 /*TODO: Add implementation */
2604 static int sof_link_esai_load(struct snd_soc_component *scomp, int index,
2605 struct snd_soc_dai_link *link,
2606 struct snd_soc_tplg_link_config *cfg,
2607 struct snd_soc_tplg_hw_config *hw_config,
2608 struct sof_ipc_dai_config *config)
2610 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2611 struct snd_soc_tplg_private *private = &cfg->priv;
2612 struct sof_ipc_reply reply;
2613 u32 size = sizeof(*config);
2616 /* handle master/slave and inverted clocks */
2617 sof_dai_set_format(hw_config, config);
2620 memset(&config->esai, 0, sizeof(struct sof_ipc_dai_esai_params));
2621 config->hdr.size = size;
2623 ret = sof_parse_tokens(scomp, &config->esai, esai_tokens,
2624 ARRAY_SIZE(esai_tokens), private->array,
2625 le32_to_cpu(private->size));
2627 dev_err(sdev->dev, "error: parse esai tokens failed %d\n",
2628 le32_to_cpu(private->size));
2632 config->esai.mclk_rate = le32_to_cpu(hw_config->mclk_rate);
2633 config->esai.bclk_rate = le32_to_cpu(hw_config->bclk_rate);
2634 config->esai.fsync_rate = le32_to_cpu(hw_config->fsync_rate);
2635 config->esai.mclk_direction = hw_config->mclk_direction;
2636 config->esai.tdm_slots = le32_to_cpu(hw_config->tdm_slots);
2637 config->esai.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width);
2638 config->esai.rx_slots = le32_to_cpu(hw_config->rx_slots);
2639 config->esai.tx_slots = le32_to_cpu(hw_config->tx_slots);
2642 "tplg: config ESAI%d fmt 0x%x mclk %d width %d slots %d mclk id %d\n",
2643 config->dai_index, config->format,
2644 config->esai.mclk_rate, config->esai.tdm_slot_width,
2645 config->esai.tdm_slots, config->esai.mclk_id);
2647 if (config->esai.tdm_slots < 1 || config->esai.tdm_slots > 8) {
2648 dev_err(sdev->dev, "error: invalid channel count for ESAI%d\n",
2653 /* send message to DSP */
2654 ret = sof_ipc_tx_message(sdev->ipc,
2655 config->hdr.cmd, config, size, &reply,
2658 dev_err(sdev->dev, "error: failed to set DAI config for ESAI%d\n",
2663 /* set config for all DAI's with name matching the link name */
2664 ret = sof_set_dai_config(sdev, size, link, config);
2666 dev_err(sdev->dev, "error: failed to save DAI config for ESAI%d\n",
2672 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index,
2673 struct snd_soc_dai_link *link,
2674 struct snd_soc_tplg_link_config *cfg,
2675 struct snd_soc_tplg_hw_config *hw_config,
2676 struct sof_ipc_dai_config *config)
2678 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2679 struct snd_soc_tplg_private *private = &cfg->priv;
2680 struct sof_ipc_dai_config *ipc_config;
2681 struct sof_ipc_reply reply;
2682 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
2683 struct sof_ipc_fw_version *v = &ready->version;
2688 * config is only used for the common params in dmic_params structure
2689 * that does not include the PDM controller config array
2690 * Set the common params to 0.
2692 memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params));
2694 /* get DMIC tokens */
2695 ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens,
2696 ARRAY_SIZE(dmic_tokens), private->array,
2697 le32_to_cpu(private->size));
2699 dev_err(sdev->dev, "error: parse dmic tokens failed %d\n",
2700 le32_to_cpu(private->size));
2705 * allocate memory for dmic dai config accounting for the
2706 * variable number of active pdm controllers
2707 * This will be the ipc payload for setting dai config
2709 size = sizeof(*config) + sizeof(struct sof_ipc_dai_dmic_pdm_ctrl) *
2710 config->dmic.num_pdm_active;
2712 ipc_config = kzalloc(size, GFP_KERNEL);
2716 /* copy the common dai config and dmic params */
2717 memcpy(ipc_config, config, sizeof(*config));
2720 * alloc memory for private member
2721 * Used to track the pdm config array index currently being parsed
2723 sdev->private = kzalloc(sizeof(u32), GFP_KERNEL);
2724 if (!sdev->private) {
2729 /* get DMIC PDM tokens */
2730 ret = sof_parse_tokens(scomp, &ipc_config->dmic.pdm[0], dmic_pdm_tokens,
2731 ARRAY_SIZE(dmic_pdm_tokens), private->array,
2732 le32_to_cpu(private->size));
2734 dev_err(sdev->dev, "error: parse dmic pdm tokens failed %d\n",
2735 le32_to_cpu(private->size));
2739 /* set IPC header size */
2740 ipc_config->hdr.size = size;
2742 /* debug messages */
2743 dev_dbg(sdev->dev, "tplg: config DMIC%d driver version %d\n",
2744 ipc_config->dai_index, ipc_config->dmic.driver_ipc_version);
2745 dev_dbg(sdev->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n",
2746 ipc_config->dmic.pdmclk_min, ipc_config->dmic.pdmclk_max,
2747 ipc_config->dmic.duty_min);
2748 dev_dbg(sdev->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n",
2749 ipc_config->dmic.duty_max, ipc_config->dmic.fifo_fs,
2750 ipc_config->dmic.num_pdm_active);
2751 dev_dbg(sdev->dev, "fifo word length %hd\n",
2752 ipc_config->dmic.fifo_bits);
2754 for (j = 0; j < ipc_config->dmic.num_pdm_active; j++) {
2755 dev_dbg(sdev->dev, "pdm %hd mic a %hd mic b %hd\n",
2756 ipc_config->dmic.pdm[j].id,
2757 ipc_config->dmic.pdm[j].enable_mic_a,
2758 ipc_config->dmic.pdm[j].enable_mic_b);
2759 dev_dbg(sdev->dev, "pdm %hd polarity a %hd polarity b %hd\n",
2760 ipc_config->dmic.pdm[j].id,
2761 ipc_config->dmic.pdm[j].polarity_mic_a,
2762 ipc_config->dmic.pdm[j].polarity_mic_b);
2763 dev_dbg(sdev->dev, "pdm %hd clk_edge %hd skew %hd\n",
2764 ipc_config->dmic.pdm[j].id,
2765 ipc_config->dmic.pdm[j].clk_edge,
2766 ipc_config->dmic.pdm[j].skew);
2769 if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1)) {
2770 /* this takes care of backwards compatible handling of fifo_bits_b */
2771 ipc_config->dmic.reserved_2 = ipc_config->dmic.fifo_bits;
2774 /* send message to DSP */
2775 ret = sof_ipc_tx_message(sdev->ipc,
2776 ipc_config->hdr.cmd, ipc_config, size, &reply,
2781 "error: failed to set DAI config for DMIC%d\n",
2786 /* set config for all DAI's with name matching the link name */
2787 ret = sof_set_dai_config(sdev, size, link, ipc_config);
2789 dev_err(sdev->dev, "error: failed to save DAI config for DMIC%d\n",
2793 kfree(sdev->private);
2800 * for hda link, playback and capture are supported by different dai
2801 * in FW. Here get the dai_index, set dma channel of each dai
2802 * and send config to FW. In FW, each dai sets config by dai_index
2804 static int sof_link_hda_process(struct snd_sof_dev *sdev,
2805 struct snd_soc_dai_link *link,
2806 struct sof_ipc_dai_config *config)
2808 struct sof_ipc_reply reply;
2809 u32 size = sizeof(*config);
2810 struct snd_sof_dai *sof_dai;
2814 list_for_each_entry(sof_dai, &sdev->dai_list, list) {
2818 if (strcmp(link->name, sof_dai->name) == 0) {
2819 config->dai_index = sof_dai->comp_dai.dai_index;
2822 config->hda.link_dma_ch = DMA_CHAN_INVALID;
2824 /* save config in dai component */
2825 sof_dai->dai_config = kmemdup(config, size, GFP_KERNEL);
2826 if (!sof_dai->dai_config)
2829 sof_dai->cpu_dai_name = link->cpus->dai_name;
2831 /* send message to DSP */
2832 ret = sof_ipc_tx_message(sdev->ipc,
2833 config->hdr.cmd, config, size,
2834 &reply, sizeof(reply));
2837 dev_err(sdev->dev, "error: failed to set DAI config for direction:%d of HDA dai %d\n",
2838 sof_dai->comp_dai.direction,
2847 * machine driver may define a dai link with playback and capture
2848 * dai enabled, but the dai link in topology would support both, one
2849 * or none of them. Here print a warning message to notify user
2852 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s",
2859 static int sof_link_hda_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 struct snd_soc_dai *dai;
2868 u32 size = sizeof(*config);
2872 memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params));
2873 config->hdr.size = size;
2875 /* get any bespoke DAI tokens */
2876 ret = sof_parse_tokens(scomp, config, hda_tokens,
2877 ARRAY_SIZE(hda_tokens), private->array,
2878 le32_to_cpu(private->size));
2880 dev_err(sdev->dev, "error: parse hda tokens failed %d\n",
2881 le32_to_cpu(private->size));
2885 dai = snd_soc_find_dai(link->cpus);
2887 dev_err(sdev->dev, "error: failed to find dai %s in %s",
2888 link->cpus->dai_name, __func__);
2892 ret = sof_link_hda_process(sdev, link, config);
2894 dev_err(sdev->dev, "error: failed to process hda dai link %s",
2900 static int sof_link_alh_load(struct snd_soc_component *scomp, int index,
2901 struct snd_soc_dai_link *link,
2902 struct snd_soc_tplg_link_config *cfg,
2903 struct snd_soc_tplg_hw_config *hw_config,
2904 struct sof_ipc_dai_config *config)
2906 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2907 struct sof_ipc_reply reply;
2908 u32 size = sizeof(*config);
2912 config->hdr.size = size;
2914 /* send message to DSP */
2915 ret = sof_ipc_tx_message(sdev->ipc,
2916 config->hdr.cmd, config, size, &reply,
2920 dev_err(sdev->dev, "error: failed to set DAI config for ALH %d\n",
2925 /* set config for all DAI's with name matching the link name */
2926 ret = sof_set_dai_config(sdev, size, link, config);
2928 dev_err(sdev->dev, "error: failed to save DAI config for ALH %d\n",
2934 /* DAI link - used for any driver specific init */
2935 static int sof_link_load(struct snd_soc_component *scomp, int index,
2936 struct snd_soc_dai_link *link,
2937 struct snd_soc_tplg_link_config *cfg)
2939 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2940 struct snd_soc_tplg_private *private = &cfg->priv;
2941 struct sof_ipc_dai_config config;
2942 struct snd_soc_tplg_hw_config *hw_config;
2947 if (!link->platforms) {
2948 dev_err(sdev->dev, "error: no platforms\n");
2951 link->platforms->name = dev_name(sdev->dev);
2954 * Set nonatomic property for FE dai links as their trigger action
2957 if (!link->no_pcm) {
2958 link->nonatomic = true;
2960 /* set trigger order */
2961 link->trigger[0] = SND_SOC_DPCM_TRIGGER_POST;
2962 link->trigger[1] = SND_SOC_DPCM_TRIGGER_POST;
2964 /* nothing more to do for FE dai links */
2968 /* check we have some tokens - we need at least DAI type */
2969 if (le32_to_cpu(private->size) == 0) {
2970 dev_err(sdev->dev, "error: expected tokens for DAI, none found\n");
2974 /* Send BE DAI link configurations to DSP */
2975 memset(&config, 0, sizeof(config));
2977 /* get any common DAI tokens */
2978 ret = sof_parse_tokens(scomp, &config, dai_link_tokens,
2979 ARRAY_SIZE(dai_link_tokens), private->array,
2980 le32_to_cpu(private->size));
2982 dev_err(sdev->dev, "error: parse link tokens failed %d\n",
2983 le32_to_cpu(private->size));
2988 * DAI links are expected to have at least 1 hw_config.
2989 * But some older topologies might have no hw_config for HDA dai links.
2991 num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
2992 if (!num_hw_configs) {
2993 if (config.type != SOF_DAI_INTEL_HDA) {
2994 dev_err(sdev->dev, "error: unexpected DAI config count %d!\n",
2995 le32_to_cpu(cfg->num_hw_configs));
2999 dev_dbg(sdev->dev, "tplg: %d hw_configs found, default id: %d!\n",
3000 cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id));
3002 for (i = 0; i < num_hw_configs; i++) {
3003 if (cfg->hw_config[i].id == cfg->default_hw_config_id)
3007 if (i == num_hw_configs) {
3008 dev_err(sdev->dev, "error: default hw_config id: %d not found!\n",
3009 le32_to_cpu(cfg->default_hw_config_id));
3014 /* configure dai IPC message */
3015 hw_config = &cfg->hw_config[i];
3017 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG;
3018 config.format = le32_to_cpu(hw_config->fmt);
3020 /* now load DAI specific data and send IPC - type comes from token */
3021 switch (config.type) {
3022 case SOF_DAI_INTEL_SSP:
3023 ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config,
3026 case SOF_DAI_INTEL_DMIC:
3027 ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config,
3030 case SOF_DAI_INTEL_HDA:
3031 ret = sof_link_hda_load(scomp, index, link, cfg, hw_config,
3034 case SOF_DAI_INTEL_ALH:
3035 ret = sof_link_alh_load(scomp, index, link, cfg, hw_config,
3038 case SOF_DAI_IMX_SAI:
3039 ret = sof_link_sai_load(scomp, index, link, cfg, hw_config,
3042 case SOF_DAI_IMX_ESAI:
3043 ret = sof_link_esai_load(scomp, index, link, cfg, hw_config,
3047 dev_err(sdev->dev, "error: invalid DAI type %d\n", config.type);
3057 static int sof_link_hda_unload(struct snd_sof_dev *sdev,
3058 struct snd_soc_dai_link *link)
3060 struct snd_soc_dai *dai;
3063 dai = snd_soc_find_dai(link->cpus);
3065 dev_err(sdev->dev, "error: failed to find dai %s in %s",
3066 link->cpus->dai_name, __func__);
3073 static int sof_link_unload(struct snd_soc_component *scomp,
3074 struct snd_soc_dobj *dobj)
3076 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3077 struct snd_soc_dai_link *link =
3078 container_of(dobj, struct snd_soc_dai_link, dobj);
3080 struct snd_sof_dai *sof_dai;
3083 /* only BE link is loaded by sof */
3087 list_for_each_entry(sof_dai, &sdev->dai_list, list) {
3091 if (strcmp(link->name, sof_dai->name) == 0)
3095 dev_err(sdev->dev, "error: failed to find dai %s in %s",
3096 link->name, __func__);
3100 switch (sof_dai->dai_config->type) {
3101 case SOF_DAI_INTEL_SSP:
3102 case SOF_DAI_INTEL_DMIC:
3103 case SOF_DAI_INTEL_ALH:
3104 /* no resource needs to be released for SSP, DMIC and ALH */
3106 case SOF_DAI_INTEL_HDA:
3107 ret = sof_link_hda_unload(sdev, link);
3110 dev_err(sdev->dev, "error: invalid DAI type %d\n",
3111 sof_dai->dai_config->type);
3119 /* DAI link - used for any driver specific init */
3120 static int sof_route_load(struct snd_soc_component *scomp, int index,
3121 struct snd_soc_dapm_route *route)
3123 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3124 struct sof_ipc_pipe_comp_connect *connect;
3125 struct snd_sof_widget *source_swidget, *sink_swidget;
3126 struct snd_soc_dobj *dobj = &route->dobj;
3127 struct snd_sof_route *sroute;
3128 struct sof_ipc_reply reply;
3131 /* allocate memory for sroute and connect */
3132 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
3136 sroute->sdev = sdev;
3138 connect = kzalloc(sizeof(*connect), GFP_KERNEL);
3144 connect->hdr.size = sizeof(*connect);
3145 connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT;
3147 dev_dbg(sdev->dev, "sink %s control %s source %s\n",
3148 route->sink, route->control ? route->control : "none",
3151 /* source component */
3152 source_swidget = snd_sof_find_swidget(sdev, (char *)route->source);
3153 if (!source_swidget) {
3154 dev_err(sdev->dev, "error: source %s not found\n",
3161 * Virtual widgets of type output/out_drv may be added in topology
3162 * for compatibility. These are not handled by the FW.
3163 * So, don't send routes whose source/sink widget is of such types
3166 if (source_swidget->id == snd_soc_dapm_out_drv ||
3167 source_swidget->id == snd_soc_dapm_output)
3170 connect->source_id = source_swidget->comp_id;
3172 /* sink component */
3173 sink_swidget = snd_sof_find_swidget(sdev, (char *)route->sink);
3174 if (!sink_swidget) {
3175 dev_err(sdev->dev, "error: sink %s not found\n",
3182 * Don't send routes whose sink widget is of type
3183 * output or out_drv to the DSP
3185 if (sink_swidget->id == snd_soc_dapm_out_drv ||
3186 sink_swidget->id == snd_soc_dapm_output)
3189 connect->sink_id = sink_swidget->comp_id;
3192 * For virtual routes, both sink and source are not
3193 * buffer. Since only buffer linked to component is supported by
3194 * FW, others are reported as error, add check in route function,
3195 * do not send it to FW when both source and sink are not buffer
3197 if (source_swidget->id != snd_soc_dapm_buffer &&
3198 sink_swidget->id != snd_soc_dapm_buffer) {
3199 dev_dbg(sdev->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n",
3200 route->source, route->sink);
3204 ret = sof_ipc_tx_message(sdev->ipc,
3206 connect, sizeof(*connect),
3207 &reply, sizeof(reply));
3209 /* check IPC return value */
3211 dev_err(sdev->dev, "error: failed to add route sink %s control %s source %s\n",
3213 route->control ? route->control : "none",
3218 /* check IPC reply */
3219 if (reply.error < 0) {
3220 dev_err(sdev->dev, "error: DSP failed to add route sink %s control %s source %s result %d\n",
3222 route->control ? route->control : "none",
3223 route->source, reply.error);
3228 sroute->route = route;
3229 dobj->private = sroute;
3230 sroute->private = connect;
3232 /* add route to route list */
3233 list_add(&sroute->list, &sdev->route_list);
3244 /* Function to set the initial value of SOF kcontrols.
3245 * The value will be stored in scontrol->control_data
3247 static int snd_sof_cache_kcontrol_val(struct snd_sof_dev *sdev)
3249 struct snd_sof_control *scontrol = NULL;
3250 int ipc_cmd, ctrl_type;
3253 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
3255 /* notify DSP of kcontrol values */
3256 switch (scontrol->cmd) {
3257 case SOF_CTRL_CMD_VOLUME:
3258 case SOF_CTRL_CMD_ENUM:
3259 case SOF_CTRL_CMD_SWITCH:
3260 ipc_cmd = SOF_IPC_COMP_GET_VALUE;
3261 ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_GET;
3263 case SOF_CTRL_CMD_BINARY:
3264 ipc_cmd = SOF_IPC_COMP_GET_DATA;
3265 ctrl_type = SOF_CTRL_TYPE_DATA_GET;
3269 "error: Invalid scontrol->cmd: %d\n",
3273 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
3279 "error: kcontrol value get for widget: %d\n",
3287 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
3288 struct snd_sof_widget *swidget)
3290 struct sof_ipc_pipe_ready ready;
3291 struct sof_ipc_reply reply;
3294 dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n",
3295 swidget->widget->name, swidget->comp_id);
3297 memset(&ready, 0, sizeof(ready));
3298 ready.hdr.size = sizeof(ready);
3299 ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE;
3300 ready.comp_id = swidget->comp_id;
3302 ret = sof_ipc_tx_message(sdev->ipc,
3303 ready.hdr.cmd, &ready, sizeof(ready), &reply,
3310 /* completion - called at completion of firmware loading */
3311 static void sof_complete(struct snd_soc_component *scomp)
3313 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3314 struct snd_sof_widget *swidget;
3316 /* some widget types require completion notificattion */
3317 list_for_each_entry(swidget, &sdev->widget_list, list) {
3318 if (swidget->complete)
3321 switch (swidget->id) {
3322 case snd_soc_dapm_scheduler:
3324 snd_sof_complete_pipeline(sdev, swidget);
3331 * cache initial values of SOF kcontrols by reading DSP value over
3332 * IPC. It may be overwritten by alsa-mixer after booting up
3334 snd_sof_cache_kcontrol_val(sdev);
3337 /* manifest - optional to inform component of manifest */
3338 static int sof_manifest(struct snd_soc_component *scomp, int index,
3339 struct snd_soc_tplg_manifest *man)
3341 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
3345 size = le32_to_cpu(man->priv.size);
3347 /* backward compatible with tplg without ABI info */
3349 dev_dbg(sdev->dev, "No topology ABI info\n");
3353 if (size != SOF_TPLG_ABI_SIZE) {
3354 dev_err(sdev->dev, "error: invalid topology ABI size\n");
3359 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
3360 man->priv.data[0], man->priv.data[1],
3361 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
3364 abi_version = SOF_ABI_VER(man->priv.data[0],
3368 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
3369 dev_err(sdev->dev, "error: incompatible topology ABI version\n");
3373 if (abi_version > SOF_ABI_VERSION) {
3374 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
3375 dev_warn(sdev->dev, "warn: topology ABI is more recent than kernel\n");
3377 dev_err(sdev->dev, "error: topology ABI is more recent than kernel\n");
3385 /* vendor specific kcontrol handlers available for binding */
3386 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
3387 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
3388 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
3389 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
3390 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
3393 /* vendor specific bytes ext handlers available for binding */
3394 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
3395 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
3398 static struct snd_soc_tplg_ops sof_tplg_ops = {
3399 /* external kcontrol init - used for any driver specific init */
3400 .control_load = sof_control_load,
3401 .control_unload = sof_control_unload,
3403 /* external kcontrol init - used for any driver specific init */
3404 .dapm_route_load = sof_route_load,
3405 .dapm_route_unload = sof_route_unload,
3407 /* external widget init - used for any driver specific init */
3408 /* .widget_load is not currently used */
3409 .widget_ready = sof_widget_ready,
3410 .widget_unload = sof_widget_unload,
3412 /* FE DAI - used for any driver specific init */
3413 .dai_load = sof_dai_load,
3414 .dai_unload = sof_dai_unload,
3416 /* DAI link - used for any driver specific init */
3417 .link_load = sof_link_load,
3418 .link_unload = sof_link_unload,
3420 /* completion - called at completion of firmware loading */
3421 .complete = sof_complete,
3423 /* manifest - optional to inform component of manifest */
3424 .manifest = sof_manifest,
3426 /* vendor specific kcontrol handlers available for binding */
3427 .io_ops = sof_io_ops,
3428 .io_ops_count = ARRAY_SIZE(sof_io_ops),
3430 /* vendor specific bytes ext handlers available for binding */
3431 .bytes_ext_ops = sof_bytes_ext_ops,
3432 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
3435 int snd_sof_init_topology(struct snd_sof_dev *sdev,
3436 struct snd_soc_tplg_ops *ops)
3438 /* TODO: support linked list of topologies */
3439 sdev->tplg_ops = ops;
3442 EXPORT_SYMBOL(snd_sof_init_topology);
3444 int snd_sof_load_topology(struct snd_sof_dev *sdev, const char *file)
3446 const struct firmware *fw;
3449 dev_dbg(sdev->dev, "loading topology:%s\n", file);
3451 ret = request_firmware(&fw, file, sdev->dev);
3453 dev_err(sdev->dev, "error: tplg request firmware %s failed err: %d\n",
3458 ret = snd_soc_tplg_component_load(sdev->component,
3460 SND_SOC_TPLG_INDEX_ALL);
3462 dev_err(sdev->dev, "error: tplg component load failed %d\n",
3467 release_firmware(fw);
3470 EXPORT_SYMBOL(snd_sof_load_topology);