1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-2022 Intel Corporation 4 // 5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com> 6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com> 7 // 8 9 #include <linux/debugfs.h> 10 #include <linux/device.h> 11 #include <sound/hda_register.h> 12 #include <sound/hdaudio_ext.h> 13 #include <sound/pcm_params.h> 14 #include <sound/soc-acpi.h> 15 #include <sound/soc-acpi-intel-match.h> 16 #include <sound/soc-component.h> 17 #include "avs.h" 18 #include "path.h" 19 #include "topology.h" 20 #include "../../codecs/hda.h" 21 22 struct avs_dma_data { 23 struct avs_tplg_path_template *template; 24 struct avs_path *path; 25 struct avs_dev *adev; 26 27 /* LINK-stream utilized in BE operations while HOST in FE ones. */ 28 union { 29 struct hdac_ext_stream *link_stream; 30 struct hdac_ext_stream *host_stream; 31 }; 32 33 struct snd_pcm_substream *substream; 34 }; 35 36 static struct avs_tplg_path_template * 37 avs_dai_find_path_template(struct snd_soc_dai *dai, bool is_fe, int direction) 38 { 39 struct snd_soc_dapm_widget *dw = snd_soc_dai_get_widget(dai, direction); 40 struct snd_soc_dapm_path *dp; 41 enum snd_soc_dapm_direction dir; 42 43 if (direction == SNDRV_PCM_STREAM_CAPTURE) { 44 dir = is_fe ? SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN; 45 } else { 46 dir = is_fe ? SND_SOC_DAPM_DIR_IN : SND_SOC_DAPM_DIR_OUT; 47 } 48 49 dp = list_first_entry_or_null(&dw->edges[dir], typeof(*dp), list_node[dir]); 50 if (!dp) 51 return NULL; 52 53 /* Get the other widget, with actual path template data */ 54 dw = (dp->source == dw) ? dp->sink : dp->source; 55 56 return dw->priv; 57 } 58 59 static int avs_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 60 { 61 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 62 struct avs_dev *adev = to_avs_dev(dai->component->dev); 63 struct avs_tplg_path_template *template; 64 struct avs_dma_data *data; 65 66 template = avs_dai_find_path_template(dai, !rtd->dai_link->no_pcm, substream->stream); 67 if (!template) { 68 dev_err(dai->dev, "no %s path for dai %s, invalid tplg?\n", 69 snd_pcm_stream_str(substream), dai->name); 70 return -EINVAL; 71 } 72 73 data = kzalloc(sizeof(*data), GFP_KERNEL); 74 if (!data) 75 return -ENOMEM; 76 77 data->substream = substream; 78 data->template = template; 79 data->adev = adev; 80 snd_soc_dai_set_dma_data(dai, substream, data); 81 82 if (rtd->dai_link->ignore_suspend) 83 adev->num_lp_paths++; 84 85 return 0; 86 } 87 88 static void avs_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 89 { 90 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 91 struct avs_dma_data *data; 92 93 data = snd_soc_dai_get_dma_data(dai, substream); 94 95 if (rtd->dai_link->ignore_suspend) 96 data->adev->num_lp_paths--; 97 98 snd_soc_dai_set_dma_data(dai, substream, NULL); 99 kfree(data); 100 } 101 102 static int avs_dai_hw_params(struct snd_pcm_substream *substream, 103 struct snd_pcm_hw_params *fe_hw_params, 104 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai, 105 int dma_id) 106 { 107 struct avs_dma_data *data; 108 struct avs_path *path; 109 int ret; 110 111 data = snd_soc_dai_get_dma_data(dai, substream); 112 113 dev_dbg(dai->dev, "%s FE hw_params str %p rtd %p", 114 __func__, substream, substream->runtime); 115 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n", 116 params_rate(fe_hw_params), params_channels(fe_hw_params), 117 params_width(fe_hw_params), params_physical_width(fe_hw_params)); 118 119 dev_dbg(dai->dev, "%s BE hw_params str %p rtd %p", 120 __func__, substream, substream->runtime); 121 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n", 122 params_rate(be_hw_params), params_channels(be_hw_params), 123 params_width(be_hw_params), params_physical_width(be_hw_params)); 124 125 path = avs_path_create(data->adev, dma_id, data->template, fe_hw_params, be_hw_params); 126 if (IS_ERR(path)) { 127 ret = PTR_ERR(path); 128 dev_err(dai->dev, "create path failed: %d\n", ret); 129 return ret; 130 } 131 132 data->path = path; 133 return 0; 134 } 135 136 static int avs_dai_be_hw_params(struct snd_pcm_substream *substream, 137 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai, 138 int dma_id) 139 { 140 struct snd_pcm_hw_params *fe_hw_params = NULL; 141 struct snd_soc_pcm_runtime *fe, *be; 142 struct snd_soc_dpcm *dpcm; 143 144 be = snd_soc_substream_to_rtd(substream); 145 for_each_dpcm_fe(be, substream->stream, dpcm) { 146 fe = dpcm->fe; 147 fe_hw_params = &fe->dpcm[substream->stream].hw_params; 148 } 149 150 return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id); 151 } 152 153 static int avs_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 154 { 155 struct avs_dma_data *data; 156 int ret; 157 158 data = snd_soc_dai_get_dma_data(dai, substream); 159 if (!data->path) 160 return 0; 161 162 ret = avs_path_reset(data->path); 163 if (ret < 0) { 164 dev_err(dai->dev, "reset path failed: %d\n", ret); 165 return ret; 166 } 167 168 ret = avs_path_pause(data->path); 169 if (ret < 0) 170 dev_err(dai->dev, "pause path failed: %d\n", ret); 171 return ret; 172 } 173 174 static int avs_dai_nonhda_be_hw_params(struct snd_pcm_substream *substream, 175 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai) 176 { 177 struct avs_dma_data *data; 178 179 data = snd_soc_dai_get_dma_data(dai, substream); 180 if (data->path) 181 return 0; 182 183 /* Actual port-id comes from topology. */ 184 return avs_dai_be_hw_params(substream, hw_params, dai, 0); 185 } 186 187 static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 188 { 189 struct avs_dma_data *data; 190 191 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name); 192 193 data = snd_soc_dai_get_dma_data(dai, substream); 194 if (data->path) { 195 avs_path_free(data->path); 196 data->path = NULL; 197 } 198 199 return 0; 200 } 201 202 static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd, 203 struct snd_soc_dai *dai) 204 { 205 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 206 struct avs_dma_data *data; 207 int ret = 0; 208 209 data = snd_soc_dai_get_dma_data(dai, substream); 210 211 switch (cmd) { 212 case SNDRV_PCM_TRIGGER_RESUME: 213 if (rtd->dai_link->ignore_suspend) 214 break; 215 fallthrough; 216 case SNDRV_PCM_TRIGGER_START: 217 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 218 ret = avs_path_pause(data->path); 219 if (ret < 0) { 220 dev_err(dai->dev, "pause BE path failed: %d\n", ret); 221 break; 222 } 223 224 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO); 225 if (ret < 0) 226 dev_err(dai->dev, "run BE path failed: %d\n", ret); 227 break; 228 229 case SNDRV_PCM_TRIGGER_SUSPEND: 230 if (rtd->dai_link->ignore_suspend) 231 break; 232 fallthrough; 233 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 234 case SNDRV_PCM_TRIGGER_STOP: 235 ret = avs_path_pause(data->path); 236 if (ret < 0) 237 dev_err(dai->dev, "pause BE path failed: %d\n", ret); 238 239 ret = avs_path_reset(data->path); 240 if (ret < 0) 241 dev_err(dai->dev, "reset BE path failed: %d\n", ret); 242 break; 243 244 default: 245 ret = -EINVAL; 246 break; 247 } 248 249 return ret; 250 } 251 252 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops = { 253 .startup = avs_dai_startup, 254 .shutdown = avs_dai_shutdown, 255 .hw_params = avs_dai_nonhda_be_hw_params, 256 .hw_free = avs_dai_nonhda_be_hw_free, 257 .prepare = avs_dai_prepare, 258 .trigger = avs_dai_nonhda_be_trigger, 259 }; 260 261 static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 262 { 263 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 264 struct hdac_ext_stream *link_stream; 265 struct avs_dma_data *data; 266 struct hda_codec *codec; 267 int ret; 268 269 ret = avs_dai_startup(substream, dai); 270 if (ret) 271 return ret; 272 273 codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev); 274 link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream, 275 HDAC_EXT_STREAM_TYPE_LINK); 276 if (!link_stream) { 277 avs_dai_shutdown(substream, dai); 278 return -EBUSY; 279 } 280 281 data = snd_soc_dai_get_dma_data(dai, substream); 282 data->link_stream = link_stream; 283 substream->runtime->private_data = link_stream; 284 return 0; 285 } 286 287 static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 288 { 289 struct avs_dma_data *data = snd_soc_dai_get_dma_data(dai, substream); 290 291 snd_hdac_ext_stream_release(data->link_stream, HDAC_EXT_STREAM_TYPE_LINK); 292 substream->runtime->private_data = NULL; 293 avs_dai_shutdown(substream, dai); 294 } 295 296 static int avs_dai_hda_be_hw_params(struct snd_pcm_substream *substream, 297 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai) 298 { 299 struct avs_dma_data *data; 300 301 data = snd_soc_dai_get_dma_data(dai, substream); 302 if (data->path) 303 return 0; 304 305 return avs_dai_be_hw_params(substream, hw_params, dai, 306 hdac_stream(data->link_stream)->stream_tag - 1); 307 } 308 309 static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 310 { 311 struct avs_dma_data *data; 312 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 313 struct hdac_ext_stream *link_stream; 314 struct hdac_ext_link *link; 315 struct hda_codec *codec; 316 317 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name); 318 319 data = snd_soc_dai_get_dma_data(dai, substream); 320 if (!data->path) 321 return 0; 322 323 link_stream = data->link_stream; 324 link_stream->link_prepared = false; 325 avs_path_free(data->path); 326 data->path = NULL; 327 328 /* clear link <-> stream mapping */ 329 codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev); 330 link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr); 331 if (!link) 332 return -EINVAL; 333 334 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 335 snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag); 336 337 return 0; 338 } 339 340 static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 341 { 342 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 343 struct snd_pcm_runtime *runtime = substream->runtime; 344 const struct snd_soc_pcm_stream *stream_info; 345 struct hdac_ext_stream *link_stream; 346 struct hdac_ext_link *link; 347 struct avs_dma_data *data; 348 struct hda_codec *codec; 349 struct hdac_bus *bus; 350 unsigned int format_val; 351 unsigned int bits; 352 int ret; 353 354 data = snd_soc_dai_get_dma_data(dai, substream); 355 link_stream = data->link_stream; 356 357 if (link_stream->link_prepared) 358 return 0; 359 360 codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev); 361 bus = &codec->bus->core; 362 stream_info = snd_soc_dai_get_pcm_stream(dai, substream->stream); 363 bits = snd_hdac_stream_format_bits(runtime->format, runtime->subformat, 364 stream_info->sig_bits); 365 format_val = snd_hdac_stream_format(runtime->channels, bits, runtime->rate); 366 367 snd_hdac_ext_stream_decouple(bus, link_stream, true); 368 snd_hdac_ext_stream_reset(link_stream); 369 snd_hdac_ext_stream_setup(link_stream, format_val); 370 371 link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr); 372 if (!link) 373 return -EINVAL; 374 375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 376 snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag); 377 378 ret = avs_dai_prepare(substream, dai); 379 if (ret) 380 return ret; 381 382 link_stream->link_prepared = true; 383 return 0; 384 } 385 386 static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd, 387 struct snd_soc_dai *dai) 388 { 389 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 390 struct avs_dma_data *data; 391 int ret = 0; 392 393 dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd); 394 395 data = snd_soc_dai_get_dma_data(dai, substream); 396 397 switch (cmd) { 398 case SNDRV_PCM_TRIGGER_RESUME: 399 if (rtd->dai_link->ignore_suspend) 400 break; 401 fallthrough; 402 case SNDRV_PCM_TRIGGER_START: 403 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 404 snd_hdac_ext_stream_start(data->link_stream); 405 406 ret = avs_path_pause(data->path); 407 if (ret < 0) { 408 dev_err(dai->dev, "pause BE path failed: %d\n", ret); 409 break; 410 } 411 412 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO); 413 if (ret < 0) 414 dev_err(dai->dev, "run BE path failed: %d\n", ret); 415 break; 416 417 case SNDRV_PCM_TRIGGER_SUSPEND: 418 if (rtd->dai_link->ignore_suspend) 419 break; 420 fallthrough; 421 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 422 case SNDRV_PCM_TRIGGER_STOP: 423 ret = avs_path_pause(data->path); 424 if (ret < 0) 425 dev_err(dai->dev, "pause BE path failed: %d\n", ret); 426 427 snd_hdac_ext_stream_clear(data->link_stream); 428 429 ret = avs_path_reset(data->path); 430 if (ret < 0) 431 dev_err(dai->dev, "reset BE path failed: %d\n", ret); 432 break; 433 434 default: 435 ret = -EINVAL; 436 break; 437 } 438 439 return ret; 440 } 441 442 static const struct snd_soc_dai_ops avs_dai_hda_be_ops = { 443 .startup = avs_dai_hda_be_startup, 444 .shutdown = avs_dai_hda_be_shutdown, 445 .hw_params = avs_dai_hda_be_hw_params, 446 .hw_free = avs_dai_hda_be_hw_free, 447 .prepare = avs_dai_hda_be_prepare, 448 .trigger = avs_dai_hda_be_trigger, 449 }; 450 451 static int hw_rule_param_size(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) 452 { 453 struct snd_interval *interval = hw_param_interval(params, rule->var); 454 struct snd_interval to; 455 456 snd_interval_any(&to); 457 to.integer = interval->integer; 458 to.max = interval->max; 459 /* 460 * Commonly 2ms buffer size is used in HDA scenarios whereas 4ms is used 461 * when streaming through GPDMA. Align to the latter to account for both. 462 */ 463 to.min = params_rate(params) / 1000 * 4; 464 465 if (rule->var == SNDRV_PCM_HW_PARAM_PERIOD_SIZE) 466 to.min /= params_periods(params); 467 468 return snd_interval_refine(interval, &to); 469 } 470 471 static int avs_pcm_hw_constraints_init(struct snd_pcm_substream *substream) 472 { 473 struct snd_pcm_runtime *runtime = substream->runtime; 474 int ret; 475 476 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 477 if (ret < 0) 478 return ret; 479 480 /* Avoid wrap-around with wall-clock. */ 481 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000); 482 if (ret < 0) 483 return ret; 484 485 /* Adjust buffer and period size based on the audio format. */ 486 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, hw_rule_param_size, NULL, 487 SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS, 488 SNDRV_PCM_HW_PARAM_RATE, -1); 489 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, hw_rule_param_size, NULL, 490 SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS, 491 SNDRV_PCM_HW_PARAM_RATE, -1); 492 493 return ret; 494 } 495 496 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 497 { 498 struct hdac_ext_stream *host_stream; 499 struct avs_dma_data *data; 500 struct hdac_bus *bus; 501 int ret; 502 503 ret = avs_pcm_hw_constraints_init(substream); 504 if (ret) 505 return ret; 506 507 ret = avs_dai_startup(substream, dai); 508 if (ret) 509 return ret; 510 511 data = snd_soc_dai_get_dma_data(dai, substream); 512 bus = &data->adev->base.core; 513 514 host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST); 515 if (!host_stream) { 516 avs_dai_shutdown(substream, dai); 517 return -EBUSY; 518 } 519 520 data->host_stream = host_stream; 521 snd_pcm_set_sync(substream); 522 523 dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p", 524 __func__, hdac_stream(host_stream)->stream_tag, substream); 525 526 return 0; 527 } 528 529 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 530 { 531 struct avs_dma_data *data; 532 533 data = snd_soc_dai_get_dma_data(dai, substream); 534 535 snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST); 536 avs_dai_shutdown(substream, dai); 537 } 538 539 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream, 540 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai) 541 { 542 struct snd_pcm_hw_params *be_hw_params = NULL; 543 struct snd_soc_pcm_runtime *fe, *be; 544 struct snd_soc_dpcm *dpcm; 545 struct avs_dma_data *data; 546 struct hdac_ext_stream *host_stream; 547 int ret; 548 549 data = snd_soc_dai_get_dma_data(dai, substream); 550 if (data->path) 551 return 0; 552 553 host_stream = data->host_stream; 554 555 hdac_stream(host_stream)->bufsize = 0; 556 hdac_stream(host_stream)->period_bytes = 0; 557 hdac_stream(host_stream)->format_val = 0; 558 559 fe = snd_soc_substream_to_rtd(substream); 560 for_each_dpcm_be(fe, substream->stream, dpcm) { 561 be = dpcm->be; 562 be_hw_params = &be->dpcm[substream->stream].hw_params; 563 } 564 565 ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai, 566 hdac_stream(host_stream)->stream_tag - 1); 567 if (ret) 568 goto create_err; 569 570 ret = avs_path_bind(data->path); 571 if (ret < 0) { 572 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret); 573 goto bind_err; 574 } 575 576 return 0; 577 578 bind_err: 579 avs_path_free(data->path); 580 data->path = NULL; 581 create_err: 582 snd_pcm_lib_free_pages(substream); 583 return ret; 584 } 585 586 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 587 { 588 struct avs_dma_data *data; 589 struct hdac_ext_stream *host_stream; 590 int ret; 591 592 dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p", 593 __func__, substream, substream->runtime); 594 595 data = snd_soc_dai_get_dma_data(dai, substream); 596 if (!data->path) 597 return 0; 598 599 host_stream = data->host_stream; 600 601 ret = avs_path_unbind(data->path); 602 if (ret < 0) 603 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret); 604 605 avs_path_free(data->path); 606 data->path = NULL; 607 snd_hdac_stream_cleanup(hdac_stream(host_stream)); 608 hdac_stream(host_stream)->prepared = false; 609 610 return ret; 611 } 612 613 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 614 { 615 int ret; 616 617 ret = __avs_dai_fe_hw_free(substream, dai); 618 snd_pcm_lib_free_pages(substream); 619 620 return ret; 621 } 622 623 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 624 { 625 struct snd_pcm_runtime *runtime = substream->runtime; 626 const struct snd_soc_pcm_stream *stream_info; 627 struct avs_dma_data *data; 628 struct hdac_ext_stream *host_stream; 629 unsigned int format_val; 630 struct hdac_bus *bus; 631 unsigned int bits; 632 int ret; 633 634 data = snd_soc_dai_get_dma_data(dai, substream); 635 host_stream = data->host_stream; 636 637 if (hdac_stream(host_stream)->prepared) 638 return 0; 639 640 bus = hdac_stream(host_stream)->bus; 641 snd_hdac_ext_stream_decouple(bus, data->host_stream, true); 642 snd_hdac_stream_reset(hdac_stream(host_stream)); 643 644 stream_info = snd_soc_dai_get_pcm_stream(dai, substream->stream); 645 bits = snd_hdac_stream_format_bits(runtime->format, runtime->subformat, 646 stream_info->sig_bits); 647 format_val = snd_hdac_stream_format(runtime->channels, bits, runtime->rate); 648 649 ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val); 650 if (ret < 0) 651 return ret; 652 653 ret = snd_hdac_ext_host_stream_setup(host_stream, false); 654 if (ret < 0) 655 return ret; 656 657 ret = avs_dai_prepare(substream, dai); 658 if (ret) 659 return ret; 660 661 hdac_stream(host_stream)->prepared = true; 662 return 0; 663 } 664 665 static void avs_hda_stream_start(struct hdac_bus *bus, struct hdac_ext_stream *host_stream) 666 { 667 struct hdac_stream *first_running = NULL; 668 struct hdac_stream *pos; 669 struct avs_dev *adev = hdac_to_avs(bus); 670 671 list_for_each_entry(pos, &bus->stream_list, list) { 672 if (pos->running) { 673 if (first_running) 674 break; /* more than one running */ 675 first_running = pos; 676 } 677 } 678 679 /* 680 * If host_stream is a CAPTURE stream and will be the only one running, 681 * disable L1SEN to avoid sound clipping. 682 */ 683 if (!first_running) { 684 if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE) 685 avs_hda_l1sen_enable(adev, false); 686 snd_hdac_stream_start(hdac_stream(host_stream)); 687 return; 688 } 689 690 snd_hdac_stream_start(hdac_stream(host_stream)); 691 /* 692 * If host_stream is the first stream to break the rule above, 693 * re-enable L1SEN. 694 */ 695 if (list_entry_is_head(pos, &bus->stream_list, list) && 696 first_running->direction == SNDRV_PCM_STREAM_CAPTURE) 697 avs_hda_l1sen_enable(adev, true); 698 } 699 700 static void avs_hda_stream_stop(struct hdac_bus *bus, struct hdac_ext_stream *host_stream) 701 { 702 struct hdac_stream *first_running = NULL; 703 struct hdac_stream *pos; 704 struct avs_dev *adev = hdac_to_avs(bus); 705 706 list_for_each_entry(pos, &bus->stream_list, list) { 707 if (pos == hdac_stream(host_stream)) 708 continue; /* ignore stream that is about to be stopped */ 709 if (pos->running) { 710 if (first_running) 711 break; /* more than one running */ 712 first_running = pos; 713 } 714 } 715 716 /* 717 * If host_stream is a CAPTURE stream and is the only one running, 718 * re-enable L1SEN. 719 */ 720 if (!first_running) { 721 snd_hdac_stream_stop(hdac_stream(host_stream)); 722 if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE) 723 avs_hda_l1sen_enable(adev, true); 724 return; 725 } 726 727 /* 728 * If by stopping host_stream there is only a single, CAPTURE stream running 729 * left, disable L1SEN to avoid sound clipping. 730 */ 731 if (list_entry_is_head(pos, &bus->stream_list, list) && 732 first_running->direction == SNDRV_PCM_STREAM_CAPTURE) 733 avs_hda_l1sen_enable(adev, false); 734 735 snd_hdac_stream_stop(hdac_stream(host_stream)); 736 } 737 738 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai) 739 { 740 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 741 struct avs_dma_data *data; 742 struct hdac_ext_stream *host_stream; 743 struct hdac_bus *bus; 744 unsigned long flags; 745 int ret = 0; 746 747 data = snd_soc_dai_get_dma_data(dai, substream); 748 host_stream = data->host_stream; 749 bus = hdac_stream(host_stream)->bus; 750 751 switch (cmd) { 752 case SNDRV_PCM_TRIGGER_RESUME: 753 if (rtd->dai_link->ignore_suspend) 754 break; 755 fallthrough; 756 case SNDRV_PCM_TRIGGER_START: 757 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 758 spin_lock_irqsave(&bus->reg_lock, flags); 759 avs_hda_stream_start(bus, host_stream); 760 spin_unlock_irqrestore(&bus->reg_lock, flags); 761 762 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */ 763 if (cmd == SNDRV_PCM_TRIGGER_RESUME) 764 snd_hdac_stream_wait_drsm(hdac_stream(host_stream)); 765 766 ret = avs_path_pause(data->path); 767 if (ret < 0) { 768 dev_err(dai->dev, "pause FE path failed: %d\n", ret); 769 break; 770 } 771 772 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO); 773 if (ret < 0) 774 dev_err(dai->dev, "run FE path failed: %d\n", ret); 775 776 break; 777 778 case SNDRV_PCM_TRIGGER_SUSPEND: 779 if (rtd->dai_link->ignore_suspend) 780 break; 781 fallthrough; 782 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 783 case SNDRV_PCM_TRIGGER_STOP: 784 ret = avs_path_pause(data->path); 785 if (ret < 0) 786 dev_err(dai->dev, "pause FE path failed: %d\n", ret); 787 788 spin_lock_irqsave(&bus->reg_lock, flags); 789 avs_hda_stream_stop(bus, host_stream); 790 spin_unlock_irqrestore(&bus->reg_lock, flags); 791 792 ret = avs_path_reset(data->path); 793 if (ret < 0) 794 dev_err(dai->dev, "reset FE path failed: %d\n", ret); 795 break; 796 797 default: 798 ret = -EINVAL; 799 break; 800 } 801 802 return ret; 803 } 804 805 const struct snd_soc_dai_ops avs_dai_fe_ops = { 806 .startup = avs_dai_fe_startup, 807 .shutdown = avs_dai_fe_shutdown, 808 .hw_params = avs_dai_fe_hw_params, 809 .hw_free = avs_dai_fe_hw_free, 810 .prepare = avs_dai_fe_prepare, 811 .trigger = avs_dai_fe_trigger, 812 }; 813 814 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count, 815 loff_t *ppos) 816 { 817 struct snd_soc_component *component = file->private_data; 818 struct snd_soc_card *card = component->card; 819 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); 820 char buf[64]; 821 size_t len; 822 823 len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix, 824 mach->tplg_filename); 825 826 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 827 } 828 829 static const struct file_operations topology_name_fops = { 830 .open = simple_open, 831 .read = topology_name_read, 832 .llseek = default_llseek, 833 }; 834 835 static int avs_component_load_libraries(struct avs_soc_component *acomp) 836 { 837 struct avs_tplg *tplg = acomp->tplg; 838 struct avs_dev *adev = to_avs_dev(acomp->base.dev); 839 int ret; 840 841 if (!tplg->num_libs) 842 return 0; 843 844 /* Parent device may be asleep and library loading involves IPCs. */ 845 ret = pm_runtime_resume_and_get(adev->dev); 846 if (ret < 0) 847 return ret; 848 849 avs_hda_power_gating_enable(adev, false); 850 avs_hda_clock_gating_enable(adev, false); 851 avs_hda_l1sen_enable(adev, false); 852 853 ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs); 854 855 avs_hda_l1sen_enable(adev, true); 856 avs_hda_clock_gating_enable(adev, true); 857 avs_hda_power_gating_enable(adev, true); 858 859 if (!ret) 860 ret = avs_module_info_init(adev, false); 861 862 pm_runtime_mark_last_busy(adev->dev); 863 pm_runtime_put_autosuspend(adev->dev); 864 865 return ret; 866 } 867 868 static int avs_component_probe(struct snd_soc_component *component) 869 { 870 struct snd_soc_card *card = component->card; 871 struct snd_soc_acpi_mach *mach; 872 struct avs_soc_component *acomp; 873 struct avs_dev *adev; 874 char *filename; 875 int ret; 876 877 dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name); 878 mach = dev_get_platdata(card->dev); 879 acomp = to_avs_soc_component(component); 880 adev = to_avs_dev(component->dev); 881 882 acomp->tplg = avs_tplg_new(component); 883 if (!acomp->tplg) 884 return -ENOMEM; 885 886 if (!mach->tplg_filename) 887 goto finalize; 888 889 /* Load specified topology and create debugfs for it. */ 890 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix, 891 mach->tplg_filename); 892 if (!filename) 893 return -ENOMEM; 894 895 ret = avs_load_topology(component, filename); 896 kfree(filename); 897 if (ret == -ENOENT && !strncmp(mach->tplg_filename, "hda-", 4)) { 898 unsigned int vendor_id; 899 900 if (sscanf(mach->tplg_filename, "hda-%08x-tplg.bin", &vendor_id) != 1) 901 return ret; 902 903 if (((vendor_id >> 16) & 0xFFFF) == 0x8086) 904 mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL, 905 "hda-8086-generic-tplg.bin"); 906 else 907 mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL, 908 "hda-generic-tplg.bin"); 909 910 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix, 911 mach->tplg_filename); 912 if (!filename) 913 return -ENOMEM; 914 915 dev_info(card->dev, "trying to load fallback topology %s\n", mach->tplg_filename); 916 ret = avs_load_topology(component, filename); 917 kfree(filename); 918 } 919 if (ret < 0) 920 return ret; 921 922 ret = avs_component_load_libraries(acomp); 923 if (ret < 0) { 924 dev_err(card->dev, "libraries loading failed: %d\n", ret); 925 goto err_load_libs; 926 } 927 928 finalize: 929 debugfs_create_file("topology_name", 0444, component->debugfs_root, component, 930 &topology_name_fops); 931 932 mutex_lock(&adev->comp_list_mutex); 933 list_add_tail(&acomp->node, &adev->comp_list); 934 mutex_unlock(&adev->comp_list_mutex); 935 936 return 0; 937 938 err_load_libs: 939 avs_remove_topology(component); 940 return ret; 941 } 942 943 static void avs_component_remove(struct snd_soc_component *component) 944 { 945 struct avs_soc_component *acomp = to_avs_soc_component(component); 946 struct snd_soc_acpi_mach *mach; 947 struct avs_dev *adev = to_avs_dev(component->dev); 948 int ret; 949 950 mach = dev_get_platdata(component->card->dev); 951 952 mutex_lock(&adev->comp_list_mutex); 953 list_del(&acomp->node); 954 mutex_unlock(&adev->comp_list_mutex); 955 956 if (mach->tplg_filename) { 957 ret = avs_remove_topology(component); 958 if (ret < 0) 959 dev_err(component->dev, "unload topology failed: %d\n", ret); 960 } 961 } 962 963 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data) 964 { 965 struct snd_pcm_substream *substream; 966 struct snd_soc_pcm_runtime *rtd; 967 int ret; 968 969 substream = data->substream; 970 rtd = snd_soc_substream_to_rtd(substream); 971 972 ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai); 973 if (ret) 974 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret); 975 976 return ret; 977 } 978 979 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data) 980 { 981 struct hdac_ext_stream *host_stream; 982 struct hdac_stream *hstream; 983 struct hdac_bus *bus; 984 int ret; 985 986 host_stream = data->host_stream; 987 hstream = hdac_stream(host_stream); 988 bus = hdac_stream(host_stream)->bus; 989 990 /* Set DRSM before programming stream and position registers. */ 991 snd_hdac_stream_drsm_enable(bus, true, hstream->index); 992 993 ret = dai->driver->ops->prepare(data->substream, dai); 994 if (ret) { 995 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret); 996 return ret; 997 } 998 999 writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL); 1000 writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU); 1001 writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL); 1002 writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU); 1003 1004 /* As per HW spec recommendation, program LPIB and DPIB to the same value. */ 1005 snd_hdac_stream_set_lpib(hstream, hstream->lpib); 1006 snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib); 1007 1008 return 0; 1009 } 1010 1011 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data) 1012 { 1013 int ret; 1014 1015 ret = dai->driver->ops->prepare(data->substream, dai); 1016 if (ret) 1017 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret); 1018 1019 return ret; 1020 } 1021 1022 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data) 1023 { 1024 struct hdac_ext_stream *host_stream; 1025 int ret; 1026 1027 host_stream = data->host_stream; 1028 1029 /* Store position addresses so we can resume from them later on. */ 1030 hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream)); 1031 host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL); 1032 host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU); 1033 host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL); 1034 host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU); 1035 1036 ret = __avs_dai_fe_hw_free(data->substream, dai); 1037 if (ret < 0) 1038 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret); 1039 1040 return ret; 1041 } 1042 1043 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data) 1044 { 1045 int ret; 1046 1047 ret = dai->driver->ops->hw_free(data->substream, dai); 1048 if (ret < 0) 1049 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret); 1050 1051 return ret; 1052 } 1053 1054 static int avs_component_pm_op(struct snd_soc_component *component, bool be, 1055 int (*op)(struct snd_soc_dai *, struct avs_dma_data *)) 1056 { 1057 struct snd_soc_pcm_runtime *rtd; 1058 struct avs_dma_data *data; 1059 struct snd_soc_dai *dai; 1060 int ret; 1061 1062 for_each_component_dais(component, dai) { 1063 data = snd_soc_dai_dma_data_get_playback(dai); 1064 if (data) { 1065 rtd = snd_soc_substream_to_rtd(data->substream); 1066 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) { 1067 ret = op(dai, data); 1068 if (ret < 0) { 1069 __snd_pcm_set_state(data->substream->runtime, 1070 SNDRV_PCM_STATE_DISCONNECTED); 1071 return ret; 1072 } 1073 } 1074 } 1075 1076 data = snd_soc_dai_dma_data_get_capture(dai); 1077 if (data) { 1078 rtd = snd_soc_substream_to_rtd(data->substream); 1079 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) { 1080 ret = op(dai, data); 1081 if (ret < 0) { 1082 __snd_pcm_set_state(data->substream->runtime, 1083 SNDRV_PCM_STATE_DISCONNECTED); 1084 return ret; 1085 } 1086 } 1087 } 1088 } 1089 1090 return 0; 1091 } 1092 1093 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be) 1094 { 1095 return avs_component_pm_op(component, be, &avs_dai_resume_hw_params); 1096 } 1097 1098 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be) 1099 { 1100 int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data); 1101 1102 if (be) 1103 prepare_cb = &avs_dai_resume_be_prepare; 1104 else 1105 prepare_cb = &avs_dai_resume_fe_prepare; 1106 1107 return avs_component_pm_op(component, be, prepare_cb); 1108 } 1109 1110 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be) 1111 { 1112 int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data); 1113 1114 if (be) 1115 hw_free_cb = &avs_dai_suspend_be_hw_free; 1116 else 1117 hw_free_cb = &avs_dai_suspend_fe_hw_free; 1118 1119 return avs_component_pm_op(component, be, hw_free_cb); 1120 } 1121 1122 static int avs_component_suspend(struct snd_soc_component *component) 1123 { 1124 int ret; 1125 1126 /* 1127 * When freeing paths, FEs need to be first as they perform 1128 * path unbinding. 1129 */ 1130 ret = avs_component_suspend_hw_free(component, false); 1131 if (ret) 1132 return ret; 1133 1134 return avs_component_suspend_hw_free(component, true); 1135 } 1136 1137 static int avs_component_resume(struct snd_soc_component *component) 1138 { 1139 int ret; 1140 1141 /* 1142 * When creating paths, FEs need to be last as they perform 1143 * path binding. 1144 */ 1145 ret = avs_component_resume_hw_params(component, true); 1146 if (ret) 1147 return ret; 1148 1149 ret = avs_component_resume_hw_params(component, false); 1150 if (ret) 1151 return ret; 1152 1153 /* It is expected that the LINK stream is prepared first. */ 1154 ret = avs_component_resume_prepare(component, true); 1155 if (ret) 1156 return ret; 1157 1158 return avs_component_resume_prepare(component, false); 1159 } 1160 1161 static const struct snd_pcm_hardware avs_pcm_hardware = { 1162 .info = SNDRV_PCM_INFO_MMAP | 1163 SNDRV_PCM_INFO_MMAP_VALID | 1164 SNDRV_PCM_INFO_INTERLEAVED | 1165 SNDRV_PCM_INFO_PAUSE | 1166 SNDRV_PCM_INFO_RESUME | 1167 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, 1168 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1169 SNDRV_PCM_FMTBIT_S32_LE, 1170 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1171 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1172 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1173 .buffer_bytes_max = AZX_MAX_BUF_SIZE, 1174 .period_bytes_min = 128, 1175 .period_bytes_max = AZX_MAX_BUF_SIZE / 2, 1176 .periods_min = 2, 1177 .periods_max = AZX_MAX_FRAG, 1178 .fifo_size = 0, 1179 }; 1180 1181 static int avs_component_open(struct snd_soc_component *component, 1182 struct snd_pcm_substream *substream) 1183 { 1184 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1185 1186 /* only FE DAI links are handled here */ 1187 if (rtd->dai_link->no_pcm) 1188 return 0; 1189 1190 return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware); 1191 } 1192 1193 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream) 1194 { 1195 return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE + 1196 (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index)); 1197 } 1198 1199 static snd_pcm_uframes_t 1200 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream) 1201 { 1202 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1203 struct avs_dma_data *data; 1204 struct hdac_ext_stream *host_stream; 1205 unsigned int pos; 1206 1207 data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream); 1208 if (!data->host_stream) 1209 return 0; 1210 1211 host_stream = data->host_stream; 1212 pos = avs_hda_stream_dpib_read(host_stream); 1213 1214 if (pos >= hdac_stream(host_stream)->bufsize) 1215 pos = 0; 1216 1217 return bytes_to_frames(substream->runtime, pos); 1218 } 1219 1220 static int avs_component_mmap(struct snd_soc_component *component, 1221 struct snd_pcm_substream *substream, 1222 struct vm_area_struct *vma) 1223 { 1224 return snd_pcm_lib_default_mmap(substream, vma); 1225 } 1226 1227 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024) 1228 1229 static int avs_component_construct(struct snd_soc_component *component, 1230 struct snd_soc_pcm_runtime *rtd) 1231 { 1232 struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0); 1233 struct snd_pcm *pcm = rtd->pcm; 1234 1235 if (dai->driver->playback.channels_min) 1236 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, 1237 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, 1238 MAX_PREALLOC_SIZE); 1239 1240 if (dai->driver->capture.channels_min) 1241 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, 1242 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, 1243 MAX_PREALLOC_SIZE); 1244 1245 return 0; 1246 } 1247 1248 static const struct snd_soc_component_driver avs_component_driver = { 1249 .name = "avs-pcm", 1250 .probe = avs_component_probe, 1251 .remove = avs_component_remove, 1252 .suspend = avs_component_suspend, 1253 .resume = avs_component_resume, 1254 .open = avs_component_open, 1255 .pointer = avs_component_pointer, 1256 .mmap = avs_component_mmap, 1257 .pcm_construct = avs_component_construct, 1258 .module_get_upon_open = 1, /* increment refcount when a pcm is opened */ 1259 .topology_name_prefix = "intel/avs", 1260 }; 1261 1262 int avs_soc_component_register(struct device *dev, const char *name, 1263 const struct snd_soc_component_driver *drv, 1264 struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais) 1265 { 1266 struct avs_soc_component *acomp; 1267 int ret; 1268 1269 acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL); 1270 if (!acomp) 1271 return -ENOMEM; 1272 1273 ret = snd_soc_component_initialize(&acomp->base, drv, dev); 1274 if (ret < 0) 1275 return ret; 1276 1277 /* force name change after ASoC is done with its init */ 1278 acomp->base.name = name; 1279 INIT_LIST_HEAD(&acomp->node); 1280 1281 return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais); 1282 } 1283 1284 static struct snd_soc_dai_driver dmic_cpu_dais[] = { 1285 { 1286 .name = "DMIC Pin", 1287 .ops = &avs_dai_nonhda_be_ops, 1288 .capture = { 1289 .stream_name = "DMIC Rx", 1290 .channels_min = 1, 1291 .channels_max = 4, 1292 .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000, 1293 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 1294 }, 1295 }, 1296 { 1297 .name = "DMIC WoV Pin", 1298 .ops = &avs_dai_nonhda_be_ops, 1299 .capture = { 1300 .stream_name = "DMIC WoV Rx", 1301 .channels_min = 1, 1302 .channels_max = 4, 1303 .rates = SNDRV_PCM_RATE_16000, 1304 .formats = SNDRV_PCM_FMTBIT_S16_LE, 1305 }, 1306 }, 1307 }; 1308 1309 int avs_dmic_platform_register(struct avs_dev *adev, const char *name) 1310 { 1311 return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais, 1312 ARRAY_SIZE(dmic_cpu_dais)); 1313 } 1314 1315 static const struct snd_soc_dai_driver i2s_dai_template = { 1316 .ops = &avs_dai_nonhda_be_ops, 1317 .playback = { 1318 .channels_min = 1, 1319 .channels_max = 8, 1320 .rates = SNDRV_PCM_RATE_8000_192000 | 1321 SNDRV_PCM_RATE_12000 | 1322 SNDRV_PCM_RATE_24000 | 1323 SNDRV_PCM_RATE_128000, 1324 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1325 SNDRV_PCM_FMTBIT_S32_LE, 1326 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1327 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1328 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1329 }, 1330 .capture = { 1331 .channels_min = 1, 1332 .channels_max = 8, 1333 .rates = SNDRV_PCM_RATE_8000_192000 | 1334 SNDRV_PCM_RATE_12000 | 1335 SNDRV_PCM_RATE_24000 | 1336 SNDRV_PCM_RATE_128000, 1337 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1338 SNDRV_PCM_FMTBIT_S32_LE, 1339 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1340 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1341 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1342 }, 1343 }; 1344 1345 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask, 1346 unsigned long *tdms) 1347 { 1348 struct snd_soc_dai_driver *cpus, *dai; 1349 size_t ssp_count, cpu_count; 1350 int i, j; 1351 1352 ssp_count = adev->hw_cfg.i2s_caps.ctrl_count; 1353 1354 cpu_count = 0; 1355 for_each_set_bit(i, &port_mask, ssp_count) 1356 if (!tdms || test_bit(0, &tdms[i])) 1357 cpu_count++; 1358 if (tdms) 1359 for_each_set_bit(i, &port_mask, ssp_count) 1360 cpu_count += hweight_long(tdms[i]); 1361 1362 cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL); 1363 if (!cpus) 1364 return -ENOMEM; 1365 1366 dai = cpus; 1367 for_each_set_bit(i, &port_mask, ssp_count) { 1368 if (!tdms || test_bit(0, &tdms[i])) { 1369 memcpy(dai, &i2s_dai_template, sizeof(*dai)); 1370 1371 dai->name = 1372 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i); 1373 dai->playback.stream_name = 1374 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i); 1375 dai->capture.stream_name = 1376 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i); 1377 1378 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name) 1379 return -ENOMEM; 1380 dai++; 1381 } 1382 } 1383 1384 if (!tdms) 1385 goto plat_register; 1386 1387 for_each_set_bit(i, &port_mask, ssp_count) { 1388 for_each_set_bit(j, &tdms[i], ssp_count) { 1389 memcpy(dai, &i2s_dai_template, sizeof(*dai)); 1390 1391 dai->name = 1392 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j); 1393 dai->playback.stream_name = 1394 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j); 1395 dai->capture.stream_name = 1396 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j); 1397 1398 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name) 1399 return -ENOMEM; 1400 dai++; 1401 } 1402 } 1403 1404 plat_register: 1405 return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count); 1406 } 1407 1408 /* HD-Audio CPU DAI template */ 1409 static const struct snd_soc_dai_driver hda_cpu_dai = { 1410 .ops = &avs_dai_hda_be_ops, 1411 .playback = { 1412 .channels_min = 1, 1413 .channels_max = 8, 1414 .rates = SNDRV_PCM_RATE_8000_192000, 1415 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1416 SNDRV_PCM_FMTBIT_S32_LE, 1417 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1418 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1419 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1420 }, 1421 .capture = { 1422 .channels_min = 1, 1423 .channels_max = 8, 1424 .rates = SNDRV_PCM_RATE_8000_192000, 1425 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1426 SNDRV_PCM_FMTBIT_S32_LE, 1427 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1428 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1429 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1430 }, 1431 }; 1432 1433 static void avs_component_hda_unregister_dais(struct snd_soc_component *component) 1434 { 1435 struct snd_soc_acpi_mach *mach; 1436 struct snd_soc_dai *dai, *save; 1437 struct hda_codec *codec; 1438 char name[32]; 1439 1440 mach = dev_get_platdata(component->card->dev); 1441 codec = mach->pdata; 1442 snprintf(name, sizeof(name), "%s-cpu", dev_name(&codec->core.dev)); 1443 1444 for_each_component_dais_safe(component, dai, save) { 1445 int stream; 1446 1447 if (!strstr(dai->driver->name, name)) 1448 continue; 1449 1450 for_each_pcm_streams(stream) 1451 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream)); 1452 1453 snd_soc_unregister_dai(dai); 1454 } 1455 } 1456 1457 static int avs_component_hda_probe(struct snd_soc_component *component) 1458 { 1459 struct snd_soc_dapm_context *dapm; 1460 struct snd_soc_dai_driver *dais; 1461 struct snd_soc_acpi_mach *mach; 1462 struct hda_codec *codec; 1463 struct hda_pcm *pcm; 1464 const char *cname; 1465 int pcm_count = 0, ret, i; 1466 1467 mach = dev_get_platdata(component->card->dev); 1468 if (!mach) 1469 return -EINVAL; 1470 1471 codec = mach->pdata; 1472 if (list_empty(&codec->pcm_list_head)) 1473 return -EINVAL; 1474 list_for_each_entry(pcm, &codec->pcm_list_head, list) 1475 pcm_count++; 1476 1477 dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais), 1478 GFP_KERNEL); 1479 if (!dais) 1480 return -ENOMEM; 1481 1482 cname = dev_name(&codec->core.dev); 1483 dapm = snd_soc_component_get_dapm(component); 1484 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list); 1485 1486 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) { 1487 struct snd_soc_dai *dai; 1488 1489 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais)); 1490 dais[i].id = i; 1491 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL, 1492 "%s-cpu%d", cname, i); 1493 if (!dais[i].name) { 1494 ret = -ENOMEM; 1495 goto exit; 1496 } 1497 1498 if (pcm->stream[0].substreams) { 1499 dais[i].playback.stream_name = 1500 devm_kasprintf(component->dev, GFP_KERNEL, 1501 "%s-cpu%d Tx", cname, i); 1502 if (!dais[i].playback.stream_name) { 1503 ret = -ENOMEM; 1504 goto exit; 1505 } 1506 1507 if (!hda_codec_is_display(codec)) { 1508 dais[i].playback.formats = pcm->stream[0].formats; 1509 dais[i].playback.subformats = pcm->stream[0].subformats; 1510 dais[i].playback.rates = pcm->stream[0].rates; 1511 dais[i].playback.channels_min = pcm->stream[0].channels_min; 1512 dais[i].playback.channels_max = pcm->stream[0].channels_max; 1513 dais[i].playback.sig_bits = pcm->stream[0].maxbps; 1514 } 1515 } 1516 1517 if (pcm->stream[1].substreams) { 1518 dais[i].capture.stream_name = 1519 devm_kasprintf(component->dev, GFP_KERNEL, 1520 "%s-cpu%d Rx", cname, i); 1521 if (!dais[i].capture.stream_name) { 1522 ret = -ENOMEM; 1523 goto exit; 1524 } 1525 1526 if (!hda_codec_is_display(codec)) { 1527 dais[i].capture.formats = pcm->stream[1].formats; 1528 dais[i].capture.subformats = pcm->stream[1].subformats; 1529 dais[i].capture.rates = pcm->stream[1].rates; 1530 dais[i].capture.channels_min = pcm->stream[1].channels_min; 1531 dais[i].capture.channels_max = pcm->stream[1].channels_max; 1532 dais[i].capture.sig_bits = pcm->stream[1].maxbps; 1533 } 1534 } 1535 1536 dai = snd_soc_register_dai(component, &dais[i], false); 1537 if (!dai) { 1538 dev_err(component->dev, "register dai for %s failed\n", 1539 pcm->name); 1540 ret = -EINVAL; 1541 goto exit; 1542 } 1543 1544 ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1545 if (ret < 0) { 1546 dev_err(component->dev, "create widgets failed: %d\n", 1547 ret); 1548 goto exit; 1549 } 1550 } 1551 1552 ret = avs_component_probe(component); 1553 exit: 1554 if (ret) 1555 avs_component_hda_unregister_dais(component); 1556 1557 return ret; 1558 } 1559 1560 static void avs_component_hda_remove(struct snd_soc_component *component) 1561 { 1562 avs_component_hda_unregister_dais(component); 1563 avs_component_remove(component); 1564 } 1565 1566 static int avs_component_hda_open(struct snd_soc_component *component, 1567 struct snd_pcm_substream *substream) 1568 { 1569 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1570 1571 if (!rtd->dai_link->no_pcm) { 1572 struct snd_pcm_hardware hwparams = avs_pcm_hardware; 1573 struct snd_soc_pcm_runtime *be; 1574 struct snd_soc_dpcm *dpcm; 1575 int dir = substream->stream; 1576 1577 /* 1578 * Support the DPCM reparenting while still fulfilling expectations of HDAudio 1579 * common code - a valid stream pointer at substream->runtime->private_data - 1580 * by having all FEs point to the same private data. 1581 */ 1582 for_each_dpcm_be(rtd, dir, dpcm) { 1583 struct snd_pcm_substream *be_substream; 1584 1585 be = dpcm->be; 1586 if (be->dpcm[dir].users == 1) 1587 break; 1588 1589 be_substream = snd_soc_dpcm_get_substream(be, dir); 1590 substream->runtime->private_data = be_substream->runtime->private_data; 1591 break; 1592 } 1593 1594 /* RESUME unsupported for de-coupled HD-Audio capture. */ 1595 if (dir == SNDRV_PCM_STREAM_CAPTURE) 1596 hwparams.info &= ~SNDRV_PCM_INFO_RESUME; 1597 1598 return snd_soc_set_runtime_hwparams(substream, &hwparams); 1599 } 1600 1601 return 0; 1602 } 1603 1604 static const struct snd_soc_component_driver avs_hda_component_driver = { 1605 .name = "avs-hda-pcm", 1606 .probe = avs_component_hda_probe, 1607 .remove = avs_component_hda_remove, 1608 .suspend = avs_component_suspend, 1609 .resume = avs_component_resume, 1610 .open = avs_component_hda_open, 1611 .pointer = avs_component_pointer, 1612 .mmap = avs_component_mmap, 1613 .pcm_construct = avs_component_construct, 1614 /* 1615 * hda platform component's probe() is dependent on 1616 * codec->pcm_list_head, it needs to be initialized after codec 1617 * component. remove_order is here for completeness sake 1618 */ 1619 .probe_order = SND_SOC_COMP_ORDER_LATE, 1620 .remove_order = SND_SOC_COMP_ORDER_EARLY, 1621 .module_get_upon_open = 1, 1622 .topology_name_prefix = "intel/avs", 1623 }; 1624 1625 int avs_hda_platform_register(struct avs_dev *adev, const char *name) 1626 { 1627 return avs_soc_component_register(adev->dev, name, 1628 &avs_hda_component_driver, NULL, 0); 1629 } 1630