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
39 /* size of tplg abi in byte */
40 #define SOF_TPLG_ABI_SIZE 3
43 * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the
45 * @scomp: pointer to SOC component
46 * @object: target IPC struct to save the parsed values
47 * @token_id: token ID for the token array to be searched
48 * @tuples: pointer to the tuples array
49 * @num_tuples: number of tuples in the tuples array
50 * @object_size: size of the object
51 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
52 * looks for @token_instance_num of each token in the token array associated
55 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
56 struct snd_sof_tuple *tuples, int num_tuples,
57 size_t object_size, int token_instance_num)
59 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
60 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
61 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
62 const struct sof_topology_token *tokens;
65 if (token_list[token_id].count < 0) {
66 dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
70 /* No tokens to match */
71 if (!token_list[token_id].count)
74 tokens = token_list[token_id].tokens;
76 dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
80 for (i = 0; i < token_list[token_id].count; i++) {
82 int num_tokens_matched = 0;
84 for (j = 0; j < num_tuples; j++) {
85 if (tokens[i].token == tuples[j].token) {
86 switch (tokens[i].type) {
87 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
89 u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
92 *val = tuples[j].value.v;
95 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
96 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
98 u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
101 *val = (u16)tuples[j].value.v;
104 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
106 if (!tokens[i].get_token) {
108 "get_token not defined for token %d in %s\n",
109 tokens[i].token, token_list[token_id].name);
113 tokens[i].get_token((void *)tuples[j].value.s, object,
114 tokens[i].offset + offset);
121 num_tokens_matched++;
123 /* found all required sets of current token. Move to the next one */
124 if (!(num_tokens_matched % token_instance_num))
127 /* move to the next object */
128 offset += object_size;
136 static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
138 /* we only support dB scale TLV type at the moment */
139 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
142 /* min value in topology tlv data is multiplied by 100 */
143 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
146 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
150 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
151 TLV_DB_SCALE_MUTE) == 0)
160 * Function to truncate an unsigned 64-bit number
161 * by x bits and return 32-bit unsigned number. This
162 * function also takes care of rounding while truncating
164 static inline u32 vol_shift_64(u64 i, u32 x)
166 /* do not truncate more than 32 bits */
173 return (u32)(((i >> (x - 1)) + 1) >> 1);
177 * Function to compute a ^ exp where,
178 * a is a fractional number represented by a fixed-point
179 * integer with a fractional world length of "fwl"
181 * fwl is the fractional word length
182 * Return value is a fractional number represented by a
183 * fixed-point integer with a fractional word length of "fwl"
185 static u32 vol_pow32(u32 a, int exp, u32 fwl)
188 u32 power = 1 << fwl;
191 /* if exponent is 0, return 1 */
195 /* determine the number of iterations based on the exponent */
201 /* mutiply a "iter" times to compute power */
202 for (i = 0; i < iter; i++) {
204 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
205 * Truncate product back to fwl fractional bits with rounding
207 power = vol_shift_64((u64)power * a, fwl);
211 /* if exp is positive, return the result */
215 /* if exp is negative, return the multiplicative inverse */
216 numerator = (u64)1 << (fwl << 1);
217 do_div(numerator, power);
219 return (u32)numerator;
223 * Function to calculate volume gain from TLV data.
224 * This function can only handle gain steps that are multiples of 0.5 dB
226 u32 vol_compute_gain(u32 value, int *tlv)
233 if (value == 0 && tlv[TLV_MUTE])
237 * compute dB gain from tlv. tlv_step
238 * in topology is multiplied by 100
240 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
243 * compute linear gain represented by fixed-point
244 * int with VOLUME_FWL fractional bits
246 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
248 /* extract the fractional part of volume step */
249 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
251 /* if volume step is an odd multiple of 0.5 dB */
252 if (f_step == VOL_HALF_DB_STEP && (value & 1))
253 linear_gain = vol_shift_64((u64)linear_gain *
254 VOL_FORTIETH_ROOT_OF_TEN,
261 * Set up volume table for kcontrols from tlv data
262 * "size" specifies the number of entries in the table
264 static int set_up_volume_table(struct snd_sof_control *scontrol,
265 int tlv[SOF_TLV_ITEMS], int size)
267 struct snd_soc_component *scomp = scontrol->scomp;
268 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
269 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
271 if (tplg_ops->control->set_up_volume_table)
272 return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
274 dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
278 struct sof_dai_types {
280 enum sof_ipc_dai_type type;
283 static const struct sof_dai_types sof_dais[] = {
284 {"SSP", SOF_DAI_INTEL_SSP},
285 {"HDA", SOF_DAI_INTEL_HDA},
286 {"DMIC", SOF_DAI_INTEL_DMIC},
287 {"ALH", SOF_DAI_INTEL_ALH},
288 {"SAI", SOF_DAI_IMX_SAI},
289 {"ESAI", SOF_DAI_IMX_ESAI},
290 {"ACP", SOF_DAI_AMD_BT},
291 {"ACPSP", SOF_DAI_AMD_SP},
292 {"ACPDMIC", SOF_DAI_AMD_DMIC},
293 {"AFE", SOF_DAI_MEDIATEK_AFE},
296 static enum sof_ipc_dai_type find_dai(const char *name)
300 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
301 if (strcmp(name, sof_dais[i].name) == 0)
302 return sof_dais[i].type;
305 return SOF_DAI_INTEL_NONE;
309 * Supported Frame format types and lookup, add new ones to end of list.
312 struct sof_frame_types {
314 enum sof_ipc_frame frame;
317 static const struct sof_frame_types sof_frames[] = {
318 {"s16le", SOF_IPC_FRAME_S16_LE},
319 {"s24le", SOF_IPC_FRAME_S24_4LE},
320 {"s32le", SOF_IPC_FRAME_S32_LE},
321 {"float", SOF_IPC_FRAME_FLOAT},
324 static enum sof_ipc_frame find_format(const char *name)
328 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
329 if (strcmp(name, sof_frames[i].name) == 0)
330 return sof_frames[i].frame;
333 /* use s32le if nothing is specified */
334 return SOF_IPC_FRAME_S32_LE;
337 int get_token_u32(void *elem, void *object, u32 offset)
339 struct snd_soc_tplg_vendor_value_elem *velem = elem;
340 u32 *val = (u32 *)((u8 *)object + offset);
342 *val = le32_to_cpu(velem->value);
346 int get_token_u16(void *elem, void *object, u32 offset)
348 struct snd_soc_tplg_vendor_value_elem *velem = elem;
349 u16 *val = (u16 *)((u8 *)object + offset);
351 *val = (u16)le32_to_cpu(velem->value);
355 int get_token_uuid(void *elem, void *object, u32 offset)
357 struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
358 u8 *dst = (u8 *)object + offset;
360 memcpy(dst, velem->uuid, UUID_SIZE);
365 int get_token_comp_format(void *elem, void *object, u32 offset)
367 u32 *val = (u32 *)((u8 *)object + offset);
369 *val = find_format((const char *)elem);
373 int get_token_dai_type(void *elem, void *object, u32 offset)
375 u32 *val = (u32 *)((u8 *)object + offset);
377 *val = find_dai((const char *)elem);
382 static const struct sof_topology_token stream_tokens[] = {
383 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
384 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
385 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
386 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
390 static const struct sof_topology_token led_tokens[] = {
391 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
392 offsetof(struct snd_sof_led_control, use_led)},
393 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
394 offsetof(struct snd_sof_led_control, direction)},
398 * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
399 * @scomp: pointer to soc component
400 * @object: target ipc struct for parsed values
401 * @offset: offset within the object pointer
402 * @tokens: array of struct sof_topology_token containing the tokens to be matched
403 * @num_tokens: number of tokens in tokens array
404 * @array: source pointer to consecutive vendor arrays in topology
406 * This function parses multiple sets of string type tokens in vendor arrays
408 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
409 void *object, size_t offset,
410 const struct sof_topology_token *tokens, int num_tokens,
411 struct snd_soc_tplg_vendor_array *array)
413 struct snd_soc_tplg_vendor_uuid_elem *elem;
417 /* parse element by element */
418 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
419 elem = &array->uuid[i];
421 /* search for token */
422 for (j = 0; j < num_tokens; j++) {
423 /* match token type */
424 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
428 if (tokens[j].token != le32_to_cpu(elem->token))
431 /* matched - now load token */
432 tokens[j].get_token(elem, object,
433 offset + tokens[j].offset);
443 * sof_copy_tuples - Parse tokens and copy them to the @tuples array
444 * @sdev: pointer to struct snd_sof_dev
445 * @array: source pointer to consecutive vendor arrays in topology
446 * @array_size: size of @array
447 * @token_id: Token ID associated with a token array
448 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
449 * looks for @token_instance_num of each token in the token array associated
451 * @tuples: tuples array to copy the matched tuples to
452 * @tuples_size: size of @tuples
453 * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
456 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
457 int array_size, u32 token_id, int token_instance_num,
458 struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
460 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
461 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
462 const struct sof_topology_token *tokens;
464 int num_tokens, asize;
467 /* nothing to do if token_list is NULL */
471 if (!tuples || !num_copied_tuples) {
472 dev_err(sdev->dev, "Invalid tuples array\n");
476 tokens = token_list[token_id].tokens;
477 num_tokens = token_list[token_id].count;
480 dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
484 /* check if there's space in the tuples array for new tokens */
485 if (*num_copied_tuples >= tuples_size) {
486 dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
487 token_list[token_id].name);
491 while (array_size > 0 && found < num_tokens * token_instance_num) {
492 asize = le32_to_cpu(array->size);
496 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
500 /* make sure there is enough data before parsing */
502 if (array_size < 0) {
503 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
507 /* parse element by element */
508 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
509 /* search for token */
510 for (j = 0; j < num_tokens; j++) {
511 /* match token type */
512 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
513 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
514 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
515 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
516 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
519 if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
520 struct snd_soc_tplg_vendor_string_elem *elem;
522 elem = &array->string[i];
525 if (tokens[j].token != le32_to_cpu(elem->token))
528 tuples[*num_copied_tuples].token = tokens[j].token;
529 tuples[*num_copied_tuples].value.s = elem->string;
531 struct snd_soc_tplg_vendor_value_elem *elem;
533 elem = &array->value[i];
536 if (tokens[j].token != le32_to_cpu(elem->token))
539 tuples[*num_copied_tuples].token = tokens[j].token;
540 tuples[*num_copied_tuples].value.v =
541 le32_to_cpu(elem->value);
544 (*num_copied_tuples)++;
546 /* stop if there's no space for any more new tuples */
547 if (*num_copied_tuples == tuples_size)
553 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
560 * sof_parse_string_tokens - Parse multiple sets of tokens
561 * @scomp: pointer to soc component
562 * @object: target ipc struct for parsed values
563 * @offset: offset within the object pointer
564 * @tokens: array of struct sof_topology_token containing the tokens to be matched
565 * @num_tokens: number of tokens in tokens array
566 * @array: source pointer to consecutive vendor arrays in topology
568 * This function parses multiple sets of string type tokens in vendor arrays
570 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
571 void *object, int offset,
572 const struct sof_topology_token *tokens, int num_tokens,
573 struct snd_soc_tplg_vendor_array *array)
575 struct snd_soc_tplg_vendor_string_elem *elem;
579 /* parse element by element */
580 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
581 elem = &array->string[i];
583 /* search for token */
584 for (j = 0; j < num_tokens; j++) {
585 /* match token type */
586 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
590 if (tokens[j].token != le32_to_cpu(elem->token))
593 /* matched - now load token */
594 tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
604 * sof_parse_word_tokens - Parse multiple sets of tokens
605 * @scomp: pointer to soc component
606 * @object: target ipc struct for parsed values
607 * @offset: offset within the object pointer
608 * @tokens: array of struct sof_topology_token containing the tokens to be matched
609 * @num_tokens: number of tokens in tokens array
610 * @array: source pointer to consecutive vendor arrays in topology
612 * This function parses multiple sets of word type tokens in vendor arrays
614 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
615 void *object, int offset,
616 const struct sof_topology_token *tokens, int num_tokens,
617 struct snd_soc_tplg_vendor_array *array)
619 struct snd_soc_tplg_vendor_value_elem *elem;
623 /* parse element by element */
624 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
625 elem = &array->value[i];
627 /* search for token */
628 for (j = 0; j < num_tokens; j++) {
629 /* match token type */
630 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
631 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
632 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
633 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
637 if (tokens[j].token != le32_to_cpu(elem->token))
641 tokens[j].get_token(elem, object, offset + tokens[j].offset);
651 * sof_parse_token_sets - Parse multiple sets of tokens
652 * @scomp: pointer to soc component
653 * @object: target ipc struct for parsed values
654 * @tokens: token definition array describing what tokens to parse
655 * @count: number of tokens in definition array
656 * @array: source pointer to consecutive vendor arrays in topology
657 * @array_size: total size of @array
658 * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
659 * looks for @token_instance_num of each token in the @tokens
660 * @object_size: offset to next target ipc struct with multiple sets
662 * This function parses multiple sets of tokens in vendor arrays into
663 * consecutive ipc structs.
665 static int sof_parse_token_sets(struct snd_soc_component *scomp,
666 void *object, const struct sof_topology_token *tokens,
667 int count, struct snd_soc_tplg_vendor_array *array,
668 int array_size, int token_instance_num, size_t object_size)
675 while (array_size > 0 && total < count * token_instance_num) {
676 asize = le32_to_cpu(array->size);
679 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
680 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
685 /* make sure there is enough data before parsing */
687 if (array_size < 0) {
688 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
693 /* call correct parser depending on type */
694 switch (le32_to_cpu(array->type)) {
695 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
696 found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
699 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
700 found += sof_parse_string_tokens(scomp, object, offset, tokens, count,
703 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
704 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
705 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
706 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
707 found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
711 dev_err(scomp->dev, "error: unknown token type %d\n",
717 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
720 /* move to next target struct */
721 if (found >= count) {
722 offset += object_size;
732 * sof_parse_tokens - Parse one set of tokens
733 * @scomp: pointer to soc component
734 * @object: target ipc struct for parsed values
735 * @tokens: token definition array describing what tokens to parse
736 * @num_tokens: number of tokens in definition array
737 * @array: source pointer to consecutive vendor arrays in topology
738 * @array_size: total size of @array
740 * This function parses a single set of tokens in vendor arrays into
741 * consecutive ipc structs.
743 static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
744 const struct sof_topology_token *tokens, int num_tokens,
745 struct snd_soc_tplg_vendor_array *array,
750 * sof_parse_tokens is used when topology contains only a single set of
751 * identical tuples arrays. So additional parameters to
752 * sof_parse_token_sets are sets = 1 (only 1 set) and
753 * object_size = 0 (irrelevant).
755 return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
760 * Standard Kcontrols.
763 static int sof_control_load_volume(struct snd_soc_component *scomp,
764 struct snd_sof_control *scontrol,
765 struct snd_kcontrol_new *kc,
766 struct snd_soc_tplg_ctl_hdr *hdr)
768 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
769 struct snd_soc_tplg_mixer_control *mc =
770 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
771 int tlv[SOF_TLV_ITEMS];
775 /* validate topology data */
776 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
780 * If control has more than 2 channels we need to override the info. This is because even if
781 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
782 * pre-defined dapm control types (and related functions) creating the actual control
783 * restrict the channels only to mono or stereo.
785 if (le32_to_cpu(mc->num_channels) > 2)
786 kc->info = snd_sof_volume_info;
788 scontrol->comp_id = sdev->next_comp_id;
789 scontrol->min_volume_step = le32_to_cpu(mc->min);
790 scontrol->max_volume_step = le32_to_cpu(mc->max);
791 scontrol->num_channels = le32_to_cpu(mc->num_channels);
793 scontrol->max = le32_to_cpu(mc->max);
794 if (le32_to_cpu(mc->max) == 1)
797 /* extract tlv data */
798 if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
799 dev_err(scomp->dev, "error: invalid TLV data\n");
803 /* set up volume table */
804 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
806 dev_err(scomp->dev, "error: setting up volume table\n");
811 /* set up possible led control from mixer private data */
812 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
813 ARRAY_SIZE(led_tokens), mc->priv.array,
814 le32_to_cpu(mc->priv.size));
816 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
817 le32_to_cpu(mc->priv.size));
821 if (scontrol->led_ctl.use_led) {
822 mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
823 SNDRV_CTL_ELEM_ACCESS_SPK_LED;
824 scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
825 scontrol->access |= mask;
826 kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
828 sdev->led_present = true;
831 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
832 scontrol->comp_id, scontrol->num_channels);
837 if (le32_to_cpu(mc->max) > 1)
838 kfree(scontrol->volume_table);
843 static int sof_control_load_enum(struct snd_soc_component *scomp,
844 struct snd_sof_control *scontrol,
845 struct snd_kcontrol_new *kc,
846 struct snd_soc_tplg_ctl_hdr *hdr)
848 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
849 struct snd_soc_tplg_enum_control *ec =
850 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
852 /* validate topology data */
853 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
856 scontrol->comp_id = sdev->next_comp_id;
857 scontrol->num_channels = le32_to_cpu(ec->num_channels);
859 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
860 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
865 static int sof_control_load_bytes(struct snd_soc_component *scomp,
866 struct snd_sof_control *scontrol,
867 struct snd_kcontrol_new *kc,
868 struct snd_soc_tplg_ctl_hdr *hdr)
870 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
871 struct snd_soc_tplg_bytes_control *control =
872 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
873 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
874 size_t priv_size = le32_to_cpu(control->priv.size);
876 scontrol->max_size = sbe->max;
877 scontrol->comp_id = sdev->next_comp_id;
879 dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
881 /* copy the private data */
883 scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
887 scontrol->priv_size = priv_size;
893 /* external kcontrol init - used for any driver specific init */
894 static int sof_control_load(struct snd_soc_component *scomp, int index,
895 struct snd_kcontrol_new *kc,
896 struct snd_soc_tplg_ctl_hdr *hdr)
898 struct soc_mixer_control *sm;
899 struct soc_bytes_ext *sbe;
901 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
902 struct snd_soc_dobj *dobj;
903 struct snd_sof_control *scontrol;
906 dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
907 hdr->type, hdr->name);
909 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
913 scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
914 if (!scontrol->name) {
919 scontrol->scomp = scomp;
920 scontrol->access = kc->access;
921 scontrol->info_type = le32_to_cpu(hdr->ops.info);
922 scontrol->index = kc->index;
924 switch (le32_to_cpu(hdr->ops.info)) {
925 case SND_SOC_TPLG_CTL_VOLSW:
926 case SND_SOC_TPLG_CTL_VOLSW_SX:
927 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
928 sm = (struct soc_mixer_control *)kc->private_value;
930 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
932 case SND_SOC_TPLG_CTL_BYTES:
933 sbe = (struct soc_bytes_ext *)kc->private_value;
935 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
937 case SND_SOC_TPLG_CTL_ENUM:
938 case SND_SOC_TPLG_CTL_ENUM_VALUE:
939 se = (struct soc_enum *)kc->private_value;
941 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
943 case SND_SOC_TPLG_CTL_RANGE:
944 case SND_SOC_TPLG_CTL_STROBE:
945 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
946 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
947 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
948 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
949 case SND_SOC_TPLG_DAPM_CTL_PIN:
951 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
952 hdr->ops.get, hdr->ops.put, hdr->ops.info);
953 kfree(scontrol->name);
959 kfree(scontrol->name);
964 scontrol->led_ctl.led_value = -1;
966 dobj->private = scontrol;
967 list_add(&scontrol->list, &sdev->kcontrol_list);
971 static int sof_control_unload(struct snd_soc_component *scomp,
972 struct snd_soc_dobj *dobj)
974 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
975 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
976 struct snd_sof_control *scontrol = dobj->private;
979 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
981 if (ipc_tplg_ops->control_free) {
982 ret = ipc_tplg_ops->control_free(sdev, scontrol);
984 dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
987 /* free all data before returning in case of error too */
988 kfree(scontrol->ipc_control_data);
989 kfree(scontrol->priv);
990 kfree(scontrol->name);
991 list_del(&scontrol->list);
1001 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1002 struct snd_soc_dapm_widget *w,
1003 struct snd_soc_tplg_dapm_widget *tw,
1004 struct snd_sof_dai *dai)
1006 struct snd_soc_card *card = scomp->card;
1007 struct snd_soc_pcm_runtime *rtd;
1008 struct snd_soc_dai *cpu_dai;
1012 dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
1016 list_for_each_entry(rtd, &card->rtd_list, list) {
1017 dev_vdbg(scomp->dev, "tplg: check widget: %s stream: %s dai stream: %s\n",
1018 w->name, w->sname, rtd->dai_link->stream_name);
1020 /* does stream match DAI link ? */
1021 if (!rtd->dai_link->stream_name ||
1022 strcmp(w->sname, rtd->dai_link->stream_name))
1026 case snd_soc_dapm_dai_out:
1027 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1029 * Please create DAI widget in the right order
1030 * to ensure BE will connect to the right DAI
1033 if (!cpu_dai->capture_widget) {
1034 cpu_dai->capture_widget = w;
1038 if (i == rtd->num_cpus) {
1039 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1044 dai->name = rtd->dai_link->name;
1045 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1046 w->name, rtd->dai_link->name);
1048 case snd_soc_dapm_dai_in:
1049 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1051 * Please create DAI widget in the right order
1052 * to ensure BE will connect to the right DAI
1055 if (!cpu_dai->playback_widget) {
1056 cpu_dai->playback_widget = w;
1060 if (i == rtd->num_cpus) {
1061 dev_err(scomp->dev, "error: can't find BE for DAI %s\n",
1066 dai->name = rtd->dai_link->name;
1067 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1068 w->name, rtd->dai_link->name);
1075 /* check we have a connection */
1077 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1085 static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
1086 struct snd_soc_dapm_widget *w)
1088 struct snd_soc_card *card = scomp->card;
1089 struct snd_soc_pcm_runtime *rtd;
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(w->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_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
1145 struct snd_soc_tplg_dapm_widget *tw,
1146 enum sof_tokens *object_token_list, int count)
1148 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1149 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1150 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
1151 struct snd_soc_tplg_private *private = &tw->priv;
1155 if (count > 0 && !object_token_list) {
1156 dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
1160 /* calculate max size of tuples array */
1161 for (i = 0; i < count; i++)
1162 num_tuples += token_list[object_token_list[i]].count;
1164 /* allocate memory for tuples array */
1165 swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
1166 if (!swidget->tuples)
1169 /* parse token list for widget */
1170 for (i = 0; i < count; i++) {
1171 if (object_token_list[i] >= SOF_TOKEN_COUNT) {
1172 dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
1173 object_token_list[i], swidget->widget->name);
1178 /* parse and save UUID in swidget */
1179 if (object_token_list[i] == SOF_COMP_EXT_TOKENS) {
1180 ret = sof_parse_tokens(scomp, swidget,
1181 token_list[object_token_list[i]].tokens,
1182 token_list[object_token_list[i]].count,
1183 private->array, le32_to_cpu(private->size));
1185 dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
1186 token_list[object_token_list[i]].name,
1187 swidget->widget->name);
1194 /* copy one set of tuples per token ID into swidget->tuples */
1195 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1196 object_token_list[i], 1, swidget->tuples,
1197 num_tuples, &swidget->num_tuples);
1199 dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
1200 token_list[object_token_list[i]].name, swidget->widget->name, ret);
1207 kfree(swidget->tuples);
1211 static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
1218 for (i = 0; i < num_tuples; i++) {
1219 if (tuples[i].token == token_id)
1220 return tuples[i].value.v;
1226 /* external widget init - used for any driver specific init */
1227 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
1228 struct snd_soc_dapm_widget *w,
1229 struct snd_soc_tplg_dapm_widget *tw)
1231 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1232 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1233 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1234 struct snd_sof_widget *swidget;
1235 struct snd_sof_dai *dai;
1236 enum sof_tokens *token_list;
1237 int token_list_size;
1240 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
1244 swidget->scomp = scomp;
1245 swidget->widget = w;
1246 swidget->comp_id = sdev->next_comp_id++;
1247 swidget->complete = 0;
1248 swidget->id = w->id;
1249 swidget->pipeline_id = index;
1250 swidget->private = NULL;
1252 dev_dbg(scomp->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n",
1253 swidget->comp_id, index, swidget->id, tw->name,
1254 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1255 ? tw->sname : "none");
1257 token_list = widget_ops[w->id].token_list;
1258 token_list_size = widget_ops[w->id].token_list_size;
1260 /* handle any special case widgets */
1262 case snd_soc_dapm_dai_in:
1263 case snd_soc_dapm_dai_out:
1264 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
1271 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1273 ret = sof_connect_dai_widget(scomp, w, tw, dai);
1278 list_add(&dai->list, &sdev->dai_list);
1279 swidget->private = dai;
1281 case snd_soc_dapm_effect:
1282 /* check we have some tokens - we need at least process type */
1283 if (le32_to_cpu(tw->priv.size) == 0) {
1284 dev_err(scomp->dev, "error: process tokens not found\n");
1288 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1290 case snd_soc_dapm_pga:
1291 if (!le32_to_cpu(tw->num_kcontrols)) {
1292 dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
1299 case snd_soc_dapm_mixer:
1300 case snd_soc_dapm_buffer:
1301 case snd_soc_dapm_scheduler:
1302 case snd_soc_dapm_aif_out:
1303 case snd_soc_dapm_aif_in:
1304 case snd_soc_dapm_src:
1305 case snd_soc_dapm_asrc:
1306 case snd_soc_dapm_siggen:
1307 case snd_soc_dapm_mux:
1308 case snd_soc_dapm_demux:
1309 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1311 case snd_soc_dapm_switch:
1312 case snd_soc_dapm_dai_link:
1313 case snd_soc_dapm_kcontrol:
1315 dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
1319 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
1320 swidget->core = SOF_DSP_PRIMARY_CORE;
1322 int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
1323 swidget->num_tuples);
1326 swidget->core = core;
1329 /* check token parsing reply */
1332 "error: failed to add widget id %d type %d name : %s stream %s\n",
1333 tw->shift, swidget->id, tw->name,
1334 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1335 ? tw->sname : "none");
1340 /* bind widget to external event */
1341 if (tw->event_type) {
1342 if (widget_ops[w->id].bind_event) {
1343 ret = widget_ops[w->id].bind_event(scomp, swidget,
1344 le16_to_cpu(tw->event_type));
1346 dev_err(scomp->dev, "widget event binding failed for %s\n",
1347 swidget->widget->name);
1348 kfree(swidget->private);
1349 kfree(swidget->tuples);
1356 w->dobj.private = swidget;
1357 list_add(&swidget->list, &sdev->widget_list);
1361 static int sof_route_unload(struct snd_soc_component *scomp,
1362 struct snd_soc_dobj *dobj)
1364 struct snd_sof_route *sroute;
1366 sroute = dobj->private;
1370 /* free sroute and its private data */
1371 kfree(sroute->private);
1372 list_del(&sroute->list);
1378 static int sof_widget_unload(struct snd_soc_component *scomp,
1379 struct snd_soc_dobj *dobj)
1381 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1382 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1383 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1384 const struct snd_kcontrol_new *kc;
1385 struct snd_soc_dapm_widget *widget;
1386 struct snd_sof_control *scontrol;
1387 struct snd_sof_widget *swidget;
1388 struct soc_mixer_control *sm;
1389 struct soc_bytes_ext *sbe;
1390 struct snd_sof_dai *dai;
1391 struct soc_enum *se;
1395 swidget = dobj->private;
1399 widget = swidget->widget;
1401 switch (swidget->id) {
1402 case snd_soc_dapm_dai_in:
1403 case snd_soc_dapm_dai_out:
1404 dai = swidget->private;
1407 list_del(&dai->list);
1409 sof_disconnect_dai_widget(scomp, widget);
1415 for (i = 0; i < widget->num_kcontrols; i++) {
1416 kc = &widget->kcontrol_news[i];
1417 switch (widget->dobj.widget.kcontrol_type[i]) {
1418 case SND_SOC_TPLG_TYPE_MIXER:
1419 sm = (struct soc_mixer_control *)kc->private_value;
1420 scontrol = sm->dobj.private;
1422 kfree(scontrol->volume_table);
1424 case SND_SOC_TPLG_TYPE_ENUM:
1425 se = (struct soc_enum *)kc->private_value;
1426 scontrol = se->dobj.private;
1428 case SND_SOC_TPLG_TYPE_BYTES:
1429 sbe = (struct soc_bytes_ext *)kc->private_value;
1430 scontrol = sbe->dobj.private;
1433 dev_warn(scomp->dev, "unsupported kcontrol_type\n");
1436 kfree(scontrol->ipc_control_data);
1437 list_del(&scontrol->list);
1438 kfree(scontrol->name);
1443 /* free IPC related data */
1444 if (widget_ops[swidget->id].ipc_free)
1445 widget_ops[swidget->id].ipc_free(swidget);
1447 kfree(swidget->tuples);
1449 /* remove and free swidget object */
1450 list_del(&swidget->list);
1457 * DAI HW configuration.
1460 /* FE DAI - used for any driver specific init */
1461 static int sof_dai_load(struct snd_soc_component *scomp, int index,
1462 struct snd_soc_dai_driver *dai_drv,
1463 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
1465 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1466 struct snd_soc_tplg_stream_caps *caps;
1467 struct snd_soc_tplg_private *private = &pcm->priv;
1468 struct snd_sof_pcm *spcm;
1472 /* nothing to do for BEs atm */
1476 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
1480 spcm->scomp = scomp;
1482 for_each_pcm_streams(stream) {
1483 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
1485 snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1487 snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1491 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
1493 dai_drv->dobj.private = spcm;
1494 list_add(&spcm->list, &sdev->pcm_list);
1496 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
1497 ARRAY_SIZE(stream_tokens), private->array,
1498 le32_to_cpu(private->size));
1500 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
1501 le32_to_cpu(private->size));
1505 /* do we need to allocate playback PCM DMA pages */
1506 if (!spcm->pcm.playback)
1509 stream = SNDRV_PCM_STREAM_PLAYBACK;
1511 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: playback d0i3:%d\n",
1512 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
1514 caps = &spcm->pcm.caps[stream];
1516 /* allocate playback page table buffer */
1517 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1518 PAGE_SIZE, &spcm->stream[stream].page_table);
1520 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1526 /* bind pcm to host comp */
1527 ret = spcm_bind(scomp, spcm, stream);
1530 "error: can't bind pcm to host\n");
1531 goto free_playback_tables;
1535 stream = SNDRV_PCM_STREAM_CAPTURE;
1537 /* do we need to allocate capture PCM DMA pages */
1538 if (!spcm->pcm.capture)
1541 dev_vdbg(scomp->dev, "tplg: pcm %s stream tokens: capture d0i3:%d\n",
1542 spcm->pcm.pcm_name, spcm->stream[stream].d0i3_compatible);
1544 caps = &spcm->pcm.caps[stream];
1546 /* allocate capture page table buffer */
1547 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1548 PAGE_SIZE, &spcm->stream[stream].page_table);
1550 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1552 goto free_playback_tables;
1555 /* bind pcm to host comp */
1556 ret = spcm_bind(scomp, spcm, stream);
1559 "error: can't bind pcm to host\n");
1560 snd_dma_free_pages(&spcm->stream[stream].page_table);
1561 goto free_playback_tables;
1566 free_playback_tables:
1567 if (spcm->pcm.playback)
1568 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1573 static int sof_dai_unload(struct snd_soc_component *scomp,
1574 struct snd_soc_dobj *dobj)
1576 struct snd_sof_pcm *spcm = dobj->private;
1578 /* free PCM DMA pages */
1579 if (spcm->pcm.playback)
1580 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1582 if (spcm->pcm.capture)
1583 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
1585 /* remove from list and free spcm */
1586 list_del(&spcm->list);
1592 static const struct sof_topology_token common_dai_link_tokens[] = {
1593 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
1594 offsetof(struct snd_sof_dai_link, type)},
1597 /* DAI link - used for any driver specific init */
1598 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
1599 struct snd_soc_tplg_link_config *cfg)
1601 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1602 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1603 const struct sof_token_info *token_list = ipc_tplg_ops->token_list;
1604 struct snd_soc_tplg_private *private = &cfg->priv;
1605 struct snd_sof_dai_link *slink;
1610 if (!link->platforms) {
1611 dev_err(scomp->dev, "error: no platforms\n");
1614 link->platforms->name = dev_name(scomp->dev);
1617 * Set nonatomic property for FE dai links as their trigger action
1620 if (!link->no_pcm) {
1621 link->nonatomic = true;
1624 * set default trigger order for all links. Exceptions to
1625 * the rule will be handled in sof_pcm_dai_link_fixup()
1626 * For playback, the sequence is the following: start FE,
1627 * start BE, stop BE, stop FE; for Capture the sequence is
1628 * inverted start BE, start FE, stop FE, stop BE
1630 link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
1631 SND_SOC_DPCM_TRIGGER_PRE;
1632 link->trigger[SNDRV_PCM_STREAM_CAPTURE] =
1633 SND_SOC_DPCM_TRIGGER_POST;
1635 /* nothing more to do for FE dai links */
1639 /* check we have some tokens - we need at least DAI type */
1640 if (le32_to_cpu(private->size) == 0) {
1641 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
1645 slink = kzalloc(sizeof(*slink), GFP_KERNEL);
1649 slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
1650 slink->hw_configs = kmemdup(cfg->hw_config,
1651 sizeof(*slink->hw_configs) * slink->num_hw_configs,
1653 if (!slink->hw_configs) {
1658 slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
1661 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
1662 slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
1664 ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
1665 ARRAY_SIZE(common_dai_link_tokens),
1666 private->array, le32_to_cpu(private->size));
1668 dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
1669 kfree(slink->hw_configs);
1677 /* calculate size of tuples array */
1678 num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
1679 num_sets = slink->num_hw_configs;
1680 switch (slink->type) {
1681 case SOF_DAI_INTEL_SSP:
1682 token_id = SOF_SSP_TOKENS;
1683 num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
1685 case SOF_DAI_INTEL_DMIC:
1686 token_id = SOF_DMIC_TOKENS;
1687 num_tuples += token_list[SOF_DMIC_TOKENS].count;
1689 /* Allocate memory for max PDM controllers */
1690 num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
1692 case SOF_DAI_INTEL_HDA:
1693 token_id = SOF_HDA_TOKENS;
1694 num_tuples += token_list[SOF_HDA_TOKENS].count;
1696 case SOF_DAI_INTEL_ALH:
1697 token_id = SOF_ALH_TOKENS;
1698 num_tuples += token_list[SOF_ALH_TOKENS].count;
1700 case SOF_DAI_IMX_SAI:
1701 token_id = SOF_SAI_TOKENS;
1702 num_tuples += token_list[SOF_SAI_TOKENS].count;
1704 case SOF_DAI_IMX_ESAI:
1705 token_id = SOF_ESAI_TOKENS;
1706 num_tuples += token_list[SOF_ESAI_TOKENS].count;
1708 case SOF_DAI_MEDIATEK_AFE:
1709 token_id = SOF_AFE_TOKENS;
1710 num_tuples += token_list[SOF_AFE_TOKENS].count;
1716 /* allocate memory for tuples array */
1717 slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
1718 if (!slink->tuples) {
1719 kfree(slink->hw_configs);
1724 if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
1725 /* parse one set of DAI link tokens */
1726 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1727 SOF_DAI_LINK_TOKENS, 1, slink->tuples,
1728 num_tuples, &slink->num_tuples);
1730 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1731 token_list[SOF_DAI_LINK_TOKENS].name, link->name);
1736 /* nothing more to do if there are no DAI type-specific tokens defined */
1737 if (!token_id || !token_list[token_id].tokens)
1740 /* parse "num_sets" sets of DAI-specific tokens */
1741 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1742 token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
1744 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1745 token_list[token_id].name, link->name);
1749 /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
1750 if (token_id == SOF_DMIC_TOKENS) {
1751 num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
1752 slink->tuples, slink->num_tuples);
1755 dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
1760 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1761 SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
1762 num_tuples, &slink->num_tuples);
1764 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1765 token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
1770 link->dobj.private = slink;
1771 list_add(&slink->list, &sdev->dai_link_list);
1776 kfree(slink->tuples);
1777 kfree(slink->hw_configs);
1783 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
1785 struct snd_sof_dai_link *slink = dobj->private;
1790 kfree(slink->tuples);
1791 list_del(&slink->list);
1792 kfree(slink->hw_configs);
1794 dobj->private = NULL;
1799 /* DAI link - used for any driver specific init */
1800 static int sof_route_load(struct snd_soc_component *scomp, int index,
1801 struct snd_soc_dapm_route *route)
1803 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1804 struct snd_sof_widget *source_swidget, *sink_swidget;
1805 struct snd_soc_dobj *dobj = &route->dobj;
1806 struct snd_sof_route *sroute;
1809 /* allocate memory for sroute and connect */
1810 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
1814 sroute->scomp = scomp;
1815 dev_dbg(scomp->dev, "sink %s control %s source %s\n",
1816 route->sink, route->control ? route->control : "none",
1819 /* source component */
1820 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
1821 if (!source_swidget) {
1822 dev_err(scomp->dev, "error: source %s not found\n",
1829 * Virtual widgets of type output/out_drv may be added in topology
1830 * for compatibility. These are not handled by the FW.
1831 * So, don't send routes whose source/sink widget is of such types
1834 if (source_swidget->id == snd_soc_dapm_out_drv ||
1835 source_swidget->id == snd_soc_dapm_output)
1838 /* sink component */
1839 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
1840 if (!sink_swidget) {
1841 dev_err(scomp->dev, "error: sink %s not found\n",
1848 * Don't send routes whose sink widget is of type
1849 * output or out_drv to the DSP
1851 if (sink_swidget->id == snd_soc_dapm_out_drv ||
1852 sink_swidget->id == snd_soc_dapm_output)
1855 sroute->route = route;
1856 dobj->private = sroute;
1857 sroute->src_widget = source_swidget;
1858 sroute->sink_widget = sink_swidget;
1860 /* add route to route list */
1861 list_add(&sroute->list, &sdev->route_list);
1870 * sof_set_pipe_widget - Set pipe_widget for a component
1871 * @sdev: pointer to struct snd_sof_dev
1872 * @pipe_widget: pointer to struct snd_sof_widget of type snd_soc_dapm_scheduler
1873 * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
1875 * Return: 0 if successful, -EINVAL on error.
1876 * The function checks if @swidget is associated with any volatile controls. If so, setting
1877 * the dynamic_pipeline_widget is disallowed.
1879 static int sof_set_pipe_widget(struct snd_sof_dev *sdev, struct snd_sof_widget *pipe_widget,
1880 struct snd_sof_widget *swidget)
1882 struct snd_sof_control *scontrol;
1884 if (pipe_widget->dynamic_pipeline_widget) {
1885 /* dynamic widgets cannot have volatile kcontrols */
1886 list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
1887 if (scontrol->comp_id == swidget->comp_id &&
1888 (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
1890 "error: volatile control found for dynamic widget %s\n",
1891 swidget->widget->name);
1896 /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
1897 swidget->pipe_widget = pipe_widget;
1898 swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
1903 /* completion - called at completion of firmware loading */
1904 static int sof_complete(struct snd_soc_component *scomp)
1906 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1907 struct snd_sof_widget *swidget, *comp_swidget;
1908 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
1909 const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
1910 struct snd_sof_control *scontrol;
1913 /* first update all control IPC structures based on the IPC version */
1914 if (ipc_tplg_ops->control_setup)
1915 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
1916 ret = ipc_tplg_ops->control_setup(sdev, scontrol);
1918 dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
1925 * then update all widget IPC structures. If any of the ipc_setup callbacks fail, the
1926 * topology will be removed and all widgets will be unloaded resulting in freeing all
1927 * associated memories.
1929 list_for_each_entry(swidget, &sdev->widget_list, list) {
1930 if (widget_ops[swidget->id].ipc_setup) {
1931 ret = widget_ops[swidget->id].ipc_setup(swidget);
1933 dev_err(sdev->dev, "failed updating IPC struct for %s\n",
1934 swidget->widget->name);
1940 /* set the pipe_widget and apply the dynamic_pipeline_widget_flag */
1941 list_for_each_entry(swidget, &sdev->widget_list, list) {
1942 switch (swidget->id) {
1943 case snd_soc_dapm_scheduler:
1945 * Apply the dynamic_pipeline_widget flag and set the pipe_widget field
1946 * for all widgets that have the same pipeline ID as the scheduler widget
1948 list_for_each_entry(comp_swidget, &sdev->widget_list, list)
1949 if (comp_swidget->pipeline_id == swidget->pipeline_id) {
1950 ret = sof_set_pipe_widget(sdev, swidget, comp_swidget);
1960 /* verify topology components loading including dynamic pipelines */
1961 if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
1962 if (ipc_tplg_ops->set_up_all_pipelines && ipc_tplg_ops->tear_down_all_pipelines) {
1963 ret = ipc_tplg_ops->set_up_all_pipelines(sdev, true);
1965 dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
1970 ret = ipc_tplg_ops->tear_down_all_pipelines(sdev, true);
1972 dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
1979 /* set up static pipelines */
1980 if (ipc_tplg_ops->set_up_all_pipelines)
1981 return ipc_tplg_ops->set_up_all_pipelines(sdev, false);
1986 /* manifest - optional to inform component of manifest */
1987 static int sof_manifest(struct snd_soc_component *scomp, int index,
1988 struct snd_soc_tplg_manifest *man)
1993 size = le32_to_cpu(man->priv.size);
1995 /* backward compatible with tplg without ABI info */
1997 dev_dbg(scomp->dev, "No topology ABI info\n");
2001 if (size != SOF_TPLG_ABI_SIZE) {
2002 dev_err(scomp->dev, "error: invalid topology ABI size\n");
2006 dev_info(scomp->dev,
2007 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
2008 man->priv.data[0], man->priv.data[1],
2009 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR,
2012 abi_version = SOF_ABI_VER(man->priv.data[0],
2016 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) {
2017 dev_err(scomp->dev, "error: incompatible topology ABI version\n");
2021 if (SOF_ABI_VERSION_MINOR(abi_version) > SOF_ABI_MINOR) {
2022 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) {
2023 dev_warn(scomp->dev, "warn: topology ABI is more recent than kernel\n");
2025 dev_err(scomp->dev, "error: topology ABI is more recent than kernel\n");
2033 /* vendor specific kcontrol handlers available for binding */
2034 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
2035 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
2036 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
2037 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
2038 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
2041 /* vendor specific bytes ext handlers available for binding */
2042 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
2043 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
2044 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
2047 static struct snd_soc_tplg_ops sof_tplg_ops = {
2048 /* external kcontrol init - used for any driver specific init */
2049 .control_load = sof_control_load,
2050 .control_unload = sof_control_unload,
2052 /* external kcontrol init - used for any driver specific init */
2053 .dapm_route_load = sof_route_load,
2054 .dapm_route_unload = sof_route_unload,
2056 /* external widget init - used for any driver specific init */
2057 /* .widget_load is not currently used */
2058 .widget_ready = sof_widget_ready,
2059 .widget_unload = sof_widget_unload,
2061 /* FE DAI - used for any driver specific init */
2062 .dai_load = sof_dai_load,
2063 .dai_unload = sof_dai_unload,
2065 /* DAI link - used for any driver specific init */
2066 .link_load = sof_link_load,
2067 .link_unload = sof_link_unload,
2069 /* completion - called at completion of firmware loading */
2070 .complete = sof_complete,
2072 /* manifest - optional to inform component of manifest */
2073 .manifest = sof_manifest,
2075 /* vendor specific kcontrol handlers available for binding */
2076 .io_ops = sof_io_ops,
2077 .io_ops_count = ARRAY_SIZE(sof_io_ops),
2079 /* vendor specific bytes ext handlers available for binding */
2080 .bytes_ext_ops = sof_bytes_ext_ops,
2081 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
2084 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
2086 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2087 const struct firmware *fw;
2090 dev_dbg(scomp->dev, "loading topology:%s\n", file);
2092 ret = request_firmware(&fw, file, scomp->dev);
2094 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2097 "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
2101 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
2103 dev_err(scomp->dev, "error: tplg component load failed %d\n",
2108 release_firmware(fw);
2110 if (ret >= 0 && sdev->led_present)
2111 ret = snd_ctl_led_request();
2115 EXPORT_SYMBOL(snd_sof_load_topology);