xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 30819358ae73326269ba61597be47d5036e05b08)
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 */
69d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset,
70c3212829SKuninori Morimoto 			   "[%s - %s]\n", fe->dai_link->name,
71c3212829SKuninori Morimoto 			   stream ? "Capture" : "Playback");
72c3212829SKuninori Morimoto 
73d0c9abb8STakashi Iwai 	offset += scnprintf(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))
78d0c9abb8STakashi Iwai 		offset += scnprintf(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 */
86d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset, "Backends:\n");
87c3212829SKuninori Morimoto 
88c3212829SKuninori Morimoto 	if (list_empty(&fe->dpcm[stream].be_clients)) {
89d0c9abb8STakashi Iwai 		offset += scnprintf(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 
99d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
100c3212829SKuninori Morimoto 				   "- %s\n", be->dai_link->name);
101c3212829SKuninori Morimoto 
102d0c9abb8STakashi Iwai 		offset += scnprintf(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))
108d0c9abb8STakashi Iwai 			offset += scnprintf(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 
1286e1276a5SBard Liao 	if (fe->num_cpus > 1) {
1296e1276a5SBard Liao 		dev_err(fe->dev,
1306e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
1316e1276a5SBard Liao 		return -EINVAL;
1326e1276a5SBard Liao 	}
1336e1276a5SBard Liao 
134c3212829SKuninori Morimoto 	buf = kmalloc(out_count, GFP_KERNEL);
135c3212829SKuninori Morimoto 	if (!buf)
136c3212829SKuninori Morimoto 		return -ENOMEM;
137c3212829SKuninori Morimoto 
138c3212829SKuninori Morimoto 	for_each_pcm_streams(stream)
139c2233a26SKuninori Morimoto 		if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
140c3212829SKuninori Morimoto 			offset += dpcm_show_state(fe, stream,
141c3212829SKuninori Morimoto 						  buf + offset,
142c3212829SKuninori Morimoto 						  out_count - offset);
143c3212829SKuninori Morimoto 
144c3212829SKuninori Morimoto 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
145c3212829SKuninori Morimoto 
146c3212829SKuninori Morimoto 	kfree(buf);
147c3212829SKuninori Morimoto 	return ret;
148c3212829SKuninori Morimoto }
149c3212829SKuninori Morimoto 
150c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
151c3212829SKuninori Morimoto 	.open = simple_open,
152c3212829SKuninori Morimoto 	.read = dpcm_state_read_file,
153c3212829SKuninori Morimoto 	.llseek = default_llseek,
154c3212829SKuninori Morimoto };
155c3212829SKuninori Morimoto 
156c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
157c3212829SKuninori Morimoto {
158c3212829SKuninori Morimoto 	if (!rtd->dai_link)
159c3212829SKuninori Morimoto 		return;
160c3212829SKuninori Morimoto 
161c3212829SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
162c3212829SKuninori Morimoto 		return;
163c3212829SKuninori Morimoto 
164c3212829SKuninori Morimoto 	if (!rtd->card->debugfs_card_root)
165c3212829SKuninori Morimoto 		return;
166c3212829SKuninori Morimoto 
167c3212829SKuninori Morimoto 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
168c3212829SKuninori Morimoto 						    rtd->card->debugfs_card_root);
169c3212829SKuninori Morimoto 
170c3212829SKuninori Morimoto 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
171c3212829SKuninori Morimoto 			    rtd, &dpcm_state_fops);
172c3212829SKuninori Morimoto }
173154dae87SKuninori Morimoto 
174154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
175154dae87SKuninori Morimoto {
176154dae87SKuninori Morimoto 	char *name;
177154dae87SKuninori Morimoto 
178154dae87SKuninori Morimoto 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
179154dae87SKuninori Morimoto 			 stream ? "capture" : "playback");
180154dae87SKuninori Morimoto 	if (name) {
181154dae87SKuninori Morimoto 		dpcm->debugfs_state = debugfs_create_dir(
182154dae87SKuninori Morimoto 			name, dpcm->fe->debugfs_dpcm_root);
183154dae87SKuninori Morimoto 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
184154dae87SKuninori Morimoto 				   &dpcm->state);
185154dae87SKuninori Morimoto 		kfree(name);
186154dae87SKuninori Morimoto 	}
187154dae87SKuninori Morimoto }
188154dae87SKuninori Morimoto 
189154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
190154dae87SKuninori Morimoto {
191154dae87SKuninori Morimoto 	debugfs_remove_recursive(dpcm->debugfs_state);
192154dae87SKuninori Morimoto }
193154dae87SKuninori Morimoto 
194154dae87SKuninori Morimoto #else
195154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
196154dae87SKuninori Morimoto 					     int stream)
197154dae87SKuninori Morimoto {
198154dae87SKuninori Morimoto }
199154dae87SKuninori Morimoto 
200154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
201154dae87SKuninori Morimoto {
202154dae87SKuninori Morimoto }
203c3212829SKuninori Morimoto #endif
204c3212829SKuninori Morimoto 
205f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
206f183f927SKuninori Morimoto 			   struct snd_pcm_substream *substream)
207f183f927SKuninori Morimoto {
208f183f927SKuninori Morimoto 	if (rtd->dai_link->ops &&
209f183f927SKuninori Morimoto 	    rtd->dai_link->ops->startup)
210f183f927SKuninori Morimoto 		return rtd->dai_link->ops->startup(substream);
211f183f927SKuninori Morimoto 	return 0;
212f183f927SKuninori Morimoto }
213f183f927SKuninori Morimoto 
2140be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
2150be429f9SKuninori Morimoto 			     struct snd_pcm_substream *substream)
2160be429f9SKuninori Morimoto {
2170be429f9SKuninori Morimoto 	if (rtd->dai_link->ops &&
2180be429f9SKuninori Morimoto 	    rtd->dai_link->ops->shutdown)
2190be429f9SKuninori Morimoto 		rtd->dai_link->ops->shutdown(substream);
2200be429f9SKuninori Morimoto }
2210be429f9SKuninori Morimoto 
22244c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
22344c1a75bSKuninori Morimoto 			   struct snd_pcm_substream *substream)
22444c1a75bSKuninori Morimoto {
22544c1a75bSKuninori Morimoto 	if (rtd->dai_link->ops &&
22644c1a75bSKuninori Morimoto 	    rtd->dai_link->ops->prepare)
22744c1a75bSKuninori Morimoto 		return rtd->dai_link->ops->prepare(substream);
22844c1a75bSKuninori Morimoto 	return 0;
22944c1a75bSKuninori Morimoto }
23044c1a75bSKuninori Morimoto 
231de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
232de9ad990SKuninori Morimoto 			     struct snd_pcm_substream *substream,
233de9ad990SKuninori Morimoto 			     struct snd_pcm_hw_params *params)
234de9ad990SKuninori Morimoto {
235de9ad990SKuninori Morimoto 	if (rtd->dai_link->ops &&
236de9ad990SKuninori Morimoto 	    rtd->dai_link->ops->hw_params)
237de9ad990SKuninori Morimoto 		return rtd->dai_link->ops->hw_params(substream, params);
238de9ad990SKuninori Morimoto 	return 0;
239de9ad990SKuninori Morimoto }
240de9ad990SKuninori Morimoto 
24149f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
24249f020e5SKuninori Morimoto 			    struct snd_pcm_substream *substream)
24349f020e5SKuninori Morimoto {
24449f020e5SKuninori Morimoto 	if (rtd->dai_link->ops &&
24549f020e5SKuninori Morimoto 	    rtd->dai_link->ops->hw_free)
24649f020e5SKuninori Morimoto 		rtd->dai_link->ops->hw_free(substream);
24749f020e5SKuninori Morimoto }
24849f020e5SKuninori Morimoto 
249ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
250ad2bf9f2SKuninori Morimoto 			   struct snd_pcm_substream *substream,
251ad2bf9f2SKuninori Morimoto 			   int cmd)
252ad2bf9f2SKuninori Morimoto {
253ad2bf9f2SKuninori Morimoto 	if (rtd->dai_link->ops &&
254ad2bf9f2SKuninori Morimoto 	    rtd->dai_link->ops->trigger)
255ad2bf9f2SKuninori Morimoto 		return rtd->dai_link->ops->trigger(substream, cmd);
256ad2bf9f2SKuninori Morimoto 	return 0;
257ad2bf9f2SKuninori Morimoto }
258ad2bf9f2SKuninori Morimoto 
2597a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2607a5aaba4SKuninori Morimoto 				   int stream, int action)
2617a5aaba4SKuninori Morimoto {
262c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
2637a5aaba4SKuninori Morimoto 	int i;
2647a5aaba4SKuninori Morimoto 
2657a5aaba4SKuninori Morimoto 	lockdep_assert_held(&rtd->card->pcm_mutex);
2667a5aaba4SKuninori Morimoto 
267c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
268c840f769SKuninori Morimoto 		dai->stream_active[stream] += action;
269c840f769SKuninori Morimoto 		dai->active += action;
270c840f769SKuninori Morimoto 		dai->component->active += action;
2717a5aaba4SKuninori Morimoto 	}
2727a5aaba4SKuninori Morimoto }
2737a5aaba4SKuninori Morimoto 
27490996f43SLars-Peter Clausen /**
27524894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
27624894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
27724894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
27824894b76SLars-Peter Clausen  *
27924894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
28024894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
28124894b76SLars-Peter Clausen  *
28272b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
28324894b76SLars-Peter Clausen  */
28424894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
28524894b76SLars-Peter Clausen {
2867a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, 1);
28724894b76SLars-Peter Clausen }
288f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_runtime_activate);
28924894b76SLars-Peter Clausen 
29024894b76SLars-Peter Clausen /**
29124894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
29224894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
29324894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
29424894b76SLars-Peter Clausen  *
29524894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
29624894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
29724894b76SLars-Peter Clausen  *
29872b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
29924894b76SLars-Peter Clausen  */
30024894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
30124894b76SLars-Peter Clausen {
3027a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, -1);
30324894b76SLars-Peter Clausen }
304f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_runtime_deactivate);
30524894b76SLars-Peter Clausen 
30624894b76SLars-Peter Clausen /**
307208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
308208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
309208a1589SLars-Peter Clausen  *
310208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
311208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
312208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
313208a1589SLars-Peter Clausen  * from having the delay.
314208a1589SLars-Peter Clausen  */
315208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
316208a1589SLars-Peter Clausen {
317fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
3182e5894d7SBenoit Cousson 	bool ignore = true;
319613fb500SKuninori Morimoto 	int i;
3202e5894d7SBenoit Cousson 
321208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
322208a1589SLars-Peter Clausen 		return true;
323208a1589SLars-Peter Clausen 
324613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
32572c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
326fbb16563SKuninori Morimoto 
327fbb16563SKuninori Morimoto 	return ignore;
328208a1589SLars-Peter Clausen }
329208a1589SLars-Peter Clausen 
330208a1589SLars-Peter Clausen /**
33190996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
33290996f43SLars-Peter Clausen  * @substream: the pcm substream
33390996f43SLars-Peter Clausen  * @hw: the hardware parameters
33490996f43SLars-Peter Clausen  *
33590996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
33690996f43SLars-Peter Clausen  */
33790996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
33890996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
33990996f43SLars-Peter Clausen {
34090996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
34190996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
34290996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
34390996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
34490996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
34590996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
34690996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
34790996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
34890996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
34990996f43SLars-Peter Clausen 	return 0;
35090996f43SLars-Peter Clausen }
35190996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
35290996f43SLars-Peter Clausen 
35301d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
35423607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
35501d7584cSLiam Girdwood 	int event)
35601d7584cSLiam Girdwood {
35701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
35801d7584cSLiam Girdwood 
3598d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
36001d7584cSLiam Girdwood 
36101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
36201d7584cSLiam Girdwood 
363103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
36401d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
36501d7584cSLiam Girdwood 
366b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
367b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
368b1cd2e34SBanajit Goswami 			continue;
369b1cd2e34SBanajit Goswami 
37001d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
37101d7584cSLiam Girdwood 	}
37201d7584cSLiam Girdwood 
37301d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
37401d7584cSLiam Girdwood 
37501d7584cSLiam Girdwood 	return 0;
37601d7584cSLiam Girdwood }
37701d7584cSLiam Girdwood 
37817841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
37917841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
380ddee627cSLiam Girdwood {
381ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
382ddee627cSLiam Girdwood 	int ret;
383ddee627cSLiam Girdwood 
3843635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
3853635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
3863635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
3873635bf09SNicolin Chen 				soc_dai->rate);
388ddee627cSLiam Girdwood 
3894dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
390ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
3914dcdd43bSLars-Peter Clausen 						soc_dai->rate);
392ddee627cSLiam Girdwood 		if (ret < 0) {
39317841020SDong Aisheng 			dev_err(soc_dai->dev,
3943635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
395103d84a3SLiam Girdwood 				ret);
396ddee627cSLiam Girdwood 			return ret;
397ddee627cSLiam Girdwood 		}
3983635bf09SNicolin Chen 	}
3993635bf09SNicolin Chen 
4003635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
4013635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
4023635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
4033635bf09SNicolin Chen 				soc_dai->channels);
4043635bf09SNicolin Chen 
4054dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4063635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
4073635bf09SNicolin Chen 						soc_dai->channels);
4083635bf09SNicolin Chen 		if (ret < 0) {
4093635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4103635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
4113635bf09SNicolin Chen 				ret);
4123635bf09SNicolin Chen 			return ret;
4133635bf09SNicolin Chen 		}
4143635bf09SNicolin Chen 	}
4153635bf09SNicolin Chen 
4163635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
4173635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
4183635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
4193635bf09SNicolin Chen 				soc_dai->sample_bits);
4203635bf09SNicolin Chen 
4214dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4223635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
4233635bf09SNicolin Chen 						soc_dai->sample_bits);
4243635bf09SNicolin Chen 		if (ret < 0) {
4253635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4263635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
4273635bf09SNicolin Chen 				ret);
4283635bf09SNicolin Chen 			return ret;
4293635bf09SNicolin Chen 		}
4303635bf09SNicolin Chen 	}
431ddee627cSLiam Girdwood 
432ddee627cSLiam Girdwood 	return 0;
433ddee627cSLiam Girdwood }
434ddee627cSLiam Girdwood 
4353635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4363635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4373635bf09SNicolin Chen {
4383635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
439c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
44019bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4412e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
4423635bf09SNicolin Chen 
4433635bf09SNicolin Chen 	rate = params_rate(params);
4443635bf09SNicolin Chen 	channels = params_channels(params);
4453635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
4463635bf09SNicolin Chen 
4473635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
44819bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_rates;
44919bdcc7aSShreyas NC 
450c840f769SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, dai)
451c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_rates;
4522e5894d7SBenoit Cousson 
45319bdcc7aSShreyas NC 	if (symmetry) {
454a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
45519bdcc7aSShreyas NC 			if (cpu_dai->rate && cpu_dai->rate != rate) {
4563635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
4573635bf09SNicolin Chen 					cpu_dai->rate, rate);
4583635bf09SNicolin Chen 				return -EINVAL;
4593635bf09SNicolin Chen 			}
46019bdcc7aSShreyas NC 		}
46119bdcc7aSShreyas NC 	}
4623635bf09SNicolin Chen 
46319bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_channels;
46419bdcc7aSShreyas NC 
465c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
466c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_channels;
4672e5894d7SBenoit Cousson 
46819bdcc7aSShreyas NC 	if (symmetry) {
469a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
47019bdcc7aSShreyas NC 			if (cpu_dai->channels &&
47119bdcc7aSShreyas NC 			    cpu_dai->channels != channels) {
4723635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
4733635bf09SNicolin Chen 					cpu_dai->channels, channels);
4743635bf09SNicolin Chen 				return -EINVAL;
4753635bf09SNicolin Chen 			}
47619bdcc7aSShreyas NC 		}
47719bdcc7aSShreyas NC 	}
4783635bf09SNicolin Chen 
47919bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_samplebits;
48019bdcc7aSShreyas NC 
481c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
482c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_samplebits;
4832e5894d7SBenoit Cousson 
48419bdcc7aSShreyas NC 	if (symmetry) {
485a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
48619bdcc7aSShreyas NC 			if (cpu_dai->sample_bits &&
48719bdcc7aSShreyas NC 			    cpu_dai->sample_bits != sample_bits) {
4883635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
4893635bf09SNicolin Chen 					cpu_dai->sample_bits, sample_bits);
4903635bf09SNicolin Chen 				return -EINVAL;
4913635bf09SNicolin Chen 			}
49219bdcc7aSShreyas NC 		}
49319bdcc7aSShreyas NC 	}
494ddee627cSLiam Girdwood 
495ddee627cSLiam Girdwood 	return 0;
496ddee627cSLiam Girdwood }
497ddee627cSLiam Girdwood 
49862e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
49962e5f676SLars-Peter Clausen {
50062e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
50162e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
502c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
5032e5894d7SBenoit Cousson 	unsigned int symmetry, i;
50462e5f676SLars-Peter Clausen 
50519bdcc7aSShreyas NC 	symmetry = link->symmetric_rates ||
50619bdcc7aSShreyas NC 		link->symmetric_channels ||
50719bdcc7aSShreyas NC 		link->symmetric_samplebits;
50819bdcc7aSShreyas NC 
509c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
51019bdcc7aSShreyas NC 		symmetry = symmetry ||
511c840f769SKuninori Morimoto 			dai->driver->symmetric_rates ||
512c840f769SKuninori Morimoto 			dai->driver->symmetric_channels ||
513c840f769SKuninori Morimoto 			dai->driver->symmetric_samplebits;
5142e5894d7SBenoit Cousson 
5152e5894d7SBenoit Cousson 	return symmetry;
51662e5f676SLars-Peter Clausen }
51762e5f676SLars-Peter Clausen 
5182e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
51958ba9b25SMark Brown {
5202e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
521c6068d3aSTakashi Iwai 	int ret;
52258ba9b25SMark Brown 
52358ba9b25SMark Brown 	if (!bits)
52458ba9b25SMark Brown 		return;
52558ba9b25SMark Brown 
5260e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
52758ba9b25SMark Brown 	if (ret != 0)
5280e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
5290e2a3751SLars-Peter Clausen 				 bits, ret);
53058ba9b25SMark Brown }
53158ba9b25SMark Brown 
532c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
533bd477c31SLars-Peter Clausen {
534c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
53519bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5362e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
53757be9206SKuninori Morimoto 	struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
53857be9206SKuninori Morimoto 	int stream = substream->stream;
5392e5894d7SBenoit Cousson 	int i;
54019bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
541c8dd1fecSBenoit Cousson 
542a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
54357be9206SKuninori Morimoto 		pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
54457be9206SKuninori Morimoto 
54557be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
5462e5894d7SBenoit Cousson 			bits = 0;
5472e5894d7SBenoit Cousson 			break;
5482e5894d7SBenoit Cousson 		}
54957be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
5502e5894d7SBenoit Cousson 	}
55157be9206SKuninori Morimoto 
552a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
55357be9206SKuninori Morimoto 		pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
55419bdcc7aSShreyas NC 
55519bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
55619bdcc7aSShreyas NC 			cpu_bits = 0;
55719bdcc7aSShreyas NC 			break;
55819bdcc7aSShreyas NC 		}
55919bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
56019bdcc7aSShreyas NC 	}
561c8dd1fecSBenoit Cousson 
5622e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5632e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
564c8dd1fecSBenoit Cousson }
565c8dd1fecSBenoit Cousson 
5665854a464SSamuel Holland /**
5675854a464SSamuel Holland  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
5685854a464SSamuel Holland  * @rtd: ASoC PCM runtime
5695854a464SSamuel Holland  * @hw: PCM hardware parameters (output)
5705854a464SSamuel Holland  * @stream: Direction of the PCM stream
5715854a464SSamuel Holland  *
5725854a464SSamuel Holland  * Calculates the subset of stream parameters supported by all DAIs
5735854a464SSamuel Holland  * associated with the PCM stream.
5745854a464SSamuel Holland  */
5755854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
5765854a464SSamuel Holland 			    struct snd_pcm_hardware *hw, int stream)
577bd477c31SLars-Peter Clausen {
5780b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
57919bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5802e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5812e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
5822e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
58319bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5842e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
58519bdcc7aSShreyas NC 	unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
58619bdcc7aSShreyas NC 	unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
5872e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
5882e5894d7SBenoit Cousson 	int i;
58978e45c99SLars-Peter Clausen 
59019bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
591a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5920e9cf4c4SBard Liao 
5930e9cf4c4SBard Liao 		/*
5940e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream type.
5950e9cf4c4SBard Liao 		 * Otherwise, since the rate, channel, and format values will
5960e9cf4c4SBard Liao 		 * zero in that case, we would have no usable settings left,
5970e9cf4c4SBard Liao 		 * causing the resulting setup to fail.
5980e9cf4c4SBard Liao 		 */
5995854a464SSamuel Holland 		if (!snd_soc_dai_stream_valid(cpu_dai, stream))
6000e9cf4c4SBard Liao 			continue;
6010e9cf4c4SBard Liao 
60219bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
60378e45c99SLars-Peter Clausen 
60419bdcc7aSShreyas NC 		cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
60519bdcc7aSShreyas NC 		cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
60619bdcc7aSShreyas NC 		cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
60719bdcc7aSShreyas NC 		cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
60819bdcc7aSShreyas NC 		formats &= cpu_stream->formats;
60919bdcc7aSShreyas NC 		cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
61019bdcc7aSShreyas NC 							cpu_rates);
61119bdcc7aSShreyas NC 	}
61219bdcc7aSShreyas NC 
61319bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
614a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
615cde79035SRicard Wanderlof 
616cde79035SRicard Wanderlof 		/*
617cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
618cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
619cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
620cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
621cde79035SRicard Wanderlof 		 */
62225c2f515SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
623cde79035SRicard Wanderlof 			continue;
624cde79035SRicard Wanderlof 
625acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
626acf253c1SKuninori Morimoto 
6272e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
6282e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
6292e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
6302e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
6312e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
6322e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
6332e5894d7SBenoit Cousson 	}
6342e5894d7SBenoit Cousson 
6355854a464SSamuel Holland 	/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6365854a464SSamuel Holland 	if (!chan_min || !cpu_chan_min)
6375854a464SSamuel Holland 		return -EINVAL;
6385854a464SSamuel Holland 
6392e5894d7SBenoit Cousson 	/*
6402e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
64119bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6422e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6432e5894d7SBenoit Cousson 	 */
6442e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
64519bdcc7aSShreyas NC 		chan_min = cpu_chan_min;
64619bdcc7aSShreyas NC 		chan_max = cpu_chan_max;
6472e5894d7SBenoit Cousson 	}
6482e5894d7SBenoit Cousson 
64919bdcc7aSShreyas NC 	/* finally find a intersection between CODECs and CPUs */
65019bdcc7aSShreyas NC 	hw->channels_min = max(chan_min, cpu_chan_min);
65119bdcc7aSShreyas NC 	hw->channels_max = min(chan_max, cpu_chan_max);
65219bdcc7aSShreyas NC 	hw->formats = formats;
65319bdcc7aSShreyas NC 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
65478e45c99SLars-Peter Clausen 
6555854a464SSamuel Holland 	snd_pcm_hw_limit_rates(hw);
65678e45c99SLars-Peter Clausen 
65719bdcc7aSShreyas NC 	hw->rate_min = max(hw->rate_min, cpu_rate_min);
6582e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
65919bdcc7aSShreyas NC 	hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
6602e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
6615854a464SSamuel Holland 
6625854a464SSamuel Holland 	return 0;
6635854a464SSamuel Holland }
6645854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
6655854a464SSamuel Holland 
6665854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
6675854a464SSamuel Holland {
6685854a464SSamuel Holland 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
6695854a464SSamuel Holland 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
6705854a464SSamuel Holland 	u64 formats = hw->formats;
6715854a464SSamuel Holland 
6725854a464SSamuel Holland 	/*
6735854a464SSamuel Holland 	 * At least one CPU and one CODEC should match. Otherwise, we should
6745854a464SSamuel Holland 	 * have bailed out on a higher level, since there would be no CPU or
6755854a464SSamuel Holland 	 * CODEC to support the transfer direction in that case.
6765854a464SSamuel Holland 	 */
6775854a464SSamuel Holland 	snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
6785854a464SSamuel Holland 
6795854a464SSamuel Holland 	if (formats)
6805854a464SSamuel Holland 		hw->formats &= formats;
681bd477c31SLars-Peter Clausen }
682bd477c31SLars-Peter Clausen 
683dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
684e7ecfdb7SKuninori Morimoto {
685e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
686d2aaa8d8SKai Vehmanen 	struct snd_soc_component *last = NULL;
687e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
688613fb500SKuninori Morimoto 	int i, ret = 0;
689e7ecfdb7SKuninori Morimoto 
690613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
691d2aaa8d8SKai Vehmanen 		last = component;
692d2aaa8d8SKai Vehmanen 
6934a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
6944a81e8f3SKuninori Morimoto 		if (ret < 0) {
695e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
696e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
697e7ecfdb7SKuninori Morimoto 				component->name);
698d2aaa8d8SKai Vehmanen 			break;
699e7ecfdb7SKuninori Morimoto 		}
700e7ecfdb7SKuninori Morimoto 
701ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
702e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
703d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
704e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
705e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
706e7ecfdb7SKuninori Morimoto 				component->name, ret);
707d2aaa8d8SKai Vehmanen 			break;
708e7ecfdb7SKuninori Morimoto 		}
709e7ecfdb7SKuninori Morimoto 	}
710dd03907bSKuninori Morimoto 
711d2aaa8d8SKai Vehmanen 	if (ret < 0) {
712d2aaa8d8SKai Vehmanen 		/* rollback on error */
713d2aaa8d8SKai Vehmanen 		for_each_rtd_components(rtd, i, component) {
714d2aaa8d8SKai Vehmanen 			if (component == last)
715d2aaa8d8SKai Vehmanen 				break;
716d2aaa8d8SKai Vehmanen 
717d2aaa8d8SKai Vehmanen 			snd_soc_component_close(component, substream);
718d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
719d2aaa8d8SKai Vehmanen 		}
720d2aaa8d8SKai Vehmanen 	}
721d2aaa8d8SKai Vehmanen 
722d2aaa8d8SKai Vehmanen 	return ret;
723e7ecfdb7SKuninori Morimoto }
724e7ecfdb7SKuninori Morimoto 
725dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream)
726244e2936SCharles Keepax {
727244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
728244e2936SCharles Keepax 	struct snd_soc_component *component;
729e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
730244e2936SCharles Keepax 
731613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
732e82ebffcSKuninori Morimoto 		r = snd_soc_component_close(component, substream);
733e82ebffcSKuninori Morimoto 		if (r < 0)
734e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
735e82ebffcSKuninori Morimoto 
7364a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
737244e2936SCharles Keepax 	}
738244e2936SCharles Keepax 
7393672beb8SKuninori Morimoto 	return ret;
740244e2936SCharles Keepax }
741244e2936SCharles Keepax 
74258ba9b25SMark Brown /*
74362c86d1dSKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
74462c86d1dSKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
74562c86d1dSKuninori Morimoto  * shutdown.
74662c86d1dSKuninori Morimoto  */
74762c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
74862c86d1dSKuninori Morimoto {
74962c86d1dSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
75062c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
751c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
75262c86d1dSKuninori Morimoto 	int i;
75362c86d1dSKuninori Morimoto 
75462c86d1dSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
75562c86d1dSKuninori Morimoto 
75662c86d1dSKuninori Morimoto 	snd_soc_runtime_deactivate(rtd, substream->stream);
75762c86d1dSKuninori Morimoto 
758c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
759c840f769SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream);
76062c86d1dSKuninori Morimoto 
76162c86d1dSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
76262c86d1dSKuninori Morimoto 
76362c86d1dSKuninori Morimoto 	soc_pcm_components_close(substream);
76462c86d1dSKuninori Morimoto 
76562c86d1dSKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
76662c86d1dSKuninori Morimoto 
76762c86d1dSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
76862c86d1dSKuninori Morimoto 
76962c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
77062c86d1dSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
77162c86d1dSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
77262c86d1dSKuninori Morimoto 	}
77362c86d1dSKuninori Morimoto 
77462c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
77562c86d1dSKuninori Morimoto 		if (!component->active)
77662c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
77762c86d1dSKuninori Morimoto 
77862c86d1dSKuninori Morimoto 	return 0;
77962c86d1dSKuninori Morimoto }
78062c86d1dSKuninori Morimoto 
78162c86d1dSKuninori Morimoto /*
782ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
783ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
784ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
785ddee627cSLiam Girdwood  */
786ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
787ddee627cSLiam Girdwood {
788ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
789ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
79090be711eSKuninori Morimoto 	struct snd_soc_component *component;
791c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
7922e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
79319bdcc7aSShreyas NC 	const char *cpu_dai_name = "multicpu";
794244e2936SCharles Keepax 	int i, ret = 0;
795ddee627cSLiam Girdwood 
79676c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
79776c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
79890be711eSKuninori Morimoto 
799613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
80090be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
801d6652ef8SMark Brown 
80272b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
803ddee627cSLiam Girdwood 
8045d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
8055d9fa03eSKuninori Morimoto 	if (ret < 0)
8065d9fa03eSKuninori Morimoto 		goto component_err;
8075d9fa03eSKuninori Morimoto 
8085d9fa03eSKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
8095d9fa03eSKuninori Morimoto 	if (ret < 0) {
8105d9fa03eSKuninori Morimoto 		pr_err("ASoC: %s startup failed: %d\n",
8115d9fa03eSKuninori Morimoto 		       rtd->dai_link->name, ret);
812d2aaa8d8SKai Vehmanen 		goto rtd_startup_err;
8135d9fa03eSKuninori Morimoto 	}
8145d9fa03eSKuninori Morimoto 
815ddee627cSLiam Girdwood 	/* startup the audio subsystem */
816c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
817c840f769SKuninori Morimoto 		ret = snd_soc_dai_startup(dai, substream);
818ddee627cSLiam Girdwood 		if (ret < 0) {
819c840f769SKuninori Morimoto 			dev_err(dai->dev,
820c840f769SKuninori Morimoto 				"ASoC: can't open DAI %s: %d\n",
821c840f769SKuninori Morimoto 				dai->name, ret);
8225d9fa03eSKuninori Morimoto 			goto config_err;
823ddee627cSLiam Girdwood 		}
824ddee627cSLiam Girdwood 
8252e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
826c840f769SKuninori Morimoto 			dai->tx_mask = 0;
8272e5894d7SBenoit Cousson 		else
828c840f769SKuninori Morimoto 			dai->rx_mask = 0;
8292e5894d7SBenoit Cousson 	}
8302e5894d7SBenoit Cousson 
83101d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
83201d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
83301d7584cSLiam Girdwood 		goto dynamic;
83401d7584cSLiam Girdwood 
835ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8362e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8372e5894d7SBenoit Cousson 
8382e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
839c2233a26SKuninori Morimoto 		codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
840ddee627cSLiam Girdwood 
84119bdcc7aSShreyas NC 	if (rtd->num_cpus == 1)
842c2233a26SKuninori Morimoto 		cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
84319bdcc7aSShreyas NC 
84462e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
84562e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
84662e5f676SLars-Peter Clausen 
847ddee627cSLiam Girdwood 	ret = -EINVAL;
848ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
849103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
85019bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
851ddee627cSLiam Girdwood 		goto config_err;
852ddee627cSLiam Girdwood 	}
853ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
854103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
85519bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
856ddee627cSLiam Girdwood 		goto config_err;
857ddee627cSLiam Girdwood 	}
858ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
859ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
860103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
86119bdcc7aSShreyas NC 				codec_dai_name, cpu_dai_name);
862ddee627cSLiam Girdwood 		goto config_err;
863ddee627cSLiam Girdwood 	}
864ddee627cSLiam Girdwood 
865c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
86658ba9b25SMark Brown 
867ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
868c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
869c840f769SKuninori Morimoto 		if (dai->active) {
870c840f769SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, dai);
871ddee627cSLiam Girdwood 			if (ret != 0)
872ddee627cSLiam Girdwood 				goto config_err;
873ddee627cSLiam Girdwood 		}
8742e5894d7SBenoit Cousson 	}
875ddee627cSLiam Girdwood 
876103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
87719bdcc7aSShreyas NC 		 codec_dai_name, cpu_dai_name);
878103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
879103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
880ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
881103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
882ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
883ddee627cSLiam Girdwood 
88401d7584cSLiam Girdwood dynamic:
88524894b76SLars-Peter Clausen 
88624894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
88724894b76SLars-Peter Clausen 
88872b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
889ddee627cSLiam Girdwood 	return 0;
890ddee627cSLiam Girdwood 
891ddee627cSLiam Girdwood config_err:
892c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
893c840f769SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream);
8942e5894d7SBenoit Cousson 
8955d9fa03eSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
896d2aaa8d8SKai Vehmanen rtd_startup_err:
897dd03907bSKuninori Morimoto 	soc_pcm_components_close(substream);
898d2aaa8d8SKai Vehmanen component_err:
89972b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
900d6652ef8SMark Brown 
901613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
90290be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
90390be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
9043f809783SSanyog Kale 	}
9053f809783SSanyog Kale 
90676c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
90776c39e86SKuninori Morimoto 		if (!component->active)
90876c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
909d6652ef8SMark Brown 
910ddee627cSLiam Girdwood 	return ret;
911ddee627cSLiam Girdwood }
912ddee627cSLiam Girdwood 
9134bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
914a342031cSJerome Brunet {
915a342031cSJerome Brunet 	/*
916a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
917a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
918a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
919a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
920a342031cSJerome Brunet 	 */
921a342031cSJerome Brunet }
922a342031cSJerome Brunet 
923ddee627cSLiam Girdwood /*
924ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
925ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
926ddee627cSLiam Girdwood  * it can refer to the runtime info.
927ddee627cSLiam Girdwood  */
928ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
929ddee627cSLiam Girdwood {
930ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
931b8135864SKuninori Morimoto 	struct snd_soc_component *component;
932c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
9332e5894d7SBenoit Cousson 	int i, ret = 0;
934ddee627cSLiam Girdwood 
93572b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
936ddee627cSLiam Girdwood 
93744c1a75bSKuninori Morimoto 	ret = soc_rtd_prepare(rtd, substream);
938ddee627cSLiam Girdwood 	if (ret < 0) {
93944c1a75bSKuninori Morimoto 		dev_err(rtd->card->dev,
94044c1a75bSKuninori Morimoto 			"ASoC: machine prepare error: %d\n", ret);
941ddee627cSLiam Girdwood 		goto out;
942ddee627cSLiam Girdwood 	}
943ddee627cSLiam Girdwood 
944613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
9456d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
946b8135864SKuninori Morimoto 		if (ret < 0) {
947b8135864SKuninori Morimoto 			dev_err(component->dev,
948b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
949b8135864SKuninori Morimoto 			goto out;
950b8135864SKuninori Morimoto 		}
951b8135864SKuninori Morimoto 	}
952b8135864SKuninori Morimoto 
953d108c7fdSKuninori Morimoto 	ret = snd_soc_pcm_dai_prepare(substream);
954ddee627cSLiam Girdwood 	if (ret < 0) {
955d108c7fdSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
956ddee627cSLiam Girdwood 		goto out;
957ddee627cSLiam Girdwood 	}
958ddee627cSLiam Girdwood 
959ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
960ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9619bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
9629bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
963ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
964ddee627cSLiam Girdwood 	}
965ddee627cSLiam Girdwood 
966d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
967ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
968ddee627cSLiam Girdwood 
969c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
970c840f769SKuninori Morimoto 		snd_soc_dai_digital_mute(dai, 0, substream->stream);
971ddee627cSLiam Girdwood 
972ddee627cSLiam Girdwood out:
97372b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
974ddee627cSLiam Girdwood 	return ret;
975ddee627cSLiam Girdwood }
976ddee627cSLiam Girdwood 
9772e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
9782e5894d7SBenoit Cousson 				       unsigned int mask)
9792e5894d7SBenoit Cousson {
9802e5894d7SBenoit Cousson 	struct snd_interval *interval;
9812e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
9822e5894d7SBenoit Cousson 
9832e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
9842e5894d7SBenoit Cousson 	interval->min = channels;
9852e5894d7SBenoit Cousson 	interval->max = channels;
9862e5894d7SBenoit Cousson }
9872e5894d7SBenoit Cousson 
988244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
989244e2936SCharles Keepax 				      struct snd_soc_component *last)
990244e2936SCharles Keepax {
991244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
992244e2936SCharles Keepax 	struct snd_soc_component *component;
993e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
994244e2936SCharles Keepax 
995613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
996244e2936SCharles Keepax 		if (component == last)
997244e2936SCharles Keepax 			break;
998244e2936SCharles Keepax 
999e82ebffcSKuninori Morimoto 		r = snd_soc_component_hw_free(component, substream);
1000e82ebffcSKuninori Morimoto 		if (r < 0)
1001e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
1002244e2936SCharles Keepax 	}
1003244e2936SCharles Keepax 
1004eae7136aSKuninori Morimoto 	return ret;
1005244e2936SCharles Keepax }
1006244e2936SCharles Keepax 
1007ddee627cSLiam Girdwood /*
1008ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
1009ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
1010ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
1011ddee627cSLiam Girdwood  */
1012ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1013ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
1014ddee627cSLiam Girdwood {
1015ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1016b8135864SKuninori Morimoto 	struct snd_soc_component *component;
101719bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
10180b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1019244e2936SCharles Keepax 	int i, ret = 0;
1020ddee627cSLiam Girdwood 
102172b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
10225cca5951SShengjiu Wang 
10235cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
10245cca5951SShengjiu Wang 	if (ret)
10255cca5951SShengjiu Wang 		goto out;
10265cca5951SShengjiu Wang 
1027de9ad990SKuninori Morimoto 	ret = soc_rtd_hw_params(rtd, substream, params);
1028ddee627cSLiam Girdwood 	if (ret < 0) {
1029de9ad990SKuninori Morimoto 		dev_err(rtd->card->dev,
1030de9ad990SKuninori Morimoto 			"ASoC: machine hw_params failed: %d\n", ret);
1031ddee627cSLiam Girdwood 		goto out;
1032ddee627cSLiam Girdwood 	}
1033ddee627cSLiam Girdwood 
1034a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
10352e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
10362e5894d7SBenoit Cousson 
1037cde79035SRicard Wanderlof 		/*
1038cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1039cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1040cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1041cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1042cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1043cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1044cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1045cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1046cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1047cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1048cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1049cde79035SRicard Wanderlof 		 * we can do the right thing here.
1050cde79035SRicard Wanderlof 		 */
1051cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1052cde79035SRicard Wanderlof 			continue;
1053cde79035SRicard Wanderlof 
10542e5894d7SBenoit Cousson 		/* copy params for each codec */
10552e5894d7SBenoit Cousson 		codec_params = *params;
10562e5894d7SBenoit Cousson 
10572e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1058570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1059570f18b6SRander Wang 		    codec_dai->tx_mask)
10602e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10612e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
1062570f18b6SRander Wang 
1063570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1064570f18b6SRander Wang 		    codec_dai->rx_mask)
10652e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10662e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
10672e5894d7SBenoit Cousson 
1068aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1069aa6166c2SKuninori Morimoto 					    &codec_params);
107093e6958aSBenoit Cousson 		if(ret < 0)
1071ddee627cSLiam Girdwood 			goto codec_err;
1072ddee627cSLiam Girdwood 
10732e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
10742e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
10752e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
10762e5894d7SBenoit Cousson 						params_format(&codec_params));
1077078a85f2SCharles Keepax 
1078078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1079ddee627cSLiam Girdwood 	}
1080ddee627cSLiam Girdwood 
1081a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
10820e9cf4c4SBard Liao 		/*
10830e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
10840e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
10850e9cf4c4SBard Liao 		 */
10860e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
10870e9cf4c4SBard Liao 			continue;
10880e9cf4c4SBard Liao 
1089aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
109093e6958aSBenoit Cousson 		if (ret < 0)
1091ddee627cSLiam Girdwood 			goto interface_err;
1092ddee627cSLiam Girdwood 
109319bdcc7aSShreyas NC 		/* store the parameters for each DAI */
1094ca58221dSKuninori Morimoto 		cpu_dai->rate = params_rate(params);
1095ca58221dSKuninori Morimoto 		cpu_dai->channels = params_channels(params);
1096ca58221dSKuninori Morimoto 		cpu_dai->sample_bits =
1097ca58221dSKuninori Morimoto 			snd_pcm_format_physical_width(params_format(params));
1098ca58221dSKuninori Morimoto 
1099ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
110019bdcc7aSShreyas NC 	}
1101ca58221dSKuninori Morimoto 
1102613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1103245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
1104244e2936SCharles Keepax 		if (ret < 0) {
1105b8135864SKuninori Morimoto 			dev_err(component->dev,
1106b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
1107244e2936SCharles Keepax 				component->name, ret);
1108b8135864SKuninori Morimoto 			goto component_err;
1109244e2936SCharles Keepax 		}
1110244e2936SCharles Keepax 	}
1111244e2936SCharles Keepax 	component = NULL;
1112b8135864SKuninori Morimoto 
1113ddee627cSLiam Girdwood out:
111472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1115ddee627cSLiam Girdwood 	return ret;
1116ddee627cSLiam Girdwood 
1117b8135864SKuninori Morimoto component_err:
1118244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
1119b8135864SKuninori Morimoto 
112019bdcc7aSShreyas NC 	i = rtd->num_cpus;
1121ddee627cSLiam Girdwood 
1122ddee627cSLiam Girdwood interface_err:
1123a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
11240e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
11250e9cf4c4SBard Liao 			continue;
11260e9cf4c4SBard Liao 
112719bdcc7aSShreyas NC 		snd_soc_dai_hw_free(cpu_dai, substream);
112819bdcc7aSShreyas NC 		cpu_dai->rate = 0;
112919bdcc7aSShreyas NC 	}
113019bdcc7aSShreyas NC 
11312e5894d7SBenoit Cousson 	i = rtd->num_codecs;
1132ddee627cSLiam Girdwood 
1133ddee627cSLiam Girdwood codec_err:
1134a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
1135f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1136f47b9ad9SJerome Brunet 			continue;
1137f47b9ad9SJerome Brunet 
1138846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
11392e5894d7SBenoit Cousson 		codec_dai->rate = 0;
11402e5894d7SBenoit Cousson 	}
11412e5894d7SBenoit Cousson 
114249f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1143ddee627cSLiam Girdwood 
114472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1145ddee627cSLiam Girdwood 	return ret;
1146ddee627cSLiam Girdwood }
1147ddee627cSLiam Girdwood 
1148ddee627cSLiam Girdwood /*
1149ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
1150ddee627cSLiam Girdwood  */
1151ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1152ddee627cSLiam Girdwood {
1153ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1154c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
11552e5894d7SBenoit Cousson 	int i;
1156ddee627cSLiam Girdwood 
115772b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1158ddee627cSLiam Girdwood 
1159d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
1160c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
1161c840f769SKuninori Morimoto 		int active = dai->stream_active[substream->stream];
1162d3383420SNicolin Chen 
1163c840f769SKuninori Morimoto 		if (dai->active == 1) {
1164c840f769SKuninori Morimoto 			dai->rate = 0;
1165c840f769SKuninori Morimoto 			dai->channels = 0;
1166c840f769SKuninori Morimoto 			dai->sample_bits = 0;
1167d3383420SNicolin Chen 		}
11680f6011fdSKuninori Morimoto 
116967ad8777SKuninori Morimoto 		if (active == 1)
1170c840f769SKuninori Morimoto 			snd_soc_dai_digital_mute(dai, 1, substream->stream);
1171a9ee331bSKuninori Morimoto 	}
1172a9ee331bSKuninori Morimoto 
1173ddee627cSLiam Girdwood 	/* free any machine hw params */
117449f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1175ddee627cSLiam Girdwood 
1176b8135864SKuninori Morimoto 	/* free any component resources */
1177244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
1178b8135864SKuninori Morimoto 
1179ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
1180c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
1181c840f769SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(dai, substream->stream))
1182f47b9ad9SJerome Brunet 			continue;
1183f47b9ad9SJerome Brunet 
1184c840f769SKuninori Morimoto 		snd_soc_dai_hw_free(dai, substream);
11850e9cf4c4SBard Liao 	}
1186ddee627cSLiam Girdwood 
118772b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1188ddee627cSLiam Girdwood 	return 0;
1189ddee627cSLiam Girdwood }
1190ddee627cSLiam Girdwood 
11914378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1192ddee627cSLiam Girdwood {
1193ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1194b8135864SKuninori Morimoto 	struct snd_soc_component *component;
11952e5894d7SBenoit Cousson 	int i, ret;
1196ddee627cSLiam Girdwood 
1197ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
1198ddee627cSLiam Girdwood 	if (ret < 0)
1199ddee627cSLiam Girdwood 		return ret;
1200ddee627cSLiam Girdwood 
1201613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12025693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1203b8135864SKuninori Morimoto 		if (ret < 0)
1204b8135864SKuninori Morimoto 			return ret;
1205b8135864SKuninori Morimoto 	}
1206b8135864SKuninori Morimoto 
120742f2472dSKuninori Morimoto 	return snd_soc_pcm_dai_trigger(substream, cmd);
12084378f1fbSPeter Ujfalusi }
12094378f1fbSPeter Ujfalusi 
12104378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
12114378f1fbSPeter Ujfalusi {
12124378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
12134378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
12144378f1fbSPeter Ujfalusi 	int i, ret;
12154378f1fbSPeter Ujfalusi 
121642f2472dSKuninori Morimoto 	ret = snd_soc_pcm_dai_trigger(substream, cmd);
12174378f1fbSPeter Ujfalusi 	if (ret < 0)
12184378f1fbSPeter Ujfalusi 		return ret;
12194378f1fbSPeter Ujfalusi 
1220613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12214378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
12224378f1fbSPeter Ujfalusi 		if (ret < 0)
12234378f1fbSPeter Ujfalusi 			return ret;
12244378f1fbSPeter Ujfalusi 	}
12254378f1fbSPeter Ujfalusi 
1226ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
12274792b0dbSJarkko Nikula 	if (ret < 0)
12284792b0dbSJarkko Nikula 		return ret;
12294792b0dbSJarkko Nikula 
1230ddee627cSLiam Girdwood 	return 0;
1231ddee627cSLiam Girdwood }
1232ddee627cSLiam Girdwood 
12334378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
12344378f1fbSPeter Ujfalusi {
12354378f1fbSPeter Ujfalusi 	int ret;
12364378f1fbSPeter Ujfalusi 
12374378f1fbSPeter Ujfalusi 	switch (cmd) {
12384378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
12394378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
12404378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
12414378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
12424378f1fbSPeter Ujfalusi 		break;
12434378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
12444378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
12454378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
12464378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
12474378f1fbSPeter Ujfalusi 		break;
12484378f1fbSPeter Ujfalusi 	default:
12494378f1fbSPeter Ujfalusi 		return -EINVAL;
12504378f1fbSPeter Ujfalusi 	}
12514378f1fbSPeter Ujfalusi 
12524378f1fbSPeter Ujfalusi 	return ret;
12534378f1fbSPeter Ujfalusi }
12544378f1fbSPeter Ujfalusi 
1255ddee627cSLiam Girdwood /*
1256ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1257ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1258ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1259ddee627cSLiam Girdwood  */
1260ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1261ddee627cSLiam Girdwood {
1262ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
126319bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12642e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1265ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1266ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1267ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
12682e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
126919bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
12702e5894d7SBenoit Cousson 	int i;
1271ddee627cSLiam Girdwood 
12729fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
12739fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
12749fb4c2bfSAkshu Agrawal 
12750035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1276b8135864SKuninori Morimoto 
12779fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
12789fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1279b8135864SKuninori Morimoto 
1280a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
128119bdcc7aSShreyas NC 		cpu_delay = max(cpu_delay,
128219bdcc7aSShreyas NC 				snd_soc_dai_delay(cpu_dai, substream));
128319bdcc7aSShreyas NC 	}
128419bdcc7aSShreyas NC 	delay += cpu_delay;
1285ddee627cSLiam Girdwood 
1286a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12872e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
12881dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
12892e5894d7SBenoit Cousson 	}
12902e5894d7SBenoit Cousson 	delay += codec_delay;
1291ddee627cSLiam Girdwood 
1292ddee627cSLiam Girdwood 	runtime->delay = delay;
1293ddee627cSLiam Girdwood 
1294ddee627cSLiam Girdwood 	return offset;
1295ddee627cSLiam Girdwood }
1296ddee627cSLiam Girdwood 
129701d7584cSLiam Girdwood /* connect a FE and BE */
129801d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
129901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
130001d7584cSLiam Girdwood {
130101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1302a9764869SKaiChieh Chuang 	unsigned long flags;
130301d7584cSLiam Girdwood 
130401d7584cSLiam Girdwood 	/* only add new dpcms */
13058d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
130601d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
130701d7584cSLiam Girdwood 			return 0;
130801d7584cSLiam Girdwood 	}
130901d7584cSLiam Girdwood 
131001d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
131101d7584cSLiam Girdwood 	if (!dpcm)
131201d7584cSLiam Girdwood 		return -ENOMEM;
131301d7584cSLiam Girdwood 
131401d7584cSLiam Girdwood 	dpcm->be = be;
131501d7584cSLiam Girdwood 	dpcm->fe = fe;
131601d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
131701d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1318a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
131901d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
132001d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1321a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
132201d7584cSLiam Girdwood 
132301d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
132401d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
132501d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
132601d7584cSLiam Girdwood 
1327154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1328154dae87SKuninori Morimoto 
132901d7584cSLiam Girdwood 	return 1;
133001d7584cSLiam Girdwood }
133101d7584cSLiam Girdwood 
133201d7584cSLiam Girdwood /* reparent a BE onto another FE */
133301d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
133401d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
133501d7584cSLiam Girdwood {
133601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
133701d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
133801d7584cSLiam Girdwood 
133901d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
134001d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
134101d7584cSLiam Girdwood 		return;
134201d7584cSLiam Girdwood 
134301d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
134401d7584cSLiam Girdwood 
1345d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
134601d7584cSLiam Girdwood 		if (dpcm->fe == fe)
134701d7584cSLiam Girdwood 			continue;
134801d7584cSLiam Girdwood 
134901d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
135001d7584cSLiam Girdwood 			stream ? "capture" : "playback",
135101d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
135201d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
135301d7584cSLiam Girdwood 
135401d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
135501d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
135601d7584cSLiam Girdwood 		break;
135701d7584cSLiam Girdwood 	}
135801d7584cSLiam Girdwood }
135901d7584cSLiam Girdwood 
136001d7584cSLiam Girdwood /* disconnect a BE and FE */
136123607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
136201d7584cSLiam Girdwood {
136301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1364a9764869SKaiChieh Chuang 	unsigned long flags;
136501d7584cSLiam Girdwood 
13668d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1367103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
136801d7584cSLiam Girdwood 				stream ? "capture" : "playback",
136901d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
137001d7584cSLiam Girdwood 
137101d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
137201d7584cSLiam Girdwood 			continue;
137301d7584cSLiam Girdwood 
137401d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
137501d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
137601d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
137701d7584cSLiam Girdwood 
137801d7584cSLiam Girdwood 		/* BEs still alive need new FE */
137901d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
138001d7584cSLiam Girdwood 
1381154dae87SKuninori Morimoto 		dpcm_remove_debugfs_state(dpcm);
1382154dae87SKuninori Morimoto 
1383a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
138401d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
138501d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1386a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
138701d7584cSLiam Girdwood 		kfree(dpcm);
138801d7584cSLiam Girdwood 	}
138901d7584cSLiam Girdwood }
139001d7584cSLiam Girdwood 
139101d7584cSLiam Girdwood /* get BE for DAI widget and stream */
139201d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
139301d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
139401d7584cSLiam Girdwood {
139501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
139693597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
13977afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
13981a497983SMengdong Lin 	int i;
139901d7584cSLiam Girdwood 
14003c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
14013c146465SLiam Girdwood 
1402bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
140301d7584cSLiam Girdwood 
140435ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
140535ea0655SLiam Girdwood 			continue;
140635ea0655SLiam Girdwood 
1407c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
140819bdcc7aSShreyas NC 			w = snd_soc_dai_get_widget(dai, stream);
140993597faeSKuninori Morimoto 
14103c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
141193597faeSKuninori Morimoto 				w ? w->name : "(not set)");
14123c146465SLiam Girdwood 
141393597faeSKuninori Morimoto 			if (w == widget)
141401d7584cSLiam Girdwood 				return be;
141519bdcc7aSShreyas NC 		}
141601d7584cSLiam Girdwood 	}
141701d7584cSLiam Girdwood 
14189d6ee365SJerome Brunet 	/* Widget provided is not a BE */
141901d7584cSLiam Girdwood 	return NULL;
142001d7584cSLiam Girdwood }
142101d7584cSLiam Girdwood 
142201d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
142301d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
142401d7584cSLiam Girdwood {
142509e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
142601d7584cSLiam Girdwood 	int i;
142701d7584cSLiam Girdwood 
142809e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
142909e88f8aSKuninori Morimoto 		if (widget == w)
143001d7584cSLiam Girdwood 			return 1;
143101d7584cSLiam Girdwood 
143201d7584cSLiam Girdwood 	return 0;
143301d7584cSLiam Girdwood }
143401d7584cSLiam Girdwood 
14355fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
14365fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
14375fdd022cSPiotr Stankiewicz {
14385fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
14395fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1440c2cd8216SKuninori Morimoto 	int stream;
14415fdd022cSPiotr Stankiewicz 
1442c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1443c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1444c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1445c2cd8216SKuninori Morimoto 	else
1446c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1447c2cd8216SKuninori Morimoto 
1448027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1449027a4838SKuninori Morimoto 	if (rtd)
14505fdd022cSPiotr Stankiewicz 		return true;
14515fdd022cSPiotr Stankiewicz 
14525fdd022cSPiotr Stankiewicz 	return false;
14535fdd022cSPiotr Stankiewicz }
14545fdd022cSPiotr Stankiewicz 
145523607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
14561ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
145701d7584cSLiam Girdwood {
1458c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
145901d7584cSLiam Girdwood 	int paths;
146001d7584cSLiam Girdwood 
14616e1276a5SBard Liao 	if (fe->num_cpus > 1) {
14626e1276a5SBard Liao 		dev_err(fe->dev,
14636e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
14646e1276a5SBard Liao 		return -EINVAL;
14656e1276a5SBard Liao 	}
14666e1276a5SBard Liao 
146701d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
14686742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
14695fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
147001d7584cSLiam Girdwood 
1471103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
147201d7584cSLiam Girdwood 			stream ? "capture" : "playback");
147301d7584cSLiam Girdwood 
147401d7584cSLiam Girdwood 	return paths;
147501d7584cSLiam Girdwood }
147601d7584cSLiam Girdwood 
147752645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
147852645e33SKuninori Morimoto {
147952645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
148052645e33SKuninori Morimoto }
148152645e33SKuninori Morimoto 
14828cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
14838cce6569SGuennadi Liakhovetski 			      struct snd_soc_dapm_widget_list *list)
148401d7584cSLiam Girdwood {
148501d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
14867afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
14872e5894d7SBenoit Cousson 	unsigned int i;
148801d7584cSLiam Girdwood 
1489c840f769SKuninori Morimoto 	/* is there a valid DAI widget for this BE */
1490c840f769SKuninori Morimoto 	for_each_rtd_dais(dpcm->be, i, dai) {
149119bdcc7aSShreyas NC 		widget = snd_soc_dai_get_widget(dai, stream);
149201d7584cSLiam Girdwood 
149319bdcc7aSShreyas NC 		/*
1494c840f769SKuninori Morimoto 		 * The BE is pruned only if none of the dai
149519bdcc7aSShreyas NC 		 * widgets are in the active list.
149619bdcc7aSShreyas NC 		 */
149701d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
14988cce6569SGuennadi Liakhovetski 			return true;
149919bdcc7aSShreyas NC 	}
150001d7584cSLiam Girdwood 
15018cce6569SGuennadi Liakhovetski 	return false;
15028cce6569SGuennadi Liakhovetski }
15038cce6569SGuennadi Liakhovetski 
15048cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
15058cce6569SGuennadi Liakhovetski 			    struct snd_soc_dapm_widget_list **list_)
15068cce6569SGuennadi Liakhovetski {
15078cce6569SGuennadi Liakhovetski 	struct snd_soc_dpcm *dpcm;
15088cce6569SGuennadi Liakhovetski 	int prune = 0;
15098cce6569SGuennadi Liakhovetski 
15108cce6569SGuennadi Liakhovetski 	/* Destroy any old FE <--> BE connections */
15118cce6569SGuennadi Liakhovetski 	for_each_dpcm_be(fe, stream, dpcm) {
15128cce6569SGuennadi Liakhovetski 		if (dpcm_be_is_active(dpcm, stream, *list_))
1513bed646dcSKuninori Morimoto 			continue;
151401d7584cSLiam Girdwood 
1515103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
151601d7584cSLiam Girdwood 			stream ? "capture" : "playback",
151701d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
151801d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
151901d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
152001d7584cSLiam Girdwood 		prune++;
152101d7584cSLiam Girdwood 	}
152201d7584cSLiam Girdwood 
1523103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
152401d7584cSLiam Girdwood 	return prune;
152501d7584cSLiam Girdwood }
152601d7584cSLiam Girdwood 
152701d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
152801d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
152901d7584cSLiam Girdwood {
153001d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
153101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
153201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
153309e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
153401d7584cSLiam Girdwood 	int i, new = 0, err;
153501d7584cSLiam Girdwood 
153601d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
153709e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
153801d7584cSLiam Girdwood 
153909e88f8aSKuninori Morimoto 		switch (widget->id) {
15404616274dSMark Brown 		case snd_soc_dapm_dai_in:
1541c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1542c5b8540dSKoro Chen 				continue;
1543c5b8540dSKoro Chen 			break;
15444616274dSMark Brown 		case snd_soc_dapm_dai_out:
1545c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1546c5b8540dSKoro Chen 				continue;
15474616274dSMark Brown 			break;
15484616274dSMark Brown 		default:
154901d7584cSLiam Girdwood 			continue;
15504616274dSMark Brown 		}
155101d7584cSLiam Girdwood 
155201d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
155309e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
155401d7584cSLiam Girdwood 		if (!be) {
1555103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
155609e88f8aSKuninori Morimoto 					widget->name);
155701d7584cSLiam Girdwood 			continue;
155801d7584cSLiam Girdwood 		}
155901d7584cSLiam Girdwood 
156001d7584cSLiam Girdwood 		/* don't connect if FE is not running */
156123607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
156201d7584cSLiam Girdwood 			continue;
156301d7584cSLiam Girdwood 
156401d7584cSLiam Girdwood 		/* newly connected FE and BE */
156501d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
156601d7584cSLiam Girdwood 		if (err < 0) {
1567103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
156809e88f8aSKuninori Morimoto 				widget->name);
156901d7584cSLiam Girdwood 			break;
157001d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
157101d7584cSLiam Girdwood 			continue;
157201d7584cSLiam Girdwood 
157301d7584cSLiam Girdwood 		/* new */
157401d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
157501d7584cSLiam Girdwood 		new++;
157601d7584cSLiam Girdwood 	}
157701d7584cSLiam Girdwood 
1578103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
157901d7584cSLiam Girdwood 	return new;
158001d7584cSLiam Girdwood }
158101d7584cSLiam Girdwood 
158201d7584cSLiam Girdwood /*
158301d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
158401d7584cSLiam Girdwood  * FE substream.
158501d7584cSLiam Girdwood  */
158623607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
158701d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
158801d7584cSLiam Girdwood {
158901d7584cSLiam Girdwood 	if (new)
159001d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
159101d7584cSLiam Girdwood 	else
159201d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
159301d7584cSLiam Girdwood }
159401d7584cSLiam Girdwood 
159523607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
159601d7584cSLiam Girdwood {
159701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1598a9764869SKaiChieh Chuang 	unsigned long flags;
159901d7584cSLiam Girdwood 
1600a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
16018d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
160201d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
160301d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1604a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
160501d7584cSLiam Girdwood }
160601d7584cSLiam Girdwood 
160701d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
160801d7584cSLiam Girdwood 	int stream)
160901d7584cSLiam Girdwood {
161001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
161101d7584cSLiam Girdwood 
161201d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
16138d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
161401d7584cSLiam Girdwood 
161501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
161601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
161701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
161801d7584cSLiam Girdwood 
161901d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1620103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
162101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
162201d7584cSLiam Girdwood 				be->dpcm[stream].state);
162301d7584cSLiam Girdwood 
162401d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
162501d7584cSLiam Girdwood 			continue;
162601d7584cSLiam Girdwood 
162701d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
162801d7584cSLiam Girdwood 			continue;
162901d7584cSLiam Girdwood 
163001d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
163101d7584cSLiam Girdwood 		be_substream->runtime = NULL;
163201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
163301d7584cSLiam Girdwood 	}
163401d7584cSLiam Girdwood }
163501d7584cSLiam Girdwood 
163623607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
163701d7584cSLiam Girdwood {
163801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
163901d7584cSLiam Girdwood 	int err, count = 0;
164001d7584cSLiam Girdwood 
164101d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
16428d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
164301d7584cSLiam Girdwood 
164401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
164501d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
164601d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
164701d7584cSLiam Girdwood 
16482062b4c5SRussell King - ARM Linux 		if (!be_substream) {
16492062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
16502062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
16512062b4c5SRussell King - ARM Linux 			continue;
16522062b4c5SRussell King - ARM Linux 		}
16532062b4c5SRussell King - ARM Linux 
165401d7584cSLiam Girdwood 		/* is this op for this BE ? */
165501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
165601d7584cSLiam Girdwood 			continue;
165701d7584cSLiam Girdwood 
165801d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
165901d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1660103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
166101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
166201d7584cSLiam Girdwood 				be->dpcm[stream].state);
166301d7584cSLiam Girdwood 
166401d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
166501d7584cSLiam Girdwood 			continue;
166601d7584cSLiam Girdwood 
166701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
166801d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
166901d7584cSLiam Girdwood 			continue;
167001d7584cSLiam Girdwood 
16712062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
16722062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
167301d7584cSLiam Girdwood 
167401d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
167501d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
167601d7584cSLiam Girdwood 		if (err < 0) {
1677103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
167801d7584cSLiam Girdwood 			be->dpcm[stream].users--;
167901d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1680103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
168101d7584cSLiam Girdwood 					stream ? "capture" : "playback",
168201d7584cSLiam Girdwood 					be->dpcm[stream].state);
168301d7584cSLiam Girdwood 
168401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
168501d7584cSLiam Girdwood 			goto unwind;
168601d7584cSLiam Girdwood 		}
168701d7584cSLiam Girdwood 
168801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
168901d7584cSLiam Girdwood 		count++;
169001d7584cSLiam Girdwood 	}
169101d7584cSLiam Girdwood 
169201d7584cSLiam Girdwood 	return count;
169301d7584cSLiam Girdwood 
169401d7584cSLiam Girdwood unwind:
169501d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
16968d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
169701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
169801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
169901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
170001d7584cSLiam Girdwood 
170101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
170201d7584cSLiam Girdwood 			continue;
170301d7584cSLiam Girdwood 
170401d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1705103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
170601d7584cSLiam Girdwood 				stream ? "capture" : "playback",
170701d7584cSLiam Girdwood 				be->dpcm[stream].state);
170801d7584cSLiam Girdwood 
170901d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
171001d7584cSLiam Girdwood 			continue;
171101d7584cSLiam Girdwood 
171201d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
171301d7584cSLiam Girdwood 			continue;
171401d7584cSLiam Girdwood 
171501d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
171601d7584cSLiam Girdwood 		be_substream->runtime = NULL;
171701d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
171801d7584cSLiam Girdwood 	}
171901d7584cSLiam Girdwood 
172001d7584cSLiam Girdwood 	return err;
172101d7584cSLiam Girdwood }
172201d7584cSLiam Girdwood 
172308ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1724435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
172508ae9b45SLars-Peter Clausen {
172608ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1727e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
172808ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
172908ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1730002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1731435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1732002220a9SLars-Peter Clausen 	else
1733435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
173408ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
173508ae9b45SLars-Peter Clausen }
173608ae9b45SLars-Peter Clausen 
1737435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1738435ffb76SJerome Brunet 				      u64 *formats)
1739b073ed4eSKuninori Morimoto {
1740b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1741b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
17427afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1743b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1744b073ed4eSKuninori Morimoto 
1745b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1746435ffb76SJerome Brunet 		return;
1747b073ed4eSKuninori Morimoto 
1748b073ed4eSKuninori Morimoto 	/*
1749b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1750b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1751b073ed4eSKuninori Morimoto 	 */
1752b073ed4eSKuninori Morimoto 
17538d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1754b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1755b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1756b073ed4eSKuninori Morimoto 		int i;
1757b073ed4eSKuninori Morimoto 
1758a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(be, i, dai) {
17594febced1SJerome Brunet 			/*
17604febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
17614febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
17624febced1SJerome Brunet 			 */
17637afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
17644febced1SJerome Brunet 				continue;
17654febced1SJerome Brunet 
1766acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1767b073ed4eSKuninori Morimoto 
1768435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1769435ffb76SJerome Brunet 		}
1770b073ed4eSKuninori Morimoto 	}
1771b073ed4eSKuninori Morimoto }
1772b073ed4eSKuninori Morimoto 
1773435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1774f4c277b8SJiada Wang 				    unsigned int *channels_min,
1775f4c277b8SJiada Wang 				    unsigned int *channels_max)
1776f4c277b8SJiada Wang {
1777f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1778f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1779f4c277b8SJiada Wang 	int stream = substream->stream;
1780f4c277b8SJiada Wang 
1781f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1782f4c277b8SJiada Wang 		return;
1783f4c277b8SJiada Wang 
1784f4c277b8SJiada Wang 	/*
1785f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1786f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1787f4c277b8SJiada Wang 	 */
1788f4c277b8SJiada Wang 
17898d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1790f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
1791f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
17924f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
179319bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
179419bdcc7aSShreyas NC 		int i;
1795f4c277b8SJiada Wang 
1796a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(be, i, dai) {
17970e9cf4c4SBard Liao 			/*
17980e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
17990e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
18000e9cf4c4SBard Liao 			 */
18010e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
18020e9cf4c4SBard Liao 				continue;
18030e9cf4c4SBard Liao 
180419bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
18054f2bd18bSJerome Brunet 
180619bdcc7aSShreyas NC 			*channels_min = max(*channels_min,
180719bdcc7aSShreyas NC 					    cpu_stream->channels_min);
180819bdcc7aSShreyas NC 			*channels_max = min(*channels_max,
180919bdcc7aSShreyas NC 					    cpu_stream->channels_max);
181019bdcc7aSShreyas NC 		}
18114f2bd18bSJerome Brunet 
18124f2bd18bSJerome Brunet 		/*
18134f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
18144f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
18154f2bd18bSJerome Brunet 		 */
18164f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
1817c2233a26SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1818f4c277b8SJiada Wang 
1819f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1820f4c277b8SJiada Wang 					    codec_stream->channels_min);
1821f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1822f4c277b8SJiada Wang 					    codec_stream->channels_max);
1823f4c277b8SJiada Wang 		}
1824f4c277b8SJiada Wang 	}
1825f4c277b8SJiada Wang }
1826f4c277b8SJiada Wang 
1827baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1828baacd8d1SJerome Brunet 				    unsigned int *rates,
1829baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1830baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1831baacd8d1SJerome Brunet {
1832baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1833baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1834baacd8d1SJerome Brunet 	int stream = substream->stream;
1835baacd8d1SJerome Brunet 
1836baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1837baacd8d1SJerome Brunet 		return;
1838baacd8d1SJerome Brunet 
1839baacd8d1SJerome Brunet 	/*
1840baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1841baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1842baacd8d1SJerome Brunet 	 */
1843baacd8d1SJerome Brunet 
18448d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1845baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1846c840f769SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm;
18477afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1848baacd8d1SJerome Brunet 		int i;
1849baacd8d1SJerome Brunet 
1850c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
18510e9cf4c4SBard Liao 			/*
1852c840f769SKuninori Morimoto 			 * Skip DAIs which don't support the current stream
18530e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
18540e9cf4c4SBard Liao 			 */
18550e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
18560e9cf4c4SBard Liao 				continue;
18570e9cf4c4SBard Liao 
1858c840f769SKuninori Morimoto 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1859baacd8d1SJerome Brunet 
1860c840f769SKuninori Morimoto 			*rate_min = max(*rate_min, pcm->rate_min);
1861c840f769SKuninori Morimoto 			*rate_max = min_not_zero(*rate_max, pcm->rate_max);
1862c840f769SKuninori Morimoto 			*rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
1863baacd8d1SJerome Brunet 		}
1864baacd8d1SJerome Brunet 	}
1865baacd8d1SJerome Brunet }
1866baacd8d1SJerome Brunet 
186745c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
186801d7584cSLiam Girdwood {
186901d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
187001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
187119bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
187219bdcc7aSShreyas NC 	int i;
187301d7584cSLiam Girdwood 
1874a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
18750e9cf4c4SBard Liao 		/*
18760e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
18770e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
18780e9cf4c4SBard Liao 		 */
18790e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
18800e9cf4c4SBard Liao 			continue;
18810e9cf4c4SBard Liao 
18820c9ba720SKuninori Morimoto 		dpcm_init_runtime_hw(runtime,
18830c9ba720SKuninori Morimoto 			snd_soc_dai_get_pcm_stream(cpu_dai,
18840c9ba720SKuninori Morimoto 						   substream->stream));
188519bdcc7aSShreyas NC 	}
1886f4c277b8SJiada Wang 
1887435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1888435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1889435ffb76SJerome Brunet 				&runtime->hw.channels_max);
1890baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1891baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
189201d7584cSLiam Girdwood }
189301d7584cSLiam Girdwood 
1894ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1895ea9d0d77STakashi Iwai 
1896ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1897ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1898ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1899ea9d0d77STakashi Iwai  * process the pending trigger action here.
1900ea9d0d77STakashi Iwai  */
1901ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1902ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1903ea9d0d77STakashi Iwai {
1904ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1905ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1906ea9d0d77STakashi Iwai 
1907ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1908ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1909ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1910ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1911ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1912ea9d0d77STakashi Iwai 	}
1913ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1914ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1915ea9d0d77STakashi Iwai }
1916ea9d0d77STakashi Iwai 
1917906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1918906c7d69SPC Liao 			       int stream)
1919906c7d69SPC Liao {
1920906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
1921906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
192219bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
1923906c7d69SPC Liao 	int err;
192419bdcc7aSShreyas NC 	int i;
1925906c7d69SPC Liao 
1926906c7d69SPC Liao 	/* apply symmetry for FE */
1927906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
1928906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1929906c7d69SPC Liao 
1930a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1931906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1932906c7d69SPC Liao 		if (fe_cpu_dai->active) {
1933906c7d69SPC Liao 			err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1934906c7d69SPC Liao 			if (err < 0)
1935906c7d69SPC Liao 				return err;
1936906c7d69SPC Liao 		}
193719bdcc7aSShreyas NC 	}
1938906c7d69SPC Liao 
1939906c7d69SPC Liao 	/* apply symmetry for BE */
19408d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1941906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1942906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1943906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
19446246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
1945c840f769SKuninori Morimoto 		struct snd_soc_dai *dai;
1946906c7d69SPC Liao 		int i;
1947906c7d69SPC Liao 
19486246f283SJerome Brunet 		/* A backend may not have the requested substream */
19496246f283SJerome Brunet 		if (!be_substream)
19506246f283SJerome Brunet 			continue;
19516246f283SJerome Brunet 
19526246f283SJerome Brunet 		rtd = be_substream->private_data;
1953f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1954f1176614SJeeja KP 			continue;
1955f1176614SJeeja KP 
1956906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
1957906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1958906c7d69SPC Liao 
1959906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1960c840f769SKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
1961c840f769SKuninori Morimoto 			if (dai->active) {
1962c840f769SKuninori Morimoto 				err = soc_pcm_apply_symmetry(fe_substream, dai);
1963906c7d69SPC Liao 				if (err < 0)
1964906c7d69SPC Liao 					return err;
1965906c7d69SPC Liao 			}
1966906c7d69SPC Liao 		}
1967906c7d69SPC Liao 	}
1968906c7d69SPC Liao 
1969906c7d69SPC Liao 	return 0;
1970906c7d69SPC Liao }
1971906c7d69SPC Liao 
197201d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
197301d7584cSLiam Girdwood {
197401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
197501d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
197601d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
197701d7584cSLiam Girdwood 
1978ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
197901d7584cSLiam Girdwood 
198025c2f515SKuninori Morimoto 	ret = dpcm_be_dai_startup(fe, stream);
198101d7584cSLiam Girdwood 	if (ret < 0) {
1982103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
198301d7584cSLiam Girdwood 		goto be_err;
198401d7584cSLiam Girdwood 	}
198501d7584cSLiam Girdwood 
1986103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
198701d7584cSLiam Girdwood 
198801d7584cSLiam Girdwood 	/* start the DAI frontend */
198901d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
199001d7584cSLiam Girdwood 	if (ret < 0) {
1991103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
199201d7584cSLiam Girdwood 		goto unwind;
199301d7584cSLiam Girdwood 	}
199401d7584cSLiam Girdwood 
199501d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
199601d7584cSLiam Girdwood 
199701d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
199801d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
199901d7584cSLiam Girdwood 
2000906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
20018a01fbf0SKuninori Morimoto 	if (ret < 0)
2002906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2003906c7d69SPC Liao 			ret);
200401d7584cSLiam Girdwood 
200501d7584cSLiam Girdwood unwind:
20068a01fbf0SKuninori Morimoto 	if (ret < 0)
200725c2f515SKuninori Morimoto 		dpcm_be_dai_startup_unwind(fe, stream);
200801d7584cSLiam Girdwood be_err:
2009ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
201001d7584cSLiam Girdwood 	return ret;
201101d7584cSLiam Girdwood }
201201d7584cSLiam Girdwood 
201323607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
201401d7584cSLiam Girdwood {
201501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
201601d7584cSLiam Girdwood 
201701d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
20188d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
201901d7584cSLiam Girdwood 
202001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
202101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
202201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
202301d7584cSLiam Girdwood 
202401d7584cSLiam Girdwood 		/* is this op for this BE ? */
202501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
202601d7584cSLiam Girdwood 			continue;
202701d7584cSLiam Girdwood 
202801d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
2029103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
203001d7584cSLiam Girdwood 				stream ? "capture" : "playback",
203101d7584cSLiam Girdwood 				be->dpcm[stream].state);
203201d7584cSLiam Girdwood 
203301d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
203401d7584cSLiam Girdwood 			continue;
203501d7584cSLiam Girdwood 
203601d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
20379c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
20389c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
20399c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
20409c0ac70aSKai Chieh Chuang 		}
204101d7584cSLiam Girdwood 
2042103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
204394d215ccS彭东林 			be->dai_link->name);
204401d7584cSLiam Girdwood 
204501d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
204601d7584cSLiam Girdwood 		be_substream->runtime = NULL;
204701d7584cSLiam Girdwood 
204801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
204901d7584cSLiam Girdwood 	}
205001d7584cSLiam Girdwood 	return 0;
205101d7584cSLiam Girdwood }
205201d7584cSLiam Girdwood 
205301d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
205401d7584cSLiam Girdwood {
205501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
205601d7584cSLiam Girdwood 	int stream = substream->stream;
205701d7584cSLiam Girdwood 
2058ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
205901d7584cSLiam Girdwood 
206001d7584cSLiam Girdwood 	/* shutdown the BEs */
206125c2f515SKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
206201d7584cSLiam Girdwood 
2063103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
206401d7584cSLiam Girdwood 
206501d7584cSLiam Girdwood 	/* now shutdown the frontend */
206601d7584cSLiam Girdwood 	soc_pcm_close(substream);
206701d7584cSLiam Girdwood 
206801d7584cSLiam Girdwood 	/* run the stream event for each BE */
20691c531230SKuninori Morimoto 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
207001d7584cSLiam Girdwood 
207101d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2072ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
207301d7584cSLiam Girdwood 	return 0;
207401d7584cSLiam Girdwood }
207501d7584cSLiam Girdwood 
207623607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
207701d7584cSLiam Girdwood {
207801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
207901d7584cSLiam Girdwood 
208001d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
208101d7584cSLiam Girdwood 	 * to this frontend DAI */
20828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
208301d7584cSLiam Girdwood 
208401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
208501d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
208601d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
208701d7584cSLiam Girdwood 
208801d7584cSLiam Girdwood 		/* is this op for this BE ? */
208901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
209001d7584cSLiam Girdwood 			continue;
209101d7584cSLiam Girdwood 
209201d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
209301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
209401d7584cSLiam Girdwood 				continue;
209501d7584cSLiam Girdwood 
209636fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
209736fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
209836fba62cSQiao Zhou 			continue;
209936fba62cSQiao Zhou 
210001d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
210101d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
210201d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
210308b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
21045e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
21055e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
210601d7584cSLiam Girdwood 			continue;
210701d7584cSLiam Girdwood 
2108103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
210994d215ccS彭东林 			be->dai_link->name);
211001d7584cSLiam Girdwood 
211101d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
211201d7584cSLiam Girdwood 
211301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
211401d7584cSLiam Girdwood 	}
211501d7584cSLiam Girdwood 
211601d7584cSLiam Girdwood 	return 0;
211701d7584cSLiam Girdwood }
211801d7584cSLiam Girdwood 
211945c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
212001d7584cSLiam Girdwood {
212101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
212201d7584cSLiam Girdwood 	int err, stream = substream->stream;
212301d7584cSLiam Girdwood 
212401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2125ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
212601d7584cSLiam Girdwood 
2127103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
212801d7584cSLiam Girdwood 
212901d7584cSLiam Girdwood 	/* call hw_free on the frontend */
213001d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
213101d7584cSLiam Girdwood 	if (err < 0)
2132103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
213301d7584cSLiam Girdwood 			fe->dai_link->name);
213401d7584cSLiam Girdwood 
213501d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
213601d7584cSLiam Girdwood 	 * to this frontend DAI */
213701d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
213801d7584cSLiam Girdwood 
213901d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2140ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
214101d7584cSLiam Girdwood 
214201d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
214301d7584cSLiam Girdwood 	return 0;
214401d7584cSLiam Girdwood }
214501d7584cSLiam Girdwood 
214623607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
214701d7584cSLiam Girdwood {
214801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
214901d7584cSLiam Girdwood 	int ret;
215001d7584cSLiam Girdwood 
21518d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
215201d7584cSLiam Girdwood 
215301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
215401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
215501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
215601d7584cSLiam Girdwood 
215701d7584cSLiam Girdwood 		/* is this op for this BE ? */
215801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
215901d7584cSLiam Girdwood 			continue;
216001d7584cSLiam Girdwood 
216101d7584cSLiam Girdwood 		/* copy params for each dpcm */
216201d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
216301d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
216401d7584cSLiam Girdwood 
216501d7584cSLiam Girdwood 		/* perform any hw_params fixups */
216601d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
216701d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
216801d7584cSLiam Girdwood 					&dpcm->hw_params);
216901d7584cSLiam Girdwood 			if (ret < 0) {
217001d7584cSLiam Girdwood 				dev_err(be->dev,
2171103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
217201d7584cSLiam Girdwood 					ret);
217301d7584cSLiam Girdwood 				goto unwind;
217401d7584cSLiam Girdwood 			}
217501d7584cSLiam Girdwood 		}
217601d7584cSLiam Girdwood 
2177ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2178ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2179ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2180ae061d2aSLibin Yang 
2181b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2182b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2183b0639bd2SKuninori Morimoto 			continue;
2184b0639bd2SKuninori Morimoto 
2185b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2186b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2187b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2188b0639bd2SKuninori Morimoto 			continue;
2189b0639bd2SKuninori Morimoto 
2190b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
219194d215ccS彭东林 			be->dai_link->name);
2192b0639bd2SKuninori Morimoto 
219301d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
219401d7584cSLiam Girdwood 		if (ret < 0) {
219501d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2196103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
219701d7584cSLiam Girdwood 			goto unwind;
219801d7584cSLiam Girdwood 		}
219901d7584cSLiam Girdwood 
220001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
220101d7584cSLiam Girdwood 	}
220201d7584cSLiam Girdwood 	return 0;
220301d7584cSLiam Girdwood 
220401d7584cSLiam Girdwood unwind:
220501d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
22068d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
220701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
220801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
220901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
221001d7584cSLiam Girdwood 
221101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
221201d7584cSLiam Girdwood 			continue;
221301d7584cSLiam Girdwood 
221401d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
221501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
221601d7584cSLiam Girdwood 			continue;
221701d7584cSLiam Girdwood 
221801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
221901d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
222001d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
222101d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
222201d7584cSLiam Girdwood 			continue;
222301d7584cSLiam Girdwood 
222401d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
222501d7584cSLiam Girdwood 	}
222601d7584cSLiam Girdwood 
222701d7584cSLiam Girdwood 	return ret;
222801d7584cSLiam Girdwood }
222901d7584cSLiam Girdwood 
223045c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
223101d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
223201d7584cSLiam Girdwood {
223301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
223401d7584cSLiam Girdwood 	int ret, stream = substream->stream;
223501d7584cSLiam Girdwood 
223601d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2237ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
223801d7584cSLiam Girdwood 
223925c2f515SKuninori Morimoto 	memcpy(&fe->dpcm[stream].hw_params, params,
224001d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
224125c2f515SKuninori Morimoto 	ret = dpcm_be_dai_hw_params(fe, stream);
224201d7584cSLiam Girdwood 	if (ret < 0) {
2243103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
224401d7584cSLiam Girdwood 		goto out;
224501d7584cSLiam Girdwood 	}
224601d7584cSLiam Girdwood 
2247103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
224801d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
224901d7584cSLiam Girdwood 			params_channels(params), params_format(params));
225001d7584cSLiam Girdwood 
225101d7584cSLiam Girdwood 	/* call hw_params on the frontend */
225201d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
225301d7584cSLiam Girdwood 	if (ret < 0) {
2254103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
225501d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
225601d7584cSLiam Girdwood 	 } else
225701d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
225801d7584cSLiam Girdwood 
225901d7584cSLiam Girdwood out:
2260ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
226101d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
226201d7584cSLiam Girdwood 	return ret;
226301d7584cSLiam Girdwood }
226401d7584cSLiam Girdwood 
226501d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
226601d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
226701d7584cSLiam Girdwood {
226801d7584cSLiam Girdwood 	int ret;
226901d7584cSLiam Girdwood 
2270103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
227194d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
227201d7584cSLiam Girdwood 
227301d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
227401d7584cSLiam Girdwood 	if (ret < 0)
2275103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
227601d7584cSLiam Girdwood 
227701d7584cSLiam Girdwood 	return ret;
227801d7584cSLiam Girdwood }
227901d7584cSLiam Girdwood 
228023607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
228145c0a188SMark Brown 			       int cmd)
228201d7584cSLiam Girdwood {
228301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
228401d7584cSLiam Girdwood 	int ret = 0;
228501d7584cSLiam Girdwood 
22868d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
228701d7584cSLiam Girdwood 
228801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
228901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
229001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
229101d7584cSLiam Girdwood 
229201d7584cSLiam Girdwood 		/* is this op for this BE ? */
229301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
229401d7584cSLiam Girdwood 			continue;
229501d7584cSLiam Girdwood 
229601d7584cSLiam Girdwood 		switch (cmd) {
229701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
229801d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
229921fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
230021fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
230101d7584cSLiam Girdwood 				continue;
230201d7584cSLiam Girdwood 
230301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
230401d7584cSLiam Girdwood 			if (ret)
230501d7584cSLiam Girdwood 				return ret;
230601d7584cSLiam Girdwood 
230701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
230801d7584cSLiam Girdwood 			break;
230901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
231001d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
231101d7584cSLiam Girdwood 				continue;
231201d7584cSLiam Girdwood 
231301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
231401d7584cSLiam Girdwood 			if (ret)
231501d7584cSLiam Girdwood 				return ret;
231601d7584cSLiam Girdwood 
231701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
231801d7584cSLiam Girdwood 			break;
231901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
232001d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
232101d7584cSLiam Girdwood 				continue;
232201d7584cSLiam Girdwood 
232301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
232401d7584cSLiam Girdwood 			if (ret)
232501d7584cSLiam Girdwood 				return ret;
232601d7584cSLiam Girdwood 
232701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
232801d7584cSLiam Girdwood 			break;
232901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
233021fca8bdS이경택 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
233121fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
233201d7584cSLiam Girdwood 				continue;
233301d7584cSLiam Girdwood 
233401d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
233501d7584cSLiam Girdwood 				continue;
233601d7584cSLiam Girdwood 
233701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
233801d7584cSLiam Girdwood 			if (ret)
233901d7584cSLiam Girdwood 				return ret;
234001d7584cSLiam Girdwood 
234101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
234201d7584cSLiam Girdwood 			break;
234301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2344868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
234501d7584cSLiam Girdwood 				continue;
234601d7584cSLiam Girdwood 
234701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
234801d7584cSLiam Girdwood 				continue;
234901d7584cSLiam Girdwood 
235001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
235101d7584cSLiam Girdwood 			if (ret)
235201d7584cSLiam Girdwood 				return ret;
235301d7584cSLiam Girdwood 
235401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
235501d7584cSLiam Girdwood 			break;
235601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
235701d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
235801d7584cSLiam Girdwood 				continue;
235901d7584cSLiam Girdwood 
236001d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
236101d7584cSLiam Girdwood 				continue;
236201d7584cSLiam Girdwood 
236301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
236401d7584cSLiam Girdwood 			if (ret)
236501d7584cSLiam Girdwood 				return ret;
236601d7584cSLiam Girdwood 
236701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
236801d7584cSLiam Girdwood 			break;
236901d7584cSLiam Girdwood 		}
237001d7584cSLiam Girdwood 	}
237101d7584cSLiam Girdwood 
237201d7584cSLiam Girdwood 	return ret;
237301d7584cSLiam Girdwood }
237401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
237501d7584cSLiam Girdwood 
2376acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2377acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2378acbf2774SRanjani Sridharan {
2379acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2380acbf2774SRanjani Sridharan 	int ret;
2381acbf2774SRanjani Sridharan 
2382acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2383acbf2774SRanjani Sridharan 	if (fe_first) {
2384acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2385acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2386acbf2774SRanjani Sridharan 
2387acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2388acbf2774SRanjani Sridharan 		if (ret < 0)
2389acbf2774SRanjani Sridharan 			return ret;
2390acbf2774SRanjani Sridharan 
2391acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2392acbf2774SRanjani Sridharan 		return ret;
2393acbf2774SRanjani Sridharan 	}
2394acbf2774SRanjani Sridharan 
2395acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2396acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2397acbf2774SRanjani Sridharan 	if (ret < 0)
2398acbf2774SRanjani Sridharan 		return ret;
2399acbf2774SRanjani Sridharan 
2400acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2401acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2402acbf2774SRanjani Sridharan 
2403acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2404acbf2774SRanjani Sridharan 
2405acbf2774SRanjani Sridharan 	return ret;
2406acbf2774SRanjani Sridharan }
2407acbf2774SRanjani Sridharan 
2408ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
240901d7584cSLiam Girdwood {
241001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2411acbf2774SRanjani Sridharan 	int stream = substream->stream;
2412acbf2774SRanjani Sridharan 	int ret = 0;
241301d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
241401d7584cSLiam Girdwood 
241501d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
241601d7584cSLiam Girdwood 
241701d7584cSLiam Girdwood 	switch (trigger) {
241801d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2419acbf2774SRanjani Sridharan 		switch (cmd) {
2420acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2421acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2422acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2423acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2424acbf2774SRanjani Sridharan 			break;
2425acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2426acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2427acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2428acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2429acbf2774SRanjani Sridharan 			break;
2430acbf2774SRanjani Sridharan 		default:
2431acbf2774SRanjani Sridharan 			ret = -EINVAL;
2432acbf2774SRanjani Sridharan 			break;
243301d7584cSLiam Girdwood 		}
243401d7584cSLiam Girdwood 		break;
243501d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2436acbf2774SRanjani Sridharan 		switch (cmd) {
2437acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2438acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2439acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2440acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2441acbf2774SRanjani Sridharan 			break;
2442acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2443acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2444acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2445acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2446acbf2774SRanjani Sridharan 			break;
2447acbf2774SRanjani Sridharan 		default:
2448acbf2774SRanjani Sridharan 			ret = -EINVAL;
2449acbf2774SRanjani Sridharan 			break;
245001d7584cSLiam Girdwood 		}
245101d7584cSLiam Girdwood 		break;
245207bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
245307bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
245407bf84aaSLiam Girdwood 
2455103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
245607bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
245707bf84aaSLiam Girdwood 
2458*30819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
245907bf84aaSLiam Girdwood 		break;
246001d7584cSLiam Girdwood 	default:
2461103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
246201d7584cSLiam Girdwood 				fe->dai_link->name);
246301d7584cSLiam Girdwood 		ret = -EINVAL;
246401d7584cSLiam Girdwood 		goto out;
246501d7584cSLiam Girdwood 	}
246601d7584cSLiam Girdwood 
2467acbf2774SRanjani Sridharan 	if (ret < 0) {
2468acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2469acbf2774SRanjani Sridharan 			cmd, ret);
2470acbf2774SRanjani Sridharan 		goto out;
2471acbf2774SRanjani Sridharan 	}
2472acbf2774SRanjani Sridharan 
247301d7584cSLiam Girdwood 	switch (cmd) {
247401d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
247501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
247601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
247701d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
247801d7584cSLiam Girdwood 		break;
247901d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
248001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
248101d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
248201d7584cSLiam Girdwood 		break;
24839f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
24849f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
24859f169b9fSPatrick Lai 		break;
248601d7584cSLiam Girdwood 	}
248701d7584cSLiam Girdwood 
248801d7584cSLiam Girdwood out:
248901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
249001d7584cSLiam Girdwood 	return ret;
249101d7584cSLiam Girdwood }
249201d7584cSLiam Girdwood 
2493ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2494ea9d0d77STakashi Iwai {
2495ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2496ea9d0d77STakashi Iwai 	int stream = substream->stream;
2497ea9d0d77STakashi Iwai 
2498ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2499ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2500ea9d0d77STakashi Iwai 	 */
2501ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2502ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2503ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2504ea9d0d77STakashi Iwai 	}
2505ea9d0d77STakashi Iwai 
2506ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2507ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2508ea9d0d77STakashi Iwai }
2509ea9d0d77STakashi Iwai 
251023607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
251101d7584cSLiam Girdwood {
251201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
251301d7584cSLiam Girdwood 	int ret = 0;
251401d7584cSLiam Girdwood 
25158d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
251601d7584cSLiam Girdwood 
251701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
251801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
251901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
252001d7584cSLiam Girdwood 
252101d7584cSLiam Girdwood 		/* is this op for this BE ? */
252201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
252301d7584cSLiam Girdwood 			continue;
252401d7584cSLiam Girdwood 
252501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
252695f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
25275087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
25285087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
252901d7584cSLiam Girdwood 			continue;
253001d7584cSLiam Girdwood 
2531103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
253294d215ccS彭东林 			be->dai_link->name);
253301d7584cSLiam Girdwood 
253401d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
253501d7584cSLiam Girdwood 		if (ret < 0) {
2536103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
253701d7584cSLiam Girdwood 				ret);
253801d7584cSLiam Girdwood 			break;
253901d7584cSLiam Girdwood 		}
254001d7584cSLiam Girdwood 
254101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
254201d7584cSLiam Girdwood 	}
254301d7584cSLiam Girdwood 	return ret;
254401d7584cSLiam Girdwood }
254501d7584cSLiam Girdwood 
254645c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
254701d7584cSLiam Girdwood {
254801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
254901d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
255001d7584cSLiam Girdwood 
255101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
255201d7584cSLiam Girdwood 
2553103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
255401d7584cSLiam Girdwood 
2555ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
255601d7584cSLiam Girdwood 
255701d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
255801d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2559103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
256001d7584cSLiam Girdwood 				fe->dai_link->name);
256101d7584cSLiam Girdwood 		ret = -EINVAL;
256201d7584cSLiam Girdwood 		goto out;
256301d7584cSLiam Girdwood 	}
256401d7584cSLiam Girdwood 
256525c2f515SKuninori Morimoto 	ret = dpcm_be_dai_prepare(fe, stream);
256601d7584cSLiam Girdwood 	if (ret < 0)
256701d7584cSLiam Girdwood 		goto out;
256801d7584cSLiam Girdwood 
256901d7584cSLiam Girdwood 	/* call prepare on the frontend */
257001d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
257101d7584cSLiam Girdwood 	if (ret < 0) {
2572103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
257301d7584cSLiam Girdwood 			fe->dai_link->name);
257401d7584cSLiam Girdwood 		goto out;
257501d7584cSLiam Girdwood 	}
257601d7584cSLiam Girdwood 
257701d7584cSLiam Girdwood 	/* run the stream event for each BE */
257801d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
257901d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
258001d7584cSLiam Girdwood 
258101d7584cSLiam Girdwood out:
2582ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
258301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
258401d7584cSLiam Girdwood 
258501d7584cSLiam Girdwood 	return ret;
258601d7584cSLiam Girdwood }
258701d7584cSLiam Girdwood 
2588618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2589618dae11SLiam Girdwood {
259007bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
259107bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
259207bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2593618dae11SLiam Girdwood 	int err;
259401d7584cSLiam Girdwood 
2595103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2596618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2597618dae11SLiam Girdwood 
259807bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
259907bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2600103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
260107bf84aaSLiam Girdwood 				fe->dai_link->name);
260207bf84aaSLiam Girdwood 
2603*30819358SKuninori Morimoto 		err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
260407bf84aaSLiam Girdwood 		if (err < 0)
2605103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
260607bf84aaSLiam Girdwood 	} else {
2607103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
260807bf84aaSLiam Girdwood 			fe->dai_link->name);
260907bf84aaSLiam Girdwood 
2610618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2611618dae11SLiam Girdwood 		if (err < 0)
2612103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
261307bf84aaSLiam Girdwood 	}
2614618dae11SLiam Girdwood 
2615618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2616618dae11SLiam Girdwood 	if (err < 0)
2617103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2618618dae11SLiam Girdwood 
2619618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2620618dae11SLiam Girdwood 	if (err < 0)
2621103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2622618dae11SLiam Girdwood 
2623618dae11SLiam Girdwood 	/* run the stream event for each BE */
2624618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2625618dae11SLiam Girdwood 
2626618dae11SLiam Girdwood 	return 0;
2627618dae11SLiam Girdwood }
2628618dae11SLiam Girdwood 
2629618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2630618dae11SLiam Girdwood {
263107bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
263207bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2633618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
263407bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2635618dae11SLiam Girdwood 	int ret;
2636a9764869SKaiChieh Chuang 	unsigned long flags;
2637618dae11SLiam Girdwood 
2638103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2639618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2640618dae11SLiam Girdwood 
2641618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2642618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2643618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2644618dae11SLiam Girdwood 		return -EINVAL;
2645618dae11SLiam Girdwood 
2646618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2647618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2648fffc0ca2SDan Carpenter 	if (ret < 0)
2649618dae11SLiam Girdwood 		goto disconnect;
2650618dae11SLiam Girdwood 
2651618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2652618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2653618dae11SLiam Girdwood 		return 0;
2654618dae11SLiam Girdwood 
2655618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2656fffc0ca2SDan Carpenter 	if (ret < 0)
2657618dae11SLiam Girdwood 		goto close;
2658618dae11SLiam Girdwood 
2659618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2660618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2661618dae11SLiam Girdwood 		return 0;
2662618dae11SLiam Girdwood 
2663618dae11SLiam Girdwood 
2664618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2665fffc0ca2SDan Carpenter 	if (ret < 0)
2666618dae11SLiam Girdwood 		goto hw_free;
2667618dae11SLiam Girdwood 
2668618dae11SLiam Girdwood 	/* run the stream event for each BE */
2669618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2670618dae11SLiam Girdwood 
2671618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2672618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2673618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2674618dae11SLiam Girdwood 		return 0;
2675618dae11SLiam Girdwood 
267607bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
267707bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2678103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
267907bf84aaSLiam Girdwood 				fe->dai_link->name);
268007bf84aaSLiam Girdwood 
2681*30819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
268207bf84aaSLiam Girdwood 		if (ret < 0) {
2683103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
268407bf84aaSLiam Girdwood 			goto hw_free;
268507bf84aaSLiam Girdwood 		}
268607bf84aaSLiam Girdwood 	} else {
2687103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2688618dae11SLiam Girdwood 			fe->dai_link->name);
2689618dae11SLiam Girdwood 
2690618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2691618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2692618dae11SLiam Girdwood 		if (ret < 0) {
2693103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2694618dae11SLiam Girdwood 			goto hw_free;
2695618dae11SLiam Girdwood 		}
269607bf84aaSLiam Girdwood 	}
2697618dae11SLiam Girdwood 
2698618dae11SLiam Girdwood 	return 0;
2699618dae11SLiam Girdwood 
2700618dae11SLiam Girdwood hw_free:
2701618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2702618dae11SLiam Girdwood close:
2703618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2704618dae11SLiam Girdwood disconnect:
2705618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2706a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
27078d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2708618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2709618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2710618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2711618dae11SLiam Girdwood 	}
2712a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2713618dae11SLiam Girdwood 
2714618dae11SLiam Girdwood 	return ret;
2715618dae11SLiam Girdwood }
2716618dae11SLiam Girdwood 
2717de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2718618dae11SLiam Girdwood {
2719618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
27207083f877SKuninori Morimoto 	int stream;
2721de15d7ffSJerome Brunet 	int count, paths;
2722580dff36SKuninori Morimoto 	int ret;
2723618dae11SLiam Girdwood 
27246e1276a5SBard Liao 	if (fe->num_cpus > 1) {
27256e1276a5SBard Liao 		dev_err(fe->dev,
27266e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
27276e1276a5SBard Liao 		return -EINVAL;
27286e1276a5SBard Liao 	}
27296e1276a5SBard Liao 
2730618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2731de15d7ffSJerome Brunet 		return 0;
2732618dae11SLiam Girdwood 
2733618dae11SLiam Girdwood 	/* only check active links */
2734c2233a26SKuninori Morimoto 	if (!asoc_rtd_to_cpu(fe, 0)->active)
2735de15d7ffSJerome Brunet 		return 0;
2736618dae11SLiam Girdwood 
2737618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2738de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2739de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2740618dae11SLiam Girdwood 
27417083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2742075207d2SQiao Zhou 
27437083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
2744c2233a26SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0),   stream) ||
2745c2233a26SKuninori Morimoto 		    !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
27467083f877SKuninori Morimoto 			continue;
2747618dae11SLiam Girdwood 
27487083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
2749c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(fe, 0)->stream_active[stream] ||
2750c2233a26SKuninori Morimoto 		    !asoc_rtd_to_codec(fe, 0)->stream_active[stream])
27517083f877SKuninori Morimoto 			continue;
27527083f877SKuninori Morimoto 
27537083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2754618dae11SLiam Girdwood 		if (paths < 0) {
2755103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
27567083f877SKuninori Morimoto 				 fe->dai_link->name,
27577083f877SKuninori Morimoto 				 stream == SNDRV_PCM_STREAM_PLAYBACK ?
27587083f877SKuninori Morimoto 				 "playback" : "capture");
2759618dae11SLiam Girdwood 			return paths;
2760618dae11SLiam Girdwood 		}
2761618dae11SLiam Girdwood 
27627083f877SKuninori Morimoto 		/* update any playback/capture paths */
27637083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2764de15d7ffSJerome Brunet 		if (count) {
2765580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2766de15d7ffSJerome Brunet 			if (new)
2767580dff36SKuninori Morimoto 				ret = dpcm_run_update_startup(fe, stream);
2768de15d7ffSJerome Brunet 			else
2769580dff36SKuninori Morimoto 				ret = dpcm_run_update_shutdown(fe, stream);
2770580dff36SKuninori Morimoto 			if (ret < 0)
2771580dff36SKuninori Morimoto 				dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2772580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2773de15d7ffSJerome Brunet 
27747083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
27757083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2776618dae11SLiam Girdwood 		}
2777618dae11SLiam Girdwood 
27787ed9de76SQiao Zhou 		dpcm_path_put(&list);
2779618dae11SLiam Girdwood 	}
2780618dae11SLiam Girdwood 
2781de15d7ffSJerome Brunet 	return 0;
2782618dae11SLiam Girdwood }
2783618dae11SLiam Girdwood 
2784de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2785de15d7ffSJerome Brunet  * any DAI links.
2786de15d7ffSJerome Brunet  */
2787f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2788de15d7ffSJerome Brunet {
2789de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2790de15d7ffSJerome Brunet 	int ret = 0;
2791de15d7ffSJerome Brunet 
2792de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2793de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2794bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2795de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2796de15d7ffSJerome Brunet 		if (ret)
2797de15d7ffSJerome Brunet 			goto out;
2798de15d7ffSJerome Brunet 	}
2799de15d7ffSJerome Brunet 
2800de15d7ffSJerome Brunet 	/* bring new paths up */
2801bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2802de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2803de15d7ffSJerome Brunet 		if (ret)
2804de15d7ffSJerome Brunet 			goto out;
2805de15d7ffSJerome Brunet 	}
2806de15d7ffSJerome Brunet 
2807de15d7ffSJerome Brunet out:
2808618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2809de15d7ffSJerome Brunet 	return ret;
2810618dae11SLiam Girdwood }
2811f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
281201d7584cSLiam Girdwood 
2813265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
281401d7584cSLiam Girdwood {
281501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
281601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2817265694b6SKuninori Morimoto 	int stream = fe_substream->stream;
281830fca26fSKuninori Morimoto 
281930fca26fSKuninori Morimoto 	/* mark FE's links ready to prune */
282030fca26fSKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
282130fca26fSKuninori Morimoto 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
282230fca26fSKuninori Morimoto 
282330fca26fSKuninori Morimoto 	dpcm_be_disconnect(fe, stream);
282430fca26fSKuninori Morimoto 
282530fca26fSKuninori Morimoto 	fe->dpcm[stream].runtime = NULL;
2826265694b6SKuninori Morimoto }
2827265694b6SKuninori Morimoto 
2828265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2829265694b6SKuninori Morimoto {
2830265694b6SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2831265694b6SKuninori Morimoto 	int ret;
2832265694b6SKuninori Morimoto 
2833265694b6SKuninori Morimoto 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2834265694b6SKuninori Morimoto 	ret = dpcm_fe_dai_shutdown(fe_substream);
2835265694b6SKuninori Morimoto 
2836265694b6SKuninori Morimoto 	dpcm_fe_dai_cleanup(fe_substream);
2837265694b6SKuninori Morimoto 
283830fca26fSKuninori Morimoto 	mutex_unlock(&fe->card->mutex);
283930fca26fSKuninori Morimoto 	return ret;
284030fca26fSKuninori Morimoto }
284130fca26fSKuninori Morimoto 
284201d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
284301d7584cSLiam Girdwood {
284401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
284501d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
284601d7584cSLiam Girdwood 	int ret;
284701d7584cSLiam Girdwood 	int stream = fe_substream->stream;
284801d7584cSLiam Girdwood 
284901d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
285001d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
285101d7584cSLiam Girdwood 
28528f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
28538f70e515SQiao Zhou 	if (ret < 0) {
2854cae06eb9SKuninori Morimoto 		goto open_end;
28558f70e515SQiao Zhou 	} else if (ret == 0) {
2856103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
285701d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
285801d7584cSLiam Girdwood 	}
285901d7584cSLiam Girdwood 
286001d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
286101d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
286201d7584cSLiam Girdwood 
286301d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
2864265694b6SKuninori Morimoto 	if (ret < 0)
2865265694b6SKuninori Morimoto 		dpcm_fe_dai_cleanup(fe_substream);
286601d7584cSLiam Girdwood 
286701d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
286801d7584cSLiam Girdwood 	dpcm_path_put(&list);
2869cae06eb9SKuninori Morimoto open_end:
287001d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
287101d7584cSLiam Girdwood 	return ret;
287201d7584cSLiam Girdwood }
287301d7584cSLiam Girdwood 
2874ddee627cSLiam Girdwood /* create a new pcm */
2875ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2876ddee627cSLiam Girdwood {
28772e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
287819bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
28792b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
2880ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2881ddee627cSLiam Girdwood 	char new_name[64];
2882ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
28832e5894d7SBenoit Cousson 	int i;
2884ddee627cSLiam Girdwood 
288501d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
28869b5db059SStephan Gerhold 		cpu_dai = asoc_rtd_to_cpu(rtd, 0);
28879b5db059SStephan Gerhold 		if (rtd->num_cpus > 1) {
28889b5db059SStephan Gerhold 			dev_err(rtd->dev,
28899b5db059SStephan Gerhold 				"DPCM doesn't support Multi CPU yet\n");
28909b5db059SStephan Gerhold 			return -EINVAL;
28919b5db059SStephan Gerhold 		}
28929b5db059SStephan Gerhold 
28939b5db059SStephan Gerhold 		playback = rtd->dai_link->dpcm_playback &&
28949b5db059SStephan Gerhold 			   snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK);
28959b5db059SStephan Gerhold 		capture = rtd->dai_link->dpcm_capture &&
28969b5db059SStephan Gerhold 			  snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE);
289701d7584cSLiam Girdwood 	} else {
2898a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2899a4877a6fSStephan Gerhold 		int cpu_capture = rtd->dai_link->params ?
2900a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2901a4877a6fSStephan Gerhold 		int cpu_playback = rtd->dai_link->params ?
2902a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2903a342031cSJerome Brunet 
2904a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
290519bdcc7aSShreyas NC 			if (rtd->num_cpus == 1) {
2906c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, 0);
290719bdcc7aSShreyas NC 			} else if (rtd->num_cpus == rtd->num_codecs) {
2908c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, i);
290919bdcc7aSShreyas NC 			} else {
291019bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
291119bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
291219bdcc7aSShreyas NC 				return -EINVAL;
291319bdcc7aSShreyas NC 			}
291419bdcc7aSShreyas NC 
2915467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2916a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2917ddee627cSLiam Girdwood 				playback = 1;
2918467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2919a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2920ddee627cSLiam Girdwood 				capture = 1;
292101d7584cSLiam Girdwood 		}
29222e5894d7SBenoit Cousson 	}
29232e5894d7SBenoit Cousson 
2924d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2925d6bead02SFabio Estevam 		playback = 1;
2926d6bead02SFabio Estevam 		capture = 0;
2927d6bead02SFabio Estevam 	}
2928d6bead02SFabio Estevam 
2929d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2930d6bead02SFabio Estevam 		playback = 0;
2931d6bead02SFabio Estevam 		capture = 1;
2932d6bead02SFabio Estevam 	}
2933d6bead02SFabio Estevam 
293401d7584cSLiam Girdwood 	/* create the PCM */
2935a342031cSJerome Brunet 	if (rtd->dai_link->params) {
2936a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2937a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2938a342031cSJerome Brunet 
2939a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2940a342031cSJerome Brunet 					   playback, capture, &pcm);
2941a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
294201d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
294301d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
294401d7584cSLiam Girdwood 
294501d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
294601d7584cSLiam Girdwood 				playback, capture, &pcm);
294701d7584cSLiam Girdwood 	} else {
294801d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
294901d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
295001d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
295101d7584cSLiam Girdwood 		else
295201d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
29532e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
29542e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
2955c2233a26SKuninori Morimoto 				"multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
295601d7584cSLiam Girdwood 
295701d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
295801d7584cSLiam Girdwood 			capture, &pcm);
295901d7584cSLiam Girdwood 	}
2960ddee627cSLiam Girdwood 	if (ret < 0) {
2961103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
29625cb9b748SLiam Girdwood 			rtd->dai_link->name);
2963ddee627cSLiam Girdwood 		return ret;
2964ddee627cSLiam Girdwood 	}
2965103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2966ddee627cSLiam Girdwood 
2967ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2968a342031cSJerome Brunet 	if (rtd->dai_link->params)
29694bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2970a342031cSJerome Brunet 	else
297183f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2972ddee627cSLiam Girdwood 
297348c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2974ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2975ddee627cSLiam Girdwood 	pcm->private_data = rtd;
297601d7584cSLiam Girdwood 
2977a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
297801d7584cSLiam Girdwood 		if (playback)
297901d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
298001d7584cSLiam Girdwood 		if (capture)
298101d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
298201d7584cSLiam Girdwood 		goto out;
298301d7584cSLiam Girdwood 	}
298401d7584cSLiam Girdwood 
298501d7584cSLiam Girdwood 	/* ASoC PCM operations */
298601d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
298701d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
298801d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
298901d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
299001d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
299101d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
299201d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
299301d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
299401d7584cSLiam Girdwood 	} else {
299501d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
299601d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
299701d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
299801d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
299901d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
300001d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
300101d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
300201d7584cSLiam Girdwood 	}
300301d7584cSLiam Girdwood 
3004613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
30052b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
3006b8135864SKuninori Morimoto 
30073b1c952cSTakashi Iwai 		if (drv->ioctl)
30083b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
30091e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
30101e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
3011e9067bb5SKuninori Morimoto 		if (drv->copy_user)
301282d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
3013e9067bb5SKuninori Morimoto 		if (drv->page)
30149c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
3015e9067bb5SKuninori Morimoto 		if (drv->mmap)
3016205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
3017b8135864SKuninori Morimoto 	}
3018b8135864SKuninori Morimoto 
3019ddee627cSLiam Girdwood 	if (playback)
302001d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3021ddee627cSLiam Girdwood 
3022ddee627cSLiam Girdwood 	if (capture)
302301d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3024ddee627cSLiam Girdwood 
3025b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
3026ddee627cSLiam Girdwood 	if (ret < 0) {
30277484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3028ddee627cSLiam Girdwood 		return ret;
3029ddee627cSLiam Girdwood 	}
3030c641e5b2SJohan Hovold 
30313d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
303201d7584cSLiam Girdwood out:
30332e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3034c2233a26SKuninori Morimoto 		 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
3035c2233a26SKuninori Morimoto 		 (rtd->num_cpus > 1)   ? "multicpu"   : asoc_rtd_to_cpu(rtd, 0)->name);
3036ddee627cSLiam Girdwood 	return ret;
3037ddee627cSLiam Girdwood }
303801d7584cSLiam Girdwood 
303901d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
304001d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
304101d7584cSLiam Girdwood {
304201d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
304301d7584cSLiam Girdwood 		return 1;
304401d7584cSLiam Girdwood 	return 0;
304501d7584cSLiam Girdwood }
304601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
304701d7584cSLiam Girdwood 
304801d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
304901d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
305001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
305101d7584cSLiam Girdwood {
305201d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
305301d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
305401d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
305501d7584cSLiam Girdwood 		return 1;
305601d7584cSLiam Girdwood 	return 0;
305701d7584cSLiam Girdwood }
305801d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
305901d7584cSLiam Girdwood 
306001d7584cSLiam Girdwood /* get the substream for this BE */
306101d7584cSLiam Girdwood struct snd_pcm_substream *
306201d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
306301d7584cSLiam Girdwood {
306401d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
306501d7584cSLiam Girdwood }
306601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
306701d7584cSLiam Girdwood 
3068085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3069085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
3070085d22beSKuninori Morimoto 				    int stream,
3071085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
3072085d22beSKuninori Morimoto 				    int num_states)
307301d7584cSLiam Girdwood {
307401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
307501d7584cSLiam Girdwood 	int state;
3076a9764869SKaiChieh Chuang 	int ret = 1;
3077a9764869SKaiChieh Chuang 	unsigned long flags;
3078085d22beSKuninori Morimoto 	int i;
307901d7584cSLiam Girdwood 
3080a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3081d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
308201d7584cSLiam Girdwood 
308301d7584cSLiam Girdwood 		if (dpcm->fe == fe)
308401d7584cSLiam Girdwood 			continue;
308501d7584cSLiam Girdwood 
308601d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
3087085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
3088085d22beSKuninori Morimoto 			if (state == states[i]) {
3089a9764869SKaiChieh Chuang 				ret = 0;
3090a9764869SKaiChieh Chuang 				break;
309101d7584cSLiam Girdwood 			}
3092a9764869SKaiChieh Chuang 		}
3093085d22beSKuninori Morimoto 	}
3094a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
309501d7584cSLiam Girdwood 
3096085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
3097a9764869SKaiChieh Chuang 	return ret;
309801d7584cSLiam Girdwood }
3099085d22beSKuninori Morimoto 
3100085d22beSKuninori Morimoto /*
3101085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3102085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3103085d22beSKuninori Morimoto  */
3104085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3105085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3106085d22beSKuninori Morimoto {
3107085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3108085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3109085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3110085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3111085d22beSKuninori Morimoto 	};
3112085d22beSKuninori Morimoto 
3113085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3114085d22beSKuninori Morimoto }
311501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
311601d7584cSLiam Girdwood 
311701d7584cSLiam Girdwood /*
311801d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
311901d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
312001d7584cSLiam Girdwood  */
312101d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
312201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
312301d7584cSLiam Girdwood {
3124085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3125085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3126085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3127085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3128085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3129085d22beSKuninori Morimoto 	};
313001d7584cSLiam Girdwood 
3131085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
313201d7584cSLiam Girdwood }
313301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3134