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 struct hda_pipe_params { 20 u32 ch; 21 u32 s_freq; 22 u32 s_fmt; 23 u8 linktype; 24 snd_pcm_format_t format; 25 int link_index; 26 int stream; 27 unsigned int link_bps; 28 }; 29 30 /* 31 * This function checks if the host dma channel corresponding 32 * to the link DMA stream_tag argument is assigned to one 33 * of the FEs connected to the BE DAI. 34 */ 35 static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd, 36 int dir, int stream_tag) 37 { 38 struct snd_pcm_substream *fe_substream; 39 struct hdac_stream *fe_hstream; 40 struct snd_soc_dpcm *dpcm; 41 42 for_each_dpcm_fe(rtd, dir, dpcm) { 43 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir); 44 fe_hstream = fe_substream->runtime->private_data; 45 if (fe_hstream->stream_tag == stream_tag) 46 return true; 47 } 48 49 return false; 50 } 51 52 static struct hdac_ext_stream * 53 hda_link_stream_assign(struct hdac_bus *bus, 54 struct snd_pcm_substream *substream) 55 { 56 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 57 struct sof_intel_hda_stream *hda_stream; 58 const struct sof_intel_dsp_desc *chip; 59 struct snd_sof_dev *sdev; 60 struct hdac_ext_stream *res = NULL; 61 struct hdac_stream *hstream = NULL; 62 63 int stream_dir = substream->stream; 64 65 if (!bus->ppcap) { 66 dev_err(bus->dev, "stream type not supported\n"); 67 return NULL; 68 } 69 70 spin_lock_irq(&bus->reg_lock); 71 list_for_each_entry(hstream, &bus->stream_list, list) { 72 struct hdac_ext_stream *hext_stream = 73 stream_to_hdac_ext_stream(hstream); 74 if (hstream->direction != substream->stream) 75 continue; 76 77 hda_stream = hstream_to_sof_hda_stream(hext_stream); 78 sdev = hda_stream->sdev; 79 chip = get_chip_info(sdev->pdata); 80 81 /* check if link is available */ 82 if (!hext_stream->link_locked) { 83 /* 84 * choose the first available link for platforms that do not have the 85 * PROCEN_FMT_QUIRK set. 86 */ 87 if (!(chip->quirks & SOF_INTEL_PROCEN_FMT_QUIRK)) { 88 res = hext_stream; 89 break; 90 } 91 92 if (hstream->opened) { 93 /* 94 * check if the stream tag matches the stream 95 * tag of one of the connected FEs 96 */ 97 if (hda_check_fes(rtd, stream_dir, 98 hstream->stream_tag)) { 99 res = hext_stream; 100 break; 101 } 102 } else { 103 res = hext_stream; 104 105 /* 106 * This must be a hostless stream. 107 * So reserve the host DMA channel. 108 */ 109 hda_stream->host_reserved = 1; 110 break; 111 } 112 } 113 } 114 115 if (res) { 116 /* 117 * Decouple host and link DMA. The decoupled flag 118 * is updated in snd_hdac_ext_stream_decouple(). 119 */ 120 if (!res->decoupled) 121 snd_hdac_ext_stream_decouple_locked(bus, res, true); 122 123 res->link_locked = 1; 124 res->link_substream = substream; 125 } 126 spin_unlock_irq(&bus->reg_lock); 127 128 return res; 129 } 130 131 static int hda_link_dma_params(struct hdac_ext_stream *hext_stream, 132 struct hda_pipe_params *params) 133 { 134 struct hdac_stream *hstream = &hext_stream->hstream; 135 unsigned char stream_tag = hstream->stream_tag; 136 struct hdac_bus *bus = hstream->bus; 137 struct hdac_ext_link *link; 138 unsigned int format_val; 139 140 snd_hdac_ext_stream_decouple(bus, hext_stream, true); 141 snd_hdac_ext_link_stream_reset(hext_stream); 142 143 format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch, 144 params->format, 145 params->link_bps, 0); 146 147 dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n", 148 format_val, params->s_freq, params->ch, params->format); 149 150 snd_hdac_ext_link_stream_setup(hext_stream, format_val); 151 152 if (hext_stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) { 153 list_for_each_entry(link, &bus->hlink_list, list) { 154 if (link->index == params->link_index) 155 snd_hdac_ext_link_set_stream_id(link, 156 stream_tag); 157 } 158 } 159 160 hext_stream->link_prepared = 1; 161 162 return 0; 163 } 164 165 static int hda_link_dai_widget_update(struct sof_intel_hda_stream *hda_stream, 166 struct snd_soc_dapm_widget *w, 167 int channel, bool widget_setup) 168 { 169 struct snd_sof_dai_config_data data; 170 171 data.dai_data = channel; 172 173 /* set up/free DAI widget and send DAI_CONFIG IPC */ 174 if (widget_setup) 175 return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_2_STEP_STOP, &data); 176 177 return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE, &data); 178 } 179 180 static int hda_link_hw_params(struct snd_pcm_substream *substream, 181 struct snd_pcm_hw_params *params, 182 struct snd_soc_dai *dai) 183 { 184 struct hdac_stream *hstream = substream->runtime->private_data; 185 struct hdac_bus *bus = hstream->bus; 186 struct hdac_ext_stream *hext_stream; 187 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 188 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 189 struct sof_intel_hda_stream *hda_stream; 190 struct hda_pipe_params p_params = {0}; 191 struct snd_soc_dapm_widget *w; 192 struct hdac_ext_link *link; 193 int stream_tag; 194 int ret; 195 196 /* get stored dma data if resuming from system suspend */ 197 hext_stream = snd_soc_dai_get_dma_data(dai, substream); 198 if (!hext_stream) { 199 hext_stream = hda_link_stream_assign(bus, substream); 200 if (!hext_stream) 201 return -EBUSY; 202 203 snd_soc_dai_set_dma_data(dai, substream, (void *)hext_stream); 204 } 205 206 stream_tag = hdac_stream(hext_stream)->stream_tag; 207 208 hda_stream = hstream_to_sof_hda_stream(hext_stream); 209 210 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 211 w = dai->playback_widget; 212 else 213 w = dai->capture_widget; 214 215 /* set up the DAI widget and send the DAI_CONFIG with the new tag */ 216 ret = hda_link_dai_widget_update(hda_stream, w, stream_tag - 1, true); 217 if (ret < 0) 218 return ret; 219 220 link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name); 221 if (!link) 222 return -EINVAL; 223 224 /* set the hdac_stream in the codec dai */ 225 snd_soc_dai_set_stream(codec_dai, hdac_stream(hext_stream), substream->stream); 226 227 p_params.s_fmt = snd_pcm_format_width(params_format(params)); 228 p_params.ch = params_channels(params); 229 p_params.s_freq = params_rate(params); 230 p_params.stream = substream->stream; 231 p_params.link_index = link->index; 232 p_params.format = params_format(params); 233 234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 235 p_params.link_bps = codec_dai->driver->playback.sig_bits; 236 else 237 p_params.link_bps = codec_dai->driver->capture.sig_bits; 238 239 return hda_link_dma_params(hext_stream, &p_params); 240 } 241 242 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream, 243 struct snd_soc_dai *dai) 244 { 245 struct hdac_ext_stream *hext_stream = 246 snd_soc_dai_get_dma_data(dai, substream); 247 struct snd_sof_dev *sdev = 248 snd_soc_component_get_drvdata(dai->component); 249 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 250 int stream = substream->stream; 251 252 if (hext_stream->link_prepared) 253 return 0; 254 255 dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream); 256 257 return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params, 258 dai); 259 } 260 261 static int hda_link_dai_config_pause_push_ipc(struct snd_soc_dapm_widget *w) 262 { 263 struct snd_sof_widget *swidget = w->dobj.private; 264 struct snd_soc_component *component = swidget->scomp; 265 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 266 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 267 int ret = 0; 268 269 if (tplg_ops->dai_config) { 270 ret = tplg_ops->dai_config(sdev, swidget, SOF_DAI_CONFIG_FLAGS_PAUSE, NULL); 271 if (ret < 0) 272 dev_err(sdev->dev, "%s: DAI config failed for widget %s\n", __func__, 273 w->name); 274 } 275 276 return ret; 277 } 278 279 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream, 280 int cmd, struct snd_soc_dai *dai) 281 { 282 struct hdac_ext_stream *hext_stream = 283 snd_soc_dai_get_dma_data(dai, substream); 284 struct sof_intel_hda_stream *hda_stream; 285 struct snd_soc_pcm_runtime *rtd; 286 struct snd_soc_dapm_widget *w; 287 struct hdac_ext_link *link; 288 struct hdac_stream *hstream; 289 struct hdac_bus *bus; 290 int stream_tag; 291 int ret; 292 293 hstream = substream->runtime->private_data; 294 bus = hstream->bus; 295 rtd = asoc_substream_to_rtd(substream); 296 297 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name); 298 if (!link) 299 return -EINVAL; 300 301 hda_stream = hstream_to_sof_hda_stream(hext_stream); 302 303 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd); 304 305 w = snd_soc_dai_get_widget(dai, substream->stream); 306 307 switch (cmd) { 308 case SNDRV_PCM_TRIGGER_START: 309 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 310 snd_hdac_ext_link_stream_start(hext_stream); 311 break; 312 case SNDRV_PCM_TRIGGER_SUSPEND: 313 case SNDRV_PCM_TRIGGER_STOP: 314 snd_hdac_ext_link_stream_clear(hext_stream); 315 316 /* 317 * free DAI widget during stop/suspend to keep widget use_count's balanced. 318 */ 319 ret = hda_link_dai_widget_update(hda_stream, w, DMA_CHAN_INVALID, false); 320 if (ret < 0) 321 return ret; 322 323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 324 stream_tag = hdac_stream(hext_stream)->stream_tag; 325 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 326 } 327 328 hext_stream->link_prepared = 0; 329 break; 330 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 331 snd_hdac_ext_link_stream_clear(hext_stream); 332 333 ret = hda_link_dai_config_pause_push_ipc(w); 334 if (ret < 0) 335 return ret; 336 break; 337 default: 338 return -EINVAL; 339 } 340 return 0; 341 } 342 343 static int hda_link_hw_free(struct snd_pcm_substream *substream, 344 struct snd_soc_dai *dai) 345 { 346 unsigned int stream_tag; 347 struct sof_intel_hda_stream *hda_stream; 348 struct hdac_bus *bus; 349 struct hdac_ext_link *link; 350 struct hdac_stream *hstream; 351 struct snd_soc_pcm_runtime *rtd; 352 struct hdac_ext_stream *hext_stream; 353 struct snd_soc_dapm_widget *w; 354 int ret; 355 356 hstream = substream->runtime->private_data; 357 bus = hstream->bus; 358 rtd = asoc_substream_to_rtd(substream); 359 hext_stream = snd_soc_dai_get_dma_data(dai, substream); 360 361 if (!hext_stream) { 362 dev_dbg(dai->dev, 363 "%s: hext_stream is not assigned\n", __func__); 364 return -EINVAL; 365 } 366 367 hda_stream = hstream_to_sof_hda_stream(hext_stream); 368 369 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 370 w = dai->playback_widget; 371 else 372 w = dai->capture_widget; 373 374 /* free the link DMA channel in the FW and the DAI widget */ 375 ret = hda_link_dai_widget_update(hda_stream, w, DMA_CHAN_INVALID, false); 376 if (ret < 0) 377 return ret; 378 379 link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name); 380 if (!link) 381 return -EINVAL; 382 383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 384 stream_tag = hdac_stream(hext_stream)->stream_tag; 385 snd_hdac_ext_link_clear_stream_id(link, stream_tag); 386 } 387 388 snd_soc_dai_set_dma_data(dai, substream, NULL); 389 snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK); 390 hext_stream->link_prepared = 0; 391 392 /* free the host DMA channel reserved by hostless streams */ 393 hda_stream->host_reserved = 0; 394 395 return 0; 396 } 397 398 static const struct snd_soc_dai_ops hda_link_dai_ops = { 399 .hw_params = hda_link_hw_params, 400 .hw_free = hda_link_hw_free, 401 .trigger = hda_link_pcm_trigger, 402 .prepare = hda_link_pcm_prepare, 403 }; 404 405 #endif 406 407 /* only one flag used so far to harden hw_params/hw_free/trigger/prepare */ 408 struct ssp_dai_dma_data { 409 bool setup; 410 }; 411 412 static int ssp_dai_setup_or_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai, 413 bool setup) 414 { 415 struct snd_soc_dapm_widget *w; 416 417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 418 w = dai->playback_widget; 419 else 420 w = dai->capture_widget; 421 422 if (setup) 423 return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_NONE, NULL); 424 425 return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE, NULL); 426 } 427 428 static int ssp_dai_startup(struct snd_pcm_substream *substream, 429 struct snd_soc_dai *dai) 430 { 431 struct ssp_dai_dma_data *dma_data; 432 433 dma_data = kzalloc(sizeof(*dma_data), GFP_KERNEL); 434 if (!dma_data) 435 return -ENOMEM; 436 437 snd_soc_dai_set_dma_data(dai, substream, dma_data); 438 439 return 0; 440 } 441 442 static int ssp_dai_setup(struct snd_pcm_substream *substream, 443 struct snd_soc_dai *dai, 444 bool setup) 445 { 446 struct ssp_dai_dma_data *dma_data; 447 int ret = 0; 448 449 dma_data = snd_soc_dai_get_dma_data(dai, substream); 450 if (!dma_data) { 451 dev_err(dai->dev, "%s: failed to get dma_data\n", __func__); 452 return -EIO; 453 } 454 455 if (dma_data->setup != setup) { 456 ret = ssp_dai_setup_or_free(substream, dai, setup); 457 if (!ret) 458 dma_data->setup = setup; 459 } 460 return ret; 461 } 462 463 static int ssp_dai_hw_params(struct snd_pcm_substream *substream, 464 struct snd_pcm_hw_params *params, 465 struct snd_soc_dai *dai) 466 { 467 /* params are ignored for now */ 468 return ssp_dai_setup(substream, dai, true); 469 } 470 471 static int ssp_dai_prepare(struct snd_pcm_substream *substream, 472 struct snd_soc_dai *dai) 473 { 474 /* 475 * the SSP will only be reconfigured during resume operations and 476 * not in case of xruns 477 */ 478 return ssp_dai_setup(substream, dai, true); 479 } 480 481 static int ssp_dai_trigger(struct snd_pcm_substream *substream, 482 int cmd, struct snd_soc_dai *dai) 483 { 484 if (cmd != SNDRV_PCM_TRIGGER_SUSPEND) 485 return 0; 486 487 return ssp_dai_setup(substream, dai, false); 488 } 489 490 static int ssp_dai_hw_free(struct snd_pcm_substream *substream, 491 struct snd_soc_dai *dai) 492 { 493 return ssp_dai_setup(substream, dai, false); 494 } 495 496 static void ssp_dai_shutdown(struct snd_pcm_substream *substream, 497 struct snd_soc_dai *dai) 498 { 499 struct ssp_dai_dma_data *dma_data; 500 501 dma_data = snd_soc_dai_get_dma_data(dai, substream); 502 if (!dma_data) { 503 dev_err(dai->dev, "%s: failed to get dma_data\n", __func__); 504 return; 505 } 506 snd_soc_dai_set_dma_data(dai, substream, NULL); 507 kfree(dma_data); 508 } 509 510 static const struct snd_soc_dai_ops ssp_dai_ops = { 511 .startup = ssp_dai_startup, 512 .hw_params = ssp_dai_hw_params, 513 .prepare = ssp_dai_prepare, 514 .trigger = ssp_dai_trigger, 515 .hw_free = ssp_dai_hw_free, 516 .shutdown = ssp_dai_shutdown, 517 }; 518 519 /* 520 * common dai driver for skl+ platforms. 521 * some products who use this DAI array only physically have a subset of 522 * the DAIs, but no harm is done here by adding the whole set. 523 */ 524 struct snd_soc_dai_driver skl_dai[] = { 525 { 526 .name = "SSP0 Pin", 527 .ops = &ssp_dai_ops, 528 .playback = { 529 .channels_min = 1, 530 .channels_max = 8, 531 }, 532 .capture = { 533 .channels_min = 1, 534 .channels_max = 8, 535 }, 536 }, 537 { 538 .name = "SSP1 Pin", 539 .ops = &ssp_dai_ops, 540 .playback = { 541 .channels_min = 1, 542 .channels_max = 8, 543 }, 544 .capture = { 545 .channels_min = 1, 546 .channels_max = 8, 547 }, 548 }, 549 { 550 .name = "SSP2 Pin", 551 .ops = &ssp_dai_ops, 552 .playback = { 553 .channels_min = 1, 554 .channels_max = 8, 555 }, 556 .capture = { 557 .channels_min = 1, 558 .channels_max = 8, 559 }, 560 }, 561 { 562 .name = "SSP3 Pin", 563 .ops = &ssp_dai_ops, 564 .playback = { 565 .channels_min = 1, 566 .channels_max = 8, 567 }, 568 .capture = { 569 .channels_min = 1, 570 .channels_max = 8, 571 }, 572 }, 573 { 574 .name = "SSP4 Pin", 575 .ops = &ssp_dai_ops, 576 .playback = { 577 .channels_min = 1, 578 .channels_max = 8, 579 }, 580 .capture = { 581 .channels_min = 1, 582 .channels_max = 8, 583 }, 584 }, 585 { 586 .name = "SSP5 Pin", 587 .ops = &ssp_dai_ops, 588 .playback = { 589 .channels_min = 1, 590 .channels_max = 8, 591 }, 592 .capture = { 593 .channels_min = 1, 594 .channels_max = 8, 595 }, 596 }, 597 { 598 .name = "DMIC01 Pin", 599 .capture = { 600 .channels_min = 1, 601 .channels_max = 4, 602 }, 603 }, 604 { 605 .name = "DMIC16k Pin", 606 .capture = { 607 .channels_min = 1, 608 .channels_max = 4, 609 }, 610 }, 611 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) 612 { 613 .name = "iDisp1 Pin", 614 .ops = &hda_link_dai_ops, 615 .playback = { 616 .channels_min = 1, 617 .channels_max = 8, 618 }, 619 }, 620 { 621 .name = "iDisp2 Pin", 622 .ops = &hda_link_dai_ops, 623 .playback = { 624 .channels_min = 1, 625 .channels_max = 8, 626 }, 627 }, 628 { 629 .name = "iDisp3 Pin", 630 .ops = &hda_link_dai_ops, 631 .playback = { 632 .channels_min = 1, 633 .channels_max = 8, 634 }, 635 }, 636 { 637 .name = "iDisp4 Pin", 638 .ops = &hda_link_dai_ops, 639 .playback = { 640 .channels_min = 1, 641 .channels_max = 8, 642 }, 643 }, 644 { 645 .name = "Analog CPU DAI", 646 .ops = &hda_link_dai_ops, 647 .playback = { 648 .channels_min = 1, 649 .channels_max = 16, 650 }, 651 .capture = { 652 .channels_min = 1, 653 .channels_max = 16, 654 }, 655 }, 656 { 657 .name = "Digital CPU DAI", 658 .ops = &hda_link_dai_ops, 659 .playback = { 660 .channels_min = 1, 661 .channels_max = 16, 662 }, 663 .capture = { 664 .channels_min = 1, 665 .channels_max = 16, 666 }, 667 }, 668 { 669 .name = "Alt Analog CPU DAI", 670 .ops = &hda_link_dai_ops, 671 .playback = { 672 .channels_min = 1, 673 .channels_max = 16, 674 }, 675 .capture = { 676 .channels_min = 1, 677 .channels_max = 16, 678 }, 679 }, 680 #endif 681 }; 682