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 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 <trace/events/sof.h> 17 #include "sof-of-dev.h" 18 #include "sof-priv.h" 19 #include "sof-audio.h" 20 #include "sof-utils.h" 21 #include "ops.h" 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 = snd_soc_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 /* 42 * sof pcm period elapse work 43 */ 44 static void snd_sof_pcm_period_elapsed_work(struct work_struct *work) 45 { 46 struct snd_sof_pcm_stream *sps = 47 container_of(work, struct snd_sof_pcm_stream, 48 period_elapsed_work); 49 50 snd_pcm_period_elapsed(sps->substream); 51 } 52 53 void snd_sof_pcm_init_elapsed_work(struct work_struct *work) 54 { 55 INIT_WORK(work, snd_sof_pcm_period_elapsed_work); 56 } 57 58 /* 59 * sof pcm period elapse, this could be called at irq thread context. 60 */ 61 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream) 62 { 63 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 64 struct snd_soc_component *component = 65 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 66 struct snd_sof_pcm *spcm; 67 68 spcm = snd_sof_find_spcm_dai(component, rtd); 69 if (!spcm) { 70 dev_err(component->dev, 71 "error: period elapsed for unknown stream!\n"); 72 return; 73 } 74 75 /* 76 * snd_pcm_period_elapsed() can be called in interrupt context 77 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(), 78 * when the PCM is done draining or xrun happened, a STOP IPC will 79 * then be sent and this IPC will hit IPC timeout. 80 * To avoid sending IPC before the previous IPC is handled, we 81 * schedule delayed work here to call the snd_pcm_period_elapsed(). 82 */ 83 schedule_work(&spcm->stream[substream->stream].period_elapsed_work); 84 } 85 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed); 86 87 static int 88 sof_pcm_setup_connected_widgets(struct snd_sof_dev *sdev, struct snd_soc_pcm_runtime *rtd, 89 struct snd_sof_pcm *spcm, struct snd_pcm_hw_params *params, 90 struct snd_sof_platform_stream_params *platform_params, int dir) 91 { 92 struct snd_soc_dai *dai; 93 int ret, j; 94 95 /* query DAPM for list of connected widgets and set them up */ 96 for_each_rtd_cpu_dais(rtd, j, dai) { 97 struct snd_soc_dapm_widget_list *list; 98 99 ret = snd_soc_dapm_dai_get_connected_widgets(dai, dir, &list, 100 dpcm_end_walk_at_be); 101 if (ret < 0) { 102 spcm_err(spcm, dir, "dai %s has no valid %s path\n", 103 dai->name, snd_pcm_direction_name(dir)); 104 return ret; 105 } 106 107 spcm->stream[dir].list = list; 108 109 ret = sof_widget_list_setup(sdev, spcm, params, platform_params, dir); 110 if (ret < 0) { 111 spcm_err(spcm, dir, "Widget list set up failed\n"); 112 spcm->stream[dir].list = NULL; 113 snd_soc_dapm_dai_free_widgets(&list); 114 return ret; 115 } 116 } 117 118 return 0; 119 } 120 121 static int sof_pcm_hw_params(struct snd_soc_component *component, 122 struct snd_pcm_substream *substream, 123 struct snd_pcm_hw_params *params) 124 { 125 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 126 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 127 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 128 struct snd_sof_platform_stream_params platform_params = { 0 }; 129 struct snd_pcm_runtime *runtime = substream->runtime; 130 struct snd_sof_pcm *spcm; 131 int ret; 132 133 /* nothing to do for BE */ 134 if (rtd->dai_link->no_pcm) 135 return 0; 136 137 spcm = snd_sof_find_spcm_dai(component, rtd); 138 if (!spcm) 139 return -EINVAL; 140 141 spcm_dbg(spcm, substream->stream, "Entry: hw_params\n"); 142 143 /* 144 * Handle repeated calls to hw_params() without free_pcm() in 145 * between. At least ALSA OSS emulation depends on this. 146 */ 147 if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) { 148 ret = pcm_ops->hw_free(component, substream); 149 if (ret < 0) 150 return ret; 151 152 spcm->prepared[substream->stream] = false; 153 } 154 155 ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, &platform_params); 156 if (ret < 0) { 157 spcm_err(spcm, substream->stream, "platform hw params failed\n"); 158 return ret; 159 } 160 161 /* if this is a repeated hw_params without hw_free, skip setting up widgets */ 162 if (!spcm->stream[substream->stream].list) { 163 ret = sof_pcm_setup_connected_widgets(sdev, rtd, spcm, params, &platform_params, 164 substream->stream); 165 if (ret < 0) 166 return ret; 167 } 168 169 /* create compressed page table for audio firmware */ 170 if (runtime->buffer_changed) { 171 ret = create_page_table(component, substream, runtime->dma_area, 172 runtime->dma_bytes); 173 174 if (ret < 0) 175 return ret; 176 } 177 178 if (pcm_ops && pcm_ops->hw_params) { 179 ret = pcm_ops->hw_params(component, substream, params, &platform_params); 180 if (ret < 0) 181 return ret; 182 } 183 184 spcm->prepared[substream->stream] = true; 185 186 /* save pcm hw_params */ 187 memcpy(&spcm->params[substream->stream], params, sizeof(*params)); 188 189 return 0; 190 } 191 192 static int sof_pcm_stream_free(struct snd_sof_dev *sdev, 193 struct snd_pcm_substream *substream, 194 struct snd_sof_pcm *spcm, int dir, 195 bool free_widget_list) 196 { 197 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 198 int ret; 199 int err = 0; 200 201 if (spcm->prepared[substream->stream]) { 202 /* stop DMA first if needed */ 203 if (pcm_ops && pcm_ops->platform_stop_during_hw_free) 204 snd_sof_pcm_platform_trigger(sdev, substream, 205 SNDRV_PCM_TRIGGER_STOP); 206 207 /* free PCM in the DSP */ 208 if (pcm_ops && pcm_ops->hw_free) { 209 ret = pcm_ops->hw_free(sdev->component, substream); 210 if (ret < 0) { 211 spcm_err(spcm, substream->stream, 212 "pcm_ops->hw_free failed %d\n", ret); 213 err = ret; 214 } 215 } 216 217 spcm->prepared[substream->stream] = false; 218 spcm->pending_stop[substream->stream] = false; 219 } 220 221 /* reset the DMA */ 222 ret = snd_sof_pcm_platform_hw_free(sdev, substream); 223 if (ret < 0) { 224 spcm_err(spcm, substream->stream, 225 "platform hw free failed %d\n", ret); 226 if (!err) 227 err = ret; 228 } 229 230 /* free widget list */ 231 if (free_widget_list) { 232 ret = sof_widget_list_free(sdev, spcm, dir); 233 if (ret < 0) { 234 spcm_err(spcm, substream->stream, 235 "sof_widget_list_free failed %d\n", ret); 236 if (!err) 237 err = ret; 238 } 239 } 240 241 return err; 242 } 243 244 int sof_pcm_free_all_streams(struct snd_sof_dev *sdev) 245 { 246 struct snd_pcm_substream *substream; 247 struct snd_sof_pcm *spcm; 248 int dir, ret; 249 250 list_for_each_entry(spcm, &sdev->pcm_list, list) { 251 for_each_pcm_streams(dir) { 252 substream = spcm->stream[dir].substream; 253 254 if (!substream || !substream->runtime || 255 spcm->stream[dir].suspend_ignored) 256 continue; 257 258 if (spcm->stream[dir].list) { 259 ret = sof_pcm_stream_free(sdev, substream, spcm, 260 dir, true); 261 if (ret < 0) 262 return ret; 263 } 264 } 265 } 266 267 return 0; 268 } 269 270 static int sof_pcm_hw_free(struct snd_soc_component *component, 271 struct snd_pcm_substream *substream) 272 { 273 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 274 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 275 struct snd_sof_pcm *spcm; 276 int ret; 277 278 /* nothing to do for BE */ 279 if (rtd->dai_link->no_pcm) 280 return 0; 281 282 spcm = snd_sof_find_spcm_dai(component, rtd); 283 if (!spcm) 284 return -EINVAL; 285 286 spcm_dbg(spcm, substream->stream, "Entry: hw_free\n"); 287 288 ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true); 289 290 cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work); 291 292 return ret; 293 } 294 295 static int sof_pcm_prepare(struct snd_soc_component *component, 296 struct snd_pcm_substream *substream) 297 { 298 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 299 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 300 struct snd_sof_pcm *spcm; 301 int ret; 302 303 /* nothing to do for BE */ 304 if (rtd->dai_link->no_pcm) 305 return 0; 306 307 spcm = snd_sof_find_spcm_dai(component, rtd); 308 if (!spcm) 309 return -EINVAL; 310 311 spcm_dbg(spcm, substream->stream, "Entry: prepare\n"); 312 313 if (spcm->prepared[substream->stream]) { 314 if (!spcm->pending_stop[substream->stream]) 315 return 0; 316 317 /* 318 * this case should be reached in case of xruns where we absolutely 319 * want to free-up and reset all PCM/DMA resources 320 */ 321 ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, true); 322 if (ret < 0) 323 return ret; 324 } 325 326 /* set hw_params */ 327 ret = sof_pcm_hw_params(component, 328 substream, &spcm->params[substream->stream]); 329 if (ret < 0) { 330 spcm_err(spcm, substream->stream, 331 "failed to set hw_params after resume\n"); 332 return ret; 333 } 334 335 return 0; 336 } 337 338 /* 339 * FE dai link trigger actions are always executed in non-atomic context because 340 * they involve IPC's. 341 */ 342 static int sof_pcm_trigger(struct snd_soc_component *component, 343 struct snd_pcm_substream *substream, int cmd) 344 { 345 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 346 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 347 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 348 struct snd_sof_pcm *spcm; 349 bool reset_hw_params = false; 350 bool ipc_first = false; 351 int ret = 0; 352 353 /* nothing to do for BE */ 354 if (rtd->dai_link->no_pcm) 355 return 0; 356 357 spcm = snd_sof_find_spcm_dai(component, rtd); 358 if (!spcm) 359 return -EINVAL; 360 361 spcm_dbg(spcm, substream->stream, "Entry: trigger (cmd: %d)\n", cmd); 362 363 spcm->pending_stop[substream->stream] = false; 364 365 switch (cmd) { 366 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 367 ipc_first = true; 368 break; 369 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 370 if (pcm_ops && pcm_ops->ipc_first_on_start) 371 ipc_first = true; 372 break; 373 case SNDRV_PCM_TRIGGER_START: 374 if (spcm->stream[substream->stream].suspend_ignored) { 375 /* 376 * This case will be triggered when INFO_RESUME is 377 * not supported, no need to re-start streams that 378 * remained enabled in D0ix. 379 */ 380 spcm->stream[substream->stream].suspend_ignored = false; 381 return 0; 382 } 383 384 if (pcm_ops && pcm_ops->ipc_first_on_start) 385 ipc_first = true; 386 break; 387 case SNDRV_PCM_TRIGGER_SUSPEND: 388 /* 389 * If DSP D0I3 is allowed during S0iX, set the suspend_ignored flag for 390 * D0I3-compatible streams to keep the firmware pipeline running 391 */ 392 if (pcm_ops && pcm_ops->d0i3_supported_in_s0ix && 393 sdev->system_suspend_target == SOF_SUSPEND_S0IX && 394 spcm->stream[substream->stream].d0i3_compatible) { 395 spcm->stream[substream->stream].suspend_ignored = true; 396 return 0; 397 } 398 399 /* On suspend the DMA must be stopped in DSPless mode */ 400 if (sdev->dspless_mode_selected) 401 reset_hw_params = true; 402 403 fallthrough; 404 case SNDRV_PCM_TRIGGER_STOP: 405 ipc_first = true; 406 if (pcm_ops && pcm_ops->reset_hw_params_during_stop) 407 reset_hw_params = true; 408 break; 409 default: 410 spcm_err(spcm, substream->stream, "Unhandled trigger cmd %d\n", cmd); 411 return -EINVAL; 412 } 413 414 if (!ipc_first) 415 snd_sof_pcm_platform_trigger(sdev, substream, cmd); 416 417 if (pcm_ops && pcm_ops->trigger) 418 ret = pcm_ops->trigger(component, substream, cmd); 419 420 switch (cmd) { 421 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 422 case SNDRV_PCM_TRIGGER_START: 423 /* invoke platform trigger to start DMA only if pcm_ops is successful */ 424 if (ipc_first && !ret) 425 snd_sof_pcm_platform_trigger(sdev, substream, cmd); 426 break; 427 case SNDRV_PCM_TRIGGER_SUSPEND: 428 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 429 case SNDRV_PCM_TRIGGER_STOP: 430 /* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */ 431 if (!pcm_ops || !pcm_ops->platform_stop_during_hw_free) 432 snd_sof_pcm_platform_trigger(sdev, substream, cmd); 433 434 /* 435 * set the pending_stop flag to indicate that pipeline stop has been delayed. 436 * This will be used later to stop the pipelines during prepare when recovering 437 * from xruns. 438 */ 439 if (pcm_ops && pcm_ops->platform_stop_during_hw_free && 440 cmd == SNDRV_PCM_TRIGGER_STOP) 441 spcm->pending_stop[substream->stream] = true; 442 break; 443 default: 444 break; 445 } 446 447 /* free PCM if reset_hw_params is set and the STOP IPC is successful */ 448 if (!ret && reset_hw_params) 449 ret = sof_pcm_stream_free(sdev, substream, spcm, substream->stream, false); 450 451 return ret; 452 } 453 454 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component, 455 struct snd_pcm_substream *substream) 456 { 457 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 458 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 459 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 460 struct snd_sof_pcm *spcm; 461 snd_pcm_uframes_t host, dai; 462 int ret = -EOPNOTSUPP; 463 464 /* nothing to do for BE */ 465 if (rtd->dai_link->no_pcm) 466 return 0; 467 468 if (pcm_ops && pcm_ops->pointer) 469 ret = pcm_ops->pointer(component, substream, &host); 470 471 if (ret != -EOPNOTSUPP) 472 return ret ? ret : host; 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 trace_sof_pcm_pointer_position(sdev, spcm, substream, host, dai); 489 490 return host; 491 } 492 493 static int sof_pcm_open(struct snd_soc_component *component, 494 struct snd_pcm_substream *substream) 495 { 496 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 497 struct snd_pcm_runtime *runtime = substream->runtime; 498 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 499 const struct snd_sof_dsp_ops *ops = sof_ops(sdev); 500 struct snd_sof_pcm *spcm; 501 struct snd_soc_tplg_stream_caps *caps; 502 int ret; 503 504 /* nothing to do for BE */ 505 if (rtd->dai_link->no_pcm) 506 return 0; 507 508 spcm = snd_sof_find_spcm_dai(component, rtd); 509 if (!spcm) 510 return -EINVAL; 511 512 spcm_dbg(spcm, substream->stream, "Entry: open\n"); 513 514 caps = &spcm->pcm.caps[substream->stream]; 515 516 /* set runtime config */ 517 runtime->hw.info = ops->hw_info; /* platform-specific */ 518 519 /* set any runtime constraints based on topology */ 520 runtime->hw.formats = le64_to_cpu(caps->formats); 521 runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min); 522 runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max); 523 runtime->hw.periods_min = le32_to_cpu(caps->periods_min); 524 runtime->hw.periods_max = le32_to_cpu(caps->periods_max); 525 526 /* 527 * caps->buffer_size_min is not used since the 528 * snd_pcm_hardware structure only defines buffer_bytes_max 529 */ 530 runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max); 531 532 /* set wait time - TODO: come from topology */ 533 substream->wait_time = 500; 534 535 spcm->stream[substream->stream].posn.host_posn = 0; 536 spcm->stream[substream->stream].posn.dai_posn = 0; 537 spcm->stream[substream->stream].substream = substream; 538 spcm->prepared[substream->stream] = false; 539 540 ret = snd_sof_pcm_platform_open(sdev, substream); 541 if (ret < 0) { 542 spcm_err(spcm, substream->stream, 543 "platform pcm open failed %d\n", ret); 544 return ret; 545 } 546 547 spcm_dbg(spcm, substream->stream, "period bytes min %zd, max %zd\n", 548 runtime->hw.period_bytes_min, runtime->hw.period_bytes_max); 549 spcm_dbg(spcm, substream->stream, "period count min %d, max %d\n", 550 runtime->hw.periods_min, runtime->hw.periods_max); 551 spcm_dbg(spcm, substream->stream, "buffer bytes max %zd\n", runtime->hw.buffer_bytes_max); 552 553 return 0; 554 } 555 556 static int sof_pcm_close(struct snd_soc_component *component, 557 struct snd_pcm_substream *substream) 558 { 559 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 560 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 561 struct snd_sof_pcm *spcm; 562 int err; 563 564 /* nothing to do for BE */ 565 if (rtd->dai_link->no_pcm) 566 return 0; 567 568 spcm = snd_sof_find_spcm_dai(component, rtd); 569 if (!spcm) 570 return -EINVAL; 571 572 spcm_dbg(spcm, substream->stream, "Entry: close\n"); 573 574 err = snd_sof_pcm_platform_close(sdev, substream); 575 if (err < 0) { 576 spcm_err(spcm, substream->stream, 577 "platform pcm close failed %d\n", err); 578 /* 579 * keep going, no point in preventing the close 580 * from happening 581 */ 582 } 583 584 spcm->stream[substream->stream].substream = NULL; 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(spcm->scomp->dev, "pcm%u (%s): Entry: pcm_construct\n", 612 spcm->pcm.pcm_id, spcm->pcm.pcm_name); 613 614 /* do we need to pre-allocate playback audio buffer pages */ 615 if (!spcm->pcm.playback) 616 goto capture; 617 618 caps = &spcm->pcm.caps[stream]; 619 620 if (!pcm->streams[stream].substream) { 621 spcm_err(spcm, stream, "NULL playback substream!\n"); 622 return -EINVAL; 623 } 624 625 /* pre-allocate playback audio buffer pages */ 626 spcm_dbg(spcm, stream, "allocate %s playback DMA buffer size 0x%x max 0x%x\n", 627 caps->name, caps->buffer_size_min, caps->buffer_size_max); 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 if (!pcm->streams[stream].substream) { 642 spcm_err(spcm, stream, "NULL capture substream!\n"); 643 return -EINVAL; 644 } 645 646 /* pre-allocate capture audio buffer pages */ 647 spcm_dbg(spcm, stream, "allocate %s capture DMA buffer size 0x%x max 0x%x\n", 648 caps->name, caps->buffer_size_min, caps->buffer_size_max); 649 650 snd_pcm_set_managed_buffer(pcm->streams[stream].substream, 651 SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 652 0, le32_to_cpu(caps->buffer_size_max)); 653 654 return 0; 655 } 656 657 /* fixup the BE DAI link to match any values from topology */ 658 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params) 659 { 660 struct snd_interval *rate = hw_param_interval(params, 661 SNDRV_PCM_HW_PARAM_RATE); 662 struct snd_interval *channels = hw_param_interval(params, 663 SNDRV_PCM_HW_PARAM_CHANNELS); 664 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 665 struct snd_soc_component *component = 666 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 667 struct snd_sof_dai *dai = 668 snd_sof_find_dai(component, (char *)rtd->dai_link->name); 669 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 670 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 671 672 /* no topology exists for this BE, try a common configuration */ 673 if (!dai) { 674 dev_warn(component->dev, 675 "warning: no topology found for BE DAI %s config\n", 676 rtd->dai_link->name); 677 678 /* set 48k, stereo, 16bits by default */ 679 rate->min = 48000; 680 rate->max = 48000; 681 682 channels->min = 2; 683 channels->max = 2; 684 685 snd_mask_none(fmt); 686 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE); 687 688 return 0; 689 } 690 691 if (pcm_ops && pcm_ops->dai_link_fixup) 692 return pcm_ops->dai_link_fixup(rtd, params); 693 694 return 0; 695 } 696 EXPORT_SYMBOL(sof_pcm_dai_link_fixup); 697 698 static int sof_pcm_probe(struct snd_soc_component *component) 699 { 700 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 701 struct snd_sof_pdata *plat_data = sdev->pdata; 702 const char *tplg_filename; 703 int ret; 704 705 /* 706 * make sure the device is pm_runtime_active before loading the 707 * topology and initiating IPC or bus transactions 708 */ 709 ret = pm_runtime_resume_and_get(component->dev); 710 if (ret < 0 && ret != -EACCES) 711 return ret; 712 713 /* load the default topology */ 714 sdev->component = component; 715 716 tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL, 717 "%s/%s", 718 plat_data->tplg_filename_prefix, 719 plat_data->tplg_filename); 720 if (!tplg_filename) { 721 ret = -ENOMEM; 722 goto pm_error; 723 } 724 725 ret = snd_sof_load_topology(component, tplg_filename); 726 if (ret < 0) 727 dev_err(component->dev, "error: failed to load DSP topology %d\n", 728 ret); 729 730 pm_error: 731 pm_runtime_mark_last_busy(component->dev); 732 pm_runtime_put_autosuspend(component->dev); 733 734 return ret; 735 } 736 737 static void sof_pcm_remove(struct snd_soc_component *component) 738 { 739 /* remove topology */ 740 snd_soc_tplg_component_remove(component); 741 } 742 743 static int sof_pcm_ack(struct snd_soc_component *component, 744 struct snd_pcm_substream *substream) 745 { 746 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 747 748 return snd_sof_pcm_platform_ack(sdev, substream); 749 } 750 751 static snd_pcm_sframes_t sof_pcm_delay(struct snd_soc_component *component, 752 struct snd_pcm_substream *substream) 753 { 754 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 755 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 756 757 if (pcm_ops && pcm_ops->delay) 758 return pcm_ops->delay(component, substream); 759 760 return 0; 761 } 762 763 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev) 764 { 765 struct snd_soc_component_driver *pd = &sdev->plat_drv; 766 struct snd_sof_pdata *plat_data = sdev->pdata; 767 const char *drv_name; 768 769 if (plat_data->machine) 770 drv_name = plat_data->machine->drv_name; 771 else if (plat_data->of_machine) 772 drv_name = plat_data->of_machine->drv_name; 773 else 774 drv_name = NULL; 775 776 pd->name = "sof-audio-component"; 777 pd->probe = sof_pcm_probe; 778 pd->remove = sof_pcm_remove; 779 pd->open = sof_pcm_open; 780 pd->close = sof_pcm_close; 781 pd->hw_params = sof_pcm_hw_params; 782 pd->prepare = sof_pcm_prepare; 783 pd->hw_free = sof_pcm_hw_free; 784 pd->trigger = sof_pcm_trigger; 785 pd->pointer = sof_pcm_pointer; 786 pd->ack = sof_pcm_ack; 787 pd->delay = sof_pcm_delay; 788 789 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS) 790 pd->compress_ops = &sof_compressed_ops; 791 #endif 792 793 pd->pcm_construct = sof_pcm_new; 794 pd->ignore_machine = drv_name; 795 pd->be_pcm_base = SOF_BE_PCM_BASE; 796 pd->use_dai_pcm_id = true; 797 pd->topology_name_prefix = "sof"; 798 799 /* increment module refcount when a pcm is opened */ 800 pd->module_get_upon_open = 1; 801 802 pd->legacy_dai_naming = 1; 803 804 /* 805 * The fixup is only needed when the DSP is in use as with the DSPless 806 * mode we are directly using the audio interface 807 */ 808 if (!sdev->dspless_mode_selected) 809 pd->be_hw_params_fixup = sof_pcm_dai_link_fixup; 810 } 811