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