1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2018 Intel Corporation. All rights reserved. 7 // 8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9 // 10 // PCM Layer, interface between ALSA and IPC. 11 // 12 13 #include <linux/pm_runtime.h> 14 #include <sound/pcm_params.h> 15 #include <sound/sof.h> 16 #include "sof-priv.h" 17 #include "sof-audio.h" 18 #include "ops.h" 19 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 20 #include "sof-probes.h" 21 #endif 22 23 /* Create DMA buffer page table for DSP */ 24 static int create_page_table(struct snd_soc_component *component, 25 struct snd_pcm_substream *substream, 26 unsigned char *dma_area, size_t size) 27 { 28 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 29 struct snd_sof_pcm *spcm; 30 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream); 31 int stream = substream->stream; 32 33 spcm = snd_sof_find_spcm_dai(component, rtd); 34 if (!spcm) 35 return -EINVAL; 36 37 return snd_sof_create_page_table(component->dev, dmab, 38 spcm->stream[stream].page_table.area, size); 39 } 40 41 static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream, 42 const struct sof_ipc_pcm_params_reply *reply) 43 { 44 struct snd_soc_component *scomp = spcm->scomp; 45 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 46 47 /* validate offset */ 48 int ret = snd_sof_ipc_pcm_params(sdev, substream, reply); 49 50 if (ret < 0) 51 dev_err(scomp->dev, "error: got wrong reply for PCM %d\n", 52 spcm->pcm.pcm_id); 53 54 return ret; 55 } 56 57 /* 58 * sof pcm period elapse work 59 */ 60 static void snd_sof_pcm_period_elapsed_work(struct work_struct *work) 61 { 62 struct snd_sof_pcm_stream *sps = 63 container_of(work, struct snd_sof_pcm_stream, 64 period_elapsed_work); 65 66 snd_pcm_period_elapsed(sps->substream); 67 } 68 69 void snd_sof_pcm_init_elapsed_work(struct work_struct *work) 70 { 71 INIT_WORK(work, snd_sof_pcm_period_elapsed_work); 72 } 73 74 /* 75 * sof pcm period elapse, this could be called at irq thread context. 76 */ 77 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream) 78 { 79 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 80 struct snd_soc_component *component = 81 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 82 struct snd_sof_pcm *spcm; 83 84 spcm = snd_sof_find_spcm_dai(component, rtd); 85 if (!spcm) { 86 dev_err(component->dev, 87 "error: period elapsed for unknown stream!\n"); 88 return; 89 } 90 91 /* 92 * snd_pcm_period_elapsed() can be called in interrupt context 93 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(), 94 * when the PCM is done draining or xrun happened, a STOP IPC will 95 * then be sent and this IPC will hit IPC timeout. 96 * To avoid sending IPC before the previous IPC is handled, we 97 * schedule delayed work here to call the snd_pcm_period_elapsed(). 98 */ 99 schedule_work(&spcm->stream[substream->stream].period_elapsed_work); 100 } 101 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed); 102 103 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev, 104 struct snd_sof_pcm *spcm) 105 { 106 struct sof_ipc_stream stream; 107 struct sof_ipc_reply reply; 108 int ret; 109 110 stream.hdr.size = sizeof(stream); 111 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE; 112 stream.comp_id = spcm->stream[substream->stream].comp_id; 113 114 /* send IPC to the DSP */ 115 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream, 116 sizeof(stream), &reply, sizeof(reply)); 117 if (!ret) 118 spcm->prepared[substream->stream] = false; 119 120 return ret; 121 } 122 123 static int sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, 124 struct snd_soc_pcm_runtime *rtd, 125 struct snd_sof_pcm *spcm, int dir) 126 { 127 struct snd_soc_dai *dai; 128 int ret, j; 129 130 /* query DAPM for list of connected widgets and set them up */ 131 for_each_rtd_cpu_dais(rtd, j, dai) { 132 struct snd_soc_dapm_widget_list *list; 133 134 ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list, 135 dpcm_end_walk_at_be); 136 if (ret < 0) { 137 dev_err(sdev->dev, "error: dai %s has no valid %s path\n", dai->name, 138 dir == SNDRV_PCM_STREAM_PLAYBACK ? "playback" : "capture"); 139 return ret; 140 } 141 142 spcm->stream[dir].list = list; 143 144 ret = sof_widget_list_setup(sdev, spcm, dir); 145 if (ret < 0) { 146 dev_err(sdev->dev, "error: failed widget list set up for pcm %d dir %d\n", 147 spcm->pcm.pcm_id, dir); 148 spcm->stream[dir].list = NULL; 149 snd_soc_dapm_dai_free_widgets(&list); 150 return ret; 151 } 152 } 153 154 return 0; 155 } 156 157 static int sof_pcm_hw_params(struct snd_soc_component *component, 158 struct snd_pcm_substream *substream, 159 struct snd_pcm_hw_params *params) 160 { 161 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 162 struct snd_pcm_runtime *runtime = substream->runtime; 163 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 164 struct snd_sof_pcm *spcm; 165 struct sof_ipc_pcm_params pcm; 166 struct sof_ipc_pcm_params_reply ipc_params_reply; 167 int ret; 168 169 /* nothing to do for BE */ 170 if (rtd->dai_link->no_pcm) 171 return 0; 172 173 spcm = snd_sof_find_spcm_dai(component, rtd); 174 if (!spcm) 175 return -EINVAL; 176 177 /* 178 * Handle repeated calls to hw_params() without free_pcm() in 179 * between. At least ALSA OSS emulation depends on this. 180 */ 181 if (spcm->prepared[substream->stream]) { 182 ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm); 183 if (ret < 0) 184 return ret; 185 } 186 187 dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n", 188 spcm->pcm.pcm_id, substream->stream); 189 190 memset(&pcm, 0, sizeof(pcm)); 191 192 /* create compressed page table for audio firmware */ 193 if (runtime->buffer_changed) { 194 ret = create_page_table(component, substream, runtime->dma_area, 195 runtime->dma_bytes); 196 if (ret < 0) 197 return ret; 198 } 199 200 /* number of pages should be rounded up */ 201 pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes); 202 203 /* set IPC PCM parameters */ 204 pcm.hdr.size = sizeof(pcm); 205 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 206 pcm.comp_id = spcm->stream[substream->stream].comp_id; 207 pcm.params.hdr.size = sizeof(pcm.params); 208 pcm.params.buffer.phy_addr = 209 spcm->stream[substream->stream].page_table.addr; 210 pcm.params.buffer.size = runtime->dma_bytes; 211 pcm.params.direction = substream->stream; 212 pcm.params.sample_valid_bytes = params_width(params) >> 3; 213 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 214 pcm.params.rate = params_rate(params); 215 pcm.params.channels = params_channels(params); 216 pcm.params.host_period_bytes = params_period_bytes(params); 217 218 /* container size */ 219 ret = snd_pcm_format_physical_width(params_format(params)); 220 if (ret < 0) 221 return ret; 222 pcm.params.sample_container_bytes = ret >> 3; 223 224 /* format */ 225 switch (params_format(params)) { 226 case SNDRV_PCM_FORMAT_S16: 227 pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE; 228 break; 229 case SNDRV_PCM_FORMAT_S24: 230 pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE; 231 break; 232 case SNDRV_PCM_FORMAT_S32: 233 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 234 break; 235 case SNDRV_PCM_FORMAT_FLOAT: 236 pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT; 237 break; 238 default: 239 return -EINVAL; 240 } 241 242 /* firmware already configured host stream */ 243 ret = snd_sof_pcm_platform_hw_params(sdev, 244 substream, 245 params, 246 &pcm.params); 247 if (ret < 0) { 248 dev_err(component->dev, "error: platform hw params failed\n"); 249 return ret; 250 } 251 252 dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag); 253 254 /* if this is a repeated hw_params without hw_free, skip setting up widgets */ 255 if (!spcm->stream[substream->stream].list) { 256 ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, substream->stream); 257 if (ret < 0) 258 return ret; 259 } 260 261 /* send hw_params IPC to the DSP */ 262 ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm), 263 &ipc_params_reply, sizeof(ipc_params_reply)); 264 if (ret < 0) { 265 dev_err(component->dev, "error: hw params ipc failed for stream %d\n", 266 pcm.params.stream_tag); 267 return ret; 268 } 269 270 ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply); 271 if (ret < 0) 272 return ret; 273 274 spcm->prepared[substream->stream] = true; 275 276 /* save pcm hw_params */ 277 memcpy(&spcm->params[substream->stream], params, sizeof(*params)); 278 279 return ret; 280 } 281 282 static int sof_pcm_hw_free(struct snd_soc_component *component, 283 struct snd_pcm_substream *substream) 284 { 285 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 286 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 287 struct snd_sof_pcm *spcm; 288 int ret, err = 0; 289 290 /* nothing to do for BE */ 291 if (rtd->dai_link->no_pcm) 292 return 0; 293 294 spcm = snd_sof_find_spcm_dai(component, rtd); 295 if (!spcm) 296 return -EINVAL; 297 298 dev_dbg(component->dev, "pcm: free stream %d dir %d\n", 299 spcm->pcm.pcm_id, substream->stream); 300 301 if (spcm->prepared[substream->stream]) { 302 ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm); 303 if (ret < 0) 304 err = ret; 305 } 306 307 ret = sof_widget_list_free(sdev, spcm, substream->stream); 308 if (ret < 0) 309 err = ret; 310 311 cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work); 312 313 ret = snd_sof_pcm_platform_hw_free(sdev, substream); 314 if (ret < 0) { 315 dev_err(component->dev, "error: platform hw free failed\n"); 316 err = ret; 317 } 318 319 return err; 320 } 321 322 static int sof_pcm_prepare(struct snd_soc_component *component, 323 struct snd_pcm_substream *substream) 324 { 325 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 326 struct snd_sof_pcm *spcm; 327 int ret; 328 329 /* nothing to do for BE */ 330 if (rtd->dai_link->no_pcm) 331 return 0; 332 333 spcm = snd_sof_find_spcm_dai(component, rtd); 334 if (!spcm) 335 return -EINVAL; 336 337 if (spcm->prepared[substream->stream]) 338 return 0; 339 340 dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n", 341 spcm->pcm.pcm_id, substream->stream); 342 343 /* set hw_params */ 344 ret = sof_pcm_hw_params(component, 345 substream, &spcm->params[substream->stream]); 346 if (ret < 0) { 347 dev_err(component->dev, 348 "error: set pcm hw_params after resume\n"); 349 return ret; 350 } 351 352 return 0; 353 } 354 355 /* 356 * FE dai link trigger actions are always executed in non-atomic context because 357 * they involve IPC's. 358 */ 359 static int sof_pcm_trigger(struct snd_soc_component *component, 360 struct snd_pcm_substream *substream, int cmd) 361 { 362 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 363 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 364 struct snd_sof_pcm *spcm; 365 struct sof_ipc_stream stream; 366 struct sof_ipc_reply reply; 367 bool reset_hw_params = false; 368 bool free_widget_list = false; 369 bool ipc_first = false; 370 int ret; 371 372 /* nothing to do for BE */ 373 if (rtd->dai_link->no_pcm) 374 return 0; 375 376 spcm = snd_sof_find_spcm_dai(component, rtd); 377 if (!spcm) 378 return -EINVAL; 379 380 dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n", 381 spcm->pcm.pcm_id, substream->stream, cmd); 382 383 stream.hdr.size = sizeof(stream); 384 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG; 385 stream.comp_id = spcm->stream[substream->stream].comp_id; 386 387 switch (cmd) { 388 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 389 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE; 390 ipc_first = true; 391 break; 392 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 393 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE; 394 break; 395 case SNDRV_PCM_TRIGGER_RESUME: 396 if (spcm->stream[substream->stream].suspend_ignored) { 397 /* 398 * this case will be triggered when INFO_RESUME is 399 * supported, no need to resume streams that remained 400 * enabled in D0ix. 401 */ 402 spcm->stream[substream->stream].suspend_ignored = false; 403 return 0; 404 } 405 406 /* set up hw_params */ 407 ret = sof_pcm_prepare(component, substream); 408 if (ret < 0) { 409 dev_err(component->dev, 410 "error: failed to set up hw_params upon resume\n"); 411 return ret; 412 } 413 414 fallthrough; 415 case SNDRV_PCM_TRIGGER_START: 416 if (spcm->stream[substream->stream].suspend_ignored) { 417 /* 418 * This case will be triggered when INFO_RESUME is 419 * not supported, no need to re-start streams that 420 * remained enabled in D0ix. 421 */ 422 spcm->stream[substream->stream].suspend_ignored = false; 423 return 0; 424 } 425 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START; 426 break; 427 case SNDRV_PCM_TRIGGER_SUSPEND: 428 if (sdev->system_suspend_target == SOF_SUSPEND_S0IX && 429 spcm->stream[substream->stream].d0i3_compatible) { 430 /* 431 * trap the event, not sending trigger stop to 432 * prevent the FW pipelines from being stopped, 433 * and mark the flag to ignore the upcoming DAPM 434 * PM events. 435 */ 436 spcm->stream[substream->stream].suspend_ignored = true; 437 return 0; 438 } 439 free_widget_list = true; 440 fallthrough; 441 case SNDRV_PCM_TRIGGER_STOP: 442 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP; 443 ipc_first = true; 444 reset_hw_params = true; 445 break; 446 default: 447 dev_err(component->dev, "error: unhandled trigger cmd %d\n", 448 cmd); 449 return -EINVAL; 450 } 451 452 /* 453 * DMA and IPC sequence is different for start and stop. Need to send 454 * STOP IPC before stop DMA 455 */ 456 if (!ipc_first) 457 snd_sof_pcm_platform_trigger(sdev, substream, cmd); 458 459 /* send IPC to the DSP */ 460 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream, 461 sizeof(stream), &reply, sizeof(reply)); 462 463 /* need to STOP DMA even if STOP IPC failed */ 464 if (ipc_first) 465 snd_sof_pcm_platform_trigger(sdev, substream, cmd); 466 467 /* free PCM if reset_hw_params is set and the STOP IPC is successful */ 468 if (!ret && reset_hw_params) { 469 ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm); 470 if (ret < 0) 471 return ret; 472 473 /* free widget list only for SUSPEND trigger */ 474 if (free_widget_list) 475 ret = sof_widget_list_free(sdev, spcm, substream->stream); 476 } 477 478 return ret; 479 } 480 481 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component, 482 struct snd_pcm_substream *substream) 483 { 484 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 485 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 486 struct snd_sof_pcm *spcm; 487 snd_pcm_uframes_t host, dai; 488 489 /* nothing to do for BE */ 490 if (rtd->dai_link->no_pcm) 491 return 0; 492 493 /* use dsp ops pointer callback directly if set */ 494 if (sof_ops(sdev)->pcm_pointer) 495 return sof_ops(sdev)->pcm_pointer(sdev, substream); 496 497 spcm = snd_sof_find_spcm_dai(component, rtd); 498 if (!spcm) 499 return -EINVAL; 500 501 /* read position from DSP */ 502 host = bytes_to_frames(substream->runtime, 503 spcm->stream[substream->stream].posn.host_posn); 504 dai = bytes_to_frames(substream->runtime, 505 spcm->stream[substream->stream].posn.dai_posn); 506 507 dev_vdbg(component->dev, 508 "PCM: stream %d dir %d DMA position %lu DAI position %lu\n", 509 spcm->pcm.pcm_id, substream->stream, host, dai); 510 511 return host; 512 } 513 514 static int sof_pcm_open(struct snd_soc_component *component, 515 struct snd_pcm_substream *substream) 516 { 517 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 518 struct snd_pcm_runtime *runtime = substream->runtime; 519 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 520 const struct snd_sof_dsp_ops *ops = sof_ops(sdev); 521 struct snd_sof_pcm *spcm; 522 struct snd_soc_tplg_stream_caps *caps; 523 int ret; 524 525 /* nothing to do for BE */ 526 if (rtd->dai_link->no_pcm) 527 return 0; 528 529 spcm = snd_sof_find_spcm_dai(component, rtd); 530 if (!spcm) 531 return -EINVAL; 532 533 dev_dbg(component->dev, "pcm: open stream %d dir %d\n", 534 spcm->pcm.pcm_id, substream->stream); 535 536 537 caps = &spcm->pcm.caps[substream->stream]; 538 539 /* set runtime config */ 540 runtime->hw.info = ops->hw_info; /* platform-specific */ 541 542 /* set any runtime constraints based on topology */ 543 runtime->hw.formats = le64_to_cpu(caps->formats); 544 runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min); 545 runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max); 546 runtime->hw.periods_min = le32_to_cpu(caps->periods_min); 547 runtime->hw.periods_max = le32_to_cpu(caps->periods_max); 548 549 /* 550 * caps->buffer_size_min is not used since the 551 * snd_pcm_hardware structure only defines buffer_bytes_max 552 */ 553 runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max); 554 555 dev_dbg(component->dev, "period min %zd max %zd bytes\n", 556 runtime->hw.period_bytes_min, 557 runtime->hw.period_bytes_max); 558 dev_dbg(component->dev, "period count %d max %d\n", 559 runtime->hw.periods_min, 560 runtime->hw.periods_max); 561 dev_dbg(component->dev, "buffer max %zd bytes\n", 562 runtime->hw.buffer_bytes_max); 563 564 /* set wait time - TODO: come from topology */ 565 substream->wait_time = 500; 566 567 spcm->stream[substream->stream].posn.host_posn = 0; 568 spcm->stream[substream->stream].posn.dai_posn = 0; 569 spcm->stream[substream->stream].substream = substream; 570 spcm->prepared[substream->stream] = false; 571 572 ret = snd_sof_pcm_platform_open(sdev, substream); 573 if (ret < 0) 574 dev_err(component->dev, "error: pcm open failed %d\n", ret); 575 576 return ret; 577 } 578 579 static int sof_pcm_close(struct snd_soc_component *component, 580 struct snd_pcm_substream *substream) 581 { 582 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 583 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 584 struct snd_sof_pcm *spcm; 585 int err; 586 587 /* nothing to do for BE */ 588 if (rtd->dai_link->no_pcm) 589 return 0; 590 591 spcm = snd_sof_find_spcm_dai(component, rtd); 592 if (!spcm) 593 return -EINVAL; 594 595 dev_dbg(component->dev, "pcm: close stream %d dir %d\n", 596 spcm->pcm.pcm_id, substream->stream); 597 598 err = snd_sof_pcm_platform_close(sdev, substream); 599 if (err < 0) { 600 dev_err(component->dev, "error: pcm close failed %d\n", 601 err); 602 /* 603 * keep going, no point in preventing the close 604 * from happening 605 */ 606 } 607 608 return 0; 609 } 610 611 /* 612 * Pre-allocate playback/capture audio buffer pages. 613 * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free 614 * snd_pcm_lib_preallocate_free_for_all() is called by the core. 615 */ 616 static int sof_pcm_new(struct snd_soc_component *component, 617 struct snd_soc_pcm_runtime *rtd) 618 { 619 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 620 struct snd_sof_pcm *spcm; 621 struct snd_pcm *pcm = rtd->pcm; 622 struct snd_soc_tplg_stream_caps *caps; 623 int stream = SNDRV_PCM_STREAM_PLAYBACK; 624 625 /* find SOF PCM for this RTD */ 626 spcm = snd_sof_find_spcm_dai(component, rtd); 627 if (!spcm) { 628 dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n", 629 rtd->dai_link->id); 630 return 0; 631 } 632 633 dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name); 634 635 /* do we need to pre-allocate playback audio buffer pages */ 636 if (!spcm->pcm.playback) 637 goto capture; 638 639 caps = &spcm->pcm.caps[stream]; 640 641 /* pre-allocate playback audio buffer pages */ 642 dev_dbg(component->dev, 643 "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n", 644 caps->name, caps->buffer_size_min, caps->buffer_size_max); 645 646 if (!pcm->streams[stream].substream) { 647 dev_err(component->dev, "error: NULL playback substream!\n"); 648 return -EINVAL; 649 } 650 651 snd_pcm_set_managed_buffer(pcm->streams[stream].substream, 652 SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 653 0, le32_to_cpu(caps->buffer_size_max)); 654 capture: 655 stream = SNDRV_PCM_STREAM_CAPTURE; 656 657 /* do we need to pre-allocate capture audio buffer pages */ 658 if (!spcm->pcm.capture) 659 return 0; 660 661 caps = &spcm->pcm.caps[stream]; 662 663 /* pre-allocate capture audio buffer pages */ 664 dev_dbg(component->dev, 665 "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n", 666 caps->name, caps->buffer_size_min, caps->buffer_size_max); 667 668 if (!pcm->streams[stream].substream) { 669 dev_err(component->dev, "error: NULL capture substream!\n"); 670 return -EINVAL; 671 } 672 673 snd_pcm_set_managed_buffer(pcm->streams[stream].substream, 674 SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 675 0, le32_to_cpu(caps->buffer_size_max)); 676 677 return 0; 678 } 679 680 static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name, 681 struct snd_pcm_hw_params *params) 682 { 683 struct sof_ipc_dai_config *config; 684 struct snd_sof_dai *dai; 685 int i; 686 687 /* 688 * Search for all matching DAIs as we can have both playback and capture DAI 689 * associated with the same link. 690 */ 691 list_for_each_entry(dai, &sdev->dai_list, list) { 692 if (!dai->name || strcmp(link_name, dai->name)) 693 continue; 694 for (i = 0; i < dai->number_configs; i++) { 695 config = &dai->dai_config[i]; 696 if (config->ssp.fsync_rate == params_rate(params)) { 697 dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i); 698 dai->current_config = i; 699 break; 700 } 701 } 702 } 703 } 704 705 /* fixup the BE DAI link to match any values from topology */ 706 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params) 707 { 708 struct snd_interval *rate = hw_param_interval(params, 709 SNDRV_PCM_HW_PARAM_RATE); 710 struct snd_interval *channels = hw_param_interval(params, 711 SNDRV_PCM_HW_PARAM_CHANNELS); 712 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 713 struct snd_soc_component *component = 714 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 715 struct snd_sof_dai *dai = 716 snd_sof_find_dai(component, (char *)rtd->dai_link->name); 717 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 718 struct snd_soc_dpcm *dpcm; 719 720 /* no topology exists for this BE, try a common configuration */ 721 if (!dai) { 722 dev_warn(component->dev, 723 "warning: no topology found for BE DAI %s config\n", 724 rtd->dai_link->name); 725 726 /* set 48k, stereo, 16bits by default */ 727 rate->min = 48000; 728 rate->max = 48000; 729 730 channels->min = 2; 731 channels->max = 2; 732 733 snd_mask_none(fmt); 734 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 735 736 return 0; 737 } 738 739 /* read format from topology */ 740 snd_mask_none(fmt); 741 742 switch (dai->comp_dai.config.frame_fmt) { 743 case SOF_IPC_FRAME_S16_LE: 744 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 745 break; 746 case SOF_IPC_FRAME_S24_4LE: 747 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 748 break; 749 case SOF_IPC_FRAME_S32_LE: 750 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE); 751 break; 752 default: 753 dev_err(component->dev, "error: No available DAI format!\n"); 754 return -EINVAL; 755 } 756 757 /* read rate and channels from topology */ 758 switch (dai->dai_config->type) { 759 case SOF_DAI_INTEL_SSP: 760 /* search for config to pcm params match, if not found use default */ 761 ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params); 762 763 rate->min = dai->dai_config[dai->current_config].ssp.fsync_rate; 764 rate->max = dai->dai_config[dai->current_config].ssp.fsync_rate; 765 channels->min = dai->dai_config[dai->current_config].ssp.tdm_slots; 766 channels->max = dai->dai_config[dai->current_config].ssp.tdm_slots; 767 768 dev_dbg(component->dev, 769 "rate_min: %d rate_max: %d\n", rate->min, rate->max); 770 dev_dbg(component->dev, 771 "channels_min: %d channels_max: %d\n", 772 channels->min, channels->max); 773 774 break; 775 case SOF_DAI_INTEL_DMIC: 776 /* DMIC only supports 16 or 32 bit formats */ 777 if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) { 778 dev_err(component->dev, 779 "error: invalid fmt %d for DAI type %d\n", 780 dai->comp_dai.config.frame_fmt, 781 dai->dai_config->type); 782 } 783 break; 784 case SOF_DAI_INTEL_HDA: 785 /* 786 * HDAudio does not follow the default trigger 787 * sequence due to firmware implementation 788 */ 789 for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) { 790 struct snd_soc_pcm_runtime *fe = dpcm->fe; 791 792 fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] = 793 SND_SOC_DPCM_TRIGGER_POST; 794 } 795 break; 796 case SOF_DAI_INTEL_ALH: 797 /* 798 * Dai could run with different channel count compared with 799 * front end, so get dai channel count from topology 800 */ 801 channels->min = dai->dai_config->alh.channels; 802 channels->max = dai->dai_config->alh.channels; 803 break; 804 case SOF_DAI_IMX_ESAI: 805 rate->min = dai->dai_config->esai.fsync_rate; 806 rate->max = dai->dai_config->esai.fsync_rate; 807 channels->min = dai->dai_config->esai.tdm_slots; 808 channels->max = dai->dai_config->esai.tdm_slots; 809 810 dev_dbg(component->dev, 811 "rate_min: %d rate_max: %d\n", rate->min, rate->max); 812 dev_dbg(component->dev, 813 "channels_min: %d channels_max: %d\n", 814 channels->min, channels->max); 815 break; 816 case SOF_DAI_MEDIATEK_AFE: 817 rate->min = dai->dai_config->afe.rate; 818 rate->max = dai->dai_config->afe.rate; 819 channels->min = dai->dai_config->afe.channels; 820 channels->max = dai->dai_config->afe.channels; 821 822 dev_dbg(component->dev, 823 "rate_min: %d rate_max: %d\n", rate->min, rate->max); 824 dev_dbg(component->dev, 825 "channels_min: %d channels_max: %d\n", 826 channels->min, channels->max); 827 break; 828 case SOF_DAI_IMX_SAI: 829 rate->min = dai->dai_config->sai.fsync_rate; 830 rate->max = dai->dai_config->sai.fsync_rate; 831 channels->min = dai->dai_config->sai.tdm_slots; 832 channels->max = dai->dai_config->sai.tdm_slots; 833 834 dev_dbg(component->dev, 835 "rate_min: %d rate_max: %d\n", rate->min, rate->max); 836 dev_dbg(component->dev, 837 "channels_min: %d channels_max: %d\n", 838 channels->min, channels->max); 839 break; 840 case SOF_DAI_AMD_BT: 841 rate->min = dai->dai_config->acpbt.fsync_rate; 842 rate->max = dai->dai_config->acpbt.fsync_rate; 843 channels->min = dai->dai_config->acpbt.tdm_slots; 844 channels->max = dai->dai_config->acpbt.tdm_slots; 845 846 dev_dbg(component->dev, 847 "AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max); 848 dev_dbg(component->dev, 849 "AMD_BT channels_min: %d channels_max: %d\n", 850 channels->min, channels->max); 851 break; 852 case SOF_DAI_AMD_SP: 853 rate->min = dai->dai_config->acpsp.fsync_rate; 854 rate->max = dai->dai_config->acpsp.fsync_rate; 855 channels->min = dai->dai_config->acpsp.tdm_slots; 856 channels->max = dai->dai_config->acpsp.tdm_slots; 857 858 dev_dbg(component->dev, 859 "AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max); 860 dev_dbg(component->dev, 861 "AMD_SP channels_min: %d channels_max: %d\n", 862 channels->min, channels->max); 863 break; 864 case SOF_DAI_AMD_DMIC: 865 rate->min = dai->dai_config->acpdmic.fsync_rate; 866 rate->max = dai->dai_config->acpdmic.fsync_rate; 867 channels->min = dai->dai_config->acpdmic.tdm_slots; 868 channels->max = dai->dai_config->acpdmic.tdm_slots; 869 870 dev_dbg(component->dev, 871 "AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max); 872 dev_dbg(component->dev, 873 "AMD_DMIC channels_min: %d channels_max: %d\n", 874 channels->min, channels->max); 875 break; 876 default: 877 dev_err(component->dev, "error: invalid DAI type %d\n", 878 dai->dai_config->type); 879 break; 880 } 881 882 return 0; 883 } 884 EXPORT_SYMBOL(sof_pcm_dai_link_fixup); 885 886 static int sof_pcm_probe(struct snd_soc_component *component) 887 { 888 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 889 struct snd_sof_pdata *plat_data = sdev->pdata; 890 const char *tplg_filename; 891 int ret; 892 893 /* load the default topology */ 894 sdev->component = component; 895 896 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 897 "%s/%s", 898 plat_data->tplg_filename_prefix, 899 plat_data->tplg_filename); 900 if (!tplg_filename) 901 return -ENOMEM; 902 903 ret = snd_sof_load_topology(component, tplg_filename); 904 if (ret < 0) { 905 dev_err(component->dev, "error: failed to load DSP topology %d\n", 906 ret); 907 return ret; 908 } 909 910 return ret; 911 } 912 913 static void sof_pcm_remove(struct snd_soc_component *component) 914 { 915 /* remove topology */ 916 snd_soc_tplg_component_remove(component); 917 } 918 919 static int sof_pcm_ack(struct snd_soc_component *component, 920 struct snd_pcm_substream *substream) 921 { 922 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 923 924 return snd_sof_pcm_platform_ack(sdev, substream); 925 } 926 927 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev) 928 { 929 struct snd_soc_component_driver *pd = &sdev->plat_drv; 930 struct snd_sof_pdata *plat_data = sdev->pdata; 931 const char *drv_name; 932 933 drv_name = plat_data->machine->drv_name; 934 935 pd->name = "sof-audio-component"; 936 pd->probe = sof_pcm_probe; 937 pd->remove = sof_pcm_remove; 938 pd->open = sof_pcm_open; 939 pd->close = sof_pcm_close; 940 pd->hw_params = sof_pcm_hw_params; 941 pd->prepare = sof_pcm_prepare; 942 pd->hw_free = sof_pcm_hw_free; 943 pd->trigger = sof_pcm_trigger; 944 pd->pointer = sof_pcm_pointer; 945 pd->ack = sof_pcm_ack; 946 947 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES) 948 pd->compress_ops = &sof_probe_compressed_ops; 949 #endif 950 pd->pcm_construct = sof_pcm_new; 951 pd->ignore_machine = drv_name; 952 pd->be_hw_params_fixup = sof_pcm_dai_link_fixup; 953 pd->be_pcm_base = SOF_BE_PCM_BASE; 954 pd->use_dai_pcm_id = true; 955 pd->topology_name_prefix = "sof"; 956 957 /* increment module refcount when a pcm is opened */ 958 pd->module_get_upon_open = 1; 959 } 960