1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2018 Intel Corporation. All rights reserved. 7 // 8 // Authors: Keyon Jie <yang.jie@linux.intel.com> 9 // 10 11 #include <sound/pcm_params.h> 12 #include <sound/hdaudio_ext.h> 13 #include "../sof-priv.h" 14 #include "../sof-audio.h" 15 #include "hda.h" 16 17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 18 19 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 20 #include "../sof-probes.h" 21 #endif 22 23 struct hda_pipe_params { 24 u8 host_dma_id; 25 u8 link_dma_id; 26 u32 ch; 27 u32 s_freq; 28 u32 s_fmt; 29 u8 linktype; 30 snd_pcm_format_t format; 31 int link_index; 32 int stream; 33 unsigned int host_bps; 34 unsigned int link_bps; 35 }; 36 37 /* 38 * This function checks if the host dma channel corresponding 39 * to the link DMA stream_tag argument is assigned to one 40 * of the FEs connected to the BE DAI. 41 */ 42 static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd, 43 int dir, int stream_tag) 44 { 45 struct snd_pcm_substream *fe_substream; 46 struct hdac_stream *fe_hstream; 47 struct snd_soc_dpcm *dpcm; 48 49 for_each_dpcm_fe(rtd, dir, dpcm) { 50 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir); 51 fe_hstream = fe_substream->runtime->private_data; 52 if (fe_hstream->stream_tag == stream_tag) 53 return true; 54 } 55 56 return false; 57 } 58 59 static struct hdac_ext_stream * 60 hda_link_stream_assign(struct hdac_bus *bus, 61 struct snd_pcm_substream *substream) 62 { 63 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 64 struct sof_intel_hda_stream *hda_stream; 65 struct hdac_ext_stream *res = NULL; 66 struct hdac_stream *stream = NULL; 67 68 int stream_dir = substream->stream; 69 70 if (!bus->ppcap) { 71 dev_err(bus->dev, "stream type not supported\n"); 72 return NULL; 73 } 74 75 list_for_each_entry(stream, &bus->stream_list, list) { 76 struct hdac_ext_stream *hstream = 77 stream_to_hdac_ext_stream(stream); 78 if (stream->direction != substream->stream) 79 continue; 80 81 hda_stream = hstream_to_sof_hda_stream(hstream); 82 83 /* check if link is available */ 84 if (!hstream->link_locked) { 85 if (stream->opened) { 86 /* 87 * check if the stream tag matches the stream 88 * tag of one of the connected FEs 89 */ 90 if (hda_check_fes(rtd, stream_dir, 91 stream->stream_tag)) { 92 res = hstream; 93 break; 94 } 95 } else { 96 res = hstream; 97 98 /* 99 * This must be a hostless stream. 100 * So reserve the host DMA channel. 101 */ 102 hda_stream->host_reserved = 1; 103 break; 104 } 105 } 106 } 107 108 if (res) { 109 /* 110 * Decouple host and link DMA. The decoupled flag 111 * is updated in snd_hdac_ext_stream_decouple(). 112 */ 113 if (!res->decoupled) 114 snd_hdac_ext_stream_decouple(bus, res, true); 115 spin_lock_irq(&bus->reg_lock); 116 res->link_locked = 1; 117 res->link_substream = substream; 118 spin_unlock_irq(&bus->reg_lock); 119 } 120 121 return res; 122 } 123 124 static int hda_link_dma_params(struct hdac_ext_stream *stream, 125 struct hda_pipe_params *params) 126 { 127 struct hdac_stream *hstream = &stream->hstream; 128 unsigned char stream_tag = hstream->stream_tag; 129 struct hdac_bus *bus = hstream->bus; 130 struct hdac_ext_link *link; 131 unsigned int format_val; 132 133 snd_hdac_ext_stream_decouple(bus, stream, true); 134 snd_hdac_ext_link_stream_reset(stream); 135 136 format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch, 137 params->format, 138 params->link_bps, 0); 139 140 dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n", 141 format_val, params->s_freq, params->ch, params->format); 142 143 snd_hdac_ext_link_stream_setup(stream, format_val); 144 145 if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) { 146 list_for_each_entry(link, &bus->hlink_list, list) { 147 if (link->index == params->link_index) 148 snd_hdac_ext_link_set_stream_id(link, 149 stream_tag); 150 } 151 } 152 153 stream->link_prepared = 1; 154 155 return 0; 156 } 157 158 /* Send DAI_CONFIG IPC to the DAI that matches the dai_name and direction */ 159 static int hda_link_config_ipc(struct sof_intel_hda_stream *hda_stream, 160 const char *dai_name, int channel, int dir) 161 { 162 struct sof_ipc_dai_config *config; 163 struct snd_sof_dai *sof_dai; 164 struct sof_ipc_reply reply; 165 int ret = 0; 166 167 list_for_each_entry(sof_dai, &hda_stream->sdev->dai_list, list) { 168 if (!sof_dai->cpu_dai_name) 169 continue; 170 171 if (!strcmp(dai_name, sof_dai->cpu_dai_name) && 172 dir == sof_dai->comp_dai.direction) { 173 config = sof_dai->dai_config; 174 175 if (!config) { 176 dev_err(hda_stream->sdev->dev, 177 "error: no config for DAI %s\n", 178 sof_dai->name); 179 return -EINVAL; 180 } 181 182 /* update config with stream tag */ 183 config->hda.link_dma_ch = channel; 184 185 /* send IPC */ 186 ret = sof_ipc_tx_message(hda_stream->sdev->ipc, 187 config->hdr.cmd, 188 config, 189 config->hdr.size, 190 &reply, sizeof(reply)); 191 192 if (ret < 0) 193 dev_err(hda_stream->sdev->dev, 194 "error: failed to set dai config for %s\n", 195 sof_dai->name); 196 return ret; 197 } 198 } 199 200 return -EINVAL; 201 } 202 203 static int hda_link_hw_params(struct snd_pcm_substream *substream, 204 struct snd_pcm_hw_params *params, 205 struct snd_soc_dai *dai) 206 { 207 struct hdac_stream *hstream = substream->runtime->private_data; 208 struct hdac_bus *bus = hstream->bus; 209 struct hdac_ext_stream *link_dev; 210 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 211 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 212 struct sof_intel_hda_stream *hda_stream; 213 struct hda_pipe_params p_params = {0}; 214 struct hdac_ext_link *link; 215 int stream_tag; 216 int ret; 217 218 /* get stored dma data if resuming from system suspend */ 219 link_dev = snd_soc_dai_get_dma_data(dai, substream); 220 if (!link_dev) { 221 link_dev = hda_link_stream_assign(bus, substream); 222 if (!link_dev) 223 return -EBUSY; 224 225 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev); 226 } 227 228 stream_tag = hdac_stream(link_dev)->stream_tag; 229 230 hda_stream = hstream_to_sof_hda_stream(link_dev); 231 232 /* update the DSP with the new tag */ 233 ret = hda_link_config_ipc(hda_stream, dai->name, stream_tag - 1, 234 substream->stream); 235 if (ret < 0) 236 return ret; 237 238 link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name); 239 if (!link) 240 return -EINVAL; 241 242 /* set the stream tag in the codec dai dma params */ 243 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 244 snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0); 245 else 246 snd_soc_dai_set_tdm_slot(codec_dai, 0, stream_tag, 0, 0); 247 248 p_params.s_fmt = snd_pcm_format_width(params_format(params)); 249 p_params.ch = params_channels(params); 250 p_params.s_freq = params_rate(params); 251 p_params.stream = substream->stream; 252 p_params.link_dma_id = stream_tag - 1; 253 p_params.link_index = link->index; 254 p_params.format = params_format(params); 255 256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 257 p_params.link_bps = codec_dai->driver->playback.sig_bits; 258 else 259 p_params.link_bps = codec_dai->driver->capture.sig_bits; 260 261 return hda_link_dma_params(link_dev, &p_params); 262 } 263 264 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream, 265 struct snd_soc_dai *dai) 266 { 267 struct hdac_ext_stream *link_dev = 268 snd_soc_dai_get_dma_data(dai, substream); 269 struct snd_sof_dev *sdev = 270 snd_soc_component_get_drvdata(dai->component); 271 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 272 int stream = substream->stream; 273 274 if (link_dev->link_prepared) 275 return 0; 276 277 dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream); 278 279 return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params, 280 dai); 281 } 282 283 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream, 284 int cmd, struct snd_soc_dai *dai) 285 { 286 struct hdac_ext_stream *link_dev = 287 snd_soc_dai_get_dma_data(dai, substream); 288 struct sof_intel_hda_stream *hda_stream; 289 struct snd_soc_pcm_runtime *rtd; 290 struct hdac_ext_link *link; 291 struct hdac_stream *hstream; 292 struct hdac_bus *bus; 293 int stream_tag; 294 int ret; 295 296 hstream = substream->runtime->private_data; 297 bus = hstream->bus; 298 rtd = asoc_substream_to_rtd(substream); 299 300 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name); 301 if (!link) 302 return -EINVAL; 303 304 hda_stream = hstream_to_sof_hda_stream(link_dev); 305 306 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd); 307 switch (cmd) { 308 case SNDRV_PCM_TRIGGER_RESUME: 309 /* set up hw_params */ 310 ret = hda_link_pcm_prepare(substream, dai); 311 if (ret < 0) { 312 dev_err(dai->dev, 313 "error: setting up hw_params during resume\n"); 314 return ret; 315 } 316 317 fallthrough; 318 case SNDRV_PCM_TRIGGER_START: 319 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 320 snd_hdac_ext_link_stream_start(link_dev); 321 break; 322 case SNDRV_PCM_TRIGGER_SUSPEND: 323 case SNDRV_PCM_TRIGGER_STOP: 324 /* 325 * clear link DMA channel. It will be assigned when 326 * hw_params is set up again after resume. 327 */ 328 ret = hda_link_config_ipc(hda_stream, dai->name, 329 DMA_CHAN_INVALID, substream->stream); 330 if (ret < 0) 331 return ret; 332 333 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 334 stream_tag = hdac_stream(link_dev)->stream_tag; 335 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 336 } 337 338 link_dev->link_prepared = 0; 339 340 fallthrough; 341 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 342 snd_hdac_ext_link_stream_clear(link_dev); 343 break; 344 default: 345 return -EINVAL; 346 } 347 return 0; 348 } 349 350 static int hda_link_hw_free(struct snd_pcm_substream *substream, 351 struct snd_soc_dai *dai) 352 { 353 unsigned int stream_tag; 354 struct sof_intel_hda_stream *hda_stream; 355 struct hdac_bus *bus; 356 struct hdac_ext_link *link; 357 struct hdac_stream *hstream; 358 struct snd_soc_pcm_runtime *rtd; 359 struct hdac_ext_stream *link_dev; 360 int ret; 361 362 hstream = substream->runtime->private_data; 363 bus = hstream->bus; 364 rtd = asoc_substream_to_rtd(substream); 365 link_dev = snd_soc_dai_get_dma_data(dai, substream); 366 367 if (!link_dev) { 368 dev_dbg(dai->dev, 369 "%s: link_dev is not assigned\n", __func__); 370 return -EINVAL; 371 } 372 373 hda_stream = hstream_to_sof_hda_stream(link_dev); 374 375 /* free the link DMA channel in the FW */ 376 ret = hda_link_config_ipc(hda_stream, dai->name, DMA_CHAN_INVALID, 377 substream->stream); 378 if (ret < 0) 379 return ret; 380 381 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name); 382 if (!link) 383 return -EINVAL; 384 385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 386 stream_tag = hdac_stream(link_dev)->stream_tag; 387 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 388 } 389 390 snd_soc_dai_set_dma_data(dai, substream, NULL); 391 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK); 392 link_dev->link_prepared = 0; 393 394 /* free the host DMA channel reserved by hostless streams */ 395 hda_stream->host_reserved = 0; 396 397 return 0; 398 } 399 400 static const struct snd_soc_dai_ops hda_link_dai_ops = { 401 .hw_params = hda_link_hw_params, 402 .hw_free = hda_link_hw_free, 403 .trigger = hda_link_pcm_trigger, 404 .prepare = hda_link_pcm_prepare, 405 }; 406 407 #endif 408 409 static int ssp_dai_hw_params(struct snd_pcm_substream *substream, 410 struct snd_pcm_hw_params *params, 411 struct snd_soc_dai *dai) 412 { 413 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 414 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 415 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 416 struct sof_ipc_fw_version *v = &sdev->fw_ready.version; 417 struct sof_ipc_dai_config *config; 418 struct snd_sof_dai *sof_dai; 419 struct sof_ipc_reply reply; 420 int ret; 421 422 /* DAI_CONFIG IPC during hw_params is not supported in older firmware */ 423 if (v->abi_version < SOF_ABI_VER(3, 18, 0)) 424 return 0; 425 426 list_for_each_entry(sof_dai, &sdev->dai_list, list) { 427 if (!sof_dai->cpu_dai_name || !sof_dai->dai_config) 428 continue; 429 430 if (!strcmp(dai->name, sof_dai->cpu_dai_name) && 431 substream->stream == sof_dai->comp_dai.direction) { 432 config = &sof_dai->dai_config[sof_dai->current_config]; 433 434 /* send IPC */ 435 ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, 436 config->hdr.size, &reply, sizeof(reply)); 437 438 if (ret < 0) 439 dev_err(sdev->dev, "error: failed to set DAI config for %s\n", 440 sof_dai->name); 441 return ret; 442 } 443 } 444 445 return 0; 446 } 447 448 static const struct snd_soc_dai_ops ssp_dai_ops = { 449 .hw_params = ssp_dai_hw_params, 450 }; 451 452 /* 453 * common dai driver for skl+ platforms. 454 * some products who use this DAI array only physically have a subset of 455 * the DAIs, but no harm is done here by adding the whole set. 456 */ 457 struct snd_soc_dai_driver skl_dai[] = { 458 { 459 .name = "SSP0 Pin", 460 .ops = &ssp_dai_ops, 461 .playback = { 462 .channels_min = 1, 463 .channels_max = 8, 464 }, 465 .capture = { 466 .channels_min = 1, 467 .channels_max = 8, 468 }, 469 }, 470 { 471 .name = "SSP1 Pin", 472 .ops = &ssp_dai_ops, 473 .playback = { 474 .channels_min = 1, 475 .channels_max = 8, 476 }, 477 .capture = { 478 .channels_min = 1, 479 .channels_max = 8, 480 }, 481 }, 482 { 483 .name = "SSP2 Pin", 484 .ops = &ssp_dai_ops, 485 .playback = { 486 .channels_min = 1, 487 .channels_max = 8, 488 }, 489 .capture = { 490 .channels_min = 1, 491 .channels_max = 8, 492 }, 493 }, 494 { 495 .name = "SSP3 Pin", 496 .ops = &ssp_dai_ops, 497 .playback = { 498 .channels_min = 1, 499 .channels_max = 8, 500 }, 501 .capture = { 502 .channels_min = 1, 503 .channels_max = 8, 504 }, 505 }, 506 { 507 .name = "SSP4 Pin", 508 .ops = &ssp_dai_ops, 509 .playback = { 510 .channels_min = 1, 511 .channels_max = 8, 512 }, 513 .capture = { 514 .channels_min = 1, 515 .channels_max = 8, 516 }, 517 }, 518 { 519 .name = "SSP5 Pin", 520 .ops = &ssp_dai_ops, 521 .playback = { 522 .channels_min = 1, 523 .channels_max = 8, 524 }, 525 .capture = { 526 .channels_min = 1, 527 .channels_max = 8, 528 }, 529 }, 530 { 531 .name = "DMIC01 Pin", 532 .capture = { 533 .channels_min = 1, 534 .channels_max = 4, 535 }, 536 }, 537 { 538 .name = "DMIC16k Pin", 539 .capture = { 540 .channels_min = 1, 541 .channels_max = 4, 542 }, 543 }, 544 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 545 { 546 .name = "iDisp1 Pin", 547 .ops = &hda_link_dai_ops, 548 .playback = { 549 .channels_min = 1, 550 .channels_max = 8, 551 }, 552 }, 553 { 554 .name = "iDisp2 Pin", 555 .ops = &hda_link_dai_ops, 556 .playback = { 557 .channels_min = 1, 558 .channels_max = 8, 559 }, 560 }, 561 { 562 .name = "iDisp3 Pin", 563 .ops = &hda_link_dai_ops, 564 .playback = { 565 .channels_min = 1, 566 .channels_max = 8, 567 }, 568 }, 569 { 570 .name = "iDisp4 Pin", 571 .ops = &hda_link_dai_ops, 572 .playback = { 573 .channels_min = 1, 574 .channels_max = 8, 575 }, 576 }, 577 { 578 .name = "Analog CPU DAI", 579 .ops = &hda_link_dai_ops, 580 .playback = { 581 .channels_min = 1, 582 .channels_max = 16, 583 }, 584 .capture = { 585 .channels_min = 1, 586 .channels_max = 16, 587 }, 588 }, 589 { 590 .name = "Digital CPU DAI", 591 .ops = &hda_link_dai_ops, 592 .playback = { 593 .channels_min = 1, 594 .channels_max = 16, 595 }, 596 .capture = { 597 .channels_min = 1, 598 .channels_max = 16, 599 }, 600 }, 601 { 602 .name = "Alt Analog CPU DAI", 603 .ops = &hda_link_dai_ops, 604 .playback = { 605 .channels_min = 1, 606 .channels_max = 16, 607 }, 608 .capture = { 609 .channels_min = 1, 610 .channels_max = 16, 611 }, 612 }, 613 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_PROBES) 614 { 615 .name = "Probe Extraction CPU DAI", 616 .compress_new = snd_soc_new_compress, 617 .cops = &sof_probe_compr_ops, 618 .capture = { 619 .stream_name = "Probe Extraction", 620 .channels_min = 1, 621 .channels_max = 8, 622 .rates = SNDRV_PCM_RATE_48000, 623 .rate_min = 48000, 624 .rate_max = 48000, 625 }, 626 }, 627 #endif 628 #endif 629 }; 630