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