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/bits.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/firmware.h>
15 #include <linux/workqueue.h>
16 #include <sound/tlv.h>
17 #include <uapi/sound/sof/tokens.h>
19 #include "sof-audio.h"
22 #define COMP_ID_UNASSIGNED 0xffffffff
24 * Constants used in the computation of linear volume gain
25 * from dB gain 20th root of 10 in Q1.16 fixed-point notation
27 #define VOL_TWENTIETH_ROOT_OF_TEN 73533
28 /* 40th root of 10 in Q1.16 fixed-point notation*/
29 #define VOL_FORTIETH_ROOT_OF_TEN 69419
31 /* 0.5 dB step value in topology TLV */
32 #define VOL_HALF_DB_STEP 50
40 * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
42 * @scomp: pointer to SOC component
43 * @object: target IPC struct to save the parsed values
44 * @token_id: token ID for the token array to be searched
45 * @tuples: pointer to the tuples array
46 * @num_tuples: number of tuples in the tuples array
47 * @object_size: size of the object
48 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
49 * looks for @token_instance_num of each token in the token array associated
52 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
53 struct snd_sof_tuple *tuples, int num_tuples,
54 size_t object_size, int token_instance_num)
56 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
57 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
58 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
59 const struct sof_topology_token *tokens;
62 if (token_list[token_id].count < 0) {
63 dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
67 /* No tokens to match */
68 if (!token_list[token_id].count)
71 tokens = token_list[token_id].tokens;
73 dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
77 for (i = 0; i < token_list[token_id].count; i++) {
79 int num_tokens_matched = 0;
81 for (j = 0; j < num_tuples; j++) {
82 if (tokens[i].token == tuples[j].token) {
83 switch (tokens[i].type) {
84 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
86 u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
89 *val = tuples[j].value.v;
92 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
93 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
95 u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
98 *val = (u16)tuples[j].value.v;
101 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
103 if (!tokens[i].get_token) {
105 "get_token not defined for token %d in %s\n",
106 tokens[i].token, token_list[token_id].name);
110 tokens[i].get_token((void *)tuples[j].value.s, object,
111 tokens[i].offset + offset);
118 num_tokens_matched++;
120 /* found all required sets of current token. Move to the next one */
121 if (!(num_tokens_matched % token_instance_num))
124 /* move to the next object */
125 offset += object_size;
133 static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
135 /* we only support dB scale TLV type at the moment */
136 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
139 /* min value in topology tlv data is multiplied by 100 */
140 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
143 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
147 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
148 TLV_DB_SCALE_MUTE) == 0)
157 * Function to truncate an unsigned 64-bit number
158 * by x bits and return 32-bit unsigned number. This
159 * function also takes care of rounding while truncating
161 static inline u32 vol_shift_64(u64 i, u32 x)
163 /* do not truncate more than 32 bits */
170 return (u32)(((i >> (x - 1)) + 1) >> 1);
174 * Function to compute a ^ exp where,
175 * a is a fractional number represented by a fixed-point
176 * integer with a fractional world length of "fwl"
178 * fwl is the fractional word length
179 * Return value is a fractional number represented by a
180 * fixed-point integer with a fractional word length of "fwl"
182 static u32 vol_pow32(u32 a, int exp, u32 fwl)
185 u32 power = 1 << fwl;
188 /* if exponent is 0, return 1 */
192 /* determine the number of iterations based on the exponent */
198 /* mutiply a "iter" times to compute power */
199 for (i = 0; i < iter; i++) {
201 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
202 * Truncate product back to fwl fractional bits with rounding
204 power = vol_shift_64((u64)power * a, fwl);
208 /* if exp is positive, return the result */
212 /* if exp is negative, return the multiplicative inverse */
213 numerator = (u64)1 << (fwl << 1);
214 do_div(numerator, power);
216 return (u32)numerator;
220 * Function to calculate volume gain from TLV data.
221 * This function can only handle gain steps that are multiples of 0.5 dB
223 u32 vol_compute_gain(u32 value, int *tlv)
230 if (value == 0 && tlv[TLV_MUTE])
234 * compute dB gain from tlv. tlv_step
235 * in topology is multiplied by 100
237 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
240 * compute linear gain represented by fixed-point
241 * int with VOLUME_FWL fractional bits
243 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
245 /* extract the fractional part of volume step */
246 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
248 /* if volume step is an odd multiple of 0.5 dB */
249 if (f_step == VOL_HALF_DB_STEP && (value & 1))
250 linear_gain = vol_shift_64((u64)linear_gain *
251 VOL_FORTIETH_ROOT_OF_TEN,
258 * Set up volume table for kcontrols from tlv data
259 * "size" specifies the number of entries in the table
261 static int set_up_volume_table(struct snd_sof_control *scontrol,
262 int tlv[SOF_TLV_ITEMS], int size)
264 struct snd_soc_component *scomp = scontrol->scomp;
265 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
266 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
268 if (tplg_ops->control->set_up_volume_table)
269 return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
271 dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
275 struct sof_dai_types {
277 enum sof_ipc_dai_type type;
280 static const struct sof_dai_types sof_dais[] = {
281 {"SSP", SOF_DAI_INTEL_SSP},
282 {"HDA", SOF_DAI_INTEL_HDA},
283 {"DMIC", SOF_DAI_INTEL_DMIC},
284 {"ALH", SOF_DAI_INTEL_ALH},
285 {"SAI", SOF_DAI_IMX_SAI},
286 {"ESAI", SOF_DAI_IMX_ESAI},
287 {"ACP", SOF_DAI_AMD_BT},
288 {"ACPSP", SOF_DAI_AMD_SP},
289 {"ACPDMIC", SOF_DAI_AMD_DMIC},
290 {"ACPHS", SOF_DAI_AMD_HS},
291 {"AFE", SOF_DAI_MEDIATEK_AFE},
294 static enum sof_ipc_dai_type find_dai(const char *name)
298 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
299 if (strcmp(name, sof_dais[i].name) == 0)
300 return sof_dais[i].type;
303 return SOF_DAI_INTEL_NONE;
307 * Supported Frame format types and lookup, add new ones to end of list.
310 struct sof_frame_types {
312 enum sof_ipc_frame frame;
315 static const struct sof_frame_types sof_frames[] = {
316 {"s16le", SOF_IPC_FRAME_S16_LE},
317 {"s24le", SOF_IPC_FRAME_S24_4LE},
318 {"s32le", SOF_IPC_FRAME_S32_LE},
319 {"float", SOF_IPC_FRAME_FLOAT},
322 static enum sof_ipc_frame find_format(const char *name)
326 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
327 if (strcmp(name, sof_frames[i].name) == 0)
328 return sof_frames[i].frame;
331 /* use s32le if nothing is specified */
332 return SOF_IPC_FRAME_S32_LE;
335 int get_token_u32(void *elem, void *object, u32 offset)
337 struct snd_soc_tplg_vendor_value_elem *velem = elem;
338 u32 *val = (u32 *)((u8 *)object + offset);
340 *val = le32_to_cpu(velem->value);
344 int get_token_u16(void *elem, void *object, u32 offset)
346 struct snd_soc_tplg_vendor_value_elem *velem = elem;
347 u16 *val = (u16 *)((u8 *)object + offset);
349 *val = (u16)le32_to_cpu(velem->value);
353 int get_token_uuid(void *elem, void *object, u32 offset)
355 struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
356 u8 *dst = (u8 *)object + offset;
358 memcpy(dst, velem->uuid, UUID_SIZE);
363 int get_token_comp_format(void *elem, void *object, u32 offset)
365 u32 *val = (u32 *)((u8 *)object + offset);
367 *val = find_format((const char *)elem);
371 int get_token_dai_type(void *elem, void *object, u32 offset)
373 u32 *val = (u32 *)((u8 *)object + offset);
375 *val = find_dai((const char *)elem);
380 static const struct sof_topology_token stream_tokens[] = {
381 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
382 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
383 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
384 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
388 static const struct sof_topology_token led_tokens[] = {
389 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
390 offsetof(struct snd_sof_led_control, use_led)},
391 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
392 offsetof(struct snd_sof_led_control, direction)},
396 * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
397 * @scomp: pointer to soc component
398 * @object: target ipc struct for parsed values
399 * @offset: offset within the object pointer
400 * @tokens: array of struct sof_topology_token containing the tokens to be matched
401 * @num_tokens: number of tokens in tokens array
402 * @array: source pointer to consecutive vendor arrays in topology
404 * This function parses multiple sets of string type tokens in vendor arrays
406 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
407 void *object, size_t offset,
408 const struct sof_topology_token *tokens, int num_tokens,
409 struct snd_soc_tplg_vendor_array *array)
411 struct snd_soc_tplg_vendor_uuid_elem *elem;
415 /* parse element by element */
416 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
417 elem = &array->uuid[i];
419 /* search for token */
420 for (j = 0; j < num_tokens; j++) {
421 /* match token type */
422 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
426 if (tokens[j].token != le32_to_cpu(elem->token))
429 /* matched - now load token */
430 tokens[j].get_token(elem, object,
431 offset + tokens[j].offset);
441 * sof_copy_tuples - Parse tokens and copy them to the @tuples array
442 * @sdev: pointer to struct snd_sof_dev
443 * @array: source pointer to consecutive vendor arrays in topology
444 * @array_size: size of @array
445 * @token_id: Token ID associated with a token array
446 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
447 * looks for @token_instance_num of each token in the token array associated
449 * @tuples: tuples array to copy the matched tuples to
450 * @tuples_size: size of @tuples
451 * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
454 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
455 int array_size, u32 token_id, int token_instance_num,
456 struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
458 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
459 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
460 const struct sof_topology_token *tokens;
462 int num_tokens, asize;
465 /* nothing to do if token_list is NULL */
469 if (!tuples || !num_copied_tuples) {
470 dev_err(sdev->dev, "Invalid tuples array\n");
474 tokens = token_list[token_id].tokens;
475 num_tokens = token_list[token_id].count;
478 dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
482 /* check if there's space in the tuples array for new tokens */
483 if (*num_copied_tuples >= tuples_size) {
484 dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
485 token_list[token_id].name);
489 while (array_size > 0 && found < num_tokens * token_instance_num) {
490 asize = le32_to_cpu(array->size);
494 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
498 /* make sure there is enough data before parsing */
500 if (array_size < 0) {
501 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
505 /* parse element by element */
506 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
507 /* search for token */
508 for (j = 0; j < num_tokens; j++) {
509 /* match token type */
510 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
511 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
512 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
513 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
514 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
517 if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
518 struct snd_soc_tplg_vendor_string_elem *elem;
520 elem = &array->string[i];
523 if (tokens[j].token != le32_to_cpu(elem->token))
526 tuples[*num_copied_tuples].token = tokens[j].token;
527 tuples[*num_copied_tuples].value.s = elem->string;
529 struct snd_soc_tplg_vendor_value_elem *elem;
531 elem = &array->value[i];
534 if (tokens[j].token != le32_to_cpu(elem->token))
537 tuples[*num_copied_tuples].token = tokens[j].token;
538 tuples[*num_copied_tuples].value.v =
539 le32_to_cpu(elem->value);
542 (*num_copied_tuples)++;
544 /* stop if there's no space for any more new tuples */
545 if (*num_copied_tuples == tuples_size)
549 /* stop when we've found the required token instances */
550 if (found == num_tokens * token_instance_num)
555 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
562 * sof_parse_string_tokens - Parse multiple sets of tokens
563 * @scomp: pointer to soc component
564 * @object: target ipc struct for parsed values
565 * @offset: offset within the object pointer
566 * @tokens: array of struct sof_topology_token containing the tokens to be matched
567 * @num_tokens: number of tokens in tokens array
568 * @array: source pointer to consecutive vendor arrays in topology
570 * This function parses multiple sets of string type tokens in vendor arrays
572 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
573 void *object, int offset,
574 const struct sof_topology_token *tokens, int num_tokens,
575 struct snd_soc_tplg_vendor_array *array)
577 struct snd_soc_tplg_vendor_string_elem *elem;
581 /* parse element by element */
582 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
583 elem = &array->string[i];
585 /* search for token */
586 for (j = 0; j < num_tokens; j++) {
587 /* match token type */
588 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
592 if (tokens[j].token != le32_to_cpu(elem->token))
595 /* matched - now load token */
596 tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
606 * sof_parse_word_tokens - Parse multiple sets of tokens
607 * @scomp: pointer to soc component
608 * @object: target ipc struct for parsed values
609 * @offset: offset within the object pointer
610 * @tokens: array of struct sof_topology_token containing the tokens to be matched
611 * @num_tokens: number of tokens in tokens array
612 * @array: source pointer to consecutive vendor arrays in topology
614 * This function parses multiple sets of word type tokens in vendor arrays
616 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
617 void *object, int offset,
618 const struct sof_topology_token *tokens, int num_tokens,
619 struct snd_soc_tplg_vendor_array *array)
621 struct snd_soc_tplg_vendor_value_elem *elem;
625 /* parse element by element */
626 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
627 elem = &array->value[i];
629 /* search for token */
630 for (j = 0; j < num_tokens; j++) {
631 /* match token type */
632 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
633 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
634 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
635 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
639 if (tokens[j].token != le32_to_cpu(elem->token))
643 tokens[j].get_token(elem, object, offset + tokens[j].offset);
653 * sof_parse_token_sets - Parse multiple sets of tokens
654 * @scomp: pointer to soc component
655 * @object: target ipc struct for parsed values
656 * @tokens: token definition array describing what tokens to parse
657 * @count: number of tokens in definition array
658 * @array: source pointer to consecutive vendor arrays in topology
659 * @array_size: total size of @array
660 * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
661 * looks for @token_instance_num of each token in the @tokens
662 * @object_size: offset to next target ipc struct with multiple sets
664 * This function parses multiple sets of tokens in vendor arrays into
665 * consecutive ipc structs.
667 static int sof_parse_token_sets(struct snd_soc_component *scomp,
668 void *object, const struct sof_topology_token *tokens,
669 int count, struct snd_soc_tplg_vendor_array *array,
670 int array_size, int token_instance_num, size_t object_size)
677 while (array_size > 0 && total < count * token_instance_num) {
678 asize = le32_to_cpu(array->size);
681 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
682 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
687 /* make sure there is enough data before parsing */
689 if (array_size < 0) {
690 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
695 /* call correct parser depending on type */
696 switch (le32_to_cpu(array->type)) {
697 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
698 found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
701 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
702 found += sof_parse_string_tokens(scomp, object, offset, tokens, count,
705 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
706 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
707 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
708 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
709 found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
713 dev_err(scomp->dev, "error: unknown token type %d\n",
719 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
722 /* move to next target struct */
723 if (found >= count) {
724 offset += object_size;
734 * sof_parse_tokens - Parse one set of tokens
735 * @scomp: pointer to soc component
736 * @object: target ipc struct for parsed values
737 * @tokens: token definition array describing what tokens to parse
738 * @num_tokens: number of tokens in definition array
739 * @array: source pointer to consecutive vendor arrays in topology
740 * @array_size: total size of @array
742 * This function parses a single set of tokens in vendor arrays into
743 * consecutive ipc structs.
745 static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
746 const struct sof_topology_token *tokens, int num_tokens,
747 struct snd_soc_tplg_vendor_array *array,
752 * sof_parse_tokens is used when topology contains only a single set of
753 * identical tuples arrays. So additional parameters to
754 * sof_parse_token_sets are sets = 1 (only 1 set) and
755 * object_size = 0 (irrelevant).
757 return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
762 * Standard Kcontrols.
765 static int sof_control_load_volume(struct snd_soc_component *scomp,
766 struct snd_sof_control *scontrol,
767 struct snd_kcontrol_new *kc,
768 struct snd_soc_tplg_ctl_hdr *hdr)
770 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
771 struct snd_soc_tplg_mixer_control *mc =
772 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
773 int tlv[SOF_TLV_ITEMS];
777 /* validate topology data */
778 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
782 * If control has more than 2 channels we need to override the info. This is because even if
783 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
784 * pre-defined dapm control types (and related functions) creating the actual control
785 * restrict the channels only to mono or stereo.
787 if (le32_to_cpu(mc->num_channels) > 2)
788 kc->info = snd_sof_volume_info;
790 scontrol->comp_id = sdev->next_comp_id;
791 scontrol->min_volume_step = le32_to_cpu(mc->min);
792 scontrol->max_volume_step = le32_to_cpu(mc->max);
793 scontrol->num_channels = le32_to_cpu(mc->num_channels);
795 scontrol->max = le32_to_cpu(mc->max);
796 if (le32_to_cpu(mc->max) == 1)
799 /* extract tlv data */
800 if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
801 dev_err(scomp->dev, "error: invalid TLV data\n");
805 /* set up volume table */
806 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
808 dev_err(scomp->dev, "error: setting up volume table\n");
813 /* set up possible led control from mixer private data */
814 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
815 ARRAY_SIZE(led_tokens), mc->priv.array,
816 le32_to_cpu(mc->priv.size));
818 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
819 le32_to_cpu(mc->priv.size));
823 if (scontrol->led_ctl.use_led) {
824 mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
825 SNDRV_CTL_ELEM_ACCESS_SPK_LED;
826 scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
827 scontrol->access |= mask;
828 kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
830 sdev->led_present = true;
833 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
834 scontrol->comp_id, scontrol->num_channels);
839 if (le32_to_cpu(mc->max) > 1)
840 kfree(scontrol->volume_table);
845 static int sof_control_load_enum(struct snd_soc_component *scomp,
846 struct snd_sof_control *scontrol,
847 struct snd_kcontrol_new *kc,
848 struct snd_soc_tplg_ctl_hdr *hdr)
850 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
851 struct snd_soc_tplg_enum_control *ec =
852 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
854 /* validate topology data */
855 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
858 scontrol->comp_id = sdev->next_comp_id;
859 scontrol->num_channels = le32_to_cpu(ec->num_channels);
861 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
862 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
867 static int sof_control_load_bytes(struct snd_soc_component *scomp,
868 struct snd_sof_control *scontrol,
869 struct snd_kcontrol_new *kc,
870 struct snd_soc_tplg_ctl_hdr *hdr)
872 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
873 struct snd_soc_tplg_bytes_control *control =
874 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
875 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
876 size_t priv_size = le32_to_cpu(control->priv.size);
878 scontrol->max_size = sbe->max;
879 scontrol->comp_id = sdev->next_comp_id;
881 dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
883 /* copy the private data */
885 scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
889 scontrol->priv_size = priv_size;
895 /* external kcontrol init - used for any driver specific init */
896 static int sof_control_load(struct snd_soc_component *scomp, int index,
897 struct snd_kcontrol_new *kc,
898 struct snd_soc_tplg_ctl_hdr *hdr)
900 struct soc_mixer_control *sm;
901 struct soc_bytes_ext *sbe;
903 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
904 struct snd_soc_dobj *dobj;
905 struct snd_sof_control *scontrol;
908 dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
909 hdr->type, hdr->name);
911 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
915 scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
916 if (!scontrol->name) {
921 scontrol->scomp = scomp;
922 scontrol->access = kc->access;
923 scontrol->info_type = le32_to_cpu(hdr->ops.info);
924 scontrol->index = kc->index;
926 switch (le32_to_cpu(hdr->ops.info)) {
927 case SND_SOC_TPLG_CTL_VOLSW:
928 case SND_SOC_TPLG_CTL_VOLSW_SX:
929 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
930 sm = (struct soc_mixer_control *)kc->private_value;
932 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
934 case SND_SOC_TPLG_CTL_BYTES:
935 sbe = (struct soc_bytes_ext *)kc->private_value;
937 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
939 case SND_SOC_TPLG_CTL_ENUM:
940 case SND_SOC_TPLG_CTL_ENUM_VALUE:
941 se = (struct soc_enum *)kc->private_value;
943 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
945 case SND_SOC_TPLG_CTL_RANGE:
946 case SND_SOC_TPLG_CTL_STROBE:
947 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
948 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
949 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
950 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
951 case SND_SOC_TPLG_DAPM_CTL_PIN:
953 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
954 hdr->ops.get, hdr->ops.put, hdr->ops.info);
955 kfree(scontrol->name);
961 kfree(scontrol->name);
966 scontrol->led_ctl.led_value = -1;
968 dobj->private = scontrol;
969 list_add(&scontrol->list, &sdev->kcontrol_list);
973 static int sof_control_unload(struct snd_soc_component *scomp,
974 struct snd_soc_dobj *dobj)
976 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
977 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
978 struct snd_sof_control *scontrol = dobj->private;
981 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
983 if (ipc_tplg_ops->control_free) {
984 ret = ipc_tplg_ops->control_free(sdev, scontrol);
986 dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
989 /* free all data before returning in case of error too */
990 kfree(scontrol->ipc_control_data);
991 kfree(scontrol->priv);
992 kfree(scontrol->name);
993 list_del(&scontrol->list);
1003 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1004 struct snd_soc_dapm_widget *w,
1005 struct snd_soc_tplg_dapm_widget *tw,
1006 struct snd_sof_dai *dai)
1008 struct snd_soc_card *card = scomp->card;
1009 struct snd_soc_pcm_runtime *rtd;
1010 struct snd_soc_dai *cpu_dai;
1014 dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
1018 list_for_each_entry(rtd, &card->rtd_list, list) {
1019 /* does stream match DAI link ? */
1020 if (!rtd->dai_link->stream_name ||
1021 strcmp(w->sname, rtd->dai_link->stream_name))
1025 case snd_soc_dapm_dai_out:
1026 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1028 * Please create DAI widget in the right order
1029 * to ensure BE will connect to the right DAI
1032 if (!cpu_dai->capture_widget) {
1033 cpu_dai->capture_widget = w;
1037 if (i == rtd->dai_link->num_cpus) {
1038 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1043 dai->name = rtd->dai_link->name;
1044 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1045 w->name, rtd->dai_link->name);
1047 case snd_soc_dapm_dai_in:
1048 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1050 * Please create DAI widget in the right order
1051 * to ensure BE will connect to the right DAI
1054 if (!cpu_dai->playback_widget) {
1055 cpu_dai->playback_widget = w;
1059 if (i == rtd->dai_link->num_cpus) {
1060 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1065 dai->name = rtd->dai_link->name;
1066 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1067 w->name, rtd->dai_link->name);
1074 /* check we have a connection */
1076 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1084 static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
1085 struct snd_soc_dapm_widget *w)
1087 struct snd_soc_card *card = scomp->card;
1088 struct snd_soc_pcm_runtime *rtd;
1089 const char *sname = w->sname;
1090 struct snd_soc_dai *cpu_dai;
1096 list_for_each_entry(rtd, &card->rtd_list, list) {
1097 /* does stream match DAI link ? */
1098 if (!rtd->dai_link->stream_name ||
1099 strcmp(sname, rtd->dai_link->stream_name))
1103 case snd_soc_dapm_dai_out:
1104 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1105 if (cpu_dai->capture_widget == w) {
1106 cpu_dai->capture_widget = NULL;
1111 case snd_soc_dapm_dai_in:
1112 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1113 if (cpu_dai->playback_widget == w) {
1114 cpu_dai->playback_widget = NULL;
1125 /* bind PCM ID to host component ID */
1126 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1129 struct snd_sof_widget *host_widget;
1131 host_widget = snd_sof_find_swidget_sname(scomp,
1132 spcm->pcm.caps[dir].name,
1135 dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1139 spcm->stream[dir].comp_id = host_widget->comp_id;
1144 static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
1151 for (i = 0; i < num_tuples; i++) {
1152 if (tuples[i].token == token_id)
1153 return tuples[i].value.v;
1159 static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
1160 struct snd_soc_tplg_dapm_widget *tw,
1161 enum sof_tokens *object_token_list, int count)
1163 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1164 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1165 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
1166 struct snd_soc_tplg_private *private = &tw->priv;
1170 if (count > 0 && !object_token_list) {
1171 dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
1175 /* calculate max size of tuples array */
1176 for (i = 0; i < count; i++)
1177 num_tuples += token_list[object_token_list[i]].count;
1179 /* allocate memory for tuples array */
1180 swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
1181 if (!swidget->tuples)
1184 /* parse token list for widget */
1185 for (i = 0; i < count; i++) {
1188 if (object_token_list[i] >= SOF_TOKEN_COUNT) {
1189 dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
1190 object_token_list[i], swidget->widget->name);
1195 switch (object_token_list[i]) {
1196 case SOF_COMP_EXT_TOKENS:
1197 /* parse and save UUID in swidget */
1198 ret = sof_parse_tokens(scomp, swidget,
1199 token_list[object_token_list[i]].tokens,
1200 token_list[object_token_list[i]].count,
1201 private->array, le32_to_cpu(private->size));
1203 dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
1204 token_list[object_token_list[i]].name,
1205 swidget->widget->name);
1210 case SOF_IN_AUDIO_FORMAT_TOKENS:
1211 case SOF_OUT_AUDIO_FORMAT_TOKENS:
1212 case SOF_COPIER_GATEWAY_CFG_TOKENS:
1213 case SOF_AUDIO_FORMAT_BUFFER_SIZE_TOKENS:
1214 num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_AUDIO_FORMATS,
1215 swidget->tuples, swidget->num_tuples);
1218 dev_err(sdev->dev, "Invalid audio format count for %s\n",
1219 swidget->widget->name);
1225 struct snd_sof_tuple *new_tuples;
1227 num_tuples += token_list[object_token_list[i]].count * num_sets;
1228 new_tuples = krealloc(swidget->tuples,
1229 sizeof(*new_tuples) * num_tuples, GFP_KERNEL);
1235 swidget->tuples = new_tuples;
1242 /* copy one set of tuples per token ID into swidget->tuples */
1243 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1244 object_token_list[i], num_sets, swidget->tuples,
1245 num_tuples, &swidget->num_tuples);
1247 dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
1248 token_list[object_token_list[i]].name, swidget->widget->name, ret);
1255 kfree(swidget->tuples);
1259 /* external widget init - used for any driver specific init */
1260 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
1261 struct snd_soc_dapm_widget *w,
1262 struct snd_soc_tplg_dapm_widget *tw)
1264 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1265 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1266 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1267 struct snd_sof_widget *swidget;
1268 struct snd_sof_dai *dai;
1269 enum sof_tokens *token_list;
1270 int token_list_size;
1273 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
1277 swidget->scomp = scomp;
1278 swidget->widget = w;
1279 swidget->comp_id = sdev->next_comp_id++;
1280 swidget->complete = 0;
1281 swidget->id = w->id;
1282 swidget->pipeline_id = index;
1283 swidget->private = NULL;
1285 dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
1286 swidget->comp_id, index, swidget->id, tw->name,
1287 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1288 ? tw->sname : "none");
1290 token_list = widget_ops[w->id].token_list;
1291 token_list_size = widget_ops[w->id].token_list_size;
1293 /* handle any special case widgets */
1295 case snd_soc_dapm_dai_in:
1296 case snd_soc_dapm_dai_out:
1297 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
1304 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1306 ret = sof_connect_dai_widget(scomp, w, tw, dai);
1311 list_add(&dai->list, &sdev->dai_list);
1312 swidget->private = dai;
1314 case snd_soc_dapm_effect:
1315 /* check we have some tokens - we need at least process type */
1316 if (le32_to_cpu(tw->priv.size) == 0) {
1317 dev_err(scomp->dev, "error: process tokens not found\n");
1321 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1323 case snd_soc_dapm_pga:
1324 if (!le32_to_cpu(tw->num_kcontrols)) {
1325 dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
1332 case snd_soc_dapm_mixer:
1333 case snd_soc_dapm_buffer:
1334 case snd_soc_dapm_scheduler:
1335 case snd_soc_dapm_aif_out:
1336 case snd_soc_dapm_aif_in:
1337 case snd_soc_dapm_src:
1338 case snd_soc_dapm_asrc:
1339 case snd_soc_dapm_siggen:
1340 case snd_soc_dapm_mux:
1341 case snd_soc_dapm_demux:
1342 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1344 case snd_soc_dapm_switch:
1345 case snd_soc_dapm_dai_link:
1346 case snd_soc_dapm_kcontrol:
1348 dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
1352 /* check token parsing reply */
1355 "error: failed to add widget id %d type %d name : %s stream %s\n",
1356 tw->shift, swidget->id, tw->name,
1357 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1358 ? tw->sname : "none");
1363 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
1364 swidget->core = SOF_DSP_PRIMARY_CORE;
1366 int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
1367 swidget->num_tuples);
1370 swidget->core = core;
1373 /* bind widget to external event */
1374 if (tw->event_type) {
1375 if (widget_ops[w->id].bind_event) {
1376 ret = widget_ops[w->id].bind_event(scomp, swidget,
1377 le16_to_cpu(tw->event_type));
1379 dev_err(scomp->dev, "widget event binding failed for %s\n",
1380 swidget->widget->name);
1381 kfree(swidget->private);
1382 kfree(swidget->tuples);
1389 w->dobj.private = swidget;
1390 list_add(&swidget->list, &sdev->widget_list);
1394 static int sof_route_unload(struct snd_soc_component *scomp,
1395 struct snd_soc_dobj *dobj)
1397 struct snd_sof_route *sroute;
1399 sroute = dobj->private;
1403 /* free sroute and its private data */
1404 kfree(sroute->private);
1405 list_del(&sroute->list);
1411 static int sof_widget_unload(struct snd_soc_component *scomp,
1412 struct snd_soc_dobj *dobj)
1414 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1415 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1416 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1417 const struct snd_kcontrol_new *kc;
1418 struct snd_soc_dapm_widget *widget;
1419 struct snd_sof_control *scontrol;
1420 struct snd_sof_widget *swidget;
1421 struct soc_mixer_control *sm;
1422 struct soc_bytes_ext *sbe;
1423 struct snd_sof_dai *dai;
1424 struct soc_enum *se;
1427 swidget = dobj->private;
1431 widget = swidget->widget;
1433 switch (swidget->id) {
1434 case snd_soc_dapm_dai_in:
1435 case snd_soc_dapm_dai_out:
1436 dai = swidget->private;
1439 list_del(&dai->list);
1441 sof_disconnect_dai_widget(scomp, widget);
1447 for (i = 0; i < widget->num_kcontrols; i++) {
1448 kc = &widget->kcontrol_news[i];
1449 switch (widget->dobj.widget.kcontrol_type[i]) {
1450 case SND_SOC_TPLG_TYPE_MIXER:
1451 sm = (struct soc_mixer_control *)kc->private_value;
1452 scontrol = sm->dobj.private;
1454 kfree(scontrol->volume_table);
1456 case SND_SOC_TPLG_TYPE_ENUM:
1457 se = (struct soc_enum *)kc->private_value;
1458 scontrol = se->dobj.private;
1460 case SND_SOC_TPLG_TYPE_BYTES:
1461 sbe = (struct soc_bytes_ext *)kc->private_value;
1462 scontrol = sbe->dobj.private;
1465 dev_warn(scomp->dev, "unsupported kcontrol_type\n");
1468 kfree(scontrol->ipc_control_data);
1469 list_del(&scontrol->list);
1470 kfree(scontrol->name);
1475 /* free IPC related data */
1476 if (widget_ops[swidget->id].ipc_free)
1477 widget_ops[swidget->id].ipc_free(swidget);
1479 kfree(swidget->tuples);
1481 /* remove and free swidget object */
1482 list_del(&swidget->list);
1489 * DAI HW configuration.
1492 /* FE DAI - used for any driver specific init */
1493 static int sof_dai_load(struct snd_soc_component *scomp, int index,
1494 struct snd_soc_dai_driver *dai_drv,
1495 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
1497 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1498 struct snd_soc_tplg_stream_caps *caps;
1499 struct snd_soc_tplg_private *private = &pcm->priv;
1500 struct snd_sof_pcm *spcm;
1504 /* nothing to do for BEs atm */
1508 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
1512 spcm->scomp = scomp;
1514 for_each_pcm_streams(stream) {
1515 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
1517 snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1519 snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1523 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
1525 dai_drv->dobj.private = spcm;
1526 list_add(&spcm->list, &sdev->pcm_list);
1528 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
1529 ARRAY_SIZE(stream_tokens), private->array,
1530 le32_to_cpu(private->size));
1532 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
1533 le32_to_cpu(private->size));
1537 /* do we need to allocate playback PCM DMA pages */
1538 if (!spcm->pcm.playback)
1541 stream = SNDRV_PCM_STREAM_PLAYBACK;
1543 caps = &spcm->pcm.caps[stream];
1545 /* allocate playback page table buffer */
1546 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1547 PAGE_SIZE, &spcm->stream[stream].page_table);
1549 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1555 /* bind pcm to host comp */
1556 ret = spcm_bind(scomp, spcm, stream);
1559 "error: can't bind pcm to host\n");
1560 goto free_playback_tables;
1564 stream = SNDRV_PCM_STREAM_CAPTURE;
1566 /* do we need to allocate capture PCM DMA pages */
1567 if (!spcm->pcm.capture)
1570 caps = &spcm->pcm.caps[stream];
1572 /* allocate capture page table buffer */
1573 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1574 PAGE_SIZE, &spcm->stream[stream].page_table);
1576 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1578 goto free_playback_tables;
1581 /* bind pcm to host comp */
1582 ret = spcm_bind(scomp, spcm, stream);
1585 "error: can't bind pcm to host\n");
1586 snd_dma_free_pages(&spcm->stream[stream].page_table);
1587 goto free_playback_tables;
1592 free_playback_tables:
1593 if (spcm->pcm.playback)
1594 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1599 static int sof_dai_unload(struct snd_soc_component *scomp,
1600 struct snd_soc_dobj *dobj)
1602 struct snd_sof_pcm *spcm = dobj->private;
1604 /* free PCM DMA pages */
1605 if (spcm->pcm.playback)
1606 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1608 if (spcm->pcm.capture)
1609 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
1611 /* remove from list and free spcm */
1612 list_del(&spcm->list);
1618 static const struct sof_topology_token common_dai_link_tokens[] = {
1619 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
1620 offsetof(struct snd_sof_dai_link, type)},
1623 /* DAI link - used for any driver specific init */
1624 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
1625 struct snd_soc_tplg_link_config *cfg)
1627 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1628 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1629 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
1630 struct snd_soc_tplg_private *private = &cfg->priv;
1631 struct snd_sof_dai_link *slink;
1636 if (!link->platforms) {
1637 dev_err(scomp->dev, "error: no platforms\n");
1640 link->platforms->name = dev_name(scomp->dev);
1643 * Set nonatomic property for FE dai links as their trigger action
1646 if (!link->no_pcm) {
1647 link->nonatomic = true;
1650 * set default trigger order for all links. Exceptions to
1651 * the rule will be handled in sof_pcm_dai_link_fixup()
1652 * For playback, the sequence is the following: start FE,
1653 * start BE, stop BE, stop FE; for Capture the sequence is
1654 * inverted start BE, start FE, stop FE, stop BE
1656 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
1657 SND_SOC_DPCM_TRIGGER_PRE;
1658 link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
1659 SND_SOC_DPCM_TRIGGER_POST;
1661 /* nothing more to do for FE dai links */
1665 /* check we have some tokens - we need at least DAI type */
1666 if (le32_to_cpu(private->size) == 0) {
1667 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
1671 slink = kzalloc(sizeof(*slink), GFP_KERNEL);
1675 slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
1676 slink->hw_configs = kmemdup(cfg->hw_config,
1677 sizeof(*slink->hw_configs) * slink->num_hw_configs,
1679 if (!slink->hw_configs) {
1684 slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
1687 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
1688 slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
1690 ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
1691 ARRAY_SIZE(common_dai_link_tokens),
1692 private->array, le32_to_cpu(private->size));
1694 dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
1695 kfree(slink->hw_configs);
1703 /* calculate size of tuples array */
1704 num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
1705 num_sets = slink->num_hw_configs;
1706 switch (slink->type) {
1707 case SOF_DAI_INTEL_SSP:
1708 token_id = SOF_SSP_TOKENS;
1709 num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
1711 case SOF_DAI_INTEL_DMIC:
1712 token_id = SOF_DMIC_TOKENS;
1713 num_tuples += token_list[SOF_DMIC_TOKENS].count;
1715 /* Allocate memory for max PDM controllers */
1716 num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
1718 case SOF_DAI_INTEL_HDA:
1719 token_id = SOF_HDA_TOKENS;
1720 num_tuples += token_list[SOF_HDA_TOKENS].count;
1722 case SOF_DAI_INTEL_ALH:
1723 token_id = SOF_ALH_TOKENS;
1724 num_tuples += token_list[SOF_ALH_TOKENS].count;
1726 case SOF_DAI_IMX_SAI:
1727 token_id = SOF_SAI_TOKENS;
1728 num_tuples += token_list[SOF_SAI_TOKENS].count;
1730 case SOF_DAI_IMX_ESAI:
1731 token_id = SOF_ESAI_TOKENS;
1732 num_tuples += token_list[SOF_ESAI_TOKENS].count;
1734 case SOF_DAI_MEDIATEK_AFE:
1735 token_id = SOF_AFE_TOKENS;
1736 num_tuples += token_list[SOF_AFE_TOKENS].count;
1738 case SOF_DAI_AMD_DMIC:
1739 token_id = SOF_ACPDMIC_TOKENS;
1740 num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
1746 /* allocate memory for tuples array */
1747 slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
1748 if (!slink->tuples) {
1749 kfree(slink->hw_configs);
1754 if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
1755 /* parse one set of DAI link tokens */
1756 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1757 SOF_DAI_LINK_TOKENS, 1, slink->tuples,
1758 num_tuples, &slink->num_tuples);
1760 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1761 token_list[SOF_DAI_LINK_TOKENS].name, link->name);
1766 /* nothing more to do if there are no DAI type-specific tokens defined */
1767 if (!token_id || !token_list[token_id].tokens)
1770 /* parse "num_sets" sets of DAI-specific tokens */
1771 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1772 token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
1774 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1775 token_list[token_id].name, link->name);
1779 /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
1780 if (token_id == SOF_DMIC_TOKENS) {
1781 num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
1782 slink->tuples, slink->num_tuples);
1785 dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
1790 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1791 SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
1792 num_tuples, &slink->num_tuples);
1794 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1795 token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
1800 link->dobj.private = slink;
1801 list_add(&slink->list, &sdev->dai_link_list);
1806 kfree(slink->tuples);
1807 kfree(slink->hw_configs);
1813 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
1815 struct snd_sof_dai_link *slink = dobj->private;
1820 kfree(slink->tuples);
1821 list_del(&slink->list);
1822 kfree(slink->hw_configs);
1824 dobj->private = NULL;
1829 /* DAI link - used for any driver specific init */
1830 static int sof_route_load(struct snd_soc_component *scomp, int index,
1831 struct snd_soc_dapm_route *route)
1833 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1834 struct snd_sof_widget *source_swidget, *sink_swidget;
1835 struct snd_soc_dobj *dobj = &route->dobj;
1836 struct snd_sof_route *sroute;
1839 /* allocate memory for sroute and connect */
1840 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
1844 sroute->scomp = scomp;
1845 dev_dbg(scomp->dev, "sink %s control %s source %s\n",
1846 route->sink, route->control ? route->control : "none",
1849 /* source component */
1850 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
1851 if (!source_swidget) {
1852 dev_err(scomp->dev, "error: source %s not found\n",
1859 * Virtual widgets of type output/out_drv may be added in topology
1860 * for compatibility. These are not handled by the FW.
1861 * So, don't send routes whose source/sink widget is of such types
1864 if (source_swidget->id == snd_soc_dapm_out_drv ||
1865 source_swidget->id == snd_soc_dapm_output)
1868 /* sink component */
1869 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
1870 if (!sink_swidget) {
1871 dev_err(scomp->dev, "error: sink %s not found\n",
1878 * Don't send routes whose sink widget is of type
1879 * output or out_drv to the DSP
1881 if (sink_swidget->id == snd_soc_dapm_out_drv ||
1882 sink_swidget->id == snd_soc_dapm_output)
1885 sroute->route = route;
1886 dobj->private = sroute;
1887 sroute->src_widget = source_swidget;
1888 sroute->sink_widget = sink_swidget;
1890 /* add route to route list */
1891 list_add(&sroute->list, &sdev->route_list);
1900 * sof_set_pipe_widget - Set pipe_widget for a component
1901 * @sdev: pointer to struct snd_sof_dev
1902 * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
1903 * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
1905 * Return: 0 if successful, -EINVAL on error.
1906 * The function checks if @swidget is associated with any volatile controls. If so, setting
1907 * the dynamic_pipeline_widget is disallowed.
1909 static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
1910 struct snd_sof_widget *swidget)
1912 struct snd_sof_control *scontrol;
1914 if (pipe_widget->dynamic_pipeline_widget) {
1915 /* dynamic widgets cannot have volatile kcontrols */
1916 list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
1917 if (scontrol->comp_id == swidget->comp_id &&
1918 (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
1920 "error: volatile control found for dynamic widget %s\n",
1921 swidget->widget->name);
1926 /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
1927 swidget->pipe_widget = pipe_widget;
1928 swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
1933 /* completion - called at completion of firmware loading */
1934 static int sof_complete(struct snd_soc_component *scomp)
1936 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1937 struct snd_sof_widget *swidget, *comp_swidget;
1938 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1939 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1940 struct snd_sof_control *scontrol;
1943 /* first update all control IPC structures based on the IPC version */
1944 if (ipc_tplg_ops->control_setup)
1945 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1946 ret = ipc_tplg_ops->control_setup(sdev, scontrol);
1948 dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
1955 * then update all widget IPC structures. If any of the ipc_setup callbacks fail, the
1956 * topology will be removed and all widgets will be unloaded resulting in freeing all
1957 * associated memories.
1959 list_for_each_entry(swidget, &sdev->widget_list, list) {
1960 if (widget_ops[swidget->id].ipc_setup) {
1961 ret = widget_ops[swidget->id].ipc_setup(swidget);
1963 dev_err(sdev->dev, "failed updating IPC struct for %s\n",
1964 swidget->widget->name);
1970 /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
1971 list_for_each_entry(swidget, &sdev->widget_list, list) {
1972 switch (swidget->id) {
1973 case snd_soc_dapm_scheduler:
1975 * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
1976 * for all widgets that have the same pipeline ID as the scheduler widget
1978 list_for_each_entry(comp_swidget, &sdev->widget_list, list)
1979 if (comp_swidget->pipeline_id == swidget->pipeline_id) {
1980 ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
1990 /* verify topology components loading including dynamic pipelines */
1991 if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
1992 if (ipc_tplg_ops->set_up_all_pipelines && ipc_tplg_ops->tear_down_all_pipelines) {
1993 ret = ipc_tplg_ops->set_up_all_pipelines(sdev, true);
1995 dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
2000 ret = ipc_tplg_ops->tear_down_all_pipelines(sdev, true);
2002 dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
2009 /* set up static pipelines */
2010 if (ipc_tplg_ops->set_up_all_pipelines)
2011 return ipc_tplg_ops->set_up_all_pipelines(sdev, false);
2016 /* manifest - optional to inform component of manifest */
2017 static int sof_manifest(struct snd_soc_component *scomp, int index,
2018 struct snd_soc_tplg_manifest *man)
2020 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2021 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
2023 if (ipc_tplg_ops->parse_manifest)
2024 return ipc_tplg_ops->parse_manifest(scomp, index, man);
2029 /* vendor specific kcontrol handlers available for binding */
2030 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
2031 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
2032 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
2033 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
2034 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
2037 /* vendor specific bytes ext handlers available for binding */
2038 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
2039 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
2040 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
2043 static struct snd_soc_tplg_ops sof_tplg_ops = {
2044 /* external kcontrol init - used for any driver specific init */
2045 .control_load = sof_control_load,
2046 .control_unload = sof_control_unload,
2048 /* external kcontrol init - used for any driver specific init */
2049 .dapm_route_load = sof_route_load,
2050 .dapm_route_unload = sof_route_unload,
2052 /* external widget init - used for any driver specific init */
2053 /* .widget_load is not currently used */
2054 .widget_ready = sof_widget_ready,
2055 .widget_unload = sof_widget_unload,
2057 /* FE DAI - used for any driver specific init */
2058 .dai_load = sof_dai_load,
2059 .dai_unload = sof_dai_unload,
2061 /* DAI link - used for any driver specific init */
2062 .link_load = sof_link_load,
2063 .link_unload = sof_link_unload,
2065 /* completion - called at completion of firmware loading */
2066 .complete = sof_complete,
2068 /* manifest - optional to inform component of manifest */
2069 .manifest = sof_manifest,
2071 /* vendor specific kcontrol handlers available for binding */
2072 .io_ops = sof_io_ops,
2073 .io_ops_count = ARRAY_SIZE(sof_io_ops),
2075 /* vendor specific bytes ext handlers available for binding */
2076 .bytes_ext_ops = sof_bytes_ext_ops,
2077 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
2080 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
2082 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2083 const struct firmware *fw;
2086 dev_dbg(scomp->dev, "loading topology:%s\n", file);
2088 ret = request_firmware(&fw, file, scomp->dev);
2090 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2093 "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
2097 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
2099 dev_err(scomp->dev, "error: tplg component load failed %d\n",
2104 release_firmware(fw);
2106 if (ret >= 0 && sdev->led_present)
2107 ret = snd_ctl_led_request();
2111 EXPORT_SYMBOL(snd_sof_load_topology);