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