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_effect: 1534 /* check we have some tokens - we need at least process type */ 1535 if (le32_to_cpu(tw->priv.size) == 0) { 1536 dev_err(scomp->dev, "error: process tokens not found\n"); 1537 ret = -EINVAL; 1538 break; 1539 } 1540 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); 1541 break; 1542 case snd_soc_dapm_pga: 1543 if (!le32_to_cpu(tw->num_kcontrols)) { 1544 dev_err(scomp->dev, "invalid kcontrol count %u for volume\n", 1545 le32_to_cpu(tw->num_kcontrols)); 1546 ret = -EINVAL; 1547 break; 1548 } 1549 1550 fallthrough; 1551 case snd_soc_dapm_mixer: 1552 case snd_soc_dapm_buffer: 1553 case snd_soc_dapm_scheduler: 1554 case snd_soc_dapm_aif_out: 1555 case snd_soc_dapm_aif_in: 1556 case snd_soc_dapm_src: 1557 case snd_soc_dapm_asrc: 1558 case snd_soc_dapm_siggen: 1559 case snd_soc_dapm_mux: 1560 case snd_soc_dapm_demux: 1561 ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); 1562 break; 1563 case snd_soc_dapm_switch: 1564 case snd_soc_dapm_dai_link: 1565 case snd_soc_dapm_kcontrol: 1566 default: 1567 dev_dbg(scomp->dev, "widget type %d name %s not handled\n", swidget->id, tw->name); 1568 break; 1569 } 1570 1571 /* check token parsing reply */ 1572 if (ret < 0) { 1573 dev_err(scomp->dev, 1574 "failed to add widget type %d name : %s stream %s\n", 1575 swidget->id, tw->name, strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 1576 ? tw->sname : "none"); 1577 goto widget_free; 1578 } 1579 1580 if (sof_debug_check_flag(SOF_DBG_DISABLE_MULTICORE)) { 1581 swidget->core = SOF_DSP_PRIMARY_CORE; 1582 } else { 1583 int core = sof_get_token_value(SOF_TKN_COMP_CORE_ID, swidget->tuples, 1584 swidget->num_tuples); 1585 1586 if (core >= 0) { 1587 if (core > sdev->num_cores - 1) { 1588 dev_info(scomp->dev, 1589 "out of range core id for %s, moving it %d -> %d\n", 1590 swidget->widget->name, core, SOF_DSP_PRIMARY_CORE); 1591 core = SOF_DSP_PRIMARY_CORE; 1592 } 1593 swidget->core = core; 1594 } 1595 } 1596 1597 /* bind widget to external event */ 1598 if (tw->event_type) { 1599 if (widget_ops && widget_ops[w->id].bind_event) { 1600 ret = widget_ops[w->id].bind_event(scomp, swidget, 1601 le16_to_cpu(tw->event_type)); 1602 if (ret) { 1603 dev_err(scomp->dev, "widget event binding failed for %s\n", 1604 swidget->widget->name); 1605 goto free; 1606 } 1607 } 1608 } 1609 1610 /* create and add pipeline for scheduler type widgets */ 1611 if (w->id == snd_soc_dapm_scheduler) { 1612 struct snd_sof_pipeline *spipe; 1613 1614 spipe = kzalloc_obj(*spipe); 1615 if (!spipe) { 1616 ret = -ENOMEM; 1617 goto free; 1618 } 1619 1620 spipe->pipe_widget = swidget; 1621 swidget->spipe = spipe; 1622 list_add(&spipe->list, &sdev->pipeline_list); 1623 } 1624 1625 w->dobj.private = swidget; 1626 list_add(&swidget->list, &sdev->widget_list); 1627 return ret; 1628 free: 1629 kfree(swidget->private); 1630 kfree(swidget->tuples); 1631 widget_free: 1632 kfree(swidget); 1633 return ret; 1634 } 1635 1636 static int sof_route_unload(struct snd_soc_component *scomp, 1637 struct snd_soc_dobj *dobj) 1638 { 1639 struct snd_sof_route *sroute; 1640 1641 sroute = dobj->private; 1642 if (!sroute) 1643 return 0; 1644 1645 /* free sroute and its private data */ 1646 kfree(sroute->private); 1647 list_del(&sroute->list); 1648 kfree(sroute); 1649 1650 return 0; 1651 } 1652 1653 static int sof_widget_unload(struct snd_soc_component *scomp, 1654 struct snd_soc_dobj *dobj) 1655 { 1656 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1657 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1658 const struct sof_ipc_tplg_widget_ops *widget_ops; 1659 const struct snd_kcontrol_new *kc; 1660 struct snd_soc_dapm_widget *widget; 1661 struct snd_sof_control *scontrol; 1662 struct snd_sof_widget *swidget; 1663 struct soc_mixer_control *sm; 1664 struct soc_bytes_ext *sbe; 1665 struct snd_sof_dai *dai; 1666 struct soc_enum *se; 1667 int i; 1668 1669 swidget = dobj->private; 1670 if (!swidget) 1671 return 0; 1672 1673 widget = swidget->widget; 1674 1675 switch (swidget->id) { 1676 case snd_soc_dapm_dai_in: 1677 case snd_soc_dapm_dai_out: 1678 dai = swidget->private; 1679 1680 if (dai) 1681 list_del(&dai->list); 1682 1683 sof_disconnect_dai_widget(scomp, widget); 1684 1685 break; 1686 case snd_soc_dapm_scheduler: 1687 { 1688 struct snd_sof_pipeline *spipe = swidget->spipe; 1689 1690 list_del(&spipe->list); 1691 kfree(spipe); 1692 swidget->spipe = NULL; 1693 break; 1694 } 1695 default: 1696 break; 1697 } 1698 for (i = 0; i < widget->num_kcontrols; i++) { 1699 kc = &widget->kcontrol_news[i]; 1700 switch (widget->dobj.widget.kcontrol_type[i]) { 1701 case SND_SOC_TPLG_TYPE_MIXER: 1702 sm = (struct soc_mixer_control *)kc->private_value; 1703 scontrol = sm->dobj.private; 1704 if (sm->max > 1) 1705 kfree(scontrol->volume_table); 1706 break; 1707 case SND_SOC_TPLG_TYPE_ENUM: 1708 se = (struct soc_enum *)kc->private_value; 1709 scontrol = se->dobj.private; 1710 break; 1711 case SND_SOC_TPLG_TYPE_BYTES: 1712 sbe = (struct soc_bytes_ext *)kc->private_value; 1713 scontrol = sbe->dobj.private; 1714 break; 1715 default: 1716 dev_warn(scomp->dev, "unsupported kcontrol_type\n"); 1717 goto out; 1718 } 1719 kfree(scontrol->ipc_control_data); 1720 list_del(&scontrol->list); 1721 kfree(scontrol->name); 1722 kfree(scontrol); 1723 } 1724 1725 out: 1726 /* free IPC related data */ 1727 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 1728 if (widget_ops && widget_ops[swidget->id].ipc_free) 1729 widget_ops[swidget->id].ipc_free(swidget); 1730 1731 ida_destroy(&swidget->output_queue_ida); 1732 ida_destroy(&swidget->input_queue_ida); 1733 1734 sof_free_pin_binding(swidget, SOF_PIN_TYPE_INPUT); 1735 sof_free_pin_binding(swidget, SOF_PIN_TYPE_OUTPUT); 1736 1737 kfree(swidget->tuples); 1738 1739 /* remove and free swidget object */ 1740 list_del(&swidget->list); 1741 kfree(swidget); 1742 1743 return 0; 1744 } 1745 1746 /* 1747 * DAI HW configuration. 1748 */ 1749 1750 /* FE DAI - used for any driver specific init */ 1751 static int sof_dai_load(struct snd_soc_component *scomp, int index, 1752 struct snd_soc_dai_driver *dai_drv, 1753 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 1754 { 1755 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1756 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); 1757 struct snd_soc_tplg_stream_caps *caps; 1758 struct snd_soc_tplg_private *private = &pcm->priv; 1759 struct snd_sof_pcm *spcm; 1760 int stream; 1761 int ret; 1762 1763 /* nothing to do for BEs atm */ 1764 if (!pcm) 1765 return 0; 1766 1767 spcm = kzalloc_obj(*spcm); 1768 if (!spcm) 1769 return -ENOMEM; 1770 1771 spcm->scomp = scomp; 1772 1773 for_each_pcm_streams(stream) { 1774 spcm->stream[stream].comp_id = COMP_ID_UNASSIGNED; 1775 if (pcm->compress) 1776 snd_sof_compr_init_elapsed_work(&spcm->stream[stream].period_elapsed_work); 1777 else 1778 snd_sof_pcm_init_elapsed_work(&spcm->stream[stream].period_elapsed_work); 1779 } 1780 1781 spcm->pcm = *pcm; 1782 dev_dbg(scomp->dev, "tplg: load pcm %s\n", pcm->dai_name); 1783 1784 /* perform pcm set op */ 1785 if (ipc_pcm_ops && ipc_pcm_ops->pcm_setup) { 1786 ret = ipc_pcm_ops->pcm_setup(sdev, spcm); 1787 if (ret < 0) { 1788 kfree(spcm); 1789 return ret; 1790 } 1791 } 1792 1793 dai_drv->dobj.private = spcm; 1794 list_add(&spcm->list, &sdev->pcm_list); 1795 1796 ret = sof_parse_tokens(scomp, spcm, stream_tokens, 1797 ARRAY_SIZE(stream_tokens), private->array, 1798 le32_to_cpu(private->size)); 1799 if (ret) { 1800 dev_err(scomp->dev, "error: parse stream tokens failed %u\n", 1801 le32_to_cpu(private->size)); 1802 return ret; 1803 } 1804 1805 /* do we need to allocate playback PCM DMA pages */ 1806 if (!spcm->pcm.playback) 1807 goto capture; 1808 1809 stream = SNDRV_PCM_STREAM_PLAYBACK; 1810 1811 caps = &spcm->pcm.caps[stream]; 1812 1813 /* allocate playback page table buffer */ 1814 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 1815 PAGE_SIZE, &spcm->stream[stream].page_table); 1816 if (ret < 0) { 1817 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n", 1818 caps->name, ret); 1819 1820 return ret; 1821 } 1822 1823 /* bind pcm to host comp */ 1824 ret = spcm_bind(scomp, spcm, stream); 1825 if (ret) { 1826 dev_err(scomp->dev, 1827 "error: can't bind pcm to host\n"); 1828 goto free_playback_tables; 1829 } 1830 1831 capture: 1832 stream = SNDRV_PCM_STREAM_CAPTURE; 1833 1834 /* do we need to allocate capture PCM DMA pages */ 1835 if (!spcm->pcm.capture) 1836 return ret; 1837 1838 caps = &spcm->pcm.caps[stream]; 1839 1840 /* allocate capture page table buffer */ 1841 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 1842 PAGE_SIZE, &spcm->stream[stream].page_table); 1843 if (ret < 0) { 1844 dev_err(scomp->dev, "error: can't alloc page table for %s %d\n", 1845 caps->name, ret); 1846 goto free_playback_tables; 1847 } 1848 1849 /* bind pcm to host comp */ 1850 ret = spcm_bind(scomp, spcm, stream); 1851 if (ret) { 1852 dev_err(scomp->dev, 1853 "error: can't bind pcm to host\n"); 1854 snd_dma_free_pages(&spcm->stream[stream].page_table); 1855 goto free_playback_tables; 1856 } 1857 1858 return ret; 1859 1860 free_playback_tables: 1861 if (spcm->pcm.playback) 1862 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 1863 1864 return ret; 1865 } 1866 1867 static int sof_dai_unload(struct snd_soc_component *scomp, 1868 struct snd_soc_dobj *dobj) 1869 { 1870 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1871 const struct sof_ipc_pcm_ops *ipc_pcm_ops = sof_ipc_get_ops(sdev, pcm); 1872 struct snd_sof_pcm *spcm = dobj->private; 1873 1874 /* free PCM DMA pages */ 1875 if (spcm->pcm.playback) 1876 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 1877 1878 if (spcm->pcm.capture) 1879 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table); 1880 1881 /* perform pcm free op */ 1882 if (ipc_pcm_ops && ipc_pcm_ops->pcm_free) 1883 ipc_pcm_ops->pcm_free(sdev, spcm); 1884 1885 /* remove from list and free spcm */ 1886 list_del(&spcm->list); 1887 kfree(spcm); 1888 1889 return 0; 1890 } 1891 1892 static const struct sof_topology_token common_dai_link_tokens[] = { 1893 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 1894 offsetof(struct snd_sof_dai_link, type)}, 1895 }; 1896 1897 /* DAI link - used for any driver specific init */ 1898 static int sof_link_load(struct snd_soc_component *scomp, int index, struct snd_soc_dai_link *link, 1899 struct snd_soc_tplg_link_config *cfg) 1900 { 1901 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1902 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 1903 struct snd_soc_tplg_private *private = &cfg->priv; 1904 const struct sof_token_info *token_list; 1905 struct snd_sof_dai_link *slink; 1906 u32 token_id = 0; 1907 int num_tuples = 0; 1908 int ret, num_sets; 1909 1910 if (!link->platforms) { 1911 dev_err(scomp->dev, "error: no platforms\n"); 1912 return -EINVAL; 1913 } 1914 link->platforms->name = dev_name(scomp->dev); 1915 1916 if (tplg_ops && tplg_ops->link_setup) { 1917 ret = tplg_ops->link_setup(sdev, link); 1918 if (ret < 0) 1919 return ret; 1920 } 1921 1922 /* Set nonatomic property for FE dai links as their trigger action involves IPC's */ 1923 if (!link->no_pcm) { 1924 link->nonatomic = true; 1925 return 0; 1926 } 1927 1928 /* check we have some tokens - we need at least DAI type */ 1929 if (le32_to_cpu(private->size) == 0) { 1930 dev_err(scomp->dev, "error: expected tokens for DAI, none found\n"); 1931 return -EINVAL; 1932 } 1933 1934 slink = kzalloc_flex(*slink, hw_configs, le32_to_cpu(cfg->num_hw_configs)); 1935 if (!slink) 1936 return -ENOMEM; 1937 1938 slink->num_hw_configs = le32_to_cpu(cfg->num_hw_configs); 1939 memcpy(slink->hw_configs, cfg->hw_config, le32_to_cpu(cfg->num_hw_configs) * sizeof(*slink->hw_configs)); 1940 1941 slink->default_hw_cfg_id = le32_to_cpu(cfg->default_hw_config_id); 1942 slink->link = link; 1943 1944 dev_dbg(scomp->dev, "tplg: %d hw_configs found, default id: %d for dai link %s!\n", 1945 slink->num_hw_configs, slink->default_hw_cfg_id, link->name); 1946 1947 ret = sof_parse_tokens(scomp, slink, common_dai_link_tokens, 1948 ARRAY_SIZE(common_dai_link_tokens), 1949 private->array, le32_to_cpu(private->size)); 1950 if (ret < 0) { 1951 dev_err(scomp->dev, "Failed tp parse common DAI link tokens\n"); 1952 goto free_slink; 1953 } 1954 1955 token_list = tplg_ops ? tplg_ops->token_list : NULL; 1956 if (!token_list) 1957 goto out; 1958 1959 /* calculate size of tuples array */ 1960 num_tuples += token_list[SOF_DAI_LINK_TOKENS].count; 1961 num_sets = slink->num_hw_configs; 1962 switch (slink->type) { 1963 case SOF_DAI_INTEL_SSP: 1964 token_id = SOF_SSP_TOKENS; 1965 num_tuples += token_list[SOF_SSP_TOKENS].count * slink->num_hw_configs; 1966 break; 1967 case SOF_DAI_INTEL_DMIC: 1968 token_id = SOF_DMIC_TOKENS; 1969 num_tuples += token_list[SOF_DMIC_TOKENS].count; 1970 1971 /* Allocate memory for max PDM controllers */ 1972 num_tuples += token_list[SOF_DMIC_PDM_TOKENS].count * SOF_DAI_INTEL_DMIC_NUM_CTRL; 1973 break; 1974 case SOF_DAI_INTEL_HDA: 1975 token_id = SOF_HDA_TOKENS; 1976 num_tuples += token_list[SOF_HDA_TOKENS].count; 1977 break; 1978 case SOF_DAI_INTEL_ALH: 1979 token_id = SOF_ALH_TOKENS; 1980 num_tuples += token_list[SOF_ALH_TOKENS].count; 1981 break; 1982 case SOF_DAI_IMX_SAI: 1983 token_id = SOF_SAI_TOKENS; 1984 num_tuples += token_list[SOF_SAI_TOKENS].count; 1985 break; 1986 case SOF_DAI_IMX_ESAI: 1987 token_id = SOF_ESAI_TOKENS; 1988 num_tuples += token_list[SOF_ESAI_TOKENS].count; 1989 break; 1990 case SOF_DAI_MEDIATEK_AFE: 1991 token_id = SOF_AFE_TOKENS; 1992 num_tuples += token_list[SOF_AFE_TOKENS].count; 1993 break; 1994 case SOF_DAI_AMD_DMIC: 1995 token_id = SOF_ACPDMIC_TOKENS; 1996 num_tuples += token_list[SOF_ACPDMIC_TOKENS].count; 1997 break; 1998 case SOF_DAI_AMD_BT: 1999 case SOF_DAI_AMD_SP: 2000 case SOF_DAI_AMD_HS: 2001 case SOF_DAI_AMD_SP_VIRTUAL: 2002 case SOF_DAI_AMD_HS_VIRTUAL: 2003 case SOF_DAI_AMD_I2S: 2004 token_id = SOF_ACPI2S_TOKENS; 2005 num_tuples += token_list[SOF_ACPI2S_TOKENS].count; 2006 break; 2007 case SOF_DAI_IMX_MICFIL: 2008 token_id = SOF_MICFIL_TOKENS; 2009 num_tuples += token_list[SOF_MICFIL_TOKENS].count; 2010 break; 2011 case SOF_DAI_AMD_SDW: 2012 token_id = SOF_ACP_SDW_TOKENS; 2013 num_tuples += token_list[SOF_ACP_SDW_TOKENS].count; 2014 break; 2015 default: 2016 break; 2017 } 2018 2019 /* allocate memory for tuples array */ 2020 slink->tuples = kzalloc_objs(*slink->tuples, num_tuples); 2021 if (!slink->tuples) { 2022 ret = -ENOMEM; 2023 goto free_slink; 2024 } 2025 2026 if (token_list[SOF_DAI_LINK_TOKENS].tokens) { 2027 /* parse one set of DAI link tokens */ 2028 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2029 SOF_DAI_LINK_TOKENS, 1, slink->tuples, 2030 num_tuples, &slink->num_tuples); 2031 if (ret < 0) { 2032 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2033 token_list[SOF_DAI_LINK_TOKENS].name, link->name); 2034 goto err; 2035 } 2036 } 2037 2038 /* nothing more to do if there are no DAI type-specific tokens defined */ 2039 if (!token_id || !token_list[token_id].tokens) 2040 goto out; 2041 2042 /* parse "num_sets" sets of DAI-specific tokens */ 2043 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2044 token_id, num_sets, slink->tuples, num_tuples, &slink->num_tuples); 2045 if (ret < 0) { 2046 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2047 token_list[token_id].name, link->name); 2048 goto err; 2049 } 2050 2051 /* for DMIC, also parse all sets of DMIC PDM tokens based on active PDM count */ 2052 if (token_id == SOF_DMIC_TOKENS) { 2053 num_sets = sof_get_token_value(SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, 2054 slink->tuples, slink->num_tuples); 2055 2056 if (num_sets < 0) { 2057 dev_err(sdev->dev, "Invalid active PDM count for %s\n", link->name); 2058 ret = num_sets; 2059 goto err; 2060 } 2061 2062 ret = sof_copy_tuples(sdev, private->array, le32_to_cpu(private->size), 2063 SOF_DMIC_PDM_TOKENS, num_sets, slink->tuples, 2064 num_tuples, &slink->num_tuples); 2065 if (ret < 0) { 2066 dev_err(scomp->dev, "failed to parse %s for dai link %s\n", 2067 token_list[SOF_DMIC_PDM_TOKENS].name, link->name); 2068 goto err; 2069 } 2070 } 2071 out: 2072 link->dobj.private = slink; 2073 list_add(&slink->list, &sdev->dai_link_list); 2074 2075 return 0; 2076 2077 err: 2078 kfree(slink->tuples); 2079 free_slink: 2080 kfree(slink); 2081 2082 return ret; 2083 } 2084 2085 static int sof_link_unload(struct snd_soc_component *scomp, struct snd_soc_dobj *dobj) 2086 { 2087 struct snd_sof_dai_link *slink = dobj->private; 2088 2089 if (!slink) 2090 return 0; 2091 2092 slink->link->platforms->name = NULL; 2093 2094 kfree(slink->tuples); 2095 list_del(&slink->list); 2096 kfree(slink); 2097 dobj->private = NULL; 2098 2099 return 0; 2100 } 2101 2102 /* DAI link - used for any driver specific init */ 2103 static int sof_route_load(struct snd_soc_component *scomp, int index, 2104 struct snd_soc_dapm_route *route) 2105 { 2106 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2107 struct snd_sof_widget *source_swidget, *sink_swidget; 2108 struct snd_soc_dobj *dobj = &route->dobj; 2109 struct snd_sof_route *sroute; 2110 int ret = 0; 2111 2112 /* allocate memory for sroute and connect */ 2113 sroute = kzalloc_obj(*sroute); 2114 if (!sroute) 2115 return -ENOMEM; 2116 2117 sroute->scomp = scomp; 2118 dev_dbg(scomp->dev, "sink %s control %s source %s\n", 2119 route->sink, route->control ? route->control : "none", 2120 route->source); 2121 2122 /* source component */ 2123 source_swidget = snd_sof_find_swidget(scomp, (char *)route->source); 2124 if (!source_swidget) { 2125 dev_err(scomp->dev, "source %s for sink %s is not found\n", 2126 route->source, route->sink); 2127 ret = -EINVAL; 2128 goto err; 2129 } 2130 2131 /* 2132 * Virtual widgets of type output/out_drv may be added in topology 2133 * for compatibility. These are not handled by the FW. 2134 * So, don't send routes whose source/sink widget is of such types 2135 * to the DSP. 2136 */ 2137 if (source_swidget->id == snd_soc_dapm_out_drv || 2138 source_swidget->id == snd_soc_dapm_output) 2139 goto err; 2140 2141 /* sink component */ 2142 sink_swidget = snd_sof_find_swidget(scomp, (char *)route->sink); 2143 if (!sink_swidget) { 2144 dev_err(scomp->dev, "sink %s for source %s is not found\n", 2145 route->sink, route->source); 2146 ret = -EINVAL; 2147 goto err; 2148 } 2149 2150 /* 2151 * Don't send routes whose sink widget is of type 2152 * output or out_drv to the DSP 2153 */ 2154 if (sink_swidget->id == snd_soc_dapm_out_drv || 2155 sink_swidget->id == snd_soc_dapm_output) 2156 goto err; 2157 2158 sroute->route = route; 2159 dobj->private = sroute; 2160 sroute->src_widget = source_swidget; 2161 sroute->sink_widget = sink_swidget; 2162 2163 /* add route to route list */ 2164 list_add(&sroute->list, &sdev->route_list); 2165 2166 return 0; 2167 err: 2168 kfree(sroute); 2169 return ret; 2170 } 2171 2172 /** 2173 * sof_set_widget_pipeline - Set pipeline for a component 2174 * @sdev: pointer to struct snd_sof_dev 2175 * @spipe: pointer to struct snd_sof_pipeline 2176 * @swidget: pointer to struct snd_sof_widget that has the same pipeline ID as @pipe_widget 2177 * 2178 * Return: 0 if successful, -EINVAL on error. 2179 * The function checks if @swidget is associated with any volatile controls. If so, setting 2180 * the dynamic_pipeline_widget is disallowed. 2181 */ 2182 static int sof_set_widget_pipeline(struct snd_sof_dev *sdev, struct snd_sof_pipeline *spipe, 2183 struct snd_sof_widget *swidget) 2184 { 2185 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 2186 struct snd_sof_control *scontrol; 2187 2188 if (pipe_widget->dynamic_pipeline_widget) { 2189 /* dynamic widgets cannot have volatile kcontrols */ 2190 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) 2191 if (scontrol->comp_id == swidget->comp_id && 2192 (scontrol->access & SNDRV_CTL_ELEM_ACCESS_VOLATILE)) { 2193 dev_err(sdev->dev, 2194 "error: volatile control found for dynamic widget %s\n", 2195 swidget->widget->name); 2196 return -EINVAL; 2197 } 2198 } 2199 2200 /* set the pipeline and apply the dynamic_pipeline_widget_flag */ 2201 swidget->spipe = spipe; 2202 swidget->dynamic_pipeline_widget = pipe_widget->dynamic_pipeline_widget; 2203 2204 return 0; 2205 } 2206 2207 /* completion - called at completion of firmware loading */ 2208 static int sof_complete(struct snd_soc_component *scomp) 2209 { 2210 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2211 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 2212 const struct sof_ipc_tplg_widget_ops *widget_ops; 2213 struct snd_sof_control *scontrol; 2214 struct snd_sof_pipeline *spipe; 2215 int ret; 2216 2217 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 2218 2219 /* first update all control IPC structures based on the IPC version */ 2220 if (tplg_ops && tplg_ops->control_setup) 2221 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 2222 ret = tplg_ops->control_setup(sdev, scontrol); 2223 if (ret < 0) { 2224 dev_err(sdev->dev, "failed updating IPC struct for control %s\n", 2225 scontrol->name); 2226 return ret; 2227 } 2228 } 2229 2230 /* set up the IPC structures for the pipeline widgets */ 2231 list_for_each_entry(spipe, &sdev->pipeline_list, list) { 2232 struct snd_sof_widget *pipe_widget = spipe->pipe_widget; 2233 struct snd_sof_widget *swidget; 2234 2235 pipe_widget->instance_id = -EINVAL; 2236 2237 /* Update the scheduler widget's IPC structure */ 2238 if (widget_ops && widget_ops[pipe_widget->id].ipc_setup) { 2239 ret = widget_ops[pipe_widget->id].ipc_setup(pipe_widget); 2240 if (ret < 0) { 2241 dev_err(sdev->dev, "failed updating IPC struct for %s\n", 2242 pipe_widget->widget->name); 2243 return ret; 2244 } 2245 } 2246 2247 /* set the pipeline and update the IPC structure for the non scheduler widgets */ 2248 list_for_each_entry(swidget, &sdev->widget_list, list) 2249 if (swidget->widget->id != snd_soc_dapm_scheduler && 2250 swidget->pipeline_id == pipe_widget->pipeline_id) { 2251 ret = sof_set_widget_pipeline(sdev, spipe, swidget); 2252 if (ret < 0) 2253 return ret; 2254 2255 if (widget_ops && widget_ops[swidget->id].ipc_setup) { 2256 ret = widget_ops[swidget->id].ipc_setup(swidget); 2257 if (ret < 0) { 2258 dev_err(sdev->dev, 2259 "failed updating IPC struct for %s\n", 2260 swidget->widget->name); 2261 return ret; 2262 } 2263 } 2264 } 2265 } 2266 2267 /* verify topology components loading including dynamic pipelines */ 2268 if (sof_debug_check_flag(SOF_DBG_VERIFY_TPLG)) { 2269 if (tplg_ops && tplg_ops->set_up_all_pipelines && 2270 tplg_ops->tear_down_all_pipelines) { 2271 ret = tplg_ops->set_up_all_pipelines(sdev, true); 2272 if (ret < 0) { 2273 dev_err(sdev->dev, "Failed to set up all topology pipelines: %d\n", 2274 ret); 2275 return ret; 2276 } 2277 2278 ret = tplg_ops->tear_down_all_pipelines(sdev, true); 2279 if (ret < 0) { 2280 dev_err(sdev->dev, "Failed to tear down topology pipelines: %d\n", 2281 ret); 2282 return ret; 2283 } 2284 } 2285 } 2286 2287 /* set up static pipelines */ 2288 if (tplg_ops && tplg_ops->set_up_all_pipelines) 2289 return tplg_ops->set_up_all_pipelines(sdev, false); 2290 2291 return 0; 2292 } 2293 2294 /* manifest - optional to inform component of manifest */ 2295 static int sof_manifest(struct snd_soc_component *scomp, int index, 2296 struct snd_soc_tplg_manifest *man) 2297 { 2298 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2299 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 2300 2301 if (tplg_ops && tplg_ops->parse_manifest) 2302 return tplg_ops->parse_manifest(scomp, index, man); 2303 2304 return 0; 2305 } 2306 2307 /* vendor specific kcontrol handlers available for binding */ 2308 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = { 2309 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put}, 2310 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put}, 2311 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put}, 2312 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put}, 2313 }; 2314 2315 /* vendor specific bytes ext handlers available for binding */ 2316 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = { 2317 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put}, 2318 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_bytes_ext_volatile_get}, 2319 }; 2320 2321 static const struct snd_soc_tplg_ops sof_tplg_ops = { 2322 /* external kcontrol init - used for any driver specific init */ 2323 .control_load = sof_control_load, 2324 .control_unload = sof_control_unload, 2325 2326 /* external kcontrol init - used for any driver specific init */ 2327 .dapm_route_load = sof_route_load, 2328 .dapm_route_unload = sof_route_unload, 2329 2330 /* external widget init - used for any driver specific init */ 2331 /* .widget_load is not currently used */ 2332 .widget_ready = sof_widget_ready, 2333 .widget_unload = sof_widget_unload, 2334 2335 /* FE DAI - used for any driver specific init */ 2336 .dai_load = sof_dai_load, 2337 .dai_unload = sof_dai_unload, 2338 2339 /* DAI link - used for any driver specific init */ 2340 .link_load = sof_link_load, 2341 .link_unload = sof_link_unload, 2342 2343 /* 2344 * No need to set the complete callback. sof_complete will be called explicitly after 2345 * topology loading is complete. 2346 */ 2347 2348 /* manifest - optional to inform component of manifest */ 2349 .manifest = sof_manifest, 2350 2351 /* vendor specific kcontrol handlers available for binding */ 2352 .io_ops = sof_io_ops, 2353 .io_ops_count = ARRAY_SIZE(sof_io_ops), 2354 2355 /* vendor specific bytes ext handlers available for binding */ 2356 .bytes_ext_ops = sof_bytes_ext_ops, 2357 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops), 2358 }; 2359 2360 static int snd_sof_dspless_kcontrol(struct snd_kcontrol *kcontrol, 2361 struct snd_ctl_elem_value *ucontrol) 2362 { 2363 return 0; 2364 } 2365 2366 static const struct snd_soc_tplg_kcontrol_ops sof_dspless_io_ops[] = { 2367 {SOF_TPLG_KCTL_VOL_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2368 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2369 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2370 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_dspless_kcontrol, snd_sof_dspless_kcontrol}, 2371 }; 2372 2373 static int snd_sof_dspless_bytes_ext_get(struct snd_kcontrol *kcontrol, 2374 unsigned int __user *binary_data, 2375 unsigned int size) 2376 { 2377 return 0; 2378 } 2379 2380 static int snd_sof_dspless_bytes_ext_put(struct snd_kcontrol *kcontrol, 2381 const unsigned int __user *binary_data, 2382 unsigned int size) 2383 { 2384 return 0; 2385 } 2386 2387 static const struct snd_soc_tplg_bytes_ext_ops sof_dspless_bytes_ext_ops[] = { 2388 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_dspless_bytes_ext_get, snd_sof_dspless_bytes_ext_put}, 2389 {SOF_TPLG_KCTL_BYTES_VOLATILE_RO, snd_sof_dspless_bytes_ext_get}, 2390 }; 2391 2392 /* external widget init - used for any driver specific init */ 2393 static int sof_dspless_widget_ready(struct snd_soc_component *scomp, int index, 2394 struct snd_soc_dapm_widget *w, 2395 struct snd_soc_tplg_dapm_widget *tw) 2396 { 2397 struct snd_soc_tplg_private *priv = &tw->priv; 2398 int ret; 2399 2400 /* for snd_soc_dapm_widget.no_wname_in_kcontrol_name */ 2401 ret = sof_parse_tokens(scomp, w, dapm_widget_tokens, 2402 ARRAY_SIZE(dapm_widget_tokens), 2403 priv->array, le32_to_cpu(priv->size)); 2404 if (ret < 0) { 2405 dev_err(scomp->dev, "failed to parse dapm widget tokens for %s\n", 2406 w->name); 2407 return ret; 2408 } 2409 2410 if (WIDGET_IS_DAI(w->id)) { 2411 static const struct sof_topology_token dai_tokens[] = { 2412 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 0}}; 2413 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2414 struct snd_sof_widget *swidget; 2415 struct snd_sof_dai *sdai; 2416 2417 swidget = kzalloc_obj(*swidget); 2418 if (!swidget) 2419 return -ENOMEM; 2420 2421 sdai = kzalloc_obj(*sdai); 2422 if (!sdai) { 2423 kfree(swidget); 2424 return -ENOMEM; 2425 } 2426 2427 ret = sof_parse_tokens(scomp, &sdai->type, dai_tokens, ARRAY_SIZE(dai_tokens), 2428 priv->array, le32_to_cpu(priv->size)); 2429 if (ret < 0) { 2430 dev_err(scomp->dev, "Failed to parse DAI tokens for %s\n", tw->name); 2431 kfree(swidget); 2432 kfree(sdai); 2433 return ret; 2434 } 2435 2436 ret = sof_connect_dai_widget(scomp, w, tw, sdai); 2437 if (ret) { 2438 kfree(swidget); 2439 kfree(sdai); 2440 return ret; 2441 } 2442 2443 swidget->scomp = scomp; 2444 swidget->widget = w; 2445 swidget->private = sdai; 2446 mutex_init(&swidget->setup_mutex); 2447 w->dobj.private = swidget; 2448 list_add(&swidget->list, &sdev->widget_list); 2449 } 2450 2451 return 0; 2452 } 2453 2454 static int sof_dspless_widget_unload(struct snd_soc_component *scomp, 2455 struct snd_soc_dobj *dobj) 2456 { 2457 struct snd_soc_dapm_widget *w = container_of(dobj, struct snd_soc_dapm_widget, dobj); 2458 2459 if (WIDGET_IS_DAI(w->id)) { 2460 struct snd_sof_widget *swidget = dobj->private; 2461 2462 sof_disconnect_dai_widget(scomp, w); 2463 2464 if (!swidget) 2465 return 0; 2466 2467 /* remove and free swidget object */ 2468 list_del(&swidget->list); 2469 kfree(swidget->private); 2470 kfree(swidget); 2471 } 2472 2473 return 0; 2474 } 2475 2476 static int sof_dspless_link_load(struct snd_soc_component *scomp, int index, 2477 struct snd_soc_dai_link *link, 2478 struct snd_soc_tplg_link_config *cfg) 2479 { 2480 link->platforms->name = dev_name(scomp->dev); 2481 2482 /* Set nonatomic property for FE dai links for FE-BE compatibility */ 2483 if (!link->no_pcm) 2484 link->nonatomic = true; 2485 2486 return 0; 2487 } 2488 2489 static const struct snd_soc_tplg_ops sof_dspless_tplg_ops = { 2490 /* external widget init - used for any driver specific init */ 2491 .widget_ready = sof_dspless_widget_ready, 2492 .widget_unload = sof_dspless_widget_unload, 2493 2494 /* FE DAI - used for any driver specific init */ 2495 .dai_load = sof_dai_load, 2496 .dai_unload = sof_dai_unload, 2497 2498 /* DAI link - used for any driver specific init */ 2499 .link_load = sof_dspless_link_load, 2500 2501 /* vendor specific kcontrol handlers available for binding */ 2502 .io_ops = sof_dspless_io_ops, 2503 .io_ops_count = ARRAY_SIZE(sof_dspless_io_ops), 2504 2505 /* vendor specific bytes ext handlers available for binding */ 2506 .bytes_ext_ops = sof_dspless_bytes_ext_ops, 2507 .bytes_ext_ops_count = ARRAY_SIZE(sof_dspless_bytes_ext_ops), 2508 }; 2509 2510 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file) 2511 { 2512 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2513 struct snd_sof_pdata *sof_pdata = sdev->pdata; 2514 const char *tplg_filename_prefix = sof_pdata->tplg_filename_prefix; 2515 int tplg_cnt = 0; 2516 int ret; 2517 int i; 2518 2519 const char **tplg_files __free(kfree) = 2520 kcalloc(scomp->card->num_links, sizeof(char *), GFP_KERNEL); 2521 if (!tplg_files) 2522 return -ENOMEM; 2523 2524 /* Try to use function topologies if possible */ 2525 if (!sof_pdata->disable_function_topology && !disable_function_topology && 2526 sof_pdata->machine && sof_pdata->machine->get_function_tplg_files) { 2527 /* 2528 * When the topology name contains 'dummy' word, it means that 2529 * there is no fallback option to monolithic topology in case 2530 * any of the function topologies might be missing. 2531 * In this case we should use best effort to form the card, 2532 * ignoring functionalities that we are missing a fragment for. 2533 * 2534 * Note: monolithic topologies also ignore these possibly 2535 * missing functions, so the functionality of the card would be 2536 * identical to the case if there would be a fallback monolithic 2537 * topology created for the configuration. 2538 */ 2539 bool no_fallback = strstr(file, "dummy"); 2540 2541 tplg_cnt = sof_pdata->machine->get_function_tplg_files(scomp->card, 2542 sof_pdata->machine, 2543 tplg_filename_prefix, 2544 &tplg_files, 2545 no_fallback); 2546 if (tplg_cnt < 0) 2547 return tplg_cnt; 2548 } 2549 2550 /* 2551 * The monolithic topology will be used if there is no get_function_tplg_files 2552 * callback or the callback returns 0. 2553 */ 2554 if (!tplg_cnt) { 2555 if (strstr(file, "dummy")) { 2556 dev_err(scomp->dev, 2557 "Function topology is required, please upgrade sof-firmware\n"); 2558 return -EINVAL; 2559 } 2560 tplg_files[0] = file; 2561 tplg_cnt = 1; 2562 dev_info(scomp->dev, "loading topology: %s\n", file); 2563 } else { 2564 dev_info(scomp->dev, "Using function topologies instead %s\n", file); 2565 } 2566 2567 for (i = 0; i < tplg_cnt; i++) { 2568 /* Only print the file names if the function topologies are used */ 2569 if (tplg_files[0] != file) 2570 dev_info(scomp->dev, "loading topology %d: %s\n", i, tplg_files[i]); 2571 2572 const struct firmware *fw __free(firmware) = NULL; 2573 ret = request_firmware(&fw, tplg_files[i], scomp->dev); 2574 if (ret < 0) { 2575 /* 2576 * snd_soc_tplg_component_remove(scomp) will be called 2577 * if snd_soc_tplg_component_load(scomp) failed and all 2578 * objects in the scomp will be removed. No need to call 2579 * snd_soc_tplg_component_remove(scomp) here. 2580 */ 2581 dev_err(scomp->dev, "tplg request firmware %s failed err: %d\n", 2582 tplg_files[i], ret); 2583 goto out; 2584 } 2585 2586 if (sdev->dspless_mode_selected) 2587 ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw); 2588 else 2589 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw); 2590 2591 if (ret < 0) { 2592 dev_err(scomp->dev, "tplg %s component load failed %d\n", 2593 tplg_files[i], ret); 2594 goto out; 2595 } 2596 } 2597 2598 /* Loading user defined topologies */ 2599 for (i = 0; i < feature_tplg_cnt; i++) { 2600 const char *feature_topology = devm_kasprintf(scomp->dev, GFP_KERNEL, "%s/%s", 2601 tplg_filename_prefix, 2602 feature_topologies[i]); 2603 2604 if (!feature_topology) { 2605 ret = -ENOMEM; 2606 goto out; 2607 } 2608 dev_info(scomp->dev, "loading feature topology %d: %s\n", i, feature_topology); 2609 2610 const struct firmware *fw __free(firmware) = NULL; 2611 ret = request_firmware(&fw, feature_topology, scomp->dev); 2612 if (ret < 0) { 2613 /* 2614 * snd_soc_tplg_component_remove(scomp) will be called 2615 * if snd_soc_tplg_component_load(scomp) failed and all 2616 * objects in the scomp will be removed. No need to call 2617 * snd_soc_tplg_component_remove(scomp) here. 2618 */ 2619 dev_warn(scomp->dev, "feature tplg request firmware %s failed err: %d\n", 2620 feature_topologies[i], ret); 2621 /* 2622 * We don't return error here because we can still have the basic 2623 * audio feature when the function topology load complete. No need 2624 * to convert the error code because we will get new 'ret' out of the 2625 * loop. 2626 */ 2627 continue; 2628 } 2629 2630 if (sdev->dspless_mode_selected) 2631 ret = snd_soc_tplg_component_load(scomp, &sof_dspless_tplg_ops, fw); 2632 else 2633 ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw); 2634 2635 if (ret < 0) { 2636 dev_err(scomp->dev, "feature tplg %s component load failed %d\n", 2637 feature_topologies[i], ret); 2638 /* 2639 * We need to return error here because it may lead to kernel NULL pointer 2640 * dereference if we continue the remaining tasks. 2641 */ 2642 goto out; 2643 } 2644 } 2645 2646 /* call sof_complete when topologies are loaded successfully */ 2647 ret = sof_complete(scomp); 2648 2649 out: 2650 if (ret >= 0 && sdev->led_present) 2651 ret = snd_ctl_led_request(); 2652 2653 return ret; 2654 } 2655 EXPORT_SYMBOL(snd_sof_load_topology); 2656