xref: /linux-6.15/sound/soc/sof/sof-audio.c (revision 66727cdb)
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 "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_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
417 			struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
418 {
419 	const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
420 	int ret;
421 
422 	/* Send PCM_FREE IPC to reset pipeline */
423 	if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
424 		ret = pcm_ops->hw_free(sdev->component, substream);
425 		if (ret < 0)
426 			return ret;
427 	}
428 
429 	spcm->prepared[substream->stream] = false;
430 
431 	/* stop the DMA */
432 	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
433 	if (ret < 0)
434 		return ret;
435 
436 	/* free widget list */
437 	if (free_widget_list) {
438 		ret = sof_widget_list_free(sdev, spcm, dir);
439 		if (ret < 0)
440 			dev_err(sdev->dev, "failed to free widgets during suspend\n");
441 	}
442 
443 	return ret;
444 }
445 
446 /*
447  * Generic object lookup APIs.
448  */
449 
450 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
451 					   const char *name)
452 {
453 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
454 	struct snd_sof_pcm *spcm;
455 
456 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
457 		/* match with PCM dai name */
458 		if (strcmp(spcm->pcm.dai_name, name) == 0)
459 			return spcm;
460 
461 		/* match with playback caps name if set */
462 		if (*spcm->pcm.caps[0].name &&
463 		    !strcmp(spcm->pcm.caps[0].name, name))
464 			return spcm;
465 
466 		/* match with capture caps name if set */
467 		if (*spcm->pcm.caps[1].name &&
468 		    !strcmp(spcm->pcm.caps[1].name, name))
469 			return spcm;
470 	}
471 
472 	return NULL;
473 }
474 
475 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
476 					   unsigned int comp_id,
477 					   int *direction)
478 {
479 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
480 	struct snd_sof_pcm *spcm;
481 	int dir;
482 
483 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
484 		for_each_pcm_streams(dir) {
485 			if (spcm->stream[dir].comp_id == comp_id) {
486 				*direction = dir;
487 				return spcm;
488 			}
489 		}
490 	}
491 
492 	return NULL;
493 }
494 
495 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
496 					    const char *name)
497 {
498 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
499 	struct snd_sof_widget *swidget;
500 
501 	list_for_each_entry(swidget, &sdev->widget_list, list) {
502 		if (strcmp(name, swidget->widget->name) == 0)
503 			return swidget;
504 	}
505 
506 	return NULL;
507 }
508 
509 /* find widget by stream name and direction */
510 struct snd_sof_widget *
511 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
512 			   const char *pcm_name, int dir)
513 {
514 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
515 	struct snd_sof_widget *swidget;
516 	enum snd_soc_dapm_type type;
517 
518 	if (dir == SNDRV_PCM_STREAM_PLAYBACK)
519 		type = snd_soc_dapm_aif_in;
520 	else
521 		type = snd_soc_dapm_aif_out;
522 
523 	list_for_each_entry(swidget, &sdev->widget_list, list) {
524 		if (!strcmp(pcm_name, swidget->widget->sname) &&
525 		    swidget->id == type)
526 			return swidget;
527 	}
528 
529 	return NULL;
530 }
531 
532 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
533 				     const char *name)
534 {
535 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
536 	struct snd_sof_dai *dai;
537 
538 	list_for_each_entry(dai, &sdev->dai_list, list) {
539 		if (dai->name && (strcmp(name, dai->name) == 0))
540 			return dai;
541 	}
542 
543 	return NULL;
544 }
545 
546 static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
547 {
548 	struct snd_soc_component *component =
549 		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
550 	struct snd_sof_dai *dai =
551 		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
552 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
553 	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
554 
555 	/* use the tplg configured mclk if existed */
556 	if (!dai)
557 		return 0;
558 
559 	if (tplg_ops->dai_get_clk)
560 		return tplg_ops->dai_get_clk(sdev, dai, clk_type);
561 
562 	return 0;
563 }
564 
565 /*
566  * Helper to get SSP MCLK from a pcm_runtime.
567  * Return 0 if not exist.
568  */
569 int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
570 {
571 	return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK);
572 }
573 EXPORT_SYMBOL(sof_dai_get_mclk);
574 
575 /*
576  * Helper to get SSP BCLK from a pcm_runtime.
577  * Return 0 if not exist.
578  */
579 int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
580 {
581 	return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK);
582 }
583 EXPORT_SYMBOL(sof_dai_get_bclk);
584 
585 /*
586  * SOF Driver enumeration.
587  */
588 int sof_machine_check(struct snd_sof_dev *sdev)
589 {
590 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
591 	const struct sof_dev_desc *desc = sof_pdata->desc;
592 	struct snd_soc_acpi_mach *mach;
593 
594 	if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
595 
596 		/* find machine */
597 		mach = snd_sof_machine_select(sdev);
598 		if (mach) {
599 			sof_pdata->machine = mach;
600 			snd_sof_set_mach_params(mach, sdev);
601 			return 0;
602 		}
603 
604 		if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
605 			dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
606 			return -ENODEV;
607 		}
608 	} else {
609 		dev_warn(sdev->dev, "Force to use nocodec mode\n");
610 	}
611 
612 	/* select nocodec mode */
613 	dev_warn(sdev->dev, "Using nocodec machine driver\n");
614 	mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
615 	if (!mach)
616 		return -ENOMEM;
617 
618 	mach->drv_name = "sof-nocodec";
619 	if (!sof_pdata->tplg_filename)
620 		sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
621 
622 	sof_pdata->machine = mach;
623 	snd_sof_set_mach_params(mach, sdev);
624 
625 	return 0;
626 }
627 EXPORT_SYMBOL(sof_machine_check);
628 
629 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
630 {
631 	struct snd_sof_pdata *plat_data = pdata;
632 	const char *drv_name;
633 	const void *mach;
634 	int size;
635 
636 	drv_name = plat_data->machine->drv_name;
637 	mach = plat_data->machine;
638 	size = sizeof(*plat_data->machine);
639 
640 	/* register machine driver, pass machine info as pdata */
641 	plat_data->pdev_mach =
642 		platform_device_register_data(sdev->dev, drv_name,
643 					      PLATFORM_DEVID_NONE, mach, size);
644 	if (IS_ERR(plat_data->pdev_mach))
645 		return PTR_ERR(plat_data->pdev_mach);
646 
647 	dev_dbg(sdev->dev, "created machine %s\n",
648 		dev_name(&plat_data->pdev_mach->dev));
649 
650 	return 0;
651 }
652 EXPORT_SYMBOL(sof_machine_register);
653 
654 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
655 {
656 	struct snd_sof_pdata *plat_data = pdata;
657 
658 	if (!IS_ERR_OR_NULL(plat_data->pdev_mach))
659 		platform_device_unregister(plat_data->pdev_mach);
660 }
661 EXPORT_SYMBOL(sof_machine_unregister);
662