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