1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 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. 5 // 6 // Copyright(c) 2018 Intel Corporation 7 // 8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // 10 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> 18 #include "sof-priv.h" 19 #include "sof-audio.h" 20 #include "ops.h" 21 22 static bool disable_function_topology; 23 module_param(disable_function_topology, bool, 0444); 24 MODULE_PARM_DESC(disable_function_topology, "Disable function topology loading"); 25 26 #define MAX_FEATURE_TPLG_COUNT 16 27 28 static char *feature_topologies[MAX_FEATURE_TPLG_COUNT]; 29 static int feature_tplg_cnt; 30 module_param_array(feature_topologies, charp, &feature_tplg_cnt, 0444); 31 MODULE_PARM_DESC(feature_topologies, "Topology list for virtual loop DAI link"); 32 33 #define COMP_ID_UNASSIGNED 0xffffffff 34 /* 35 * Constants used in the computation of linear volume gain 36 * from dB gain 20th root of 10 in Q1.16 fixed-point notation 37 */ 38 #define VOL_TWENTIETH_ROOT_OF_TEN 73533 39 /* 40th root of 10 in Q1.16 fixed-point notation*/ 40 #define VOL_FORTIETH_ROOT_OF_TEN 69419 41 42 /* 0.5 dB step value in topology TLV */ 43 #define VOL_HALF_DB_STEP 50 44 45 /* TLV data items */ 46 #define TLV_MIN 0 47 #define TLV_STEP 1 48 #define TLV_MUTE 2 49 50 /** 51 * sof_update_ipc_object - Parse multiple sets of tokens within the token array associated with the 52 * token ID. 53 * @scomp: pointer to SOC component 54 * @object: target IPC struct to save the parsed values 55 * @token_id: token ID for the token array to be searched 56 * @tuples: pointer to the tuples array 57 * @num_tuples: number of tuples in the tuples array 58 * @object_size: size of the object 59 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function 60 * looks for @token_instance_num of each token in the token array associated 61 * with the @token_id 62 */ 63 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id, 64 struct snd_sof_tuple *tuples, int num_tuples, 65 size_t object_size, int token_instance_num) 66 { 67 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 68 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 69 const struct sof_token_info *token_list; 70 const struct sof_topology_token *tokens; 71 int i, j; 72 73 token_list = tplg_ops ? tplg_ops->token_list : NULL; 74 /* nothing to do if token_list is NULL */ 75 if (!token_list) 76 return 0; 77 78 if (token_list[token_id].count < 0) { 79 dev_err(scomp->dev, "Invalid token count for token ID: %d\n", token_id); 80 return -EINVAL; 81 } 82 83 /* No tokens to match */ 84 if (!token_list[token_id].count) 85 return 0; 86 87 tokens = token_list[token_id].tokens; 88 if (!tokens) { 89 dev_err(scomp->dev, "Invalid tokens for token id: %d\n", token_id); 90 return -EINVAL; 91 } 92 93 for (i = 0; i < token_list[token_id].count; i++) { 94 int offset = 0; 95 int num_tokens_matched = 0; 96 97 for (j = 0; j < num_tuples; j++) { 98 if (tokens[i].token == tuples[j].token) { 99 switch (tokens[i].type) { 100 case SND_SOC_TPLG_TUPLE_TYPE_WORD: 101 { 102 u32 *val = (u32 *)((u8 *)object + tokens[i].offset + 103 offset); 104 105 *val = tuples[j].value.v; 106 break; 107 } 108 case SND_SOC_TPLG_TUPLE_TYPE_SHORT: 109 case SND_SOC_TPLG_TUPLE_TYPE_BOOL: 110 { 111 u16 *val = (u16 *)((u8 *)object + tokens[i].offset + 112 offset); 113 114 *val = (u16)tuples[j].value.v; 115 break; 116 } 117 case SND_SOC_TPLG_TUPLE_TYPE_STRING: 118 { 119 if (!tokens[i].get_token) { 120 dev_err(scomp->dev, 121 "get_token not defined for token %d in %s\n", 122 tokens[i].token, token_list[token_id].name); 123 return -EINVAL; 124 } 125 126 tokens[i].get_token((void *)tuples[j].value.s, object, 127 tokens[i].offset + offset); 128 break; 129 } 130 default: 131 break; 132 } 133 134 num_tokens_matched++; 135 136 /* found all required sets of current token. Move to the next one */ 137 if (!(num_tokens_matched % token_instance_num)) 138 break; 139 140 /* move to the next object */ 141 offset += object_size; 142 } 143 } 144 } 145 146 return 0; 147 } 148 149 static inline int get_tlv_data(const int *p, int tlv[SOF_TLV_ITEMS]) 150 { 151 /* we only support dB scale TLV type at the moment */ 152 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE) 153 return -EINVAL; 154 155 /* min value in topology tlv data is multiplied by 100 */ 156 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100; 157 158 /* volume steps */ 159 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 160 TLV_DB_SCALE_MASK); 161 162 /* mute ON/OFF */ 163 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 164 TLV_DB_SCALE_MUTE) == 0) 165 tlv[TLV_MUTE] = 0; 166 else 167 tlv[TLV_MUTE] = 1; 168 169 return 0; 170 } 171 172 /* 173 * Function to truncate an unsigned 64-bit number 174 * by x bits and return 32-bit unsigned number. This 175 * function also takes care of rounding while truncating 176 */ 177 static inline u32 vol_shift_64(u64 i, u32 x) 178 { 179 /* do not truncate more than 32 bits */ 180 if (x > 32) 181 x = 32; 182 183 if (x == 0) 184 return (u32)i; 185 186 return (u32)(((i >> (x - 1)) + 1) >> 1); 187 } 188 189 /* 190 * Function to compute a ^ exp where, 191 * a is a fractional number represented by a fixed-point 192 * integer with a fractional world length of "fwl" 193 * exp is an integer 194 * fwl is the fractional word length 195 * Return value is a fractional number represented by a 196 * fixed-point integer with a fractional word length of "fwl" 197 */ 198 static u32 vol_pow32(u32 a, int exp, u32 fwl) 199 { 200 int i, iter; 201 u32 power = 1 << fwl; 202 u64 numerator; 203 204 /* if exponent is 0, return 1 */ 205 if (exp == 0) 206 return power; 207 208 /* determine the number of iterations based on the exponent */ 209 if (exp < 0) 210 iter = exp * -1; 211 else 212 iter = exp; 213 214 /* mutiply a "iter" times to compute power */ 215 for (i = 0; i < iter; i++) { 216 /* 217 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl 218 * Truncate product back to fwl fractional bits with rounding 219 */ 220 power = vol_shift_64((u64)power * a, fwl); 221 } 222 223 if (exp > 0) { 224 /* if exp is positive, return the result */ 225 return power; 226 } 227 228 /* if exp is negative, return the multiplicative inverse */ 229 numerator = (u64)1 << (fwl << 1); 230 do_div(numerator, power); 231 232 return (u32)numerator; 233 } 234 235 /* 236 * Function to calculate volume gain from TLV data. 237 * This function can only handle gain steps that are multiples of 0.5 dB 238 */ 239 u32 vol_compute_gain(u32 value, int *tlv) 240 { 241 int dB_gain; 242 u32 linear_gain; 243 int f_step; 244 245 /* mute volume */ 246 if (value == 0 && tlv[TLV_MUTE]) 247 return 0; 248 249 /* 250 * compute dB gain from tlv. tlv_step 251 * in topology is multiplied by 100 252 */ 253 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100; 254 255 /* 256 * compute linear gain represented by fixed-point 257 * int with VOLUME_FWL fractional bits 258 */ 259 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL); 260 261 /* extract the fractional part of volume step */ 262 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100); 263 264 /* if volume step is an odd multiple of 0.5 dB */ 265 if (f_step == VOL_HALF_DB_STEP && (value & 1)) 266 linear_gain = vol_shift_64((u64)linear_gain * 267 VOL_FORTIETH_ROOT_OF_TEN, 268 VOLUME_FWL); 269 270 return linear_gain; 271 } 272 273 /* 274 * Set up volume table for kcontrols from tlv data 275 * "size" specifies the number of entries in the table 276 */ 277 static int set_up_volume_table(struct snd_sof_control *scontrol, 278 int tlv[SOF_TLV_ITEMS], int size) 279 { 280 struct snd_soc_component *scomp = scontrol->scomp; 281 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 282 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 283 284 if (tplg_ops && tplg_ops->control && tplg_ops->control->set_up_volume_table) 285 return tplg_ops->control->set_up_volume_table(scontrol, tlv, size); 286 287 dev_err(scomp->dev, "Mandatory op %s not set\n", __func__); 288 return -EINVAL; 289 } 290 291 struct sof_dai_types { 292 const char *name; 293 enum sof_ipc_dai_type type; 294 }; 295 296 static const struct sof_dai_types sof_dais[] = { 297 {"SSP", SOF_DAI_INTEL_SSP}, 298 {"HDA", SOF_DAI_INTEL_HDA}, 299 {"DMIC", SOF_DAI_INTEL_DMIC}, 300 {"ALH", SOF_DAI_INTEL_ALH}, 301 {"SAI", SOF_DAI_IMX_SAI}, 302 {"ESAI", SOF_DAI_IMX_ESAI}, 303 {"ACPBT", SOF_DAI_AMD_BT}, 304 {"ACPSP", SOF_DAI_AMD_SP}, 305 {"ACPDMIC", SOF_DAI_AMD_DMIC}, 306 {"ACPHS", SOF_DAI_AMD_HS}, 307 {"AFE", SOF_DAI_MEDIATEK_AFE}, 308 {"ACPSP_VIRTUAL", SOF_DAI_AMD_SP_VIRTUAL}, 309 {"ACPHS_VIRTUAL", SOF_DAI_AMD_HS_VIRTUAL}, 310 {"MICFIL", SOF_DAI_IMX_MICFIL}, 311 {"ACP_SDW", SOF_DAI_AMD_SDW}, 312 {"ACPTDM", SOF_DAI_AMD_I2S}, 313 }; 314 315 static enum sof_ipc_dai_type find_dai(const char *name) 316 { 317 int i; 318 319 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) { 320 if (strcmp(name, sof_dais[i].name) == 0) 321 return sof_dais[i].type; 322 } 323 324 return SOF_DAI_INTEL_NONE; 325 } 326 327 /* 328 * Supported Frame format types and lookup, add new ones to end of list. 329 */ 330 331 struct sof_frame_types { 332 const char *name; 333 enum sof_ipc_frame frame; 334 }; 335 336 static const struct sof_frame_types sof_frames[] = { 337 {"s16le", SOF_IPC_FRAME_S16_LE}, 338 {"s24le", SOF_IPC_FRAME_S24_4LE}, 339 {"s32le", SOF_IPC_FRAME_S32_LE}, 340 {"float", SOF_IPC_FRAME_FLOAT}, 341 }; 342 343 static enum sof_ipc_frame find_format(const char *name) 344 { 345 int i; 346 347 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) { 348 if (strcmp(name, sof_frames[i].name) == 0) 349 return sof_frames[i].frame; 350 } 351 352 /* use s32le if nothing is specified */ 353 return SOF_IPC_FRAME_S32_LE; 354 } 355 356 int get_token_u32(void *elem, void *object, u32 offset) 357 { 358 struct snd_soc_tplg_vendor_value_elem *velem = elem; 359 u32 *val = (u32 *)((u8 *)object + offset); 360 361 *val = le32_to_cpu(velem->value); 362 return 0; 363 } 364 365 int get_token_u16(void *elem, void *object, u32 offset) 366 { 367 struct snd_soc_tplg_vendor_value_elem *velem = elem; 368 u16 *val = (u16 *)((u8 *)object + offset); 369 370 *val = (u16)le32_to_cpu(velem->value); 371 return 0; 372 } 373 374 int get_token_uuid(void *elem, void *object, u32 offset) 375 { 376 struct snd_soc_tplg_vendor_uuid_elem *velem = elem; 377 u8 *dst = (u8 *)object + offset; 378 379 memcpy(dst, velem->uuid, UUID_SIZE); 380 381 return 0; 382 } 383 384 /* 385 * The string gets from topology will be stored in heap, the owner only 386 * holds a char* member point to the heap. 387 */ 388 int get_token_string(void *elem, void *object, u32 offset) 389 { 390 /* "dst" here points to the char* member of the owner */ 391 char **dst = (char **)((u8 *)object + offset); 392 393 *dst = kstrdup(elem, GFP_KERNEL); 394 if (!*dst) 395 return -ENOMEM; 396 return 0; 397 }; 398 399 int get_token_comp_format(void *elem, void *object, u32 offset) 400 { 401 u32 *val = (u32 *)((u8 *)object + offset); 402 403 *val = find_format((const char *)elem); 404 return 0; 405 } 406 407 int get_token_dai_type(void *elem, void *object, u32 offset) 408 { 409 u32 *val = (u32 *)((u8 *)object + offset); 410 411 *val = find_dai((const char *)elem); 412 return 0; 413 } 414 415 /* PCM */ 416 static const struct sof_topology_token stream_tokens[] = { 417 {SOF_TKN_STREAM_PLAYBACK_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 418 offsetof(struct snd_sof_pcm, stream[0].d0i3_compatible)}, 419 {SOF_TKN_STREAM_CAPTURE_COMPATIBLE_D0I3, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 420 offsetof(struct snd_sof_pcm, stream[1].d0i3_compatible)}, 421 {SOF_TKN_STREAM_PLAYBACK_PAUSE_SUPPORTED, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 422 offsetof(struct snd_sof_pcm, stream[0].pause_supported)}, 423 {SOF_TKN_STREAM_CAPTURE_PAUSE_SUPPORTED, SND_SOC_TPLG_TUPLE_TYPE_BOOL, get_token_u16, 424 offsetof(struct snd_sof_pcm, stream[1].pause_supported)}, 425 }; 426 427 /* Leds */ 428 static const struct sof_topology_token led_tokens[] = { 429 {SOF_TKN_MUTE_LED_USE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 430 offsetof(struct snd_sof_led_control, use_led)}, 431 {SOF_TKN_MUTE_LED_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 432 offsetof(struct snd_sof_led_control, direction)}, 433 }; 434 435 static const struct sof_topology_token comp_pin_tokens[] = { 436 {SOF_TKN_COMP_NUM_INPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 437 offsetof(struct snd_sof_widget, num_input_pins)}, 438 {SOF_TKN_COMP_NUM_OUTPUT_PINS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 439 offsetof(struct snd_sof_widget, num_output_pins)}, 440 }; 441 442 static const struct sof_topology_token comp_input_pin_binding_tokens[] = { 443 {SOF_TKN_COMP_INPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING, 444 get_token_string, 0}, 445 }; 446 447 static const struct sof_topology_token comp_output_pin_binding_tokens[] = { 448 {SOF_TKN_COMP_OUTPUT_PIN_BINDING_WNAME, SND_SOC_TPLG_TUPLE_TYPE_STRING, 449 get_token_string, 0}, 450 }; 451 452 /** 453 * sof_parse_uuid_tokens - Parse multiple sets of UUID tokens 454 * @scomp: pointer to soc component 455 * @object: target ipc struct for parsed values 456 * @offset: offset within the object pointer 457 * @tokens: array of struct sof_topology_token containing the tokens to be matched 458 * @num_tokens: number of tokens in tokens array 459 * @array: source pointer to consecutive vendor arrays in topology 460 * 461 * This function parses multiple sets of string type tokens in vendor arrays 462 */ 463 static int sof_parse_uuid_tokens(struct snd_soc_component *scomp, 464 void *object, size_t offset, 465 const struct sof_topology_token *tokens, int num_tokens, 466 struct snd_soc_tplg_vendor_array *array) 467 { 468 struct snd_soc_tplg_vendor_uuid_elem *elem; 469 int found = 0; 470 int i, j; 471 472 /* parse element by element */ 473 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 474 elem = &array->uuid[i]; 475 476 /* search for token */ 477 for (j = 0; j < num_tokens; j++) { 478 /* match token type */ 479 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID) 480 continue; 481 482 /* match token id */ 483 if (tokens[j].token != le32_to_cpu(elem->token)) 484 continue; 485 486 /* matched - now load token */ 487 tokens[j].get_token(elem, object, 488 offset + tokens[j].offset); 489 490 found++; 491 } 492 } 493 494 return found; 495 } 496 497 /** 498 * sof_copy_tuples - Parse tokens and copy them to the @tuples array 499 * @sdev: pointer to struct snd_sof_dev 500 * @array: source pointer to consecutive vendor arrays in topology 501 * @array_size: size of @array 502 * @token_id: Token ID associated with a token array 503 * @token_instance_num: number of times the same @token_id needs to be parsed i.e. the function 504 * looks for @token_instance_num of each token in the token array associated 505 * with the @token_id 506 * @tuples: tuples array to copy the matched tuples to 507 * @tuples_size: size of @tuples 508 * @num_copied_tuples: pointer to the number of copied tuples in the tuples array 509 * 510 */ 511 static int sof_copy_tuples(struct snd_sof_dev *sdev, struct snd_soc_tplg_vendor_array *array, 512 int array_size, u32 token_id, int token_instance_num, 513 struct snd_sof_tuple *tuples, int tuples_size, int *num_copied_tuples) 514 { 515 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 516 const struct sof_token_info *token_list; 517 const struct sof_topology_token *tokens; 518 int found = 0; 519 int num_tokens, asize; 520 int i, j; 521 522 token_list = tplg_ops ? tplg_ops->token_list : NULL; 523 /* nothing to do if token_list is NULL */ 524 if (!token_list) 525 return 0; 526 527 if (!tuples || !num_copied_tuples) { 528 dev_err(sdev->dev, "Invalid tuples array\n"); 529 return -EINVAL; 530 } 531 532 tokens = token_list[token_id].tokens; 533 num_tokens = token_list[token_id].count; 534 535 if (!tokens) { 536 dev_err(sdev->dev, "No token array defined for token ID: %d\n", token_id); 537 return -EINVAL; 538 } 539 540 /* check if there's space in the tuples array for new tokens */ 541 if (*num_copied_tuples >= tuples_size) { 542 dev_err(sdev->dev, "No space in tuples array for new tokens from %s", 543 token_list[token_id].name); 544 return -EINVAL; 545 } 546 547 while (array_size > 0 && found < num_tokens * token_instance_num) { 548 asize = le32_to_cpu(array->size); 549 550 /* validate asize */ 551 if (asize < 0) { 552 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize); 553 return -EINVAL; 554 } 555 556 /* make sure there is enough data before parsing */ 557 array_size -= asize; 558 if (array_size < 0) { 559 dev_err(sdev->dev, "Invalid array size 0x%x\n", asize); 560 return -EINVAL; 561 } 562 563 /* parse element by element */ 564 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 565 /* search for token */ 566 for (j = 0; j < num_tokens; j++) { 567 /* match token type */ 568 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || 569 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || 570 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || 571 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL || 572 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING)) 573 continue; 574 575 if (tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_STRING) { 576 struct snd_soc_tplg_vendor_string_elem *elem; 577 578 elem = &array->string[i]; 579 580 /* match token id */ 581 if (tokens[j].token != le32_to_cpu(elem->token)) 582 continue; 583 584 tuples[*num_copied_tuples].token = tokens[j].token; 585 tuples[*num_copied_tuples].value.s = 586 devm_kasprintf(sdev->dev, GFP_KERNEL, 587 "%s", elem->string); 588 if (!tuples[*num_copied_tuples].value.s) 589 return -ENOMEM; 590 } else { 591 struct snd_soc_tplg_vendor_value_elem *elem; 592 593 elem = &array->value[i]; 594 595 /* match token id */ 596 if (tokens[j].token != le32_to_cpu(elem->token)) 597 continue; 598 599 tuples[*num_copied_tuples].token = tokens[j].token; 600 tuples[*num_copied_tuples].value.v = 601 le32_to_cpu(elem->value); 602 } 603 found++; 604 (*num_copied_tuples)++; 605 606 /* stop if there's no space for any more new tuples */ 607 if (*num_copied_tuples == tuples_size) 608 return 0; 609 } 610 611 /* stop when we've found the required token instances */ 612 if (found == num_tokens * token_instance_num) 613 return 0; 614 } 615 616 /* next array */ 617 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array + asize); 618 } 619 620 return 0; 621 } 622 623 /** 624 * sof_parse_string_tokens - Parse multiple sets of tokens 625 * @scomp: pointer to soc component 626 * @object: target ipc struct for parsed values 627 * @offset: offset within the object pointer 628 * @tokens: array of struct sof_topology_token containing the tokens to be matched 629 * @num_tokens: number of tokens in tokens array 630 * @array: source pointer to consecutive vendor arrays in topology 631 * 632 * This function parses multiple sets of string type tokens in vendor arrays 633 */ 634 static int sof_parse_string_tokens(struct snd_soc_component *scomp, 635 void *object, int offset, 636 const struct sof_topology_token *tokens, int num_tokens, 637 struct snd_soc_tplg_vendor_array *array) 638 { 639 struct snd_soc_tplg_vendor_string_elem *elem; 640 int found = 0; 641 int i, j, ret; 642 643 /* parse element by element */ 644 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 645 elem = &array->string[i]; 646 647 /* search for token */ 648 for (j = 0; j < num_tokens; j++) { 649 /* match token type */ 650 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING) 651 continue; 652 653 /* match token id */ 654 if (tokens[j].token != le32_to_cpu(elem->token)) 655 continue; 656 657 /* matched - now load token */ 658 ret = tokens[j].get_token(elem->string, object, offset + tokens[j].offset); 659 if (ret < 0) 660 return ret; 661 662 found++; 663 } 664 } 665 666 return found; 667 } 668 669 /** 670 * sof_parse_word_tokens - Parse multiple sets of tokens 671 * @scomp: pointer to soc component 672 * @object: target ipc struct for parsed values 673 * @offset: offset within the object pointer 674 * @tokens: array of struct sof_topology_token containing the tokens to be matched 675 * @num_tokens: number of tokens in tokens array 676 * @array: source pointer to consecutive vendor arrays in topology 677 * 678 * This function parses multiple sets of word type tokens in vendor arrays 679 */ 680 static int sof_parse_word_tokens(struct snd_soc_component *scomp, 681 void *object, int offset, 682 const struct sof_topology_token *tokens, int num_tokens, 683 struct snd_soc_tplg_vendor_array *array) 684 { 685 struct snd_soc_tplg_vendor_value_elem *elem; 686 int found = 0; 687 int i, j; 688 689 /* parse element by element */ 690 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 691 elem = &array->value[i]; 692 693 /* search for token */ 694 for (j = 0; j < num_tokens; j++) { 695 /* match token type */ 696 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || 697 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || 698 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || 699 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL)) 700 continue; 701 702 /* match token id */ 703 if (tokens[j].token != le32_to_cpu(elem->token)) 704 continue; 705 706 /* load token */ 707 tokens[j].get_token(elem, object, offset + tokens[j].offset); 708 709 found++; 710 } 711 } 712 713 return found; 714 } 715 716 /** 717 * sof_parse_token_sets - Parse multiple sets of tokens 718 * @scomp: pointer to soc component 719 * @object: target ipc struct for parsed values 720 * @tokens: token definition array describing what tokens to parse 721 * @count: number of tokens in definition array 722 * @array: source pointer to consecutive vendor arrays in topology 723 * @array_size: total size of @array 724 * @token_instance_num: number of times the same tokens needs to be parsed i.e. the function 725 * looks for @token_instance_num of each token in the @tokens 726 * @object_size: offset to next target ipc struct with multiple sets 727 * 728 * This function parses multiple sets of tokens in vendor arrays into 729 * consecutive ipc structs. 730 */ 731 static int sof_parse_token_sets(struct snd_soc_component *scomp, 732 void *object, const struct sof_topology_token *tokens, 733 int count, struct snd_soc_tplg_vendor_array *array, 734 int array_size, int token_instance_num, size_t object_size) 735 { 736 size_t offset = 0; 737 int found = 0; 738 int total = 0; 739 int asize; 740 int ret; 741 742 while (array_size > 0 && total < count * token_instance_num) { 743 if (array_size < (int)sizeof(*array)) 744 return -EINVAL; 745 746 asize = le32_to_cpu(array->size); 747 748 /* validate asize */ 749 if (asize < (int)sizeof(*array)) { 750 dev_err(scomp->dev, "error: invalid array size 0x%x\n", 751 asize); 752 return -EINVAL; 753 } 754 755 /* make sure there is enough data before parsing */ 756 array_size -= asize; 757 if (array_size < 0) { 758 dev_err(scomp->dev, "error: invalid array size 0x%x\n", 759 asize); 760 return -EINVAL; 761 } 762 763 /* call correct parser depending on type */ 764 switch (le32_to_cpu(array->type)) { 765 case SND_SOC_TPLG_TUPLE_TYPE_UUID: 766 found += sof_parse_uuid_tokens(scomp, object, offset, tokens, count, 767 array); 768 break; 769 case SND_SOC_TPLG_TUPLE_TYPE_STRING: 770 771 ret = sof_parse_string_tokens(scomp, object, offset, tokens, count, 772 array); 773 if (ret < 0) { 774 dev_err(scomp->dev, "error: no memory to copy string token\n"); 775 return ret; 776 } 777 778 found += ret; 779 break; 780 case SND_SOC_TPLG_TUPLE_TYPE_BOOL: 781 case SND_SOC_TPLG_TUPLE_TYPE_BYTE: 782 case SND_SOC_TPLG_TUPLE_TYPE_WORD: 783 case SND_SOC_TPLG_TUPLE_TYPE_SHORT: 784 found += sof_parse_word_tokens(scomp, object, offset, tokens, count, 785 array); 786 break; 787 default: 788 dev_err(scomp->dev, "error: unknown token type %u\n", 789 le32_to_cpu(array->type)); 790 return -EINVAL; 791 } 792 793 /* next array */ 794 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array 795 + asize); 796 797 /* move to next target struct */ 798 if (found >= count) { 799 offset += object_size; 800 total += found; 801 found = 0; 802 } 803 } 804 805 return 0; 806 } 807 808 /** 809 * sof_parse_tokens - Parse one set of tokens 810 * @scomp: pointer to soc component 811 * @object: target ipc struct for parsed values 812 * @tokens: token definition array describing what tokens to parse 813 * @num_tokens: number of tokens in definition array 814 * @array: source pointer to consecutive vendor arrays in topology 815 * @array_size: total size of @array 816 * 817 * This function parses a single set of tokens in vendor arrays into 818 * consecutive ipc structs. 819 */ 820 static int sof_parse_tokens(struct snd_soc_component *scomp, void *object, 821 const struct sof_topology_token *tokens, int num_tokens, 822 struct snd_soc_tplg_vendor_array *array, 823 int array_size) 824 825 { 826 /* 827 * sof_parse_tokens is used when topology contains only a single set of 828 * identical tuples arrays. So additional parameters to 829 * sof_parse_token_sets are sets = 1 (only 1 set) and 830 * object_size = 0 (irrelevant). 831 */ 832 return sof_parse_token_sets(scomp, object, tokens, num_tokens, array, 833 array_size, 1, 0); 834 } 835 836 /* 837 * Standard Kcontrols. 838 */ 839 840 static int sof_control_load_volume(struct snd_soc_component *scomp, 841 struct snd_sof_control *scontrol, 842 struct snd_kcontrol_new *kc, 843 struct snd_soc_tplg_ctl_hdr *hdr) 844 { 845 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 846 struct snd_soc_tplg_mixer_control *mc = 847 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr); 848 int tlv[SOF_TLV_ITEMS]; 849 u32 min, max; 850 unsigned int mask; 851 int ret; 852 853 /* validate topology data */ 854 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) 855 return -EINVAL; 856 857 min = le32_to_cpu(mc->min); 858 max = le32_to_cpu(mc->max); 859 if (min > max || max >= INT_MAX) 860 return -EINVAL; 861 862 /* 863 * If control has more than 2 channels we need to override the info. This is because even if 864 * ASoC layer has defined topology's max channel count to SND_SOC_TPLG_MAX_CHAN = 8, the 865 * pre-defined dapm control types (and related functions) creating the actual control 866 * restrict the channels only to mono or stereo. 867 */ 868 if (le32_to_cpu(mc->num_channels) > 2) 869 kc->info = snd_sof_volume_info; 870 871 scontrol->comp_id = sdev->next_comp_id; 872 scontrol->min_volume_step = min; 873 scontrol->max_volume_step = max; 874 scontrol->num_channels = le32_to_cpu(mc->num_channels); 875 876 scontrol->max = max; 877 if (max == 1) 878 goto skip; 879 880 /* extract tlv data */ 881 if (!kc->tlv.p || get_tlv_data(kc->tlv.p, tlv) < 0) { 882 dev_err(scomp->dev, "error: invalid TLV data\n"); 883 return -EINVAL; 884 } 885 886 /* set up volume table */ 887 ret = set_up_volume_table(scontrol, tlv, max + 1); 888 if (ret < 0) { 889 dev_err(scomp->dev, "error: setting up volume table\n"); 890 return ret; 891 } 892 893 skip: 894 /* set up possible led control from mixer private data */ 895 ret = sof_parse_tokens(scomp, &scontrol->led_ctl, led_tokens, 896 ARRAY_SIZE(led_tokens), mc->priv.array, 897 le32_to_cpu(mc->priv.size)); 898 if (ret != 0) { 899 dev_err(scomp->dev, "error: parse led tokens failed %u\n", 900 le32_to_cpu(mc->priv.size)); 901 goto err; 902 } 903 904 if (scontrol->led_ctl.use_led) { 905 mask = scontrol->led_ctl.direction ? SNDRV_CTL_ELEM_ACCESS_MIC_LED : 906 SNDRV_CTL_ELEM_ACCESS_SPK_LED; 907 scontrol->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK; 908 scontrol->access |= mask; 909 kc->access &= ~SNDRV_CTL_ELEM_ACCESS_LED_MASK; 910 kc->access |= mask; 911 sdev->led_present = true; 912 } 913 914 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d\n", 915 scontrol->comp_id, scontrol->num_channels); 916 917 return 0; 918 919 err: 920 if (max > 1) 921 kfree(scontrol->volume_table); 922 923 return ret; 924 } 925 926 static int sof_control_load_enum(struct snd_soc_component *scomp, 927 struct snd_sof_control *scontrol, 928 struct snd_kcontrol_new *kc, 929 struct snd_soc_tplg_ctl_hdr *hdr) 930 { 931 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 932 struct snd_soc_tplg_enum_control *ec = 933 container_of(hdr, struct snd_soc_tplg_enum_control, hdr); 934 935 /* validate topology data */ 936 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN) 937 return -EINVAL; 938 939 scontrol->comp_id = sdev->next_comp_id; 940 scontrol->num_channels = le32_to_cpu(ec->num_channels); 941 942 dev_dbg(scomp->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n", 943 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id); 944 945 return 0; 946 } 947 948 static int sof_control_load_bytes(struct snd_soc_component *scomp, 949 struct snd_sof_control *scontrol, 950 struct snd_kcontrol_new *kc, 951 struct snd_soc_tplg_ctl_hdr *hdr) 952 { 953 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 954 struct snd_soc_tplg_bytes_control *control = 955 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); 956 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value; 957 size_t priv_size = le32_to_cpu(control->priv.size); 958 959 scontrol->max_size = sbe->max; 960 scontrol->comp_id = sdev->next_comp_id; 961 962 dev_dbg(scomp->dev, "tplg: load kcontrol index %d\n", scontrol->comp_id); 963 964 /* copy the private data */ 965 if (priv_size > 0) { 966 scontrol->priv = kmemdup(control->priv.data, priv_size, GFP_KERNEL); 967 if (!scontrol->priv) 968 return -ENOMEM; 969 970 scontrol->priv_size = priv_size; 971 } 972 973 return 0; 974 } 975 976 /* external kcontrol init - used for any driver specific init */ 977 static int sof_control_load(struct snd_soc_component *scomp, int index, 978 struct snd_kcontrol_new *kc, 979 struct snd_soc_tplg_ctl_hdr *hdr) 980 { 981 struct soc_mixer_control *sm; 982 struct soc_bytes_ext *sbe; 983 struct soc_enum *se; 984 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 985 struct snd_soc_dobj *dobj; 986 struct snd_sof_control *scontrol; 987 int ret; 988 989 dev_dbg(scomp->dev, "tplg: load control type %u name : %s\n", 990 le32_to_cpu(hdr->type), hdr->name); 991 992 scontrol = kzalloc_obj(*scontrol); 993 if (!scontrol) 994 return -ENOMEM; 995 996 scontrol->name = kstrdup(hdr->name, GFP_KERNEL); 997 if (!scontrol->name) { 998 kfree(scontrol); 999 return -ENOMEM; 1000 } 1001 1002 scontrol->scomp = scomp; 1003 scontrol->access = kc->access; 1004 scontrol->info_type = le32_to_cpu(hdr->ops.info); 1005 scontrol->index = kc->index; 1006 1007 switch (le32_to_cpu(hdr->ops.info)) { 1008 case SND_SOC_TPLG_CTL_VOLSW: 1009 case SND_SOC_TPLG_CTL_VOLSW_SX: 1010 case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 1011 sm = (struct soc_mixer_control *)kc->private_value; 1012 dobj = &sm->dobj; 1013 ret = sof_control_load_volume(scomp, scontrol, kc, hdr); 1014 break; 1015 case SND_SOC_TPLG_CTL_BYTES: 1016 sbe = (struct soc_bytes_ext *)kc->private_value; 1017 dobj = &sbe->dobj; 1018 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr); 1019 break; 1020 case SND_SOC_TPLG_CTL_ENUM: 1021 case SND_SOC_TPLG_CTL_ENUM_VALUE: 1022 se = (struct soc_enum *)kc->private_value; 1023 dobj = &se->dobj; 1024 ret = sof_control_load_enum(scomp, scontrol, kc, hdr); 1025 break; 1026 case SND_SOC_TPLG_CTL_RANGE: 1027 case SND_SOC_TPLG_CTL_STROBE: 1028 case SND_SOC_TPLG_DAPM_CTL_VOLSW: 1029 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 1030 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 1031 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1032 case SND_SOC_TPLG_DAPM_CTL_PIN: 1033 default: 1034 dev_warn(scomp->dev, "control type not supported %u:%u:%u\n", 1035 le32_to_cpu(hdr->ops.get), 1036 le32_to_cpu(hdr->ops.put), 1037 le32_to_cpu(hdr->ops.info)); 1038 kfree(scontrol->name); 1039 kfree(scontrol); 1040 return 0; 1041 } 1042 1043 if (ret < 0) { 1044 kfree(scontrol->name); 1045 kfree(scontrol); 1046 return ret; 1047 } 1048 1049 scontrol->led_ctl.led_value = -1; 1050 1051 dobj->private = scontrol; 1052 list_add(&scontrol->list, &sdev->kcontrol_list); 1053 return 0; 1054 } 1055 1056 static int sof_control_unload(struct snd_soc_component *scomp, 1057 struct snd_soc_dobj *dobj) 1058 { 1059 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1060 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1061 struct snd_sof_control *scontrol = dobj->private; 1062 int ret = 0; 1063 1064 dev_dbg(scomp->dev, "tplg: unload control name : %s\n", scontrol->name); 1065 1066 if (tplg_ops && tplg_ops->control_free) { 1067 ret = tplg_ops->control_free(sdev, scontrol); 1068 if (ret < 0) 1069 dev_err(scomp->dev, "failed to free control: %s\n", scontrol->name); 1070 } 1071 1072 /* free all data before returning in case of error too */ 1073 kfree(scontrol->ipc_control_data); 1074 kfree(scontrol->priv); 1075 kfree(scontrol->name); 1076 list_del(&scontrol->list); 1077 kfree(scontrol); 1078 1079 return ret; 1080 } 1081 1082 /* 1083 * DAI Topology 1084 */ 1085 1086 static int sof_connect_dai_widget(struct snd_soc_component *scomp, 1087 struct snd_soc_dapm_widget *w, 1088 struct snd_soc_tplg_dapm_widget *tw, 1089 struct snd_sof_dai *dai) 1090 { 1091 struct snd_soc_card *card = scomp->card; 1092 struct snd_soc_pcm_runtime *rtd, *full, *partial; 1093 struct snd_soc_dai *cpu_dai; 1094 int stream; 1095 int i; 1096 1097 if (!w->sname) { 1098 dev_err(scomp->dev, "Widget %s does not have stream\n", w->name); 1099 return -EINVAL; 1100 } 1101 1102 if (w->id == snd_soc_dapm_dai_out) 1103 stream = SNDRV_PCM_STREAM_CAPTURE; 1104 else if (w->id == snd_soc_dapm_dai_in) 1105 stream = SNDRV_PCM_STREAM_PLAYBACK; 1106 else 1107 goto end; 1108 1109 full = NULL; 1110 partial = NULL; 1111 for_each_card_rtds(card, rtd) { 1112 /* does stream match DAI link ? */ 1113 if (rtd->dai_link->stream_name) { 1114 if (!strcmp(rtd->dai_link->stream_name, w->sname)) { 1115 full = rtd; 1116 break; 1117 } else if (strstr(rtd->dai_link->stream_name, w->sname)) { 1118 partial = rtd; 1119 } 1120 } 1121 } 1122 1123 rtd = full ? full : partial; 1124 if (rtd) { 1125 for_each_rtd_cpu_dais(rtd, i, cpu_dai) { 1126 /* 1127 * Please create DAI widget in the right order 1128 * to ensure BE will connect to the right DAI 1129 * widget. 1130 */ 1131 if (!snd_soc_dai_get_widget(cpu_dai, stream)) { 1132 snd_soc_dai_set_widget(cpu_dai, stream, w); 1133 break; 1134 } 1135 } 1136 if (i == rtd->dai_link->num_cpus) { 1137 dev_err(scomp->dev, "error: can't find BE for DAI %s\n", w->name); 1138 1139 return -EINVAL; 1140 } 1141 1142 dai->name = rtd->dai_link->name; 1143 dev_dbg(scomp->dev, "tplg: connected widget %s -> DAI link %s\n", 1144 w->name, rtd->dai_link->name); 1145 } 1146 end: 1147 /* check we have a connection */ 1148 if (!dai->name) { 1149 dev_err(scomp->dev, "error: can't connect DAI %s stream %s\n", 1150 w->name, w->sname); 1151 return -EINVAL; 1152 } 1153 1154 return 0; 1155 } 1156 1157 static void sof_disconnect_dai_widget(struct snd_soc_component *scomp, 1158 struct snd_soc_dapm_widget *w) 1159 { 1160 struct snd_soc_card *card = scomp->card; 1161 struct snd_soc_pcm_runtime *rtd; 1162 const char *sname = w->sname; 1163 struct snd_soc_dai *cpu_dai; 1164 int i, stream; 1165 1166 if (!sname) 1167 return; 1168 1169 if (w->id == snd_soc_dapm_dai_out) 1170 stream = SNDRV_PCM_STREAM_CAPTURE; 1171 else if (w->id == snd_soc_dapm_dai_in) 1172 stream = SNDRV_PCM_STREAM_PLAYBACK; 1173 else 1174 return; 1175 1176 for_each_card_rtds(card, rtd) { 1177 /* does stream match DAI link ? */ 1178 if (!rtd->dai_link->stream_name || 1179 !strstr(rtd->dai_link->stream_name, sname)) 1180 continue; 1181 1182 for_each_rtd_cpu_dais(rtd, i, cpu_dai) 1183 if (snd_soc_dai_get_widget(cpu_dai, stream) == w) { 1184 snd_soc_dai_set_widget(cpu_dai, stream, NULL); 1185 break; 1186 } 1187 } 1188 } 1189 1190 /* bind PCM ID to host component ID */ 1191 static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm, 1192 int dir) 1193 { 1194 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1195 struct snd_sof_widget *host_widget; 1196 1197 if (sdev->dspless_mode_selected) 1198 return 0; 1199 1200 host_widget = snd_sof_find_swidget_sname(scomp, 1201 spcm->pcm.caps[dir].name, 1202 dir); 1203 if (!host_widget) { 1204 dev_err(scomp->dev, "can't find host comp to bind pcm\n"); 1205 return -EINVAL; 1206 } 1207 1208 spcm->stream[dir].comp_id = host_widget->comp_id; 1209 1210 return 0; 1211 } 1212 1213 static int sof_get_token_value(u32 token_id, struct snd_sof_tuple *tuples, int num_tuples) 1214 { 1215 int i; 1216 1217 if (!tuples) 1218 return -EINVAL; 1219 1220 for (i = 0; i < num_tuples; i++) { 1221 if (tuples[i].token == token_id) 1222 return tuples[i].value.v; 1223 } 1224 1225 return -EINVAL; 1226 } 1227 1228 static int sof_widget_parse_tokens(struct snd_soc_component *scomp, struct snd_sof_widget *swidget, 1229 struct snd_soc_tplg_dapm_widget *tw, 1230 enum sof_tokens *object_token_list, int count) 1231 { 1232 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1233 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1234 struct snd_soc_tplg_private *private = &tw->priv; 1235 const struct sof_token_info *token_list; 1236 int num_tuples = 0; 1237 int ret, i; 1238 1239 token_list = tplg_ops ? tplg_ops->token_list : NULL; 1240 /* nothing to do if token_list is NULL */ 1241 if (!token_list) 1242 return 0; 1243 1244 if (count > 0 && !object_token_list) { 1245 dev_err(scomp->dev, "No token list for widget %s\n", swidget->widget->name); 1246 return -EINVAL; 1247 } 1248 1249 /* calculate max size of tuples array */ 1250 for (i = 0; i < count; i++) 1251 num_tuples += token_list[object_token_list[i]].count; 1252 1253 /* allocate memory for tuples array */ 1254 swidget->tuples = kzalloc_objs(*swidget->tuples, num_tuples); 1255 if (!swidget->tuples) 1256 return -ENOMEM; 1257 1258 /* parse token list for widget */ 1259 for (i = 0; i < count; i++) { 1260 int num_sets = 1; 1261 1262 if (object_token_list[i] >= SOF_TOKEN_COUNT) { 1263 dev_err(scomp->dev, "Invalid token id %d for widget %s\n", 1264 object_token_list[i], swidget->widget->name); 1265 ret = -EINVAL; 1266 goto err; 1267 } 1268 1269 switch (object_token_list[i]) { 1270 case SOF_COMP_EXT_TOKENS: 1271 /* parse and save UUID in swidget */ 1272 ret = sof_parse_tokens(scomp, swidget, 1273 token_list[object_token_list[i]].tokens, 1274 token_list[object_token_list[i]].count, 1275 private->array, le32_to_cpu(private->size)); 1276 if (ret < 0) { 1277 dev_err(scomp->dev, "Failed parsing %s for widget %s\n", 1278 token_list[object_token_list[i]].name, 1279 swidget->widget->name); 1280 goto err; 1281 } 1282 1283 continue; 1284 case SOF_IN_AUDIO_FORMAT_TOKENS: 1285 num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_INPUT_AUDIO_FORMATS, 1286 swidget->tuples, swidget->num_tuples); 1287 if (num_sets < 0) { 1288 dev_err(sdev->dev, "Invalid input audio format count for %s\n", 1289 swidget->widget->name); 1290 ret = num_sets; 1291 goto err; 1292 } 1293 break; 1294 case SOF_OUT_AUDIO_FORMAT_TOKENS: 1295 num_sets = sof_get_token_value(SOF_TKN_COMP_NUM_OUTPUT_AUDIO_FORMATS, 1296 swidget->tuples, swidget->num_tuples); 1297 if (num_sets < 0) { 1298 dev_err(sdev->dev, "Invalid output audio format count for %s\n", 1299 swidget->widget->name); 1300 ret = num_sets; 1301 goto err; 1302 } 1303 break; 1304 default: 1305 break; 1306 } 1307 1308 if (num_sets > 1) { 1309 struct snd_sof_tuple *new_tuples; 1310 1311 num_tuples += token_list[object_token_list[i]].count * (num_sets - 1); 1312 new_tuples = krealloc_array(swidget->tuples, 1313 num_tuples, sizeof(*new_tuples), GFP_KERNEL); 1314 if (!new_tuples) { 1315 ret = -ENOMEM; 1316 goto err; 1317 } 1318 1319 swidget->tuples = new_tuples; 1320 } 1321 1322 /* copy one set of tuples per token ID into swidget->tuples */ 1323 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 1324 object_token_list[i], num_sets, swidget->tuples, 1325 num_tuples, &swidget->num_tuples); 1326 if (ret < 0) { 1327 dev_err(scomp->dev, "Failed parsing %s for widget %s err: %d\n", 1328 token_list[object_token_list[i]].name, swidget->widget->name, ret); 1329 goto err; 1330 } 1331 } 1332 1333 return 0; 1334 err: 1335 kfree(swidget->tuples); 1336 return ret; 1337 } 1338 1339 static void sof_free_pin_binding(struct snd_sof_widget *swidget, 1340 bool pin_type) 1341 { 1342 char **pin_binding; 1343 u32 num_pins; 1344 int i; 1345 1346 if (pin_type == SOF_PIN_TYPE_INPUT) { 1347 pin_binding = swidget->input_pin_binding; 1348 num_pins = swidget->num_input_pins; 1349 } else { 1350 pin_binding = swidget->output_pin_binding; 1351 num_pins = swidget->num_output_pins; 1352 } 1353 1354 if (pin_binding) { 1355 for (i = 0; i < num_pins; i++) 1356 kfree(pin_binding[i]); 1357 } 1358 1359 kfree(pin_binding); 1360 } 1361 1362 static int sof_parse_pin_binding(struct snd_sof_widget *swidget, 1363 struct snd_soc_tplg_private *priv, bool pin_type) 1364 { 1365 const struct sof_topology_token *pin_binding_token; 1366 char *pin_binding[SOF_WIDGET_MAX_NUM_PINS]; 1367 int token_count; 1368 u32 num_pins; 1369 char **pb; 1370 int ret; 1371 int i; 1372 1373 if (pin_type == SOF_PIN_TYPE_INPUT) { 1374 num_pins = swidget->num_input_pins; 1375 pin_binding_token = comp_input_pin_binding_tokens; 1376 token_count = ARRAY_SIZE(comp_input_pin_binding_tokens); 1377 } else { 1378 num_pins = swidget->num_output_pins; 1379 pin_binding_token = comp_output_pin_binding_tokens; 1380 token_count = ARRAY_SIZE(comp_output_pin_binding_tokens); 1381 } 1382 1383 memset(pin_binding, 0, SOF_WIDGET_MAX_NUM_PINS * sizeof(char *)); 1384 ret = sof_parse_token_sets(swidget->scomp, pin_binding, pin_binding_token, 1385 token_count, priv->array, le32_to_cpu(priv->size), 1386 num_pins, sizeof(char *)); 1387 if (ret < 0) 1388 goto err; 1389 1390 /* copy pin binding array to swidget only if it is defined in topology */ 1391 if (pin_binding[0]) { 1392 pb = kmemdup_array(pin_binding, num_pins, sizeof(char *), GFP_KERNEL); 1393 if (!pb) { 1394 ret = -ENOMEM; 1395 goto err; 1396 } 1397 if (pin_type == SOF_PIN_TYPE_INPUT) 1398 swidget->input_pin_binding = pb; 1399 else 1400 swidget->output_pin_binding = pb; 1401 } 1402 1403 return 0; 1404 1405 err: 1406 for (i = 0; i < num_pins; i++) 1407 kfree(pin_binding[i]); 1408 1409 return ret; 1410 } 1411 1412 static int get_w_no_wname_in_long_name(void *elem, void *object, u32 offset) 1413 { 1414 struct snd_soc_tplg_vendor_value_elem *velem = elem; 1415 struct snd_soc_dapm_widget *w = object; 1416 1417 w->no_wname_in_kcontrol_name = !!le32_to_cpu(velem->value); 1418 return 0; 1419 } 1420 1421 static const struct sof_topology_token dapm_widget_tokens[] = { 1422 {SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME, SND_SOC_TPLG_TUPLE_TYPE_BOOL, 1423 get_w_no_wname_in_long_name, 0} 1424 }; 1425 1426 /* external widget init - used for any driver specific init */ 1427 static int sof_widget_ready(struct snd_soc_component *scomp, int index, 1428 struct snd_soc_dapm_widget *w, 1429 struct snd_soc_tplg_dapm_widget *tw) 1430 { 1431 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1432 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1433 const struct sof_ipc_tplg_widget_ops *widget_ops; 1434 struct snd_soc_tplg_private *priv = &tw->priv; 1435 enum sof_tokens *token_list = NULL; 1436 struct snd_sof_widget *swidget; 1437 struct snd_sof_dai *dai; 1438 int token_list_size = 0; 1439 int ret = 0; 1440 1441 swidget = kzalloc_obj(*swidget); 1442 if (!swidget) 1443 return -ENOMEM; 1444 1445 swidget->scomp = scomp; 1446 swidget->widget = w; 1447 swidget->comp_id = sdev->next_comp_id++; 1448 swidget->id = w->id; 1449 swidget->pipeline_id = index; 1450 swidget->private = NULL; 1451 mutex_init(&swidget->setup_mutex); 1452 1453 ida_init(&swidget->output_queue_ida); 1454 ida_init(&swidget->input_queue_ida); 1455 1456 ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, ARRAY_SIZE(dapm_widget_tokens), 1457 priv->array, le32_to_cpu(priv->size)); 1458 if (ret < 0) { 1459 dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n", 1460 w->name); 1461 goto widget_free; 1462 } 1463 1464 ret = sof_parse_tokens(scomp, swidget, comp_pin_tokens, 1465 ARRAY_SIZE(comp_pin_tokens), priv->array, 1466 le32_to_cpu(priv->size)); 1467 if (ret < 0) { 1468 dev_err(scomp->dev, "failed to parse component pin tokens for %s\n", 1469 w->name); 1470 goto widget_free; 1471 } 1472 1473 if (swidget->num_input_pins > SOF_WIDGET_MAX_NUM_PINS || 1474 swidget->num_output_pins > SOF_WIDGET_MAX_NUM_PINS) { 1475 dev_err(scomp->dev, "invalid pins for %s: [input: %d, output: %d]\n", 1476 swidget->widget->name, swidget->num_input_pins, swidget->num_output_pins); 1477 ret = -EINVAL; 1478 goto widget_free; 1479 } 1480 1481 if (swidget->num_input_pins > 1) { 1482 ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_INPUT); 1483 /* on parsing error, pin binding is not allocated, nothing to free. */ 1484 if (ret < 0) { 1485 dev_err(scomp->dev, "failed to parse input pin binding for %s\n", 1486 w->name); 1487 goto widget_free; 1488 } 1489 } 1490 1491 if (swidget->num_output_pins > 1) { 1492 ret = sof_parse_pin_binding(swidget, priv, SOF_PIN_TYPE_OUTPUT); 1493 /* on parsing error, pin binding is not allocated, nothing to free. */ 1494 if (ret < 0) { 1495 dev_err(scomp->dev, "failed to parse output pin binding for %s\n", 1496 w->name); 1497 goto widget_free; 1498 } 1499 } 1500 1501 dev_dbg(scomp->dev, 1502 "tplg: widget %d (%s) is ready [type: %d, pipe: %d, pins: %d / %d, stream: %s]\n", 1503 swidget->comp_id, w->name, swidget->id, index, 1504 swidget->num_input_pins, swidget->num_output_pins, 1505 strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 ? w->sname : "none"); 1506 1507 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 1508 if (widget_ops) { 1509 token_list = widget_ops[w->id].token_list; 1510 token_list_size = widget_ops[w->id].token_list_size; 1511 } 1512 1513 /* handle any special case widgets */ 1514 switch (w->id) { 1515 case snd_soc_dapm_dai_in: 1516 case snd_soc_dapm_dai_out: 1517 dai = kzalloc_obj(*dai); 1518 if (!dai) { 1519 ret = -ENOMEM; 1520 goto widget_free; 1521 } 1522 1523 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); 1524 if (!ret) 1525 ret = sof_connect_dai_widget(scomp, w, tw, dai); 1526 if (ret < 0) { 1527 kfree(dai); 1528 break; 1529 } 1530 list_add(&dai->list, &sdev->dai_list); 1531 swidget->private = dai; 1532 break; 1533 case snd_soc_dapm_decoder: 1534 case snd_soc_dapm_encoder: 1535 case snd_soc_dapm_effect: 1536 /* check we have some tokens - we need at least process type */ 1537 if (le32_to_cpu(tw->priv.size) == 0) { 1538 dev_err(scomp->dev, "error: process tokens not found\n"); 1539 ret = -EINVAL; 1540 break; 1541 } 1542 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); 1543 break; 1544 case snd_soc_dapm_pga: 1545 if (!le32_to_cpu(tw->num_kcontrols)) { 1546 dev_err(scomp->dev, "invalid kcontrol count %u for volume\n", 1547 le32_to_cpu(tw->num_kcontrols)); 1548 ret = -EINVAL; 1549 break; 1550 } 1551 1552 fallthrough; 1553 case snd_soc_dapm_mixer: 1554 case snd_soc_dapm_buffer: 1555 case snd_soc_dapm_scheduler: 1556 case snd_soc_dapm_aif_out: 1557 case snd_soc_dapm_aif_in: 1558 case snd_soc_dapm_src: 1559 case snd_soc_dapm_asrc: 1560 case snd_soc_dapm_siggen: 1561 case snd_soc_dapm_mux: 1562 case snd_soc_dapm_demux: 1563 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); 1564 break; 1565 case snd_soc_dapm_switch: 1566 case snd_soc_dapm_dai_link: 1567 case snd_soc_dapm_kcontrol: 1568 default: 1569 dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name); 1570 break; 1571 } 1572 1573 /* check token parsing reply */ 1574 if (ret < 0) { 1575 dev_err(scomp->dev, 1576 "failed to add widget type %d name : %s stream %s\n", 1577 swidget->id, tw->name, strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 1578 ? tw->sname : "none"); 1579 goto widget_free; 1580 } 1581 1582 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) { 1583 swidget->core = SOF_DSP_PRIMARY_CORE; 1584 } else { 1585 int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples, 1586 swidget->num_tuples); 1587 1588 if (core >= 0) { 1589 if (core > sdev->num_cores - 1) { 1590 dev_info(scomp->dev, 1591 "out of range core id for %s, moving it %d -> %d\n", 1592 swidget->widget->name, core, SOF_DSP_PRIMARY_CORE); 1593 core = SOF_DSP_PRIMARY_CORE; 1594 } 1595 swidget->core = core; 1596 } 1597 } 1598 1599 /* bind widget to external event */ 1600 if (tw->event_type) { 1601 if (widget_ops && widget_ops[w->id].bind_event) { 1602 ret = widget_ops[w->id].bind_event(scomp, swidget, 1603 le16_to_cpu(tw->event_type)); 1604 if (ret) { 1605 dev_err(scomp->dev, "widget event binding failed for %s\n", 1606 swidget->widget->name); 1607 goto free; 1608 } 1609 } 1610 } 1611 1612 /* create and add pipeline for scheduler type widgets */ 1613 if (w->id == snd_soc_dapm_scheduler) { 1614 struct snd_sof_pipeline *spipe; 1615 1616 spipe = kzalloc_obj(*spipe); 1617 if (!spipe) { 1618 ret = -ENOMEM; 1619 goto free; 1620 } 1621 1622 spipe->pipe_widget = swidget; 1623 swidget->spipe = spipe; 1624 list_add(&spipe->list, &sdev->pipeline_list); 1625 } 1626 1627 w->dobj.private = swidget; 1628 list_add(&swidget->list, &sdev->widget_list); 1629 return ret; 1630 free: 1631 kfree(swidget->private); 1632 kfree(swidget->tuples); 1633 widget_free: 1634 kfree(swidget); 1635 return ret; 1636 } 1637 1638 static int sof_route_unload(struct snd_soc_component *scomp, 1639 struct snd_soc_dobj *dobj) 1640 { 1641 struct snd_sof_route *sroute; 1642 1643 sroute = dobj->private; 1644 if (!sroute) 1645 return 0; 1646 1647 /* free sroute and its private data */ 1648 kfree(sroute->private); 1649 list_del(&sroute->list); 1650 kfree(sroute); 1651 1652 return 0; 1653 } 1654 1655 static int sof_widget_unload(struct snd_soc_component *scomp, 1656 struct snd_soc_dobj *dobj) 1657 { 1658 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1659 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1660 const struct sof_ipc_tplg_widget_ops *widget_ops; 1661 const struct snd_kcontrol_new *kc; 1662 struct snd_soc_dapm_widget *widget; 1663 struct snd_sof_control *scontrol; 1664 struct snd_sof_widget *swidget; 1665 struct soc_mixer_control *sm; 1666 struct soc_bytes_ext *sbe; 1667 struct snd_sof_dai *dai; 1668 struct soc_enum *se; 1669 int i; 1670 1671 swidget = dobj->private; 1672 if (!swidget) 1673 return 0; 1674 1675 widget = swidget->widget; 1676 1677 switch (swidget->id) { 1678 case snd_soc_dapm_dai_in: 1679 case snd_soc_dapm_dai_out: 1680 dai = swidget->private; 1681 1682 if (dai) 1683 list_del(&dai->list); 1684 1685 sof_disconnect_dai_widget(scomp, widget); 1686 1687 break; 1688 case snd_soc_dapm_scheduler: 1689 { 1690 struct snd_sof_pipeline *spipe = swidget->spipe; 1691 1692 list_del(&spipe->list); 1693 kfree(spipe); 1694 swidget->spipe = NULL; 1695 break; 1696 } 1697 default: 1698 break; 1699 } 1700 for (i = 0; i < widget->num_kcontrols; i++) { 1701 kc = &widget->kcontrol_news[i]; 1702 switch (widget->dobj.widget.kcontrol_type[i]) { 1703 case SND_SOC_TPLG_TYPE_MIXER: 1704 sm = (struct soc_mixer_control *)kc->private_value; 1705 scontrol = sm->dobj.private; 1706 if (sm->max > 1) 1707 kfree(scontrol->volume_table); 1708 break; 1709 case SND_SOC_TPLG_TYPE_ENUM: 1710 se = (struct soc_enum *)kc->private_value; 1711 scontrol = se->dobj.private; 1712 break; 1713 case SND_SOC_TPLG_TYPE_BYTES: 1714 sbe = (struct soc_bytes_ext *)kc->private_value; 1715 scontrol = sbe->dobj.private; 1716 break; 1717 default: 1718 dev_warn(scomp->dev, "unsupported kcontrol_type\n"); 1719 goto out; 1720 } 1721 kfree(scontrol->ipc_control_data); 1722 list_del(&scontrol->list); 1723 kfree(scontrol->name); 1724 kfree(scontrol); 1725 } 1726 1727 out: 1728 /* free IPC related data */ 1729 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 1730 if (widget_ops && widget_ops[swidget->id].ipc_free) 1731 widget_ops[swidget->id].ipc_free(swidget); 1732 1733 ida_destroy(&swidget->output_queue_ida); 1734 ida_destroy(&swidget->input_queue_ida); 1735 1736 sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT); 1737 sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT); 1738 1739 kfree(swidget->tuples); 1740 1741 /* remove and free swidget object */ 1742 list_del(&swidget->list); 1743 kfree(swidget); 1744 1745 return 0; 1746 } 1747 1748 /* 1749 * DAI HW configuration. 1750 */ 1751 1752 /* FE DAI - used for any driver specific init */ 1753 static int sof_dai_load(struct snd_soc_component *scomp, int index, 1754 struct snd_soc_dai_driver *dai_drv, 1755 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 1756 { 1757 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1758 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); 1759 struct snd_soc_tplg_stream_caps *caps; 1760 struct snd_soc_tplg_private *private = &pcm->priv; 1761 struct snd_sof_pcm *spcm; 1762 int stream; 1763 int ret; 1764 1765 /* nothing to do for BEs atm */ 1766 if (!pcm) 1767 return 0; 1768 1769 spcm = kzalloc_obj(*spcm); 1770 if (!spcm) 1771 return -ENOMEM; 1772 1773 spcm->scomp = scomp; 1774 1775 for_each_pcm_streams(stream) { 1776 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED; 1777 if (pcm->compress) 1778 snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work); 1779 else 1780 snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work); 1781 } 1782 1783 spcm->pcm = *pcm; 1784 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name); 1785 1786 /* perform pcm set op */ 1787 if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) { 1788 ret = ipc_pcm_ops->pcm_setup(sdev, spcm); 1789 if (ret < 0) { 1790 kfree(spcm); 1791 return ret; 1792 } 1793 } 1794 1795 dai_drv->dobj.private = spcm; 1796 list_add(&spcm->list, &sdev->pcm_list); 1797 1798 ret = sof_parse_tokens(scomp, spcm, stream_tokens, 1799 ARRAY_SIZE(stream_tokens), private->array, 1800 le32_to_cpu(private->size)); 1801 if (ret) { 1802 dev_err(scomp->dev, "error: parse stream tokens failed %u\n", 1803 le32_to_cpu(private->size)); 1804 return ret; 1805 } 1806 1807 /* do we need to allocate playback PCM DMA pages */ 1808 if (!spcm->pcm.playback) 1809 goto capture; 1810 1811 stream = SNDRV_PCM_STREAM_PLAYBACK; 1812 1813 caps = &spcm->pcm.caps[stream]; 1814 1815 /* allocate playback page table buffer */ 1816 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 1817 PAGE_SIZE, &spcm->stream[stream].page_table); 1818 if (ret < 0) { 1819 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n", 1820 caps->name, ret); 1821 1822 return ret; 1823 } 1824 1825 /* bind pcm to host comp */ 1826 ret = spcm_bind(scomp, spcm, stream); 1827 if (ret) { 1828 dev_err(scomp->dev, 1829 "error: can't bind pcm to host\n"); 1830 goto free_playback_tables; 1831 } 1832 1833 capture: 1834 stream = SNDRV_PCM_STREAM_CAPTURE; 1835 1836 /* do we need to allocate capture PCM DMA pages */ 1837 if (!spcm->pcm.capture) 1838 return ret; 1839 1840 caps = &spcm->pcm.caps[stream]; 1841 1842 /* allocate capture page table buffer */ 1843 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 1844 PAGE_SIZE, &spcm->stream[stream].page_table); 1845 if (ret < 0) { 1846 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n", 1847 caps->name, ret); 1848 goto free_playback_tables; 1849 } 1850 1851 /* bind pcm to host comp */ 1852 ret = spcm_bind(scomp, spcm, stream); 1853 if (ret) { 1854 dev_err(scomp->dev, 1855 "error: can't bind pcm to host\n"); 1856 snd_dma_free_pages(&spcm->stream[stream].page_table); 1857 goto free_playback_tables; 1858 } 1859 1860 return ret; 1861 1862 free_playback_tables: 1863 if (spcm->pcm.playback) 1864 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 1865 1866 return ret; 1867 } 1868 1869 static int sof_dai_unload(struct snd_soc_component *scomp, 1870 struct snd_soc_dobj *dobj) 1871 { 1872 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1873 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); 1874 struct snd_sof_pcm *spcm = dobj->private; 1875 1876 /* free PCM DMA pages */ 1877 if (spcm->pcm.playback) 1878 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 1879 1880 if (spcm->pcm.capture) 1881 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table); 1882 1883 /* perform pcm free op */ 1884 if (ipc_pcm_ops && ipc_pcm_ops->pcm_free) 1885 ipc_pcm_ops->pcm_free(sdev, spcm); 1886 1887 /* remove from list and free spcm */ 1888 list_del(&spcm->list); 1889 kfree(spcm); 1890 1891 return 0; 1892 } 1893 1894 static const struct sof_topology_token common_dai_link_tokens[] = { 1895 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 1896 offsetof(struct snd_sof_dai_link, type)}, 1897 }; 1898 1899 /* DAI link - used for any driver specific init */ 1900 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link, 1901 struct snd_soc_tplg_link_config *cfg) 1902 { 1903 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1904 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1905 struct snd_soc_tplg_private *private = &cfg->priv; 1906 const struct sof_token_info *token_list; 1907 struct snd_sof_dai_link *slink; 1908 u32 token_id = 0; 1909 int num_tuples = 0; 1910 int ret, num_sets; 1911 1912 if (!link->platforms) { 1913 dev_err(scomp->dev, "error: no platforms\n"); 1914 return -EINVAL; 1915 } 1916 link->platforms->name = dev_name(scomp->dev); 1917 1918 if (tplg_ops && tplg_ops->link_setup) { 1919 ret = tplg_ops->link_setup(sdev, link); 1920 if (ret < 0) 1921 return ret; 1922 } 1923 1924 /* Set nonatomic property for FE dai links as their trigger action involves IPC's */ 1925 if (!link->no_pcm) { 1926 link->nonatomic = true; 1927 return 0; 1928 } 1929 1930 /* check we have some tokens - we need at least DAI type */ 1931 if (le32_to_cpu(private->size) == 0) { 1932 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n"); 1933 return -EINVAL; 1934 } 1935 1936 slink = kzalloc_flex(*slink, hw_configs, le32_to_cpu(cfg->num_hw_configs)); 1937 if (!slink) 1938 return -ENOMEM; 1939 1940 slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs); 1941 memcpy(slink->hw_configs, cfg->hw_config, le32_to_cpu(cfg->num_hw_configs) * sizeof(*slink->hw_configs)); 1942 1943 slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id); 1944 slink->link = link; 1945 1946 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n", 1947 slink->num_hw_configs, slink->default_hw_cfg_id, link->name); 1948 1949 ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens, 1950 ARRAY_SIZE(common_dai_link_tokens), 1951 private->array, le32_to_cpu(private->size)); 1952 if (ret < 0) { 1953 dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n"); 1954 goto free_slink; 1955 } 1956 1957 token_list = tplg_ops ? tplg_ops->token_list : NULL; 1958 if (!token_list) 1959 goto out; 1960 1961 /* calculate size of tuples array */ 1962 num_tuples += token_list[SOF_DAI_LINK_TOKENS].count; 1963 num_sets = slink->num_hw_configs; 1964 switch (slink->type) { 1965 case SOF_DAI_INTEL_SSP: 1966 token_id = SOF_SSP_TOKENS; 1967 num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs; 1968 break; 1969 case SOF_DAI_INTEL_DMIC: 1970 token_id = SOF_DMIC_TOKENS; 1971 num_tuples += token_list[SOF_DMIC_TOKENS].count; 1972 1973 /* Allocate memory for max PDM controllers */ 1974 num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL; 1975 break; 1976 case SOF_DAI_INTEL_HDA: 1977 token_id = SOF_HDA_TOKENS; 1978 num_tuples += token_list[SOF_HDA_TOKENS].count; 1979 break; 1980 case SOF_DAI_INTEL_ALH: 1981 token_id = SOF_ALH_TOKENS; 1982 num_tuples += token_list[SOF_ALH_TOKENS].count; 1983 break; 1984 case SOF_DAI_IMX_SAI: 1985 token_id = SOF_SAI_TOKENS; 1986 num_tuples += token_list[SOF_SAI_TOKENS].count; 1987 break; 1988 case SOF_DAI_IMX_ESAI: 1989 token_id = SOF_ESAI_TOKENS; 1990 num_tuples += token_list[SOF_ESAI_TOKENS].count; 1991 break; 1992 case SOF_DAI_MEDIATEK_AFE: 1993 token_id = SOF_AFE_TOKENS; 1994 num_tuples += token_list[SOF_AFE_TOKENS].count; 1995 break; 1996 case SOF_DAI_AMD_DMIC: 1997 token_id = SOF_ACPDMIC_TOKENS; 1998 num_tuples += token_list[SOF_ACPDMIC_TOKENS].count; 1999 break; 2000 case SOF_DAI_AMD_BT: 2001 case SOF_DAI_AMD_SP: 2002 case SOF_DAI_AMD_HS: 2003 case SOF_DAI_AMD_SP_VIRTUAL: 2004 case SOF_DAI_AMD_HS_VIRTUAL: 2005 case SOF_DAI_AMD_I2S: 2006 token_id = SOF_ACPI2S_TOKENS; 2007 num_tuples += token_list[SOF_ACPI2S_TOKENS].count; 2008 break; 2009 case SOF_DAI_IMX_MICFIL: 2010 token_id = SOF_MICFIL_TOKENS; 2011 num_tuples += token_list[SOF_MICFIL_TOKENS].count; 2012 break; 2013 case SOF_DAI_AMD_SDW: 2014 token_id = SOF_ACP_SDW_TOKENS; 2015 num_tuples += token_list[SOF_ACP_SDW_TOKENS].count; 2016 break; 2017 default: 2018 break; 2019 } 2020 2021 /* allocate memory for tuples array */ 2022 slink->tuples = kzalloc_objs(*slink->tuples, num_tuples); 2023 if (!slink->tuples) { 2024 ret = -ENOMEM; 2025 goto free_slink; 2026 } 2027 2028 if (token_list[SOF_DAI_LINK_TOKENS].tokens) { 2029 /* parse one set of DAI link tokens */ 2030 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2031 SOF_DAI_LINK_TOKENS, 1, slink->tuples, 2032 num_tuples, &slink->num_tuples); 2033 if (ret < 0) { 2034 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2035 token_list[SOF_DAI_LINK_TOKENS].name, link->name); 2036 goto err; 2037 } 2038 } 2039 2040 /* nothing more to do if there are no DAI type-specific tokens defined */ 2041 if (!token_id || !token_list[token_id].tokens) 2042 goto out; 2043 2044 /* parse "num_sets" sets of DAI-specific tokens */ 2045 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2046 token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples); 2047 if (ret < 0) { 2048 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2049 token_list[token_id].name, link->name); 2050 goto err; 2051 } 2052 2053 /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */ 2054 if (token_id == SOF_DMIC_TOKENS) { 2055 num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, 2056 slink->tuples, slink->num_tuples); 2057 2058 if (num_sets < 0) { 2059 dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name); 2060 ret = num_sets; 2061 goto err; 2062 } 2063 2064 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2065 SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples, 2066 num_tuples, &slink->num_tuples); 2067 if (ret < 0) { 2068 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2069 token_list[SOF_DMIC_PDM_TOKENS].name, link->name); 2070 goto err; 2071 } 2072 } 2073 out: 2074 link->dobj.private = slink; 2075 list_add(&slink->list, &sdev->dai_link_list); 2076 2077 return 0; 2078 2079 err: 2080 kfree(slink->tuples); 2081 free_slink: 2082 kfree(slink); 2083 2084 return ret; 2085 } 2086 2087 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj) 2088 { 2089 struct snd_sof_dai_link *slink = dobj->private; 2090 2091 if (!slink) 2092 return 0; 2093 2094 slink->link->platforms->name = NULL; 2095 2096 kfree(slink->tuples); 2097 list_del(&slink->list); 2098 kfree(slink); 2099 dobj->private = NULL; 2100 2101 return 0; 2102 } 2103 2104 /* DAI link - used for any driver specific init */ 2105 static int sof_route_load(struct snd_soc_component *scomp, int index, 2106 struct snd_soc_dapm_route *route) 2107 { 2108 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2109 struct snd_sof_widget *source_swidget, *sink_swidget; 2110 struct snd_soc_dobj *dobj = &route->dobj; 2111 struct snd_sof_route *sroute; 2112 int ret = 0; 2113 2114 /* allocate memory for sroute and connect */ 2115 sroute = kzalloc_obj(*sroute); 2116 if (!sroute) 2117 return -ENOMEM; 2118 2119 sroute->scomp = scomp; 2120 dev_dbg(scomp->dev, "sink %s control %s source %s\n", 2121 route->sink, route->control ? route->control : "none", 2122 route->source); 2123 2124 /* source component */ 2125 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source); 2126 if (!source_swidget) { 2127 dev_err(scomp->dev, "source %s for sink %s is not found\n", 2128 route->source, route->sink); 2129 ret = -EINVAL; 2130 goto err; 2131 } 2132 2133 /* 2134 * Virtual widgets of type output/out_drv may be added in topology 2135 * for compatibility. These are not handled by the FW. 2136 * So, don't send routes whose source/sink widget is of such types 2137 * to the DSP. 2138 */ 2139 if (source_swidget->id == snd_soc_dapm_out_drv || 2140 source_swidget->id == snd_soc_dapm_output) 2141 goto err; 2142 2143 /* sink component */ 2144 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink); 2145 if (!sink_swidget) { 2146 dev_err(scomp->dev, "sink %s for source %s is not found\n", 2147 route->sink, route->source); 2148 ret = -EINVAL; 2149 goto err; 2150 } 2151 2152 /* 2153 * Don't send routes whose sink widget is of type 2154 * output or out_drv to the DSP 2155 */ 2156 if (sink_swidget->id == snd_soc_dapm_out_drv || 2157 sink_swidget->id == snd_soc_dapm_output) 2158 goto err; 2159 2160 sroute->route = route; 2161 dobj->private = sroute; 2162 sroute->src_widget = source_swidget; 2163 sroute->sink_widget = sink_swidget; 2164 2165 /* add route to route list */ 2166 list_add(&sroute->list, &sdev->route_list); 2167 2168 return 0; 2169 err: 2170 kfree(sroute); 2171 return ret; 2172 } 2173 2174 /** 2175 * sof_set_widget_pipeline - Set pipeline for a component 2176 * @sdev: pointer to struct snd_sof_dev 2177 * @spipe: pointer to struct snd_sof_pipeline 2178 * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget 2179 * 2180 * Return: 0 if successful, -EINVAL on error. 2181 * The function checks if @swidget is associated with any volatile controls. If so, setting 2182 * the dynamic_pipeline_widget is disallowed. 2183 */ 2184 static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe, 2185 struct snd_sof_widget *swidget) 2186 { 2187 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 2188 struct snd_sof_control *scontrol; 2189 2190 if (pipe_widget->dynamic_pipeline_widget) { 2191 /* dynamic widgets cannot have volatile kcontrols */ 2192 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) 2193 if (scontrol->comp_id == swidget->comp_id && 2194 (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) { 2195 dev_err(sdev->dev, 2196 "error: volatile control found for dynamic widget %s\n", 2197 swidget->widget->name); 2198 return -EINVAL; 2199 } 2200 } 2201 2202 /* set the pipeline and apply the dynamic_pipeline_widget_flag */ 2203 swidget->spipe = spipe; 2204 swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget; 2205 2206 return 0; 2207 } 2208 2209 /* completion - called at completion of firmware loading */ 2210 static int sof_complete(struct snd_soc_component *scomp) 2211 { 2212 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2213 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 2214 const struct sof_ipc_tplg_widget_ops *widget_ops; 2215 struct snd_sof_control *scontrol; 2216 struct snd_sof_pipeline *spipe; 2217 int ret; 2218 2219 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 2220 2221 /* first update all control IPC structures based on the IPC version */ 2222 if (tplg_ops && tplg_ops->control_setup) 2223 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 2224 ret = tplg_ops->control_setup(sdev, scontrol); 2225 if (ret < 0) { 2226 dev_err(sdev->dev, "failed updating IPC struct for control %s\n", 2227 scontrol->name); 2228 return ret; 2229 } 2230 } 2231 2232 /* set up the IPC structures for the pipeline widgets */ 2233 list_for_each_entry(spipe, &sdev->pipeline_list, list) { 2234 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 2235 struct snd_sof_widget *swidget; 2236 2237 pipe_widget->instance_id = -EINVAL; 2238 2239 /* Update the scheduler widget's IPC structure */ 2240 if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) { 2241 ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget); 2242 if (ret < 0) { 2243 dev_err(sdev->dev, "failed updating IPC struct for %s\n", 2244 pipe_widget->widget->name); 2245 return ret; 2246 } 2247 } 2248 2249 /* set the pipeline and update the IPC structure for the non scheduler widgets */ 2250 list_for_each_entry(swidget, &sdev->widget_list, list) 2251 if (swidget->widget->id != snd_soc_dapm_scheduler && 2252 swidget->pipeline_id == pipe_widget->pipeline_id) { 2253 ret = sof_set_widget_pipeline(sdev, spipe, swidget); 2254 if (ret < 0) 2255 return ret; 2256 2257 if (widget_ops && widget_ops[swidget->id].ipc_setup) { 2258 ret = widget_ops[swidget->id].ipc_setup(swidget); 2259 if (ret < 0) { 2260 dev_err(sdev->dev, 2261 "failed updating IPC struct for %s\n", 2262 swidget->widget->name); 2263 return ret; 2264 } 2265 } 2266 } 2267 } 2268 2269 /* verify topology components loading including dynamic pipelines */ 2270 if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) { 2271 if (tplg_ops && tplg_ops->set_up_all_pipelines && 2272 tplg_ops->tear_down_all_pipelines) { 2273 ret = tplg_ops->set_up_all_pipelines(sdev, true); 2274 if (ret < 0) { 2275 dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n", 2276 ret); 2277 return ret; 2278 } 2279 2280 ret = tplg_ops->tear_down_all_pipelines(sdev, true); 2281 if (ret < 0) { 2282 dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n", 2283 ret); 2284 return ret; 2285 } 2286 } 2287 } 2288 2289 /* set up static pipelines */ 2290 if (tplg_ops && tplg_ops->set_up_all_pipelines) 2291 return tplg_ops->set_up_all_pipelines(sdev, false); 2292 2293 return 0; 2294 } 2295 2296 /* manifest - optional to inform component of manifest */ 2297 static int sof_manifest(struct snd_soc_component *scomp, int index, 2298 struct snd_soc_tplg_manifest *man) 2299 { 2300 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2301 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 2302 2303 if (tplg_ops && tplg_ops->parse_manifest) 2304 return tplg_ops->parse_manifest(scomp, index, man); 2305 2306 return 0; 2307 } 2308 2309 /* vendor specific kcontrol handlers available for binding */ 2310 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = { 2311 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put}, 2312 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put}, 2313 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put}, 2314 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put}, 2315 }; 2316 2317 /* vendor specific bytes ext handlers available for binding */ 2318 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = { 2319 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put}, 2320 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get}, 2321 }; 2322 2323 static const struct snd_soc_tplg_ops sof_tplg_ops = { 2324 /* external kcontrol init - used for any driver specific init */ 2325 .control_load = sof_control_load, 2326 .control_unload = sof_control_unload, 2327 2328 /* external kcontrol init - used for any driver specific init */ 2329 .dapm_route_load = sof_route_load, 2330 .dapm_route_unload = sof_route_unload, 2331 2332 /* external widget init - used for any driver specific init */ 2333 /* .widget_load is not currently used */ 2334 .widget_ready = sof_widget_ready, 2335 .widget_unload = sof_widget_unload, 2336 2337 /* FE DAI - used for any driver specific init */ 2338 .dai_load = sof_dai_load, 2339 .dai_unload = sof_dai_unload, 2340 2341 /* DAI link - used for any driver specific init */ 2342 .link_load = sof_link_load, 2343 .link_unload = sof_link_unload, 2344 2345 /* 2346 * No need to set the complete callback. sof_complete will be called explicitly after 2347 * topology loading is complete. 2348 */ 2349 2350 /* manifest - optional to inform component of manifest */ 2351 .manifest = sof_manifest, 2352 2353 /* vendor specific kcontrol handlers available for binding */ 2354 .io_ops = sof_io_ops, 2355 .io_ops_count = ARRAY_SIZE(sof_io_ops), 2356 2357 /* vendor specific bytes ext handlers available for binding */ 2358 .bytes_ext_ops = sof_bytes_ext_ops, 2359 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops), 2360 }; 2361 2362 static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol, 2363 struct snd_ctl_elem_value *ucontrol) 2364 { 2365 return 0; 2366 } 2367 2368 static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = { 2369 {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2370 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2371 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2372 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2373 }; 2374 2375 static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol, 2376 unsigned int __user *binary_data, 2377 unsigned int size) 2378 { 2379 return 0; 2380 } 2381 2382 static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol, 2383 const unsigned int __user *binary_data, 2384 unsigned int size) 2385 { 2386 return 0; 2387 } 2388 2389 static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = { 2390 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put}, 2391 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get}, 2392 }; 2393 2394 /* external widget init - used for any driver specific init */ 2395 static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index, 2396 struct snd_soc_dapm_widget *w, 2397 struct snd_soc_tplg_dapm_widget *tw) 2398 { 2399 struct snd_soc_tplg_private *priv = &tw->priv; 2400 int ret; 2401 2402 /* for snd_soc_dapm_widget.no_wname_in_kcontrol_name */ 2403 ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, 2404 ARRAY_SIZE(dapm_widget_tokens), 2405 priv->array, le32_to_cpu(priv->size)); 2406 if (ret < 0) { 2407 dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n", 2408 w->name); 2409 return ret; 2410 } 2411 2412 if (WIDGET_IS_DAI(w->id)) { 2413 static const struct sof_topology_token dai_tokens[] = { 2414 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 0}}; 2415 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2416 struct snd_sof_widget *swidget; 2417 struct snd_sof_dai *sdai; 2418 2419 swidget = kzalloc_obj(*swidget); 2420 if (!swidget) 2421 return -ENOMEM; 2422 2423 sdai = kzalloc_obj(*sdai); 2424 if (!sdai) { 2425 kfree(swidget); 2426 return -ENOMEM; 2427 } 2428 2429 ret = sof_parse_tokens(scomp, &sdai->type, dai_tokens, ARRAY_SIZE(dai_tokens), 2430 priv->array, le32_to_cpu(priv->size)); 2431 if (ret < 0) { 2432 dev_err(scomp->dev, "Failed to parse DAI tokens for %s\n", tw->name); 2433 kfree(swidget); 2434 kfree(sdai); 2435 return ret; 2436 } 2437 2438 ret = sof_connect_dai_widget(scomp, w, tw, sdai); 2439 if (ret) { 2440 kfree(swidget); 2441 kfree(sdai); 2442 return ret; 2443 } 2444 2445 swidget->scomp = scomp; 2446 swidget->widget = w; 2447 swidget->private = sdai; 2448 mutex_init(&swidget->setup_mutex); 2449 w->dobj.private = swidget; 2450 list_add(&swidget->list, &sdev->widget_list); 2451 } 2452 2453 return 0; 2454 } 2455 2456 static int sof_dspless_widget_unload(struct snd_soc_component *scomp, 2457 struct snd_soc_dobj *dobj) 2458 { 2459 struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj); 2460 2461 if (WIDGET_IS_DAI(w->id)) { 2462 struct snd_sof_widget *swidget = dobj->private; 2463 2464 sof_disconnect_dai_widget(scomp, w); 2465 2466 if (!swidget) 2467 return 0; 2468 2469 /* remove and free swidget object */ 2470 list_del(&swidget->list); 2471 kfree(swidget->private); 2472 kfree(swidget); 2473 } 2474 2475 return 0; 2476 } 2477 2478 static int sof_dspless_link_load(struct snd_soc_component *scomp, int index, 2479 struct snd_soc_dai_link *link, 2480 struct snd_soc_tplg_link_config *cfg) 2481 { 2482 link->platforms->name = dev_name(scomp->dev); 2483 2484 /* Set nonatomic property for FE dai links for FE-BE compatibility */ 2485 if (!link->no_pcm) 2486 link->nonatomic = true; 2487 2488 return 0; 2489 } 2490 2491 static const struct snd_soc_tplg_ops sof_dspless_tplg_ops = { 2492 /* external widget init - used for any driver specific init */ 2493 .widget_ready = sof_dspless_widget_ready, 2494 .widget_unload = sof_dspless_widget_unload, 2495 2496 /* FE DAI - used for any driver specific init */ 2497 .dai_load = sof_dai_load, 2498 .dai_unload = sof_dai_unload, 2499 2500 /* DAI link - used for any driver specific init */ 2501 .link_load = sof_dspless_link_load, 2502 2503 /* vendor specific kcontrol handlers available for binding */ 2504 .io_ops = sof_dspless_io_ops, 2505 .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops), 2506 2507 /* vendor specific bytes ext handlers available for binding */ 2508 .bytes_ext_ops = sof_dspless_bytes_ext_ops, 2509 .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops), 2510 }; 2511 2512 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file) 2513 { 2514 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2515 struct snd_sof_pdata *sof_pdata = sdev->pdata; 2516 const char *tplg_filename_prefix = sof_pdata->tplg_filename_prefix; 2517 int tplg_cnt = 0; 2518 int ret; 2519 int i; 2520 2521 const char **tplg_files __free(kfree) = 2522 kzalloc_objs(*tplg_files, scomp->card->num_links); 2523 if (!tplg_files) 2524 return -ENOMEM; 2525 2526 /* Try to use function topologies if possible */ 2527 if (!sof_pdata->disable_function_topology && !disable_function_topology && 2528 sof_pdata->machine && sof_pdata->machine->get_function_tplg_files) { 2529 /* 2530 * When the topology name contains 'dummy' word, it means that 2531 * there is no fallback option to monolithic topology in case 2532 * any of the function topologies might be missing. 2533 * In this case we should use best effort to form the card, 2534 * ignoring functionalities that we are missing a fragment for. 2535 * 2536 * Note: monolithic topologies also ignore these possibly 2537 * missing functions, so the functionality of the card would be 2538 * identical to the case if there would be a fallback monolithic 2539 * topology created for the configuration. 2540 */ 2541 bool no_fallback = strstr(file, "dummy"); 2542 2543 tplg_cnt = sof_pdata->machine->get_function_tplg_files(scomp->card, 2544 sof_pdata->machine, 2545 tplg_filename_prefix, 2546 &tplg_files, 2547 no_fallback); 2548 if (tplg_cnt < 0) 2549 return tplg_cnt; 2550 } 2551 2552 /* 2553 * The monolithic topology will be used if there is no get_function_tplg_files 2554 * callback or the callback returns 0. 2555 */ 2556 if (!tplg_cnt) { 2557 if (strstr(file, "dummy")) { 2558 dev_err(scomp->dev, 2559 "Function topology is required, please upgrade sof-firmware\n"); 2560 return -EINVAL; 2561 } 2562 tplg_files[0] = file; 2563 tplg_cnt = 1; 2564 dev_info(scomp->dev, "loading topology: %s\n", file); 2565 } else { 2566 dev_info(scomp->dev, "Using function topologies instead %s\n", file); 2567 } 2568 2569 for (i = 0; i < tplg_cnt; i++) { 2570 /* Only print the file names if the function topologies are used */ 2571 if (tplg_files[0] != file) 2572 dev_info(scomp->dev, "loading topology %d: %s\n", i, tplg_files[i]); 2573 2574 const struct firmware *fw __free(firmware) = NULL; 2575 ret = request_firmware(&fw, tplg_files[i], scomp->dev); 2576 if (ret < 0) { 2577 /* 2578 * snd_soc_tplg_component_remove(scomp) will be called 2579 * if snd_soc_tplg_component_load(scomp) failed and all 2580 * objects in the scomp will be removed. No need to call 2581 * snd_soc_tplg_component_remove(scomp) here. 2582 */ 2583 dev_err(scomp->dev, "tplg request firmware %s failed err: %d\n", 2584 tplg_files[i], ret); 2585 goto out; 2586 } 2587 2588 if (sdev->dspless_mode_selected) 2589 ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw); 2590 else 2591 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw); 2592 2593 if (ret < 0) { 2594 dev_err(scomp->dev, "tplg %s component load failed %d\n", 2595 tplg_files[i], ret); 2596 goto out; 2597 } 2598 } 2599 2600 /* Loading user defined topologies */ 2601 for (i = 0; i < feature_tplg_cnt; i++) { 2602 const char *feature_topology = devm_kasprintf(scomp->dev, GFP_KERNEL, "%s/%s", 2603 tplg_filename_prefix, 2604 feature_topologies[i]); 2605 2606 if (!feature_topology) { 2607 ret = -ENOMEM; 2608 goto out; 2609 } 2610 dev_info(scomp->dev, "loading feature topology %d: %s\n", i, feature_topology); 2611 2612 const struct firmware *fw __free(firmware) = NULL; 2613 ret = request_firmware(&fw, feature_topology, scomp->dev); 2614 if (ret < 0) { 2615 /* 2616 * snd_soc_tplg_component_remove(scomp) will be called 2617 * if snd_soc_tplg_component_load(scomp) failed and all 2618 * objects in the scomp will be removed. No need to call 2619 * snd_soc_tplg_component_remove(scomp) here. 2620 */ 2621 dev_warn(scomp->dev, "feature tplg request firmware %s failed err: %d\n", 2622 feature_topologies[i], ret); 2623 /* 2624 * We don't return error here because we can still have the basic 2625 * audio feature when the function topology load complete. No need 2626 * to convert the error code because we will get new 'ret' out of the 2627 * loop. 2628 */ 2629 continue; 2630 } 2631 2632 if (sdev->dspless_mode_selected) 2633 ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw); 2634 else 2635 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw); 2636 2637 if (ret < 0) { 2638 dev_err(scomp->dev, "feature tplg %s component load failed %d\n", 2639 feature_topologies[i], ret); 2640 /* 2641 * We need to return error here because it may lead to kernel NULL pointer 2642 * dereference if we continue the remaining tasks. 2643 */ 2644 goto out; 2645 } 2646 } 2647 2648 /* call sof_complete when topologies are loaded successfully */ 2649 ret = sof_complete(scomp); 2650 2651 out: 2652 if (ret >= 0 && sdev->led_present) 2653 ret = snd_ctl_led_request(); 2654 2655 return ret; 2656 } 2657 EXPORT_SYMBOL(snd_sof_load_topology); 2658