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 *tplg_ops = sof_ipc_get_ops(sdev, tplg);
58 const struct sof_token_info *token_list;
59 const struct sof_topology_token *tokens;
62 token_list = tplg_ops ? tplg_ops->token_list : NULL;
63 /* nothing to do if token_list is NULL */
67 if (token_list[token_id].count < 0) {
68 dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id);
72 /* No tokens to match */
73 if (!token_list[token_id].count)
76 tokens = token_list[token_id].tokens;
78 dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id);
82 for (i = 0; i < token_list[token_id].count; i++) {
84 int num_tokens_matched = 0;
86 for (j = 0; j < num_tuples; j++) {
87 if (tokens[i].token == tuples[j].token) {
88 switch (tokens[i].type) {
89 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
91 u32 *val = (u32 *)((u8 *)object + tokens[i].offset +
94 *val = tuples[j].value.v;
97 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
98 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
100 u16 *val = (u16 *)((u8 *)object + tokens[i].offset +
103 *val = (u16)tuples[j].value.v;
106 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
108 if (!tokens[i].get_token) {
110 "get_token not defined for token %d in %s\n",
111 tokens[i].token, token_list[token_id].name);
115 tokens[i].get_token((void *)tuples[j].value.s, object,
116 tokens[i].offset + offset);
123 num_tokens_matched++;
125 /* found all required sets of current token. Move to the next one */
126 if (!(num_tokens_matched % token_instance_num))
129 /* move to the next object */
130 offset += object_size;
138 static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS])
140 /* we only support dB scale TLV type at the moment */
141 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
144 /* min value in topology tlv data is multiplied by 100 */
145 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100;
148 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
152 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] &
153 TLV_DB_SCALE_MUTE) == 0)
162 * Function to truncate an unsigned 64-bit number
163 * by x bits and return 32-bit unsigned number. This
164 * function also takes care of rounding while truncating
166 static inline u32 vol_shift_64(u64 i, u32 x)
168 /* do not truncate more than 32 bits */
175 return (u32)(((i >> (x - 1)) + 1) >> 1);
179 * Function to compute a ^ exp where,
180 * a is a fractional number represented by a fixed-point
181 * integer with a fractional world length of "fwl"
183 * fwl is the fractional word length
184 * Return value is a fractional number represented by a
185 * fixed-point integer with a fractional word length of "fwl"
187 static u32 vol_pow32(u32 a, int exp, u32 fwl)
190 u32 power = 1 << fwl;
193 /* if exponent is 0, return 1 */
197 /* determine the number of iterations based on the exponent */
203 /* mutiply a "iter" times to compute power */
204 for (i = 0; i < iter; i++) {
206 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl
207 * Truncate product back to fwl fractional bits with rounding
209 power = vol_shift_64((u64)power * a, fwl);
213 /* if exp is positive, return the result */
217 /* if exp is negative, return the multiplicative inverse */
218 numerator = (u64)1 << (fwl << 1);
219 do_div(numerator, power);
221 return (u32)numerator;
225 * Function to calculate volume gain from TLV data.
226 * This function can only handle gain steps that are multiples of 0.5 dB
228 u32 vol_compute_gain(u32 value, int *tlv)
235 if (value == 0 && tlv[TLV_MUTE])
239 * compute dB gain from tlv. tlv_step
240 * in topology is multiplied by 100
242 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100;
245 * compute linear gain represented by fixed-point
246 * int with VOLUME_FWL fractional bits
248 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL);
250 /* extract the fractional part of volume step */
251 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100);
253 /* if volume step is an odd multiple of 0.5 dB */
254 if (f_step == VOL_HALF_DB_STEP && (value & 1))
255 linear_gain = vol_shift_64((u64)linear_gain *
256 VOL_FORTIETH_ROOT_OF_TEN,
263 * Set up volume table for kcontrols from tlv data
264 * "size" specifies the number of entries in the table
266 static int set_up_volume_table(struct snd_sof_control *scontrol,
267 int tlv[SOF_TLV_ITEMS], int size)
269 struct snd_soc_component *scomp = scontrol->scomp;
270 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
271 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
273 if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table)
274 return tplg_ops->control->set_up_volume_table(scontrol, tlv, size);
276 dev_err(scomp->dev, "Mandatory op %s not set\n", __func__);
280 struct sof_dai_types {
282 enum sof_ipc_dai_type type;
285 static const struct sof_dai_types sof_dais[] = {
286 {"SSP", SOF_DAI_INTEL_SSP},
287 {"HDA", SOF_DAI_INTEL_HDA},
288 {"DMIC", SOF_DAI_INTEL_DMIC},
289 {"ALH", SOF_DAI_INTEL_ALH},
290 {"SAI", SOF_DAI_IMX_SAI},
291 {"ESAI", SOF_DAI_IMX_ESAI},
292 {"ACP", SOF_DAI_AMD_BT},
293 {"ACPSP", SOF_DAI_AMD_SP},
294 {"ACPDMIC", SOF_DAI_AMD_DMIC},
295 {"ACPHS", SOF_DAI_AMD_HS},
296 {"AFE", SOF_DAI_MEDIATEK_AFE},
297 {"ACPSP_VIRTUAL", SOF_DAI_AMD_SP_VIRTUAL},
298 {"ACPHS_VIRTUAL", SOF_DAI_AMD_HS_VIRTUAL},
302 static enum sof_ipc_dai_type find_dai(const char *name)
306 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) {
307 if (strcmp(name, sof_dais[i].name) == 0)
308 return sof_dais[i].type;
311 return SOF_DAI_INTEL_NONE;
315 * Supported Frame format types and lookup, add new ones to end of list.
318 struct sof_frame_types {
320 enum sof_ipc_frame frame;
323 static const struct sof_frame_types sof_frames[] = {
324 {"s16le", SOF_IPC_FRAME_S16_LE},
325 {"s24le", SOF_IPC_FRAME_S24_4LE},
326 {"s32le", SOF_IPC_FRAME_S32_LE},
327 {"float", SOF_IPC_FRAME_FLOAT},
330 static enum sof_ipc_frame find_format(const char *name)
334 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) {
335 if (strcmp(name, sof_frames[i].name) == 0)
336 return sof_frames[i].frame;
339 /* use s32le if nothing is specified */
340 return SOF_IPC_FRAME_S32_LE;
343 int get_token_u32(void *elem, void *object, u32 offset)
345 struct snd_soc_tplg_vendor_value_elem *velem = elem;
346 u32 *val = (u32 *)((u8 *)object + offset);
348 *val = le32_to_cpu(velem->value);
352 int get_token_u16(void *elem, void *object, u32 offset)
354 struct snd_soc_tplg_vendor_value_elem *velem = elem;
355 u16 *val = (u16 *)((u8 *)object + offset);
357 *val = (u16)le32_to_cpu(velem->value);
361 int get_token_uuid(void *elem, void *object, u32 offset)
363 struct snd_soc_tplg_vendor_uuid_elem *velem = elem;
364 u8 *dst = (u8 *)object + offset;
366 memcpy(dst, velem->uuid, UUID_SIZE);
372 * The string gets from topology will be stored in heap, the owner only
373 * holds a char* member point to the heap.
375 int get_token_string(void *elem, void *object, u32 offset)
377 /* "dst" here points to the char* member of the owner */
378 char **dst = (char **)((u8 *)object + offset);
380 *dst = kstrdup(elem, GFP_KERNEL);
386 int get_token_comp_format(void *elem, void *object, u32 offset)
388 u32 *val = (u32 *)((u8 *)object + offset);
390 *val = find_format((const char *)elem);
394 int get_token_dai_type(void *elem, void *object, u32 offset)
396 u32 *val = (u32 *)((u8 *)object + offset);
398 *val = find_dai((const char *)elem);
403 static const struct sof_topology_token stream_tokens[] = {
404 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
405 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)},
406 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16,
407 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)},
411 static const struct sof_topology_token led_tokens[] = {
412 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
413 offsetof(struct snd_sof_led_control, use_led)},
414 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
415 offsetof(struct snd_sof_led_control, direction)},
418 static const struct sof_topology_token comp_pin_tokens[] = {
419 {SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
420 offsetof(struct snd_sof_widget, num_input_pins)},
421 {SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32,
422 offsetof(struct snd_sof_widget, num_output_pins)},
425 static const struct sof_topology_token comp_input_pin_binding_tokens[] = {
426 {SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
427 get_token_string, 0},
430 static const struct sof_topology_token comp_output_pin_binding_tokens[] = {
431 {SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING,
432 get_token_string, 0},
436 * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens
437 * @scomp: pointer to soc component
438 * @object: target ipc struct for parsed values
439 * @offset: offset within the object pointer
440 * @tokens: array of struct sof_topology_token containing the tokens to be matched
441 * @num_tokens: number of tokens in tokens array
442 * @array: source pointer to consecutive vendor arrays in topology
444 * This function parses multiple sets of string type tokens in vendor arrays
446 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp,
447 void *object, size_t offset,
448 const struct sof_topology_token *tokens, int num_tokens,
449 struct snd_soc_tplg_vendor_array *array)
451 struct snd_soc_tplg_vendor_uuid_elem *elem;
455 /* parse element by element */
456 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
457 elem = &array->uuid[i];
459 /* search for token */
460 for (j = 0; j < num_tokens; j++) {
461 /* match token type */
462 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID)
466 if (tokens[j].token != le32_to_cpu(elem->token))
469 /* matched - now load token */
470 tokens[j].get_token(elem, object,
471 offset + tokens[j].offset);
481 * sof_copy_tuples - Parse tokens and copy them to the @tuples array
482 * @sdev: pointer to struct snd_sof_dev
483 * @array: source pointer to consecutive vendor arrays in topology
484 * @array_size: size of @array
485 * @token_id: Token ID associated with a token array
486 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function
487 * looks for @token_instance_num of each token in the token array associated
489 * @tuples: tuples array to copy the matched tuples to
490 * @tuples_size: size of @tuples
491 * @num_copied_tuples: pointer to the number of copied tuples in the tuples array
494 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array,
495 int array_size, u32 token_id, int token_instance_num,
496 struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples)
498 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
499 const struct sof_token_info *token_list;
500 const struct sof_topology_token *tokens;
502 int num_tokens, asize;
505 token_list = tplg_ops ? tplg_ops->token_list : NULL;
506 /* nothing to do if token_list is NULL */
510 if (!tuples || !num_copied_tuples) {
511 dev_err(sdev->dev, "Invalid tuples array\n");
515 tokens = token_list[token_id].tokens;
516 num_tokens = token_list[token_id].count;
519 dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id);
523 /* check if there's space in the tuples array for new tokens */
524 if (*num_copied_tuples >= tuples_size) {
525 dev_err(sdev->dev, "No space in tuples array for new tokens from %s",
526 token_list[token_id].name);
530 while (array_size > 0 && found < num_tokens * token_instance_num) {
531 asize = le32_to_cpu(array->size);
535 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
539 /* make sure there is enough data before parsing */
541 if (array_size < 0) {
542 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize);
546 /* parse element by element */
547 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
548 /* search for token */
549 for (j = 0; j < num_tokens; j++) {
550 /* match token type */
551 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
552 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
553 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
554 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL ||
555 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING))
558 if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) {
559 struct snd_soc_tplg_vendor_string_elem *elem;
561 elem = &array->string[i];
564 if (tokens[j].token != le32_to_cpu(elem->token))
567 tuples[*num_copied_tuples].token = tokens[j].token;
568 tuples[*num_copied_tuples].value.s = elem->string;
570 struct snd_soc_tplg_vendor_value_elem *elem;
572 elem = &array->value[i];
575 if (tokens[j].token != le32_to_cpu(elem->token))
578 tuples[*num_copied_tuples].token = tokens[j].token;
579 tuples[*num_copied_tuples].value.v =
580 le32_to_cpu(elem->value);
583 (*num_copied_tuples)++;
585 /* stop if there's no space for any more new tuples */
586 if (*num_copied_tuples == tuples_size)
592 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize);
599 * sof_parse_string_tokens - Parse multiple sets of tokens
600 * @scomp: pointer to soc component
601 * @object: target ipc struct for parsed values
602 * @offset: offset within the object pointer
603 * @tokens: array of struct sof_topology_token containing the tokens to be matched
604 * @num_tokens: number of tokens in tokens array
605 * @array: source pointer to consecutive vendor arrays in topology
607 * This function parses multiple sets of string type tokens in vendor arrays
609 static int sof_parse_string_tokens(struct snd_soc_component *scomp,
610 void *object, int offset,
611 const struct sof_topology_token *tokens, int num_tokens,
612 struct snd_soc_tplg_vendor_array *array)
614 struct snd_soc_tplg_vendor_string_elem *elem;
618 /* parse element by element */
619 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
620 elem = &array->string[i];
622 /* search for token */
623 for (j = 0; j < num_tokens; j++) {
624 /* match token type */
625 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING)
629 if (tokens[j].token != le32_to_cpu(elem->token))
632 /* matched - now load token */
633 ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset);
645 * sof_parse_word_tokens - Parse multiple sets of tokens
646 * @scomp: pointer to soc component
647 * @object: target ipc struct for parsed values
648 * @offset: offset within the object pointer
649 * @tokens: array of struct sof_topology_token containing the tokens to be matched
650 * @num_tokens: number of tokens in tokens array
651 * @array: source pointer to consecutive vendor arrays in topology
653 * This function parses multiple sets of word type tokens in vendor arrays
655 static int sof_parse_word_tokens(struct snd_soc_component *scomp,
656 void *object, int offset,
657 const struct sof_topology_token *tokens, int num_tokens,
658 struct snd_soc_tplg_vendor_array *array)
660 struct snd_soc_tplg_vendor_value_elem *elem;
664 /* parse element by element */
665 for (i = 0; i < le32_to_cpu(array->num_elems); i++) {
666 elem = &array->value[i];
668 /* search for token */
669 for (j = 0; j < num_tokens; j++) {
670 /* match token type */
671 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD ||
672 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT ||
673 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE ||
674 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL))
678 if (tokens[j].token != le32_to_cpu(elem->token))
682 tokens[j].get_token(elem, object, offset + tokens[j].offset);
692 * sof_parse_token_sets - Parse multiple sets of tokens
693 * @scomp: pointer to soc component
694 * @object: target ipc struct for parsed values
695 * @tokens: token definition array describing what tokens to parse
696 * @count: number of tokens in definition array
697 * @array: source pointer to consecutive vendor arrays in topology
698 * @array_size: total size of @array
699 * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function
700 * looks for @token_instance_num of each token in the @tokens
701 * @object_size: offset to next target ipc struct with multiple sets
703 * This function parses multiple sets of tokens in vendor arrays into
704 * consecutive ipc structs.
706 static int sof_parse_token_sets(struct snd_soc_component *scomp,
707 void *object, const struct sof_topology_token *tokens,
708 int count, struct snd_soc_tplg_vendor_array *array,
709 int array_size, int token_instance_num, size_t object_size)
717 while (array_size > 0 && total < count * token_instance_num) {
718 asize = le32_to_cpu(array->size);
721 if (asize < 0) { /* FIXME: A zero-size array makes no sense */
722 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
727 /* make sure there is enough data before parsing */
729 if (array_size < 0) {
730 dev_err(scomp->dev, "error: invalid array size 0x%x\n",
735 /* call correct parser depending on type */
736 switch (le32_to_cpu(array->type)) {
737 case SND_SOC_TPLG_TUPLE_TYPE_UUID:
738 found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count,
741 case SND_SOC_TPLG_TUPLE_TYPE_STRING:
743 ret = sof_parse_string_tokens(scomp, object, offset, tokens, count,
746 dev_err(scomp->dev, "error: no memory to copy string token\n");
752 case SND_SOC_TPLG_TUPLE_TYPE_BOOL:
753 case SND_SOC_TPLG_TUPLE_TYPE_BYTE:
754 case SND_SOC_TPLG_TUPLE_TYPE_WORD:
755 case SND_SOC_TPLG_TUPLE_TYPE_SHORT:
756 found += sof_parse_word_tokens(scomp, object, offset, tokens, count,
760 dev_err(scomp->dev, "error: unknown token type %d\n",
766 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array
769 /* move to next target struct */
770 if (found >= count) {
771 offset += object_size;
781 * sof_parse_tokens - Parse one set of tokens
782 * @scomp: pointer to soc component
783 * @object: target ipc struct for parsed values
784 * @tokens: token definition array describing what tokens to parse
785 * @num_tokens: number of tokens in definition array
786 * @array: source pointer to consecutive vendor arrays in topology
787 * @array_size: total size of @array
789 * This function parses a single set of tokens in vendor arrays into
790 * consecutive ipc structs.
792 static int sof_parse_tokens(struct snd_soc_component *scomp, void *object,
793 const struct sof_topology_token *tokens, int num_tokens,
794 struct snd_soc_tplg_vendor_array *array,
799 * sof_parse_tokens is used when topology contains only a single set of
800 * identical tuples arrays. So additional parameters to
801 * sof_parse_token_sets are sets = 1 (only 1 set) and
802 * object_size = 0 (irrelevant).
804 return sof_parse_token_sets(scomp, object, tokens, num_tokens, array,
809 * Standard Kcontrols.
812 static int sof_control_load_volume(struct snd_soc_component *scomp,
813 struct snd_sof_control *scontrol,
814 struct snd_kcontrol_new *kc,
815 struct snd_soc_tplg_ctl_hdr *hdr)
817 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
818 struct snd_soc_tplg_mixer_control *mc =
819 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr);
820 int tlv[SOF_TLV_ITEMS];
824 /* validate topology data */
825 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN)
829 * If control has more than 2 channels we need to override the info. This is because even if
830 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the
831 * pre-defined dapm control types (and related functions) creating the actual control
832 * restrict the channels only to mono or stereo.
834 if (le32_to_cpu(mc->num_channels) > 2)
835 kc->info = snd_sof_volume_info;
837 scontrol->comp_id = sdev->next_comp_id;
838 scontrol->min_volume_step = le32_to_cpu(mc->min);
839 scontrol->max_volume_step = le32_to_cpu(mc->max);
840 scontrol->num_channels = le32_to_cpu(mc->num_channels);
842 scontrol->max = le32_to_cpu(mc->max);
843 if (le32_to_cpu(mc->max) == 1)
846 /* extract tlv data */
847 if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) {
848 dev_err(scomp->dev, "error: invalid TLV data\n");
852 /* set up volume table */
853 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1);
855 dev_err(scomp->dev, "error: setting up volume table\n");
860 /* set up possible led control from mixer private data */
861 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens,
862 ARRAY_SIZE(led_tokens), mc->priv.array,
863 le32_to_cpu(mc->priv.size));
865 dev_err(scomp->dev, "error: parse led tokens failed %d\n",
866 le32_to_cpu(mc->priv.size));
870 if (scontrol->led_ctl.use_led) {
871 mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED :
872 SNDRV_CTL_ELEM_ACCESS_SPK_LED;
873 scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
874 scontrol->access |= mask;
875 kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
877 sdev->led_present = true;
880 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n",
881 scontrol->comp_id, scontrol->num_channels);
886 if (le32_to_cpu(mc->max) > 1)
887 kfree(scontrol->volume_table);
892 static int sof_control_load_enum(struct snd_soc_component *scomp,
893 struct snd_sof_control *scontrol,
894 struct snd_kcontrol_new *kc,
895 struct snd_soc_tplg_ctl_hdr *hdr)
897 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
898 struct snd_soc_tplg_enum_control *ec =
899 container_of(hdr, struct snd_soc_tplg_enum_control, hdr);
901 /* validate topology data */
902 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN)
905 scontrol->comp_id = sdev->next_comp_id;
906 scontrol->num_channels = le32_to_cpu(ec->num_channels);
908 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n",
909 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id);
914 static int sof_control_load_bytes(struct snd_soc_component *scomp,
915 struct snd_sof_control *scontrol,
916 struct snd_kcontrol_new *kc,
917 struct snd_soc_tplg_ctl_hdr *hdr)
919 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
920 struct snd_soc_tplg_bytes_control *control =
921 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
922 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value;
923 size_t priv_size = le32_to_cpu(control->priv.size);
925 scontrol->max_size = sbe->max;
926 scontrol->comp_id = sdev->next_comp_id;
928 dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id);
930 /* copy the private data */
932 scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL);
936 scontrol->priv_size = priv_size;
942 /* external kcontrol init - used for any driver specific init */
943 static int sof_control_load(struct snd_soc_component *scomp, int index,
944 struct snd_kcontrol_new *kc,
945 struct snd_soc_tplg_ctl_hdr *hdr)
947 struct soc_mixer_control *sm;
948 struct soc_bytes_ext *sbe;
950 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
951 struct snd_soc_dobj *dobj;
952 struct snd_sof_control *scontrol;
955 dev_dbg(scomp->dev, "tplg: load control type %d name : %s\n",
956 hdr->type, hdr->name);
958 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL);
962 scontrol->name = kstrdup(hdr->name, GFP_KERNEL);
963 if (!scontrol->name) {
968 scontrol->scomp = scomp;
969 scontrol->access = kc->access;
970 scontrol->info_type = le32_to_cpu(hdr->ops.info);
971 scontrol->index = kc->index;
973 switch (le32_to_cpu(hdr->ops.info)) {
974 case SND_SOC_TPLG_CTL_VOLSW:
975 case SND_SOC_TPLG_CTL_VOLSW_SX:
976 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
977 sm = (struct soc_mixer_control *)kc->private_value;
979 ret = sof_control_load_volume(scomp, scontrol, kc, hdr);
981 case SND_SOC_TPLG_CTL_BYTES:
982 sbe = (struct soc_bytes_ext *)kc->private_value;
984 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr);
986 case SND_SOC_TPLG_CTL_ENUM:
987 case SND_SOC_TPLG_CTL_ENUM_VALUE:
988 se = (struct soc_enum *)kc->private_value;
990 ret = sof_control_load_enum(scomp, scontrol, kc, hdr);
992 case SND_SOC_TPLG_CTL_RANGE:
993 case SND_SOC_TPLG_CTL_STROBE:
994 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
995 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
996 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
997 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
998 case SND_SOC_TPLG_DAPM_CTL_PIN:
1000 dev_warn(scomp->dev, "control type not supported %d:%d:%d\n",
1001 hdr->ops.get, hdr->ops.put, hdr->ops.info);
1002 kfree(scontrol->name);
1008 kfree(scontrol->name);
1013 scontrol->led_ctl.led_value = -1;
1015 dobj->private = scontrol;
1016 list_add(&scontrol->list, &sdev->kcontrol_list);
1020 static int sof_control_unload(struct snd_soc_component *scomp,
1021 struct snd_soc_dobj *dobj)
1023 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1024 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1025 struct snd_sof_control *scontrol = dobj->private;
1028 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name);
1030 if (tplg_ops && tplg_ops->control_free) {
1031 ret = tplg_ops->control_free(sdev, scontrol);
1033 dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name);
1036 /* free all data before returning in case of error too */
1037 kfree(scontrol->ipc_control_data);
1038 kfree(scontrol->priv);
1039 kfree(scontrol->name);
1040 list_del(&scontrol->list);
1050 static int sof_connect_dai_widget(struct snd_soc_component *scomp,
1051 struct snd_soc_dapm_widget *w,
1052 struct snd_soc_tplg_dapm_widget *tw,
1053 struct snd_sof_dai *dai)
1055 struct snd_soc_card *card = scomp->card;
1056 struct snd_soc_pcm_runtime *rtd;
1057 struct snd_soc_dai *cpu_dai;
1062 dev_err(scomp->dev, "Widget %s does not have stream\n", w->name);
1066 if (w->id == snd_soc_dapm_dai_out)
1067 stream = SNDRV_PCM_STREAM_CAPTURE;
1068 else if (w->id == snd_soc_dapm_dai_in)
1069 stream = SNDRV_PCM_STREAM_PLAYBACK;
1073 list_for_each_entry(rtd, &card->rtd_list, list) {
1074 /* does stream match DAI link ? */
1075 if (!rtd->dai_link->stream_name ||
1076 strcmp(w->sname, rtd->dai_link->stream_name))
1079 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1081 * Please create DAI widget in the right order
1082 * to ensure BE will connect to the right DAI
1085 if (!snd_soc_dai_get_widget(cpu_dai, stream)) {
1086 snd_soc_dai_set_widget(cpu_dai, stream, w);
1090 if (i == rtd->dai_link->num_cpus) {
1091 dev_err(scomp->dev, "error: can't find BE for DAI %s\n", w->name);
1096 dai->name = rtd->dai_link->name;
1097 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n",
1098 w->name, rtd->dai_link->name);
1101 /* check we have a connection */
1103 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n",
1111 static void sof_disconnect_dai_widget(struct snd_soc_component *scomp,
1112 struct snd_soc_dapm_widget *w)
1114 struct snd_soc_card *card = scomp->card;
1115 struct snd_soc_pcm_runtime *rtd;
1116 struct snd_soc_dai *cpu_dai;
1122 if (w->id == snd_soc_dapm_dai_out)
1123 stream = SNDRV_PCM_STREAM_CAPTURE;
1124 else if (w->id == snd_soc_dapm_dai_in)
1125 stream = SNDRV_PCM_STREAM_PLAYBACK;
1129 list_for_each_entry(rtd, &card->rtd_list, list) {
1130 /* does stream match DAI link ? */
1131 if (!rtd->dai_link->stream_name ||
1132 strcmp(w->sname, rtd->dai_link->stream_name))
1135 for_each_rtd_cpu_dais(rtd, i, cpu_dai)
1136 if (snd_soc_dai_get_widget(cpu_dai, stream) == w) {
1137 snd_soc_dai_set_widget(cpu_dai, stream, NULL);
1143 /* bind PCM ID to host component ID */
1144 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm,
1147 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1148 struct snd_sof_widget *host_widget;
1150 if (sdev->dspless_mode_selected)
1153 host_widget = snd_sof_find_swidget_sname(scomp,
1154 spcm->pcm.caps[dir].name,
1157 dev_err(scomp->dev, "can't find host comp to bind pcm\n");
1161 spcm->stream[dir].comp_id = host_widget->comp_id;
1166 static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples)
1173 for (i = 0; i < num_tuples; i++) {
1174 if (tuples[i].token == token_id)
1175 return tuples[i].value.v;
1181 static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
1182 struct snd_soc_tplg_dapm_widget *tw,
1183 enum sof_tokens *object_token_list, int count)
1185 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1186 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1187 struct snd_soc_tplg_private *private = &tw->priv;
1188 const struct sof_token_info *token_list;
1192 token_list = tplg_ops ? tplg_ops->token_list : NULL;
1193 /* nothing to do if token_list is NULL */
1197 if (count > 0 && !object_token_list) {
1198 dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name);
1202 /* calculate max size of tuples array */
1203 for (i = 0; i < count; i++)
1204 num_tuples += token_list[object_token_list[i]].count;
1206 /* allocate memory for tuples array */
1207 swidget->tuples = kcalloc(num_tuples, sizeof(*swidget->tuples), GFP_KERNEL);
1208 if (!swidget->tuples)
1211 /* parse token list for widget */
1212 for (i = 0; i < count; i++) {
1215 if (object_token_list[i] >= SOF_TOKEN_COUNT) {
1216 dev_err(scomp->dev, "Invalid token id %d for widget %s\n",
1217 object_token_list[i], swidget->widget->name);
1222 switch (object_token_list[i]) {
1223 case SOF_COMP_EXT_TOKENS:
1224 /* parse and save UUID in swidget */
1225 ret = sof_parse_tokens(scomp, swidget,
1226 token_list[object_token_list[i]].tokens,
1227 token_list[object_token_list[i]].count,
1228 private->array, le32_to_cpu(private->size));
1230 dev_err(scomp->dev, "Failed parsing %s for widget %s\n",
1231 token_list[object_token_list[i]].name,
1232 swidget->widget->name);
1237 case SOF_IN_AUDIO_FORMAT_TOKENS:
1238 num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS,
1239 swidget->tuples, swidget->num_tuples);
1241 dev_err(sdev->dev, "Invalid input audio format count for %s\n",
1242 swidget->widget->name);
1247 case SOF_OUT_AUDIO_FORMAT_TOKENS:
1248 num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS,
1249 swidget->tuples, swidget->num_tuples);
1251 dev_err(sdev->dev, "Invalid output audio format count for %s\n",
1252 swidget->widget->name);
1262 struct snd_sof_tuple *new_tuples;
1264 num_tuples += token_list[object_token_list[i]].count * num_sets;
1265 new_tuples = krealloc(swidget->tuples,
1266 sizeof(*new_tuples) * num_tuples, GFP_KERNEL);
1272 swidget->tuples = new_tuples;
1275 /* copy one set of tuples per token ID into swidget->tuples */
1276 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1277 object_token_list[i], num_sets, swidget->tuples,
1278 num_tuples, &swidget->num_tuples);
1280 dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n",
1281 token_list[object_token_list[i]].name, swidget->widget->name, ret);
1288 kfree(swidget->tuples);
1292 static void sof_free_pin_binding(struct snd_sof_widget *swidget,
1299 if (pin_type == SOF_PIN_TYPE_INPUT) {
1300 pin_binding = swidget->input_pin_binding;
1301 num_pins = swidget->num_input_pins;
1303 pin_binding = swidget->output_pin_binding;
1304 num_pins = swidget->num_output_pins;
1308 for (i = 0; i < num_pins; i++)
1309 kfree(pin_binding[i]);
1315 static int sof_parse_pin_binding(struct snd_sof_widget *swidget,
1316 struct snd_soc_tplg_private *priv, bool pin_type)
1318 const struct sof_topology_token *pin_binding_token;
1319 char *pin_binding[SOF_WIDGET_MAX_NUM_PINS];
1326 if (pin_type == SOF_PIN_TYPE_INPUT) {
1327 num_pins = swidget->num_input_pins;
1328 pin_binding_token = comp_input_pin_binding_tokens;
1329 token_count = ARRAY_SIZE(comp_input_pin_binding_tokens);
1331 num_pins = swidget->num_output_pins;
1332 pin_binding_token = comp_output_pin_binding_tokens;
1333 token_count = ARRAY_SIZE(comp_output_pin_binding_tokens);
1336 memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *));
1337 ret = sof_parse_token_sets(swidget->scomp, pin_binding, pin_binding_token,
1338 token_count, priv->array, le32_to_cpu(priv->size),
1339 num_pins, sizeof(char *));
1343 /* copy pin binding array to swidget only if it is defined in topology */
1344 if (pin_binding[0]) {
1345 pb = kmemdup(pin_binding, num_pins * sizeof(char *), GFP_KERNEL);
1350 if (pin_type == SOF_PIN_TYPE_INPUT)
1351 swidget->input_pin_binding = pb;
1353 swidget->output_pin_binding = pb;
1359 for (i = 0; i < num_pins; i++)
1360 kfree(pin_binding[i]);
1365 /* external widget init - used for any driver specific init */
1366 static int sof_widget_ready(struct snd_soc_component *scomp, int index,
1367 struct snd_soc_dapm_widget *w,
1368 struct snd_soc_tplg_dapm_widget *tw)
1370 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1371 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1372 const struct sof_ipc_tplg_widget_ops *widget_ops;
1373 struct snd_soc_tplg_private *priv = &tw->priv;
1374 enum sof_tokens *token_list = NULL;
1375 struct snd_sof_widget *swidget;
1376 struct snd_sof_dai *dai;
1377 int token_list_size = 0;
1380 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
1384 swidget->scomp = scomp;
1385 swidget->widget = w;
1386 swidget->comp_id = sdev->next_comp_id++;
1387 swidget->id = w->id;
1388 swidget->pipeline_id = index;
1389 swidget->private = NULL;
1390 mutex_init(&swidget->setup_mutex);
1392 ida_init(&swidget->output_queue_ida);
1393 ida_init(&swidget->input_queue_ida);
1395 ret = sof_parse_tokens(scomp, swidget, comp_pin_tokens,
1396 ARRAY_SIZE(comp_pin_tokens), priv->array,
1397 le32_to_cpu(priv->size));
1399 dev_err(scomp->dev, "failed to parse component pin tokens for %s\n",
1404 if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS ||
1405 swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) {
1406 dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n",
1407 swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins);
1412 if (swidget->num_input_pins > 1) {
1413 ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT);
1414 /* on parsing error, pin binding is not allocated, nothing to free. */
1416 dev_err(scomp->dev, "failed to parse input pin binding for %s\n",
1422 if (swidget->num_output_pins > 1) {
1423 ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT);
1424 /* on parsing error, pin binding is not allocated, nothing to free. */
1426 dev_err(scomp->dev, "failed to parse output pin binding for %s\n",
1433 "tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n",
1434 swidget->comp_id, w->name, swidget->id, index,
1435 swidget->num_input_pins, swidget->num_output_pins,
1436 strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none");
1438 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1440 token_list = widget_ops[w->id].token_list;
1441 token_list_size = widget_ops[w->id].token_list_size;
1444 /* handle any special case widgets */
1446 case snd_soc_dapm_dai_in:
1447 case snd_soc_dapm_dai_out:
1448 dai = kzalloc(sizeof(*dai), GFP_KERNEL);
1454 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1456 ret = sof_connect_dai_widget(scomp, w, tw, dai);
1461 list_add(&dai->list, &sdev->dai_list);
1462 swidget->private = dai;
1464 case snd_soc_dapm_effect:
1465 /* check we have some tokens - we need at least process type */
1466 if (le32_to_cpu(tw->priv.size) == 0) {
1467 dev_err(scomp->dev, "error: process tokens not found\n");
1471 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1473 case snd_soc_dapm_pga:
1474 if (!le32_to_cpu(tw->num_kcontrols)) {
1475 dev_err(scomp->dev, "invalid kcontrol count %d for volume\n",
1482 case snd_soc_dapm_mixer:
1483 case snd_soc_dapm_buffer:
1484 case snd_soc_dapm_scheduler:
1485 case snd_soc_dapm_aif_out:
1486 case snd_soc_dapm_aif_in:
1487 case snd_soc_dapm_src:
1488 case snd_soc_dapm_asrc:
1489 case snd_soc_dapm_siggen:
1490 case snd_soc_dapm_mux:
1491 case snd_soc_dapm_demux:
1492 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size);
1494 case snd_soc_dapm_switch:
1495 case snd_soc_dapm_dai_link:
1496 case snd_soc_dapm_kcontrol:
1498 dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name);
1502 /* check token parsing reply */
1505 "error: failed to add widget id %d type %d name : %s stream %s\n",
1506 tw->shift, swidget->id, tw->name,
1507 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0
1508 ? tw->sname : "none");
1512 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) {
1513 swidget->core = SOF_DSP_PRIMARY_CORE;
1515 int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples,
1516 swidget->num_tuples);
1519 swidget->core = core;
1522 /* bind widget to external event */
1523 if (tw->event_type) {
1524 if (widget_ops && widget_ops[w->id].bind_event) {
1525 ret = widget_ops[w->id].bind_event(scomp, swidget,
1526 le16_to_cpu(tw->event_type));
1528 dev_err(scomp->dev, "widget event binding failed for %s\n",
1529 swidget->widget->name);
1535 /* create and add pipeline for scheduler type widgets */
1536 if (w->id == snd_soc_dapm_scheduler) {
1537 struct snd_sof_pipeline *spipe;
1539 spipe = kzalloc(sizeof(*spipe), GFP_KERNEL);
1545 spipe->pipe_widget = swidget;
1546 swidget->spipe = spipe;
1547 list_add(&spipe->list, &sdev->pipeline_list);
1550 w->dobj.private = swidget;
1551 list_add(&swidget->list, &sdev->widget_list);
1554 kfree(swidget->private);
1555 kfree(swidget->tuples);
1561 static int sof_route_unload(struct snd_soc_component *scomp,
1562 struct snd_soc_dobj *dobj)
1564 struct snd_sof_route *sroute;
1566 sroute = dobj->private;
1570 /* free sroute and its private data */
1571 kfree(sroute->private);
1572 list_del(&sroute->list);
1578 static int sof_widget_unload(struct snd_soc_component *scomp,
1579 struct snd_soc_dobj *dobj)
1581 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1582 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1583 const struct sof_ipc_tplg_widget_ops *widget_ops;
1584 const struct snd_kcontrol_new *kc;
1585 struct snd_soc_dapm_widget *widget;
1586 struct snd_sof_control *scontrol;
1587 struct snd_sof_widget *swidget;
1588 struct soc_mixer_control *sm;
1589 struct soc_bytes_ext *sbe;
1590 struct snd_sof_dai *dai;
1591 struct soc_enum *se;
1594 swidget = dobj->private;
1598 widget = swidget->widget;
1600 switch (swidget->id) {
1601 case snd_soc_dapm_dai_in:
1602 case snd_soc_dapm_dai_out:
1603 dai = swidget->private;
1606 list_del(&dai->list);
1608 sof_disconnect_dai_widget(scomp, widget);
1611 case snd_soc_dapm_scheduler:
1613 struct snd_sof_pipeline *spipe = swidget->spipe;
1615 list_del(&spipe->list);
1617 swidget->spipe = NULL;
1623 for (i = 0; i < widget->num_kcontrols; i++) {
1624 kc = &widget->kcontrol_news[i];
1625 switch (widget->dobj.widget.kcontrol_type[i]) {
1626 case SND_SOC_TPLG_TYPE_MIXER:
1627 sm = (struct soc_mixer_control *)kc->private_value;
1628 scontrol = sm->dobj.private;
1630 kfree(scontrol->volume_table);
1632 case SND_SOC_TPLG_TYPE_ENUM:
1633 se = (struct soc_enum *)kc->private_value;
1634 scontrol = se->dobj.private;
1636 case SND_SOC_TPLG_TYPE_BYTES:
1637 sbe = (struct soc_bytes_ext *)kc->private_value;
1638 scontrol = sbe->dobj.private;
1641 dev_warn(scomp->dev, "unsupported kcontrol_type\n");
1644 kfree(scontrol->ipc_control_data);
1645 list_del(&scontrol->list);
1646 kfree(scontrol->name);
1651 /* free IPC related data */
1652 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
1653 if (widget_ops && widget_ops[swidget->id].ipc_free)
1654 widget_ops[swidget->id].ipc_free(swidget);
1656 ida_destroy(&swidget->output_queue_ida);
1657 ida_destroy(&swidget->input_queue_ida);
1659 sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT);
1660 sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT);
1662 kfree(swidget->tuples);
1664 /* remove and free swidget object */
1665 list_del(&swidget->list);
1672 * DAI HW configuration.
1675 /* FE DAI - used for any driver specific init */
1676 static int sof_dai_load(struct snd_soc_component *scomp, int index,
1677 struct snd_soc_dai_driver *dai_drv,
1678 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
1680 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1681 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1682 struct snd_soc_tplg_stream_caps *caps;
1683 struct snd_soc_tplg_private *private = &pcm->priv;
1684 struct snd_sof_pcm *spcm;
1688 /* nothing to do for BEs atm */
1692 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL);
1696 spcm->scomp = scomp;
1698 for_each_pcm_streams(stream) {
1699 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED;
1701 snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1703 snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work);
1707 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name);
1709 /* perform pcm set op */
1710 if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) {
1711 ret = ipc_pcm_ops->pcm_setup(sdev, spcm);
1716 dai_drv->dobj.private = spcm;
1717 list_add(&spcm->list, &sdev->pcm_list);
1719 ret = sof_parse_tokens(scomp, spcm, stream_tokens,
1720 ARRAY_SIZE(stream_tokens), private->array,
1721 le32_to_cpu(private->size));
1723 dev_err(scomp->dev, "error: parse stream tokens failed %d\n",
1724 le32_to_cpu(private->size));
1728 /* do we need to allocate playback PCM DMA pages */
1729 if (!spcm->pcm.playback)
1732 stream = SNDRV_PCM_STREAM_PLAYBACK;
1734 caps = &spcm->pcm.caps[stream];
1736 /* allocate playback page table buffer */
1737 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1738 PAGE_SIZE, &spcm->stream[stream].page_table);
1740 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1746 /* bind pcm to host comp */
1747 ret = spcm_bind(scomp, spcm, stream);
1750 "error: can't bind pcm to host\n");
1751 goto free_playback_tables;
1755 stream = SNDRV_PCM_STREAM_CAPTURE;
1757 /* do we need to allocate capture PCM DMA pages */
1758 if (!spcm->pcm.capture)
1761 caps = &spcm->pcm.caps[stream];
1763 /* allocate capture page table buffer */
1764 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
1765 PAGE_SIZE, &spcm->stream[stream].page_table);
1767 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n",
1769 goto free_playback_tables;
1772 /* bind pcm to host comp */
1773 ret = spcm_bind(scomp, spcm, stream);
1776 "error: can't bind pcm to host\n");
1777 snd_dma_free_pages(&spcm->stream[stream].page_table);
1778 goto free_playback_tables;
1783 free_playback_tables:
1784 if (spcm->pcm.playback)
1785 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1790 static int sof_dai_unload(struct snd_soc_component *scomp,
1791 struct snd_soc_dobj *dobj)
1793 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1794 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm);
1795 struct snd_sof_pcm *spcm = dobj->private;
1797 /* free PCM DMA pages */
1798 if (spcm->pcm.playback)
1799 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
1801 if (spcm->pcm.capture)
1802 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
1804 /* perform pcm free op */
1805 if (ipc_pcm_ops && ipc_pcm_ops->pcm_free)
1806 ipc_pcm_ops->pcm_free(sdev, spcm);
1808 /* remove from list and free spcm */
1809 list_del(&spcm->list);
1815 static const struct sof_topology_token common_dai_link_tokens[] = {
1816 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type,
1817 offsetof(struct snd_sof_dai_link, type)},
1820 /* DAI link - used for any driver specific init */
1821 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link,
1822 struct snd_soc_tplg_link_config *cfg)
1824 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
1825 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
1826 struct snd_soc_tplg_private *private = &cfg->priv;
1827 const struct sof_token_info *token_list;
1828 struct snd_sof_dai_link *slink;
1833 if (!link->platforms) {
1834 dev_err(scomp->dev, "error: no platforms\n");
1837 link->platforms->name = dev_name(scomp->dev);
1839 if (tplg_ops && tplg_ops->link_setup) {
1840 ret = tplg_ops->link_setup(sdev, link);
1845 /* Set nonatomic property for FE dai links as their trigger action involves IPC's */
1846 if (!link->no_pcm) {
1847 link->nonatomic = true;
1851 /* check we have some tokens - we need at least DAI type */
1852 if (le32_to_cpu(private->size) == 0) {
1853 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n");
1857 slink = kzalloc(sizeof(*slink), GFP_KERNEL);
1861 slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs);
1862 slink->hw_configs = kmemdup(cfg->hw_config,
1863 sizeof(*slink->hw_configs) * slink->num_hw_configs,
1865 if (!slink->hw_configs) {
1870 slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id);
1873 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n",
1874 slink->num_hw_configs, slink->default_hw_cfg_id, link->name);
1876 ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens,
1877 ARRAY_SIZE(common_dai_link_tokens),
1878 private->array, le32_to_cpu(private->size));
1880 dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n");
1881 kfree(slink->hw_configs);
1886 token_list = tplg_ops ? tplg_ops->token_list : NULL;
1890 /* calculate size of tuples array */
1891 num_tuples += token_list[SOF_DAI_LINK_TOKENS].count;
1892 num_sets = slink->num_hw_configs;
1893 switch (slink->type) {
1894 case SOF_DAI_INTEL_SSP:
1895 token_id = SOF_SSP_TOKENS;
1896 num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs;
1898 case SOF_DAI_INTEL_DMIC:
1899 token_id = SOF_DMIC_TOKENS;
1900 num_tuples += token_list[SOF_DMIC_TOKENS].count;
1902 /* Allocate memory for max PDM controllers */
1903 num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL;
1905 case SOF_DAI_INTEL_HDA:
1906 token_id = SOF_HDA_TOKENS;
1907 num_tuples += token_list[SOF_HDA_TOKENS].count;
1909 case SOF_DAI_INTEL_ALH:
1910 token_id = SOF_ALH_TOKENS;
1911 num_tuples += token_list[SOF_ALH_TOKENS].count;
1913 case SOF_DAI_IMX_SAI:
1914 token_id = SOF_SAI_TOKENS;
1915 num_tuples += token_list[SOF_SAI_TOKENS].count;
1917 case SOF_DAI_IMX_ESAI:
1918 token_id = SOF_ESAI_TOKENS;
1919 num_tuples += token_list[SOF_ESAI_TOKENS].count;
1921 case SOF_DAI_MEDIATEK_AFE:
1922 token_id = SOF_AFE_TOKENS;
1923 num_tuples += token_list[SOF_AFE_TOKENS].count;
1925 case SOF_DAI_AMD_DMIC:
1926 token_id = SOF_ACPDMIC_TOKENS;
1927 num_tuples += token_list[SOF_ACPDMIC_TOKENS].count;
1929 case SOF_DAI_AMD_SP:
1930 case SOF_DAI_AMD_HS:
1931 case SOF_DAI_AMD_SP_VIRTUAL:
1932 case SOF_DAI_AMD_HS_VIRTUAL:
1933 token_id = SOF_ACPI2S_TOKENS;
1934 num_tuples += token_list[SOF_ACPI2S_TOKENS].count;
1940 /* allocate memory for tuples array */
1941 slink->tuples = kcalloc(num_tuples, sizeof(*slink->tuples), GFP_KERNEL);
1942 if (!slink->tuples) {
1943 kfree(slink->hw_configs);
1948 if (token_list[SOF_DAI_LINK_TOKENS].tokens) {
1949 /* parse one set of DAI link tokens */
1950 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1951 SOF_DAI_LINK_TOKENS, 1, slink->tuples,
1952 num_tuples, &slink->num_tuples);
1954 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1955 token_list[SOF_DAI_LINK_TOKENS].name, link->name);
1960 /* nothing more to do if there are no DAI type-specific tokens defined */
1961 if (!token_id || !token_list[token_id].tokens)
1964 /* parse "num_sets" sets of DAI-specific tokens */
1965 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1966 token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples);
1968 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1969 token_list[token_id].name, link->name);
1973 /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */
1974 if (token_id == SOF_DMIC_TOKENS) {
1975 num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE,
1976 slink->tuples, slink->num_tuples);
1979 dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name);
1984 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size),
1985 SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples,
1986 num_tuples, &slink->num_tuples);
1988 dev_err(scomp->dev, "failed to parse %s for dai link %s\n",
1989 token_list[SOF_DMIC_PDM_TOKENS].name, link->name);
1994 link->dobj.private = slink;
1995 list_add(&slink->list, &sdev->dai_link_list);
2000 kfree(slink->tuples);
2001 kfree(slink->hw_configs);
2007 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj)
2009 struct snd_sof_dai_link *slink = dobj->private;
2014 kfree(slink->tuples);
2015 list_del(&slink->list);
2016 kfree(slink->hw_configs);
2018 dobj->private = NULL;
2023 /* DAI link - used for any driver specific init */
2024 static int sof_route_load(struct snd_soc_component *scomp, int index,
2025 struct snd_soc_dapm_route *route)
2027 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2028 struct snd_sof_widget *source_swidget, *sink_swidget;
2029 struct snd_soc_dobj *dobj = &route->dobj;
2030 struct snd_sof_route *sroute;
2033 /* allocate memory for sroute and connect */
2034 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL);
2038 sroute->scomp = scomp;
2039 dev_dbg(scomp->dev, "sink %s control %s source %s\n",
2040 route->sink, route->control ? route->control : "none",
2043 /* source component */
2044 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source);
2045 if (!source_swidget) {
2046 dev_err(scomp->dev, "error: source %s not found\n",
2053 * Virtual widgets of type output/out_drv may be added in topology
2054 * for compatibility. These are not handled by the FW.
2055 * So, don't send routes whose source/sink widget is of such types
2058 if (source_swidget->id == snd_soc_dapm_out_drv ||
2059 source_swidget->id == snd_soc_dapm_output)
2062 /* sink component */
2063 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink);
2064 if (!sink_swidget) {
2065 dev_err(scomp->dev, "error: sink %s not found\n",
2072 * Don't send routes whose sink widget is of type
2073 * output or out_drv to the DSP
2075 if (sink_swidget->id == snd_soc_dapm_out_drv ||
2076 sink_swidget->id == snd_soc_dapm_output)
2079 sroute->route = route;
2080 dobj->private = sroute;
2081 sroute->src_widget = source_swidget;
2082 sroute->sink_widget = sink_swidget;
2084 /* add route to route list */
2085 list_add(&sroute->list, &sdev->route_list);
2094 * sof_set_widget_pipeline - Set pipeline for a component
2095 * @sdev: pointer to struct snd_sof_dev
2096 * @spipe: pointer to struct snd_sof_pipeline
2097 * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget
2099 * Return: 0 if successful, -EINVAL on error.
2100 * The function checks if @swidget is associated with any volatile controls. If so, setting
2101 * the dynamic_pipeline_widget is disallowed.
2103 static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe,
2104 struct snd_sof_widget *swidget)
2106 struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
2107 struct snd_sof_control *scontrol;
2109 if (pipe_widget->dynamic_pipeline_widget) {
2110 /* dynamic widgets cannot have volatile kcontrols */
2111 list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
2112 if (scontrol->comp_id == swidget->comp_id &&
2113 (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) {
2115 "error: volatile control found for dynamic widget %s\n",
2116 swidget->widget->name);
2121 /* set the pipeline and apply the dynamic_pipeline_widget_flag */
2122 swidget->spipe = spipe;
2123 swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget;
2128 /* completion - called at completion of firmware loading */
2129 static int sof_complete(struct snd_soc_component *scomp)
2131 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2132 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2133 const struct sof_ipc_tplg_widget_ops *widget_ops;
2134 struct snd_sof_control *scontrol;
2135 struct snd_sof_pipeline *spipe;
2138 widget_ops = tplg_ops ? tplg_ops->widget : NULL;
2140 /* first update all control IPC structures based on the IPC version */
2141 if (tplg_ops && tplg_ops->control_setup)
2142 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
2143 ret = tplg_ops->control_setup(sdev, scontrol);
2145 dev_err(sdev->dev, "failed updating IPC struct for control %s\n",
2151 /* set up the IPC structures for the pipeline widgets */
2152 list_for_each_entry(spipe, &sdev->pipeline_list, list) {
2153 struct snd_sof_widget *pipe_widget = spipe->pipe_widget;
2154 struct snd_sof_widget *swidget;
2156 /* Update the scheduler widget's IPC structure */
2157 if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) {
2158 ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget);
2160 dev_err(sdev->dev, "failed updating IPC struct for %s\n",
2161 pipe_widget->widget->name);
2166 /* set the pipeline and update the IPC structure for the non scheduler widgets */
2167 list_for_each_entry(swidget, &sdev->widget_list, list)
2168 if (swidget->widget->id != snd_soc_dapm_scheduler &&
2169 swidget->pipeline_id == pipe_widget->pipeline_id) {
2170 ret = sof_set_widget_pipeline(sdev, spipe, swidget);
2174 if (widget_ops && widget_ops[swidget->id].ipc_setup) {
2175 ret = widget_ops[swidget->id].ipc_setup(swidget);
2178 "failed updating IPC struct for %s\n",
2179 swidget->widget->name);
2186 /* verify topology components loading including dynamic pipelines */
2187 if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) {
2188 if (tplg_ops && tplg_ops->set_up_all_pipelines &&
2189 tplg_ops->tear_down_all_pipelines) {
2190 ret = tplg_ops->set_up_all_pipelines(sdev, true);
2192 dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n",
2197 ret = tplg_ops->tear_down_all_pipelines(sdev, true);
2199 dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n",
2206 /* set up static pipelines */
2207 if (tplg_ops && tplg_ops->set_up_all_pipelines)
2208 return tplg_ops->set_up_all_pipelines(sdev, false);
2213 /* manifest - optional to inform component of manifest */
2214 static int sof_manifest(struct snd_soc_component *scomp, int index,
2215 struct snd_soc_tplg_manifest *man)
2217 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2218 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
2220 if (tplg_ops && tplg_ops->parse_manifest)
2221 return tplg_ops->parse_manifest(scomp, index, man);
2226 /* vendor specific kcontrol handlers available for binding */
2227 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = {
2228 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put},
2229 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put},
2230 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put},
2231 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put},
2234 /* vendor specific bytes ext handlers available for binding */
2235 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = {
2236 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put},
2237 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get},
2240 static struct snd_soc_tplg_ops sof_tplg_ops = {
2241 /* external kcontrol init - used for any driver specific init */
2242 .control_load = sof_control_load,
2243 .control_unload = sof_control_unload,
2245 /* external kcontrol init - used for any driver specific init */
2246 .dapm_route_load = sof_route_load,
2247 .dapm_route_unload = sof_route_unload,
2249 /* external widget init - used for any driver specific init */
2250 /* .widget_load is not currently used */
2251 .widget_ready = sof_widget_ready,
2252 .widget_unload = sof_widget_unload,
2254 /* FE DAI - used for any driver specific init */
2255 .dai_load = sof_dai_load,
2256 .dai_unload = sof_dai_unload,
2258 /* DAI link - used for any driver specific init */
2259 .link_load = sof_link_load,
2260 .link_unload = sof_link_unload,
2262 /* completion - called at completion of firmware loading */
2263 .complete = sof_complete,
2265 /* manifest - optional to inform component of manifest */
2266 .manifest = sof_manifest,
2268 /* vendor specific kcontrol handlers available for binding */
2269 .io_ops = sof_io_ops,
2270 .io_ops_count = ARRAY_SIZE(sof_io_ops),
2272 /* vendor specific bytes ext handlers available for binding */
2273 .bytes_ext_ops = sof_bytes_ext_ops,
2274 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops),
2277 static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2283 static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = {
2284 {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2285 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2286 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2287 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol},
2290 static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol,
2291 unsigned int __user *binary_data,
2297 static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol,
2298 const unsigned int __user *binary_data,
2304 static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = {
2305 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put},
2306 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get},
2309 /* external widget init - used for any driver specific init */
2310 static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index,
2311 struct snd_soc_dapm_widget *w,
2312 struct snd_soc_tplg_dapm_widget *tw)
2314 if (WIDGET_IS_DAI(w->id)) {
2315 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2316 struct snd_sof_widget *swidget;
2317 struct snd_sof_dai dai;
2320 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL);
2324 memset(&dai, 0, sizeof(dai));
2326 ret = sof_connect_dai_widget(scomp, w, tw, &dai);
2332 swidget->scomp = scomp;
2333 swidget->widget = w;
2334 mutex_init(&swidget->setup_mutex);
2335 w->dobj.private = swidget;
2336 list_add(&swidget->list, &sdev->widget_list);
2342 static int sof_dspless_widget_unload(struct snd_soc_component *scomp,
2343 struct snd_soc_dobj *dobj)
2345 struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj);
2347 if (WIDGET_IS_DAI(w->id)) {
2348 struct snd_sof_widget *swidget = dobj->private;
2350 sof_disconnect_dai_widget(scomp, w);
2355 /* remove and free swidget object */
2356 list_del(&swidget->list);
2363 static int sof_dspless_link_load(struct snd_soc_component *scomp, int index,
2364 struct snd_soc_dai_link *link,
2365 struct snd_soc_tplg_link_config *cfg)
2367 link->platforms->name = dev_name(scomp->dev);
2369 /* Set nonatomic property for FE dai links for FE-BE compatibility */
2371 link->nonatomic = true;
2376 static struct snd_soc_tplg_ops sof_dspless_tplg_ops = {
2377 /* external widget init - used for any driver specific init */
2378 .widget_ready = sof_dspless_widget_ready,
2379 .widget_unload = sof_dspless_widget_unload,
2381 /* FE DAI - used for any driver specific init */
2382 .dai_load = sof_dai_load,
2383 .dai_unload = sof_dai_unload,
2385 /* DAI link - used for any driver specific init */
2386 .link_load = sof_dspless_link_load,
2388 /* vendor specific kcontrol handlers available for binding */
2389 .io_ops = sof_dspless_io_ops,
2390 .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops),
2392 /* vendor specific bytes ext handlers available for binding */
2393 .bytes_ext_ops = sof_dspless_bytes_ext_ops,
2394 .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops),
2397 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
2399 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
2400 const struct firmware *fw;
2403 dev_dbg(scomp->dev, "loading topology:%s\n", file);
2405 ret = request_firmware(&fw, file, scomp->dev);
2407 dev_err(scomp->dev, "error: tplg request firmware %s failed err: %d\n",
2410 "you may need to download the firmware from https://github.com/thesofproject/sof-bin/\n");
2414 if (sdev->dspless_mode_selected)
2415 ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw);
2417 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
2420 dev_err(scomp->dev, "error: tplg component load failed %d\n",
2425 release_firmware(fw);
2427 if (ret >= 0 && sdev->led_present)
2428 ret = snd_ctl_led_request();
2432 EXPORT_SYMBOL(snd_sof_load_topology);