1 // SPDX-License-Identifier: (GPL-2.0 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. All rights reserved. 7 // 8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // 10 11 #include <linux/firmware.h> 12 #include <sound/tlv.h> 13 #include <sound/pcm_params.h> 14 #include <uapi/sound/sof/tokens.h> 15 #include "sof-priv.h" 16 #include "ops.h" 17 18 #define COMP_ID_UNASSIGNED 0xffffffff 19 /* 20 * Constants used in the computation of linear volume gain 21 * from dB gain 20th root of 10 in Q1.16 fixed-point notation 22 */ 23 #define VOL_TWENTIETH_ROOT_OF_TEN 73533 24 /* 40th root of 10 in Q1.16 fixed-point notation*/ 25 #define VOL_FORTIETH_ROOT_OF_TEN 69419 26 /* 27 * Volume fractional word length define to 16 sets 28 * the volume linear gain value to use Qx.16 format 29 */ 30 #define VOLUME_FWL 16 31 /* 0.5 dB step value in topology TLV */ 32 #define VOL_HALF_DB_STEP 50 33 /* Full volume for default values */ 34 #define VOL_ZERO_DB BIT(VOLUME_FWL) 35 36 /* TLV data items */ 37 #define TLV_ITEMS 3 38 #define TLV_MIN 0 39 #define TLV_STEP 1 40 #define TLV_MUTE 2 41 42 /* size of tplg abi in byte */ 43 #define SOF_TPLG_ABI_SIZE 3 44 45 struct sof_widget_data { 46 int ctrl_type; 47 int ipc_cmd; 48 struct sof_abi_hdr *pdata; 49 struct snd_sof_control *control; 50 }; 51 52 /* send pcm params ipc */ 53 static int ipc_pcm_params(struct snd_sof_widget *swidget, int dir) 54 { 55 struct sof_ipc_pcm_params_reply ipc_params_reply; 56 struct snd_sof_dev *sdev = swidget->sdev; 57 struct sof_ipc_pcm_params pcm; 58 struct snd_pcm_hw_params *params; 59 struct snd_sof_pcm *spcm; 60 int ret = 0; 61 62 memset(&pcm, 0, sizeof(pcm)); 63 64 /* get runtime PCM params using widget's stream name */ 65 spcm = snd_sof_find_spcm_name(sdev, swidget->widget->sname); 66 if (!spcm) { 67 dev_err(sdev->dev, "error: cannot find PCM for %s\n", 68 swidget->widget->name); 69 return -EINVAL; 70 } 71 72 params = &spcm->params[dir]; 73 74 /* set IPC PCM params */ 75 pcm.hdr.size = sizeof(pcm); 76 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 77 pcm.comp_id = swidget->comp_id; 78 pcm.params.hdr.size = sizeof(pcm.params); 79 pcm.params.direction = dir; 80 pcm.params.sample_valid_bytes = params_width(params) >> 3; 81 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 82 pcm.params.rate = params_rate(params); 83 pcm.params.channels = params_channels(params); 84 pcm.params.host_period_bytes = params_period_bytes(params); 85 86 /* set format */ 87 switch (params_format(params)) { 88 case SNDRV_PCM_FORMAT_S16: 89 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE; 90 break; 91 case SNDRV_PCM_FORMAT_S24: 92 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE; 93 break; 94 case SNDRV_PCM_FORMAT_S32: 95 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 96 break; 97 default: 98 return -EINVAL; 99 } 100 101 /* send IPC to the DSP */ 102 ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm), 103 &ipc_params_reply, sizeof(ipc_params_reply)); 104 if (ret < 0) 105 dev_err(sdev->dev, "error: pcm params failed for %s\n", 106 swidget->widget->name); 107 108 return ret; 109 } 110 111 /* send stream trigger ipc */ 112 static int ipc_trigger(struct snd_sof_widget *swidget, int cmd) 113 { 114 struct snd_sof_dev *sdev = swidget->sdev; 115 struct sof_ipc_stream stream; 116 struct sof_ipc_reply reply; 117 int ret = 0; 118 119 /* set IPC stream params */ 120 stream.hdr.size = sizeof(stream); 121 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | cmd; 122 stream.comp_id = swidget->comp_id; 123 124 /* send IPC to the DSP */ 125 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream, 126 sizeof(stream), &reply, sizeof(reply)); 127 if (ret < 0) 128 dev_err(sdev->dev, "error: failed to trigger %s\n", 129 swidget->widget->name); 130 131 return ret; 132 } 133 134 static int sof_keyword_dapm_event(struct snd_soc_dapm_widget *w, 135 struct snd_kcontrol *k, int event) 136 { 137 struct snd_sof_widget *swidget = w->dobj.private; 138 struct snd_sof_dev *sdev; 139 int ret = 0; 140 141 if (!swidget) 142 return 0; 143 144 sdev = swidget->sdev; 145 146 dev_dbg(sdev->dev, "received event %d for widget %s\n", 147 event, w->name); 148 149 /* process events */ 150 switch (event) { 151 case SND_SOC_DAPM_PRE_PMU: 152 /* set pcm params */ 153 ret = ipc_pcm_params(swidget, SOF_IPC_STREAM_CAPTURE); 154 if (ret < 0) { 155 dev_err(sdev->dev, 156 "error: failed to set pcm params for widget %s\n", 157 swidget->widget->name); 158 break; 159 } 160 161 /* start trigger */ 162 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_START); 163 if (ret < 0) 164 dev_err(sdev->dev, 165 "error: failed to trigger widget %s\n", 166 swidget->widget->name); 167 break; 168 case SND_SOC_DAPM_POST_PMD: 169 /* stop trigger */ 170 ret = ipc_trigger(swidget, SOF_IPC_STREAM_TRIG_STOP); 171 if (ret < 0) 172 dev_err(sdev->dev, 173 "error: failed to trigger widget %s\n", 174 swidget->widget->name); 175 176 /* pcm free */ 177 ret = ipc_trigger(swidget, SOF_IPC_STREAM_PCM_FREE); 178 if (ret < 0) 179 dev_err(sdev->dev, 180 "error: failed to trigger widget %s\n", 181 swidget->widget->name); 182 break; 183 default: 184 break; 185 } 186 187 return ret; 188 } 189 190 /* event handlers for keyword detect component */ 191 static const struct snd_soc_tplg_widget_events sof_kwd_events[] = { 192 {SOF_KEYWORD_DETECT_DAPM_EVENT, sof_keyword_dapm_event}, 193 }; 194 195 static inline int get_tlv_data(const int *p, int tlv[TLV_ITEMS]) 196 { 197 /* we only support dB scale TLV type at the moment */ 198 if ((int)p[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE) 199 return -EINVAL; 200 201 /* min value in topology tlv data is multiplied by 100 */ 202 tlv[TLV_MIN] = (int)p[SNDRV_CTL_TLVO_DB_SCALE_MIN] / 100; 203 204 /* volume steps */ 205 tlv[TLV_STEP] = (int)(p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 206 TLV_DB_SCALE_MASK); 207 208 /* mute ON/OFF */ 209 if ((p[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 210 TLV_DB_SCALE_MUTE) == 0) 211 tlv[TLV_MUTE] = 0; 212 else 213 tlv[TLV_MUTE] = 1; 214 215 return 0; 216 } 217 218 /* 219 * Function to truncate an unsigned 64-bit number 220 * by x bits and return 32-bit unsigned number. This 221 * function also takes care of rounding while truncating 222 */ 223 static inline u32 vol_shift_64(u64 i, u32 x) 224 { 225 /* do not truncate more than 32 bits */ 226 if (x > 32) 227 x = 32; 228 229 if (x == 0) 230 return (u32)i; 231 232 return (u32)(((i >> (x - 1)) + 1) >> 1); 233 } 234 235 /* 236 * Function to compute a ^ exp where, 237 * a is a fractional number represented by a fixed-point 238 * integer with a fractional world length of "fwl" 239 * exp is an integer 240 * fwl is the fractional word length 241 * Return value is a fractional number represented by a 242 * fixed-point integer with a fractional word length of "fwl" 243 */ 244 static u32 vol_pow32(u32 a, int exp, u32 fwl) 245 { 246 int i, iter; 247 u32 power = 1 << fwl; 248 u64 numerator; 249 250 /* if exponent is 0, return 1 */ 251 if (exp == 0) 252 return power; 253 254 /* determine the number of iterations based on the exponent */ 255 if (exp < 0) 256 iter = exp * -1; 257 else 258 iter = exp; 259 260 /* mutiply a "iter" times to compute power */ 261 for (i = 0; i < iter; i++) { 262 /* 263 * Product of 2 Qx.fwl fixed-point numbers yields a Q2*x.2*fwl 264 * Truncate product back to fwl fractional bits with rounding 265 */ 266 power = vol_shift_64((u64)power * a, fwl); 267 } 268 269 if (exp > 0) { 270 /* if exp is positive, return the result */ 271 return power; 272 } 273 274 /* if exp is negative, return the multiplicative inverse */ 275 numerator = (u64)1 << (fwl << 1); 276 do_div(numerator, power); 277 278 return (u32)numerator; 279 } 280 281 /* 282 * Function to calculate volume gain from TLV data. 283 * This function can only handle gain steps that are multiples of 0.5 dB 284 */ 285 static u32 vol_compute_gain(u32 value, int *tlv) 286 { 287 int dB_gain; 288 u32 linear_gain; 289 int f_step; 290 291 /* mute volume */ 292 if (value == 0 && tlv[TLV_MUTE]) 293 return 0; 294 295 /* 296 * compute dB gain from tlv. tlv_step 297 * in topology is multiplied by 100 298 */ 299 dB_gain = tlv[TLV_MIN] + (value * tlv[TLV_STEP]) / 100; 300 301 /* 302 * compute linear gain represented by fixed-point 303 * int with VOLUME_FWL fractional bits 304 */ 305 linear_gain = vol_pow32(VOL_TWENTIETH_ROOT_OF_TEN, dB_gain, VOLUME_FWL); 306 307 /* extract the fractional part of volume step */ 308 f_step = tlv[TLV_STEP] - (tlv[TLV_STEP] / 100); 309 310 /* if volume step is an odd multiple of 0.5 dB */ 311 if (f_step == VOL_HALF_DB_STEP && (value & 1)) 312 linear_gain = vol_shift_64((u64)linear_gain * 313 VOL_FORTIETH_ROOT_OF_TEN, 314 VOLUME_FWL); 315 316 return linear_gain; 317 } 318 319 /* 320 * Set up volume table for kcontrols from tlv data 321 * "size" specifies the number of entries in the table 322 */ 323 static int set_up_volume_table(struct snd_sof_control *scontrol, 324 int tlv[TLV_ITEMS], int size) 325 { 326 int j; 327 328 /* init the volume table */ 329 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL); 330 if (!scontrol->volume_table) 331 return -ENOMEM; 332 333 /* populate the volume table */ 334 for (j = 0; j < size ; j++) 335 scontrol->volume_table[j] = vol_compute_gain(j, tlv); 336 337 return 0; 338 } 339 340 struct sof_dai_types { 341 const char *name; 342 enum sof_ipc_dai_type type; 343 }; 344 345 static const struct sof_dai_types sof_dais[] = { 346 {"SSP", SOF_DAI_INTEL_SSP}, 347 {"HDA", SOF_DAI_INTEL_HDA}, 348 {"DMIC", SOF_DAI_INTEL_DMIC}, 349 {"ALH", SOF_DAI_INTEL_ALH}, 350 {"SAI", SOF_DAI_IMX_SAI}, 351 {"ESAI", SOF_DAI_IMX_ESAI}, 352 }; 353 354 static enum sof_ipc_dai_type find_dai(const char *name) 355 { 356 int i; 357 358 for (i = 0; i < ARRAY_SIZE(sof_dais); i++) { 359 if (strcmp(name, sof_dais[i].name) == 0) 360 return sof_dais[i].type; 361 } 362 363 return SOF_DAI_INTEL_NONE; 364 } 365 366 /* 367 * Supported Frame format types and lookup, add new ones to end of list. 368 */ 369 370 struct sof_frame_types { 371 const char *name; 372 enum sof_ipc_frame frame; 373 }; 374 375 static const struct sof_frame_types sof_frames[] = { 376 {"s16le", SOF_IPC_FRAME_S16_LE}, 377 {"s24le", SOF_IPC_FRAME_S24_4LE}, 378 {"s32le", SOF_IPC_FRAME_S32_LE}, 379 {"float", SOF_IPC_FRAME_FLOAT}, 380 }; 381 382 static enum sof_ipc_frame find_format(const char *name) 383 { 384 int i; 385 386 for (i = 0; i < ARRAY_SIZE(sof_frames); i++) { 387 if (strcmp(name, sof_frames[i].name) == 0) 388 return sof_frames[i].frame; 389 } 390 391 /* use s32le if nothing is specified */ 392 return SOF_IPC_FRAME_S32_LE; 393 } 394 395 struct sof_process_types { 396 const char *name; 397 enum sof_ipc_process_type type; 398 enum sof_comp_type comp_type; 399 }; 400 401 static const struct sof_process_types sof_process[] = { 402 {"EQFIR", SOF_PROCESS_EQFIR, SOF_COMP_EQ_FIR}, 403 {"EQIIR", SOF_PROCESS_EQIIR, SOF_COMP_EQ_IIR}, 404 {"KEYWORD_DETECT", SOF_PROCESS_KEYWORD_DETECT, SOF_COMP_KEYWORD_DETECT}, 405 {"KPB", SOF_PROCESS_KPB, SOF_COMP_KPB}, 406 {"CHAN_SELECTOR", SOF_PROCESS_CHAN_SELECTOR, SOF_COMP_SELECTOR}, 407 {"MUX", SOF_PROCESS_MUX, SOF_COMP_MUX}, 408 {"DEMUX", SOF_PROCESS_DEMUX, SOF_COMP_DEMUX}, 409 }; 410 411 static enum sof_ipc_process_type find_process(const char *name) 412 { 413 int i; 414 415 for (i = 0; i < ARRAY_SIZE(sof_process); i++) { 416 if (strcmp(name, sof_process[i].name) == 0) 417 return sof_process[i].type; 418 } 419 420 return SOF_PROCESS_NONE; 421 } 422 423 static enum sof_comp_type find_process_comp_type(enum sof_ipc_process_type type) 424 { 425 int i; 426 427 for (i = 0; i < ARRAY_SIZE(sof_process); i++) { 428 if (sof_process[i].type == type) 429 return sof_process[i].comp_type; 430 } 431 432 return SOF_COMP_NONE; 433 } 434 435 /* 436 * Standard Kcontrols. 437 */ 438 439 static int sof_control_load_volume(struct snd_soc_component *scomp, 440 struct snd_sof_control *scontrol, 441 struct snd_kcontrol_new *kc, 442 struct snd_soc_tplg_ctl_hdr *hdr) 443 { 444 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 445 struct snd_soc_tplg_mixer_control *mc = 446 container_of(hdr, struct snd_soc_tplg_mixer_control, hdr); 447 struct sof_ipc_ctrl_data *cdata; 448 int tlv[TLV_ITEMS]; 449 unsigned int i; 450 int ret; 451 452 /* validate topology data */ 453 if (le32_to_cpu(mc->num_channels) > SND_SOC_TPLG_MAX_CHAN) 454 return -EINVAL; 455 456 /* init the volume get/put data */ 457 scontrol->size = struct_size(scontrol->control_data, chanv, 458 le32_to_cpu(mc->num_channels)); 459 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL); 460 if (!scontrol->control_data) 461 return -ENOMEM; 462 463 scontrol->comp_id = sdev->next_comp_id; 464 scontrol->min_volume_step = le32_to_cpu(mc->min); 465 scontrol->max_volume_step = le32_to_cpu(mc->max); 466 scontrol->num_channels = le32_to_cpu(mc->num_channels); 467 468 /* set cmd for mixer control */ 469 if (le32_to_cpu(mc->max) == 1) { 470 scontrol->cmd = SOF_CTRL_CMD_SWITCH; 471 goto out; 472 } 473 474 scontrol->cmd = SOF_CTRL_CMD_VOLUME; 475 476 /* extract tlv data */ 477 if (get_tlv_data(kc->tlv.p, tlv) < 0) { 478 dev_err(sdev->dev, "error: invalid TLV data\n"); 479 return -EINVAL; 480 } 481 482 /* set up volume table */ 483 ret = set_up_volume_table(scontrol, tlv, le32_to_cpu(mc->max) + 1); 484 if (ret < 0) { 485 dev_err(sdev->dev, "error: setting up volume table\n"); 486 return ret; 487 } 488 489 /* set default volume values to 0dB in control */ 490 cdata = scontrol->control_data; 491 for (i = 0; i < scontrol->num_channels; i++) { 492 cdata->chanv[i].channel = i; 493 cdata->chanv[i].value = VOL_ZERO_DB; 494 } 495 496 out: 497 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d\n", 498 scontrol->comp_id, scontrol->num_channels); 499 500 return 0; 501 } 502 503 static int sof_control_load_enum(struct snd_soc_component *scomp, 504 struct snd_sof_control *scontrol, 505 struct snd_kcontrol_new *kc, 506 struct snd_soc_tplg_ctl_hdr *hdr) 507 { 508 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 509 struct snd_soc_tplg_enum_control *ec = 510 container_of(hdr, struct snd_soc_tplg_enum_control, hdr); 511 512 /* validate topology data */ 513 if (le32_to_cpu(ec->num_channels) > SND_SOC_TPLG_MAX_CHAN) 514 return -EINVAL; 515 516 /* init the enum get/put data */ 517 scontrol->size = struct_size(scontrol->control_data, chanv, 518 le32_to_cpu(ec->num_channels)); 519 scontrol->control_data = kzalloc(scontrol->size, GFP_KERNEL); 520 if (!scontrol->control_data) 521 return -ENOMEM; 522 523 scontrol->comp_id = sdev->next_comp_id; 524 scontrol->num_channels = le32_to_cpu(ec->num_channels); 525 526 scontrol->cmd = SOF_CTRL_CMD_ENUM; 527 528 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d comp_id %d\n", 529 scontrol->comp_id, scontrol->num_channels, scontrol->comp_id); 530 531 return 0; 532 } 533 534 static int sof_control_load_bytes(struct snd_soc_component *scomp, 535 struct snd_sof_control *scontrol, 536 struct snd_kcontrol_new *kc, 537 struct snd_soc_tplg_ctl_hdr *hdr) 538 { 539 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 540 struct sof_ipc_ctrl_data *cdata; 541 struct snd_soc_tplg_bytes_control *control = 542 container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); 543 struct soc_bytes_ext *sbe = (struct soc_bytes_ext *)kc->private_value; 544 int max_size = sbe->max; 545 546 if (le32_to_cpu(control->priv.size) > max_size) { 547 dev_err(sdev->dev, "err: bytes data size %d exceeds max %d.\n", 548 control->priv.size, max_size); 549 return -EINVAL; 550 } 551 552 /* init the get/put bytes data */ 553 scontrol->size = sizeof(struct sof_ipc_ctrl_data) + 554 le32_to_cpu(control->priv.size); 555 scontrol->control_data = kzalloc(max_size, GFP_KERNEL); 556 cdata = scontrol->control_data; 557 if (!scontrol->control_data) 558 return -ENOMEM; 559 560 scontrol->comp_id = sdev->next_comp_id; 561 scontrol->cmd = SOF_CTRL_CMD_BINARY; 562 563 dev_dbg(sdev->dev, "tplg: load kcontrol index %d chans %d\n", 564 scontrol->comp_id, scontrol->num_channels); 565 566 if (le32_to_cpu(control->priv.size) > 0) { 567 memcpy(cdata->data, control->priv.data, 568 le32_to_cpu(control->priv.size)); 569 570 if (cdata->data->magic != SOF_ABI_MAGIC) { 571 dev_err(sdev->dev, "error: Wrong ABI magic 0x%08x.\n", 572 cdata->data->magic); 573 return -EINVAL; 574 } 575 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, 576 cdata->data->abi)) { 577 dev_err(sdev->dev, 578 "error: Incompatible ABI version 0x%08x.\n", 579 cdata->data->abi); 580 return -EINVAL; 581 } 582 if (cdata->data->size + sizeof(const struct sof_abi_hdr) != 583 le32_to_cpu(control->priv.size)) { 584 dev_err(sdev->dev, 585 "error: Conflict in bytes vs. priv size.\n"); 586 return -EINVAL; 587 } 588 } 589 return 0; 590 } 591 592 /* 593 * Topology Token Parsing. 594 * New tokens should be added to headers and parsing tables below. 595 */ 596 597 struct sof_topology_token { 598 u32 token; 599 u32 type; 600 int (*get_token)(void *elem, void *object, u32 offset, u32 size); 601 u32 offset; 602 u32 size; 603 }; 604 605 static int get_token_u32(void *elem, void *object, u32 offset, u32 size) 606 { 607 struct snd_soc_tplg_vendor_value_elem *velem = elem; 608 u32 *val = (u32 *)((u8 *)object + offset); 609 610 *val = le32_to_cpu(velem->value); 611 return 0; 612 } 613 614 static int get_token_u16(void *elem, void *object, u32 offset, u32 size) 615 { 616 struct snd_soc_tplg_vendor_value_elem *velem = elem; 617 u16 *val = (u16 *)((u8 *)object + offset); 618 619 *val = (u16)le32_to_cpu(velem->value); 620 return 0; 621 } 622 623 static int get_token_comp_format(void *elem, void *object, u32 offset, u32 size) 624 { 625 struct snd_soc_tplg_vendor_string_elem *velem = elem; 626 u32 *val = (u32 *)((u8 *)object + offset); 627 628 *val = find_format(velem->string); 629 return 0; 630 } 631 632 static int get_token_dai_type(void *elem, void *object, u32 offset, u32 size) 633 { 634 struct snd_soc_tplg_vendor_string_elem *velem = elem; 635 u32 *val = (u32 *)((u8 *)object + offset); 636 637 *val = find_dai(velem->string); 638 return 0; 639 } 640 641 static int get_token_process_type(void *elem, void *object, u32 offset, 642 u32 size) 643 { 644 struct snd_soc_tplg_vendor_string_elem *velem = elem; 645 u32 *val = (u32 *)((u8 *)object + offset); 646 647 *val = find_process(velem->string); 648 return 0; 649 } 650 651 /* Buffers */ 652 static const struct sof_topology_token buffer_tokens[] = { 653 {SOF_TKN_BUF_SIZE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 654 offsetof(struct sof_ipc_buffer, size), 0}, 655 {SOF_TKN_BUF_CAPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 656 offsetof(struct sof_ipc_buffer, caps), 0}, 657 }; 658 659 /* DAI */ 660 static const struct sof_topology_token dai_tokens[] = { 661 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 662 offsetof(struct sof_ipc_comp_dai, type), 0}, 663 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 664 offsetof(struct sof_ipc_comp_dai, dai_index), 0}, 665 {SOF_TKN_DAI_DIRECTION, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 666 offsetof(struct sof_ipc_comp_dai, direction), 0}, 667 }; 668 669 /* BE DAI link */ 670 static const struct sof_topology_token dai_link_tokens[] = { 671 {SOF_TKN_DAI_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_dai_type, 672 offsetof(struct sof_ipc_dai_config, type), 0}, 673 {SOF_TKN_DAI_INDEX, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 674 offsetof(struct sof_ipc_dai_config, dai_index), 0}, 675 }; 676 677 /* scheduling */ 678 static const struct sof_topology_token sched_tokens[] = { 679 {SOF_TKN_SCHED_PERIOD, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 680 offsetof(struct sof_ipc_pipe_new, period), 0}, 681 {SOF_TKN_SCHED_PRIORITY, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 682 offsetof(struct sof_ipc_pipe_new, priority), 0}, 683 {SOF_TKN_SCHED_MIPS, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 684 offsetof(struct sof_ipc_pipe_new, period_mips), 0}, 685 {SOF_TKN_SCHED_CORE, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 686 offsetof(struct sof_ipc_pipe_new, core), 0}, 687 {SOF_TKN_SCHED_FRAMES, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 688 offsetof(struct sof_ipc_pipe_new, frames_per_sched), 0}, 689 {SOF_TKN_SCHED_TIME_DOMAIN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 690 offsetof(struct sof_ipc_pipe_new, time_domain), 0}, 691 }; 692 693 /* volume */ 694 static const struct sof_topology_token volume_tokens[] = { 695 {SOF_TKN_VOLUME_RAMP_STEP_TYPE, SND_SOC_TPLG_TUPLE_TYPE_WORD, 696 get_token_u32, offsetof(struct sof_ipc_comp_volume, ramp), 0}, 697 {SOF_TKN_VOLUME_RAMP_STEP_MS, 698 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 699 offsetof(struct sof_ipc_comp_volume, initial_ramp), 0}, 700 }; 701 702 /* SRC */ 703 static const struct sof_topology_token src_tokens[] = { 704 {SOF_TKN_SRC_RATE_IN, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 705 offsetof(struct sof_ipc_comp_src, source_rate), 0}, 706 {SOF_TKN_SRC_RATE_OUT, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 707 offsetof(struct sof_ipc_comp_src, sink_rate), 0}, 708 }; 709 710 /* Tone */ 711 static const struct sof_topology_token tone_tokens[] = { 712 }; 713 714 /* EFFECT */ 715 static const struct sof_topology_token process_tokens[] = { 716 {SOF_TKN_PROCESS_TYPE, SND_SOC_TPLG_TUPLE_TYPE_STRING, 717 get_token_process_type, 718 offsetof(struct sof_ipc_comp_process, type), 0}, 719 }; 720 721 /* PCM */ 722 static const struct sof_topology_token pcm_tokens[] = { 723 {SOF_TKN_PCM_DMAC_CONFIG, SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 724 offsetof(struct sof_ipc_comp_host, dmac_config), 0}, 725 }; 726 727 /* Generic components */ 728 static const struct sof_topology_token comp_tokens[] = { 729 {SOF_TKN_COMP_PERIOD_SINK_COUNT, 730 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 731 offsetof(struct sof_ipc_comp_config, periods_sink), 0}, 732 {SOF_TKN_COMP_PERIOD_SOURCE_COUNT, 733 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 734 offsetof(struct sof_ipc_comp_config, periods_source), 0}, 735 {SOF_TKN_COMP_FORMAT, 736 SND_SOC_TPLG_TUPLE_TYPE_STRING, get_token_comp_format, 737 offsetof(struct sof_ipc_comp_config, frame_fmt), 0}, 738 }; 739 740 /* SSP */ 741 static const struct sof_topology_token ssp_tokens[] = { 742 {SOF_TKN_INTEL_SSP_CLKS_CONTROL, 743 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 744 offsetof(struct sof_ipc_dai_ssp_params, clks_control), 0}, 745 {SOF_TKN_INTEL_SSP_MCLK_ID, 746 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 747 offsetof(struct sof_ipc_dai_ssp_params, mclk_id), 0}, 748 {SOF_TKN_INTEL_SSP_SAMPLE_BITS, SND_SOC_TPLG_TUPLE_TYPE_WORD, 749 get_token_u32, 750 offsetof(struct sof_ipc_dai_ssp_params, sample_valid_bits), 0}, 751 {SOF_TKN_INTEL_SSP_FRAME_PULSE_WIDTH, SND_SOC_TPLG_TUPLE_TYPE_SHORT, 752 get_token_u16, 753 offsetof(struct sof_ipc_dai_ssp_params, frame_pulse_width), 0}, 754 {SOF_TKN_INTEL_SSP_QUIRKS, SND_SOC_TPLG_TUPLE_TYPE_WORD, 755 get_token_u32, 756 offsetof(struct sof_ipc_dai_ssp_params, quirks), 0}, 757 {SOF_TKN_INTEL_SSP_TDM_PADDING_PER_SLOT, SND_SOC_TPLG_TUPLE_TYPE_BOOL, 758 get_token_u16, 759 offsetof(struct sof_ipc_dai_ssp_params, 760 tdm_per_slot_padding_flag), 0}, 761 {SOF_TKN_INTEL_SSP_BCLK_DELAY, SND_SOC_TPLG_TUPLE_TYPE_WORD, 762 get_token_u32, 763 offsetof(struct sof_ipc_dai_ssp_params, bclk_delay), 0}, 764 765 }; 766 767 /* DMIC */ 768 static const struct sof_topology_token dmic_tokens[] = { 769 {SOF_TKN_INTEL_DMIC_DRIVER_VERSION, 770 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 771 offsetof(struct sof_ipc_dai_dmic_params, driver_ipc_version), 772 0}, 773 {SOF_TKN_INTEL_DMIC_CLK_MIN, 774 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 775 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_min), 0}, 776 {SOF_TKN_INTEL_DMIC_CLK_MAX, 777 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 778 offsetof(struct sof_ipc_dai_dmic_params, pdmclk_max), 0}, 779 {SOF_TKN_INTEL_DMIC_SAMPLE_RATE, 780 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 781 offsetof(struct sof_ipc_dai_dmic_params, fifo_fs), 0}, 782 {SOF_TKN_INTEL_DMIC_DUTY_MIN, 783 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 784 offsetof(struct sof_ipc_dai_dmic_params, duty_min), 0}, 785 {SOF_TKN_INTEL_DMIC_DUTY_MAX, 786 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 787 offsetof(struct sof_ipc_dai_dmic_params, duty_max), 0}, 788 {SOF_TKN_INTEL_DMIC_NUM_PDM_ACTIVE, 789 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 790 offsetof(struct sof_ipc_dai_dmic_params, 791 num_pdm_active), 0}, 792 {SOF_TKN_INTEL_DMIC_FIFO_WORD_LENGTH, 793 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 794 offsetof(struct sof_ipc_dai_dmic_params, fifo_bits), 0}, 795 {SOF_TKN_INTEL_DMIC_UNMUTE_RAMP_TIME_MS, 796 SND_SOC_TPLG_TUPLE_TYPE_WORD, get_token_u32, 797 offsetof(struct sof_ipc_dai_dmic_params, unmute_ramp_time), 0}, 798 799 }; 800 801 /* 802 * DMIC PDM Tokens 803 * SOF_TKN_INTEL_DMIC_PDM_CTRL_ID should be the first token 804 * as it increments the index while parsing the array of pdm tokens 805 * and determines the correct offset 806 */ 807 static const struct sof_topology_token dmic_pdm_tokens[] = { 808 {SOF_TKN_INTEL_DMIC_PDM_CTRL_ID, 809 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 810 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, id), 811 0}, 812 {SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable, 813 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 814 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_a), 815 0}, 816 {SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable, 817 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 818 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, enable_mic_b), 819 0}, 820 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_A, 821 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 822 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_a), 823 0}, 824 {SOF_TKN_INTEL_DMIC_PDM_POLARITY_B, 825 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 826 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, polarity_mic_b), 827 0}, 828 {SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE, 829 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 830 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, clk_edge), 831 0}, 832 {SOF_TKN_INTEL_DMIC_PDM_SKEW, 833 SND_SOC_TPLG_TUPLE_TYPE_SHORT, get_token_u16, 834 offsetof(struct sof_ipc_dai_dmic_pdm_ctrl, skew), 835 0}, 836 }; 837 838 /* HDA */ 839 static const struct sof_topology_token hda_tokens[] = { 840 }; 841 842 static void sof_parse_uuid_tokens(struct snd_soc_component *scomp, 843 void *object, 844 const struct sof_topology_token *tokens, 845 int count, 846 struct snd_soc_tplg_vendor_array *array) 847 { 848 struct snd_soc_tplg_vendor_uuid_elem *elem; 849 int i, j; 850 851 /* parse element by element */ 852 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 853 elem = &array->uuid[i]; 854 855 /* search for token */ 856 for (j = 0; j < count; j++) { 857 /* match token type */ 858 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_UUID) 859 continue; 860 861 /* match token id */ 862 if (tokens[j].token != le32_to_cpu(elem->token)) 863 continue; 864 865 /* matched - now load token */ 866 tokens[j].get_token(elem, object, tokens[j].offset, 867 tokens[j].size); 868 } 869 } 870 } 871 872 static void sof_parse_string_tokens(struct snd_soc_component *scomp, 873 void *object, 874 const struct sof_topology_token *tokens, 875 int count, 876 struct snd_soc_tplg_vendor_array *array) 877 { 878 struct snd_soc_tplg_vendor_string_elem *elem; 879 int i, j; 880 881 /* parse element by element */ 882 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 883 elem = &array->string[i]; 884 885 /* search for token */ 886 for (j = 0; j < count; j++) { 887 /* match token type */ 888 if (tokens[j].type != SND_SOC_TPLG_TUPLE_TYPE_STRING) 889 continue; 890 891 /* match token id */ 892 if (tokens[j].token != le32_to_cpu(elem->token)) 893 continue; 894 895 /* matched - now load token */ 896 tokens[j].get_token(elem, object, tokens[j].offset, 897 tokens[j].size); 898 } 899 } 900 } 901 902 static void sof_parse_word_tokens(struct snd_soc_component *scomp, 903 void *object, 904 const struct sof_topology_token *tokens, 905 int count, 906 struct snd_soc_tplg_vendor_array *array) 907 { 908 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 909 struct snd_soc_tplg_vendor_value_elem *elem; 910 size_t size = sizeof(struct sof_ipc_dai_dmic_pdm_ctrl); 911 int i, j; 912 u32 offset; 913 u32 *index = NULL; 914 915 /* parse element by element */ 916 for (i = 0; i < le32_to_cpu(array->num_elems); i++) { 917 elem = &array->value[i]; 918 919 /* search for token */ 920 for (j = 0; j < count; j++) { 921 /* match token type */ 922 if (!(tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_WORD || 923 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_SHORT || 924 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BYTE || 925 tokens[j].type == SND_SOC_TPLG_TUPLE_TYPE_BOOL)) 926 continue; 927 928 /* match token id */ 929 if (tokens[j].token != le32_to_cpu(elem->token)) 930 continue; 931 932 /* pdm config array index */ 933 if (sdev->private) 934 index = sdev->private; 935 936 /* matched - determine offset */ 937 switch (tokens[j].token) { 938 case SOF_TKN_INTEL_DMIC_PDM_CTRL_ID: 939 940 /* inc number of pdm array index */ 941 if (index) 942 (*index)++; 943 /* fallthrough */ 944 case SOF_TKN_INTEL_DMIC_PDM_MIC_A_Enable: 945 case SOF_TKN_INTEL_DMIC_PDM_MIC_B_Enable: 946 case SOF_TKN_INTEL_DMIC_PDM_POLARITY_A: 947 case SOF_TKN_INTEL_DMIC_PDM_POLARITY_B: 948 case SOF_TKN_INTEL_DMIC_PDM_CLK_EDGE: 949 case SOF_TKN_INTEL_DMIC_PDM_SKEW: 950 951 /* check if array index is valid */ 952 if (!index || *index == 0) { 953 dev_err(sdev->dev, 954 "error: invalid array offset\n"); 955 continue; 956 } else { 957 /* offset within the pdm config array */ 958 offset = size * (*index - 1); 959 } 960 break; 961 default: 962 offset = 0; 963 break; 964 } 965 966 /* load token */ 967 tokens[j].get_token(elem, object, 968 offset + tokens[j].offset, 969 tokens[j].size); 970 } 971 } 972 } 973 974 static int sof_parse_tokens(struct snd_soc_component *scomp, 975 void *object, 976 const struct sof_topology_token *tokens, 977 int count, 978 struct snd_soc_tplg_vendor_array *array, 979 int priv_size) 980 { 981 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 982 int asize; 983 984 while (priv_size > 0) { 985 asize = le32_to_cpu(array->size); 986 987 /* validate asize */ 988 if (asize < 0) { /* FIXME: A zero-size array makes no sense */ 989 dev_err(sdev->dev, "error: invalid array size 0x%x\n", 990 asize); 991 return -EINVAL; 992 } 993 994 /* make sure there is enough data before parsing */ 995 priv_size -= asize; 996 if (priv_size < 0) { 997 dev_err(sdev->dev, "error: invalid array size 0x%x\n", 998 asize); 999 return -EINVAL; 1000 } 1001 1002 /* call correct parser depending on type */ 1003 switch (le32_to_cpu(array->type)) { 1004 case SND_SOC_TPLG_TUPLE_TYPE_UUID: 1005 sof_parse_uuid_tokens(scomp, object, tokens, count, 1006 array); 1007 break; 1008 case SND_SOC_TPLG_TUPLE_TYPE_STRING: 1009 sof_parse_string_tokens(scomp, object, tokens, count, 1010 array); 1011 break; 1012 case SND_SOC_TPLG_TUPLE_TYPE_BOOL: 1013 case SND_SOC_TPLG_TUPLE_TYPE_BYTE: 1014 case SND_SOC_TPLG_TUPLE_TYPE_WORD: 1015 case SND_SOC_TPLG_TUPLE_TYPE_SHORT: 1016 sof_parse_word_tokens(scomp, object, tokens, count, 1017 array); 1018 break; 1019 default: 1020 dev_err(sdev->dev, "error: unknown token type %d\n", 1021 array->type); 1022 return -EINVAL; 1023 } 1024 1025 /* next array */ 1026 array = (struct snd_soc_tplg_vendor_array *)((u8 *)array 1027 + asize); 1028 } 1029 return 0; 1030 } 1031 1032 static void sof_dbg_comp_config(struct snd_soc_component *scomp, 1033 struct sof_ipc_comp_config *config) 1034 { 1035 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1036 1037 dev_dbg(sdev->dev, " config: periods snk %d src %d fmt %d\n", 1038 config->periods_sink, config->periods_source, 1039 config->frame_fmt); 1040 } 1041 1042 /* external kcontrol init - used for any driver specific init */ 1043 static int sof_control_load(struct snd_soc_component *scomp, int index, 1044 struct snd_kcontrol_new *kc, 1045 struct snd_soc_tplg_ctl_hdr *hdr) 1046 { 1047 struct soc_mixer_control *sm; 1048 struct soc_bytes_ext *sbe; 1049 struct soc_enum *se; 1050 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1051 struct snd_soc_dobj *dobj; 1052 struct snd_sof_control *scontrol; 1053 int ret = -EINVAL; 1054 1055 dev_dbg(sdev->dev, "tplg: load control type %d name : %s\n", 1056 hdr->type, hdr->name); 1057 1058 scontrol = kzalloc(sizeof(*scontrol), GFP_KERNEL); 1059 if (!scontrol) 1060 return -ENOMEM; 1061 1062 scontrol->sdev = sdev; 1063 1064 switch (le32_to_cpu(hdr->ops.info)) { 1065 case SND_SOC_TPLG_CTL_VOLSW: 1066 case SND_SOC_TPLG_CTL_VOLSW_SX: 1067 case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 1068 sm = (struct soc_mixer_control *)kc->private_value; 1069 dobj = &sm->dobj; 1070 ret = sof_control_load_volume(scomp, scontrol, kc, hdr); 1071 break; 1072 case SND_SOC_TPLG_CTL_BYTES: 1073 sbe = (struct soc_bytes_ext *)kc->private_value; 1074 dobj = &sbe->dobj; 1075 ret = sof_control_load_bytes(scomp, scontrol, kc, hdr); 1076 break; 1077 case SND_SOC_TPLG_CTL_ENUM: 1078 case SND_SOC_TPLG_CTL_ENUM_VALUE: 1079 se = (struct soc_enum *)kc->private_value; 1080 dobj = &se->dobj; 1081 ret = sof_control_load_enum(scomp, scontrol, kc, hdr); 1082 break; 1083 case SND_SOC_TPLG_CTL_RANGE: 1084 case SND_SOC_TPLG_CTL_STROBE: 1085 case SND_SOC_TPLG_DAPM_CTL_VOLSW: 1086 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 1087 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 1088 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 1089 case SND_SOC_TPLG_DAPM_CTL_PIN: 1090 default: 1091 dev_warn(sdev->dev, "control type not supported %d:%d:%d\n", 1092 hdr->ops.get, hdr->ops.put, hdr->ops.info); 1093 kfree(scontrol); 1094 return 0; 1095 } 1096 1097 dobj->private = scontrol; 1098 list_add(&scontrol->list, &sdev->kcontrol_list); 1099 return ret; 1100 } 1101 1102 static int sof_control_unload(struct snd_soc_component *scomp, 1103 struct snd_soc_dobj *dobj) 1104 { 1105 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1106 struct sof_ipc_free fcomp; 1107 struct snd_sof_control *scontrol = dobj->private; 1108 1109 dev_dbg(sdev->dev, "tplg: unload control name : %s\n", scomp->name); 1110 1111 fcomp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_FREE; 1112 fcomp.hdr.size = sizeof(fcomp); 1113 fcomp.id = scontrol->comp_id; 1114 1115 kfree(scontrol->control_data); 1116 list_del(&scontrol->list); 1117 kfree(scontrol); 1118 /* send IPC to the DSP */ 1119 return sof_ipc_tx_message(sdev->ipc, 1120 fcomp.hdr.cmd, &fcomp, sizeof(fcomp), 1121 NULL, 0); 1122 } 1123 1124 /* 1125 * DAI Topology 1126 */ 1127 1128 static int sof_connect_dai_widget(struct snd_soc_component *scomp, 1129 struct snd_soc_dapm_widget *w, 1130 struct snd_soc_tplg_dapm_widget *tw, 1131 struct snd_sof_dai *dai) 1132 { 1133 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1134 struct snd_soc_card *card = scomp->card; 1135 struct snd_soc_pcm_runtime *rtd; 1136 1137 list_for_each_entry(rtd, &card->rtd_list, list) { 1138 dev_vdbg(sdev->dev, "tplg: check widget: %s stream: %s dai stream: %s\n", 1139 w->name, w->sname, rtd->dai_link->stream_name); 1140 1141 if (!w->sname || !rtd->dai_link->stream_name) 1142 continue; 1143 1144 /* does stream match DAI link ? */ 1145 if (strcmp(w->sname, rtd->dai_link->stream_name)) 1146 continue; 1147 1148 switch (w->id) { 1149 case snd_soc_dapm_dai_out: 1150 rtd->cpu_dai->capture_widget = w; 1151 dai->name = rtd->dai_link->name; 1152 dev_dbg(sdev->dev, "tplg: connected widget %s -> DAI link %s\n", 1153 w->name, rtd->dai_link->name); 1154 break; 1155 case snd_soc_dapm_dai_in: 1156 rtd->cpu_dai->playback_widget = w; 1157 dai->name = rtd->dai_link->name; 1158 dev_dbg(sdev->dev, "tplg: connected widget %s -> DAI link %s\n", 1159 w->name, rtd->dai_link->name); 1160 break; 1161 default: 1162 break; 1163 } 1164 } 1165 1166 /* check we have a connection */ 1167 if (!dai->name) { 1168 dev_err(sdev->dev, "error: can't connect DAI %s stream %s\n", 1169 w->name, w->sname); 1170 return -EINVAL; 1171 } 1172 1173 return 0; 1174 } 1175 1176 static int sof_widget_load_dai(struct snd_soc_component *scomp, int index, 1177 struct snd_sof_widget *swidget, 1178 struct snd_soc_tplg_dapm_widget *tw, 1179 struct sof_ipc_comp_reply *r, 1180 struct snd_sof_dai *dai) 1181 { 1182 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1183 struct snd_soc_tplg_private *private = &tw->priv; 1184 struct sof_ipc_comp_dai comp_dai; 1185 int ret; 1186 1187 /* configure dai IPC message */ 1188 memset(&comp_dai, 0, sizeof(comp_dai)); 1189 comp_dai.comp.hdr.size = sizeof(comp_dai); 1190 comp_dai.comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1191 comp_dai.comp.id = swidget->comp_id; 1192 comp_dai.comp.type = SOF_COMP_DAI; 1193 comp_dai.comp.pipeline_id = index; 1194 comp_dai.config.hdr.size = sizeof(comp_dai.config); 1195 1196 ret = sof_parse_tokens(scomp, &comp_dai, dai_tokens, 1197 ARRAY_SIZE(dai_tokens), private->array, 1198 le32_to_cpu(private->size)); 1199 if (ret != 0) { 1200 dev_err(sdev->dev, "error: parse dai tokens failed %d\n", 1201 le32_to_cpu(private->size)); 1202 return ret; 1203 } 1204 1205 ret = sof_parse_tokens(scomp, &comp_dai.config, comp_tokens, 1206 ARRAY_SIZE(comp_tokens), private->array, 1207 le32_to_cpu(private->size)); 1208 if (ret != 0) { 1209 dev_err(sdev->dev, "error: parse dai.cfg tokens failed %d\n", 1210 private->size); 1211 return ret; 1212 } 1213 1214 dev_dbg(sdev->dev, "dai %s: type %d index %d\n", 1215 swidget->widget->name, comp_dai.type, comp_dai.dai_index); 1216 sof_dbg_comp_config(scomp, &comp_dai.config); 1217 1218 ret = sof_ipc_tx_message(sdev->ipc, comp_dai.comp.hdr.cmd, 1219 &comp_dai, sizeof(comp_dai), r, sizeof(*r)); 1220 1221 if (ret == 0 && dai) { 1222 dai->sdev = sdev; 1223 memcpy(&dai->comp_dai, &comp_dai, sizeof(comp_dai)); 1224 } 1225 1226 return ret; 1227 } 1228 1229 /* 1230 * Buffer topology 1231 */ 1232 1233 static int sof_widget_load_buffer(struct snd_soc_component *scomp, int index, 1234 struct snd_sof_widget *swidget, 1235 struct snd_soc_tplg_dapm_widget *tw, 1236 struct sof_ipc_comp_reply *r) 1237 { 1238 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1239 struct snd_soc_tplg_private *private = &tw->priv; 1240 struct sof_ipc_buffer *buffer; 1241 int ret; 1242 1243 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); 1244 if (!buffer) 1245 return -ENOMEM; 1246 1247 /* configure dai IPC message */ 1248 buffer->comp.hdr.size = sizeof(*buffer); 1249 buffer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_BUFFER_NEW; 1250 buffer->comp.id = swidget->comp_id; 1251 buffer->comp.type = SOF_COMP_BUFFER; 1252 buffer->comp.pipeline_id = index; 1253 1254 ret = sof_parse_tokens(scomp, buffer, buffer_tokens, 1255 ARRAY_SIZE(buffer_tokens), private->array, 1256 le32_to_cpu(private->size)); 1257 if (ret != 0) { 1258 dev_err(sdev->dev, "error: parse buffer tokens failed %d\n", 1259 private->size); 1260 kfree(buffer); 1261 return ret; 1262 } 1263 1264 dev_dbg(sdev->dev, "buffer %s: size %d caps 0x%x\n", 1265 swidget->widget->name, buffer->size, buffer->caps); 1266 1267 swidget->private = buffer; 1268 1269 ret = sof_ipc_tx_message(sdev->ipc, buffer->comp.hdr.cmd, buffer, 1270 sizeof(*buffer), r, sizeof(*r)); 1271 if (ret < 0) { 1272 dev_err(sdev->dev, "error: buffer %s load failed\n", 1273 swidget->widget->name); 1274 kfree(buffer); 1275 } 1276 1277 return ret; 1278 } 1279 1280 /* bind PCM ID to host component ID */ 1281 static int spcm_bind(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, 1282 int dir) 1283 { 1284 struct snd_sof_widget *host_widget; 1285 1286 host_widget = snd_sof_find_swidget_sname(sdev, 1287 spcm->pcm.caps[dir].name, 1288 dir); 1289 if (!host_widget) { 1290 dev_err(sdev->dev, "can't find host comp to bind pcm\n"); 1291 return -EINVAL; 1292 } 1293 1294 spcm->stream[dir].comp_id = host_widget->comp_id; 1295 1296 return 0; 1297 } 1298 1299 /* 1300 * PCM Topology 1301 */ 1302 1303 static int sof_widget_load_pcm(struct snd_soc_component *scomp, int index, 1304 struct snd_sof_widget *swidget, 1305 enum sof_ipc_stream_direction dir, 1306 struct snd_soc_tplg_dapm_widget *tw, 1307 struct sof_ipc_comp_reply *r) 1308 { 1309 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1310 struct snd_soc_tplg_private *private = &tw->priv; 1311 struct sof_ipc_comp_host *host; 1312 int ret; 1313 1314 host = kzalloc(sizeof(*host), GFP_KERNEL); 1315 if (!host) 1316 return -ENOMEM; 1317 1318 /* configure host comp IPC message */ 1319 host->comp.hdr.size = sizeof(*host); 1320 host->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1321 host->comp.id = swidget->comp_id; 1322 host->comp.type = SOF_COMP_HOST; 1323 host->comp.pipeline_id = index; 1324 host->direction = dir; 1325 host->config.hdr.size = sizeof(host->config); 1326 1327 ret = sof_parse_tokens(scomp, host, pcm_tokens, 1328 ARRAY_SIZE(pcm_tokens), private->array, 1329 le32_to_cpu(private->size)); 1330 if (ret != 0) { 1331 dev_err(sdev->dev, "error: parse host tokens failed %d\n", 1332 private->size); 1333 goto err; 1334 } 1335 1336 ret = sof_parse_tokens(scomp, &host->config, comp_tokens, 1337 ARRAY_SIZE(comp_tokens), private->array, 1338 le32_to_cpu(private->size)); 1339 if (ret != 0) { 1340 dev_err(sdev->dev, "error: parse host.cfg tokens failed %d\n", 1341 le32_to_cpu(private->size)); 1342 goto err; 1343 } 1344 1345 dev_dbg(sdev->dev, "loaded host %s\n", swidget->widget->name); 1346 sof_dbg_comp_config(scomp, &host->config); 1347 1348 swidget->private = host; 1349 1350 ret = sof_ipc_tx_message(sdev->ipc, host->comp.hdr.cmd, host, 1351 sizeof(*host), r, sizeof(*r)); 1352 if (ret >= 0) 1353 return ret; 1354 err: 1355 kfree(host); 1356 return ret; 1357 } 1358 1359 /* 1360 * Pipeline Topology 1361 */ 1362 int sof_load_pipeline_ipc(struct snd_sof_dev *sdev, 1363 struct sof_ipc_pipe_new *pipeline, 1364 struct sof_ipc_comp_reply *r) 1365 { 1366 struct sof_ipc_pm_core_config pm_core_config; 1367 int ret; 1368 1369 ret = sof_ipc_tx_message(sdev->ipc, pipeline->hdr.cmd, pipeline, 1370 sizeof(*pipeline), r, sizeof(*r)); 1371 if (ret < 0) { 1372 dev_err(sdev->dev, "error: load pipeline ipc failure\n"); 1373 return ret; 1374 } 1375 1376 /* power up the core that this pipeline is scheduled on */ 1377 ret = snd_sof_dsp_core_power_up(sdev, 1 << pipeline->core); 1378 if (ret < 0) { 1379 dev_err(sdev->dev, "error: powering up pipeline schedule core %d\n", 1380 pipeline->core); 1381 return ret; 1382 } 1383 1384 /* update enabled cores mask */ 1385 sdev->enabled_cores_mask |= 1 << pipeline->core; 1386 1387 /* 1388 * Now notify DSP that the core that this pipeline is scheduled on 1389 * has been powered up 1390 */ 1391 memset(&pm_core_config, 0, sizeof(pm_core_config)); 1392 pm_core_config.enable_mask = sdev->enabled_cores_mask; 1393 1394 /* configure CORE_ENABLE ipc message */ 1395 pm_core_config.hdr.size = sizeof(pm_core_config); 1396 pm_core_config.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CORE_ENABLE; 1397 1398 /* send ipc */ 1399 ret = sof_ipc_tx_message(sdev->ipc, pm_core_config.hdr.cmd, 1400 &pm_core_config, sizeof(pm_core_config), 1401 &pm_core_config, sizeof(pm_core_config)); 1402 if (ret < 0) 1403 dev_err(sdev->dev, "error: core enable ipc failure\n"); 1404 1405 return ret; 1406 } 1407 1408 static int sof_widget_load_pipeline(struct snd_soc_component *scomp, 1409 int index, struct snd_sof_widget *swidget, 1410 struct snd_soc_tplg_dapm_widget *tw, 1411 struct sof_ipc_comp_reply *r) 1412 { 1413 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1414 struct snd_soc_tplg_private *private = &tw->priv; 1415 struct sof_ipc_pipe_new *pipeline; 1416 struct snd_sof_widget *comp_swidget; 1417 int ret; 1418 1419 pipeline = kzalloc(sizeof(*pipeline), GFP_KERNEL); 1420 if (!pipeline) 1421 return -ENOMEM; 1422 1423 /* configure dai IPC message */ 1424 pipeline->hdr.size = sizeof(*pipeline); 1425 pipeline->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_NEW; 1426 pipeline->pipeline_id = index; 1427 pipeline->comp_id = swidget->comp_id; 1428 1429 /* component at start of pipeline is our stream id */ 1430 comp_swidget = snd_sof_find_swidget(sdev, tw->sname); 1431 if (!comp_swidget) { 1432 dev_err(sdev->dev, "error: widget %s refers to non existent widget %s\n", 1433 tw->name, tw->sname); 1434 ret = -EINVAL; 1435 goto err; 1436 } 1437 1438 pipeline->sched_id = comp_swidget->comp_id; 1439 1440 dev_dbg(sdev->dev, "tplg: pipeline id %d comp %d scheduling comp id %d\n", 1441 pipeline->pipeline_id, pipeline->comp_id, pipeline->sched_id); 1442 1443 ret = sof_parse_tokens(scomp, pipeline, sched_tokens, 1444 ARRAY_SIZE(sched_tokens), private->array, 1445 le32_to_cpu(private->size)); 1446 if (ret != 0) { 1447 dev_err(sdev->dev, "error: parse pipeline tokens failed %d\n", 1448 private->size); 1449 goto err; 1450 } 1451 1452 dev_dbg(sdev->dev, "pipeline %s: period %d pri %d mips %d core %d frames %d\n", 1453 swidget->widget->name, pipeline->period, pipeline->priority, 1454 pipeline->period_mips, pipeline->core, pipeline->frames_per_sched); 1455 1456 swidget->private = pipeline; 1457 1458 /* send ipc's to create pipeline comp and power up schedule core */ 1459 ret = sof_load_pipeline_ipc(sdev, pipeline, r); 1460 if (ret >= 0) 1461 return ret; 1462 err: 1463 kfree(pipeline); 1464 return ret; 1465 } 1466 1467 /* 1468 * Mixer topology 1469 */ 1470 1471 static int sof_widget_load_mixer(struct snd_soc_component *scomp, int index, 1472 struct snd_sof_widget *swidget, 1473 struct snd_soc_tplg_dapm_widget *tw, 1474 struct sof_ipc_comp_reply *r) 1475 { 1476 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1477 struct snd_soc_tplg_private *private = &tw->priv; 1478 struct sof_ipc_comp_mixer *mixer; 1479 int ret; 1480 1481 mixer = kzalloc(sizeof(*mixer), GFP_KERNEL); 1482 if (!mixer) 1483 return -ENOMEM; 1484 1485 /* configure mixer IPC message */ 1486 mixer->comp.hdr.size = sizeof(*mixer); 1487 mixer->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1488 mixer->comp.id = swidget->comp_id; 1489 mixer->comp.type = SOF_COMP_MIXER; 1490 mixer->comp.pipeline_id = index; 1491 mixer->config.hdr.size = sizeof(mixer->config); 1492 1493 ret = sof_parse_tokens(scomp, &mixer->config, comp_tokens, 1494 ARRAY_SIZE(comp_tokens), private->array, 1495 le32_to_cpu(private->size)); 1496 if (ret != 0) { 1497 dev_err(sdev->dev, "error: parse mixer.cfg tokens failed %d\n", 1498 private->size); 1499 kfree(mixer); 1500 return ret; 1501 } 1502 1503 sof_dbg_comp_config(scomp, &mixer->config); 1504 1505 swidget->private = mixer; 1506 1507 ret = sof_ipc_tx_message(sdev->ipc, mixer->comp.hdr.cmd, mixer, 1508 sizeof(*mixer), r, sizeof(*r)); 1509 if (ret < 0) 1510 kfree(mixer); 1511 1512 return ret; 1513 } 1514 1515 /* 1516 * Mux topology 1517 */ 1518 static int sof_widget_load_mux(struct snd_soc_component *scomp, int index, 1519 struct snd_sof_widget *swidget, 1520 struct snd_soc_tplg_dapm_widget *tw, 1521 struct sof_ipc_comp_reply *r) 1522 { 1523 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1524 struct snd_soc_tplg_private *private = &tw->priv; 1525 struct sof_ipc_comp_mux *mux; 1526 int ret; 1527 1528 mux = kzalloc(sizeof(*mux), GFP_KERNEL); 1529 if (!mux) 1530 return -ENOMEM; 1531 1532 /* configure mux IPC message */ 1533 mux->comp.hdr.size = sizeof(*mux); 1534 mux->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1535 mux->comp.id = swidget->comp_id; 1536 mux->comp.type = SOF_COMP_MUX; 1537 mux->comp.pipeline_id = index; 1538 mux->config.hdr.size = sizeof(mux->config); 1539 1540 ret = sof_parse_tokens(scomp, &mux->config, comp_tokens, 1541 ARRAY_SIZE(comp_tokens), private->array, 1542 le32_to_cpu(private->size)); 1543 if (ret != 0) { 1544 dev_err(sdev->dev, "error: parse mux.cfg tokens failed %d\n", 1545 private->size); 1546 kfree(mux); 1547 return ret; 1548 } 1549 1550 sof_dbg_comp_config(scomp, &mux->config); 1551 1552 swidget->private = mux; 1553 1554 ret = sof_ipc_tx_message(sdev->ipc, mux->comp.hdr.cmd, mux, 1555 sizeof(*mux), r, sizeof(*r)); 1556 if (ret < 0) 1557 kfree(mux); 1558 1559 return ret; 1560 } 1561 1562 /* 1563 * PGA Topology 1564 */ 1565 1566 static int sof_widget_load_pga(struct snd_soc_component *scomp, int index, 1567 struct snd_sof_widget *swidget, 1568 struct snd_soc_tplg_dapm_widget *tw, 1569 struct sof_ipc_comp_reply *r) 1570 { 1571 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1572 struct snd_soc_tplg_private *private = &tw->priv; 1573 struct sof_ipc_comp_volume *volume; 1574 struct snd_sof_control *scontrol; 1575 int min_step; 1576 int max_step; 1577 int ret; 1578 1579 volume = kzalloc(sizeof(*volume), GFP_KERNEL); 1580 if (!volume) 1581 return -ENOMEM; 1582 1583 if (le32_to_cpu(tw->num_kcontrols) != 1) { 1584 dev_err(sdev->dev, "error: invalid kcontrol count %d for volume\n", 1585 tw->num_kcontrols); 1586 ret = -EINVAL; 1587 goto err; 1588 } 1589 1590 /* configure volume IPC message */ 1591 volume->comp.hdr.size = sizeof(*volume); 1592 volume->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1593 volume->comp.id = swidget->comp_id; 1594 volume->comp.type = SOF_COMP_VOLUME; 1595 volume->comp.pipeline_id = index; 1596 volume->config.hdr.size = sizeof(volume->config); 1597 1598 ret = sof_parse_tokens(scomp, volume, volume_tokens, 1599 ARRAY_SIZE(volume_tokens), private->array, 1600 le32_to_cpu(private->size)); 1601 if (ret != 0) { 1602 dev_err(sdev->dev, "error: parse volume tokens failed %d\n", 1603 private->size); 1604 goto err; 1605 } 1606 ret = sof_parse_tokens(scomp, &volume->config, comp_tokens, 1607 ARRAY_SIZE(comp_tokens), private->array, 1608 le32_to_cpu(private->size)); 1609 if (ret != 0) { 1610 dev_err(sdev->dev, "error: parse volume.cfg tokens failed %d\n", 1611 le32_to_cpu(private->size)); 1612 goto err; 1613 } 1614 1615 sof_dbg_comp_config(scomp, &volume->config); 1616 1617 swidget->private = volume; 1618 1619 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 1620 if (scontrol->comp_id == swidget->comp_id) { 1621 min_step = scontrol->min_volume_step; 1622 max_step = scontrol->max_volume_step; 1623 volume->min_value = scontrol->volume_table[min_step]; 1624 volume->max_value = scontrol->volume_table[max_step]; 1625 volume->channels = scontrol->num_channels; 1626 break; 1627 } 1628 } 1629 1630 ret = sof_ipc_tx_message(sdev->ipc, volume->comp.hdr.cmd, volume, 1631 sizeof(*volume), r, sizeof(*r)); 1632 if (ret >= 0) 1633 return ret; 1634 err: 1635 kfree(volume); 1636 return ret; 1637 } 1638 1639 /* 1640 * SRC Topology 1641 */ 1642 1643 static int sof_widget_load_src(struct snd_soc_component *scomp, int index, 1644 struct snd_sof_widget *swidget, 1645 struct snd_soc_tplg_dapm_widget *tw, 1646 struct sof_ipc_comp_reply *r) 1647 { 1648 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1649 struct snd_soc_tplg_private *private = &tw->priv; 1650 struct sof_ipc_comp_src *src; 1651 int ret; 1652 1653 src = kzalloc(sizeof(*src), GFP_KERNEL); 1654 if (!src) 1655 return -ENOMEM; 1656 1657 /* configure src IPC message */ 1658 src->comp.hdr.size = sizeof(*src); 1659 src->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1660 src->comp.id = swidget->comp_id; 1661 src->comp.type = SOF_COMP_SRC; 1662 src->comp.pipeline_id = index; 1663 src->config.hdr.size = sizeof(src->config); 1664 1665 ret = sof_parse_tokens(scomp, src, src_tokens, 1666 ARRAY_SIZE(src_tokens), private->array, 1667 le32_to_cpu(private->size)); 1668 if (ret != 0) { 1669 dev_err(sdev->dev, "error: parse src tokens failed %d\n", 1670 private->size); 1671 goto err; 1672 } 1673 1674 ret = sof_parse_tokens(scomp, &src->config, comp_tokens, 1675 ARRAY_SIZE(comp_tokens), private->array, 1676 le32_to_cpu(private->size)); 1677 if (ret != 0) { 1678 dev_err(sdev->dev, "error: parse src.cfg tokens failed %d\n", 1679 le32_to_cpu(private->size)); 1680 goto err; 1681 } 1682 1683 dev_dbg(sdev->dev, "src %s: source rate %d sink rate %d\n", 1684 swidget->widget->name, src->source_rate, src->sink_rate); 1685 sof_dbg_comp_config(scomp, &src->config); 1686 1687 swidget->private = src; 1688 1689 ret = sof_ipc_tx_message(sdev->ipc, src->comp.hdr.cmd, src, 1690 sizeof(*src), r, sizeof(*r)); 1691 if (ret >= 0) 1692 return ret; 1693 err: 1694 kfree(src); 1695 return ret; 1696 } 1697 1698 /* 1699 * Signal Generator Topology 1700 */ 1701 1702 static int sof_widget_load_siggen(struct snd_soc_component *scomp, int index, 1703 struct snd_sof_widget *swidget, 1704 struct snd_soc_tplg_dapm_widget *tw, 1705 struct sof_ipc_comp_reply *r) 1706 { 1707 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1708 struct snd_soc_tplg_private *private = &tw->priv; 1709 struct sof_ipc_comp_tone *tone; 1710 int ret; 1711 1712 tone = kzalloc(sizeof(*tone), GFP_KERNEL); 1713 if (!tone) 1714 return -ENOMEM; 1715 1716 /* configure siggen IPC message */ 1717 tone->comp.hdr.size = sizeof(*tone); 1718 tone->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1719 tone->comp.id = swidget->comp_id; 1720 tone->comp.type = SOF_COMP_TONE; 1721 tone->comp.pipeline_id = index; 1722 tone->config.hdr.size = sizeof(tone->config); 1723 1724 ret = sof_parse_tokens(scomp, tone, tone_tokens, 1725 ARRAY_SIZE(tone_tokens), private->array, 1726 le32_to_cpu(private->size)); 1727 if (ret != 0) { 1728 dev_err(sdev->dev, "error: parse tone tokens failed %d\n", 1729 le32_to_cpu(private->size)); 1730 goto err; 1731 } 1732 1733 ret = sof_parse_tokens(scomp, &tone->config, comp_tokens, 1734 ARRAY_SIZE(comp_tokens), private->array, 1735 le32_to_cpu(private->size)); 1736 if (ret != 0) { 1737 dev_err(sdev->dev, "error: parse tone.cfg tokens failed %d\n", 1738 le32_to_cpu(private->size)); 1739 goto err; 1740 } 1741 1742 dev_dbg(sdev->dev, "tone %s: frequency %d amplitude %d\n", 1743 swidget->widget->name, tone->frequency, tone->amplitude); 1744 sof_dbg_comp_config(scomp, &tone->config); 1745 1746 swidget->private = tone; 1747 1748 ret = sof_ipc_tx_message(sdev->ipc, tone->comp.hdr.cmd, tone, 1749 sizeof(*tone), r, sizeof(*r)); 1750 if (ret >= 0) 1751 return ret; 1752 err: 1753 kfree(tone); 1754 return ret; 1755 } 1756 1757 static int sof_get_control_data(struct snd_sof_dev *sdev, 1758 struct snd_soc_dapm_widget *widget, 1759 struct sof_widget_data *wdata, 1760 size_t *size) 1761 { 1762 const struct snd_kcontrol_new *kc; 1763 struct soc_mixer_control *sm; 1764 struct soc_bytes_ext *sbe; 1765 struct soc_enum *se; 1766 int i; 1767 1768 *size = 0; 1769 1770 for (i = 0; i < widget->num_kcontrols; i++) { 1771 kc = &widget->kcontrol_news[i]; 1772 1773 switch (widget->dobj.widget.kcontrol_type) { 1774 case SND_SOC_TPLG_TYPE_MIXER: 1775 sm = (struct soc_mixer_control *)kc->private_value; 1776 wdata[i].control = sm->dobj.private; 1777 break; 1778 case SND_SOC_TPLG_TYPE_BYTES: 1779 sbe = (struct soc_bytes_ext *)kc->private_value; 1780 wdata[i].control = sbe->dobj.private; 1781 break; 1782 case SND_SOC_TPLG_TYPE_ENUM: 1783 se = (struct soc_enum *)kc->private_value; 1784 wdata[i].control = se->dobj.private; 1785 break; 1786 default: 1787 dev_err(sdev->dev, "error: unknown kcontrol type %d in widget %s\n", 1788 widget->dobj.widget.kcontrol_type, 1789 widget->name); 1790 return -EINVAL; 1791 } 1792 1793 if (!wdata[i].control) { 1794 dev_err(sdev->dev, "error: no scontrol for widget %s\n", 1795 widget->name); 1796 return -EINVAL; 1797 } 1798 1799 wdata[i].pdata = wdata[i].control->control_data->data; 1800 if (!wdata[i].pdata) 1801 return -EINVAL; 1802 1803 /* make sure data is valid - data can be updated at runtime */ 1804 if (wdata[i].pdata->magic != SOF_ABI_MAGIC) 1805 return -EINVAL; 1806 1807 *size += wdata[i].pdata->size; 1808 1809 /* get data type */ 1810 switch (wdata[i].control->cmd) { 1811 case SOF_CTRL_CMD_VOLUME: 1812 case SOF_CTRL_CMD_ENUM: 1813 case SOF_CTRL_CMD_SWITCH: 1814 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_VALUE; 1815 wdata[i].ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET; 1816 break; 1817 case SOF_CTRL_CMD_BINARY: 1818 wdata[i].ipc_cmd = SOF_IPC_COMP_SET_DATA; 1819 wdata[i].ctrl_type = SOF_CTRL_TYPE_DATA_SET; 1820 break; 1821 default: 1822 break; 1823 } 1824 } 1825 1826 return 0; 1827 } 1828 1829 static int sof_process_load(struct snd_soc_component *scomp, int index, 1830 struct snd_sof_widget *swidget, 1831 struct snd_soc_tplg_dapm_widget *tw, 1832 struct sof_ipc_comp_reply *r, 1833 int type) 1834 { 1835 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1836 struct snd_soc_dapm_widget *widget = swidget->widget; 1837 struct snd_soc_tplg_private *private = &tw->priv; 1838 struct sof_ipc_comp_process *process = NULL; 1839 struct sof_widget_data *wdata = NULL; 1840 size_t ipc_data_size = 0; 1841 size_t ipc_size; 1842 int offset = 0; 1843 int ret = 0; 1844 int i; 1845 1846 if (type == SOF_COMP_NONE) { 1847 dev_err(sdev->dev, "error: invalid process comp type %d\n", 1848 type); 1849 return -EINVAL; 1850 } 1851 1852 /* allocate struct for widget control data sizes and types */ 1853 if (widget->num_kcontrols) { 1854 wdata = kcalloc(widget->num_kcontrols, 1855 sizeof(*wdata), 1856 GFP_KERNEL); 1857 1858 if (!wdata) 1859 return -ENOMEM; 1860 1861 /* get possible component controls and get size of all pdata */ 1862 ret = sof_get_control_data(sdev, widget, wdata, 1863 &ipc_data_size); 1864 1865 if (ret < 0) 1866 goto out; 1867 } 1868 1869 ipc_size = sizeof(struct sof_ipc_comp_process) + 1870 le32_to_cpu(private->size) + 1871 ipc_data_size; 1872 1873 /* we are exceeding max ipc size, config needs to be sent separately */ 1874 if (ipc_size > SOF_IPC_MSG_MAX_SIZE) { 1875 ipc_size -= ipc_data_size; 1876 ipc_data_size = 0; 1877 } 1878 1879 process = kzalloc(ipc_size, GFP_KERNEL); 1880 if (!process) { 1881 ret = -ENOMEM; 1882 goto out; 1883 } 1884 1885 /* configure iir IPC message */ 1886 process->comp.hdr.size = ipc_size; 1887 process->comp.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_NEW; 1888 process->comp.id = swidget->comp_id; 1889 process->comp.type = type; 1890 process->comp.pipeline_id = index; 1891 process->config.hdr.size = sizeof(process->config); 1892 1893 ret = sof_parse_tokens(scomp, &process->config, comp_tokens, 1894 ARRAY_SIZE(comp_tokens), private->array, 1895 le32_to_cpu(private->size)); 1896 if (ret != 0) { 1897 dev_err(sdev->dev, "error: parse process.cfg tokens failed %d\n", 1898 le32_to_cpu(private->size)); 1899 goto err; 1900 } 1901 1902 sof_dbg_comp_config(scomp, &process->config); 1903 1904 /* 1905 * found private data in control, so copy it. 1906 * get possible component controls - get size of all pdata, 1907 * then memcpy with headers 1908 */ 1909 if (ipc_data_size) { 1910 for (i = 0; i < widget->num_kcontrols; i++) { 1911 memcpy(&process->data + offset, 1912 wdata[i].pdata->data, 1913 wdata[i].pdata->size); 1914 offset += wdata[i].pdata->size; 1915 } 1916 } 1917 1918 process->size = ipc_data_size; 1919 swidget->private = process; 1920 1921 ret = sof_ipc_tx_message(sdev->ipc, process->comp.hdr.cmd, process, 1922 ipc_size, r, sizeof(*r)); 1923 1924 if (ret < 0) { 1925 dev_err(sdev->dev, "error: create process failed\n"); 1926 goto err; 1927 } 1928 1929 /* we sent the data in single message so return */ 1930 if (ipc_data_size) 1931 goto out; 1932 1933 /* send control data with large message supported method */ 1934 for (i = 0; i < widget->num_kcontrols; i++) { 1935 wdata[i].control->readback_offset = 0; 1936 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, wdata[i].control, 1937 wdata[i].ipc_cmd, 1938 wdata[i].ctrl_type, 1939 wdata[i].control->cmd, 1940 true); 1941 if (ret != 0) { 1942 dev_err(sdev->dev, "error: send control failed\n"); 1943 break; 1944 } 1945 } 1946 1947 err: 1948 if (ret < 0) 1949 kfree(process); 1950 out: 1951 kfree(wdata); 1952 return ret; 1953 } 1954 1955 /* 1956 * Processing Component Topology - can be "effect", "codec", or general 1957 * "processing". 1958 */ 1959 1960 static int sof_widget_load_process(struct snd_soc_component *scomp, int index, 1961 struct snd_sof_widget *swidget, 1962 struct snd_soc_tplg_dapm_widget *tw, 1963 struct sof_ipc_comp_reply *r) 1964 { 1965 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 1966 struct snd_soc_tplg_private *private = &tw->priv; 1967 struct sof_ipc_comp_process config; 1968 int ret; 1969 1970 /* check we have some tokens - we need at least process type */ 1971 if (le32_to_cpu(private->size) == 0) { 1972 dev_err(sdev->dev, "error: process tokens not found\n"); 1973 return -EINVAL; 1974 } 1975 1976 memset(&config, 0, sizeof(config)); 1977 1978 /* get the process token */ 1979 ret = sof_parse_tokens(scomp, &config, process_tokens, 1980 ARRAY_SIZE(process_tokens), private->array, 1981 le32_to_cpu(private->size)); 1982 if (ret != 0) { 1983 dev_err(sdev->dev, "error: parse process tokens failed %d\n", 1984 le32_to_cpu(private->size)); 1985 return ret; 1986 } 1987 1988 /* now load process specific data and send IPC */ 1989 ret = sof_process_load(scomp, index, swidget, tw, r, 1990 find_process_comp_type(config.type)); 1991 if (ret < 0) { 1992 dev_err(sdev->dev, "error: process loading failed\n"); 1993 return ret; 1994 } 1995 1996 return 0; 1997 } 1998 1999 static int sof_widget_bind_event(struct snd_sof_dev *sdev, 2000 struct snd_sof_widget *swidget, 2001 u16 event_type) 2002 { 2003 struct sof_ipc_comp *ipc_comp; 2004 2005 /* validate widget event type */ 2006 switch (event_type) { 2007 case SOF_KEYWORD_DETECT_DAPM_EVENT: 2008 /* only KEYWORD_DETECT comps should handle this */ 2009 if (swidget->id != snd_soc_dapm_effect) 2010 break; 2011 2012 ipc_comp = swidget->private; 2013 if (ipc_comp && ipc_comp->type != SOF_COMP_KEYWORD_DETECT) 2014 break; 2015 2016 /* bind event to keyword detect comp */ 2017 return snd_soc_tplg_widget_bind_event(swidget->widget, 2018 sof_kwd_events, 2019 ARRAY_SIZE(sof_kwd_events), 2020 event_type); 2021 default: 2022 break; 2023 } 2024 2025 dev_err(sdev->dev, 2026 "error: invalid event type %d for widget %s\n", 2027 event_type, swidget->widget->name); 2028 return -EINVAL; 2029 } 2030 2031 /* external widget init - used for any driver specific init */ 2032 static int sof_widget_ready(struct snd_soc_component *scomp, int index, 2033 struct snd_soc_dapm_widget *w, 2034 struct snd_soc_tplg_dapm_widget *tw) 2035 { 2036 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2037 struct snd_sof_widget *swidget; 2038 struct snd_sof_dai *dai; 2039 struct sof_ipc_comp_reply reply; 2040 struct snd_sof_control *scontrol; 2041 int ret = 0; 2042 2043 swidget = kzalloc(sizeof(*swidget), GFP_KERNEL); 2044 if (!swidget) 2045 return -ENOMEM; 2046 2047 swidget->sdev = sdev; 2048 swidget->widget = w; 2049 swidget->comp_id = sdev->next_comp_id++; 2050 swidget->complete = 0; 2051 swidget->id = w->id; 2052 swidget->pipeline_id = index; 2053 swidget->private = NULL; 2054 memset(&reply, 0, sizeof(reply)); 2055 2056 dev_dbg(sdev->dev, "tplg: ready widget id %d pipe %d type %d name : %s stream %s\n", 2057 swidget->comp_id, index, swidget->id, tw->name, 2058 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 2059 ? tw->sname : "none"); 2060 2061 /* handle any special case widgets */ 2062 switch (w->id) { 2063 case snd_soc_dapm_dai_in: 2064 case snd_soc_dapm_dai_out: 2065 dai = kzalloc(sizeof(*dai), GFP_KERNEL); 2066 if (!dai) { 2067 kfree(swidget); 2068 return -ENOMEM; 2069 } 2070 2071 ret = sof_widget_load_dai(scomp, index, swidget, tw, &reply, 2072 dai); 2073 if (ret == 0) { 2074 sof_connect_dai_widget(scomp, w, tw, dai); 2075 list_add(&dai->list, &sdev->dai_list); 2076 swidget->private = dai; 2077 } else { 2078 kfree(dai); 2079 } 2080 break; 2081 case snd_soc_dapm_mixer: 2082 ret = sof_widget_load_mixer(scomp, index, swidget, tw, &reply); 2083 break; 2084 case snd_soc_dapm_pga: 2085 ret = sof_widget_load_pga(scomp, index, swidget, tw, &reply); 2086 /* Find scontrol for this pga and set readback offset*/ 2087 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 2088 if (scontrol->comp_id == swidget->comp_id) { 2089 scontrol->readback_offset = reply.offset; 2090 break; 2091 } 2092 } 2093 break; 2094 case snd_soc_dapm_buffer: 2095 ret = sof_widget_load_buffer(scomp, index, swidget, tw, &reply); 2096 break; 2097 case snd_soc_dapm_scheduler: 2098 ret = sof_widget_load_pipeline(scomp, index, swidget, tw, 2099 &reply); 2100 break; 2101 case snd_soc_dapm_aif_out: 2102 ret = sof_widget_load_pcm(scomp, index, swidget, 2103 SOF_IPC_STREAM_CAPTURE, tw, &reply); 2104 break; 2105 case snd_soc_dapm_aif_in: 2106 ret = sof_widget_load_pcm(scomp, index, swidget, 2107 SOF_IPC_STREAM_PLAYBACK, tw, &reply); 2108 break; 2109 case snd_soc_dapm_src: 2110 ret = sof_widget_load_src(scomp, index, swidget, tw, &reply); 2111 break; 2112 case snd_soc_dapm_siggen: 2113 ret = sof_widget_load_siggen(scomp, index, swidget, tw, &reply); 2114 break; 2115 case snd_soc_dapm_effect: 2116 ret = sof_widget_load_process(scomp, index, swidget, tw, 2117 &reply); 2118 break; 2119 case snd_soc_dapm_mux: 2120 case snd_soc_dapm_demux: 2121 ret = sof_widget_load_mux(scomp, index, swidget, tw, &reply); 2122 break; 2123 case snd_soc_dapm_switch: 2124 case snd_soc_dapm_dai_link: 2125 case snd_soc_dapm_kcontrol: 2126 default: 2127 dev_warn(sdev->dev, "warning: widget type %d name %s not handled\n", 2128 swidget->id, tw->name); 2129 break; 2130 } 2131 2132 /* check IPC reply */ 2133 if (ret < 0 || reply.rhdr.error < 0) { 2134 dev_err(sdev->dev, 2135 "error: DSP failed to add widget id %d type %d name : %s stream %s reply %d\n", 2136 tw->shift, swidget->id, tw->name, 2137 strnlen(tw->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) > 0 2138 ? tw->sname : "none", reply.rhdr.error); 2139 kfree(swidget); 2140 return ret; 2141 } 2142 2143 /* bind widget to external event */ 2144 if (tw->event_type) { 2145 ret = sof_widget_bind_event(sdev, swidget, 2146 le16_to_cpu(tw->event_type)); 2147 if (ret) { 2148 dev_err(sdev->dev, "error: widget event binding failed\n"); 2149 kfree(swidget->private); 2150 kfree(swidget); 2151 return ret; 2152 } 2153 } 2154 2155 w->dobj.private = swidget; 2156 list_add(&swidget->list, &sdev->widget_list); 2157 return ret; 2158 } 2159 2160 static int sof_route_unload(struct snd_soc_component *scomp, 2161 struct snd_soc_dobj *dobj) 2162 { 2163 struct snd_sof_route *sroute; 2164 2165 sroute = dobj->private; 2166 if (!sroute) 2167 return 0; 2168 2169 /* free sroute and its private data */ 2170 kfree(sroute->private); 2171 list_del(&sroute->list); 2172 kfree(sroute); 2173 2174 return 0; 2175 } 2176 2177 static int sof_widget_unload(struct snd_soc_component *scomp, 2178 struct snd_soc_dobj *dobj) 2179 { 2180 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2181 const struct snd_kcontrol_new *kc; 2182 struct snd_soc_dapm_widget *widget; 2183 struct sof_ipc_pipe_new *pipeline; 2184 struct snd_sof_control *scontrol; 2185 struct snd_sof_widget *swidget; 2186 struct soc_mixer_control *sm; 2187 struct soc_bytes_ext *sbe; 2188 struct snd_sof_dai *dai; 2189 struct soc_enum *se; 2190 int ret = 0; 2191 int i; 2192 2193 swidget = dobj->private; 2194 if (!swidget) 2195 return 0; 2196 2197 widget = swidget->widget; 2198 2199 switch (swidget->id) { 2200 case snd_soc_dapm_dai_in: 2201 case snd_soc_dapm_dai_out: 2202 dai = swidget->private; 2203 2204 if (dai) { 2205 /* free dai config */ 2206 kfree(dai->dai_config); 2207 list_del(&dai->list); 2208 } 2209 break; 2210 case snd_soc_dapm_scheduler: 2211 2212 /* power down the pipeline schedule core */ 2213 pipeline = swidget->private; 2214 ret = snd_sof_dsp_core_power_down(sdev, 1 << pipeline->core); 2215 if (ret < 0) 2216 dev_err(sdev->dev, "error: powering down pipeline schedule core %d\n", 2217 pipeline->core); 2218 2219 /* update enabled cores mask */ 2220 sdev->enabled_cores_mask &= ~(1 << pipeline->core); 2221 2222 break; 2223 default: 2224 break; 2225 } 2226 for (i = 0; i < widget->num_kcontrols; i++) { 2227 kc = &widget->kcontrol_news[i]; 2228 switch (dobj->widget.kcontrol_type) { 2229 case SND_SOC_TPLG_TYPE_MIXER: 2230 sm = (struct soc_mixer_control *)kc->private_value; 2231 scontrol = sm->dobj.private; 2232 if (sm->max > 1) 2233 kfree(scontrol->volume_table); 2234 break; 2235 case SND_SOC_TPLG_TYPE_ENUM: 2236 se = (struct soc_enum *)kc->private_value; 2237 scontrol = se->dobj.private; 2238 break; 2239 case SND_SOC_TPLG_TYPE_BYTES: 2240 sbe = (struct soc_bytes_ext *)kc->private_value; 2241 scontrol = sbe->dobj.private; 2242 break; 2243 default: 2244 dev_warn(sdev->dev, "unsupported kcontrol_type\n"); 2245 goto out; 2246 } 2247 kfree(scontrol->control_data); 2248 list_del(&scontrol->list); 2249 kfree(scontrol); 2250 } 2251 2252 out: 2253 /* free private value */ 2254 kfree(swidget->private); 2255 2256 /* remove and free swidget object */ 2257 list_del(&swidget->list); 2258 kfree(swidget); 2259 2260 return ret; 2261 } 2262 2263 /* 2264 * DAI HW configuration. 2265 */ 2266 2267 /* FE DAI - used for any driver specific init */ 2268 static int sof_dai_load(struct snd_soc_component *scomp, int index, 2269 struct snd_soc_dai_driver *dai_drv, 2270 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 2271 { 2272 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2273 struct snd_soc_tplg_stream_caps *caps; 2274 struct snd_sof_pcm *spcm; 2275 int stream = SNDRV_PCM_STREAM_PLAYBACK; 2276 int ret = 0; 2277 2278 /* nothing to do for BEs atm */ 2279 if (!pcm) 2280 return 0; 2281 2282 spcm = kzalloc(sizeof(*spcm), GFP_KERNEL); 2283 if (!spcm) 2284 return -ENOMEM; 2285 2286 spcm->sdev = sdev; 2287 spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].comp_id = COMP_ID_UNASSIGNED; 2288 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].comp_id = COMP_ID_UNASSIGNED; 2289 2290 if (pcm) { 2291 spcm->pcm = *pcm; 2292 dev_dbg(sdev->dev, "tplg: load pcm %s\n", pcm->dai_name); 2293 } 2294 dai_drv->dobj.private = spcm; 2295 list_add(&spcm->list, &sdev->pcm_list); 2296 2297 /* do we need to allocate playback PCM DMA pages */ 2298 if (!spcm->pcm.playback) 2299 goto capture; 2300 2301 caps = &spcm->pcm.caps[stream]; 2302 2303 /* allocate playback page table buffer */ 2304 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 2305 PAGE_SIZE, &spcm->stream[stream].page_table); 2306 if (ret < 0) { 2307 dev_err(sdev->dev, "error: can't alloc page table for %s %d\n", 2308 caps->name, ret); 2309 2310 return ret; 2311 } 2312 2313 /* bind pcm to host comp */ 2314 ret = spcm_bind(sdev, spcm, stream); 2315 if (ret) { 2316 dev_err(sdev->dev, 2317 "error: can't bind pcm to host\n"); 2318 goto free_playback_tables; 2319 } 2320 2321 capture: 2322 stream = SNDRV_PCM_STREAM_CAPTURE; 2323 2324 /* do we need to allocate capture PCM DMA pages */ 2325 if (!spcm->pcm.capture) 2326 return ret; 2327 2328 caps = &spcm->pcm.caps[stream]; 2329 2330 /* allocate capture page table buffer */ 2331 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 2332 PAGE_SIZE, &spcm->stream[stream].page_table); 2333 if (ret < 0) { 2334 dev_err(sdev->dev, "error: can't alloc page table for %s %d\n", 2335 caps->name, ret); 2336 goto free_playback_tables; 2337 } 2338 2339 /* bind pcm to host comp */ 2340 ret = spcm_bind(sdev, spcm, stream); 2341 if (ret) { 2342 dev_err(sdev->dev, 2343 "error: can't bind pcm to host\n"); 2344 snd_dma_free_pages(&spcm->stream[stream].page_table); 2345 goto free_playback_tables; 2346 } 2347 2348 return ret; 2349 2350 free_playback_tables: 2351 if (spcm->pcm.playback) 2352 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 2353 2354 return ret; 2355 } 2356 2357 static int sof_dai_unload(struct snd_soc_component *scomp, 2358 struct snd_soc_dobj *dobj) 2359 { 2360 struct snd_sof_pcm *spcm = dobj->private; 2361 2362 /* free PCM DMA pages */ 2363 if (spcm->pcm.playback) 2364 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table); 2365 2366 if (spcm->pcm.capture) 2367 snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table); 2368 2369 /* remove from list and free spcm */ 2370 list_del(&spcm->list); 2371 kfree(spcm); 2372 2373 return 0; 2374 } 2375 2376 static void sof_dai_set_format(struct snd_soc_tplg_hw_config *hw_config, 2377 struct sof_ipc_dai_config *config) 2378 { 2379 /* clock directions wrt codec */ 2380 if (hw_config->bclk_master == SND_SOC_TPLG_BCLK_CM) { 2381 /* codec is bclk master */ 2382 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM) 2383 config->format |= SOF_DAI_FMT_CBM_CFM; 2384 else 2385 config->format |= SOF_DAI_FMT_CBM_CFS; 2386 } else { 2387 /* codec is bclk slave */ 2388 if (hw_config->fsync_master == SND_SOC_TPLG_FSYNC_CM) 2389 config->format |= SOF_DAI_FMT_CBS_CFM; 2390 else 2391 config->format |= SOF_DAI_FMT_CBS_CFS; 2392 } 2393 2394 /* inverted clocks ? */ 2395 if (hw_config->invert_bclk) { 2396 if (hw_config->invert_fsync) 2397 config->format |= SOF_DAI_FMT_IB_IF; 2398 else 2399 config->format |= SOF_DAI_FMT_IB_NF; 2400 } else { 2401 if (hw_config->invert_fsync) 2402 config->format |= SOF_DAI_FMT_NB_IF; 2403 else 2404 config->format |= SOF_DAI_FMT_NB_NF; 2405 } 2406 } 2407 2408 /* set config for all DAI's with name matching the link name */ 2409 static int sof_set_dai_config(struct snd_sof_dev *sdev, u32 size, 2410 struct snd_soc_dai_link *link, 2411 struct sof_ipc_dai_config *config) 2412 { 2413 struct snd_sof_dai *dai; 2414 int found = 0; 2415 2416 list_for_each_entry(dai, &sdev->dai_list, list) { 2417 if (!dai->name) 2418 continue; 2419 2420 if (strcmp(link->name, dai->name) == 0) { 2421 dai->dai_config = kmemdup(config, size, GFP_KERNEL); 2422 if (!dai->dai_config) 2423 return -ENOMEM; 2424 2425 /* set cpu_dai_name */ 2426 dai->cpu_dai_name = link->cpus->dai_name; 2427 2428 found = 1; 2429 } 2430 } 2431 2432 /* 2433 * machine driver may define a dai link with playback and capture 2434 * dai enabled, but the dai link in topology would support both, one 2435 * or none of them. Here print a warning message to notify user 2436 */ 2437 if (!found) { 2438 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s", 2439 link->name); 2440 } 2441 2442 return 0; 2443 } 2444 2445 static int sof_link_ssp_load(struct snd_soc_component *scomp, int index, 2446 struct snd_soc_dai_link *link, 2447 struct snd_soc_tplg_link_config *cfg, 2448 struct snd_soc_tplg_hw_config *hw_config, 2449 struct sof_ipc_dai_config *config) 2450 { 2451 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2452 struct snd_soc_tplg_private *private = &cfg->priv; 2453 struct sof_ipc_reply reply; 2454 u32 size = sizeof(*config); 2455 int ret; 2456 2457 /* handle master/slave and inverted clocks */ 2458 sof_dai_set_format(hw_config, config); 2459 2460 /* init IPC */ 2461 memset(&config->ssp, 0, sizeof(struct sof_ipc_dai_ssp_params)); 2462 config->hdr.size = size; 2463 2464 ret = sof_parse_tokens(scomp, &config->ssp, ssp_tokens, 2465 ARRAY_SIZE(ssp_tokens), private->array, 2466 le32_to_cpu(private->size)); 2467 if (ret != 0) { 2468 dev_err(sdev->dev, "error: parse ssp tokens failed %d\n", 2469 le32_to_cpu(private->size)); 2470 return ret; 2471 } 2472 2473 config->ssp.mclk_rate = le32_to_cpu(hw_config->mclk_rate); 2474 config->ssp.bclk_rate = le32_to_cpu(hw_config->bclk_rate); 2475 config->ssp.fsync_rate = le32_to_cpu(hw_config->fsync_rate); 2476 config->ssp.tdm_slots = le32_to_cpu(hw_config->tdm_slots); 2477 config->ssp.tdm_slot_width = le32_to_cpu(hw_config->tdm_slot_width); 2478 config->ssp.mclk_direction = hw_config->mclk_direction; 2479 config->ssp.rx_slots = le32_to_cpu(hw_config->rx_slots); 2480 config->ssp.tx_slots = le32_to_cpu(hw_config->tx_slots); 2481 2482 dev_dbg(sdev->dev, "tplg: config SSP%d fmt 0x%x mclk %d bclk %d fclk %d width (%d)%d slots %d mclk id %d quirks %d\n", 2483 config->dai_index, config->format, 2484 config->ssp.mclk_rate, config->ssp.bclk_rate, 2485 config->ssp.fsync_rate, config->ssp.sample_valid_bits, 2486 config->ssp.tdm_slot_width, config->ssp.tdm_slots, 2487 config->ssp.mclk_id, config->ssp.quirks); 2488 2489 /* validate SSP fsync rate and channel count */ 2490 if (config->ssp.fsync_rate < 8000 || config->ssp.fsync_rate > 192000) { 2491 dev_err(sdev->dev, "error: invalid fsync rate for SSP%d\n", 2492 config->dai_index); 2493 return -EINVAL; 2494 } 2495 2496 if (config->ssp.tdm_slots < 1 || config->ssp.tdm_slots > 8) { 2497 dev_err(sdev->dev, "error: invalid channel count for SSP%d\n", 2498 config->dai_index); 2499 return -EINVAL; 2500 } 2501 2502 /* send message to DSP */ 2503 ret = sof_ipc_tx_message(sdev->ipc, 2504 config->hdr.cmd, config, size, &reply, 2505 sizeof(reply)); 2506 2507 if (ret < 0) { 2508 dev_err(sdev->dev, "error: failed to set DAI config for SSP%d\n", 2509 config->dai_index); 2510 return ret; 2511 } 2512 2513 /* set config for all DAI's with name matching the link name */ 2514 ret = sof_set_dai_config(sdev, size, link, config); 2515 if (ret < 0) 2516 dev_err(sdev->dev, "error: failed to save DAI config for SSP%d\n", 2517 config->dai_index); 2518 2519 return ret; 2520 } 2521 2522 static int sof_link_sai_load(struct snd_soc_component *scomp, int index, 2523 struct snd_soc_dai_link *link, 2524 struct snd_soc_tplg_link_config *cfg, 2525 struct snd_soc_tplg_hw_config *hw_config, 2526 struct sof_ipc_dai_config *config) 2527 { 2528 /*TODO: Add implementation */ 2529 return 0; 2530 } 2531 2532 static int sof_link_esai_load(struct snd_soc_component *scomp, int index, 2533 struct snd_soc_dai_link *link, 2534 struct snd_soc_tplg_link_config *cfg, 2535 struct snd_soc_tplg_hw_config *hw_config, 2536 struct sof_ipc_dai_config *config) 2537 { 2538 /*TODO: Add implementation */ 2539 return 0; 2540 } 2541 2542 static int sof_link_dmic_load(struct snd_soc_component *scomp, int index, 2543 struct snd_soc_dai_link *link, 2544 struct snd_soc_tplg_link_config *cfg, 2545 struct snd_soc_tplg_hw_config *hw_config, 2546 struct sof_ipc_dai_config *config) 2547 { 2548 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2549 struct snd_soc_tplg_private *private = &cfg->priv; 2550 struct sof_ipc_dai_config *ipc_config; 2551 struct sof_ipc_reply reply; 2552 struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 2553 struct sof_ipc_fw_version *v = &ready->version; 2554 u32 size; 2555 int ret, j; 2556 2557 /* 2558 * config is only used for the common params in dmic_params structure 2559 * that does not include the PDM controller config array 2560 * Set the common params to 0. 2561 */ 2562 memset(&config->dmic, 0, sizeof(struct sof_ipc_dai_dmic_params)); 2563 2564 /* get DMIC tokens */ 2565 ret = sof_parse_tokens(scomp, &config->dmic, dmic_tokens, 2566 ARRAY_SIZE(dmic_tokens), private->array, 2567 le32_to_cpu(private->size)); 2568 if (ret != 0) { 2569 dev_err(sdev->dev, "error: parse dmic tokens failed %d\n", 2570 le32_to_cpu(private->size)); 2571 return ret; 2572 } 2573 2574 /* 2575 * allocate memory for dmic dai config accounting for the 2576 * variable number of active pdm controllers 2577 * This will be the ipc payload for setting dai config 2578 */ 2579 size = sizeof(*config) + sizeof(struct sof_ipc_dai_dmic_pdm_ctrl) * 2580 config->dmic.num_pdm_active; 2581 2582 ipc_config = kzalloc(size, GFP_KERNEL); 2583 if (!ipc_config) 2584 return -ENOMEM; 2585 2586 /* copy the common dai config and dmic params */ 2587 memcpy(ipc_config, config, sizeof(*config)); 2588 2589 /* 2590 * alloc memory for private member 2591 * Used to track the pdm config array index currently being parsed 2592 */ 2593 sdev->private = kzalloc(sizeof(u32), GFP_KERNEL); 2594 if (!sdev->private) { 2595 kfree(ipc_config); 2596 return -ENOMEM; 2597 } 2598 2599 /* get DMIC PDM tokens */ 2600 ret = sof_parse_tokens(scomp, &ipc_config->dmic.pdm[0], dmic_pdm_tokens, 2601 ARRAY_SIZE(dmic_pdm_tokens), private->array, 2602 le32_to_cpu(private->size)); 2603 if (ret != 0) { 2604 dev_err(sdev->dev, "error: parse dmic pdm tokens failed %d\n", 2605 le32_to_cpu(private->size)); 2606 goto err; 2607 } 2608 2609 /* set IPC header size */ 2610 ipc_config->hdr.size = size; 2611 2612 /* debug messages */ 2613 dev_dbg(sdev->dev, "tplg: config DMIC%d driver version %d\n", 2614 ipc_config->dai_index, ipc_config->dmic.driver_ipc_version); 2615 dev_dbg(sdev->dev, "pdmclk_min %d pdm_clkmax %d duty_min %hd\n", 2616 ipc_config->dmic.pdmclk_min, ipc_config->dmic.pdmclk_max, 2617 ipc_config->dmic.duty_min); 2618 dev_dbg(sdev->dev, "duty_max %hd fifo_fs %d num_pdms active %d\n", 2619 ipc_config->dmic.duty_max, ipc_config->dmic.fifo_fs, 2620 ipc_config->dmic.num_pdm_active); 2621 dev_dbg(sdev->dev, "fifo word length %hd\n", 2622 ipc_config->dmic.fifo_bits); 2623 2624 for (j = 0; j < ipc_config->dmic.num_pdm_active; j++) { 2625 dev_dbg(sdev->dev, "pdm %hd mic a %hd mic b %hd\n", 2626 ipc_config->dmic.pdm[j].id, 2627 ipc_config->dmic.pdm[j].enable_mic_a, 2628 ipc_config->dmic.pdm[j].enable_mic_b); 2629 dev_dbg(sdev->dev, "pdm %hd polarity a %hd polarity b %hd\n", 2630 ipc_config->dmic.pdm[j].id, 2631 ipc_config->dmic.pdm[j].polarity_mic_a, 2632 ipc_config->dmic.pdm[j].polarity_mic_b); 2633 dev_dbg(sdev->dev, "pdm %hd clk_edge %hd skew %hd\n", 2634 ipc_config->dmic.pdm[j].id, 2635 ipc_config->dmic.pdm[j].clk_edge, 2636 ipc_config->dmic.pdm[j].skew); 2637 } 2638 2639 if (SOF_ABI_VER(v->major, v->minor, v->micro) < SOF_ABI_VER(3, 0, 1)) { 2640 /* this takes care of backwards compatible handling of fifo_bits_b */ 2641 ipc_config->dmic.reserved_2 = ipc_config->dmic.fifo_bits; 2642 } 2643 2644 /* send message to DSP */ 2645 ret = sof_ipc_tx_message(sdev->ipc, 2646 ipc_config->hdr.cmd, ipc_config, size, &reply, 2647 sizeof(reply)); 2648 2649 if (ret < 0) { 2650 dev_err(sdev->dev, 2651 "error: failed to set DAI config for DMIC%d\n", 2652 config->dai_index); 2653 goto err; 2654 } 2655 2656 /* set config for all DAI's with name matching the link name */ 2657 ret = sof_set_dai_config(sdev, size, link, ipc_config); 2658 if (ret < 0) 2659 dev_err(sdev->dev, "error: failed to save DAI config for DMIC%d\n", 2660 config->dai_index); 2661 2662 err: 2663 kfree(sdev->private); 2664 kfree(ipc_config); 2665 2666 return ret; 2667 } 2668 2669 /* 2670 * for hda link, playback and capture are supported by different dai 2671 * in FW. Here get the dai_index, set dma channel of each dai 2672 * and send config to FW. In FW, each dai sets config by dai_index 2673 */ 2674 static int sof_link_hda_process(struct snd_sof_dev *sdev, 2675 struct snd_soc_dai_link *link, 2676 struct sof_ipc_dai_config *config) 2677 { 2678 struct sof_ipc_reply reply; 2679 u32 size = sizeof(*config); 2680 struct snd_sof_dai *sof_dai; 2681 int found = 0; 2682 int ret; 2683 2684 list_for_each_entry(sof_dai, &sdev->dai_list, list) { 2685 if (!sof_dai->name) 2686 continue; 2687 2688 if (strcmp(link->name, sof_dai->name) == 0) { 2689 config->dai_index = sof_dai->comp_dai.dai_index; 2690 found = 1; 2691 2692 config->hda.link_dma_ch = DMA_CHAN_INVALID; 2693 2694 /* save config in dai component */ 2695 sof_dai->dai_config = kmemdup(config, size, GFP_KERNEL); 2696 if (!sof_dai->dai_config) 2697 return -ENOMEM; 2698 2699 sof_dai->cpu_dai_name = link->cpus->dai_name; 2700 2701 /* send message to DSP */ 2702 ret = sof_ipc_tx_message(sdev->ipc, 2703 config->hdr.cmd, config, size, 2704 &reply, sizeof(reply)); 2705 2706 if (ret < 0) { 2707 dev_err(sdev->dev, "error: failed to set DAI config for direction:%d of HDA dai %d\n", 2708 sof_dai->comp_dai.direction, 2709 config->dai_index); 2710 2711 return ret; 2712 } 2713 } 2714 } 2715 2716 /* 2717 * machine driver may define a dai link with playback and capture 2718 * dai enabled, but the dai link in topology would support both, one 2719 * or none of them. Here print a warning message to notify user 2720 */ 2721 if (!found) { 2722 dev_warn(sdev->dev, "warning: failed to find dai for dai link %s", 2723 link->name); 2724 } 2725 2726 return 0; 2727 } 2728 2729 static int sof_link_hda_load(struct snd_soc_component *scomp, int index, 2730 struct snd_soc_dai_link *link, 2731 struct snd_soc_tplg_link_config *cfg, 2732 struct snd_soc_tplg_hw_config *hw_config, 2733 struct sof_ipc_dai_config *config) 2734 { 2735 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2736 struct snd_soc_tplg_private *private = &cfg->priv; 2737 struct snd_soc_dai *dai; 2738 u32 size = sizeof(*config); 2739 int ret; 2740 2741 /* init IPC */ 2742 memset(&config->hda, 0, sizeof(struct sof_ipc_dai_hda_params)); 2743 config->hdr.size = size; 2744 2745 /* get any bespoke DAI tokens */ 2746 ret = sof_parse_tokens(scomp, config, hda_tokens, 2747 ARRAY_SIZE(hda_tokens), private->array, 2748 le32_to_cpu(private->size)); 2749 if (ret != 0) { 2750 dev_err(sdev->dev, "error: parse hda tokens failed %d\n", 2751 le32_to_cpu(private->size)); 2752 return ret; 2753 } 2754 2755 dai = snd_soc_find_dai(link->cpus); 2756 if (!dai) { 2757 dev_err(sdev->dev, "error: failed to find dai %s in %s", 2758 link->cpus->dai_name, __func__); 2759 return -EINVAL; 2760 } 2761 2762 ret = sof_link_hda_process(sdev, link, config); 2763 if (ret < 0) 2764 dev_err(sdev->dev, "error: failed to process hda dai link %s", 2765 link->name); 2766 2767 return ret; 2768 } 2769 2770 static int sof_link_alh_load(struct snd_soc_component *scomp, int index, 2771 struct snd_soc_dai_link *link, 2772 struct snd_soc_tplg_link_config *cfg, 2773 struct snd_soc_tplg_hw_config *hw_config, 2774 struct sof_ipc_dai_config *config) 2775 { 2776 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2777 struct sof_ipc_reply reply; 2778 u32 size = sizeof(*config); 2779 int ret; 2780 2781 /* init IPC */ 2782 config->hdr.size = size; 2783 2784 /* send message to DSP */ 2785 ret = sof_ipc_tx_message(sdev->ipc, 2786 config->hdr.cmd, config, size, &reply, 2787 sizeof(reply)); 2788 2789 if (ret < 0) { 2790 dev_err(sdev->dev, "error: failed to set DAI config for ALH %d\n", 2791 config->dai_index); 2792 return ret; 2793 } 2794 2795 /* set config for all DAI's with name matching the link name */ 2796 ret = sof_set_dai_config(sdev, size, link, config); 2797 if (ret < 0) 2798 dev_err(sdev->dev, "error: failed to save DAI config for ALH %d\n", 2799 config->dai_index); 2800 2801 return ret; 2802 } 2803 2804 /* DAI link - used for any driver specific init */ 2805 static int sof_link_load(struct snd_soc_component *scomp, int index, 2806 struct snd_soc_dai_link *link, 2807 struct snd_soc_tplg_link_config *cfg) 2808 { 2809 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2810 struct snd_soc_tplg_private *private = &cfg->priv; 2811 struct sof_ipc_dai_config config; 2812 struct snd_soc_tplg_hw_config *hw_config; 2813 int num_hw_configs; 2814 int ret; 2815 int i = 0; 2816 2817 if (!link->platforms) { 2818 dev_err(sdev->dev, "error: no platforms\n"); 2819 return -EINVAL; 2820 } 2821 link->platforms->name = dev_name(sdev->dev); 2822 2823 /* 2824 * Set nonatomic property for FE dai links as their trigger action 2825 * involves IPC's. 2826 */ 2827 if (!link->no_pcm) { 2828 link->nonatomic = true; 2829 2830 /* nothing more to do for FE dai links */ 2831 return 0; 2832 } 2833 2834 /* check we have some tokens - we need at least DAI type */ 2835 if (le32_to_cpu(private->size) == 0) { 2836 dev_err(sdev->dev, "error: expected tokens for DAI, none found\n"); 2837 return -EINVAL; 2838 } 2839 2840 /* Send BE DAI link configurations to DSP */ 2841 memset(&config, 0, sizeof(config)); 2842 2843 /* get any common DAI tokens */ 2844 ret = sof_parse_tokens(scomp, &config, dai_link_tokens, 2845 ARRAY_SIZE(dai_link_tokens), private->array, 2846 le32_to_cpu(private->size)); 2847 if (ret != 0) { 2848 dev_err(sdev->dev, "error: parse link tokens failed %d\n", 2849 le32_to_cpu(private->size)); 2850 return ret; 2851 } 2852 2853 /* 2854 * DAI links are expected to have at least 1 hw_config. 2855 * But some older topologies might have no hw_config for HDA dai links. 2856 */ 2857 num_hw_configs = le32_to_cpu(cfg->num_hw_configs); 2858 if (!num_hw_configs) { 2859 if (config.type != SOF_DAI_INTEL_HDA) { 2860 dev_err(sdev->dev, "error: unexpected DAI config count %d!\n", 2861 le32_to_cpu(cfg->num_hw_configs)); 2862 return -EINVAL; 2863 } 2864 } else { 2865 dev_dbg(sdev->dev, "tplg: %d hw_configs found, default id: %d!\n", 2866 cfg->num_hw_configs, le32_to_cpu(cfg->default_hw_config_id)); 2867 2868 for (i = 0; i < num_hw_configs; i++) { 2869 if (cfg->hw_config[i].id == cfg->default_hw_config_id) 2870 break; 2871 } 2872 2873 if (i == num_hw_configs) { 2874 dev_err(sdev->dev, "error: default hw_config id: %d not found!\n", 2875 le32_to_cpu(cfg->default_hw_config_id)); 2876 return -EINVAL; 2877 } 2878 } 2879 2880 /* configure dai IPC message */ 2881 hw_config = &cfg->hw_config[i]; 2882 2883 config.hdr.cmd = SOF_IPC_GLB_DAI_MSG | SOF_IPC_DAI_CONFIG; 2884 config.format = le32_to_cpu(hw_config->fmt); 2885 2886 /* now load DAI specific data and send IPC - type comes from token */ 2887 switch (config.type) { 2888 case SOF_DAI_INTEL_SSP: 2889 ret = sof_link_ssp_load(scomp, index, link, cfg, hw_config, 2890 &config); 2891 break; 2892 case SOF_DAI_INTEL_DMIC: 2893 ret = sof_link_dmic_load(scomp, index, link, cfg, hw_config, 2894 &config); 2895 break; 2896 case SOF_DAI_INTEL_HDA: 2897 ret = sof_link_hda_load(scomp, index, link, cfg, hw_config, 2898 &config); 2899 break; 2900 case SOF_DAI_INTEL_ALH: 2901 ret = sof_link_alh_load(scomp, index, link, cfg, hw_config, 2902 &config); 2903 break; 2904 case SOF_DAI_IMX_SAI: 2905 ret = sof_link_sai_load(scomp, index, link, cfg, hw_config, 2906 &config); 2907 break; 2908 case SOF_DAI_IMX_ESAI: 2909 ret = sof_link_esai_load(scomp, index, link, cfg, hw_config, 2910 &config); 2911 break; 2912 default: 2913 dev_err(sdev->dev, "error: invalid DAI type %d\n", config.type); 2914 ret = -EINVAL; 2915 break; 2916 } 2917 if (ret < 0) 2918 return ret; 2919 2920 return 0; 2921 } 2922 2923 static int sof_link_hda_unload(struct snd_sof_dev *sdev, 2924 struct snd_soc_dai_link *link) 2925 { 2926 struct snd_soc_dai *dai; 2927 int ret = 0; 2928 2929 dai = snd_soc_find_dai(link->cpus); 2930 if (!dai) { 2931 dev_err(sdev->dev, "error: failed to find dai %s in %s", 2932 link->cpus->dai_name, __func__); 2933 return -EINVAL; 2934 } 2935 2936 return ret; 2937 } 2938 2939 static int sof_link_unload(struct snd_soc_component *scomp, 2940 struct snd_soc_dobj *dobj) 2941 { 2942 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2943 struct snd_soc_dai_link *link = 2944 container_of(dobj, struct snd_soc_dai_link, dobj); 2945 2946 struct snd_sof_dai *sof_dai; 2947 int ret = 0; 2948 2949 /* only BE link is loaded by sof */ 2950 if (!link->no_pcm) 2951 return 0; 2952 2953 list_for_each_entry(sof_dai, &sdev->dai_list, list) { 2954 if (!sof_dai->name) 2955 continue; 2956 2957 if (strcmp(link->name, sof_dai->name) == 0) 2958 goto found; 2959 } 2960 2961 dev_err(sdev->dev, "error: failed to find dai %s in %s", 2962 link->name, __func__); 2963 return -EINVAL; 2964 found: 2965 2966 switch (sof_dai->dai_config->type) { 2967 case SOF_DAI_INTEL_SSP: 2968 case SOF_DAI_INTEL_DMIC: 2969 case SOF_DAI_INTEL_ALH: 2970 /* no resource needs to be released for SSP, DMIC and ALH */ 2971 break; 2972 case SOF_DAI_INTEL_HDA: 2973 ret = sof_link_hda_unload(sdev, link); 2974 break; 2975 default: 2976 dev_err(sdev->dev, "error: invalid DAI type %d\n", 2977 sof_dai->dai_config->type); 2978 ret = -EINVAL; 2979 break; 2980 } 2981 2982 return ret; 2983 } 2984 2985 /* DAI link - used for any driver specific init */ 2986 static int sof_route_load(struct snd_soc_component *scomp, int index, 2987 struct snd_soc_dapm_route *route) 2988 { 2989 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 2990 struct sof_ipc_pipe_comp_connect *connect; 2991 struct snd_sof_widget *source_swidget, *sink_swidget; 2992 struct snd_soc_dobj *dobj = &route->dobj; 2993 struct snd_sof_route *sroute; 2994 struct sof_ipc_reply reply; 2995 int ret = 0; 2996 2997 /* allocate memory for sroute and connect */ 2998 sroute = kzalloc(sizeof(*sroute), GFP_KERNEL); 2999 if (!sroute) 3000 return -ENOMEM; 3001 3002 sroute->sdev = sdev; 3003 3004 connect = kzalloc(sizeof(*connect), GFP_KERNEL); 3005 if (!connect) { 3006 kfree(sroute); 3007 return -ENOMEM; 3008 } 3009 3010 connect->hdr.size = sizeof(*connect); 3011 connect->hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_COMP_CONNECT; 3012 3013 dev_dbg(sdev->dev, "sink %s control %s source %s\n", 3014 route->sink, route->control ? route->control : "none", 3015 route->source); 3016 3017 /* source component */ 3018 source_swidget = snd_sof_find_swidget(sdev, (char *)route->source); 3019 if (!source_swidget) { 3020 dev_err(sdev->dev, "error: source %s not found\n", 3021 route->source); 3022 ret = -EINVAL; 3023 goto err; 3024 } 3025 3026 /* 3027 * Virtual widgets of type output/out_drv may be added in topology 3028 * for compatibility. These are not handled by the FW. 3029 * So, don't send routes whose source/sink widget is of such types 3030 * to the DSP. 3031 */ 3032 if (source_swidget->id == snd_soc_dapm_out_drv || 3033 source_swidget->id == snd_soc_dapm_output) 3034 goto err; 3035 3036 connect->source_id = source_swidget->comp_id; 3037 3038 /* sink component */ 3039 sink_swidget = snd_sof_find_swidget(sdev, (char *)route->sink); 3040 if (!sink_swidget) { 3041 dev_err(sdev->dev, "error: sink %s not found\n", 3042 route->sink); 3043 ret = -EINVAL; 3044 goto err; 3045 } 3046 3047 /* 3048 * Don't send routes whose sink widget is of type 3049 * output or out_drv to the DSP 3050 */ 3051 if (sink_swidget->id == snd_soc_dapm_out_drv || 3052 sink_swidget->id == snd_soc_dapm_output) 3053 goto err; 3054 3055 connect->sink_id = sink_swidget->comp_id; 3056 3057 /* 3058 * For virtual routes, both sink and source are not 3059 * buffer. Since only buffer linked to component is supported by 3060 * FW, others are reported as error, add check in route function, 3061 * do not send it to FW when both source and sink are not buffer 3062 */ 3063 if (source_swidget->id != snd_soc_dapm_buffer && 3064 sink_swidget->id != snd_soc_dapm_buffer) { 3065 dev_dbg(sdev->dev, "warning: neither Linked source component %s nor sink component %s is of buffer type, ignoring link\n", 3066 route->source, route->sink); 3067 ret = 0; 3068 goto err; 3069 } else { 3070 ret = sof_ipc_tx_message(sdev->ipc, 3071 connect->hdr.cmd, 3072 connect, sizeof(*connect), 3073 &reply, sizeof(reply)); 3074 3075 /* check IPC return value */ 3076 if (ret < 0) { 3077 dev_err(sdev->dev, "error: failed to add route sink %s control %s source %s\n", 3078 route->sink, 3079 route->control ? route->control : "none", 3080 route->source); 3081 goto err; 3082 } 3083 3084 /* check IPC reply */ 3085 if (reply.error < 0) { 3086 dev_err(sdev->dev, "error: DSP failed to add route sink %s control %s source %s result %d\n", 3087 route->sink, 3088 route->control ? route->control : "none", 3089 route->source, reply.error); 3090 ret = reply.error; 3091 goto err; 3092 } 3093 3094 sroute->route = route; 3095 dobj->private = sroute; 3096 sroute->private = connect; 3097 3098 /* add route to route list */ 3099 list_add(&sroute->list, &sdev->route_list); 3100 3101 return ret; 3102 } 3103 3104 err: 3105 kfree(connect); 3106 kfree(sroute); 3107 return ret; 3108 } 3109 3110 /* Function to set the initial value of SOF kcontrols. 3111 * The value will be stored in scontrol->control_data 3112 */ 3113 static int snd_sof_cache_kcontrol_val(struct snd_sof_dev *sdev) 3114 { 3115 struct snd_sof_control *scontrol = NULL; 3116 int ipc_cmd, ctrl_type; 3117 int ret = 0; 3118 3119 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 3120 3121 /* notify DSP of kcontrol values */ 3122 switch (scontrol->cmd) { 3123 case SOF_CTRL_CMD_VOLUME: 3124 case SOF_CTRL_CMD_ENUM: 3125 case SOF_CTRL_CMD_SWITCH: 3126 ipc_cmd = SOF_IPC_COMP_GET_VALUE; 3127 ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_GET; 3128 break; 3129 case SOF_CTRL_CMD_BINARY: 3130 ipc_cmd = SOF_IPC_COMP_GET_DATA; 3131 ctrl_type = SOF_CTRL_TYPE_DATA_GET; 3132 break; 3133 default: 3134 dev_err(sdev->dev, 3135 "error: Invalid scontrol->cmd: %d\n", 3136 scontrol->cmd); 3137 return -EINVAL; 3138 } 3139 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol, 3140 ipc_cmd, ctrl_type, 3141 scontrol->cmd, 3142 false); 3143 if (ret < 0) { 3144 dev_warn(sdev->dev, 3145 "error: kcontrol value get for widget: %d\n", 3146 scontrol->comp_id); 3147 } 3148 } 3149 3150 return ret; 3151 } 3152 3153 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev, 3154 struct snd_sof_widget *swidget) 3155 { 3156 struct sof_ipc_pipe_ready ready; 3157 struct sof_ipc_reply reply; 3158 int ret; 3159 3160 dev_dbg(sdev->dev, "tplg: complete pipeline %s id %d\n", 3161 swidget->widget->name, swidget->comp_id); 3162 3163 memset(&ready, 0, sizeof(ready)); 3164 ready.hdr.size = sizeof(ready); 3165 ready.hdr.cmd = SOF_IPC_GLB_TPLG_MSG | SOF_IPC_TPLG_PIPE_COMPLETE; 3166 ready.comp_id = swidget->comp_id; 3167 3168 ret = sof_ipc_tx_message(sdev->ipc, 3169 ready.hdr.cmd, &ready, sizeof(ready), &reply, 3170 sizeof(reply)); 3171 if (ret < 0) 3172 return ret; 3173 return 1; 3174 } 3175 3176 /* completion - called at completion of firmware loading */ 3177 static void sof_complete(struct snd_soc_component *scomp) 3178 { 3179 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 3180 struct snd_sof_widget *swidget; 3181 3182 /* some widget types require completion notificattion */ 3183 list_for_each_entry(swidget, &sdev->widget_list, list) { 3184 if (swidget->complete) 3185 continue; 3186 3187 switch (swidget->id) { 3188 case snd_soc_dapm_scheduler: 3189 swidget->complete = 3190 snd_sof_complete_pipeline(sdev, swidget); 3191 break; 3192 default: 3193 break; 3194 } 3195 } 3196 /* 3197 * cache initial values of SOF kcontrols by reading DSP value over 3198 * IPC. It may be overwritten by alsa-mixer after booting up 3199 */ 3200 snd_sof_cache_kcontrol_val(sdev); 3201 } 3202 3203 /* manifest - optional to inform component of manifest */ 3204 static int sof_manifest(struct snd_soc_component *scomp, int index, 3205 struct snd_soc_tplg_manifest *man) 3206 { 3207 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 3208 u32 size; 3209 u32 abi_version; 3210 3211 size = le32_to_cpu(man->priv.size); 3212 3213 /* backward compatible with tplg without ABI info */ 3214 if (!size) { 3215 dev_dbg(sdev->dev, "No topology ABI info\n"); 3216 return 0; 3217 } 3218 3219 if (size != SOF_TPLG_ABI_SIZE) { 3220 dev_err(sdev->dev, "error: invalid topology ABI size\n"); 3221 return -EINVAL; 3222 } 3223 3224 dev_info(sdev->dev, 3225 "Topology: ABI %d:%d:%d Kernel ABI %d:%d:%d\n", 3226 man->priv.data[0], man->priv.data[1], 3227 man->priv.data[2], SOF_ABI_MAJOR, SOF_ABI_MINOR, 3228 SOF_ABI_PATCH); 3229 3230 abi_version = SOF_ABI_VER(man->priv.data[0], 3231 man->priv.data[1], 3232 man->priv.data[2]); 3233 3234 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, abi_version)) { 3235 dev_err(sdev->dev, "error: incompatible topology ABI version\n"); 3236 return -EINVAL; 3237 } 3238 3239 if (abi_version > SOF_ABI_VERSION) { 3240 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_STRICT_ABI_CHECKS)) { 3241 dev_warn(sdev->dev, "warn: topology ABI is more recent than kernel\n"); 3242 } else { 3243 dev_err(sdev->dev, "error: topology ABI is more recent than kernel\n"); 3244 return -EINVAL; 3245 } 3246 } 3247 3248 return 0; 3249 } 3250 3251 /* vendor specific kcontrol handlers available for binding */ 3252 static const struct snd_soc_tplg_kcontrol_ops sof_io_ops[] = { 3253 {SOF_TPLG_KCTL_VOL_ID, snd_sof_volume_get, snd_sof_volume_put}, 3254 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_get, snd_sof_bytes_put}, 3255 {SOF_TPLG_KCTL_ENUM_ID, snd_sof_enum_get, snd_sof_enum_put}, 3256 {SOF_TPLG_KCTL_SWITCH_ID, snd_sof_switch_get, snd_sof_switch_put}, 3257 }; 3258 3259 /* vendor specific bytes ext handlers available for binding */ 3260 static const struct snd_soc_tplg_bytes_ext_ops sof_bytes_ext_ops[] = { 3261 {SOF_TPLG_KCTL_BYTES_ID, snd_sof_bytes_ext_get, snd_sof_bytes_ext_put}, 3262 }; 3263 3264 static struct snd_soc_tplg_ops sof_tplg_ops = { 3265 /* external kcontrol init - used for any driver specific init */ 3266 .control_load = sof_control_load, 3267 .control_unload = sof_control_unload, 3268 3269 /* external kcontrol init - used for any driver specific init */ 3270 .dapm_route_load = sof_route_load, 3271 .dapm_route_unload = sof_route_unload, 3272 3273 /* external widget init - used for any driver specific init */ 3274 /* .widget_load is not currently used */ 3275 .widget_ready = sof_widget_ready, 3276 .widget_unload = sof_widget_unload, 3277 3278 /* FE DAI - used for any driver specific init */ 3279 .dai_load = sof_dai_load, 3280 .dai_unload = sof_dai_unload, 3281 3282 /* DAI link - used for any driver specific init */ 3283 .link_load = sof_link_load, 3284 .link_unload = sof_link_unload, 3285 3286 /* completion - called at completion of firmware loading */ 3287 .complete = sof_complete, 3288 3289 /* manifest - optional to inform component of manifest */ 3290 .manifest = sof_manifest, 3291 3292 /* vendor specific kcontrol handlers available for binding */ 3293 .io_ops = sof_io_ops, 3294 .io_ops_count = ARRAY_SIZE(sof_io_ops), 3295 3296 /* vendor specific bytes ext handlers available for binding */ 3297 .bytes_ext_ops = sof_bytes_ext_ops, 3298 .bytes_ext_ops_count = ARRAY_SIZE(sof_bytes_ext_ops), 3299 }; 3300 3301 int snd_sof_init_topology(struct snd_sof_dev *sdev, 3302 struct snd_soc_tplg_ops *ops) 3303 { 3304 /* TODO: support linked list of topologies */ 3305 sdev->tplg_ops = ops; 3306 return 0; 3307 } 3308 EXPORT_SYMBOL(snd_sof_init_topology); 3309 3310 int snd_sof_load_topology(struct snd_sof_dev *sdev, const char *file) 3311 { 3312 const struct firmware *fw; 3313 int ret; 3314 3315 dev_dbg(sdev->dev, "loading topology:%s\n", file); 3316 3317 ret = request_firmware(&fw, file, sdev->dev); 3318 if (ret < 0) { 3319 dev_err(sdev->dev, "error: tplg request firmware %s failed err: %d\n", 3320 file, ret); 3321 return ret; 3322 } 3323 3324 ret = snd_soc_tplg_component_load(sdev->component, 3325 &sof_tplg_ops, fw, 3326 SND_SOC_TPLG_INDEX_ALL); 3327 if (ret < 0) { 3328 dev_err(sdev->dev, "error: tplg component load failed %d\n", 3329 ret); 3330 ret = -EINVAL; 3331 } 3332 3333 release_firmware(fw); 3334 return ret; 3335 } 3336 EXPORT_SYMBOL(snd_sof_load_topology); 3337