xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 19bdcc7aeed4169820be6a683c422fc06d030136)
1ed517582SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2ed517582SKuninori Morimoto //
3ed517582SKuninori Morimoto // soc-pcm.c  --  ALSA SoC PCM
4ed517582SKuninori Morimoto //
5ed517582SKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6ed517582SKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7ed517582SKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8ed517582SKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9ed517582SKuninori Morimoto //
10ed517582SKuninori Morimoto // Authors: Liam Girdwood <lrg@ti.com>
11ed517582SKuninori Morimoto //          Mark Brown <broonie@opensource.wolfsonmicro.com>
12ddee627cSLiam Girdwood 
13ddee627cSLiam Girdwood #include <linux/kernel.h>
14ddee627cSLiam Girdwood #include <linux/init.h>
15ddee627cSLiam Girdwood #include <linux/delay.h>
16988e8cc4SNicolin Chen #include <linux/pinctrl/consumer.h>
17d6652ef8SMark Brown #include <linux/pm_runtime.h>
18ddee627cSLiam Girdwood #include <linux/slab.h>
19ddee627cSLiam Girdwood #include <linux/workqueue.h>
2001d7584cSLiam Girdwood #include <linux/export.h>
21f86dcef8SLiam Girdwood #include <linux/debugfs.h>
22ddee627cSLiam Girdwood #include <sound/core.h>
23ddee627cSLiam Girdwood #include <sound/pcm.h>
24ddee627cSLiam Girdwood #include <sound/pcm_params.h>
25ddee627cSLiam Girdwood #include <sound/soc.h>
2601d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
27ddee627cSLiam Girdwood #include <sound/initval.h>
28ddee627cSLiam Girdwood 
2901d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS	8
3001d7584cSLiam Girdwood 
31c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS
32c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
33c3212829SKuninori Morimoto {
34c3212829SKuninori Morimoto 	switch (state) {
35c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_NEW:
36c3212829SKuninori Morimoto 		return "new";
37c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_OPEN:
38c3212829SKuninori Morimoto 		return "open";
39c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_PARAMS:
40c3212829SKuninori Morimoto 		return "hw_params";
41c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PREPARE:
42c3212829SKuninori Morimoto 		return "prepare";
43c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_START:
44c3212829SKuninori Morimoto 		return "start";
45c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_STOP:
46c3212829SKuninori Morimoto 		return "stop";
47c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_SUSPEND:
48c3212829SKuninori Morimoto 		return "suspend";
49c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PAUSED:
50c3212829SKuninori Morimoto 		return "paused";
51c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_FREE:
52c3212829SKuninori Morimoto 		return "hw_free";
53c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_CLOSE:
54c3212829SKuninori Morimoto 		return "close";
55c3212829SKuninori Morimoto 	}
56c3212829SKuninori Morimoto 
57c3212829SKuninori Morimoto 	return "unknown";
58c3212829SKuninori Morimoto }
59c3212829SKuninori Morimoto 
60c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
61c3212829SKuninori Morimoto 			       int stream, char *buf, size_t size)
62c3212829SKuninori Morimoto {
63c3212829SKuninori Morimoto 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
64c3212829SKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
65c3212829SKuninori Morimoto 	ssize_t offset = 0;
66c3212829SKuninori Morimoto 	unsigned long flags;
67c3212829SKuninori Morimoto 
68c3212829SKuninori Morimoto 	/* FE state */
69c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset,
70c3212829SKuninori Morimoto 			   "[%s - %s]\n", fe->dai_link->name,
71c3212829SKuninori Morimoto 			   stream ? "Capture" : "Playback");
72c3212829SKuninori Morimoto 
73c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
74c3212829SKuninori Morimoto 			   dpcm_state_string(fe->dpcm[stream].state));
75c3212829SKuninori Morimoto 
76c3212829SKuninori Morimoto 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
77c3212829SKuninori Morimoto 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
78c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
79c3212829SKuninori Morimoto 				   "Hardware Params: "
80c3212829SKuninori Morimoto 				   "Format = %s, Channels = %d, Rate = %d\n",
81c3212829SKuninori Morimoto 				   snd_pcm_format_name(params_format(params)),
82c3212829SKuninori Morimoto 				   params_channels(params),
83c3212829SKuninori Morimoto 				   params_rate(params));
84c3212829SKuninori Morimoto 
85c3212829SKuninori Morimoto 	/* BEs state */
86c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
87c3212829SKuninori Morimoto 
88c3212829SKuninori Morimoto 	if (list_empty(&fe->dpcm[stream].be_clients)) {
89c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
90c3212829SKuninori Morimoto 				   " No active DSP links\n");
91c3212829SKuninori Morimoto 		goto out;
92c3212829SKuninori Morimoto 	}
93c3212829SKuninori Morimoto 
94c3212829SKuninori Morimoto 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
95c3212829SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
96c3212829SKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
97c3212829SKuninori Morimoto 		params = &dpcm->hw_params;
98c3212829SKuninori Morimoto 
99c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
100c3212829SKuninori Morimoto 				   "- %s\n", be->dai_link->name);
101c3212829SKuninori Morimoto 
102c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
103c3212829SKuninori Morimoto 				   "   State: %s\n",
104c3212829SKuninori Morimoto 				   dpcm_state_string(be->dpcm[stream].state));
105c3212829SKuninori Morimoto 
106c3212829SKuninori Morimoto 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
107c3212829SKuninori Morimoto 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
108c3212829SKuninori Morimoto 			offset += snprintf(buf + offset, size - offset,
109c3212829SKuninori Morimoto 					   "   Hardware Params: "
110c3212829SKuninori Morimoto 					   "Format = %s, Channels = %d, Rate = %d\n",
111c3212829SKuninori Morimoto 					   snd_pcm_format_name(params_format(params)),
112c3212829SKuninori Morimoto 					   params_channels(params),
113c3212829SKuninori Morimoto 					   params_rate(params));
114c3212829SKuninori Morimoto 	}
115c3212829SKuninori Morimoto 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116c3212829SKuninori Morimoto out:
117c3212829SKuninori Morimoto 	return offset;
118c3212829SKuninori Morimoto }
119c3212829SKuninori Morimoto 
120c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
121c3212829SKuninori Morimoto 				    size_t count, loff_t *ppos)
122c3212829SKuninori Morimoto {
123c3212829SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = file->private_data;
124c3212829SKuninori Morimoto 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
125c3212829SKuninori Morimoto 	int stream;
126c3212829SKuninori Morimoto 	char *buf;
127c3212829SKuninori Morimoto 
128c3212829SKuninori Morimoto 	buf = kmalloc(out_count, GFP_KERNEL);
129c3212829SKuninori Morimoto 	if (!buf)
130c3212829SKuninori Morimoto 		return -ENOMEM;
131c3212829SKuninori Morimoto 
132c3212829SKuninori Morimoto 	for_each_pcm_streams(stream)
133c3212829SKuninori Morimoto 		if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
134c3212829SKuninori Morimoto 			offset += dpcm_show_state(fe, stream,
135c3212829SKuninori Morimoto 						  buf + offset,
136c3212829SKuninori Morimoto 						  out_count - offset);
137c3212829SKuninori Morimoto 
138c3212829SKuninori Morimoto 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
139c3212829SKuninori Morimoto 
140c3212829SKuninori Morimoto 	kfree(buf);
141c3212829SKuninori Morimoto 	return ret;
142c3212829SKuninori Morimoto }
143c3212829SKuninori Morimoto 
144c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
145c3212829SKuninori Morimoto 	.open = simple_open,
146c3212829SKuninori Morimoto 	.read = dpcm_state_read_file,
147c3212829SKuninori Morimoto 	.llseek = default_llseek,
148c3212829SKuninori Morimoto };
149c3212829SKuninori Morimoto 
150c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
151c3212829SKuninori Morimoto {
152c3212829SKuninori Morimoto 	if (!rtd->dai_link)
153c3212829SKuninori Morimoto 		return;
154c3212829SKuninori Morimoto 
155c3212829SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
156c3212829SKuninori Morimoto 		return;
157c3212829SKuninori Morimoto 
158c3212829SKuninori Morimoto 	if (!rtd->card->debugfs_card_root)
159c3212829SKuninori Morimoto 		return;
160c3212829SKuninori Morimoto 
161c3212829SKuninori Morimoto 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
162c3212829SKuninori Morimoto 						    rtd->card->debugfs_card_root);
163c3212829SKuninori Morimoto 
164c3212829SKuninori Morimoto 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
165c3212829SKuninori Morimoto 			    rtd, &dpcm_state_fops);
166c3212829SKuninori Morimoto }
167154dae87SKuninori Morimoto 
168154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
169154dae87SKuninori Morimoto {
170154dae87SKuninori Morimoto 	char *name;
171154dae87SKuninori Morimoto 
172154dae87SKuninori Morimoto 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
173154dae87SKuninori Morimoto 			 stream ? "capture" : "playback");
174154dae87SKuninori Morimoto 	if (name) {
175154dae87SKuninori Morimoto 		dpcm->debugfs_state = debugfs_create_dir(
176154dae87SKuninori Morimoto 			name, dpcm->fe->debugfs_dpcm_root);
177154dae87SKuninori Morimoto 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
178154dae87SKuninori Morimoto 				   &dpcm->state);
179154dae87SKuninori Morimoto 		kfree(name);
180154dae87SKuninori Morimoto 	}
181154dae87SKuninori Morimoto }
182154dae87SKuninori Morimoto 
183154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
184154dae87SKuninori Morimoto {
185154dae87SKuninori Morimoto 	debugfs_remove_recursive(dpcm->debugfs_state);
186154dae87SKuninori Morimoto }
187154dae87SKuninori Morimoto 
188154dae87SKuninori Morimoto #else
189154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
190154dae87SKuninori Morimoto 					     int stream)
191154dae87SKuninori Morimoto {
192154dae87SKuninori Morimoto }
193154dae87SKuninori Morimoto 
194154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
195154dae87SKuninori Morimoto {
196154dae87SKuninori Morimoto }
197c3212829SKuninori Morimoto #endif
198c3212829SKuninori Morimoto 
199f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
200f183f927SKuninori Morimoto 			   struct snd_pcm_substream *substream)
201f183f927SKuninori Morimoto {
202f183f927SKuninori Morimoto 	if (rtd->dai_link->ops &&
203f183f927SKuninori Morimoto 	    rtd->dai_link->ops->startup)
204f183f927SKuninori Morimoto 		return rtd->dai_link->ops->startup(substream);
205f183f927SKuninori Morimoto 	return 0;
206f183f927SKuninori Morimoto }
207f183f927SKuninori Morimoto 
2080be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
2090be429f9SKuninori Morimoto 			     struct snd_pcm_substream *substream)
2100be429f9SKuninori Morimoto {
2110be429f9SKuninori Morimoto 	if (rtd->dai_link->ops &&
2120be429f9SKuninori Morimoto 	    rtd->dai_link->ops->shutdown)
2130be429f9SKuninori Morimoto 		rtd->dai_link->ops->shutdown(substream);
2140be429f9SKuninori Morimoto }
2150be429f9SKuninori Morimoto 
21644c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
21744c1a75bSKuninori Morimoto 			   struct snd_pcm_substream *substream)
21844c1a75bSKuninori Morimoto {
21944c1a75bSKuninori Morimoto 	if (rtd->dai_link->ops &&
22044c1a75bSKuninori Morimoto 	    rtd->dai_link->ops->prepare)
22144c1a75bSKuninori Morimoto 		return rtd->dai_link->ops->prepare(substream);
22244c1a75bSKuninori Morimoto 	return 0;
22344c1a75bSKuninori Morimoto }
22444c1a75bSKuninori Morimoto 
225de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
226de9ad990SKuninori Morimoto 			     struct snd_pcm_substream *substream,
227de9ad990SKuninori Morimoto 			     struct snd_pcm_hw_params *params)
228de9ad990SKuninori Morimoto {
229de9ad990SKuninori Morimoto 	if (rtd->dai_link->ops &&
230de9ad990SKuninori Morimoto 	    rtd->dai_link->ops->hw_params)
231de9ad990SKuninori Morimoto 		return rtd->dai_link->ops->hw_params(substream, params);
232de9ad990SKuninori Morimoto 	return 0;
233de9ad990SKuninori Morimoto }
234de9ad990SKuninori Morimoto 
23549f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
23649f020e5SKuninori Morimoto 			    struct snd_pcm_substream *substream)
23749f020e5SKuninori Morimoto {
23849f020e5SKuninori Morimoto 	if (rtd->dai_link->ops &&
23949f020e5SKuninori Morimoto 	    rtd->dai_link->ops->hw_free)
24049f020e5SKuninori Morimoto 		rtd->dai_link->ops->hw_free(substream);
24149f020e5SKuninori Morimoto }
24249f020e5SKuninori Morimoto 
243ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
244ad2bf9f2SKuninori Morimoto 			   struct snd_pcm_substream *substream,
245ad2bf9f2SKuninori Morimoto 			   int cmd)
246ad2bf9f2SKuninori Morimoto {
247ad2bf9f2SKuninori Morimoto 	if (rtd->dai_link->ops &&
248ad2bf9f2SKuninori Morimoto 	    rtd->dai_link->ops->trigger)
249ad2bf9f2SKuninori Morimoto 		return rtd->dai_link->ops->trigger(substream, cmd);
250ad2bf9f2SKuninori Morimoto 	return 0;
251ad2bf9f2SKuninori Morimoto }
252ad2bf9f2SKuninori Morimoto 
2537a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2547a5aaba4SKuninori Morimoto 				   int stream, int action)
2557a5aaba4SKuninori Morimoto {
256*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
2577a5aaba4SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
2587a5aaba4SKuninori Morimoto 	int i;
2597a5aaba4SKuninori Morimoto 
2607a5aaba4SKuninori Morimoto 	lockdep_assert_held(&rtd->card->pcm_mutex);
2617a5aaba4SKuninori Morimoto 
262*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
2630f6011fdSKuninori Morimoto 		cpu_dai->stream_active[stream] += action;
264*19bdcc7aSShreyas NC 
2657a5aaba4SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2660f6011fdSKuninori Morimoto 		codec_dai->stream_active[stream] += action;
2677a5aaba4SKuninori Morimoto 
268*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
2697a5aaba4SKuninori Morimoto 		cpu_dai->active += action;
2707a5aaba4SKuninori Morimoto 		cpu_dai->component->active += action;
271*19bdcc7aSShreyas NC 	}
2727a5aaba4SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
2737a5aaba4SKuninori Morimoto 		codec_dai->active += action;
2747a5aaba4SKuninori Morimoto 		codec_dai->component->active += action;
2757a5aaba4SKuninori Morimoto 	}
2767a5aaba4SKuninori Morimoto }
2777a5aaba4SKuninori Morimoto 
27890996f43SLars-Peter Clausen /**
27924894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
28024894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
28124894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
28224894b76SLars-Peter Clausen  *
28324894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
28424894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
28524894b76SLars-Peter Clausen  *
28672b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
28724894b76SLars-Peter Clausen  */
28824894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
28924894b76SLars-Peter Clausen {
2907a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, 1);
29124894b76SLars-Peter Clausen }
29224894b76SLars-Peter Clausen 
29324894b76SLars-Peter Clausen /**
29424894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
29524894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
29624894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
29724894b76SLars-Peter Clausen  *
29824894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
29924894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
30024894b76SLars-Peter Clausen  *
30172b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
30224894b76SLars-Peter Clausen  */
30324894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
30424894b76SLars-Peter Clausen {
3057a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, -1);
30624894b76SLars-Peter Clausen }
30724894b76SLars-Peter Clausen 
30824894b76SLars-Peter Clausen /**
309208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
310208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
311208a1589SLars-Peter Clausen  *
312208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
313208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
314208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
315208a1589SLars-Peter Clausen  * from having the delay.
316208a1589SLars-Peter Clausen  */
317208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
318208a1589SLars-Peter Clausen {
319fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
3202e5894d7SBenoit Cousson 	bool ignore = true;
321613fb500SKuninori Morimoto 	int i;
3222e5894d7SBenoit Cousson 
323208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
324208a1589SLars-Peter Clausen 		return true;
325208a1589SLars-Peter Clausen 
326613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
32772c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
328fbb16563SKuninori Morimoto 
329fbb16563SKuninori Morimoto 	return ignore;
330208a1589SLars-Peter Clausen }
331208a1589SLars-Peter Clausen 
332208a1589SLars-Peter Clausen /**
33390996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
33490996f43SLars-Peter Clausen  * @substream: the pcm substream
33590996f43SLars-Peter Clausen  * @hw: the hardware parameters
33690996f43SLars-Peter Clausen  *
33790996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
33890996f43SLars-Peter Clausen  */
33990996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
34090996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
34190996f43SLars-Peter Clausen {
34290996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
34390996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
34490996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
34590996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
34690996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
34790996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
34890996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
34990996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
35090996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
35190996f43SLars-Peter Clausen 	return 0;
35290996f43SLars-Peter Clausen }
35390996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
35490996f43SLars-Peter Clausen 
35501d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
35623607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
35701d7584cSLiam Girdwood 	int event)
35801d7584cSLiam Girdwood {
35901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
36001d7584cSLiam Girdwood 
3618d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
36201d7584cSLiam Girdwood 
36301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
36401d7584cSLiam Girdwood 
365103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
36601d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
36701d7584cSLiam Girdwood 
368b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
369b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
370b1cd2e34SBanajit Goswami 			continue;
371b1cd2e34SBanajit Goswami 
37201d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
37301d7584cSLiam Girdwood 	}
37401d7584cSLiam Girdwood 
37501d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
37601d7584cSLiam Girdwood 
37701d7584cSLiam Girdwood 	return 0;
37801d7584cSLiam Girdwood }
37901d7584cSLiam Girdwood 
38017841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
38117841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
382ddee627cSLiam Girdwood {
383ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
384ddee627cSLiam Girdwood 	int ret;
385ddee627cSLiam Girdwood 
3863635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
3873635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
3883635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
3893635bf09SNicolin Chen 				soc_dai->rate);
390ddee627cSLiam Girdwood 
3914dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
392ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
3934dcdd43bSLars-Peter Clausen 						soc_dai->rate);
394ddee627cSLiam Girdwood 		if (ret < 0) {
39517841020SDong Aisheng 			dev_err(soc_dai->dev,
3963635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
397103d84a3SLiam Girdwood 				ret);
398ddee627cSLiam Girdwood 			return ret;
399ddee627cSLiam Girdwood 		}
4003635bf09SNicolin Chen 	}
4013635bf09SNicolin Chen 
4023635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
4033635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
4043635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
4053635bf09SNicolin Chen 				soc_dai->channels);
4063635bf09SNicolin Chen 
4074dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4083635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
4093635bf09SNicolin Chen 						soc_dai->channels);
4103635bf09SNicolin Chen 		if (ret < 0) {
4113635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4123635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
4133635bf09SNicolin Chen 				ret);
4143635bf09SNicolin Chen 			return ret;
4153635bf09SNicolin Chen 		}
4163635bf09SNicolin Chen 	}
4173635bf09SNicolin Chen 
4183635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
4193635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
4203635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
4213635bf09SNicolin Chen 				soc_dai->sample_bits);
4223635bf09SNicolin Chen 
4234dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4243635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
4253635bf09SNicolin Chen 						soc_dai->sample_bits);
4263635bf09SNicolin Chen 		if (ret < 0) {
4273635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4283635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
4293635bf09SNicolin Chen 				ret);
4303635bf09SNicolin Chen 			return ret;
4313635bf09SNicolin Chen 		}
4323635bf09SNicolin Chen 	}
433ddee627cSLiam Girdwood 
434ddee627cSLiam Girdwood 	return 0;
435ddee627cSLiam Girdwood }
436ddee627cSLiam Girdwood 
4373635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4383635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4393635bf09SNicolin Chen {
4403635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
441*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4420b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
4432e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
4443635bf09SNicolin Chen 
4453635bf09SNicolin Chen 	rate = params_rate(params);
4463635bf09SNicolin Chen 	channels = params_channels(params);
4473635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
4483635bf09SNicolin Chen 
4493635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
450*19bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_rates;
451*19bdcc7aSShreyas NC 
452*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
453*19bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_rates;
4542e5894d7SBenoit Cousson 
4550b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4560b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_rates;
4572e5894d7SBenoit Cousson 
458*19bdcc7aSShreyas NC 	if (symmetry) {
459*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
460*19bdcc7aSShreyas NC 			if (cpu_dai->rate && cpu_dai->rate != rate) {
4613635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
4623635bf09SNicolin Chen 					cpu_dai->rate, rate);
4633635bf09SNicolin Chen 				return -EINVAL;
4643635bf09SNicolin Chen 			}
465*19bdcc7aSShreyas NC 		}
466*19bdcc7aSShreyas NC 	}
4673635bf09SNicolin Chen 
468*19bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_channels;
469*19bdcc7aSShreyas NC 
470*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
471*19bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_channels;
4722e5894d7SBenoit Cousson 
4730b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4740b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_channels;
4752e5894d7SBenoit Cousson 
476*19bdcc7aSShreyas NC 	if (symmetry) {
477*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
478*19bdcc7aSShreyas NC 			if (cpu_dai->channels &&
479*19bdcc7aSShreyas NC 			    cpu_dai->channels != channels) {
4803635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
4813635bf09SNicolin Chen 					cpu_dai->channels, channels);
4823635bf09SNicolin Chen 				return -EINVAL;
4833635bf09SNicolin Chen 			}
484*19bdcc7aSShreyas NC 		}
485*19bdcc7aSShreyas NC 	}
4863635bf09SNicolin Chen 
487*19bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_samplebits;
488*19bdcc7aSShreyas NC 
489*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
490*19bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_samplebits;
4912e5894d7SBenoit Cousson 
4920b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4930b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_samplebits;
4942e5894d7SBenoit Cousson 
495*19bdcc7aSShreyas NC 	if (symmetry) {
496*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
497*19bdcc7aSShreyas NC 			if (cpu_dai->sample_bits &&
498*19bdcc7aSShreyas NC 			    cpu_dai->sample_bits != sample_bits) {
4993635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
5003635bf09SNicolin Chen 					cpu_dai->sample_bits, sample_bits);
5013635bf09SNicolin Chen 				return -EINVAL;
5023635bf09SNicolin Chen 			}
503*19bdcc7aSShreyas NC 		}
504*19bdcc7aSShreyas NC 	}
505ddee627cSLiam Girdwood 
506ddee627cSLiam Girdwood 	return 0;
507ddee627cSLiam Girdwood }
508ddee627cSLiam Girdwood 
50962e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
51062e5f676SLars-Peter Clausen {
51162e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
51262e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
5130b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
514*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5152e5894d7SBenoit Cousson 	unsigned int symmetry, i;
51662e5f676SLars-Peter Clausen 
517*19bdcc7aSShreyas NC 	symmetry = link->symmetric_rates ||
518*19bdcc7aSShreyas NC 		link->symmetric_channels ||
519*19bdcc7aSShreyas NC 		link->symmetric_samplebits;
520*19bdcc7aSShreyas NC 
521*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
522*19bdcc7aSShreyas NC 		symmetry = symmetry ||
523*19bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_rates ||
524*19bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_channels ||
525*19bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_samplebits;
5262e5894d7SBenoit Cousson 
5270b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
5282e5894d7SBenoit Cousson 		symmetry = symmetry ||
5290b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_rates ||
5300b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_channels ||
5310b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_samplebits;
5322e5894d7SBenoit Cousson 
5332e5894d7SBenoit Cousson 	return symmetry;
53462e5f676SLars-Peter Clausen }
53562e5f676SLars-Peter Clausen 
5362e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
53758ba9b25SMark Brown {
5382e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
539c6068d3aSTakashi Iwai 	int ret;
54058ba9b25SMark Brown 
54158ba9b25SMark Brown 	if (!bits)
54258ba9b25SMark Brown 		return;
54358ba9b25SMark Brown 
5440e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
54558ba9b25SMark Brown 	if (ret != 0)
5460e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
5470e2a3751SLars-Peter Clausen 				 bits, ret);
54858ba9b25SMark Brown }
54958ba9b25SMark Brown 
550c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
551bd477c31SLars-Peter Clausen {
552c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
553*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5542e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
55557be9206SKuninori Morimoto 	struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
55657be9206SKuninori Morimoto 	int stream = substream->stream;
5572e5894d7SBenoit Cousson 	int i;
558*19bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
559c8dd1fecSBenoit Cousson 
5600b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
56157be9206SKuninori Morimoto 		pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
56257be9206SKuninori Morimoto 
56357be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
5642e5894d7SBenoit Cousson 			bits = 0;
5652e5894d7SBenoit Cousson 			break;
5662e5894d7SBenoit Cousson 		}
56757be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
5682e5894d7SBenoit Cousson 	}
56957be9206SKuninori Morimoto 
570*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
57157be9206SKuninori Morimoto 		pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
572*19bdcc7aSShreyas NC 
573*19bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
574*19bdcc7aSShreyas NC 			cpu_bits = 0;
575*19bdcc7aSShreyas NC 			break;
576*19bdcc7aSShreyas NC 		}
577*19bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
578*19bdcc7aSShreyas NC 	}
579c8dd1fecSBenoit Cousson 
5802e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5812e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
582c8dd1fecSBenoit Cousson }
583c8dd1fecSBenoit Cousson 
5842e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
585bd477c31SLars-Peter Clausen {
5862e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
58778e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
5882e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
5890b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
590*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5912e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5922e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
5932e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
594*19bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5952e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
596*19bdcc7aSShreyas NC 	unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
597*19bdcc7aSShreyas NC 	unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
5982e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
599acf253c1SKuninori Morimoto 	int stream = substream->stream;
6002e5894d7SBenoit Cousson 	int i;
60178e45c99SLars-Peter Clausen 
602*19bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
603*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
604*19bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
60578e45c99SLars-Peter Clausen 
606*19bdcc7aSShreyas NC 		cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
607*19bdcc7aSShreyas NC 		cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
608*19bdcc7aSShreyas NC 		cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
609*19bdcc7aSShreyas NC 		cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
610*19bdcc7aSShreyas NC 		formats &= cpu_stream->formats;
611*19bdcc7aSShreyas NC 		cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
612*19bdcc7aSShreyas NC 							cpu_rates);
613*19bdcc7aSShreyas NC 	}
614*19bdcc7aSShreyas NC 
615*19bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
6160b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
617cde79035SRicard Wanderlof 
618cde79035SRicard Wanderlof 		/*
619cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
620cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
621cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
622cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
623cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
624cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
625cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
626cde79035SRicard Wanderlof 		 */
6270b7990e3SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai,
628cde79035SRicard Wanderlof 					      substream->stream))
629cde79035SRicard Wanderlof 			continue;
630cde79035SRicard Wanderlof 
631acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
632acf253c1SKuninori Morimoto 
6332e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
6342e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
6352e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
6362e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
6372e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
6382e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
6392e5894d7SBenoit Cousson 	}
6402e5894d7SBenoit Cousson 
6412e5894d7SBenoit Cousson 	/*
6422e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
643*19bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6442e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6452e5894d7SBenoit Cousson 	 */
6462e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
647*19bdcc7aSShreyas NC 		chan_min = cpu_chan_min;
648*19bdcc7aSShreyas NC 		chan_max = cpu_chan_max;
6492e5894d7SBenoit Cousson 	}
6502e5894d7SBenoit Cousson 
651*19bdcc7aSShreyas NC 	/* finally find a intersection between CODECs and CPUs */
652*19bdcc7aSShreyas NC 	hw->channels_min = max(chan_min, cpu_chan_min);
653*19bdcc7aSShreyas NC 	hw->channels_max = min(chan_max, cpu_chan_max);
6542e5894d7SBenoit Cousson 	if (hw->formats)
655*19bdcc7aSShreyas NC 		hw->formats &= formats;
6562e5894d7SBenoit Cousson 	else
657*19bdcc7aSShreyas NC 		hw->formats = formats;
658*19bdcc7aSShreyas NC 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
65978e45c99SLars-Peter Clausen 
66078e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
66178e45c99SLars-Peter Clausen 
662*19bdcc7aSShreyas NC 	hw->rate_min = max(hw->rate_min, cpu_rate_min);
6632e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
664*19bdcc7aSShreyas NC 	hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
6652e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
666bd477c31SLars-Peter Clausen }
667bd477c31SLars-Peter Clausen 
668dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
669e7ecfdb7SKuninori Morimoto {
670e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
671d2aaa8d8SKai Vehmanen 	struct snd_soc_component *last = NULL;
672e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
673613fb500SKuninori Morimoto 	int i, ret = 0;
674e7ecfdb7SKuninori Morimoto 
675613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
676d2aaa8d8SKai Vehmanen 		last = component;
677d2aaa8d8SKai Vehmanen 
6784a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
6794a81e8f3SKuninori Morimoto 		if (ret < 0) {
680e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
681e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
682e7ecfdb7SKuninori Morimoto 				component->name);
683d2aaa8d8SKai Vehmanen 			break;
684e7ecfdb7SKuninori Morimoto 		}
685e7ecfdb7SKuninori Morimoto 
686ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
687e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
688d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
689e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
690e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
691e7ecfdb7SKuninori Morimoto 				component->name, ret);
692d2aaa8d8SKai Vehmanen 			break;
693e7ecfdb7SKuninori Morimoto 		}
694e7ecfdb7SKuninori Morimoto 	}
695dd03907bSKuninori Morimoto 
696d2aaa8d8SKai Vehmanen 	if (ret < 0) {
697d2aaa8d8SKai Vehmanen 		/* rollback on error */
698d2aaa8d8SKai Vehmanen 		for_each_rtd_components(rtd, i, component) {
699d2aaa8d8SKai Vehmanen 			if (component == last)
700d2aaa8d8SKai Vehmanen 				break;
701d2aaa8d8SKai Vehmanen 
702d2aaa8d8SKai Vehmanen 			snd_soc_component_close(component, substream);
703d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
704d2aaa8d8SKai Vehmanen 		}
705d2aaa8d8SKai Vehmanen 	}
706d2aaa8d8SKai Vehmanen 
707d2aaa8d8SKai Vehmanen 	return ret;
708e7ecfdb7SKuninori Morimoto }
709e7ecfdb7SKuninori Morimoto 
710dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream)
711244e2936SCharles Keepax {
712244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
713244e2936SCharles Keepax 	struct snd_soc_component *component;
714e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
715244e2936SCharles Keepax 
716613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
717e82ebffcSKuninori Morimoto 		r = snd_soc_component_close(component, substream);
718e82ebffcSKuninori Morimoto 		if (r < 0)
719e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
720e82ebffcSKuninori Morimoto 
7214a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
722244e2936SCharles Keepax 	}
723244e2936SCharles Keepax 
7243672beb8SKuninori Morimoto 	return ret;
725244e2936SCharles Keepax }
726244e2936SCharles Keepax 
72758ba9b25SMark Brown /*
72862c86d1dSKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
72962c86d1dSKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
73062c86d1dSKuninori Morimoto  * shutdown.
73162c86d1dSKuninori Morimoto  */
73262c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
73362c86d1dSKuninori Morimoto {
73462c86d1dSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
73562c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
736*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
73762c86d1dSKuninori Morimoto 	struct snd_soc_dai *codec_dai;
73862c86d1dSKuninori Morimoto 	int i;
73962c86d1dSKuninori Morimoto 
74062c86d1dSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
74162c86d1dSKuninori Morimoto 
74262c86d1dSKuninori Morimoto 	snd_soc_runtime_deactivate(rtd, substream->stream);
74362c86d1dSKuninori Morimoto 
744*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
74562c86d1dSKuninori Morimoto 		snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
74662c86d1dSKuninori Morimoto 
747*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
74862c86d1dSKuninori Morimoto 		snd_soc_dai_shutdown(cpu_dai, substream);
74962c86d1dSKuninori Morimoto 
75062c86d1dSKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
75162c86d1dSKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
75262c86d1dSKuninori Morimoto 
75362c86d1dSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
75462c86d1dSKuninori Morimoto 
75562c86d1dSKuninori Morimoto 	soc_pcm_components_close(substream);
75662c86d1dSKuninori Morimoto 
75762c86d1dSKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
75862c86d1dSKuninori Morimoto 
75962c86d1dSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
76062c86d1dSKuninori Morimoto 
76162c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
76262c86d1dSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
76362c86d1dSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
76462c86d1dSKuninori Morimoto 	}
76562c86d1dSKuninori Morimoto 
76662c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
76762c86d1dSKuninori Morimoto 		if (!component->active)
76862c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
76962c86d1dSKuninori Morimoto 
77062c86d1dSKuninori Morimoto 	return 0;
77162c86d1dSKuninori Morimoto }
77262c86d1dSKuninori Morimoto 
77362c86d1dSKuninori Morimoto /*
774ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
775ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
776ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
777ddee627cSLiam Girdwood  */
778ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
779ddee627cSLiam Girdwood {
780ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
781ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
78290be711eSKuninori Morimoto 	struct snd_soc_component *component;
783*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
7842e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7852e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
786*19bdcc7aSShreyas NC 	const char *cpu_dai_name = "multicpu";
787244e2936SCharles Keepax 	int i, ret = 0;
788ddee627cSLiam Girdwood 
78976c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
79076c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
79190be711eSKuninori Morimoto 
792613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
79390be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
794d6652ef8SMark Brown 
79572b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
796ddee627cSLiam Girdwood 
7975d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
7985d9fa03eSKuninori Morimoto 	if (ret < 0)
7995d9fa03eSKuninori Morimoto 		goto component_err;
8005d9fa03eSKuninori Morimoto 
8015d9fa03eSKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
8025d9fa03eSKuninori Morimoto 	if (ret < 0) {
8035d9fa03eSKuninori Morimoto 		pr_err("ASoC: %s startup failed: %d\n",
8045d9fa03eSKuninori Morimoto 		       rtd->dai_link->name, ret);
805d2aaa8d8SKai Vehmanen 		goto rtd_startup_err;
8065d9fa03eSKuninori Morimoto 	}
8075d9fa03eSKuninori Morimoto 
808ddee627cSLiam Girdwood 	/* startup the audio subsystem */
809*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
8105a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(cpu_dai, substream);
811ddee627cSLiam Girdwood 		if (ret < 0) {
8125a52a045SKuninori Morimoto 			dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
8135a52a045SKuninori Morimoto 				cpu_dai->name, ret);
8145d9fa03eSKuninori Morimoto 			goto cpu_dai_err;
815ddee627cSLiam Girdwood 		}
816*19bdcc7aSShreyas NC 	}
817ddee627cSLiam Girdwood 
8180b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8195a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(codec_dai, substream);
820ddee627cSLiam Girdwood 		if (ret < 0) {
8212e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
8222e5894d7SBenoit Cousson 				"ASoC: can't open codec %s: %d\n",
8232e5894d7SBenoit Cousson 				codec_dai->name, ret);
8245d9fa03eSKuninori Morimoto 			goto config_err;
825ddee627cSLiam Girdwood 		}
826ddee627cSLiam Girdwood 
8272e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
8282e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
8292e5894d7SBenoit Cousson 		else
8302e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
8312e5894d7SBenoit Cousson 	}
8322e5894d7SBenoit Cousson 
83301d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
83401d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
83501d7584cSLiam Girdwood 		goto dynamic;
83601d7584cSLiam Girdwood 
837ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8382e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8392e5894d7SBenoit Cousson 
8402e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
8412e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
842ddee627cSLiam Girdwood 
843*19bdcc7aSShreyas NC 	if (rtd->num_cpus == 1)
844*19bdcc7aSShreyas NC 		cpu_dai_name = rtd->cpu_dai->name;
845*19bdcc7aSShreyas NC 
84662e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
84762e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
84862e5f676SLars-Peter Clausen 
849ddee627cSLiam Girdwood 	ret = -EINVAL;
850ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
851103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
852*19bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
853ddee627cSLiam Girdwood 		goto config_err;
854ddee627cSLiam Girdwood 	}
855ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
856103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
857*19bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
858ddee627cSLiam Girdwood 		goto config_err;
859ddee627cSLiam Girdwood 	}
860ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
861ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
862103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
863*19bdcc7aSShreyas NC 				codec_dai_name, cpu_dai_name);
864ddee627cSLiam Girdwood 		goto config_err;
865ddee627cSLiam Girdwood 	}
866ddee627cSLiam Girdwood 
867c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
86858ba9b25SMark Brown 
869ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
870*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
87117841020SDong Aisheng 		if (cpu_dai->active) {
87217841020SDong Aisheng 			ret = soc_pcm_apply_symmetry(substream, cpu_dai);
87317841020SDong Aisheng 			if (ret != 0)
87417841020SDong Aisheng 				goto config_err;
87517841020SDong Aisheng 		}
876*19bdcc7aSShreyas NC 	}
87717841020SDong Aisheng 
8780b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8790b7990e3SKuninori Morimoto 		if (codec_dai->active) {
8800b7990e3SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, codec_dai);
881ddee627cSLiam Girdwood 			if (ret != 0)
882ddee627cSLiam Girdwood 				goto config_err;
883ddee627cSLiam Girdwood 		}
8842e5894d7SBenoit Cousson 	}
885ddee627cSLiam Girdwood 
886103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
887*19bdcc7aSShreyas NC 		 codec_dai_name, cpu_dai_name);
888103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
889103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
890ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
891103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
892ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
893ddee627cSLiam Girdwood 
89401d7584cSLiam Girdwood dynamic:
89524894b76SLars-Peter Clausen 
89624894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
89724894b76SLars-Peter Clausen 
89872b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
899ddee627cSLiam Girdwood 	return 0;
900ddee627cSLiam Girdwood 
901ddee627cSLiam Girdwood config_err:
902b56be800SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
903330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
9045d9fa03eSKuninori Morimoto cpu_dai_err:
905*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
9065d9fa03eSKuninori Morimoto 		snd_soc_dai_shutdown(cpu_dai, substream);
9072e5894d7SBenoit Cousson 
9085d9fa03eSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
909d2aaa8d8SKai Vehmanen rtd_startup_err:
910dd03907bSKuninori Morimoto 	soc_pcm_components_close(substream);
911d2aaa8d8SKai Vehmanen component_err:
91272b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
913d6652ef8SMark Brown 
914613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
91590be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
91690be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
9173f809783SSanyog Kale 	}
9183f809783SSanyog Kale 
91976c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
92076c39e86SKuninori Morimoto 		if (!component->active)
92176c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
922d6652ef8SMark Brown 
923ddee627cSLiam Girdwood 	return ret;
924ddee627cSLiam Girdwood }
925ddee627cSLiam Girdwood 
9264bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
927a342031cSJerome Brunet {
928a342031cSJerome Brunet 	/*
929a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
930a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
931a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
932a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
933a342031cSJerome Brunet 	 */
934a342031cSJerome Brunet }
935a342031cSJerome Brunet 
936ddee627cSLiam Girdwood /*
937ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
938ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
939ddee627cSLiam Girdwood  * it can refer to the runtime info.
940ddee627cSLiam Girdwood  */
941ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
942ddee627cSLiam Girdwood {
943ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
944b8135864SKuninori Morimoto 	struct snd_soc_component *component;
945*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
9462e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9472e5894d7SBenoit Cousson 	int i, ret = 0;
948ddee627cSLiam Girdwood 
94972b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
950ddee627cSLiam Girdwood 
95144c1a75bSKuninori Morimoto 	ret = soc_rtd_prepare(rtd, substream);
952ddee627cSLiam Girdwood 	if (ret < 0) {
95344c1a75bSKuninori Morimoto 		dev_err(rtd->card->dev,
95444c1a75bSKuninori Morimoto 			"ASoC: machine prepare error: %d\n", ret);
955ddee627cSLiam Girdwood 		goto out;
956ddee627cSLiam Girdwood 	}
957ddee627cSLiam Girdwood 
958613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
9596d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
960b8135864SKuninori Morimoto 		if (ret < 0) {
961b8135864SKuninori Morimoto 			dev_err(component->dev,
962b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
963b8135864SKuninori Morimoto 			goto out;
964b8135864SKuninori Morimoto 		}
965b8135864SKuninori Morimoto 	}
966b8135864SKuninori Morimoto 
9670b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
9684beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(codec_dai, substream);
969ddee627cSLiam Girdwood 		if (ret < 0) {
9702e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
97190cc7f1cSJarkko Nikula 				"ASoC: codec DAI prepare error: %d\n",
97290cc7f1cSJarkko Nikula 				ret);
973ddee627cSLiam Girdwood 			goto out;
974ddee627cSLiam Girdwood 		}
975ddee627cSLiam Girdwood 	}
976ddee627cSLiam Girdwood 
977*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
9784beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(cpu_dai, substream);
979ddee627cSLiam Girdwood 		if (ret < 0) {
98090cc7f1cSJarkko Nikula 			dev_err(cpu_dai->dev,
98190cc7f1cSJarkko Nikula 				"ASoC: cpu DAI prepare error: %d\n", ret);
982ddee627cSLiam Girdwood 			goto out;
983ddee627cSLiam Girdwood 		}
984*19bdcc7aSShreyas NC 	}
985ddee627cSLiam Girdwood 
986ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
987ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9889bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
9899bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
990ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
991ddee627cSLiam Girdwood 	}
992ddee627cSLiam Girdwood 
993d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
994ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
995ddee627cSLiam Girdwood 
9960b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
9970b7990e3SKuninori Morimoto 		snd_soc_dai_digital_mute(codec_dai, 0,
9982e5894d7SBenoit Cousson 					 substream->stream);
999*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
1000ae11601bSRamesh Babu 		snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
1001ddee627cSLiam Girdwood 
1002ddee627cSLiam Girdwood out:
100372b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1004ddee627cSLiam Girdwood 	return ret;
1005ddee627cSLiam Girdwood }
1006ddee627cSLiam Girdwood 
10072e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
10082e5894d7SBenoit Cousson 				       unsigned int mask)
10092e5894d7SBenoit Cousson {
10102e5894d7SBenoit Cousson 	struct snd_interval *interval;
10112e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
10122e5894d7SBenoit Cousson 
10132e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
10142e5894d7SBenoit Cousson 	interval->min = channels;
10152e5894d7SBenoit Cousson 	interval->max = channels;
10162e5894d7SBenoit Cousson }
10172e5894d7SBenoit Cousson 
1018244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
1019244e2936SCharles Keepax 				      struct snd_soc_component *last)
1020244e2936SCharles Keepax {
1021244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1022244e2936SCharles Keepax 	struct snd_soc_component *component;
1023e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
1024244e2936SCharles Keepax 
1025613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1026244e2936SCharles Keepax 		if (component == last)
1027244e2936SCharles Keepax 			break;
1028244e2936SCharles Keepax 
1029e82ebffcSKuninori Morimoto 		r = snd_soc_component_hw_free(component, substream);
1030e82ebffcSKuninori Morimoto 		if (r < 0)
1031e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
1032244e2936SCharles Keepax 	}
1033244e2936SCharles Keepax 
1034eae7136aSKuninori Morimoto 	return ret;
1035244e2936SCharles Keepax }
1036244e2936SCharles Keepax 
1037ddee627cSLiam Girdwood /*
1038ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
1039ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
1040ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
1041ddee627cSLiam Girdwood  */
1042ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1043ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
1044ddee627cSLiam Girdwood {
1045ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1046b8135864SKuninori Morimoto 	struct snd_soc_component *component;
1047*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
10480b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1049244e2936SCharles Keepax 	int i, ret = 0;
1050ddee627cSLiam Girdwood 
105172b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
10525cca5951SShengjiu Wang 
10535cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
10545cca5951SShengjiu Wang 	if (ret)
10555cca5951SShengjiu Wang 		goto out;
10565cca5951SShengjiu Wang 
1057de9ad990SKuninori Morimoto 	ret = soc_rtd_hw_params(rtd, substream, params);
1058ddee627cSLiam Girdwood 	if (ret < 0) {
1059de9ad990SKuninori Morimoto 		dev_err(rtd->card->dev,
1060de9ad990SKuninori Morimoto 			"ASoC: machine hw_params failed: %d\n", ret);
1061ddee627cSLiam Girdwood 		goto out;
1062ddee627cSLiam Girdwood 	}
1063ddee627cSLiam Girdwood 
10640b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10652e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
10662e5894d7SBenoit Cousson 
1067cde79035SRicard Wanderlof 		/*
1068cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1069cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1070cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1071cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1072cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1073cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1074cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1075cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1076cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1077cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1078cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1079cde79035SRicard Wanderlof 		 * we can do the right thing here.
1080cde79035SRicard Wanderlof 		 */
1081cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1082cde79035SRicard Wanderlof 			continue;
1083cde79035SRicard Wanderlof 
10842e5894d7SBenoit Cousson 		/* copy params for each codec */
10852e5894d7SBenoit Cousson 		codec_params = *params;
10862e5894d7SBenoit Cousson 
10872e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1088570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1089570f18b6SRander Wang 		    codec_dai->tx_mask)
10902e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10912e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
1092570f18b6SRander Wang 
1093570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1094570f18b6SRander Wang 		    codec_dai->rx_mask)
10952e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10962e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
10972e5894d7SBenoit Cousson 
1098aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1099aa6166c2SKuninori Morimoto 					    &codec_params);
110093e6958aSBenoit Cousson 		if(ret < 0)
1101ddee627cSLiam Girdwood 			goto codec_err;
1102ddee627cSLiam Girdwood 
11032e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
11042e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
11052e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
11062e5894d7SBenoit Cousson 						params_format(&codec_params));
1107078a85f2SCharles Keepax 
1108078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1109ddee627cSLiam Girdwood 	}
1110ddee627cSLiam Girdwood 
1111*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1112aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
111393e6958aSBenoit Cousson 		if (ret < 0)
1114ddee627cSLiam Girdwood 			goto interface_err;
1115ddee627cSLiam Girdwood 
1116*19bdcc7aSShreyas NC 		/* store the parameters for each DAI */
1117ca58221dSKuninori Morimoto 		cpu_dai->rate = params_rate(params);
1118ca58221dSKuninori Morimoto 		cpu_dai->channels = params_channels(params);
1119ca58221dSKuninori Morimoto 		cpu_dai->sample_bits =
1120ca58221dSKuninori Morimoto 			snd_pcm_format_physical_width(params_format(params));
1121ca58221dSKuninori Morimoto 
1122ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
1123*19bdcc7aSShreyas NC 	}
1124ca58221dSKuninori Morimoto 
1125613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1126245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
1127244e2936SCharles Keepax 		if (ret < 0) {
1128b8135864SKuninori Morimoto 			dev_err(component->dev,
1129b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
1130244e2936SCharles Keepax 				component->name, ret);
1131b8135864SKuninori Morimoto 			goto component_err;
1132244e2936SCharles Keepax 		}
1133244e2936SCharles Keepax 	}
1134244e2936SCharles Keepax 	component = NULL;
1135b8135864SKuninori Morimoto 
1136ddee627cSLiam Girdwood out:
113772b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1138ddee627cSLiam Girdwood 	return ret;
1139ddee627cSLiam Girdwood 
1140b8135864SKuninori Morimoto component_err:
1141244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
1142b8135864SKuninori Morimoto 
1143*19bdcc7aSShreyas NC 	i = rtd->num_cpus;
1144ddee627cSLiam Girdwood 
1145ddee627cSLiam Girdwood interface_err:
1146*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai_rollback(rtd, i, cpu_dai) {
1147*19bdcc7aSShreyas NC 		snd_soc_dai_hw_free(cpu_dai, substream);
1148*19bdcc7aSShreyas NC 		cpu_dai->rate = 0;
1149*19bdcc7aSShreyas NC 	}
1150*19bdcc7aSShreyas NC 
11512e5894d7SBenoit Cousson 	i = rtd->num_codecs;
1152ddee627cSLiam Girdwood 
1153ddee627cSLiam Girdwood codec_err:
11546d11b128SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1155f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1156f47b9ad9SJerome Brunet 			continue;
1157f47b9ad9SJerome Brunet 
1158846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
11592e5894d7SBenoit Cousson 		codec_dai->rate = 0;
11602e5894d7SBenoit Cousson 	}
11612e5894d7SBenoit Cousson 
116249f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1163ddee627cSLiam Girdwood 
116472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1165ddee627cSLiam Girdwood 	return ret;
1166ddee627cSLiam Girdwood }
1167ddee627cSLiam Girdwood 
1168ddee627cSLiam Girdwood /*
1169ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
1170ddee627cSLiam Girdwood  */
1171ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1172ddee627cSLiam Girdwood {
1173ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1174*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
11752e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
11767f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
11772e5894d7SBenoit Cousson 	int i;
1178ddee627cSLiam Girdwood 
117972b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1180ddee627cSLiam Girdwood 
1181d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
1182*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1183d3383420SNicolin Chen 		if (cpu_dai->active == 1) {
1184d3383420SNicolin Chen 			cpu_dai->rate = 0;
1185d3383420SNicolin Chen 			cpu_dai->channels = 0;
1186d3383420SNicolin Chen 			cpu_dai->sample_bits = 0;
1187d3383420SNicolin Chen 		}
1188*19bdcc7aSShreyas NC 	}
1189d3383420SNicolin Chen 
11900b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1191d3383420SNicolin Chen 		if (codec_dai->active == 1) {
1192d3383420SNicolin Chen 			codec_dai->rate = 0;
1193d3383420SNicolin Chen 			codec_dai->channels = 0;
1194d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
1195d3383420SNicolin Chen 		}
11962e5894d7SBenoit Cousson 	}
1197d3383420SNicolin Chen 
1198ddee627cSLiam Girdwood 	/* apply codec digital mute */
11990b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
12000f6011fdSKuninori Morimoto 		int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK];
12010f6011fdSKuninori Morimoto 		int capture_active  = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE];
12020f6011fdSKuninori Morimoto 
12030f6011fdSKuninori Morimoto 		if ((playback && playback_active == 1) ||
12040f6011fdSKuninori Morimoto 		    (!playback && capture_active == 1))
12050b7990e3SKuninori Morimoto 			snd_soc_dai_digital_mute(codec_dai, 1,
12062e5894d7SBenoit Cousson 						 substream->stream);
12072e5894d7SBenoit Cousson 	}
1208ddee627cSLiam Girdwood 
1209ddee627cSLiam Girdwood 	/* free any machine hw params */
121049f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1211ddee627cSLiam Girdwood 
1212b8135864SKuninori Morimoto 	/* free any component resources */
1213244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
1214b8135864SKuninori Morimoto 
1215ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
12160b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1217f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1218f47b9ad9SJerome Brunet 			continue;
1219f47b9ad9SJerome Brunet 
1220846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
12212e5894d7SBenoit Cousson 	}
1222ddee627cSLiam Girdwood 
1223*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
1224846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(cpu_dai, substream);
1225ddee627cSLiam Girdwood 
122672b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1227ddee627cSLiam Girdwood 	return 0;
1228ddee627cSLiam Girdwood }
1229ddee627cSLiam Girdwood 
12304378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1231ddee627cSLiam Girdwood {
1232ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1233b8135864SKuninori Morimoto 	struct snd_soc_component *component;
1234*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12352e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
12362e5894d7SBenoit Cousson 	int i, ret;
1237ddee627cSLiam Girdwood 
1238ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
1239ddee627cSLiam Girdwood 	if (ret < 0)
1240ddee627cSLiam Girdwood 		return ret;
1241ddee627cSLiam Girdwood 
1242613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12435693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1244b8135864SKuninori Morimoto 		if (ret < 0)
1245b8135864SKuninori Morimoto 			return ret;
1246b8135864SKuninori Morimoto 	}
1247b8135864SKuninori Morimoto 
1248*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1249901e822bSDan Carpenter 		ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1250ddee627cSLiam Girdwood 		if (ret < 0)
1251ddee627cSLiam Girdwood 			return ret;
1252*19bdcc7aSShreyas NC 	}
12534792b0dbSJarkko Nikula 
12544378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
12554378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
12564378f1fbSPeter Ujfalusi 		if (ret < 0)
12574378f1fbSPeter Ujfalusi 			return ret;
12584378f1fbSPeter Ujfalusi 	}
12594378f1fbSPeter Ujfalusi 
12604378f1fbSPeter Ujfalusi 	return 0;
12614378f1fbSPeter Ujfalusi }
12624378f1fbSPeter Ujfalusi 
12634378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
12644378f1fbSPeter Ujfalusi {
12654378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
12664378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
1267*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12684378f1fbSPeter Ujfalusi 	struct snd_soc_dai *codec_dai;
12694378f1fbSPeter Ujfalusi 	int i, ret;
12704378f1fbSPeter Ujfalusi 
12714378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
12724378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
12734378f1fbSPeter Ujfalusi 		if (ret < 0)
12744378f1fbSPeter Ujfalusi 			return ret;
12754378f1fbSPeter Ujfalusi 	}
12764378f1fbSPeter Ujfalusi 
1277*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
12784378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
12794378f1fbSPeter Ujfalusi 		if (ret < 0)
12804378f1fbSPeter Ujfalusi 			return ret;
1281*19bdcc7aSShreyas NC 	}
12824378f1fbSPeter Ujfalusi 
1283613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12844378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
12854378f1fbSPeter Ujfalusi 		if (ret < 0)
12864378f1fbSPeter Ujfalusi 			return ret;
12874378f1fbSPeter Ujfalusi 	}
12884378f1fbSPeter Ujfalusi 
1289ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
12904792b0dbSJarkko Nikula 	if (ret < 0)
12914792b0dbSJarkko Nikula 		return ret;
12924792b0dbSJarkko Nikula 
1293ddee627cSLiam Girdwood 	return 0;
1294ddee627cSLiam Girdwood }
1295ddee627cSLiam Girdwood 
12964378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
12974378f1fbSPeter Ujfalusi {
12984378f1fbSPeter Ujfalusi 	int ret;
12994378f1fbSPeter Ujfalusi 
13004378f1fbSPeter Ujfalusi 	switch (cmd) {
13014378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
13024378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
13034378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
13044378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
13054378f1fbSPeter Ujfalusi 		break;
13064378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
13074378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
13084378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
13094378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
13104378f1fbSPeter Ujfalusi 		break;
13114378f1fbSPeter Ujfalusi 	default:
13124378f1fbSPeter Ujfalusi 		return -EINVAL;
13134378f1fbSPeter Ujfalusi 	}
13144378f1fbSPeter Ujfalusi 
13154378f1fbSPeter Ujfalusi 	return ret;
13164378f1fbSPeter Ujfalusi }
13174378f1fbSPeter Ujfalusi 
131845c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
131945c0a188SMark Brown 				   int cmd)
132007bf84aaSLiam Girdwood {
132107bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1322*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
13232e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
13242e5894d7SBenoit Cousson 	int i, ret;
132507bf84aaSLiam Girdwood 
13260b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
13275c0769afSKuninori Morimoto 		ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
132807bf84aaSLiam Girdwood 		if (ret < 0)
132907bf84aaSLiam Girdwood 			return ret;
133007bf84aaSLiam Girdwood 	}
133107bf84aaSLiam Girdwood 
1332*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1333901e822bSDan Carpenter 		ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
133407bf84aaSLiam Girdwood 		if (ret < 0)
133507bf84aaSLiam Girdwood 			return ret;
1336*19bdcc7aSShreyas NC 	}
13375c0769afSKuninori Morimoto 
133807bf84aaSLiam Girdwood 	return 0;
133907bf84aaSLiam Girdwood }
1340ddee627cSLiam Girdwood /*
1341ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1342ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1343ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1344ddee627cSLiam Girdwood  */
1345ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1346ddee627cSLiam Girdwood {
1347ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1348*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
13492e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1350ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1351ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1352ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
13532e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
1354*19bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
13552e5894d7SBenoit Cousson 	int i;
1356ddee627cSLiam Girdwood 
13579fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
13589fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
13599fb4c2bfSAkshu Agrawal 
13600035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1361b8135864SKuninori Morimoto 
13629fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
13639fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1364b8135864SKuninori Morimoto 
1365*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1366*19bdcc7aSShreyas NC 		cpu_delay = max(cpu_delay,
1367*19bdcc7aSShreyas NC 				snd_soc_dai_delay(cpu_dai, substream));
1368*19bdcc7aSShreyas NC 	}
1369*19bdcc7aSShreyas NC 	delay += cpu_delay;
1370ddee627cSLiam Girdwood 
13710b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
13722e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
13731dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
13742e5894d7SBenoit Cousson 	}
13752e5894d7SBenoit Cousson 	delay += codec_delay;
1376ddee627cSLiam Girdwood 
1377ddee627cSLiam Girdwood 	runtime->delay = delay;
1378ddee627cSLiam Girdwood 
1379ddee627cSLiam Girdwood 	return offset;
1380ddee627cSLiam Girdwood }
1381ddee627cSLiam Girdwood 
138201d7584cSLiam Girdwood /* connect a FE and BE */
138301d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
138401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
138501d7584cSLiam Girdwood {
138601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1387a9764869SKaiChieh Chuang 	unsigned long flags;
138801d7584cSLiam Girdwood 
138901d7584cSLiam Girdwood 	/* only add new dpcms */
13908d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
139101d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
139201d7584cSLiam Girdwood 			return 0;
139301d7584cSLiam Girdwood 	}
139401d7584cSLiam Girdwood 
139501d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
139601d7584cSLiam Girdwood 	if (!dpcm)
139701d7584cSLiam Girdwood 		return -ENOMEM;
139801d7584cSLiam Girdwood 
139901d7584cSLiam Girdwood 	dpcm->be = be;
140001d7584cSLiam Girdwood 	dpcm->fe = fe;
140101d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
140201d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1403a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
140401d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
140501d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1406a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
140701d7584cSLiam Girdwood 
140801d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
140901d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
141001d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
141101d7584cSLiam Girdwood 
1412154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1413154dae87SKuninori Morimoto 
141401d7584cSLiam Girdwood 	return 1;
141501d7584cSLiam Girdwood }
141601d7584cSLiam Girdwood 
141701d7584cSLiam Girdwood /* reparent a BE onto another FE */
141801d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
141901d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
142001d7584cSLiam Girdwood {
142101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
142201d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
142301d7584cSLiam Girdwood 
142401d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
142501d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
142601d7584cSLiam Girdwood 		return;
142701d7584cSLiam Girdwood 
142801d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
142901d7584cSLiam Girdwood 
1430d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
143101d7584cSLiam Girdwood 		if (dpcm->fe == fe)
143201d7584cSLiam Girdwood 			continue;
143301d7584cSLiam Girdwood 
143401d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
143501d7584cSLiam Girdwood 			stream ? "capture" : "playback",
143601d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
143701d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
143801d7584cSLiam Girdwood 
143901d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
144001d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
144101d7584cSLiam Girdwood 		break;
144201d7584cSLiam Girdwood 	}
144301d7584cSLiam Girdwood }
144401d7584cSLiam Girdwood 
144501d7584cSLiam Girdwood /* disconnect a BE and FE */
144623607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
144701d7584cSLiam Girdwood {
144801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1449a9764869SKaiChieh Chuang 	unsigned long flags;
145001d7584cSLiam Girdwood 
14518d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1452103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
145301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
145401d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
145501d7584cSLiam Girdwood 
145601d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
145701d7584cSLiam Girdwood 			continue;
145801d7584cSLiam Girdwood 
145901d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
146001d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
146101d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
146201d7584cSLiam Girdwood 
146301d7584cSLiam Girdwood 		/* BEs still alive need new FE */
146401d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
146501d7584cSLiam Girdwood 
1466154dae87SKuninori Morimoto 		dpcm_remove_debugfs_state(dpcm);
1467154dae87SKuninori Morimoto 
1468a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
146901d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
147001d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1471a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
147201d7584cSLiam Girdwood 		kfree(dpcm);
147301d7584cSLiam Girdwood 	}
147401d7584cSLiam Girdwood }
147501d7584cSLiam Girdwood 
147601d7584cSLiam Girdwood /* get BE for DAI widget and stream */
147701d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
147801d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
147901d7584cSLiam Girdwood {
148001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
148193597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
14827afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
14831a497983SMengdong Lin 	int i;
148401d7584cSLiam Girdwood 
14853c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
14863c146465SLiam Girdwood 
1487bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
148801d7584cSLiam Girdwood 
148935ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
149035ea0655SLiam Girdwood 			continue;
149135ea0655SLiam Girdwood 
1492*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
1493*19bdcc7aSShreyas NC 			w = snd_soc_dai_get_widget(dai, stream);
149493597faeSKuninori Morimoto 
14953c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
149693597faeSKuninori Morimoto 				w ? w->name : "(not set)");
14973c146465SLiam Girdwood 
149893597faeSKuninori Morimoto 			if (w == widget)
149901d7584cSLiam Girdwood 				return be;
1500*19bdcc7aSShreyas NC 		}
15012e5894d7SBenoit Cousson 
15027afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
15030c01f6caSKuninori Morimoto 			w = snd_soc_dai_get_widget(dai, stream);
150493597faeSKuninori Morimoto 
150593597faeSKuninori Morimoto 			if (w == widget)
15062e5894d7SBenoit Cousson 				return be;
15072e5894d7SBenoit Cousson 		}
150801d7584cSLiam Girdwood 	}
150901d7584cSLiam Girdwood 
15109d6ee365SJerome Brunet 	/* Widget provided is not a BE */
151101d7584cSLiam Girdwood 	return NULL;
151201d7584cSLiam Girdwood }
151301d7584cSLiam Girdwood 
151401d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
151501d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
151601d7584cSLiam Girdwood {
151709e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
151801d7584cSLiam Girdwood 	int i;
151901d7584cSLiam Girdwood 
152009e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
152109e88f8aSKuninori Morimoto 		if (widget == w)
152201d7584cSLiam Girdwood 			return 1;
152301d7584cSLiam Girdwood 
152401d7584cSLiam Girdwood 	return 0;
152501d7584cSLiam Girdwood }
152601d7584cSLiam Girdwood 
15275fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
15285fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
15295fdd022cSPiotr Stankiewicz {
15305fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
15315fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1532c2cd8216SKuninori Morimoto 	int stream;
15335fdd022cSPiotr Stankiewicz 
1534c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1535c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1536c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1537c2cd8216SKuninori Morimoto 	else
1538c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1539c2cd8216SKuninori Morimoto 
1540027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1541027a4838SKuninori Morimoto 	if (rtd)
15425fdd022cSPiotr Stankiewicz 		return true;
15435fdd022cSPiotr Stankiewicz 
15445fdd022cSPiotr Stankiewicz 	return false;
15455fdd022cSPiotr Stankiewicz }
15465fdd022cSPiotr Stankiewicz 
154723607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
15481ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
154901d7584cSLiam Girdwood {
155001d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
155101d7584cSLiam Girdwood 	int paths;
155201d7584cSLiam Girdwood 
155301d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
15546742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
15555fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
155601d7584cSLiam Girdwood 
1557103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
155801d7584cSLiam Girdwood 			stream ? "capture" : "playback");
155901d7584cSLiam Girdwood 
156001d7584cSLiam Girdwood 	return paths;
156101d7584cSLiam Girdwood }
156201d7584cSLiam Girdwood 
156352645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
156452645e33SKuninori Morimoto {
156552645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
156652645e33SKuninori Morimoto }
156752645e33SKuninori Morimoto 
156801d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
156901d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
157001d7584cSLiam Girdwood {
157101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
157201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
157301d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
15747afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
157501d7584cSLiam Girdwood 	int prune = 0;
1576bed646dcSKuninori Morimoto 	int do_prune;
157701d7584cSLiam Girdwood 
157801d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
15798d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
15802e5894d7SBenoit Cousson 		unsigned int i;
158101d7584cSLiam Girdwood 
158201d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
1583*19bdcc7aSShreyas NC 		do_prune = 1;
1584*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(dpcm->be, i, dai) {
1585*19bdcc7aSShreyas NC 			widget = snd_soc_dai_get_widget(dai, stream);
158601d7584cSLiam Girdwood 
1587*19bdcc7aSShreyas NC 			/*
1588*19bdcc7aSShreyas NC 			 * The BE is pruned only if none of the cpu_dai
1589*19bdcc7aSShreyas NC 			 * widgets are in the active list.
1590*19bdcc7aSShreyas NC 			 */
159101d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
1592*19bdcc7aSShreyas NC 				do_prune = 0;
1593*19bdcc7aSShreyas NC 		}
1594*19bdcc7aSShreyas NC 		if (!do_prune)
159501d7584cSLiam Girdwood 			continue;
159601d7584cSLiam Girdwood 
159701d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
1598bed646dcSKuninori Morimoto 		do_prune = 1;
15997afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(dpcm->be, i, dai) {
16000c01f6caSKuninori Morimoto 			widget = snd_soc_dai_get_widget(dai, stream);
160101d7584cSLiam Girdwood 
160201d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
160301d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
1604bed646dcSKuninori Morimoto 				do_prune = 0;
16052e5894d7SBenoit Cousson 		}
1606bed646dcSKuninori Morimoto 		if (!do_prune)
1607bed646dcSKuninori Morimoto 			continue;
160801d7584cSLiam Girdwood 
1609103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
161001d7584cSLiam Girdwood 			stream ? "capture" : "playback",
161101d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
161201d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
161301d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
161401d7584cSLiam Girdwood 		prune++;
161501d7584cSLiam Girdwood 	}
161601d7584cSLiam Girdwood 
1617103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
161801d7584cSLiam Girdwood 	return prune;
161901d7584cSLiam Girdwood }
162001d7584cSLiam Girdwood 
162101d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
162201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
162301d7584cSLiam Girdwood {
162401d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
162501d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
162601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
162709e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
162801d7584cSLiam Girdwood 	int i, new = 0, err;
162901d7584cSLiam Girdwood 
163001d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
163109e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
163201d7584cSLiam Girdwood 
163309e88f8aSKuninori Morimoto 		switch (widget->id) {
16344616274dSMark Brown 		case snd_soc_dapm_dai_in:
1635c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1636c5b8540dSKoro Chen 				continue;
1637c5b8540dSKoro Chen 			break;
16384616274dSMark Brown 		case snd_soc_dapm_dai_out:
1639c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1640c5b8540dSKoro Chen 				continue;
16414616274dSMark Brown 			break;
16424616274dSMark Brown 		default:
164301d7584cSLiam Girdwood 			continue;
16444616274dSMark Brown 		}
164501d7584cSLiam Girdwood 
164601d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
164709e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
164801d7584cSLiam Girdwood 		if (!be) {
1649103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
165009e88f8aSKuninori Morimoto 					widget->name);
165101d7584cSLiam Girdwood 			continue;
165201d7584cSLiam Girdwood 		}
165301d7584cSLiam Girdwood 
165401d7584cSLiam Girdwood 		/* make sure BE is a real BE */
165501d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
165601d7584cSLiam Girdwood 			continue;
165701d7584cSLiam Girdwood 
165801d7584cSLiam Girdwood 		/* don't connect if FE is not running */
165923607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
166001d7584cSLiam Girdwood 			continue;
166101d7584cSLiam Girdwood 
166201d7584cSLiam Girdwood 		/* newly connected FE and BE */
166301d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
166401d7584cSLiam Girdwood 		if (err < 0) {
1665103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
166609e88f8aSKuninori Morimoto 				widget->name);
166701d7584cSLiam Girdwood 			break;
166801d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
166901d7584cSLiam Girdwood 			continue;
167001d7584cSLiam Girdwood 
167101d7584cSLiam Girdwood 		/* new */
167201d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
167301d7584cSLiam Girdwood 		new++;
167401d7584cSLiam Girdwood 	}
167501d7584cSLiam Girdwood 
1676103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
167701d7584cSLiam Girdwood 	return new;
167801d7584cSLiam Girdwood }
167901d7584cSLiam Girdwood 
168001d7584cSLiam Girdwood /*
168101d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
168201d7584cSLiam Girdwood  * FE substream.
168301d7584cSLiam Girdwood  */
168423607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
168501d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
168601d7584cSLiam Girdwood {
168701d7584cSLiam Girdwood 	if (new)
168801d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
168901d7584cSLiam Girdwood 	else
169001d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
169101d7584cSLiam Girdwood }
169201d7584cSLiam Girdwood 
169323607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
169401d7584cSLiam Girdwood {
169501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1696a9764869SKaiChieh Chuang 	unsigned long flags;
169701d7584cSLiam Girdwood 
1698a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
16998d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
170001d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
170101d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1702a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
170301d7584cSLiam Girdwood }
170401d7584cSLiam Girdwood 
170501d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
170601d7584cSLiam Girdwood 	int stream)
170701d7584cSLiam Girdwood {
170801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
170901d7584cSLiam Girdwood 
171001d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
17118d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
171201d7584cSLiam Girdwood 
171301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
171401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
171501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
171601d7584cSLiam Girdwood 
171701d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1718103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
171901d7584cSLiam Girdwood 				stream ? "capture" : "playback",
172001d7584cSLiam Girdwood 				be->dpcm[stream].state);
172101d7584cSLiam Girdwood 
172201d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
172301d7584cSLiam Girdwood 			continue;
172401d7584cSLiam Girdwood 
172501d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
172601d7584cSLiam Girdwood 			continue;
172701d7584cSLiam Girdwood 
172801d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
172901d7584cSLiam Girdwood 		be_substream->runtime = NULL;
173001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
173101d7584cSLiam Girdwood 	}
173201d7584cSLiam Girdwood }
173301d7584cSLiam Girdwood 
173423607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
173501d7584cSLiam Girdwood {
173601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
173701d7584cSLiam Girdwood 	int err, count = 0;
173801d7584cSLiam Girdwood 
173901d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
17408d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
174101d7584cSLiam Girdwood 
174201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
174301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
174401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
174501d7584cSLiam Girdwood 
17462062b4c5SRussell King - ARM Linux 		if (!be_substream) {
17472062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
17482062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
17492062b4c5SRussell King - ARM Linux 			continue;
17502062b4c5SRussell King - ARM Linux 		}
17512062b4c5SRussell King - ARM Linux 
175201d7584cSLiam Girdwood 		/* is this op for this BE ? */
175301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
175401d7584cSLiam Girdwood 			continue;
175501d7584cSLiam Girdwood 
175601d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
175701d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1758103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
175901d7584cSLiam Girdwood 				stream ? "capture" : "playback",
176001d7584cSLiam Girdwood 				be->dpcm[stream].state);
176101d7584cSLiam Girdwood 
176201d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
176301d7584cSLiam Girdwood 			continue;
176401d7584cSLiam Girdwood 
176501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
176601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
176701d7584cSLiam Girdwood 			continue;
176801d7584cSLiam Girdwood 
17692062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
17702062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
177101d7584cSLiam Girdwood 
177201d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
177301d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
177401d7584cSLiam Girdwood 		if (err < 0) {
1775103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
177601d7584cSLiam Girdwood 			be->dpcm[stream].users--;
177701d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1778103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
177901d7584cSLiam Girdwood 					stream ? "capture" : "playback",
178001d7584cSLiam Girdwood 					be->dpcm[stream].state);
178101d7584cSLiam Girdwood 
178201d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
178301d7584cSLiam Girdwood 			goto unwind;
178401d7584cSLiam Girdwood 		}
178501d7584cSLiam Girdwood 
178601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
178701d7584cSLiam Girdwood 		count++;
178801d7584cSLiam Girdwood 	}
178901d7584cSLiam Girdwood 
179001d7584cSLiam Girdwood 	return count;
179101d7584cSLiam Girdwood 
179201d7584cSLiam Girdwood unwind:
179301d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
17948d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
179501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
179601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
179701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
179801d7584cSLiam Girdwood 
179901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
180001d7584cSLiam Girdwood 			continue;
180101d7584cSLiam Girdwood 
180201d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1803103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
180401d7584cSLiam Girdwood 				stream ? "capture" : "playback",
180501d7584cSLiam Girdwood 				be->dpcm[stream].state);
180601d7584cSLiam Girdwood 
180701d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
180801d7584cSLiam Girdwood 			continue;
180901d7584cSLiam Girdwood 
181001d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
181101d7584cSLiam Girdwood 			continue;
181201d7584cSLiam Girdwood 
181301d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
181401d7584cSLiam Girdwood 		be_substream->runtime = NULL;
181501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
181601d7584cSLiam Girdwood 	}
181701d7584cSLiam Girdwood 
181801d7584cSLiam Girdwood 	return err;
181901d7584cSLiam Girdwood }
182001d7584cSLiam Girdwood 
182108ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1822435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
182308ae9b45SLars-Peter Clausen {
182408ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1825e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
182608ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
182708ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1828002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1829435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1830002220a9SLars-Peter Clausen 	else
1831435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
183208ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
183308ae9b45SLars-Peter Clausen }
183408ae9b45SLars-Peter Clausen 
1835435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1836435ffb76SJerome Brunet 				      u64 *formats)
1837b073ed4eSKuninori Morimoto {
1838b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1839b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
18407afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1841b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1842b073ed4eSKuninori Morimoto 
1843b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1844435ffb76SJerome Brunet 		return;
1845b073ed4eSKuninori Morimoto 
1846b073ed4eSKuninori Morimoto 	/*
1847b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1848b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1849b073ed4eSKuninori Morimoto 	 */
1850b073ed4eSKuninori Morimoto 
18518d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1852b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1853b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1854b073ed4eSKuninori Morimoto 		int i;
1855b073ed4eSKuninori Morimoto 
18567afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
18574febced1SJerome Brunet 			/*
18584febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
18594febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
18604febced1SJerome Brunet 			 */
18617afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
18624febced1SJerome Brunet 				continue;
18634febced1SJerome Brunet 
1864acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1865b073ed4eSKuninori Morimoto 
1866435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1867435ffb76SJerome Brunet 		}
1868b073ed4eSKuninori Morimoto 	}
1869b073ed4eSKuninori Morimoto }
1870b073ed4eSKuninori Morimoto 
1871435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1872f4c277b8SJiada Wang 				    unsigned int *channels_min,
1873f4c277b8SJiada Wang 				    unsigned int *channels_max)
1874f4c277b8SJiada Wang {
1875f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1876f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1877f4c277b8SJiada Wang 	int stream = substream->stream;
1878f4c277b8SJiada Wang 
1879f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1880f4c277b8SJiada Wang 		return;
1881f4c277b8SJiada Wang 
1882f4c277b8SJiada Wang 	/*
1883f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1884f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1885f4c277b8SJiada Wang 	 */
1886f4c277b8SJiada Wang 
18878d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1888f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
1889f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
18904f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
1891*19bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
1892*19bdcc7aSShreyas NC 		int i;
1893f4c277b8SJiada Wang 
1894*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
1895*19bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
18964f2bd18bSJerome Brunet 
1897*19bdcc7aSShreyas NC 			*channels_min = max(*channels_min,
1898*19bdcc7aSShreyas NC 					    cpu_stream->channels_min);
1899*19bdcc7aSShreyas NC 			*channels_max = min(*channels_max,
1900*19bdcc7aSShreyas NC 					    cpu_stream->channels_max);
1901*19bdcc7aSShreyas NC 		}
19024f2bd18bSJerome Brunet 
19034f2bd18bSJerome Brunet 		/*
19044f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
19054f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
19064f2bd18bSJerome Brunet 		 */
19074f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
1908acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream);
1909f4c277b8SJiada Wang 
1910f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1911f4c277b8SJiada Wang 					    codec_stream->channels_min);
1912f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1913f4c277b8SJiada Wang 					    codec_stream->channels_max);
1914f4c277b8SJiada Wang 		}
1915f4c277b8SJiada Wang 	}
1916f4c277b8SJiada Wang }
1917f4c277b8SJiada Wang 
1918baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1919baacd8d1SJerome Brunet 				    unsigned int *rates,
1920baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1921baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1922baacd8d1SJerome Brunet {
1923baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1924baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1925baacd8d1SJerome Brunet 	int stream = substream->stream;
1926baacd8d1SJerome Brunet 
1927baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1928baacd8d1SJerome Brunet 		return;
1929baacd8d1SJerome Brunet 
1930baacd8d1SJerome Brunet 	/*
1931baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1932baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1933baacd8d1SJerome Brunet 	 */
1934baacd8d1SJerome Brunet 
19358d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1936baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1937baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *codec_stream;
1938baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
19397afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1940baacd8d1SJerome Brunet 		int i;
1941baacd8d1SJerome Brunet 
1942*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
1943*19bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1944baacd8d1SJerome Brunet 
1945baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, cpu_stream->rate_min);
1946*19bdcc7aSShreyas NC 			*rate_max = min_not_zero(*rate_max,
1947*19bdcc7aSShreyas NC 						 cpu_stream->rate_max);
1948*19bdcc7aSShreyas NC 			*rates = snd_pcm_rate_mask_intersect(*rates,
1949*19bdcc7aSShreyas NC 						cpu_stream->rates);
1950*19bdcc7aSShreyas NC 		}
1951baacd8d1SJerome Brunet 
19527afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
1953baacd8d1SJerome Brunet 			/*
1954baacd8d1SJerome Brunet 			 * Skip CODECs which don't support the current stream
1955baacd8d1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
1956baacd8d1SJerome Brunet 			 */
19577afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
1958baacd8d1SJerome Brunet 				continue;
1959baacd8d1SJerome Brunet 
1960acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1961baacd8d1SJerome Brunet 
1962baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, codec_stream->rate_min);
1963baacd8d1SJerome Brunet 			*rate_max = min_not_zero(*rate_max,
1964baacd8d1SJerome Brunet 						 codec_stream->rate_max);
1965baacd8d1SJerome Brunet 			*rates = snd_pcm_rate_mask_intersect(*rates,
1966baacd8d1SJerome Brunet 						codec_stream->rates);
1967baacd8d1SJerome Brunet 		}
1968baacd8d1SJerome Brunet 	}
1969baacd8d1SJerome Brunet }
1970baacd8d1SJerome Brunet 
197145c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
197201d7584cSLiam Girdwood {
197301d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
197401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1975*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
1976*19bdcc7aSShreyas NC 	struct snd_soc_dai_driver *cpu_dai_drv;
1977*19bdcc7aSShreyas NC 	int i;
197801d7584cSLiam Girdwood 
1979*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1980*19bdcc7aSShreyas NC 		cpu_dai_drv = cpu_dai->driver;
198108ae9b45SLars-Peter Clausen 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1982435ffb76SJerome Brunet 			dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
198308ae9b45SLars-Peter Clausen 		else
1984435ffb76SJerome Brunet 			dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1985*19bdcc7aSShreyas NC 	}
1986f4c277b8SJiada Wang 
1987435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1988435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1989435ffb76SJerome Brunet 				&runtime->hw.channels_max);
1990baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1991baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
199201d7584cSLiam Girdwood }
199301d7584cSLiam Girdwood 
1994ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1995ea9d0d77STakashi Iwai 
1996ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1997ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1998ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1999ea9d0d77STakashi Iwai  * process the pending trigger action here.
2000ea9d0d77STakashi Iwai  */
2001ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2002ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
2003ea9d0d77STakashi Iwai {
2004ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
2005ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
2006ea9d0d77STakashi Iwai 
2007ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
2008ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2009ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
2010ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
2011ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
2012ea9d0d77STakashi Iwai 	}
2013ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
2014ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
2015ea9d0d77STakashi Iwai }
2016ea9d0d77STakashi Iwai 
2017906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
2018906c7d69SPC Liao 			       int stream)
2019906c7d69SPC Liao {
2020906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
2021906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2022*19bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
2023906c7d69SPC Liao 	int err;
2024*19bdcc7aSShreyas NC 	int i;
2025906c7d69SPC Liao 
2026906c7d69SPC Liao 	/* apply symmetry for FE */
2027906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
2028906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2029906c7d69SPC Liao 
2030*19bdcc7aSShreyas NC 	for_each_rtd_cpu_dai (fe, i, fe_cpu_dai) {
2031906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
2032906c7d69SPC Liao 		if (fe_cpu_dai->active) {
2033906c7d69SPC Liao 			err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
2034906c7d69SPC Liao 			if (err < 0)
2035906c7d69SPC Liao 				return err;
2036906c7d69SPC Liao 		}
2037*19bdcc7aSShreyas NC 	}
2038906c7d69SPC Liao 
2039906c7d69SPC Liao 	/* apply symmetry for BE */
20408d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2041906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
2042906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
2043906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
20446246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
20450b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
2046*19bdcc7aSShreyas NC 		struct snd_soc_dai *cpu_dai;
2047906c7d69SPC Liao 		int i;
2048906c7d69SPC Liao 
20496246f283SJerome Brunet 		/* A backend may not have the requested substream */
20506246f283SJerome Brunet 		if (!be_substream)
20516246f283SJerome Brunet 			continue;
20526246f283SJerome Brunet 
20536246f283SJerome Brunet 		rtd = be_substream->private_data;
2054f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
2055f1176614SJeeja KP 			continue;
2056f1176614SJeeja KP 
2057906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
2058906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2059906c7d69SPC Liao 
2060906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
2061*19bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
2062*19bdcc7aSShreyas NC 			if (cpu_dai->active) {
206399bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
2064*19bdcc7aSShreyas NC 							     cpu_dai);
2065906c7d69SPC Liao 				if (err < 0)
2066906c7d69SPC Liao 					return err;
2067906c7d69SPC Liao 			}
2068*19bdcc7aSShreyas NC 		}
2069906c7d69SPC Liao 
20700b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
20710b7990e3SKuninori Morimoto 			if (codec_dai->active) {
207299bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
20730b7990e3SKuninori Morimoto 							     codec_dai);
2074906c7d69SPC Liao 				if (err < 0)
2075906c7d69SPC Liao 					return err;
2076906c7d69SPC Liao 			}
2077906c7d69SPC Liao 		}
2078906c7d69SPC Liao 	}
2079906c7d69SPC Liao 
2080906c7d69SPC Liao 	return 0;
2081906c7d69SPC Liao }
2082906c7d69SPC Liao 
208301d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
208401d7584cSLiam Girdwood {
208501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
208601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
208701d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
208801d7584cSLiam Girdwood 
2089ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
209001d7584cSLiam Girdwood 
209101d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
209201d7584cSLiam Girdwood 	if (ret < 0) {
2093103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
209401d7584cSLiam Girdwood 		goto be_err;
209501d7584cSLiam Girdwood 	}
209601d7584cSLiam Girdwood 
2097103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
209801d7584cSLiam Girdwood 
209901d7584cSLiam Girdwood 	/* start the DAI frontend */
210001d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
210101d7584cSLiam Girdwood 	if (ret < 0) {
2102103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
210301d7584cSLiam Girdwood 		goto unwind;
210401d7584cSLiam Girdwood 	}
210501d7584cSLiam Girdwood 
210601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
210701d7584cSLiam Girdwood 
210801d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
210901d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
211001d7584cSLiam Girdwood 
2111906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
2112906c7d69SPC Liao 	if (ret < 0) {
2113906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2114906c7d69SPC Liao 			ret);
2115906c7d69SPC Liao 		goto unwind;
2116906c7d69SPC Liao 	}
2117906c7d69SPC Liao 
2118ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
211901d7584cSLiam Girdwood 	return 0;
212001d7584cSLiam Girdwood 
212101d7584cSLiam Girdwood unwind:
212201d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
212301d7584cSLiam Girdwood be_err:
2124ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
212501d7584cSLiam Girdwood 	return ret;
212601d7584cSLiam Girdwood }
212701d7584cSLiam Girdwood 
212823607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
212901d7584cSLiam Girdwood {
213001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
213101d7584cSLiam Girdwood 
213201d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
21338d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
213401d7584cSLiam Girdwood 
213501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
213601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
213701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
213801d7584cSLiam Girdwood 
213901d7584cSLiam Girdwood 		/* is this op for this BE ? */
214001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
214101d7584cSLiam Girdwood 			continue;
214201d7584cSLiam Girdwood 
214301d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
2144103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
214501d7584cSLiam Girdwood 				stream ? "capture" : "playback",
214601d7584cSLiam Girdwood 				be->dpcm[stream].state);
214701d7584cSLiam Girdwood 
214801d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
214901d7584cSLiam Girdwood 			continue;
215001d7584cSLiam Girdwood 
215101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
21529c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
21539c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
21549c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
21559c0ac70aSKai Chieh Chuang 		}
215601d7584cSLiam Girdwood 
2157103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
215894d215ccS彭东林 			be->dai_link->name);
215901d7584cSLiam Girdwood 
216001d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
216101d7584cSLiam Girdwood 		be_substream->runtime = NULL;
216201d7584cSLiam Girdwood 
216301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
216401d7584cSLiam Girdwood 	}
216501d7584cSLiam Girdwood 	return 0;
216601d7584cSLiam Girdwood }
216701d7584cSLiam Girdwood 
216801d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
216901d7584cSLiam Girdwood {
217001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
217101d7584cSLiam Girdwood 	int stream = substream->stream;
217201d7584cSLiam Girdwood 
2173ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
217401d7584cSLiam Girdwood 
217501d7584cSLiam Girdwood 	/* shutdown the BEs */
217601d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
217701d7584cSLiam Girdwood 
2178103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
217901d7584cSLiam Girdwood 
218001d7584cSLiam Girdwood 	/* now shutdown the frontend */
218101d7584cSLiam Girdwood 	soc_pcm_close(substream);
218201d7584cSLiam Girdwood 
218301d7584cSLiam Girdwood 	/* run the stream event for each BE */
21841c531230SKuninori Morimoto 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
218501d7584cSLiam Girdwood 
218601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2187ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
218801d7584cSLiam Girdwood 	return 0;
218901d7584cSLiam Girdwood }
219001d7584cSLiam Girdwood 
219123607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
219201d7584cSLiam Girdwood {
219301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
219401d7584cSLiam Girdwood 
219501d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
219601d7584cSLiam Girdwood 	 * to this frontend DAI */
21978d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
219801d7584cSLiam Girdwood 
219901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
220001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
220101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
220201d7584cSLiam Girdwood 
220301d7584cSLiam Girdwood 		/* is this op for this BE ? */
220401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
220501d7584cSLiam Girdwood 			continue;
220601d7584cSLiam Girdwood 
220701d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
220801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
220901d7584cSLiam Girdwood 				continue;
221001d7584cSLiam Girdwood 
221136fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
221236fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
221336fba62cSQiao Zhou 			continue;
221436fba62cSQiao Zhou 
221501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
221601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
221701d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
221808b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
22195e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
22205e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
222101d7584cSLiam Girdwood 			continue;
222201d7584cSLiam Girdwood 
2223103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
222494d215ccS彭东林 			be->dai_link->name);
222501d7584cSLiam Girdwood 
222601d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
222701d7584cSLiam Girdwood 
222801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
222901d7584cSLiam Girdwood 	}
223001d7584cSLiam Girdwood 
223101d7584cSLiam Girdwood 	return 0;
223201d7584cSLiam Girdwood }
223301d7584cSLiam Girdwood 
223445c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
223501d7584cSLiam Girdwood {
223601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
223701d7584cSLiam Girdwood 	int err, stream = substream->stream;
223801d7584cSLiam Girdwood 
223901d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2240ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
224101d7584cSLiam Girdwood 
2242103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
224301d7584cSLiam Girdwood 
224401d7584cSLiam Girdwood 	/* call hw_free on the frontend */
224501d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
224601d7584cSLiam Girdwood 	if (err < 0)
2247103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
224801d7584cSLiam Girdwood 			fe->dai_link->name);
224901d7584cSLiam Girdwood 
225001d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
225101d7584cSLiam Girdwood 	 * to this frontend DAI */
225201d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
225301d7584cSLiam Girdwood 
225401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2255ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
225601d7584cSLiam Girdwood 
225701d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
225801d7584cSLiam Girdwood 	return 0;
225901d7584cSLiam Girdwood }
226001d7584cSLiam Girdwood 
226123607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
226201d7584cSLiam Girdwood {
226301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
226401d7584cSLiam Girdwood 	int ret;
226501d7584cSLiam Girdwood 
22668d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
226701d7584cSLiam Girdwood 
226801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
226901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
227001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
227101d7584cSLiam Girdwood 
227201d7584cSLiam Girdwood 		/* is this op for this BE ? */
227301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
227401d7584cSLiam Girdwood 			continue;
227501d7584cSLiam Girdwood 
227601d7584cSLiam Girdwood 		/* copy params for each dpcm */
227701d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
227801d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
227901d7584cSLiam Girdwood 
228001d7584cSLiam Girdwood 		/* perform any hw_params fixups */
228101d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
228201d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
228301d7584cSLiam Girdwood 					&dpcm->hw_params);
228401d7584cSLiam Girdwood 			if (ret < 0) {
228501d7584cSLiam Girdwood 				dev_err(be->dev,
2286103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
228701d7584cSLiam Girdwood 					ret);
228801d7584cSLiam Girdwood 				goto unwind;
228901d7584cSLiam Girdwood 			}
229001d7584cSLiam Girdwood 		}
229101d7584cSLiam Girdwood 
2292ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2293ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2294ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2295ae061d2aSLibin Yang 
2296b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2297b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2298b0639bd2SKuninori Morimoto 			continue;
2299b0639bd2SKuninori Morimoto 
2300b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2301b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2302b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2303b0639bd2SKuninori Morimoto 			continue;
2304b0639bd2SKuninori Morimoto 
2305b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
230694d215ccS彭东林 			be->dai_link->name);
2307b0639bd2SKuninori Morimoto 
230801d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
230901d7584cSLiam Girdwood 		if (ret < 0) {
231001d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2311103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
231201d7584cSLiam Girdwood 			goto unwind;
231301d7584cSLiam Girdwood 		}
231401d7584cSLiam Girdwood 
231501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
231601d7584cSLiam Girdwood 	}
231701d7584cSLiam Girdwood 	return 0;
231801d7584cSLiam Girdwood 
231901d7584cSLiam Girdwood unwind:
232001d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
23218d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
232201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
232301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
232401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
232501d7584cSLiam Girdwood 
232601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
232701d7584cSLiam Girdwood 			continue;
232801d7584cSLiam Girdwood 
232901d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
233001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
233101d7584cSLiam Girdwood 			continue;
233201d7584cSLiam Girdwood 
233301d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
233401d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
233501d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
233601d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
233701d7584cSLiam Girdwood 			continue;
233801d7584cSLiam Girdwood 
233901d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
234001d7584cSLiam Girdwood 	}
234101d7584cSLiam Girdwood 
234201d7584cSLiam Girdwood 	return ret;
234301d7584cSLiam Girdwood }
234401d7584cSLiam Girdwood 
234545c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
234601d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
234701d7584cSLiam Girdwood {
234801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
234901d7584cSLiam Girdwood 	int ret, stream = substream->stream;
235001d7584cSLiam Girdwood 
235101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2352ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
235301d7584cSLiam Girdwood 
235401d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
235501d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
235601d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
235701d7584cSLiam Girdwood 	if (ret < 0) {
2358103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
235901d7584cSLiam Girdwood 		goto out;
236001d7584cSLiam Girdwood 	}
236101d7584cSLiam Girdwood 
2362103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
236301d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
236401d7584cSLiam Girdwood 			params_channels(params), params_format(params));
236501d7584cSLiam Girdwood 
236601d7584cSLiam Girdwood 	/* call hw_params on the frontend */
236701d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
236801d7584cSLiam Girdwood 	if (ret < 0) {
2369103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
237001d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
237101d7584cSLiam Girdwood 	 } else
237201d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
237301d7584cSLiam Girdwood 
237401d7584cSLiam Girdwood out:
2375ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
237601d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
237701d7584cSLiam Girdwood 	return ret;
237801d7584cSLiam Girdwood }
237901d7584cSLiam Girdwood 
238001d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
238101d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
238201d7584cSLiam Girdwood {
238301d7584cSLiam Girdwood 	int ret;
238401d7584cSLiam Girdwood 
2385103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
238694d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
238701d7584cSLiam Girdwood 
238801d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
238901d7584cSLiam Girdwood 	if (ret < 0)
2390103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
239101d7584cSLiam Girdwood 
239201d7584cSLiam Girdwood 	return ret;
239301d7584cSLiam Girdwood }
239401d7584cSLiam Girdwood 
239523607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
239645c0a188SMark Brown 			       int cmd)
239701d7584cSLiam Girdwood {
239801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
239901d7584cSLiam Girdwood 	int ret = 0;
240001d7584cSLiam Girdwood 
24018d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
240201d7584cSLiam Girdwood 
240301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
240401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
240501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
240601d7584cSLiam Girdwood 
240701d7584cSLiam Girdwood 		/* is this op for this BE ? */
240801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
240901d7584cSLiam Girdwood 			continue;
241001d7584cSLiam Girdwood 
241101d7584cSLiam Girdwood 		switch (cmd) {
241201d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
241301d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
241401d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
241501d7584cSLiam Girdwood 				continue;
241601d7584cSLiam Girdwood 
241701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
241801d7584cSLiam Girdwood 			if (ret)
241901d7584cSLiam Girdwood 				return ret;
242001d7584cSLiam Girdwood 
242101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
242201d7584cSLiam Girdwood 			break;
242301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
242401d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
242501d7584cSLiam Girdwood 				continue;
242601d7584cSLiam Girdwood 
242701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
242801d7584cSLiam Girdwood 			if (ret)
242901d7584cSLiam Girdwood 				return ret;
243001d7584cSLiam Girdwood 
243101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
243201d7584cSLiam Girdwood 			break;
243301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
243401d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
243501d7584cSLiam Girdwood 				continue;
243601d7584cSLiam Girdwood 
243701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
243801d7584cSLiam Girdwood 			if (ret)
243901d7584cSLiam Girdwood 				return ret;
244001d7584cSLiam Girdwood 
244101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
244201d7584cSLiam Girdwood 			break;
244301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
244401d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
244501d7584cSLiam Girdwood 				continue;
244601d7584cSLiam Girdwood 
244701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
244801d7584cSLiam Girdwood 				continue;
244901d7584cSLiam Girdwood 
245001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
245101d7584cSLiam Girdwood 			if (ret)
245201d7584cSLiam Girdwood 				return ret;
245301d7584cSLiam Girdwood 
245401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
245501d7584cSLiam Girdwood 			break;
245601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2457868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
245801d7584cSLiam Girdwood 				continue;
245901d7584cSLiam Girdwood 
246001d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
246101d7584cSLiam Girdwood 				continue;
246201d7584cSLiam Girdwood 
246301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
246401d7584cSLiam Girdwood 			if (ret)
246501d7584cSLiam Girdwood 				return ret;
246601d7584cSLiam Girdwood 
246701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
246801d7584cSLiam Girdwood 			break;
246901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
247001d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
247101d7584cSLiam Girdwood 				continue;
247201d7584cSLiam Girdwood 
247301d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
247401d7584cSLiam Girdwood 				continue;
247501d7584cSLiam Girdwood 
247601d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
247701d7584cSLiam Girdwood 			if (ret)
247801d7584cSLiam Girdwood 				return ret;
247901d7584cSLiam Girdwood 
248001d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
248101d7584cSLiam Girdwood 			break;
248201d7584cSLiam Girdwood 		}
248301d7584cSLiam Girdwood 	}
248401d7584cSLiam Girdwood 
248501d7584cSLiam Girdwood 	return ret;
248601d7584cSLiam Girdwood }
248701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
248801d7584cSLiam Girdwood 
2489acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2490acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2491acbf2774SRanjani Sridharan {
2492acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2493acbf2774SRanjani Sridharan 	int ret;
2494acbf2774SRanjani Sridharan 
2495acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2496acbf2774SRanjani Sridharan 	if (fe_first) {
2497acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2498acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2499acbf2774SRanjani Sridharan 
2500acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2501acbf2774SRanjani Sridharan 		if (ret < 0)
2502acbf2774SRanjani Sridharan 			return ret;
2503acbf2774SRanjani Sridharan 
2504acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2505acbf2774SRanjani Sridharan 		return ret;
2506acbf2774SRanjani Sridharan 	}
2507acbf2774SRanjani Sridharan 
2508acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2509acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2510acbf2774SRanjani Sridharan 	if (ret < 0)
2511acbf2774SRanjani Sridharan 		return ret;
2512acbf2774SRanjani Sridharan 
2513acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2514acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2515acbf2774SRanjani Sridharan 
2516acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2517acbf2774SRanjani Sridharan 
2518acbf2774SRanjani Sridharan 	return ret;
2519acbf2774SRanjani Sridharan }
2520acbf2774SRanjani Sridharan 
2521ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
252201d7584cSLiam Girdwood {
252301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2524acbf2774SRanjani Sridharan 	int stream = substream->stream;
2525acbf2774SRanjani Sridharan 	int ret = 0;
252601d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
252701d7584cSLiam Girdwood 
252801d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
252901d7584cSLiam Girdwood 
253001d7584cSLiam Girdwood 	switch (trigger) {
253101d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2532acbf2774SRanjani Sridharan 		switch (cmd) {
2533acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2534acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2535acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2536acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2537acbf2774SRanjani Sridharan 			break;
2538acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2539acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2540acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2541acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2542acbf2774SRanjani Sridharan 			break;
2543acbf2774SRanjani Sridharan 		default:
2544acbf2774SRanjani Sridharan 			ret = -EINVAL;
2545acbf2774SRanjani Sridharan 			break;
254601d7584cSLiam Girdwood 		}
254701d7584cSLiam Girdwood 		break;
254801d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2549acbf2774SRanjani Sridharan 		switch (cmd) {
2550acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2551acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2552acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2553acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2554acbf2774SRanjani Sridharan 			break;
2555acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2556acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2557acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2558acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2559acbf2774SRanjani Sridharan 			break;
2560acbf2774SRanjani Sridharan 		default:
2561acbf2774SRanjani Sridharan 			ret = -EINVAL;
2562acbf2774SRanjani Sridharan 			break;
256301d7584cSLiam Girdwood 		}
256401d7584cSLiam Girdwood 		break;
256507bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
256607bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
256707bf84aaSLiam Girdwood 
2568103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
256907bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
257007bf84aaSLiam Girdwood 
257107bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
257207bf84aaSLiam Girdwood 		break;
257301d7584cSLiam Girdwood 	default:
2574103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
257501d7584cSLiam Girdwood 				fe->dai_link->name);
257601d7584cSLiam Girdwood 		ret = -EINVAL;
257701d7584cSLiam Girdwood 		goto out;
257801d7584cSLiam Girdwood 	}
257901d7584cSLiam Girdwood 
2580acbf2774SRanjani Sridharan 	if (ret < 0) {
2581acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2582acbf2774SRanjani Sridharan 			cmd, ret);
2583acbf2774SRanjani Sridharan 		goto out;
2584acbf2774SRanjani Sridharan 	}
2585acbf2774SRanjani Sridharan 
258601d7584cSLiam Girdwood 	switch (cmd) {
258701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
258801d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
258901d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
259001d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
259101d7584cSLiam Girdwood 		break;
259201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
259301d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
259401d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
259501d7584cSLiam Girdwood 		break;
25969f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
25979f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
25989f169b9fSPatrick Lai 		break;
259901d7584cSLiam Girdwood 	}
260001d7584cSLiam Girdwood 
260101d7584cSLiam Girdwood out:
260201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
260301d7584cSLiam Girdwood 	return ret;
260401d7584cSLiam Girdwood }
260501d7584cSLiam Girdwood 
2606ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2607ea9d0d77STakashi Iwai {
2608ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2609ea9d0d77STakashi Iwai 	int stream = substream->stream;
2610ea9d0d77STakashi Iwai 
2611ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2612ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2613ea9d0d77STakashi Iwai 	 */
2614ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2615ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2616ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2617ea9d0d77STakashi Iwai 	}
2618ea9d0d77STakashi Iwai 
2619ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2620ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2621ea9d0d77STakashi Iwai }
2622ea9d0d77STakashi Iwai 
262323607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
262401d7584cSLiam Girdwood {
262501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
262601d7584cSLiam Girdwood 	int ret = 0;
262701d7584cSLiam Girdwood 
26288d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
262901d7584cSLiam Girdwood 
263001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
263101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
263201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
263301d7584cSLiam Girdwood 
263401d7584cSLiam Girdwood 		/* is this op for this BE ? */
263501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
263601d7584cSLiam Girdwood 			continue;
263701d7584cSLiam Girdwood 
263801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
263995f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
26405087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
26415087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
264201d7584cSLiam Girdwood 			continue;
264301d7584cSLiam Girdwood 
2644103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
264594d215ccS彭东林 			be->dai_link->name);
264601d7584cSLiam Girdwood 
264701d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
264801d7584cSLiam Girdwood 		if (ret < 0) {
2649103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
265001d7584cSLiam Girdwood 				ret);
265101d7584cSLiam Girdwood 			break;
265201d7584cSLiam Girdwood 		}
265301d7584cSLiam Girdwood 
265401d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
265501d7584cSLiam Girdwood 	}
265601d7584cSLiam Girdwood 	return ret;
265701d7584cSLiam Girdwood }
265801d7584cSLiam Girdwood 
265945c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
266001d7584cSLiam Girdwood {
266101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
266201d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
266301d7584cSLiam Girdwood 
266401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
266501d7584cSLiam Girdwood 
2666103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
266701d7584cSLiam Girdwood 
2668ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
266901d7584cSLiam Girdwood 
267001d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
267101d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2672103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
267301d7584cSLiam Girdwood 				fe->dai_link->name);
267401d7584cSLiam Girdwood 		ret = -EINVAL;
267501d7584cSLiam Girdwood 		goto out;
267601d7584cSLiam Girdwood 	}
267701d7584cSLiam Girdwood 
267801d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
267901d7584cSLiam Girdwood 	if (ret < 0)
268001d7584cSLiam Girdwood 		goto out;
268101d7584cSLiam Girdwood 
268201d7584cSLiam Girdwood 	/* call prepare on the frontend */
268301d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
268401d7584cSLiam Girdwood 	if (ret < 0) {
2685103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
268601d7584cSLiam Girdwood 			fe->dai_link->name);
268701d7584cSLiam Girdwood 		goto out;
268801d7584cSLiam Girdwood 	}
268901d7584cSLiam Girdwood 
269001d7584cSLiam Girdwood 	/* run the stream event for each BE */
269101d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
269201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
269301d7584cSLiam Girdwood 
269401d7584cSLiam Girdwood out:
2695ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
269601d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
269701d7584cSLiam Girdwood 
269801d7584cSLiam Girdwood 	return ret;
269901d7584cSLiam Girdwood }
270001d7584cSLiam Girdwood 
2701618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2702618dae11SLiam Girdwood {
270307bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
270407bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
270507bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2706618dae11SLiam Girdwood 	int err;
270701d7584cSLiam Girdwood 
2708103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2709618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2710618dae11SLiam Girdwood 
271107bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
271207bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2713103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
271407bf84aaSLiam Girdwood 				fe->dai_link->name);
271507bf84aaSLiam Girdwood 
271607bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
271707bf84aaSLiam Girdwood 		if (err < 0)
2718103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
271907bf84aaSLiam Girdwood 	} else {
2720103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
272107bf84aaSLiam Girdwood 			fe->dai_link->name);
272207bf84aaSLiam Girdwood 
2723618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2724618dae11SLiam Girdwood 		if (err < 0)
2725103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
272607bf84aaSLiam Girdwood 	}
2727618dae11SLiam Girdwood 
2728618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2729618dae11SLiam Girdwood 	if (err < 0)
2730103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2731618dae11SLiam Girdwood 
2732618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2733618dae11SLiam Girdwood 	if (err < 0)
2734103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2735618dae11SLiam Girdwood 
2736618dae11SLiam Girdwood 	/* run the stream event for each BE */
2737618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2738618dae11SLiam Girdwood 
2739618dae11SLiam Girdwood 	return 0;
2740618dae11SLiam Girdwood }
2741618dae11SLiam Girdwood 
2742618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2743618dae11SLiam Girdwood {
274407bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
274507bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2746618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
274707bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2748618dae11SLiam Girdwood 	int ret;
2749a9764869SKaiChieh Chuang 	unsigned long flags;
2750618dae11SLiam Girdwood 
2751103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2752618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2753618dae11SLiam Girdwood 
2754618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2755618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2756618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2757618dae11SLiam Girdwood 		return -EINVAL;
2758618dae11SLiam Girdwood 
2759618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2760618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2761fffc0ca2SDan Carpenter 	if (ret < 0)
2762618dae11SLiam Girdwood 		goto disconnect;
2763618dae11SLiam Girdwood 
2764618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2765618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2766618dae11SLiam Girdwood 		return 0;
2767618dae11SLiam Girdwood 
2768618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2769fffc0ca2SDan Carpenter 	if (ret < 0)
2770618dae11SLiam Girdwood 		goto close;
2771618dae11SLiam Girdwood 
2772618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2773618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2774618dae11SLiam Girdwood 		return 0;
2775618dae11SLiam Girdwood 
2776618dae11SLiam Girdwood 
2777618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2778fffc0ca2SDan Carpenter 	if (ret < 0)
2779618dae11SLiam Girdwood 		goto hw_free;
2780618dae11SLiam Girdwood 
2781618dae11SLiam Girdwood 	/* run the stream event for each BE */
2782618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2783618dae11SLiam Girdwood 
2784618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2785618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2786618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2787618dae11SLiam Girdwood 		return 0;
2788618dae11SLiam Girdwood 
278907bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
279007bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2791103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
279207bf84aaSLiam Girdwood 				fe->dai_link->name);
279307bf84aaSLiam Girdwood 
279407bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
279507bf84aaSLiam Girdwood 		if (ret < 0) {
2796103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
279707bf84aaSLiam Girdwood 			goto hw_free;
279807bf84aaSLiam Girdwood 		}
279907bf84aaSLiam Girdwood 	} else {
2800103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2801618dae11SLiam Girdwood 			fe->dai_link->name);
2802618dae11SLiam Girdwood 
2803618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2804618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2805618dae11SLiam Girdwood 		if (ret < 0) {
2806103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2807618dae11SLiam Girdwood 			goto hw_free;
2808618dae11SLiam Girdwood 		}
280907bf84aaSLiam Girdwood 	}
2810618dae11SLiam Girdwood 
2811618dae11SLiam Girdwood 	return 0;
2812618dae11SLiam Girdwood 
2813618dae11SLiam Girdwood hw_free:
2814618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2815618dae11SLiam Girdwood close:
2816618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2817618dae11SLiam Girdwood disconnect:
2818618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2819a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
28208d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2821618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2822618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2823618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2824618dae11SLiam Girdwood 	}
2825a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2826618dae11SLiam Girdwood 
2827618dae11SLiam Girdwood 	return ret;
2828618dae11SLiam Girdwood }
2829618dae11SLiam Girdwood 
2830de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2831618dae11SLiam Girdwood {
2832618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
28337083f877SKuninori Morimoto 	int stream;
2834de15d7ffSJerome Brunet 	int count, paths;
2835580dff36SKuninori Morimoto 	int ret;
2836618dae11SLiam Girdwood 
2837618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2838de15d7ffSJerome Brunet 		return 0;
2839618dae11SLiam Girdwood 
2840618dae11SLiam Girdwood 	/* only check active links */
2841618dae11SLiam Girdwood 	if (!fe->cpu_dai->active)
2842de15d7ffSJerome Brunet 		return 0;
2843618dae11SLiam Girdwood 
2844618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2845de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2846de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2847618dae11SLiam Girdwood 
28487083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2849075207d2SQiao Zhou 
28507083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
28517083f877SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(fe->cpu_dai,   stream) ||
28527083f877SKuninori Morimoto 		    !snd_soc_dai_stream_valid(fe->codec_dai, stream))
28537083f877SKuninori Morimoto 			continue;
2854618dae11SLiam Girdwood 
28557083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
28567083f877SKuninori Morimoto 		if (!fe->cpu_dai->stream_active[stream] ||
28577083f877SKuninori Morimoto 		    !fe->codec_dai->stream_active[stream])
28587083f877SKuninori Morimoto 			continue;
28597083f877SKuninori Morimoto 
28607083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2861618dae11SLiam Girdwood 		if (paths < 0) {
2862103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
28637083f877SKuninori Morimoto 				 fe->dai_link->name,
28647083f877SKuninori Morimoto 				 stream == SNDRV_PCM_STREAM_PLAYBACK ?
28657083f877SKuninori Morimoto 				 "playback" : "capture");
2866618dae11SLiam Girdwood 			return paths;
2867618dae11SLiam Girdwood 		}
2868618dae11SLiam Girdwood 
28697083f877SKuninori Morimoto 		/* update any playback/capture paths */
28707083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2871de15d7ffSJerome Brunet 		if (count) {
2872580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2873de15d7ffSJerome Brunet 			if (new)
2874580dff36SKuninori Morimoto 				ret = dpcm_run_update_startup(fe, stream);
2875de15d7ffSJerome Brunet 			else
2876580dff36SKuninori Morimoto 				ret = dpcm_run_update_shutdown(fe, stream);
2877580dff36SKuninori Morimoto 			if (ret < 0)
2878580dff36SKuninori Morimoto 				dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2879580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2880de15d7ffSJerome Brunet 
28817083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
28827083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2883618dae11SLiam Girdwood 		}
2884618dae11SLiam Girdwood 
28857ed9de76SQiao Zhou 		dpcm_path_put(&list);
2886618dae11SLiam Girdwood 	}
2887618dae11SLiam Girdwood 
2888de15d7ffSJerome Brunet 	return 0;
2889618dae11SLiam Girdwood }
2890618dae11SLiam Girdwood 
2891de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2892de15d7ffSJerome Brunet  * any DAI links.
2893de15d7ffSJerome Brunet  */
2894de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card)
2895de15d7ffSJerome Brunet {
2896de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2897de15d7ffSJerome Brunet 	int ret = 0;
2898de15d7ffSJerome Brunet 
2899de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2900de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2901bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2902de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2903de15d7ffSJerome Brunet 		if (ret)
2904de15d7ffSJerome Brunet 			goto out;
2905de15d7ffSJerome Brunet 	}
2906de15d7ffSJerome Brunet 
2907de15d7ffSJerome Brunet 	/* bring new paths up */
2908bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2909de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2910de15d7ffSJerome Brunet 		if (ret)
2911de15d7ffSJerome Brunet 			goto out;
2912de15d7ffSJerome Brunet 	}
2913de15d7ffSJerome Brunet 
2914de15d7ffSJerome Brunet out:
2915618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2916de15d7ffSJerome Brunet 	return ret;
2917618dae11SLiam Girdwood }
291801d7584cSLiam Girdwood 
291945c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
292001d7584cSLiam Girdwood {
292101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
292201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
292301d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
292401d7584cSLiam Girdwood 	int ret;
292501d7584cSLiam Girdwood 	int stream = fe_substream->stream;
292601d7584cSLiam Girdwood 
292701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
292801d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
292901d7584cSLiam Girdwood 
29308f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
29318f70e515SQiao Zhou 	if (ret < 0) {
2932cae06eb9SKuninori Morimoto 		goto open_end;
29338f70e515SQiao Zhou 	} else if (ret == 0) {
2934103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
293501d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
293601d7584cSLiam Girdwood 	}
293701d7584cSLiam Girdwood 
293801d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
293901d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
294001d7584cSLiam Girdwood 
294101d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
294201d7584cSLiam Girdwood 	if (ret < 0) {
294301d7584cSLiam Girdwood 		/* clean up all links */
29448d6258a4SKuninori Morimoto 		for_each_dpcm_be(fe, stream, dpcm)
294501d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
294601d7584cSLiam Girdwood 
294701d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
294801d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
294901d7584cSLiam Girdwood 	}
295001d7584cSLiam Girdwood 
295101d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
295201d7584cSLiam Girdwood 	dpcm_path_put(&list);
2953cae06eb9SKuninori Morimoto open_end:
295401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
295501d7584cSLiam Girdwood 	return ret;
295601d7584cSLiam Girdwood }
295701d7584cSLiam Girdwood 
295845c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
295901d7584cSLiam Girdwood {
296001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
296101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
296201d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
296301d7584cSLiam Girdwood 
296401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
296501d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
296601d7584cSLiam Girdwood 
296701d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
29688d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
296901d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
297001d7584cSLiam Girdwood 
297101d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
297201d7584cSLiam Girdwood 
297301d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
297401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
297501d7584cSLiam Girdwood 	return ret;
297601d7584cSLiam Girdwood }
297701d7584cSLiam Girdwood 
2978ddee627cSLiam Girdwood /* create a new pcm */
2979ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2980ddee627cSLiam Girdwood {
29812e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2982*19bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
29832b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
2984ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2985ddee627cSLiam Girdwood 	char new_name[64];
2986ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
29872e5894d7SBenoit Cousson 	int i;
2988ddee627cSLiam Girdwood 
298901d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
29901e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
29911e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
299201d7584cSLiam Girdwood 	} else {
2993a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2994a4877a6fSStephan Gerhold 		int cpu_capture = rtd->dai_link->params ?
2995a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2996a4877a6fSStephan Gerhold 		int cpu_playback = rtd->dai_link->params ?
2997a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2998a342031cSJerome Brunet 
29990b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
3000*19bdcc7aSShreyas NC 			if (rtd->num_cpus == 1) {
3001*19bdcc7aSShreyas NC 				cpu_dai = rtd->cpu_dais[0];
3002*19bdcc7aSShreyas NC 			} else if (rtd->num_cpus == rtd->num_codecs) {
3003*19bdcc7aSShreyas NC 				cpu_dai = rtd->cpu_dais[i];
3004*19bdcc7aSShreyas NC 			} else {
3005*19bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
3006*19bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
3007*19bdcc7aSShreyas NC 				return -EINVAL;
3008*19bdcc7aSShreyas NC 			}
3009*19bdcc7aSShreyas NC 
3010467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
3011a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
3012ddee627cSLiam Girdwood 				playback = 1;
3013467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
3014a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
3015ddee627cSLiam Girdwood 				capture = 1;
301601d7584cSLiam Girdwood 		}
30172e5894d7SBenoit Cousson 	}
30182e5894d7SBenoit Cousson 
3019d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
3020d6bead02SFabio Estevam 		playback = 1;
3021d6bead02SFabio Estevam 		capture = 0;
3022d6bead02SFabio Estevam 	}
3023d6bead02SFabio Estevam 
3024d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
3025d6bead02SFabio Estevam 		playback = 0;
3026d6bead02SFabio Estevam 		capture = 1;
3027d6bead02SFabio Estevam 	}
3028d6bead02SFabio Estevam 
302901d7584cSLiam Girdwood 	/* create the PCM */
3030a342031cSJerome Brunet 	if (rtd->dai_link->params) {
3031a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
3032a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
3033a342031cSJerome Brunet 
3034a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3035a342031cSJerome Brunet 					   playback, capture, &pcm);
3036a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
303701d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
303801d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
303901d7584cSLiam Girdwood 
304001d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
304101d7584cSLiam Girdwood 				playback, capture, &pcm);
304201d7584cSLiam Girdwood 	} else {
304301d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
304401d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
304501d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
304601d7584cSLiam Girdwood 		else
304701d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
30482e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
30492e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
30502e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
305101d7584cSLiam Girdwood 
305201d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
305301d7584cSLiam Girdwood 			capture, &pcm);
305401d7584cSLiam Girdwood 	}
3055ddee627cSLiam Girdwood 	if (ret < 0) {
3056103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
30575cb9b748SLiam Girdwood 			rtd->dai_link->name);
3058ddee627cSLiam Girdwood 		return ret;
3059ddee627cSLiam Girdwood 	}
3060103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3061ddee627cSLiam Girdwood 
3062ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
3063a342031cSJerome Brunet 	if (rtd->dai_link->params)
30644bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
3065a342031cSJerome Brunet 	else
306683f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
3067ddee627cSLiam Girdwood 
306848c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
3069ddee627cSLiam Girdwood 	rtd->pcm = pcm;
3070ddee627cSLiam Girdwood 	pcm->private_data = rtd;
307101d7584cSLiam Girdwood 
3072a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
307301d7584cSLiam Girdwood 		if (playback)
307401d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
307501d7584cSLiam Girdwood 		if (capture)
307601d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
307701d7584cSLiam Girdwood 		goto out;
307801d7584cSLiam Girdwood 	}
307901d7584cSLiam Girdwood 
308001d7584cSLiam Girdwood 	/* ASoC PCM operations */
308101d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
308201d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
308301d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
308401d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
308501d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
308601d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
308701d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
308801d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
308901d7584cSLiam Girdwood 	} else {
309001d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
309101d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
309201d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
309301d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
309401d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
309501d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
309601d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
309701d7584cSLiam Girdwood 	}
309801d7584cSLiam Girdwood 
3099613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
31002b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
3101b8135864SKuninori Morimoto 
31023b1c952cSTakashi Iwai 		if (drv->ioctl)
31033b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
31041e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
31051e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
3106e9067bb5SKuninori Morimoto 		if (drv->copy_user)
310782d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
3108e9067bb5SKuninori Morimoto 		if (drv->page)
31099c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
3110e9067bb5SKuninori Morimoto 		if (drv->mmap)
3111205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
3112b8135864SKuninori Morimoto 	}
3113b8135864SKuninori Morimoto 
3114ddee627cSLiam Girdwood 	if (playback)
311501d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3116ddee627cSLiam Girdwood 
3117ddee627cSLiam Girdwood 	if (capture)
311801d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3119ddee627cSLiam Girdwood 
3120b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
3121ddee627cSLiam Girdwood 	if (ret < 0) {
31227484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3123ddee627cSLiam Girdwood 		return ret;
3124ddee627cSLiam Girdwood 	}
3125c641e5b2SJohan Hovold 
31263d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
312701d7584cSLiam Girdwood out:
31282e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
31292e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3130*19bdcc7aSShreyas NC 		 (rtd->num_cpus > 1) ? "multicpu" : rtd->cpu_dai->name);
3131ddee627cSLiam Girdwood 	return ret;
3132ddee627cSLiam Girdwood }
313301d7584cSLiam Girdwood 
313401d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
313501d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
313601d7584cSLiam Girdwood {
313701d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
313801d7584cSLiam Girdwood 		return 1;
313901d7584cSLiam Girdwood 	return 0;
314001d7584cSLiam Girdwood }
314101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
314201d7584cSLiam Girdwood 
314301d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
314401d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
314501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
314601d7584cSLiam Girdwood {
314701d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
314801d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
314901d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
315001d7584cSLiam Girdwood 		return 1;
315101d7584cSLiam Girdwood 	return 0;
315201d7584cSLiam Girdwood }
315301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
315401d7584cSLiam Girdwood 
315501d7584cSLiam Girdwood /* get the substream for this BE */
315601d7584cSLiam Girdwood struct snd_pcm_substream *
315701d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
315801d7584cSLiam Girdwood {
315901d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
316001d7584cSLiam Girdwood }
316101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
316201d7584cSLiam Girdwood 
3163085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3164085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
3165085d22beSKuninori Morimoto 				    int stream,
3166085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
3167085d22beSKuninori Morimoto 				    int num_states)
316801d7584cSLiam Girdwood {
316901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
317001d7584cSLiam Girdwood 	int state;
3171a9764869SKaiChieh Chuang 	int ret = 1;
3172a9764869SKaiChieh Chuang 	unsigned long flags;
3173085d22beSKuninori Morimoto 	int i;
317401d7584cSLiam Girdwood 
3175a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3176d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
317701d7584cSLiam Girdwood 
317801d7584cSLiam Girdwood 		if (dpcm->fe == fe)
317901d7584cSLiam Girdwood 			continue;
318001d7584cSLiam Girdwood 
318101d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
3182085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
3183085d22beSKuninori Morimoto 			if (state == states[i]) {
3184a9764869SKaiChieh Chuang 				ret = 0;
3185a9764869SKaiChieh Chuang 				break;
318601d7584cSLiam Girdwood 			}
3187a9764869SKaiChieh Chuang 		}
3188085d22beSKuninori Morimoto 	}
3189a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
319001d7584cSLiam Girdwood 
3191085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
3192a9764869SKaiChieh Chuang 	return ret;
319301d7584cSLiam Girdwood }
3194085d22beSKuninori Morimoto 
3195085d22beSKuninori Morimoto /*
3196085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3197085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3198085d22beSKuninori Morimoto  */
3199085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3200085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3201085d22beSKuninori Morimoto {
3202085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3203085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3204085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3205085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3206085d22beSKuninori Morimoto 	};
3207085d22beSKuninori Morimoto 
3208085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3209085d22beSKuninori Morimoto }
321001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
321101d7584cSLiam Girdwood 
321201d7584cSLiam Girdwood /*
321301d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
321401d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
321501d7584cSLiam Girdwood  */
321601d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
321701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
321801d7584cSLiam Girdwood {
3219085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3220085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3221085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3222085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3223085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3224085d22beSKuninori Morimoto 	};
322501d7584cSLiam Girdwood 
3226085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
322701d7584cSLiam Girdwood }
322801d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3229