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