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