1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
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.
6 // Copyright(c) 2019 Intel Corporation. All rights reserved.
8 // Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
11 #include "sof-audio.h"
15 * helper to determine if there are only D0i3 compatible
18 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev
*sdev
)
20 struct snd_pcm_substream
*substream
;
21 struct snd_sof_pcm
*spcm
;
22 bool d0i3_compatible_active
= false;
25 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
26 for_each_pcm_streams(dir
) {
27 substream
= spcm
->stream
[dir
].substream
;
28 if (!substream
|| !substream
->runtime
)
32 * substream->runtime being not NULL indicates that
33 * that the stream is open. No need to check the
36 if (!spcm
->stream
[dir
].d0i3_compatible
)
39 d0i3_compatible_active
= true;
43 return d0i3_compatible_active
;
45 EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active
);
47 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev
*sdev
)
49 struct snd_sof_pcm
*spcm
;
51 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
52 if (spcm
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].suspend_ignored
||
53 spcm
->stream
[SNDRV_PCM_STREAM_CAPTURE
].suspend_ignored
)
60 int sof_set_hw_params_upon_resume(struct device
*dev
)
62 struct snd_sof_dev
*sdev
= dev_get_drvdata(dev
);
63 struct snd_pcm_substream
*substream
;
64 struct snd_sof_pcm
*spcm
;
65 snd_pcm_state_t state
;
69 * SOF requires hw_params to be set-up internally upon resume.
70 * So, set the flag to indicate this for those streams that
71 * have been suspended.
73 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
74 for_each_pcm_streams(dir
) {
76 * do not reset hw_params upon resume for streams that
77 * were kept running during suspend
79 if (spcm
->stream
[dir
].suspend_ignored
)
82 substream
= spcm
->stream
[dir
].substream
;
83 if (!substream
|| !substream
->runtime
)
86 state
= substream
->runtime
->status
->state
;
87 if (state
== SNDRV_PCM_STATE_SUSPENDED
)
88 spcm
->prepared
[dir
] = false;
92 /* set internal flag for BE */
93 return snd_sof_dsp_hw_params_upon_resume(sdev
);
96 static int sof_restore_kcontrols(struct device
*dev
)
98 struct snd_sof_dev
*sdev
= dev_get_drvdata(dev
);
99 struct snd_sof_control
*scontrol
;
100 int ipc_cmd
, ctrl_type
;
103 /* restore kcontrol values */
104 list_for_each_entry(scontrol
, &sdev
->kcontrol_list
, list
) {
105 /* reset readback offset for scontrol after resuming */
106 scontrol
->readback_offset
= 0;
108 /* notify DSP of kcontrol values */
109 switch (scontrol
->cmd
) {
110 case SOF_CTRL_CMD_VOLUME
:
111 case SOF_CTRL_CMD_ENUM
:
112 case SOF_CTRL_CMD_SWITCH
:
113 ipc_cmd
= SOF_IPC_COMP_SET_VALUE
;
114 ctrl_type
= SOF_CTRL_TYPE_VALUE_CHAN_SET
;
115 ret
= snd_sof_ipc_set_get_comp_data(scontrol
,
120 case SOF_CTRL_CMD_BINARY
:
121 ipc_cmd
= SOF_IPC_COMP_SET_DATA
;
122 ctrl_type
= SOF_CTRL_TYPE_DATA_SET
;
123 ret
= snd_sof_ipc_set_get_comp_data(scontrol
,
135 "error: failed kcontrol value set for widget: %d\n",
145 int sof_restore_pipelines(struct device
*dev
)
147 struct snd_sof_dev
*sdev
= dev_get_drvdata(dev
);
148 struct snd_sof_widget
*swidget
;
149 struct snd_sof_route
*sroute
;
150 struct sof_ipc_pipe_new
*pipeline
;
151 struct snd_sof_dai
*dai
;
152 struct sof_ipc_comp_dai
*comp_dai
;
153 struct sof_ipc_cmd_hdr
*hdr
;
156 /* restore pipeline components */
157 list_for_each_entry_reverse(swidget
, &sdev
->widget_list
, list
) {
158 struct sof_ipc_comp_reply r
;
160 /* skip if there is no private data */
161 if (!swidget
->private)
164 switch (swidget
->id
) {
165 case snd_soc_dapm_dai_in
:
166 case snd_soc_dapm_dai_out
:
167 dai
= swidget
->private;
168 comp_dai
= &dai
->comp_dai
;
169 ret
= sof_ipc_tx_message(sdev
->ipc
,
170 comp_dai
->comp
.hdr
.cmd
,
171 comp_dai
, sizeof(*comp_dai
),
174 case snd_soc_dapm_scheduler
:
177 * During suspend, all DSP cores are powered off.
178 * Therefore upon resume, create the pipeline comp
179 * and power up the core that the pipeline is
182 pipeline
= swidget
->private;
183 ret
= sof_load_pipeline_ipc(dev
, pipeline
, &r
);
186 hdr
= swidget
->private;
187 ret
= sof_ipc_tx_message(sdev
->ipc
, hdr
->cmd
,
188 swidget
->private, hdr
->size
,
194 "error: failed to load widget type %d with ID: %d\n",
195 swidget
->widget
->id
, swidget
->comp_id
);
201 /* restore pipeline connections */
202 list_for_each_entry_reverse(sroute
, &sdev
->route_list
, list
) {
203 struct sof_ipc_pipe_comp_connect
*connect
;
204 struct sof_ipc_reply reply
;
206 /* skip if there's no private data */
207 if (!sroute
->private)
210 connect
= sroute
->private;
213 ret
= sof_ipc_tx_message(sdev
->ipc
,
215 connect
, sizeof(*connect
),
216 &reply
, sizeof(reply
));
219 "error: failed to load route sink %s control %s source %s\n",
221 sroute
->route
->control
? sroute
->route
->control
223 sroute
->route
->source
);
229 /* restore dai links */
230 list_for_each_entry_reverse(dai
, &sdev
->dai_list
, list
) {
231 struct sof_ipc_reply reply
;
232 struct sof_ipc_dai_config
*config
= dai
->dai_config
;
235 dev_err(dev
, "error: no config for DAI %s\n",
241 * The link DMA channel would be invalidated for running
242 * streams but not for streams that were in the PAUSED
243 * state during suspend. So invalidate it here before setting
244 * the dai config in the DSP.
246 if (config
->type
== SOF_DAI_INTEL_HDA
)
247 config
->hda
.link_dma_ch
= DMA_CHAN_INVALID
;
249 ret
= sof_ipc_tx_message(sdev
->ipc
,
250 config
->hdr
.cmd
, config
,
252 &reply
, sizeof(reply
));
256 "error: failed to set dai config for %s\n",
263 /* complete pipeline */
264 list_for_each_entry(swidget
, &sdev
->widget_list
, list
) {
265 switch (swidget
->id
) {
266 case snd_soc_dapm_scheduler
:
268 snd_sof_complete_pipeline(dev
, swidget
);
275 /* restore pipeline kcontrols */
276 ret
= sof_restore_kcontrols(dev
);
279 "error: restoring kcontrols after resume\n");
285 * Generic object lookup APIs.
288 struct snd_sof_pcm
*snd_sof_find_spcm_name(struct snd_soc_component
*scomp
,
291 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
292 struct snd_sof_pcm
*spcm
;
294 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
295 /* match with PCM dai name */
296 if (strcmp(spcm
->pcm
.dai_name
, name
) == 0)
299 /* match with playback caps name if set */
300 if (*spcm
->pcm
.caps
[0].name
&&
301 !strcmp(spcm
->pcm
.caps
[0].name
, name
))
304 /* match with capture caps name if set */
305 if (*spcm
->pcm
.caps
[1].name
&&
306 !strcmp(spcm
->pcm
.caps
[1].name
, name
))
313 struct snd_sof_pcm
*snd_sof_find_spcm_comp(struct snd_soc_component
*scomp
,
314 unsigned int comp_id
,
317 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
318 struct snd_sof_pcm
*spcm
;
321 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
322 for_each_pcm_streams(dir
) {
323 if (spcm
->stream
[dir
].comp_id
== comp_id
) {
333 struct snd_sof_pcm
*snd_sof_find_spcm_pcm_id(struct snd_soc_component
*scomp
,
336 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
337 struct snd_sof_pcm
*spcm
;
339 list_for_each_entry(spcm
, &sdev
->pcm_list
, list
) {
340 if (le32_to_cpu(spcm
->pcm
.pcm_id
) == pcm_id
)
347 struct snd_sof_widget
*snd_sof_find_swidget(struct snd_soc_component
*scomp
,
350 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
351 struct snd_sof_widget
*swidget
;
353 list_for_each_entry(swidget
, &sdev
->widget_list
, list
) {
354 if (strcmp(name
, swidget
->widget
->name
) == 0)
361 /* find widget by stream name and direction */
362 struct snd_sof_widget
*
363 snd_sof_find_swidget_sname(struct snd_soc_component
*scomp
,
364 const char *pcm_name
, int dir
)
366 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
367 struct snd_sof_widget
*swidget
;
368 enum snd_soc_dapm_type type
;
370 if (dir
== SNDRV_PCM_STREAM_PLAYBACK
)
371 type
= snd_soc_dapm_aif_in
;
373 type
= snd_soc_dapm_aif_out
;
375 list_for_each_entry(swidget
, &sdev
->widget_list
, list
) {
376 if (!strcmp(pcm_name
, swidget
->widget
->sname
) &&
384 struct snd_sof_dai
*snd_sof_find_dai(struct snd_soc_component
*scomp
,
387 struct snd_sof_dev
*sdev
= snd_soc_component_get_drvdata(scomp
);
388 struct snd_sof_dai
*dai
;
390 list_for_each_entry(dai
, &sdev
->dai_list
, list
) {
391 if (dai
->name
&& (strcmp(name
, dai
->name
) == 0))
399 * SOF Driver enumeration.
401 int sof_machine_check(struct snd_sof_dev
*sdev
)
403 struct snd_sof_pdata
*sof_pdata
= sdev
->pdata
;
404 const struct sof_dev_desc
*desc
= sof_pdata
->desc
;
405 struct snd_soc_acpi_mach
*mach
;
408 /* force nocodec mode */
409 #if IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)
410 dev_warn(sdev
->dev
, "Force to use nocodec mode\n");
415 snd_sof_machine_select(sdev
);
416 if (sof_pdata
->machine
) {
417 snd_sof_set_mach_params(sof_pdata
->machine
, sdev
->dev
);
421 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)
422 dev_err(sdev
->dev
, "error: no matching ASoC machine driver found - aborting probe\n");
425 #if IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)
428 /* select nocodec mode */
429 dev_warn(sdev
->dev
, "Using nocodec machine driver\n");
430 mach
= devm_kzalloc(sdev
->dev
, sizeof(*mach
), GFP_KERNEL
);
434 mach
->drv_name
= "sof-nocodec";
435 sof_pdata
->tplg_filename
= desc
->nocodec_tplg_filename
;
437 ret
= sof_nocodec_setup(sdev
->dev
, desc
->ops
);
441 sof_pdata
->machine
= mach
;
442 snd_sof_set_mach_params(sof_pdata
->machine
, sdev
->dev
);
446 EXPORT_SYMBOL(sof_machine_check
);
448 int sof_machine_register(struct snd_sof_dev
*sdev
, void *pdata
)
450 struct snd_sof_pdata
*plat_data
= (struct snd_sof_pdata
*)pdata
;
451 const char *drv_name
;
455 drv_name
= plat_data
->machine
->drv_name
;
456 mach
= (const void *)plat_data
->machine
;
457 size
= sizeof(*plat_data
->machine
);
459 /* register machine driver, pass machine info as pdata */
460 plat_data
->pdev_mach
=
461 platform_device_register_data(sdev
->dev
, drv_name
,
462 PLATFORM_DEVID_NONE
, mach
, size
);
463 if (IS_ERR(plat_data
->pdev_mach
))
464 return PTR_ERR(plat_data
->pdev_mach
);
466 dev_dbg(sdev
->dev
, "created machine %s\n",
467 dev_name(&plat_data
->pdev_mach
->dev
));
471 EXPORT_SYMBOL(sof_machine_register
);
473 void sof_machine_unregister(struct snd_sof_dev
*sdev
, void *pdata
)
475 struct snd_sof_pdata
*plat_data
= (struct snd_sof_pdata
*)pdata
;
477 if (!IS_ERR_OR_NULL(plat_data
->pdev_mach
))
478 platform_device_unregister(plat_data
->pdev_mach
);
480 EXPORT_SYMBOL(sof_machine_unregister
);