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 static const unsigned int rates[] = { 475 8000, 11025, 12000, 16000, 476 22050, 24000, 32000, 44100, 477 48000, 64000, 88200, 96000, 478 128000, 176400, 192000, 479 }; 480 static const struct snd_pcm_hw_constraint_list rate_list = { 481 .count = ARRAY_SIZE(rates), 482 .list = rates, 483 }; 484 int ret; 485 486 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 487 if (ret < 0) 488 return ret; 489 490 /* Avoid wrap-around with wall-clock. */ 491 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000); 492 if (ret < 0) 493 return ret; 494 495 ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &rate_list); 496 if (ret < 0) 497 return ret; 498 499 /* Adjust buffer and period size based on the audio format. */ 500 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, hw_rule_param_size, NULL, 501 SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS, 502 SNDRV_PCM_HW_PARAM_RATE, -1); 503 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, hw_rule_param_size, NULL, 504 SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS, 505 SNDRV_PCM_HW_PARAM_RATE, -1); 506 507 return ret; 508 } 509 510 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 511 { 512 struct hdac_ext_stream *host_stream; 513 struct avs_dma_data *data; 514 struct hdac_bus *bus; 515 int ret; 516 517 ret = avs_pcm_hw_constraints_init(substream); 518 if (ret) 519 return ret; 520 521 ret = avs_dai_startup(substream, dai); 522 if (ret) 523 return ret; 524 525 data = snd_soc_dai_get_dma_data(dai, substream); 526 bus = &data->adev->base.core; 527 528 host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST); 529 if (!host_stream) { 530 avs_dai_shutdown(substream, dai); 531 return -EBUSY; 532 } 533 534 data->host_stream = host_stream; 535 snd_pcm_set_sync(substream); 536 537 dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p", 538 __func__, hdac_stream(host_stream)->stream_tag, substream); 539 540 return 0; 541 } 542 543 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 544 { 545 struct avs_dma_data *data; 546 547 data = snd_soc_dai_get_dma_data(dai, substream); 548 549 snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST); 550 avs_dai_shutdown(substream, dai); 551 } 552 553 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream, 554 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai) 555 { 556 struct snd_pcm_hw_params *be_hw_params = NULL; 557 struct snd_soc_pcm_runtime *fe, *be; 558 struct snd_soc_dpcm *dpcm; 559 struct avs_dma_data *data; 560 struct hdac_ext_stream *host_stream; 561 int ret; 562 563 data = snd_soc_dai_get_dma_data(dai, substream); 564 if (data->path) 565 return 0; 566 567 host_stream = data->host_stream; 568 569 hdac_stream(host_stream)->bufsize = 0; 570 hdac_stream(host_stream)->period_bytes = 0; 571 hdac_stream(host_stream)->format_val = 0; 572 573 fe = snd_soc_substream_to_rtd(substream); 574 for_each_dpcm_be(fe, substream->stream, dpcm) { 575 be = dpcm->be; 576 be_hw_params = &be->dpcm[substream->stream].hw_params; 577 } 578 579 ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai, 580 hdac_stream(host_stream)->stream_tag - 1); 581 if (ret) 582 goto create_err; 583 584 ret = avs_path_bind(data->path); 585 if (ret < 0) { 586 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret); 587 goto bind_err; 588 } 589 590 return 0; 591 592 bind_err: 593 avs_path_free(data->path); 594 data->path = NULL; 595 create_err: 596 snd_pcm_lib_free_pages(substream); 597 return ret; 598 } 599 600 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 601 { 602 struct avs_dma_data *data; 603 struct hdac_ext_stream *host_stream; 604 int ret; 605 606 dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p", 607 __func__, substream, substream->runtime); 608 609 data = snd_soc_dai_get_dma_data(dai, substream); 610 if (!data->path) 611 return 0; 612 613 host_stream = data->host_stream; 614 615 ret = avs_path_unbind(data->path); 616 if (ret < 0) 617 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret); 618 619 avs_path_free(data->path); 620 data->path = NULL; 621 snd_hdac_stream_cleanup(hdac_stream(host_stream)); 622 hdac_stream(host_stream)->prepared = false; 623 624 return ret; 625 } 626 627 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 628 { 629 int ret; 630 631 ret = __avs_dai_fe_hw_free(substream, dai); 632 snd_pcm_lib_free_pages(substream); 633 634 return ret; 635 } 636 637 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) 638 { 639 struct snd_pcm_runtime *runtime = substream->runtime; 640 const struct snd_soc_pcm_stream *stream_info; 641 struct avs_dma_data *data; 642 struct hdac_ext_stream *host_stream; 643 unsigned int format_val; 644 struct hdac_bus *bus; 645 unsigned int bits; 646 int ret; 647 648 data = snd_soc_dai_get_dma_data(dai, substream); 649 host_stream = data->host_stream; 650 651 if (hdac_stream(host_stream)->prepared) 652 return 0; 653 654 bus = hdac_stream(host_stream)->bus; 655 snd_hdac_ext_stream_decouple(bus, data->host_stream, true); 656 snd_hdac_stream_reset(hdac_stream(host_stream)); 657 658 stream_info = snd_soc_dai_get_pcm_stream(dai, substream->stream); 659 bits = snd_hdac_stream_format_bits(runtime->format, runtime->subformat, 660 stream_info->sig_bits); 661 format_val = snd_hdac_stream_format(runtime->channels, bits, runtime->rate); 662 663 ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val); 664 if (ret < 0) 665 return ret; 666 667 ret = snd_hdac_ext_host_stream_setup(host_stream, false); 668 if (ret < 0) 669 return ret; 670 671 ret = avs_dai_prepare(substream, dai); 672 if (ret) 673 return ret; 674 675 hdac_stream(host_stream)->prepared = true; 676 return 0; 677 } 678 679 static void avs_hda_stream_start(struct hdac_bus *bus, struct hdac_ext_stream *host_stream) 680 { 681 struct hdac_stream *first_running = NULL; 682 struct hdac_stream *pos; 683 struct avs_dev *adev = hdac_to_avs(bus); 684 685 list_for_each_entry(pos, &bus->stream_list, list) { 686 if (pos->running) { 687 if (first_running) 688 break; /* more than one running */ 689 first_running = pos; 690 } 691 } 692 693 /* 694 * If host_stream is a CAPTURE stream and will be the only one running, 695 * disable L1SEN to avoid sound clipping. 696 */ 697 if (!first_running) { 698 if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE) 699 avs_hda_l1sen_enable(adev, false); 700 snd_hdac_stream_start(hdac_stream(host_stream)); 701 return; 702 } 703 704 snd_hdac_stream_start(hdac_stream(host_stream)); 705 /* 706 * If host_stream is the first stream to break the rule above, 707 * re-enable L1SEN. 708 */ 709 if (list_entry_is_head(pos, &bus->stream_list, list) && 710 first_running->direction == SNDRV_PCM_STREAM_CAPTURE) 711 avs_hda_l1sen_enable(adev, true); 712 } 713 714 static void avs_hda_stream_stop(struct hdac_bus *bus, struct hdac_ext_stream *host_stream) 715 { 716 struct hdac_stream *first_running = NULL; 717 struct hdac_stream *pos; 718 struct avs_dev *adev = hdac_to_avs(bus); 719 720 list_for_each_entry(pos, &bus->stream_list, list) { 721 if (pos == hdac_stream(host_stream)) 722 continue; /* ignore stream that is about to be stopped */ 723 if (pos->running) { 724 if (first_running) 725 break; /* more than one running */ 726 first_running = pos; 727 } 728 } 729 730 /* 731 * If host_stream is a CAPTURE stream and is the only one running, 732 * re-enable L1SEN. 733 */ 734 if (!first_running) { 735 snd_hdac_stream_stop(hdac_stream(host_stream)); 736 if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE) 737 avs_hda_l1sen_enable(adev, true); 738 return; 739 } 740 741 /* 742 * If by stopping host_stream there is only a single, CAPTURE stream running 743 * left, disable L1SEN to avoid sound clipping. 744 */ 745 if (list_entry_is_head(pos, &bus->stream_list, list) && 746 first_running->direction == SNDRV_PCM_STREAM_CAPTURE) 747 avs_hda_l1sen_enable(adev, false); 748 749 snd_hdac_stream_stop(hdac_stream(host_stream)); 750 } 751 752 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai) 753 { 754 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 755 struct avs_dma_data *data; 756 struct hdac_ext_stream *host_stream; 757 struct hdac_bus *bus; 758 unsigned long flags; 759 int ret = 0; 760 761 data = snd_soc_dai_get_dma_data(dai, substream); 762 host_stream = data->host_stream; 763 bus = hdac_stream(host_stream)->bus; 764 765 switch (cmd) { 766 case SNDRV_PCM_TRIGGER_RESUME: 767 if (rtd->dai_link->ignore_suspend) 768 break; 769 fallthrough; 770 case SNDRV_PCM_TRIGGER_START: 771 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 772 spin_lock_irqsave(&bus->reg_lock, flags); 773 avs_hda_stream_start(bus, host_stream); 774 spin_unlock_irqrestore(&bus->reg_lock, flags); 775 776 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */ 777 if (cmd == SNDRV_PCM_TRIGGER_RESUME) 778 snd_hdac_stream_wait_drsm(hdac_stream(host_stream)); 779 780 ret = avs_path_pause(data->path); 781 if (ret < 0) { 782 dev_err(dai->dev, "pause FE path failed: %d\n", ret); 783 break; 784 } 785 786 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO); 787 if (ret < 0) 788 dev_err(dai->dev, "run FE path failed: %d\n", ret); 789 790 break; 791 792 case SNDRV_PCM_TRIGGER_SUSPEND: 793 if (rtd->dai_link->ignore_suspend) 794 break; 795 fallthrough; 796 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 797 case SNDRV_PCM_TRIGGER_STOP: 798 ret = avs_path_pause(data->path); 799 if (ret < 0) 800 dev_err(dai->dev, "pause FE path failed: %d\n", ret); 801 802 spin_lock_irqsave(&bus->reg_lock, flags); 803 avs_hda_stream_stop(bus, host_stream); 804 spin_unlock_irqrestore(&bus->reg_lock, flags); 805 806 ret = avs_path_reset(data->path); 807 if (ret < 0) 808 dev_err(dai->dev, "reset FE path failed: %d\n", ret); 809 break; 810 811 default: 812 ret = -EINVAL; 813 break; 814 } 815 816 return ret; 817 } 818 819 const struct snd_soc_dai_ops avs_dai_fe_ops = { 820 .startup = avs_dai_fe_startup, 821 .shutdown = avs_dai_fe_shutdown, 822 .hw_params = avs_dai_fe_hw_params, 823 .hw_free = avs_dai_fe_hw_free, 824 .prepare = avs_dai_fe_prepare, 825 .trigger = avs_dai_fe_trigger, 826 }; 827 828 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count, 829 loff_t *ppos) 830 { 831 struct snd_soc_component *component = file->private_data; 832 struct snd_soc_card *card = component->card; 833 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); 834 char buf[64]; 835 size_t len; 836 837 len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix, 838 mach->tplg_filename); 839 840 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 841 } 842 843 static const struct file_operations topology_name_fops = { 844 .open = simple_open, 845 .read = topology_name_read, 846 .llseek = default_llseek, 847 }; 848 849 static int avs_component_load_libraries(struct avs_soc_component *acomp) 850 { 851 struct avs_tplg *tplg = acomp->tplg; 852 struct avs_dev *adev = to_avs_dev(acomp->base.dev); 853 int ret; 854 855 if (!tplg->num_libs) 856 return 0; 857 858 /* Parent device may be asleep and library loading involves IPCs. */ 859 ret = pm_runtime_resume_and_get(adev->dev); 860 if (ret < 0) 861 return ret; 862 863 avs_hda_power_gating_enable(adev, false); 864 avs_hda_clock_gating_enable(adev, false); 865 avs_hda_l1sen_enable(adev, false); 866 867 ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs); 868 869 avs_hda_l1sen_enable(adev, true); 870 avs_hda_clock_gating_enable(adev, true); 871 avs_hda_power_gating_enable(adev, true); 872 873 if (!ret) 874 ret = avs_module_info_init(adev, false); 875 876 pm_runtime_mark_last_busy(adev->dev); 877 pm_runtime_put_autosuspend(adev->dev); 878 879 return ret; 880 } 881 882 static int avs_component_probe(struct snd_soc_component *component) 883 { 884 struct snd_soc_card *card = component->card; 885 struct snd_soc_acpi_mach *mach; 886 struct avs_soc_component *acomp; 887 struct avs_dev *adev; 888 char *filename; 889 int ret; 890 891 dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name); 892 mach = dev_get_platdata(card->dev); 893 acomp = to_avs_soc_component(component); 894 adev = to_avs_dev(component->dev); 895 896 acomp->tplg = avs_tplg_new(component); 897 if (!acomp->tplg) 898 return -ENOMEM; 899 900 if (!mach->tplg_filename) 901 goto finalize; 902 903 /* Load specified topology and create debugfs for it. */ 904 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix, 905 mach->tplg_filename); 906 if (!filename) 907 return -ENOMEM; 908 909 ret = avs_load_topology(component, filename); 910 kfree(filename); 911 if (ret == -ENOENT && !strncmp(mach->tplg_filename, "hda-", 4)) { 912 unsigned int vendor_id; 913 914 if (sscanf(mach->tplg_filename, "hda-%08x-tplg.bin", &vendor_id) != 1) 915 return ret; 916 917 if (((vendor_id >> 16) & 0xFFFF) == 0x8086) 918 mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL, 919 "hda-8086-generic-tplg.bin"); 920 else 921 mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL, 922 "hda-generic-tplg.bin"); 923 924 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix, 925 mach->tplg_filename); 926 if (!filename) 927 return -ENOMEM; 928 929 dev_info(card->dev, "trying to load fallback topology %s\n", mach->tplg_filename); 930 ret = avs_load_topology(component, filename); 931 kfree(filename); 932 } 933 if (ret < 0) 934 return ret; 935 936 ret = avs_component_load_libraries(acomp); 937 if (ret < 0) { 938 dev_err(card->dev, "libraries loading failed: %d\n", ret); 939 goto err_load_libs; 940 } 941 942 finalize: 943 debugfs_create_file("topology_name", 0444, component->debugfs_root, component, 944 &topology_name_fops); 945 946 mutex_lock(&adev->comp_list_mutex); 947 list_add_tail(&acomp->node, &adev->comp_list); 948 mutex_unlock(&adev->comp_list_mutex); 949 950 return 0; 951 952 err_load_libs: 953 avs_remove_topology(component); 954 return ret; 955 } 956 957 static void avs_component_remove(struct snd_soc_component *component) 958 { 959 struct avs_soc_component *acomp = to_avs_soc_component(component); 960 struct snd_soc_acpi_mach *mach; 961 struct avs_dev *adev = to_avs_dev(component->dev); 962 int ret; 963 964 mach = dev_get_platdata(component->card->dev); 965 966 mutex_lock(&adev->comp_list_mutex); 967 list_del(&acomp->node); 968 mutex_unlock(&adev->comp_list_mutex); 969 970 if (mach->tplg_filename) { 971 ret = avs_remove_topology(component); 972 if (ret < 0) 973 dev_err(component->dev, "unload topology failed: %d\n", ret); 974 } 975 } 976 977 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data) 978 { 979 struct snd_pcm_substream *substream; 980 struct snd_soc_pcm_runtime *rtd; 981 int ret; 982 983 substream = data->substream; 984 rtd = snd_soc_substream_to_rtd(substream); 985 986 ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai); 987 if (ret) 988 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret); 989 990 return ret; 991 } 992 993 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data) 994 { 995 struct hdac_ext_stream *host_stream; 996 struct hdac_stream *hstream; 997 struct hdac_bus *bus; 998 int ret; 999 1000 host_stream = data->host_stream; 1001 hstream = hdac_stream(host_stream); 1002 bus = hdac_stream(host_stream)->bus; 1003 1004 /* Set DRSM before programming stream and position registers. */ 1005 snd_hdac_stream_drsm_enable(bus, true, hstream->index); 1006 1007 ret = dai->driver->ops->prepare(data->substream, dai); 1008 if (ret) { 1009 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret); 1010 return ret; 1011 } 1012 1013 writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL); 1014 writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU); 1015 writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL); 1016 writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU); 1017 1018 /* As per HW spec recommendation, program LPIB and DPIB to the same value. */ 1019 snd_hdac_stream_set_lpib(hstream, hstream->lpib); 1020 snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib); 1021 1022 return 0; 1023 } 1024 1025 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data) 1026 { 1027 int ret; 1028 1029 ret = dai->driver->ops->prepare(data->substream, dai); 1030 if (ret) 1031 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret); 1032 1033 return ret; 1034 } 1035 1036 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data) 1037 { 1038 struct hdac_ext_stream *host_stream; 1039 int ret; 1040 1041 host_stream = data->host_stream; 1042 1043 /* Store position addresses so we can resume from them later on. */ 1044 hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream)); 1045 host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL); 1046 host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU); 1047 host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL); 1048 host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU); 1049 1050 ret = __avs_dai_fe_hw_free(data->substream, dai); 1051 if (ret < 0) 1052 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret); 1053 1054 return ret; 1055 } 1056 1057 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data) 1058 { 1059 int ret; 1060 1061 ret = dai->driver->ops->hw_free(data->substream, dai); 1062 if (ret < 0) 1063 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret); 1064 1065 return ret; 1066 } 1067 1068 static int avs_component_pm_op(struct snd_soc_component *component, bool be, 1069 int (*op)(struct snd_soc_dai *, struct avs_dma_data *)) 1070 { 1071 struct snd_soc_pcm_runtime *rtd; 1072 struct avs_dma_data *data; 1073 struct snd_soc_dai *dai; 1074 int ret; 1075 1076 for_each_component_dais(component, dai) { 1077 data = snd_soc_dai_dma_data_get_playback(dai); 1078 if (data) { 1079 rtd = snd_soc_substream_to_rtd(data->substream); 1080 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) { 1081 ret = op(dai, data); 1082 if (ret < 0) { 1083 __snd_pcm_set_state(data->substream->runtime, 1084 SNDRV_PCM_STATE_DISCONNECTED); 1085 return ret; 1086 } 1087 } 1088 } 1089 1090 data = snd_soc_dai_dma_data_get_capture(dai); 1091 if (data) { 1092 rtd = snd_soc_substream_to_rtd(data->substream); 1093 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) { 1094 ret = op(dai, data); 1095 if (ret < 0) { 1096 __snd_pcm_set_state(data->substream->runtime, 1097 SNDRV_PCM_STATE_DISCONNECTED); 1098 return ret; 1099 } 1100 } 1101 } 1102 } 1103 1104 return 0; 1105 } 1106 1107 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be) 1108 { 1109 return avs_component_pm_op(component, be, &avs_dai_resume_hw_params); 1110 } 1111 1112 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be) 1113 { 1114 int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data); 1115 1116 if (be) 1117 prepare_cb = &avs_dai_resume_be_prepare; 1118 else 1119 prepare_cb = &avs_dai_resume_fe_prepare; 1120 1121 return avs_component_pm_op(component, be, prepare_cb); 1122 } 1123 1124 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be) 1125 { 1126 int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data); 1127 1128 if (be) 1129 hw_free_cb = &avs_dai_suspend_be_hw_free; 1130 else 1131 hw_free_cb = &avs_dai_suspend_fe_hw_free; 1132 1133 return avs_component_pm_op(component, be, hw_free_cb); 1134 } 1135 1136 static int avs_component_suspend(struct snd_soc_component *component) 1137 { 1138 int ret; 1139 1140 /* 1141 * When freeing paths, FEs need to be first as they perform 1142 * path unbinding. 1143 */ 1144 ret = avs_component_suspend_hw_free(component, false); 1145 if (ret) 1146 return ret; 1147 1148 return avs_component_suspend_hw_free(component, true); 1149 } 1150 1151 static int avs_component_resume(struct snd_soc_component *component) 1152 { 1153 int ret; 1154 1155 /* 1156 * When creating paths, FEs need to be last as they perform 1157 * path binding. 1158 */ 1159 ret = avs_component_resume_hw_params(component, true); 1160 if (ret) 1161 return ret; 1162 1163 ret = avs_component_resume_hw_params(component, false); 1164 if (ret) 1165 return ret; 1166 1167 /* It is expected that the LINK stream is prepared first. */ 1168 ret = avs_component_resume_prepare(component, true); 1169 if (ret) 1170 return ret; 1171 1172 return avs_component_resume_prepare(component, false); 1173 } 1174 1175 static const struct snd_pcm_hardware avs_pcm_hardware = { 1176 .info = SNDRV_PCM_INFO_MMAP | 1177 SNDRV_PCM_INFO_MMAP_VALID | 1178 SNDRV_PCM_INFO_INTERLEAVED | 1179 SNDRV_PCM_INFO_PAUSE | 1180 SNDRV_PCM_INFO_RESUME | 1181 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, 1182 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1183 SNDRV_PCM_FMTBIT_S32_LE, 1184 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1185 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1186 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1187 .buffer_bytes_max = AZX_MAX_BUF_SIZE, 1188 .period_bytes_min = 128, 1189 .period_bytes_max = AZX_MAX_BUF_SIZE / 2, 1190 .periods_min = 2, 1191 .periods_max = AZX_MAX_FRAG, 1192 .fifo_size = 0, 1193 }; 1194 1195 static int avs_component_open(struct snd_soc_component *component, 1196 struct snd_pcm_substream *substream) 1197 { 1198 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1199 1200 /* only FE DAI links are handled here */ 1201 if (rtd->dai_link->no_pcm) 1202 return 0; 1203 1204 return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware); 1205 } 1206 1207 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream) 1208 { 1209 return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE + 1210 (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index)); 1211 } 1212 1213 static snd_pcm_uframes_t 1214 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream) 1215 { 1216 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1217 struct avs_dma_data *data; 1218 struct hdac_ext_stream *host_stream; 1219 unsigned int pos; 1220 1221 data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream); 1222 if (!data->host_stream) 1223 return 0; 1224 1225 host_stream = data->host_stream; 1226 pos = avs_hda_stream_dpib_read(host_stream); 1227 1228 if (pos >= hdac_stream(host_stream)->bufsize) 1229 pos = 0; 1230 1231 return bytes_to_frames(substream->runtime, pos); 1232 } 1233 1234 static int avs_component_mmap(struct snd_soc_component *component, 1235 struct snd_pcm_substream *substream, 1236 struct vm_area_struct *vma) 1237 { 1238 return snd_pcm_lib_default_mmap(substream, vma); 1239 } 1240 1241 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024) 1242 1243 static int avs_component_construct(struct snd_soc_component *component, 1244 struct snd_soc_pcm_runtime *rtd) 1245 { 1246 struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0); 1247 struct snd_pcm *pcm = rtd->pcm; 1248 1249 if (dai->driver->playback.channels_min) 1250 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, 1251 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, 1252 MAX_PREALLOC_SIZE); 1253 1254 if (dai->driver->capture.channels_min) 1255 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, 1256 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, 1257 MAX_PREALLOC_SIZE); 1258 1259 return 0; 1260 } 1261 1262 static const struct snd_soc_component_driver avs_component_driver = { 1263 .name = "avs-pcm", 1264 .probe = avs_component_probe, 1265 .remove = avs_component_remove, 1266 .suspend = avs_component_suspend, 1267 .resume = avs_component_resume, 1268 .open = avs_component_open, 1269 .pointer = avs_component_pointer, 1270 .mmap = avs_component_mmap, 1271 .pcm_construct = avs_component_construct, 1272 .module_get_upon_open = 1, /* increment refcount when a pcm is opened */ 1273 .topology_name_prefix = "intel/avs", 1274 }; 1275 1276 int avs_soc_component_register(struct device *dev, const char *name, 1277 const struct snd_soc_component_driver *drv, 1278 struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais) 1279 { 1280 struct avs_soc_component *acomp; 1281 int ret; 1282 1283 acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL); 1284 if (!acomp) 1285 return -ENOMEM; 1286 1287 ret = snd_soc_component_initialize(&acomp->base, drv, dev); 1288 if (ret < 0) 1289 return ret; 1290 1291 /* force name change after ASoC is done with its init */ 1292 acomp->base.name = name; 1293 INIT_LIST_HEAD(&acomp->node); 1294 1295 return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais); 1296 } 1297 1298 static struct snd_soc_dai_driver dmic_cpu_dais[] = { 1299 { 1300 .name = "DMIC Pin", 1301 .ops = &avs_dai_nonhda_be_ops, 1302 .capture = { 1303 .stream_name = "DMIC Rx", 1304 .channels_min = 1, 1305 .channels_max = 4, 1306 .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000, 1307 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE, 1308 }, 1309 }, 1310 { 1311 .name = "DMIC WoV Pin", 1312 .ops = &avs_dai_nonhda_be_ops, 1313 .capture = { 1314 .stream_name = "DMIC WoV Rx", 1315 .channels_min = 1, 1316 .channels_max = 4, 1317 .rates = SNDRV_PCM_RATE_16000, 1318 .formats = SNDRV_PCM_FMTBIT_S16_LE, 1319 }, 1320 }, 1321 }; 1322 1323 int avs_dmic_platform_register(struct avs_dev *adev, const char *name) 1324 { 1325 return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais, 1326 ARRAY_SIZE(dmic_cpu_dais)); 1327 } 1328 1329 static const struct snd_soc_dai_driver i2s_dai_template = { 1330 .ops = &avs_dai_nonhda_be_ops, 1331 .playback = { 1332 .channels_min = 1, 1333 .channels_max = 8, 1334 .rates = SNDRV_PCM_RATE_8000_192000 | 1335 SNDRV_PCM_RATE_KNOT, 1336 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1337 SNDRV_PCM_FMTBIT_S32_LE, 1338 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1339 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1340 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1341 }, 1342 .capture = { 1343 .channels_min = 1, 1344 .channels_max = 8, 1345 .rates = SNDRV_PCM_RATE_8000_192000 | 1346 SNDRV_PCM_RATE_KNOT, 1347 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1348 SNDRV_PCM_FMTBIT_S32_LE, 1349 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1350 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1351 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1352 }, 1353 }; 1354 1355 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask, 1356 unsigned long *tdms) 1357 { 1358 struct snd_soc_dai_driver *cpus, *dai; 1359 size_t ssp_count, cpu_count; 1360 int i, j; 1361 1362 ssp_count = adev->hw_cfg.i2s_caps.ctrl_count; 1363 1364 cpu_count = 0; 1365 for_each_set_bit(i, &port_mask, ssp_count) 1366 if (!tdms || test_bit(0, &tdms[i])) 1367 cpu_count++; 1368 if (tdms) 1369 for_each_set_bit(i, &port_mask, ssp_count) 1370 cpu_count += hweight_long(tdms[i]); 1371 1372 cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL); 1373 if (!cpus) 1374 return -ENOMEM; 1375 1376 dai = cpus; 1377 for_each_set_bit(i, &port_mask, ssp_count) { 1378 if (!tdms || test_bit(0, &tdms[i])) { 1379 memcpy(dai, &i2s_dai_template, sizeof(*dai)); 1380 1381 dai->name = 1382 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i); 1383 dai->playback.stream_name = 1384 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i); 1385 dai->capture.stream_name = 1386 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i); 1387 1388 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name) 1389 return -ENOMEM; 1390 dai++; 1391 } 1392 } 1393 1394 if (!tdms) 1395 goto plat_register; 1396 1397 for_each_set_bit(i, &port_mask, ssp_count) { 1398 for_each_set_bit(j, &tdms[i], ssp_count) { 1399 memcpy(dai, &i2s_dai_template, sizeof(*dai)); 1400 1401 dai->name = 1402 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j); 1403 dai->playback.stream_name = 1404 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j); 1405 dai->capture.stream_name = 1406 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j); 1407 1408 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name) 1409 return -ENOMEM; 1410 dai++; 1411 } 1412 } 1413 1414 plat_register: 1415 return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count); 1416 } 1417 1418 /* HD-Audio CPU DAI template */ 1419 static const struct snd_soc_dai_driver hda_cpu_dai = { 1420 .ops = &avs_dai_hda_be_ops, 1421 .playback = { 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 .capture = { 1432 .channels_min = 1, 1433 .channels_max = 8, 1434 .rates = SNDRV_PCM_RATE_8000_192000, 1435 .formats = SNDRV_PCM_FMTBIT_S16_LE | 1436 SNDRV_PCM_FMTBIT_S32_LE, 1437 .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 | 1438 SNDRV_PCM_SUBFMTBIT_MSBITS_24 | 1439 SNDRV_PCM_SUBFMTBIT_MSBITS_MAX, 1440 }, 1441 }; 1442 1443 static void avs_component_hda_unregister_dais(struct snd_soc_component *component) 1444 { 1445 struct snd_soc_acpi_mach *mach; 1446 struct snd_soc_dai *dai, *save; 1447 struct hda_codec *codec; 1448 char name[32]; 1449 1450 mach = dev_get_platdata(component->card->dev); 1451 codec = mach->pdata; 1452 snprintf(name, sizeof(name), "%s-cpu", dev_name(&codec->core.dev)); 1453 1454 for_each_component_dais_safe(component, dai, save) { 1455 int stream; 1456 1457 if (!strstr(dai->driver->name, name)) 1458 continue; 1459 1460 for_each_pcm_streams(stream) 1461 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream)); 1462 1463 snd_soc_unregister_dai(dai); 1464 } 1465 } 1466 1467 static int avs_component_hda_probe(struct snd_soc_component *component) 1468 { 1469 struct snd_soc_dapm_context *dapm; 1470 struct snd_soc_dai_driver *dais; 1471 struct snd_soc_acpi_mach *mach; 1472 struct hda_codec *codec; 1473 struct hda_pcm *pcm; 1474 const char *cname; 1475 int pcm_count = 0, ret, i; 1476 1477 mach = dev_get_platdata(component->card->dev); 1478 if (!mach) 1479 return -EINVAL; 1480 1481 codec = mach->pdata; 1482 if (list_empty(&codec->pcm_list_head)) 1483 return -EINVAL; 1484 list_for_each_entry(pcm, &codec->pcm_list_head, list) 1485 pcm_count++; 1486 1487 dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais), 1488 GFP_KERNEL); 1489 if (!dais) 1490 return -ENOMEM; 1491 1492 cname = dev_name(&codec->core.dev); 1493 dapm = snd_soc_component_get_dapm(component); 1494 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list); 1495 1496 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) { 1497 struct snd_soc_dai *dai; 1498 1499 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais)); 1500 dais[i].id = i; 1501 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL, 1502 "%s-cpu%d", cname, i); 1503 if (!dais[i].name) { 1504 ret = -ENOMEM; 1505 goto exit; 1506 } 1507 1508 if (pcm->stream[0].substreams) { 1509 dais[i].playback.stream_name = 1510 devm_kasprintf(component->dev, GFP_KERNEL, 1511 "%s-cpu%d Tx", cname, i); 1512 if (!dais[i].playback.stream_name) { 1513 ret = -ENOMEM; 1514 goto exit; 1515 } 1516 1517 if (!hda_codec_is_display(codec)) { 1518 dais[i].playback.formats = pcm->stream[0].formats; 1519 dais[i].playback.subformats = pcm->stream[0].subformats; 1520 dais[i].playback.rates = pcm->stream[0].rates; 1521 dais[i].playback.channels_min = pcm->stream[0].channels_min; 1522 dais[i].playback.channels_max = pcm->stream[0].channels_max; 1523 dais[i].playback.sig_bits = pcm->stream[0].maxbps; 1524 } 1525 } 1526 1527 if (pcm->stream[1].substreams) { 1528 dais[i].capture.stream_name = 1529 devm_kasprintf(component->dev, GFP_KERNEL, 1530 "%s-cpu%d Rx", cname, i); 1531 if (!dais[i].capture.stream_name) { 1532 ret = -ENOMEM; 1533 goto exit; 1534 } 1535 1536 if (!hda_codec_is_display(codec)) { 1537 dais[i].capture.formats = pcm->stream[1].formats; 1538 dais[i].capture.subformats = pcm->stream[1].subformats; 1539 dais[i].capture.rates = pcm->stream[1].rates; 1540 dais[i].capture.channels_min = pcm->stream[1].channels_min; 1541 dais[i].capture.channels_max = pcm->stream[1].channels_max; 1542 dais[i].capture.sig_bits = pcm->stream[1].maxbps; 1543 } 1544 } 1545 1546 dai = snd_soc_register_dai(component, &dais[i], false); 1547 if (!dai) { 1548 dev_err(component->dev, "register dai for %s failed\n", 1549 pcm->name); 1550 ret = -EINVAL; 1551 goto exit; 1552 } 1553 1554 ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1555 if (ret < 0) { 1556 dev_err(component->dev, "create widgets failed: %d\n", 1557 ret); 1558 goto exit; 1559 } 1560 } 1561 1562 ret = avs_component_probe(component); 1563 exit: 1564 if (ret) 1565 avs_component_hda_unregister_dais(component); 1566 1567 return ret; 1568 } 1569 1570 static void avs_component_hda_remove(struct snd_soc_component *component) 1571 { 1572 avs_component_hda_unregister_dais(component); 1573 avs_component_remove(component); 1574 } 1575 1576 static int avs_component_hda_open(struct snd_soc_component *component, 1577 struct snd_pcm_substream *substream) 1578 { 1579 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1580 1581 if (!rtd->dai_link->no_pcm) { 1582 struct snd_pcm_hardware hwparams = avs_pcm_hardware; 1583 struct snd_soc_pcm_runtime *be; 1584 struct snd_soc_dpcm *dpcm; 1585 int dir = substream->stream; 1586 1587 /* 1588 * Support the DPCM reparenting while still fulfilling expectations of HDAudio 1589 * common code - a valid stream pointer at substream->runtime->private_data - 1590 * by having all FEs point to the same private data. 1591 */ 1592 for_each_dpcm_be(rtd, dir, dpcm) { 1593 struct snd_pcm_substream *be_substream; 1594 1595 be = dpcm->be; 1596 if (be->dpcm[dir].users == 1) 1597 break; 1598 1599 be_substream = snd_soc_dpcm_get_substream(be, dir); 1600 substream->runtime->private_data = be_substream->runtime->private_data; 1601 break; 1602 } 1603 1604 /* RESUME unsupported for de-coupled HD-Audio capture. */ 1605 if (dir == SNDRV_PCM_STREAM_CAPTURE) 1606 hwparams.info &= ~SNDRV_PCM_INFO_RESUME; 1607 1608 return snd_soc_set_runtime_hwparams(substream, &hwparams); 1609 } 1610 1611 return 0; 1612 } 1613 1614 static const struct snd_soc_component_driver avs_hda_component_driver = { 1615 .name = "avs-hda-pcm", 1616 .probe = avs_component_hda_probe, 1617 .remove = avs_component_hda_remove, 1618 .suspend = avs_component_suspend, 1619 .resume = avs_component_resume, 1620 .open = avs_component_hda_open, 1621 .pointer = avs_component_pointer, 1622 .mmap = avs_component_mmap, 1623 .pcm_construct = avs_component_construct, 1624 /* 1625 * hda platform component's probe() is dependent on 1626 * codec->pcm_list_head, it needs to be initialized after codec 1627 * component. remove_order is here for completeness sake 1628 */ 1629 .probe_order = SND_SOC_COMP_ORDER_LATE, 1630 .remove_order = SND_SOC_COMP_ORDER_EARLY, 1631 .module_get_upon_open = 1, 1632 .topology_name_prefix = "intel/avs", 1633 }; 1634 1635 int avs_hda_platform_register(struct avs_dev *adev, const char *name) 1636 { 1637 return avs_soc_component_register(adev->dev, name, 1638 &avs_hda_component_driver, NULL, 0); 1639 } 1640