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) 2019 Intel Corporation. All rights reserved. 7 // 8 // Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> 9 // 10 11 #include <linux/bitfield.h> 12 #include "sof-audio.h" 13 #include "ops.h" 14 15 static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget) 16 { 17 struct snd_sof_route *sroute; 18 19 list_for_each_entry(sroute, &sdev->route_list, list) 20 if (sroute->src_widget == widget || sroute->sink_widget == widget) 21 sroute->setup = false; 22 } 23 24 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 25 { 26 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 27 int err = 0; 28 int ret; 29 30 if (!swidget->private) 31 return 0; 32 33 /* only free when use_count is 0 */ 34 if (--swidget->use_count) 35 return 0; 36 37 /* continue to disable core even if IPC fails */ 38 if (tplg_ops->widget_free) 39 err = tplg_ops->widget_free(sdev, swidget); 40 41 /* 42 * disable widget core. continue to route setup status and complete flag 43 * even if this fails and return the appropriate error 44 */ 45 ret = snd_sof_dsp_core_put(sdev, swidget->core); 46 if (ret < 0) { 47 dev_err(sdev->dev, "error: failed to disable target core: %d for widget %s\n", 48 swidget->core, swidget->widget->name); 49 if (!err) 50 err = ret; 51 } 52 53 /* reset route setup status for all routes that contain this widget */ 54 sof_reset_route_setup_status(sdev, swidget); 55 swidget->complete = 0; 56 57 /* 58 * free the scheduler widget (same as pipe_widget) associated with the current swidget. 59 * skip for static pipelines 60 */ 61 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) { 62 ret = sof_widget_free(sdev, swidget->pipe_widget); 63 if (ret < 0 && !err) 64 err = ret; 65 } 66 67 if (!err) 68 dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name); 69 70 return err; 71 } 72 EXPORT_SYMBOL(sof_widget_free); 73 74 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 75 { 76 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 77 int ret; 78 79 /* skip if there is no private data */ 80 if (!swidget->private) 81 return 0; 82 83 /* widget already set up */ 84 if (++swidget->use_count > 1) 85 return 0; 86 87 /* 88 * The scheduler widget for a pipeline is not part of the connected DAPM 89 * widget list and it needs to be set up before the widgets in the pipeline 90 * are set up. The use_count for the scheduler widget is incremented for every 91 * widget in a given pipeline to ensure that it is freed only after the last 92 * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines. 93 */ 94 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) { 95 if (!swidget->pipe_widget) { 96 dev_err(sdev->dev, "No scheduler widget set for %s\n", 97 swidget->widget->name); 98 ret = -EINVAL; 99 goto use_count_dec; 100 } 101 102 ret = sof_widget_setup(sdev, swidget->pipe_widget); 103 if (ret < 0) 104 goto use_count_dec; 105 } 106 107 /* enable widget core */ 108 ret = snd_sof_dsp_core_get(sdev, swidget->core); 109 if (ret < 0) { 110 dev_err(sdev->dev, "error: failed to enable target core for widget %s\n", 111 swidget->widget->name); 112 goto pipe_widget_free; 113 } 114 115 /* setup widget in the DSP */ 116 if (tplg_ops->widget_setup) { 117 ret = tplg_ops->widget_setup(sdev, swidget); 118 if (ret < 0) 119 goto core_put; 120 } 121 122 /* send config for DAI components */ 123 if (WIDGET_IS_DAI(swidget->id)) { 124 unsigned int flags = SOF_DAI_CONFIG_FLAGS_NONE; 125 126 if (tplg_ops->dai_config) { 127 ret = tplg_ops->dai_config(sdev, swidget, flags, NULL); 128 if (ret < 0) 129 goto widget_free; 130 } 131 } 132 133 /* restore kcontrols for widget */ 134 if (tplg_ops->control->widget_kcontrol_setup) { 135 ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget); 136 if (ret < 0) 137 goto widget_free; 138 } 139 140 dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name); 141 142 return 0; 143 144 widget_free: 145 /* widget use_count and core ref_count will both be decremented by sof_widget_free() */ 146 sof_widget_free(sdev, swidget); 147 core_put: 148 snd_sof_dsp_core_put(sdev, swidget->core); 149 pipe_widget_free: 150 if (swidget->id != snd_soc_dapm_scheduler) 151 sof_widget_free(sdev, swidget->pipe_widget); 152 use_count_dec: 153 swidget->use_count--; 154 return ret; 155 } 156 EXPORT_SYMBOL(sof_widget_setup); 157 158 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource, 159 struct snd_soc_dapm_widget *wsink) 160 { 161 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg; 162 struct snd_sof_widget *src_widget = wsource->dobj.private; 163 struct snd_sof_widget *sink_widget = wsink->dobj.private; 164 struct snd_sof_route *sroute; 165 bool route_found = false; 166 int ret; 167 168 /* ignore routes involving virtual widgets in topology */ 169 switch (src_widget->id) { 170 case snd_soc_dapm_out_drv: 171 case snd_soc_dapm_output: 172 case snd_soc_dapm_input: 173 return 0; 174 default: 175 break; 176 } 177 178 switch (sink_widget->id) { 179 case snd_soc_dapm_out_drv: 180 case snd_soc_dapm_output: 181 case snd_soc_dapm_input: 182 return 0; 183 default: 184 break; 185 } 186 187 /* find route matching source and sink widgets */ 188 list_for_each_entry(sroute, &sdev->route_list, list) 189 if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) { 190 route_found = true; 191 break; 192 } 193 194 if (!route_found) { 195 dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n", 196 wsource->name, wsink->name); 197 return -EINVAL; 198 } 199 200 /* nothing to do if route is already set up */ 201 if (sroute->setup) 202 return 0; 203 204 ret = ipc_tplg_ops->route_setup(sdev, sroute); 205 if (ret < 0) 206 return ret; 207 208 sroute->setup = true; 209 return 0; 210 } 211 212 static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev, 213 struct snd_soc_dapm_widget_list *list, int dir) 214 { 215 struct snd_soc_dapm_widget *widget; 216 struct snd_soc_dapm_path *p; 217 int ret; 218 int i; 219 220 /* 221 * Set up connections between widgets in the sink/source paths based on direction. 222 * Some non-SOF widgets exist in topology either for compatibility or for the 223 * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM 224 * events. But they are not handled by the firmware. So ignore them. 225 */ 226 if (dir == SNDRV_PCM_STREAM_PLAYBACK) { 227 for_each_dapm_widgets(list, i, widget) { 228 if (!widget->dobj.private) 229 continue; 230 231 snd_soc_dapm_widget_for_each_sink_path(widget, p) 232 if (p->sink->dobj.private) { 233 ret = sof_route_setup(sdev, widget, p->sink); 234 if (ret < 0) 235 return ret; 236 } 237 } 238 } else { 239 for_each_dapm_widgets(list, i, widget) { 240 if (!widget->dobj.private) 241 continue; 242 243 snd_soc_dapm_widget_for_each_source_path(widget, p) 244 if (p->source->dobj.private) { 245 ret = sof_route_setup(sdev, p->source, widget); 246 if (ret < 0) 247 return ret; 248 } 249 } 250 } 251 252 return 0; 253 } 254 255 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir) 256 { 257 const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg; 258 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list; 259 struct snd_soc_dapm_widget *widget; 260 int i, ret, num_widgets; 261 262 /* nothing to set up */ 263 if (!list) 264 return 0; 265 266 /* set up widgets in the list */ 267 for_each_dapm_widgets(list, num_widgets, widget) { 268 struct snd_sof_widget *swidget = widget->dobj.private; 269 270 if (!swidget) 271 continue; 272 273 /* set up the widget */ 274 ret = sof_widget_setup(sdev, swidget); 275 if (ret < 0) 276 goto widget_free; 277 } 278 279 /* 280 * error in setting pipeline connections will result in route status being reset for 281 * routes that were successfully set up when the widgets are freed. 282 */ 283 ret = sof_setup_pipeline_connections(sdev, list, dir); 284 if (ret < 0) 285 goto widget_free; 286 287 /* complete pipelines */ 288 for_each_dapm_widgets(list, i, widget) { 289 struct snd_sof_widget *swidget = widget->dobj.private; 290 struct snd_sof_widget *pipe_widget; 291 292 if (!swidget) 293 continue; 294 295 pipe_widget = swidget->pipe_widget; 296 if (!pipe_widget) { 297 dev_err(sdev->dev, "error: no pipeline widget found for %s\n", 298 swidget->widget->name); 299 ret = -EINVAL; 300 goto widget_free; 301 } 302 303 if (pipe_widget->complete) 304 continue; 305 306 if (ipc_tplg_ops->pipeline_complete) { 307 pipe_widget->complete = ipc_tplg_ops->pipeline_complete(sdev, pipe_widget); 308 if (pipe_widget->complete < 0) { 309 ret = pipe_widget->complete; 310 goto widget_free; 311 } 312 } 313 } 314 315 return 0; 316 317 widget_free: 318 /* free all widgets that have been set up successfully */ 319 for_each_dapm_widgets(list, i, widget) { 320 struct snd_sof_widget *swidget = widget->dobj.private; 321 322 if (!swidget) 323 continue; 324 325 if (!num_widgets--) 326 break; 327 328 sof_widget_free(sdev, swidget); 329 } 330 331 return ret; 332 } 333 334 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir) 335 { 336 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list; 337 struct snd_soc_dapm_widget *widget; 338 int i, ret; 339 int ret1 = 0; 340 341 /* nothing to free */ 342 if (!list) 343 return 0; 344 345 /* 346 * Free widgets in the list. This can fail but continue freeing other widgets to keep 347 * use_counts balanced. 348 */ 349 for_each_dapm_widgets(list, i, widget) { 350 struct snd_sof_widget *swidget = widget->dobj.private; 351 352 if (!swidget) 353 continue; 354 355 /* 356 * free widget and its pipe_widget. Either of these can fail, but free as many as 357 * possible before freeing the list and returning the error. 358 */ 359 ret = sof_widget_free(sdev, swidget); 360 if (ret < 0) 361 ret1 = ret; 362 } 363 364 snd_soc_dapm_dai_free_widgets(&list); 365 spcm->stream[dir].list = NULL; 366 367 return ret1; 368 } 369 370 /* 371 * helper to determine if there are only D0i3 compatible 372 * streams active 373 */ 374 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev) 375 { 376 struct snd_pcm_substream *substream; 377 struct snd_sof_pcm *spcm; 378 bool d0i3_compatible_active = false; 379 int dir; 380 381 list_for_each_entry(spcm, &sdev->pcm_list, list) { 382 for_each_pcm_streams(dir) { 383 substream = spcm->stream[dir].substream; 384 if (!substream || !substream->runtime) 385 continue; 386 387 /* 388 * substream->runtime being not NULL indicates 389 * that the stream is open. No need to check the 390 * stream state. 391 */ 392 if (!spcm->stream[dir].d0i3_compatible) 393 return false; 394 395 d0i3_compatible_active = true; 396 } 397 } 398 399 return d0i3_compatible_active; 400 } 401 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active); 402 403 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev) 404 { 405 struct snd_sof_pcm *spcm; 406 407 list_for_each_entry(spcm, &sdev->pcm_list, list) { 408 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored || 409 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored) 410 return true; 411 } 412 413 return false; 414 } 415 416 int sof_set_hw_params_upon_resume(struct device *dev) 417 { 418 struct snd_sof_dev *sdev = dev_get_drvdata(dev); 419 struct snd_pcm_substream *substream; 420 struct snd_sof_pcm *spcm; 421 snd_pcm_state_t state; 422 int dir; 423 424 /* 425 * SOF requires hw_params to be set-up internally upon resume. 426 * So, set the flag to indicate this for those streams that 427 * have been suspended. 428 */ 429 list_for_each_entry(spcm, &sdev->pcm_list, list) { 430 for_each_pcm_streams(dir) { 431 /* 432 * do not reset hw_params upon resume for streams that 433 * were kept running during suspend 434 */ 435 if (spcm->stream[dir].suspend_ignored) 436 continue; 437 438 substream = spcm->stream[dir].substream; 439 if (!substream || !substream->runtime) 440 continue; 441 442 state = substream->runtime->status->state; 443 if (state == SNDRV_PCM_STATE_SUSPENDED) 444 spcm->prepared[dir] = false; 445 } 446 } 447 448 /* set internal flag for BE */ 449 return snd_sof_dsp_hw_params_upon_resume(sdev); 450 } 451 452 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream, 453 struct snd_sof_pcm *spcm, int dir, bool free_widget_list) 454 { 455 const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm; 456 int ret; 457 458 /* Send PCM_FREE IPC to reset pipeline */ 459 if (pcm_ops->hw_free && spcm->prepared[substream->stream]) { 460 ret = pcm_ops->hw_free(sdev->component, substream); 461 if (ret < 0) 462 return ret; 463 } 464 465 spcm->prepared[substream->stream] = false; 466 467 /* stop the DMA */ 468 ret = snd_sof_pcm_platform_hw_free(sdev, substream); 469 if (ret < 0) 470 return ret; 471 472 /* free widget list */ 473 if (free_widget_list) { 474 ret = sof_widget_list_free(sdev, spcm, dir); 475 if (ret < 0) 476 dev_err(sdev->dev, "failed to free widgets during suspend\n"); 477 } 478 479 return ret; 480 } 481 482 /* 483 * Generic object lookup APIs. 484 */ 485 486 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp, 487 const char *name) 488 { 489 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 490 struct snd_sof_pcm *spcm; 491 492 list_for_each_entry(spcm, &sdev->pcm_list, list) { 493 /* match with PCM dai name */ 494 if (strcmp(spcm->pcm.dai_name, name) == 0) 495 return spcm; 496 497 /* match with playback caps name if set */ 498 if (*spcm->pcm.caps[0].name && 499 !strcmp(spcm->pcm.caps[0].name, name)) 500 return spcm; 501 502 /* match with capture caps name if set */ 503 if (*spcm->pcm.caps[1].name && 504 !strcmp(spcm->pcm.caps[1].name, name)) 505 return spcm; 506 } 507 508 return NULL; 509 } 510 511 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp, 512 unsigned int comp_id, 513 int *direction) 514 { 515 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 516 struct snd_sof_pcm *spcm; 517 int dir; 518 519 list_for_each_entry(spcm, &sdev->pcm_list, list) { 520 for_each_pcm_streams(dir) { 521 if (spcm->stream[dir].comp_id == comp_id) { 522 *direction = dir; 523 return spcm; 524 } 525 } 526 } 527 528 return NULL; 529 } 530 531 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp, 532 const char *name) 533 { 534 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 535 struct snd_sof_widget *swidget; 536 537 list_for_each_entry(swidget, &sdev->widget_list, list) { 538 if (strcmp(name, swidget->widget->name) == 0) 539 return swidget; 540 } 541 542 return NULL; 543 } 544 545 /* find widget by stream name and direction */ 546 struct snd_sof_widget * 547 snd_sof_find_swidget_sname(struct snd_soc_component *scomp, 548 const char *pcm_name, int dir) 549 { 550 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 551 struct snd_sof_widget *swidget; 552 enum snd_soc_dapm_type type; 553 554 if (dir == SNDRV_PCM_STREAM_PLAYBACK) 555 type = snd_soc_dapm_aif_in; 556 else 557 type = snd_soc_dapm_aif_out; 558 559 list_for_each_entry(swidget, &sdev->widget_list, list) { 560 if (!strcmp(pcm_name, swidget->widget->sname) && 561 swidget->id == type) 562 return swidget; 563 } 564 565 return NULL; 566 } 567 568 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp, 569 const char *name) 570 { 571 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 572 struct snd_sof_dai *dai; 573 574 list_for_each_entry(dai, &sdev->dai_list, list) { 575 if (dai->name && (strcmp(name, dai->name) == 0)) 576 return dai; 577 } 578 579 return NULL; 580 } 581 582 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type) 583 { 584 struct snd_soc_component *component = 585 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 586 struct snd_sof_dai *dai = 587 snd_sof_find_dai(component, (char *)rtd->dai_link->name); 588 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 589 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg; 590 591 /* use the tplg configured mclk if existed */ 592 if (!dai) 593 return 0; 594 595 if (tplg_ops->dai_get_clk) 596 return tplg_ops->dai_get_clk(sdev, dai, clk_type); 597 598 return 0; 599 } 600 601 /* 602 * Helper to get SSP MCLK from a pcm_runtime. 603 * Return 0 if not exist. 604 */ 605 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd) 606 { 607 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK); 608 } 609 EXPORT_SYMBOL(sof_dai_get_mclk); 610 611 /* 612 * Helper to get SSP BCLK from a pcm_runtime. 613 * Return 0 if not exist. 614 */ 615 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd) 616 { 617 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK); 618 } 619 EXPORT_SYMBOL(sof_dai_get_bclk); 620 621 /* 622 * SOF Driver enumeration. 623 */ 624 int sof_machine_check(struct snd_sof_dev *sdev) 625 { 626 struct snd_sof_pdata *sof_pdata = sdev->pdata; 627 const struct sof_dev_desc *desc = sof_pdata->desc; 628 struct snd_soc_acpi_mach *mach; 629 630 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) { 631 632 /* find machine */ 633 mach = snd_sof_machine_select(sdev); 634 if (mach) { 635 sof_pdata->machine = mach; 636 snd_sof_set_mach_params(mach, sdev); 637 return 0; 638 } 639 640 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) { 641 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n"); 642 return -ENODEV; 643 } 644 } else { 645 dev_warn(sdev->dev, "Force to use nocodec mode\n"); 646 } 647 648 /* select nocodec mode */ 649 dev_warn(sdev->dev, "Using nocodec machine driver\n"); 650 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL); 651 if (!mach) 652 return -ENOMEM; 653 654 mach->drv_name = "sof-nocodec"; 655 sof_pdata->tplg_filename = desc->nocodec_tplg_filename; 656 657 sof_pdata->machine = mach; 658 snd_sof_set_mach_params(mach, sdev); 659 660 return 0; 661 } 662 EXPORT_SYMBOL(sof_machine_check); 663 664 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) 665 { 666 struct snd_sof_pdata *plat_data = pdata; 667 const char *drv_name; 668 const void *mach; 669 int size; 670 671 drv_name = plat_data->machine->drv_name; 672 mach = plat_data->machine; 673 size = sizeof(*plat_data->machine); 674 675 /* register machine driver, pass machine info as pdata */ 676 plat_data->pdev_mach = 677 platform_device_register_data(sdev->dev, drv_name, 678 PLATFORM_DEVID_NONE, mach, size); 679 if (IS_ERR(plat_data->pdev_mach)) 680 return PTR_ERR(plat_data->pdev_mach); 681 682 dev_dbg(sdev->dev, "created machine %s\n", 683 dev_name(&plat_data->pdev_mach->dev)); 684 685 return 0; 686 } 687 EXPORT_SYMBOL(sof_machine_register); 688 689 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata) 690 { 691 struct snd_sof_pdata *plat_data = pdata; 692 693 if (!IS_ERR_OR_NULL(plat_data->pdev_mach)) 694 platform_device_unregister(plat_data->pdev_mach); 695 } 696 EXPORT_SYMBOL(sof_machine_unregister); 697