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 <[email protected]> 9 // 10 11 #include <linux/bitfield.h> 12 #include <trace/events/sof.h> 13 #include "sof-audio.h" 14 #include "sof-of-dev.h" 15 #include "ops.h" 16 17 static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget) 18 { 19 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 20 struct snd_sof_route *sroute; 21 22 list_for_each_entry(sroute, &sdev->route_list, list) 23 if (sroute->src_widget == widget || sroute->sink_widget == widget) { 24 if (sroute->setup && tplg_ops && tplg_ops->route_free) 25 tplg_ops->route_free(sdev, sroute); 26 27 sroute->setup = false; 28 } 29 } 30 31 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 32 { 33 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 34 int err = 0; 35 int ret; 36 37 if (!swidget->private) 38 return 0; 39 40 trace_sof_widget_free(swidget); 41 42 /* only free when use_count is 0 */ 43 if (--swidget->use_count) 44 return 0; 45 46 /* reset route setup status for all routes that contain this widget */ 47 sof_reset_route_setup_status(sdev, swidget); 48 49 /* continue to disable core even if IPC fails */ 50 if (tplg_ops && tplg_ops->widget_free) 51 err = tplg_ops->widget_free(sdev, swidget); 52 53 /* 54 * disable widget core. continue to route setup status and complete flag 55 * even if this fails and return the appropriate error 56 */ 57 ret = snd_sof_dsp_core_put(sdev, swidget->core); 58 if (ret < 0) { 59 dev_err(sdev->dev, "error: failed to disable target core: %d for widget %s\n", 60 swidget->core, swidget->widget->name); 61 if (!err) 62 err = ret; 63 } 64 65 /* 66 * free the scheduler widget (same as pipe_widget) associated with the current swidget. 67 * skip for static pipelines 68 */ 69 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) { 70 ret = sof_widget_free(sdev, swidget->pipe_widget); 71 if (ret < 0 && !err) 72 err = ret; 73 swidget->pipe_widget->complete = 0; 74 } 75 76 if (!err) 77 dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name); 78 79 return err; 80 } 81 EXPORT_SYMBOL(sof_widget_free); 82 83 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 84 { 85 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 86 int ret; 87 88 /* skip if there is no private data */ 89 if (!swidget->private) 90 return 0; 91 92 trace_sof_widget_setup(swidget); 93 94 /* widget already set up */ 95 if (++swidget->use_count > 1) 96 return 0; 97 98 /* 99 * The scheduler widget for a pipeline is not part of the connected DAPM 100 * widget list and it needs to be set up before the widgets in the pipeline 101 * are set up. The use_count for the scheduler widget is incremented for every 102 * widget in a given pipeline to ensure that it is freed only after the last 103 * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines. 104 */ 105 if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) { 106 if (!swidget->pipe_widget) { 107 dev_err(sdev->dev, "No scheduler widget set for %s\n", 108 swidget->widget->name); 109 ret = -EINVAL; 110 goto use_count_dec; 111 } 112 113 ret = sof_widget_setup(sdev, swidget->pipe_widget); 114 if (ret < 0) 115 goto use_count_dec; 116 } 117 118 /* enable widget core */ 119 ret = snd_sof_dsp_core_get(sdev, swidget->core); 120 if (ret < 0) { 121 dev_err(sdev->dev, "error: failed to enable target core for widget %s\n", 122 swidget->widget->name); 123 goto pipe_widget_free; 124 } 125 126 /* setup widget in the DSP */ 127 if (tplg_ops && tplg_ops->widget_setup) { 128 ret = tplg_ops->widget_setup(sdev, swidget); 129 if (ret < 0) 130 goto core_put; 131 } 132 133 /* send config for DAI components */ 134 if (WIDGET_IS_DAI(swidget->id)) { 135 unsigned int flags = SOF_DAI_CONFIG_FLAGS_NONE; 136 137 if (tplg_ops && tplg_ops->dai_config) { 138 ret = tplg_ops->dai_config(sdev, swidget, flags, NULL); 139 if (ret < 0) 140 goto widget_free; 141 } 142 } 143 144 /* restore kcontrols for widget */ 145 if (tplg_ops && tplg_ops->control && tplg_ops->control->widget_kcontrol_setup) { 146 ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget); 147 if (ret < 0) 148 goto widget_free; 149 } 150 151 dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name); 152 153 return 0; 154 155 widget_free: 156 /* widget use_count and core ref_count will both be decremented by sof_widget_free() */ 157 sof_widget_free(sdev, swidget); 158 core_put: 159 snd_sof_dsp_core_put(sdev, swidget->core); 160 pipe_widget_free: 161 if (swidget->id != snd_soc_dapm_scheduler) 162 sof_widget_free(sdev, swidget->pipe_widget); 163 use_count_dec: 164 swidget->use_count--; 165 return ret; 166 } 167 EXPORT_SYMBOL(sof_widget_setup); 168 169 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource, 170 struct snd_soc_dapm_widget *wsink) 171 { 172 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 173 struct snd_sof_widget *src_widget = wsource->dobj.private; 174 struct snd_sof_widget *sink_widget = wsink->dobj.private; 175 struct snd_sof_route *sroute; 176 bool route_found = false; 177 178 /* ignore routes involving virtual widgets in topology */ 179 switch (src_widget->id) { 180 case snd_soc_dapm_out_drv: 181 case snd_soc_dapm_output: 182 case snd_soc_dapm_input: 183 return 0; 184 default: 185 break; 186 } 187 188 switch (sink_widget->id) { 189 case snd_soc_dapm_out_drv: 190 case snd_soc_dapm_output: 191 case snd_soc_dapm_input: 192 return 0; 193 default: 194 break; 195 } 196 197 /* find route matching source and sink widgets */ 198 list_for_each_entry(sroute, &sdev->route_list, list) 199 if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) { 200 route_found = true; 201 break; 202 } 203 204 if (!route_found) { 205 dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n", 206 wsource->name, wsink->name); 207 return -EINVAL; 208 } 209 210 /* nothing to do if route is already set up */ 211 if (sroute->setup) 212 return 0; 213 214 if (tplg_ops && tplg_ops->route_setup) { 215 int ret = tplg_ops->route_setup(sdev, sroute); 216 217 if (ret < 0) 218 return ret; 219 } 220 221 sroute->setup = true; 222 return 0; 223 } 224 225 static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev, 226 struct snd_soc_dapm_widget_list *list, int dir) 227 { 228 struct snd_soc_dapm_widget *widget; 229 struct snd_soc_dapm_path *p; 230 int ret; 231 int i; 232 233 /* 234 * Set up connections between widgets in the sink/source paths based on direction. 235 * Some non-SOF widgets exist in topology either for compatibility or for the 236 * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM 237 * events. But they are not handled by the firmware. So ignore them. 238 */ 239 if (dir == SNDRV_PCM_STREAM_PLAYBACK) { 240 for_each_dapm_widgets(list, i, widget) { 241 if (!widget->dobj.private) 242 continue; 243 244 snd_soc_dapm_widget_for_each_sink_path(widget, p) 245 if (p->sink->dobj.private) { 246 ret = sof_route_setup(sdev, widget, p->sink); 247 if (ret < 0) 248 return ret; 249 } 250 } 251 } else { 252 for_each_dapm_widgets(list, i, widget) { 253 if (!widget->dobj.private) 254 continue; 255 256 snd_soc_dapm_widget_for_each_source_path(widget, p) 257 if (p->source->dobj.private) { 258 ret = sof_route_setup(sdev, p->source, widget); 259 if (ret < 0) 260 return ret; 261 } 262 } 263 } 264 265 return 0; 266 } 267 268 static void 269 sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget) 270 { 271 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 272 struct snd_sof_widget *swidget = widget->dobj.private; 273 const struct sof_ipc_tplg_widget_ops *widget_ops; 274 struct snd_soc_dapm_path *p; 275 276 /* return if the widget is in use or if it is already unprepared */ 277 if (!swidget->prepared || swidget->use_count > 1) 278 return; 279 280 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 281 if (widget_ops && widget_ops[widget->id].ipc_unprepare) 282 /* unprepare the source widget */ 283 widget_ops[widget->id].ipc_unprepare(swidget); 284 285 swidget->prepared = false; 286 287 /* unprepare all widgets in the sink paths */ 288 snd_soc_dapm_widget_for_each_sink_path(widget, p) { 289 if (!p->walking && p->sink->dobj.private) { 290 p->walking = true; 291 sof_unprepare_widgets_in_path(sdev, p->sink); 292 p->walking = false; 293 } 294 } 295 } 296 297 static int 298 sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget, 299 struct snd_pcm_hw_params *fe_params, 300 struct snd_sof_platform_stream_params *platform_params, 301 struct snd_pcm_hw_params *pipeline_params, int dir) 302 { 303 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 304 struct snd_sof_widget *swidget = widget->dobj.private; 305 const struct sof_ipc_tplg_widget_ops *widget_ops; 306 struct snd_soc_dapm_path *p; 307 int ret; 308 309 widget_ops = tplg_ops ? tplg_ops->widget : NULL; 310 if (!widget_ops) 311 return 0; 312 313 if (!widget_ops[widget->id].ipc_prepare || swidget->prepared) 314 goto sink_prepare; 315 316 /* prepare the source widget */ 317 ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params, 318 pipeline_params, dir); 319 if (ret < 0) { 320 dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name); 321 return ret; 322 } 323 324 swidget->prepared = true; 325 326 sink_prepare: 327 /* prepare all widgets in the sink paths */ 328 snd_soc_dapm_widget_for_each_sink_path(widget, p) { 329 if (!p->walking && p->sink->dobj.private) { 330 p->walking = true; 331 ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params, 332 platform_params, pipeline_params, dir); 333 p->walking = false; 334 if (ret < 0) { 335 /* unprepare the source widget */ 336 if (widget_ops[widget->id].ipc_unprepare && swidget->prepared) { 337 widget_ops[widget->id].ipc_unprepare(swidget); 338 swidget->prepared = false; 339 } 340 return ret; 341 } 342 } 343 } 344 345 return 0; 346 } 347 348 /* 349 * free all widgets in the sink path starting from the source widget 350 * (DAI type for capture, AIF type for playback) 351 */ 352 static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget, 353 int dir) 354 { 355 struct snd_soc_dapm_path *p; 356 int err; 357 int ret = 0; 358 359 /* free all widgets even in case of error to keep use counts balanced */ 360 snd_soc_dapm_widget_for_each_sink_path(widget, p) { 361 if (!p->walking && p->sink->dobj.private && widget->dobj.private) { 362 p->walking = true; 363 if (WIDGET_IS_AIF_OR_DAI(widget->id)) { 364 err = sof_widget_free(sdev, widget->dobj.private); 365 if (err < 0) 366 ret = err; 367 } 368 369 err = sof_widget_free(sdev, p->sink->dobj.private); 370 if (err < 0) 371 ret = err; 372 373 err = sof_free_widgets_in_path(sdev, p->sink, dir); 374 if (err < 0) 375 ret = err; 376 p->walking = false; 377 } 378 } 379 380 return ret; 381 } 382 383 /* 384 * set up all widgets in the sink path starting from the source widget 385 * (DAI type for capture, AIF type for playback). 386 * The error path in this function ensures that all successfully set up widgets getting freed. 387 */ 388 static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget, 389 int dir) 390 { 391 struct snd_soc_dapm_path *p; 392 int ret; 393 394 snd_soc_dapm_widget_for_each_sink_path(widget, p) { 395 if (!p->walking && p->sink->dobj.private && widget->dobj.private) { 396 p->walking = true; 397 if (WIDGET_IS_AIF_OR_DAI(widget->id)) { 398 ret = sof_widget_setup(sdev, widget->dobj.private); 399 if (ret < 0) 400 goto out; 401 } 402 403 ret = sof_widget_setup(sdev, p->sink->dobj.private); 404 if (ret < 0) { 405 if (WIDGET_IS_AIF_OR_DAI(widget->id)) 406 sof_widget_free(sdev, widget->dobj.private); 407 goto out; 408 } 409 410 ret = sof_set_up_widgets_in_path(sdev, p->sink, dir); 411 if (ret < 0) { 412 if (WIDGET_IS_AIF_OR_DAI(widget->id)) 413 sof_widget_free(sdev, widget->dobj.private); 414 sof_widget_free(sdev, p->sink->dobj.private); 415 } 416 out: 417 p->walking = false; 418 if (ret < 0) 419 return ret; 420 } 421 } 422 423 return 0; 424 } 425 426 static int 427 sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget_list *list, 428 struct snd_pcm_hw_params *fe_params, 429 struct snd_sof_platform_stream_params *platform_params, int dir, 430 enum sof_widget_op op) 431 { 432 struct snd_soc_dapm_widget *widget; 433 char *str; 434 int ret = 0; 435 int i; 436 437 for_each_dapm_widgets(list, i, widget) { 438 /* starting widget for playback is AIF type */ 439 if (dir == SNDRV_PCM_STREAM_PLAYBACK && !WIDGET_IS_AIF(widget->id)) 440 continue; 441 442 /* starting widget for capture is DAI type */ 443 if (dir == SNDRV_PCM_STREAM_CAPTURE && !WIDGET_IS_DAI(widget->id)) 444 continue; 445 446 switch (op) { 447 case SOF_WIDGET_SETUP: 448 ret = sof_set_up_widgets_in_path(sdev, widget, dir); 449 str = "set up"; 450 break; 451 case SOF_WIDGET_FREE: 452 ret = sof_free_widgets_in_path(sdev, widget, dir); 453 str = "free"; 454 break; 455 case SOF_WIDGET_PREPARE: 456 { 457 struct snd_pcm_hw_params pipeline_params; 458 459 str = "prepare"; 460 /* 461 * When walking the list of connected widgets, the pipeline_params for each 462 * widget is modified by the source widget in the path. Use a local 463 * copy of the runtime params as the pipeline_params so that the runtime 464 * params does not get overwritten. 465 */ 466 memcpy(&pipeline_params, fe_params, sizeof(*fe_params)); 467 468 ret = sof_prepare_widgets_in_path(sdev, widget, fe_params, 469 platform_params, &pipeline_params, dir); 470 break; 471 } 472 case SOF_WIDGET_UNPREPARE: 473 sof_unprepare_widgets_in_path(sdev, widget); 474 break; 475 default: 476 dev_err(sdev->dev, "Invalid widget op %d\n", op); 477 return -EINVAL; 478 } 479 if (ret < 0) { 480 dev_err(sdev->dev, "Failed to %s connected widgets\n", str); 481 return ret; 482 } 483 } 484 485 return 0; 486 } 487 488 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, 489 struct snd_pcm_hw_params *fe_params, 490 struct snd_sof_platform_stream_params *platform_params, 491 int dir) 492 { 493 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 494 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list; 495 struct snd_soc_dapm_widget *widget; 496 int i, ret; 497 498 /* nothing to set up */ 499 if (!list) 500 return 0; 501 502 /* 503 * Prepare widgets for set up. The prepare step is used to allocate memory, assign 504 * instance ID and pick the widget configuration based on the runtime PCM params. 505 */ 506 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, 507 dir, SOF_WIDGET_PREPARE); 508 if (ret < 0) 509 return ret; 510 511 /* Set up is used to send the IPC to the DSP to create the widget */ 512 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, 513 dir, SOF_WIDGET_SETUP); 514 if (ret < 0) { 515 ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, 516 dir, SOF_WIDGET_UNPREPARE); 517 return ret; 518 } 519 520 /* 521 * error in setting pipeline connections will result in route status being reset for 522 * routes that were successfully set up when the widgets are freed. 523 */ 524 ret = sof_setup_pipeline_connections(sdev, list, dir); 525 if (ret < 0) 526 goto widget_free; 527 528 /* complete pipelines */ 529 for_each_dapm_widgets(list, i, widget) { 530 struct snd_sof_widget *swidget = widget->dobj.private; 531 struct snd_sof_widget *pipe_widget; 532 533 if (!swidget) 534 continue; 535 536 pipe_widget = swidget->pipe_widget; 537 if (!pipe_widget) { 538 dev_err(sdev->dev, "error: no pipeline widget found for %s\n", 539 swidget->widget->name); 540 ret = -EINVAL; 541 goto widget_free; 542 } 543 544 if (pipe_widget->complete) 545 continue; 546 547 if (tplg_ops && tplg_ops->pipeline_complete) { 548 pipe_widget->complete = tplg_ops->pipeline_complete(sdev, pipe_widget); 549 if (pipe_widget->complete < 0) { 550 ret = pipe_widget->complete; 551 goto widget_free; 552 } 553 } 554 } 555 556 return 0; 557 558 widget_free: 559 sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, dir, 560 SOF_WIDGET_FREE); 561 sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE); 562 563 return ret; 564 } 565 566 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir) 567 { 568 struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list; 569 int ret; 570 571 /* nothing to free */ 572 if (!list) 573 return 0; 574 575 /* send IPC to free widget in the DSP */ 576 ret = sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_FREE); 577 578 /* unprepare the widget */ 579 sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE); 580 581 snd_soc_dapm_dai_free_widgets(&list); 582 spcm->stream[dir].list = NULL; 583 584 return ret; 585 } 586 587 /* 588 * helper to determine if there are only D0i3 compatible 589 * streams active 590 */ 591 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev) 592 { 593 struct snd_pcm_substream *substream; 594 struct snd_sof_pcm *spcm; 595 bool d0i3_compatible_active = false; 596 int dir; 597 598 list_for_each_entry(spcm, &sdev->pcm_list, list) { 599 for_each_pcm_streams(dir) { 600 substream = spcm->stream[dir].substream; 601 if (!substream || !substream->runtime) 602 continue; 603 604 /* 605 * substream->runtime being not NULL indicates 606 * that the stream is open. No need to check the 607 * stream state. 608 */ 609 if (!spcm->stream[dir].d0i3_compatible) 610 return false; 611 612 d0i3_compatible_active = true; 613 } 614 } 615 616 return d0i3_compatible_active; 617 } 618 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active); 619 620 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev) 621 { 622 struct snd_sof_pcm *spcm; 623 624 list_for_each_entry(spcm, &sdev->pcm_list, list) { 625 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored || 626 spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored) 627 return true; 628 } 629 630 return false; 631 } 632 633 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream, 634 struct snd_sof_pcm *spcm, int dir, bool free_widget_list) 635 { 636 const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm); 637 int ret; 638 639 /* Send PCM_FREE IPC to reset pipeline */ 640 if (pcm_ops && pcm_ops->hw_free && spcm->prepared[substream->stream]) { 641 ret = pcm_ops->hw_free(sdev->component, substream); 642 if (ret < 0) 643 return ret; 644 } 645 646 spcm->prepared[substream->stream] = false; 647 648 /* stop the DMA */ 649 ret = snd_sof_pcm_platform_hw_free(sdev, substream); 650 if (ret < 0) 651 return ret; 652 653 /* free widget list */ 654 if (free_widget_list) { 655 ret = sof_widget_list_free(sdev, spcm, dir); 656 if (ret < 0) 657 dev_err(sdev->dev, "failed to free widgets during suspend\n"); 658 } 659 660 return ret; 661 } 662 663 /* 664 * Generic object lookup APIs. 665 */ 666 667 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp, 668 const char *name) 669 { 670 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 671 struct snd_sof_pcm *spcm; 672 673 list_for_each_entry(spcm, &sdev->pcm_list, list) { 674 /* match with PCM dai name */ 675 if (strcmp(spcm->pcm.dai_name, name) == 0) 676 return spcm; 677 678 /* match with playback caps name if set */ 679 if (*spcm->pcm.caps[0].name && 680 !strcmp(spcm->pcm.caps[0].name, name)) 681 return spcm; 682 683 /* match with capture caps name if set */ 684 if (*spcm->pcm.caps[1].name && 685 !strcmp(spcm->pcm.caps[1].name, name)) 686 return spcm; 687 } 688 689 return NULL; 690 } 691 692 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp, 693 unsigned int comp_id, 694 int *direction) 695 { 696 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 697 struct snd_sof_pcm *spcm; 698 int dir; 699 700 list_for_each_entry(spcm, &sdev->pcm_list, list) { 701 for_each_pcm_streams(dir) { 702 if (spcm->stream[dir].comp_id == comp_id) { 703 *direction = dir; 704 return spcm; 705 } 706 } 707 } 708 709 return NULL; 710 } 711 712 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp, 713 const char *name) 714 { 715 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 716 struct snd_sof_widget *swidget; 717 718 list_for_each_entry(swidget, &sdev->widget_list, list) { 719 if (strcmp(name, swidget->widget->name) == 0) 720 return swidget; 721 } 722 723 return NULL; 724 } 725 726 /* find widget by stream name and direction */ 727 struct snd_sof_widget * 728 snd_sof_find_swidget_sname(struct snd_soc_component *scomp, 729 const char *pcm_name, int dir) 730 { 731 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 732 struct snd_sof_widget *swidget; 733 enum snd_soc_dapm_type type; 734 735 if (dir == SNDRV_PCM_STREAM_PLAYBACK) 736 type = snd_soc_dapm_aif_in; 737 else 738 type = snd_soc_dapm_aif_out; 739 740 list_for_each_entry(swidget, &sdev->widget_list, list) { 741 if (!strcmp(pcm_name, swidget->widget->sname) && 742 swidget->id == type) 743 return swidget; 744 } 745 746 return NULL; 747 } 748 749 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp, 750 const char *name) 751 { 752 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 753 struct snd_sof_dai *dai; 754 755 list_for_each_entry(dai, &sdev->dai_list, list) { 756 if (dai->name && (strcmp(name, dai->name) == 0)) 757 return dai; 758 } 759 760 return NULL; 761 } 762 763 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type) 764 { 765 struct snd_soc_component *component = 766 snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 767 struct snd_sof_dai *dai = 768 snd_sof_find_dai(component, (char *)rtd->dai_link->name); 769 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 770 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg); 771 772 /* use the tplg configured mclk if existed */ 773 if (!dai) 774 return 0; 775 776 if (tplg_ops && tplg_ops->dai_get_clk) 777 return tplg_ops->dai_get_clk(sdev, dai, clk_type); 778 779 return 0; 780 } 781 782 /* 783 * Helper to get SSP MCLK from a pcm_runtime. 784 * Return 0 if not exist. 785 */ 786 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd) 787 { 788 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK); 789 } 790 EXPORT_SYMBOL(sof_dai_get_mclk); 791 792 /* 793 * Helper to get SSP BCLK from a pcm_runtime. 794 * Return 0 if not exist. 795 */ 796 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd) 797 { 798 return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK); 799 } 800 EXPORT_SYMBOL(sof_dai_get_bclk); 801 802 static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev) 803 { 804 struct snd_sof_pdata *sof_pdata = sdev->pdata; 805 const struct sof_dev_desc *desc = sof_pdata->desc; 806 struct snd_sof_of_mach *mach = desc->of_machines; 807 808 if (!mach) 809 return NULL; 810 811 for (; mach->compatible; mach++) { 812 if (of_machine_is_compatible(mach->compatible)) { 813 sof_pdata->tplg_filename = mach->sof_tplg_filename; 814 if (mach->fw_filename) 815 sof_pdata->fw_filename = mach->fw_filename; 816 817 return mach; 818 } 819 } 820 821 return NULL; 822 } 823 824 /* 825 * SOF Driver enumeration. 826 */ 827 int sof_machine_check(struct snd_sof_dev *sdev) 828 { 829 struct snd_sof_pdata *sof_pdata = sdev->pdata; 830 const struct sof_dev_desc *desc = sof_pdata->desc; 831 struct snd_soc_acpi_mach *mach; 832 833 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) { 834 const struct snd_sof_of_mach *of_mach; 835 836 if (IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC_DEBUG_SUPPORT) && 837 sof_debug_check_flag(SOF_DBG_FORCE_NOCODEC)) 838 goto nocodec; 839 840 /* find machine */ 841 mach = snd_sof_machine_select(sdev); 842 if (mach) { 843 sof_pdata->machine = mach; 844 snd_sof_set_mach_params(mach, sdev); 845 return 0; 846 } 847 848 of_mach = sof_of_machine_select(sdev); 849 if (of_mach) { 850 sof_pdata->of_machine = of_mach; 851 return 0; 852 } 853 854 if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) { 855 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n"); 856 return -ENODEV; 857 } 858 } else { 859 dev_warn(sdev->dev, "Force to use nocodec mode\n"); 860 } 861 862 nocodec: 863 /* select nocodec mode */ 864 dev_warn(sdev->dev, "Using nocodec machine driver\n"); 865 mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL); 866 if (!mach) 867 return -ENOMEM; 868 869 mach->drv_name = "sof-nocodec"; 870 if (!sof_pdata->tplg_filename) 871 sof_pdata->tplg_filename = desc->nocodec_tplg_filename; 872 873 sof_pdata->machine = mach; 874 snd_sof_set_mach_params(mach, sdev); 875 876 return 0; 877 } 878 EXPORT_SYMBOL(sof_machine_check); 879 880 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata) 881 { 882 struct snd_sof_pdata *plat_data = pdata; 883 const char *drv_name; 884 const void *mach; 885 int size; 886 887 drv_name = plat_data->machine->drv_name; 888 mach = plat_data->machine; 889 size = sizeof(*plat_data->machine); 890 891 /* register machine driver, pass machine info as pdata */ 892 plat_data->pdev_mach = 893 platform_device_register_data(sdev->dev, drv_name, 894 PLATFORM_DEVID_NONE, mach, size); 895 if (IS_ERR(plat_data->pdev_mach)) 896 return PTR_ERR(plat_data->pdev_mach); 897 898 dev_dbg(sdev->dev, "created machine %s\n", 899 dev_name(&plat_data->pdev_mach->dev)); 900 901 return 0; 902 } 903 EXPORT_SYMBOL(sof_machine_register); 904 905 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata) 906 { 907 struct snd_sof_pdata *plat_data = pdata; 908 909 if (!IS_ERR_OR_NULL(plat_data->pdev_mach)) 910 platform_device_unregister(plat_data->pdev_mach); 911 } 912 EXPORT_SYMBOL(sof_machine_unregister); 913