xref: /openbmc/linux/sound/soc/soc-pcm.c (revision d9051d86ad30ca1ef1fc99e29854daeeb3944f8f)
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 
259*d9051d86SKuninori Morimoto /**
260*d9051d86SKuninori Morimoto  * snd_soc_runtime_action() - Increment/Decrement active count for
261*d9051d86SKuninori Morimoto  * PCM runtime components
262*d9051d86SKuninori Morimoto  * @rtd: ASoC PCM runtime that is activated
263*d9051d86SKuninori Morimoto  * @stream: Direction of the PCM stream
264*d9051d86SKuninori Morimoto  *
265*d9051d86SKuninori Morimoto  * Increments/Decrements the active count for all the DAIs and components
266*d9051d86SKuninori Morimoto  * attached to a PCM runtime.
267*d9051d86SKuninori Morimoto  * Should typically be called when a stream is opened.
268*d9051d86SKuninori Morimoto  *
269*d9051d86SKuninori Morimoto  * Must be called with the rtd->card->pcm_mutex being held
270*d9051d86SKuninori Morimoto  */
271*d9051d86SKuninori Morimoto void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2727a5aaba4SKuninori Morimoto 			    int stream, int action)
2737a5aaba4SKuninori Morimoto {
274c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
2757a5aaba4SKuninori Morimoto 	int i;
2767a5aaba4SKuninori Morimoto 
2777a5aaba4SKuninori Morimoto 	lockdep_assert_held(&rtd->card->pcm_mutex);
2787a5aaba4SKuninori Morimoto 
279c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
280c840f769SKuninori Morimoto 		dai->stream_active[stream] += action;
281c840f769SKuninori Morimoto 		dai->active += action;
282c840f769SKuninori Morimoto 		dai->component->active += action;
2837a5aaba4SKuninori Morimoto 	}
2847a5aaba4SKuninori Morimoto }
285*d9051d86SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
28624894b76SLars-Peter Clausen 
28724894b76SLars-Peter Clausen /**
288208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
289208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
290208a1589SLars-Peter Clausen  *
291208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
292208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
293208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
294208a1589SLars-Peter Clausen  * from having the delay.
295208a1589SLars-Peter Clausen  */
296208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
297208a1589SLars-Peter Clausen {
298fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
2992e5894d7SBenoit Cousson 	bool ignore = true;
300613fb500SKuninori Morimoto 	int i;
3012e5894d7SBenoit Cousson 
302208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
303208a1589SLars-Peter Clausen 		return true;
304208a1589SLars-Peter Clausen 
305613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
30672c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
307fbb16563SKuninori Morimoto 
308fbb16563SKuninori Morimoto 	return ignore;
309208a1589SLars-Peter Clausen }
310208a1589SLars-Peter Clausen 
311208a1589SLars-Peter Clausen /**
31290996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
31390996f43SLars-Peter Clausen  * @substream: the pcm substream
31490996f43SLars-Peter Clausen  * @hw: the hardware parameters
31590996f43SLars-Peter Clausen  *
31690996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
31790996f43SLars-Peter Clausen  */
31890996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
31990996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
32090996f43SLars-Peter Clausen {
32190996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
32290996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
32390996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
32490996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
32590996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
32690996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
32790996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
32890996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
32990996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
33090996f43SLars-Peter Clausen 	return 0;
33190996f43SLars-Peter Clausen }
33290996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
33390996f43SLars-Peter Clausen 
33401d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
33523607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
33601d7584cSLiam Girdwood 	int event)
33701d7584cSLiam Girdwood {
33801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
33901d7584cSLiam Girdwood 
3408d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
34101d7584cSLiam Girdwood 
34201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
34301d7584cSLiam Girdwood 
344103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
34501d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
34601d7584cSLiam Girdwood 
347b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
348b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
349b1cd2e34SBanajit Goswami 			continue;
350b1cd2e34SBanajit Goswami 
35101d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
35201d7584cSLiam Girdwood 	}
35301d7584cSLiam Girdwood 
35401d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
35501d7584cSLiam Girdwood 
35601d7584cSLiam Girdwood 	return 0;
35701d7584cSLiam Girdwood }
35801d7584cSLiam Girdwood 
35917841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
36017841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
361ddee627cSLiam Girdwood {
362ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
363ddee627cSLiam Girdwood 	int ret;
364ddee627cSLiam Girdwood 
3653635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
3663635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
3673635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
3683635bf09SNicolin Chen 				soc_dai->rate);
369ddee627cSLiam Girdwood 
3704dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
371ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
3724dcdd43bSLars-Peter Clausen 						soc_dai->rate);
373ddee627cSLiam Girdwood 		if (ret < 0) {
37417841020SDong Aisheng 			dev_err(soc_dai->dev,
3753635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
376103d84a3SLiam Girdwood 				ret);
377ddee627cSLiam Girdwood 			return ret;
378ddee627cSLiam Girdwood 		}
3793635bf09SNicolin Chen 	}
3803635bf09SNicolin Chen 
3813635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
3823635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
3833635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
3843635bf09SNicolin Chen 				soc_dai->channels);
3853635bf09SNicolin Chen 
3864dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
3873635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
3883635bf09SNicolin Chen 						soc_dai->channels);
3893635bf09SNicolin Chen 		if (ret < 0) {
3903635bf09SNicolin Chen 			dev_err(soc_dai->dev,
3913635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
3923635bf09SNicolin Chen 				ret);
3933635bf09SNicolin Chen 			return ret;
3943635bf09SNicolin Chen 		}
3953635bf09SNicolin Chen 	}
3963635bf09SNicolin Chen 
3973635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
3983635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
3993635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
4003635bf09SNicolin Chen 				soc_dai->sample_bits);
4013635bf09SNicolin Chen 
4024dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4033635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
4043635bf09SNicolin Chen 						soc_dai->sample_bits);
4053635bf09SNicolin Chen 		if (ret < 0) {
4063635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4073635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
4083635bf09SNicolin Chen 				ret);
4093635bf09SNicolin Chen 			return ret;
4103635bf09SNicolin Chen 		}
4113635bf09SNicolin Chen 	}
412ddee627cSLiam Girdwood 
413ddee627cSLiam Girdwood 	return 0;
414ddee627cSLiam Girdwood }
415ddee627cSLiam Girdwood 
4163635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4173635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4183635bf09SNicolin Chen {
4193635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
420c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
42119bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4222e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
4233635bf09SNicolin Chen 
4243635bf09SNicolin Chen 	rate = params_rate(params);
4253635bf09SNicolin Chen 	channels = params_channels(params);
4263635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
4273635bf09SNicolin Chen 
4283635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
42919bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_rates;
43019bdcc7aSShreyas NC 
431c840f769SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, dai)
432c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_rates;
4332e5894d7SBenoit Cousson 
43419bdcc7aSShreyas NC 	if (symmetry) {
435a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
43619bdcc7aSShreyas NC 			if (cpu_dai->rate && cpu_dai->rate != rate) {
4373635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
4383635bf09SNicolin Chen 					cpu_dai->rate, rate);
4393635bf09SNicolin Chen 				return -EINVAL;
4403635bf09SNicolin Chen 			}
44119bdcc7aSShreyas NC 		}
44219bdcc7aSShreyas NC 	}
4433635bf09SNicolin Chen 
44419bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_channels;
44519bdcc7aSShreyas NC 
446c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
447c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_channels;
4482e5894d7SBenoit Cousson 
44919bdcc7aSShreyas NC 	if (symmetry) {
450a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
45119bdcc7aSShreyas NC 			if (cpu_dai->channels &&
45219bdcc7aSShreyas NC 			    cpu_dai->channels != channels) {
4533635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
4543635bf09SNicolin Chen 					cpu_dai->channels, channels);
4553635bf09SNicolin Chen 				return -EINVAL;
4563635bf09SNicolin Chen 			}
45719bdcc7aSShreyas NC 		}
45819bdcc7aSShreyas NC 	}
4593635bf09SNicolin Chen 
46019bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_samplebits;
46119bdcc7aSShreyas NC 
462c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
463c840f769SKuninori Morimoto 		symmetry |= dai->driver->symmetric_samplebits;
4642e5894d7SBenoit Cousson 
46519bdcc7aSShreyas NC 	if (symmetry) {
466a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
46719bdcc7aSShreyas NC 			if (cpu_dai->sample_bits &&
46819bdcc7aSShreyas NC 			    cpu_dai->sample_bits != sample_bits) {
4693635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
4703635bf09SNicolin Chen 					cpu_dai->sample_bits, sample_bits);
4713635bf09SNicolin Chen 				return -EINVAL;
4723635bf09SNicolin Chen 			}
47319bdcc7aSShreyas NC 		}
47419bdcc7aSShreyas NC 	}
475ddee627cSLiam Girdwood 
476ddee627cSLiam Girdwood 	return 0;
477ddee627cSLiam Girdwood }
478ddee627cSLiam Girdwood 
47962e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
48062e5f676SLars-Peter Clausen {
48162e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
48262e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
483c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
4842e5894d7SBenoit Cousson 	unsigned int symmetry, i;
48562e5f676SLars-Peter Clausen 
48619bdcc7aSShreyas NC 	symmetry = link->symmetric_rates ||
48719bdcc7aSShreyas NC 		link->symmetric_channels ||
48819bdcc7aSShreyas NC 		link->symmetric_samplebits;
48919bdcc7aSShreyas NC 
490c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
49119bdcc7aSShreyas NC 		symmetry = symmetry ||
492c840f769SKuninori Morimoto 			dai->driver->symmetric_rates ||
493c840f769SKuninori Morimoto 			dai->driver->symmetric_channels ||
494c840f769SKuninori Morimoto 			dai->driver->symmetric_samplebits;
4952e5894d7SBenoit Cousson 
4962e5894d7SBenoit Cousson 	return symmetry;
49762e5f676SLars-Peter Clausen }
49862e5f676SLars-Peter Clausen 
4992e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
50058ba9b25SMark Brown {
5012e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
502c6068d3aSTakashi Iwai 	int ret;
50358ba9b25SMark Brown 
50458ba9b25SMark Brown 	if (!bits)
50558ba9b25SMark Brown 		return;
50658ba9b25SMark Brown 
5070e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
50858ba9b25SMark Brown 	if (ret != 0)
5090e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
5100e2a3751SLars-Peter Clausen 				 bits, ret);
51158ba9b25SMark Brown }
51258ba9b25SMark Brown 
513c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
514bd477c31SLars-Peter Clausen {
515c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
51619bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5172e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
51857be9206SKuninori Morimoto 	struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
51957be9206SKuninori Morimoto 	int stream = substream->stream;
5202e5894d7SBenoit Cousson 	int i;
52119bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
522c8dd1fecSBenoit Cousson 
523a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
52457be9206SKuninori Morimoto 		pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
52557be9206SKuninori Morimoto 
52657be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
5272e5894d7SBenoit Cousson 			bits = 0;
5282e5894d7SBenoit Cousson 			break;
5292e5894d7SBenoit Cousson 		}
53057be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
5312e5894d7SBenoit Cousson 	}
53257be9206SKuninori Morimoto 
533a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
53457be9206SKuninori Morimoto 		pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
53519bdcc7aSShreyas NC 
53619bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
53719bdcc7aSShreyas NC 			cpu_bits = 0;
53819bdcc7aSShreyas NC 			break;
53919bdcc7aSShreyas NC 		}
54019bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
54119bdcc7aSShreyas NC 	}
542c8dd1fecSBenoit Cousson 
5432e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5442e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
545c8dd1fecSBenoit Cousson }
546c8dd1fecSBenoit Cousson 
5475854a464SSamuel Holland /**
5485854a464SSamuel Holland  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
5495854a464SSamuel Holland  * @rtd: ASoC PCM runtime
5505854a464SSamuel Holland  * @hw: PCM hardware parameters (output)
5515854a464SSamuel Holland  * @stream: Direction of the PCM stream
5525854a464SSamuel Holland  *
5535854a464SSamuel Holland  * Calculates the subset of stream parameters supported by all DAIs
5545854a464SSamuel Holland  * associated with the PCM stream.
5555854a464SSamuel Holland  */
5565854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
5575854a464SSamuel Holland 			    struct snd_pcm_hardware *hw, int stream)
558bd477c31SLars-Peter Clausen {
5590b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
56019bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5612e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5622e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
5632e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
56419bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5652e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
56619bdcc7aSShreyas NC 	unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
56719bdcc7aSShreyas NC 	unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
5682e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
5692e5894d7SBenoit Cousson 	int i;
57078e45c99SLars-Peter Clausen 
57119bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
572a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5730e9cf4c4SBard Liao 
5740e9cf4c4SBard Liao 		/*
5750e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream type.
5760e9cf4c4SBard Liao 		 * Otherwise, since the rate, channel, and format values will
5770e9cf4c4SBard Liao 		 * zero in that case, we would have no usable settings left,
5780e9cf4c4SBard Liao 		 * causing the resulting setup to fail.
5790e9cf4c4SBard Liao 		 */
5805854a464SSamuel Holland 		if (!snd_soc_dai_stream_valid(cpu_dai, stream))
5810e9cf4c4SBard Liao 			continue;
5820e9cf4c4SBard Liao 
58319bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
58478e45c99SLars-Peter Clausen 
58519bdcc7aSShreyas NC 		cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
58619bdcc7aSShreyas NC 		cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
58719bdcc7aSShreyas NC 		cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
58819bdcc7aSShreyas NC 		cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
58919bdcc7aSShreyas NC 		formats &= cpu_stream->formats;
59019bdcc7aSShreyas NC 		cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
59119bdcc7aSShreyas NC 							cpu_rates);
59219bdcc7aSShreyas NC 	}
59319bdcc7aSShreyas NC 
59419bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
595a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
596cde79035SRicard Wanderlof 
597cde79035SRicard Wanderlof 		/*
598cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
599cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
600cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
601cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
602cde79035SRicard Wanderlof 		 */
60325c2f515SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
604cde79035SRicard Wanderlof 			continue;
605cde79035SRicard Wanderlof 
606acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
607acf253c1SKuninori Morimoto 
6082e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
6092e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
6102e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
6112e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
6122e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
6132e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
6142e5894d7SBenoit Cousson 	}
6152e5894d7SBenoit Cousson 
6165854a464SSamuel Holland 	/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6175854a464SSamuel Holland 	if (!chan_min || !cpu_chan_min)
6185854a464SSamuel Holland 		return -EINVAL;
6195854a464SSamuel Holland 
6202e5894d7SBenoit Cousson 	/*
6212e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
62219bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6232e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6242e5894d7SBenoit Cousson 	 */
6252e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
62619bdcc7aSShreyas NC 		chan_min = cpu_chan_min;
62719bdcc7aSShreyas NC 		chan_max = cpu_chan_max;
6282e5894d7SBenoit Cousson 	}
6292e5894d7SBenoit Cousson 
63019bdcc7aSShreyas NC 	/* finally find a intersection between CODECs and CPUs */
63119bdcc7aSShreyas NC 	hw->channels_min = max(chan_min, cpu_chan_min);
63219bdcc7aSShreyas NC 	hw->channels_max = min(chan_max, cpu_chan_max);
63319bdcc7aSShreyas NC 	hw->formats = formats;
63419bdcc7aSShreyas NC 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
63578e45c99SLars-Peter Clausen 
6365854a464SSamuel Holland 	snd_pcm_hw_limit_rates(hw);
63778e45c99SLars-Peter Clausen 
63819bdcc7aSShreyas NC 	hw->rate_min = max(hw->rate_min, cpu_rate_min);
6392e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
64019bdcc7aSShreyas NC 	hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
6412e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
6425854a464SSamuel Holland 
6435854a464SSamuel Holland 	return 0;
6445854a464SSamuel Holland }
6455854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
6465854a464SSamuel Holland 
6475854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
6485854a464SSamuel Holland {
6495854a464SSamuel Holland 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
6505854a464SSamuel Holland 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
6515854a464SSamuel Holland 	u64 formats = hw->formats;
6525854a464SSamuel Holland 
6535854a464SSamuel Holland 	/*
6545854a464SSamuel Holland 	 * At least one CPU and one CODEC should match. Otherwise, we should
6555854a464SSamuel Holland 	 * have bailed out on a higher level, since there would be no CPU or
6565854a464SSamuel Holland 	 * CODEC to support the transfer direction in that case.
6575854a464SSamuel Holland 	 */
6585854a464SSamuel Holland 	snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
6595854a464SSamuel Holland 
6605854a464SSamuel Holland 	if (formats)
6615854a464SSamuel Holland 		hw->formats &= formats;
662bd477c31SLars-Peter Clausen }
663bd477c31SLars-Peter Clausen 
664dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
665e7ecfdb7SKuninori Morimoto {
666e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
667d2aaa8d8SKai Vehmanen 	struct snd_soc_component *last = NULL;
668e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
669613fb500SKuninori Morimoto 	int i, ret = 0;
670e7ecfdb7SKuninori Morimoto 
671613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
672d2aaa8d8SKai Vehmanen 		last = component;
673d2aaa8d8SKai Vehmanen 
6744a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
6754a81e8f3SKuninori Morimoto 		if (ret < 0) {
676e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
677e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
678e7ecfdb7SKuninori Morimoto 				component->name);
679d2aaa8d8SKai Vehmanen 			break;
680e7ecfdb7SKuninori Morimoto 		}
681e7ecfdb7SKuninori Morimoto 
682ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
683e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
684d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
685e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
686e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
687e7ecfdb7SKuninori Morimoto 				component->name, ret);
688d2aaa8d8SKai Vehmanen 			break;
689e7ecfdb7SKuninori Morimoto 		}
690e7ecfdb7SKuninori Morimoto 	}
691dd03907bSKuninori Morimoto 
692d2aaa8d8SKai Vehmanen 	if (ret < 0) {
693d2aaa8d8SKai Vehmanen 		/* rollback on error */
694d2aaa8d8SKai Vehmanen 		for_each_rtd_components(rtd, i, component) {
695d2aaa8d8SKai Vehmanen 			if (component == last)
696d2aaa8d8SKai Vehmanen 				break;
697d2aaa8d8SKai Vehmanen 
698d2aaa8d8SKai Vehmanen 			snd_soc_component_close(component, substream);
699d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
700d2aaa8d8SKai Vehmanen 		}
701d2aaa8d8SKai Vehmanen 	}
702d2aaa8d8SKai Vehmanen 
703d2aaa8d8SKai Vehmanen 	return ret;
704e7ecfdb7SKuninori Morimoto }
705e7ecfdb7SKuninori Morimoto 
706dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream)
707244e2936SCharles Keepax {
708244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
709244e2936SCharles Keepax 	struct snd_soc_component *component;
710e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
711244e2936SCharles Keepax 
712613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
713e82ebffcSKuninori Morimoto 		r = snd_soc_component_close(component, substream);
714e82ebffcSKuninori Morimoto 		if (r < 0)
715e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
716e82ebffcSKuninori Morimoto 
7174a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
718244e2936SCharles Keepax 	}
719244e2936SCharles Keepax 
7203672beb8SKuninori Morimoto 	return ret;
721244e2936SCharles Keepax }
722244e2936SCharles Keepax 
72358ba9b25SMark Brown /*
72462c86d1dSKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
72562c86d1dSKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
72662c86d1dSKuninori Morimoto  * shutdown.
72762c86d1dSKuninori Morimoto  */
72862c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
72962c86d1dSKuninori Morimoto {
73062c86d1dSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
73162c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
732c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
73362c86d1dSKuninori Morimoto 	int i;
73462c86d1dSKuninori Morimoto 
73562c86d1dSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
73662c86d1dSKuninori Morimoto 
73762c86d1dSKuninori Morimoto 	snd_soc_runtime_deactivate(rtd, substream->stream);
73862c86d1dSKuninori Morimoto 
739c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
740c840f769SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream);
74162c86d1dSKuninori Morimoto 
74262c86d1dSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
74362c86d1dSKuninori Morimoto 
74462c86d1dSKuninori Morimoto 	soc_pcm_components_close(substream);
74562c86d1dSKuninori Morimoto 
74662c86d1dSKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
74762c86d1dSKuninori Morimoto 
74862c86d1dSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
74962c86d1dSKuninori Morimoto 
75062c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
75162c86d1dSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
75262c86d1dSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
75362c86d1dSKuninori Morimoto 	}
75462c86d1dSKuninori Morimoto 
75562c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
75662c86d1dSKuninori Morimoto 		if (!component->active)
75762c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
75862c86d1dSKuninori Morimoto 
75962c86d1dSKuninori Morimoto 	return 0;
76062c86d1dSKuninori Morimoto }
76162c86d1dSKuninori Morimoto 
76262c86d1dSKuninori Morimoto /*
763ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
764ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
765ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
766ddee627cSLiam Girdwood  */
767ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
768ddee627cSLiam Girdwood {
769ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
770ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
77190be711eSKuninori Morimoto 	struct snd_soc_component *component;
772c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
7732e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
77419bdcc7aSShreyas NC 	const char *cpu_dai_name = "multicpu";
775244e2936SCharles Keepax 	int i, ret = 0;
776ddee627cSLiam Girdwood 
77776c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
77876c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
77990be711eSKuninori Morimoto 
780613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
78190be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
782d6652ef8SMark Brown 
78372b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
784ddee627cSLiam Girdwood 
7855d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
7865d9fa03eSKuninori Morimoto 	if (ret < 0)
7875d9fa03eSKuninori Morimoto 		goto component_err;
7885d9fa03eSKuninori Morimoto 
7895d9fa03eSKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
7905d9fa03eSKuninori Morimoto 	if (ret < 0) {
7915d9fa03eSKuninori Morimoto 		pr_err("ASoC: %s startup failed: %d\n",
7925d9fa03eSKuninori Morimoto 		       rtd->dai_link->name, ret);
793d2aaa8d8SKai Vehmanen 		goto rtd_startup_err;
7945d9fa03eSKuninori Morimoto 	}
7955d9fa03eSKuninori Morimoto 
796ddee627cSLiam Girdwood 	/* startup the audio subsystem */
797c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
798c840f769SKuninori Morimoto 		ret = snd_soc_dai_startup(dai, substream);
799ddee627cSLiam Girdwood 		if (ret < 0) {
800c840f769SKuninori Morimoto 			dev_err(dai->dev,
801c840f769SKuninori Morimoto 				"ASoC: can't open DAI %s: %d\n",
802c840f769SKuninori Morimoto 				dai->name, ret);
8035d9fa03eSKuninori Morimoto 			goto config_err;
804ddee627cSLiam Girdwood 		}
805ddee627cSLiam Girdwood 
8062e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
807c840f769SKuninori Morimoto 			dai->tx_mask = 0;
8082e5894d7SBenoit Cousson 		else
809c840f769SKuninori Morimoto 			dai->rx_mask = 0;
8102e5894d7SBenoit Cousson 	}
8112e5894d7SBenoit Cousson 
81201d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
81301d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
81401d7584cSLiam Girdwood 		goto dynamic;
81501d7584cSLiam Girdwood 
816ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8172e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8182e5894d7SBenoit Cousson 
8192e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
820c2233a26SKuninori Morimoto 		codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
821ddee627cSLiam Girdwood 
82219bdcc7aSShreyas NC 	if (rtd->num_cpus == 1)
823c2233a26SKuninori Morimoto 		cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
82419bdcc7aSShreyas NC 
82562e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
82662e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
82762e5f676SLars-Peter Clausen 
828ddee627cSLiam Girdwood 	ret = -EINVAL;
829ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
830103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
83119bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
832ddee627cSLiam Girdwood 		goto config_err;
833ddee627cSLiam Girdwood 	}
834ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
835103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
83619bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
837ddee627cSLiam Girdwood 		goto config_err;
838ddee627cSLiam Girdwood 	}
839ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
840ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
841103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
84219bdcc7aSShreyas NC 				codec_dai_name, cpu_dai_name);
843ddee627cSLiam Girdwood 		goto config_err;
844ddee627cSLiam Girdwood 	}
845ddee627cSLiam Girdwood 
846c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
84758ba9b25SMark Brown 
848ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
849c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
850c840f769SKuninori Morimoto 		if (dai->active) {
851c840f769SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, dai);
852ddee627cSLiam Girdwood 			if (ret != 0)
853ddee627cSLiam Girdwood 				goto config_err;
854ddee627cSLiam Girdwood 		}
8552e5894d7SBenoit Cousson 	}
856ddee627cSLiam Girdwood 
857103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
85819bdcc7aSShreyas NC 		 codec_dai_name, cpu_dai_name);
859103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
860103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
861ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
862103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
863ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
864ddee627cSLiam Girdwood 
86501d7584cSLiam Girdwood dynamic:
86624894b76SLars-Peter Clausen 
86724894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
86824894b76SLars-Peter Clausen 
86972b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
870ddee627cSLiam Girdwood 	return 0;
871ddee627cSLiam Girdwood 
872ddee627cSLiam Girdwood config_err:
873c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
874c840f769SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream);
8752e5894d7SBenoit Cousson 
8765d9fa03eSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
877d2aaa8d8SKai Vehmanen rtd_startup_err:
878dd03907bSKuninori Morimoto 	soc_pcm_components_close(substream);
879d2aaa8d8SKai Vehmanen component_err:
88072b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
881d6652ef8SMark Brown 
882613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
88390be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
88490be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
8853f809783SSanyog Kale 	}
8863f809783SSanyog Kale 
88776c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
88876c39e86SKuninori Morimoto 		if (!component->active)
88976c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
890d6652ef8SMark Brown 
891ddee627cSLiam Girdwood 	return ret;
892ddee627cSLiam Girdwood }
893ddee627cSLiam Girdwood 
8944bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
895a342031cSJerome Brunet {
896a342031cSJerome Brunet 	/*
897a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
898a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
899a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
900a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
901a342031cSJerome Brunet 	 */
902a342031cSJerome Brunet }
903a342031cSJerome Brunet 
904ddee627cSLiam Girdwood /*
905ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
906ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
907ddee627cSLiam Girdwood  * it can refer to the runtime info.
908ddee627cSLiam Girdwood  */
909ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
910ddee627cSLiam Girdwood {
911ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
912b8135864SKuninori Morimoto 	struct snd_soc_component *component;
913c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
9142e5894d7SBenoit Cousson 	int i, ret = 0;
915ddee627cSLiam Girdwood 
91672b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
917ddee627cSLiam Girdwood 
91844c1a75bSKuninori Morimoto 	ret = soc_rtd_prepare(rtd, substream);
919ddee627cSLiam Girdwood 	if (ret < 0) {
92044c1a75bSKuninori Morimoto 		dev_err(rtd->card->dev,
92144c1a75bSKuninori Morimoto 			"ASoC: machine prepare error: %d\n", ret);
922ddee627cSLiam Girdwood 		goto out;
923ddee627cSLiam Girdwood 	}
924ddee627cSLiam Girdwood 
925613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
9266d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
927b8135864SKuninori Morimoto 		if (ret < 0) {
928b8135864SKuninori Morimoto 			dev_err(component->dev,
929b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
930b8135864SKuninori Morimoto 			goto out;
931b8135864SKuninori Morimoto 		}
932b8135864SKuninori Morimoto 	}
933b8135864SKuninori Morimoto 
934d108c7fdSKuninori Morimoto 	ret = snd_soc_pcm_dai_prepare(substream);
935ddee627cSLiam Girdwood 	if (ret < 0) {
936d108c7fdSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
937ddee627cSLiam Girdwood 		goto out;
938ddee627cSLiam Girdwood 	}
939ddee627cSLiam Girdwood 
940ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
941ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9429bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
9439bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
944ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
945ddee627cSLiam Girdwood 	}
946ddee627cSLiam Girdwood 
947d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
948ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
949ddee627cSLiam Girdwood 
950c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
951c840f769SKuninori Morimoto 		snd_soc_dai_digital_mute(dai, 0, substream->stream);
952ddee627cSLiam Girdwood 
953ddee627cSLiam Girdwood out:
95472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
955ddee627cSLiam Girdwood 	return ret;
956ddee627cSLiam Girdwood }
957ddee627cSLiam Girdwood 
9582e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
9592e5894d7SBenoit Cousson 				       unsigned int mask)
9602e5894d7SBenoit Cousson {
9612e5894d7SBenoit Cousson 	struct snd_interval *interval;
9622e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
9632e5894d7SBenoit Cousson 
9642e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
9652e5894d7SBenoit Cousson 	interval->min = channels;
9662e5894d7SBenoit Cousson 	interval->max = channels;
9672e5894d7SBenoit Cousson }
9682e5894d7SBenoit Cousson 
969244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
970244e2936SCharles Keepax 				      struct snd_soc_component *last)
971244e2936SCharles Keepax {
972244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
973244e2936SCharles Keepax 	struct snd_soc_component *component;
974e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
975244e2936SCharles Keepax 
976613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
977244e2936SCharles Keepax 		if (component == last)
978244e2936SCharles Keepax 			break;
979244e2936SCharles Keepax 
980e82ebffcSKuninori Morimoto 		r = snd_soc_component_hw_free(component, substream);
981e82ebffcSKuninori Morimoto 		if (r < 0)
982e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
983244e2936SCharles Keepax 	}
984244e2936SCharles Keepax 
985eae7136aSKuninori Morimoto 	return ret;
986244e2936SCharles Keepax }
987244e2936SCharles Keepax 
988ddee627cSLiam Girdwood /*
989ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
990ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
991ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
992ddee627cSLiam Girdwood  */
993ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
994ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
995ddee627cSLiam Girdwood {
996ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
997b8135864SKuninori Morimoto 	struct snd_soc_component *component;
99819bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
9990b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1000244e2936SCharles Keepax 	int i, ret = 0;
1001ddee627cSLiam Girdwood 
100272b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
10035cca5951SShengjiu Wang 
10045cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
10055cca5951SShengjiu Wang 	if (ret)
10065cca5951SShengjiu Wang 		goto out;
10075cca5951SShengjiu Wang 
1008de9ad990SKuninori Morimoto 	ret = soc_rtd_hw_params(rtd, substream, params);
1009ddee627cSLiam Girdwood 	if (ret < 0) {
1010de9ad990SKuninori Morimoto 		dev_err(rtd->card->dev,
1011de9ad990SKuninori Morimoto 			"ASoC: machine hw_params failed: %d\n", ret);
1012ddee627cSLiam Girdwood 		goto out;
1013ddee627cSLiam Girdwood 	}
1014ddee627cSLiam Girdwood 
1015a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
10162e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
10172e5894d7SBenoit Cousson 
1018cde79035SRicard Wanderlof 		/*
1019cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1020cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1021cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1022cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1023cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1024cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1025cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1026cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1027cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1028cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1029cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1030cde79035SRicard Wanderlof 		 * we can do the right thing here.
1031cde79035SRicard Wanderlof 		 */
1032cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1033cde79035SRicard Wanderlof 			continue;
1034cde79035SRicard Wanderlof 
10352e5894d7SBenoit Cousson 		/* copy params for each codec */
10362e5894d7SBenoit Cousson 		codec_params = *params;
10372e5894d7SBenoit Cousson 
10382e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1039570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1040570f18b6SRander Wang 		    codec_dai->tx_mask)
10412e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10422e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
1043570f18b6SRander Wang 
1044570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1045570f18b6SRander Wang 		    codec_dai->rx_mask)
10462e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10472e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
10482e5894d7SBenoit Cousson 
1049aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1050aa6166c2SKuninori Morimoto 					    &codec_params);
105193e6958aSBenoit Cousson 		if(ret < 0)
1052ddee627cSLiam Girdwood 			goto codec_err;
1053ddee627cSLiam Girdwood 
10542e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
10552e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
10562e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
10572e5894d7SBenoit Cousson 						params_format(&codec_params));
1058078a85f2SCharles Keepax 
1059078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1060ddee627cSLiam Girdwood 	}
1061ddee627cSLiam Girdwood 
1062a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
10630e9cf4c4SBard Liao 		/*
10640e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
10650e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
10660e9cf4c4SBard Liao 		 */
10670e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
10680e9cf4c4SBard Liao 			continue;
10690e9cf4c4SBard Liao 
1070aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
107193e6958aSBenoit Cousson 		if (ret < 0)
1072ddee627cSLiam Girdwood 			goto interface_err;
1073ddee627cSLiam Girdwood 
107419bdcc7aSShreyas NC 		/* store the parameters for each DAI */
1075ca58221dSKuninori Morimoto 		cpu_dai->rate = params_rate(params);
1076ca58221dSKuninori Morimoto 		cpu_dai->channels = params_channels(params);
1077ca58221dSKuninori Morimoto 		cpu_dai->sample_bits =
1078ca58221dSKuninori Morimoto 			snd_pcm_format_physical_width(params_format(params));
1079ca58221dSKuninori Morimoto 
1080ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
108119bdcc7aSShreyas NC 	}
1082ca58221dSKuninori Morimoto 
1083613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1084245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
1085244e2936SCharles Keepax 		if (ret < 0) {
1086b8135864SKuninori Morimoto 			dev_err(component->dev,
1087b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
1088244e2936SCharles Keepax 				component->name, ret);
1089b8135864SKuninori Morimoto 			goto component_err;
1090244e2936SCharles Keepax 		}
1091244e2936SCharles Keepax 	}
1092244e2936SCharles Keepax 	component = NULL;
1093b8135864SKuninori Morimoto 
1094ddee627cSLiam Girdwood out:
109572b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1096ddee627cSLiam Girdwood 	return ret;
1097ddee627cSLiam Girdwood 
1098b8135864SKuninori Morimoto component_err:
1099244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
1100b8135864SKuninori Morimoto 
110119bdcc7aSShreyas NC 	i = rtd->num_cpus;
1102ddee627cSLiam Girdwood 
1103ddee627cSLiam Girdwood interface_err:
1104a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
11050e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
11060e9cf4c4SBard Liao 			continue;
11070e9cf4c4SBard Liao 
110819bdcc7aSShreyas NC 		snd_soc_dai_hw_free(cpu_dai, substream);
110919bdcc7aSShreyas NC 		cpu_dai->rate = 0;
111019bdcc7aSShreyas NC 	}
111119bdcc7aSShreyas NC 
11122e5894d7SBenoit Cousson 	i = rtd->num_codecs;
1113ddee627cSLiam Girdwood 
1114ddee627cSLiam Girdwood codec_err:
1115a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
1116f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1117f47b9ad9SJerome Brunet 			continue;
1118f47b9ad9SJerome Brunet 
1119846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
11202e5894d7SBenoit Cousson 		codec_dai->rate = 0;
11212e5894d7SBenoit Cousson 	}
11222e5894d7SBenoit Cousson 
112349f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1124ddee627cSLiam Girdwood 
112572b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1126ddee627cSLiam Girdwood 	return ret;
1127ddee627cSLiam Girdwood }
1128ddee627cSLiam Girdwood 
1129ddee627cSLiam Girdwood /*
1130ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
1131ddee627cSLiam Girdwood  */
1132ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1133ddee627cSLiam Girdwood {
1134ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1135c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
11362e5894d7SBenoit Cousson 	int i;
1137ddee627cSLiam Girdwood 
113872b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1139ddee627cSLiam Girdwood 
1140d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
1141c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
1142c840f769SKuninori Morimoto 		int active = dai->stream_active[substream->stream];
1143d3383420SNicolin Chen 
1144c840f769SKuninori Morimoto 		if (dai->active == 1) {
1145c840f769SKuninori Morimoto 			dai->rate = 0;
1146c840f769SKuninori Morimoto 			dai->channels = 0;
1147c840f769SKuninori Morimoto 			dai->sample_bits = 0;
1148d3383420SNicolin Chen 		}
11490f6011fdSKuninori Morimoto 
115067ad8777SKuninori Morimoto 		if (active == 1)
1151c840f769SKuninori Morimoto 			snd_soc_dai_digital_mute(dai, 1, substream->stream);
1152a9ee331bSKuninori Morimoto 	}
1153a9ee331bSKuninori Morimoto 
1154ddee627cSLiam Girdwood 	/* free any machine hw params */
115549f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1156ddee627cSLiam Girdwood 
1157b8135864SKuninori Morimoto 	/* free any component resources */
1158244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
1159b8135864SKuninori Morimoto 
1160ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
1161c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
1162c840f769SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(dai, substream->stream))
1163f47b9ad9SJerome Brunet 			continue;
1164f47b9ad9SJerome Brunet 
1165c840f769SKuninori Morimoto 		snd_soc_dai_hw_free(dai, substream);
11660e9cf4c4SBard Liao 	}
1167ddee627cSLiam Girdwood 
116872b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1169ddee627cSLiam Girdwood 	return 0;
1170ddee627cSLiam Girdwood }
1171ddee627cSLiam Girdwood 
11724378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1173ddee627cSLiam Girdwood {
1174ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1175b8135864SKuninori Morimoto 	struct snd_soc_component *component;
11762e5894d7SBenoit Cousson 	int i, ret;
1177ddee627cSLiam Girdwood 
1178ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
1179ddee627cSLiam Girdwood 	if (ret < 0)
1180ddee627cSLiam Girdwood 		return ret;
1181ddee627cSLiam Girdwood 
1182613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
11835693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1184b8135864SKuninori Morimoto 		if (ret < 0)
1185b8135864SKuninori Morimoto 			return ret;
1186b8135864SKuninori Morimoto 	}
1187b8135864SKuninori Morimoto 
118842f2472dSKuninori Morimoto 	return snd_soc_pcm_dai_trigger(substream, cmd);
11894378f1fbSPeter Ujfalusi }
11904378f1fbSPeter Ujfalusi 
11914378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
11924378f1fbSPeter Ujfalusi {
11934378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
11944378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
11954378f1fbSPeter Ujfalusi 	int i, ret;
11964378f1fbSPeter Ujfalusi 
119742f2472dSKuninori Morimoto 	ret = snd_soc_pcm_dai_trigger(substream, cmd);
11984378f1fbSPeter Ujfalusi 	if (ret < 0)
11994378f1fbSPeter Ujfalusi 		return ret;
12004378f1fbSPeter Ujfalusi 
1201613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12024378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
12034378f1fbSPeter Ujfalusi 		if (ret < 0)
12044378f1fbSPeter Ujfalusi 			return ret;
12054378f1fbSPeter Ujfalusi 	}
12064378f1fbSPeter Ujfalusi 
1207ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
12084792b0dbSJarkko Nikula 	if (ret < 0)
12094792b0dbSJarkko Nikula 		return ret;
12104792b0dbSJarkko Nikula 
1211ddee627cSLiam Girdwood 	return 0;
1212ddee627cSLiam Girdwood }
1213ddee627cSLiam Girdwood 
12144378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
12154378f1fbSPeter Ujfalusi {
12164378f1fbSPeter Ujfalusi 	int ret;
12174378f1fbSPeter Ujfalusi 
12184378f1fbSPeter Ujfalusi 	switch (cmd) {
12194378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
12204378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
12214378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
12224378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
12234378f1fbSPeter Ujfalusi 		break;
12244378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
12254378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
12264378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
12274378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
12284378f1fbSPeter Ujfalusi 		break;
12294378f1fbSPeter Ujfalusi 	default:
12304378f1fbSPeter Ujfalusi 		return -EINVAL;
12314378f1fbSPeter Ujfalusi 	}
12324378f1fbSPeter Ujfalusi 
12334378f1fbSPeter Ujfalusi 	return ret;
12344378f1fbSPeter Ujfalusi }
12354378f1fbSPeter Ujfalusi 
1236ddee627cSLiam Girdwood /*
1237ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1238ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1239ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1240ddee627cSLiam Girdwood  */
1241ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1242ddee627cSLiam Girdwood {
1243ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
124419bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12452e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1246ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1247ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1248ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
12492e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
125019bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
12512e5894d7SBenoit Cousson 	int i;
1252ddee627cSLiam Girdwood 
12539fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
12549fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
12559fb4c2bfSAkshu Agrawal 
12560035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1257b8135864SKuninori Morimoto 
12589fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
12599fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1260b8135864SKuninori Morimoto 
1261a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
126219bdcc7aSShreyas NC 		cpu_delay = max(cpu_delay,
126319bdcc7aSShreyas NC 				snd_soc_dai_delay(cpu_dai, substream));
126419bdcc7aSShreyas NC 	}
126519bdcc7aSShreyas NC 	delay += cpu_delay;
1266ddee627cSLiam Girdwood 
1267a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
12682e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
12691dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
12702e5894d7SBenoit Cousson 	}
12712e5894d7SBenoit Cousson 	delay += codec_delay;
1272ddee627cSLiam Girdwood 
1273ddee627cSLiam Girdwood 	runtime->delay = delay;
1274ddee627cSLiam Girdwood 
1275ddee627cSLiam Girdwood 	return offset;
1276ddee627cSLiam Girdwood }
1277ddee627cSLiam Girdwood 
127801d7584cSLiam Girdwood /* connect a FE and BE */
127901d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
128001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
128101d7584cSLiam Girdwood {
128201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1283a9764869SKaiChieh Chuang 	unsigned long flags;
128401d7584cSLiam Girdwood 
128501d7584cSLiam Girdwood 	/* only add new dpcms */
12868d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
128701d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
128801d7584cSLiam Girdwood 			return 0;
128901d7584cSLiam Girdwood 	}
129001d7584cSLiam Girdwood 
129101d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
129201d7584cSLiam Girdwood 	if (!dpcm)
129301d7584cSLiam Girdwood 		return -ENOMEM;
129401d7584cSLiam Girdwood 
129501d7584cSLiam Girdwood 	dpcm->be = be;
129601d7584cSLiam Girdwood 	dpcm->fe = fe;
129701d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
129801d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1299a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
130001d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
130101d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1302a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
130301d7584cSLiam Girdwood 
130401d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
130501d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
130601d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
130701d7584cSLiam Girdwood 
1308154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1309154dae87SKuninori Morimoto 
131001d7584cSLiam Girdwood 	return 1;
131101d7584cSLiam Girdwood }
131201d7584cSLiam Girdwood 
131301d7584cSLiam Girdwood /* reparent a BE onto another FE */
131401d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
131501d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
131601d7584cSLiam Girdwood {
131701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
131801d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
131901d7584cSLiam Girdwood 
132001d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
132101d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
132201d7584cSLiam Girdwood 		return;
132301d7584cSLiam Girdwood 
132401d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
132501d7584cSLiam Girdwood 
1326d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
132701d7584cSLiam Girdwood 		if (dpcm->fe == fe)
132801d7584cSLiam Girdwood 			continue;
132901d7584cSLiam Girdwood 
133001d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
133101d7584cSLiam Girdwood 			stream ? "capture" : "playback",
133201d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
133301d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
133401d7584cSLiam Girdwood 
133501d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
133601d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
133701d7584cSLiam Girdwood 		break;
133801d7584cSLiam Girdwood 	}
133901d7584cSLiam Girdwood }
134001d7584cSLiam Girdwood 
134101d7584cSLiam Girdwood /* disconnect a BE and FE */
134223607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
134301d7584cSLiam Girdwood {
134401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1345a9764869SKaiChieh Chuang 	unsigned long flags;
134601d7584cSLiam Girdwood 
13478d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1348103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
134901d7584cSLiam Girdwood 				stream ? "capture" : "playback",
135001d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
135101d7584cSLiam Girdwood 
135201d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
135301d7584cSLiam Girdwood 			continue;
135401d7584cSLiam Girdwood 
135501d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
135601d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
135701d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
135801d7584cSLiam Girdwood 
135901d7584cSLiam Girdwood 		/* BEs still alive need new FE */
136001d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
136101d7584cSLiam Girdwood 
1362154dae87SKuninori Morimoto 		dpcm_remove_debugfs_state(dpcm);
1363154dae87SKuninori Morimoto 
1364a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
136501d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
136601d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1367a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
136801d7584cSLiam Girdwood 		kfree(dpcm);
136901d7584cSLiam Girdwood 	}
137001d7584cSLiam Girdwood }
137101d7584cSLiam Girdwood 
137201d7584cSLiam Girdwood /* get BE for DAI widget and stream */
137301d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
137401d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
137501d7584cSLiam Girdwood {
137601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
137793597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
13787afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
13791a497983SMengdong Lin 	int i;
138001d7584cSLiam Girdwood 
13813c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
13823c146465SLiam Girdwood 
1383bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
138401d7584cSLiam Girdwood 
138535ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
138635ea0655SLiam Girdwood 			continue;
138735ea0655SLiam Girdwood 
1388c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
138919bdcc7aSShreyas NC 			w = snd_soc_dai_get_widget(dai, stream);
139093597faeSKuninori Morimoto 
13913c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
139293597faeSKuninori Morimoto 				w ? w->name : "(not set)");
13933c146465SLiam Girdwood 
139493597faeSKuninori Morimoto 			if (w == widget)
139501d7584cSLiam Girdwood 				return be;
139619bdcc7aSShreyas NC 		}
139701d7584cSLiam Girdwood 	}
139801d7584cSLiam Girdwood 
13999d6ee365SJerome Brunet 	/* Widget provided is not a BE */
140001d7584cSLiam Girdwood 	return NULL;
140101d7584cSLiam Girdwood }
140201d7584cSLiam Girdwood 
140301d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
140401d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
140501d7584cSLiam Girdwood {
140609e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
140701d7584cSLiam Girdwood 	int i;
140801d7584cSLiam Girdwood 
140909e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
141009e88f8aSKuninori Morimoto 		if (widget == w)
141101d7584cSLiam Girdwood 			return 1;
141201d7584cSLiam Girdwood 
141301d7584cSLiam Girdwood 	return 0;
141401d7584cSLiam Girdwood }
141501d7584cSLiam Girdwood 
14165fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
14175fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
14185fdd022cSPiotr Stankiewicz {
14195fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
14205fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1421c2cd8216SKuninori Morimoto 	int stream;
14225fdd022cSPiotr Stankiewicz 
1423c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1424c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1425c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1426c2cd8216SKuninori Morimoto 	else
1427c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1428c2cd8216SKuninori Morimoto 
1429027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1430027a4838SKuninori Morimoto 	if (rtd)
14315fdd022cSPiotr Stankiewicz 		return true;
14325fdd022cSPiotr Stankiewicz 
14335fdd022cSPiotr Stankiewicz 	return false;
14345fdd022cSPiotr Stankiewicz }
14355fdd022cSPiotr Stankiewicz 
143623607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
14371ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
143801d7584cSLiam Girdwood {
1439c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
144001d7584cSLiam Girdwood 	int paths;
144101d7584cSLiam Girdwood 
14426e1276a5SBard Liao 	if (fe->num_cpus > 1) {
14436e1276a5SBard Liao 		dev_err(fe->dev,
14446e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
14456e1276a5SBard Liao 		return -EINVAL;
14466e1276a5SBard Liao 	}
14476e1276a5SBard Liao 
144801d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
14496742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
14505fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
145101d7584cSLiam Girdwood 
1452103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
145301d7584cSLiam Girdwood 			stream ? "capture" : "playback");
145401d7584cSLiam Girdwood 
145501d7584cSLiam Girdwood 	return paths;
145601d7584cSLiam Girdwood }
145701d7584cSLiam Girdwood 
145852645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
145952645e33SKuninori Morimoto {
146052645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
146152645e33SKuninori Morimoto }
146252645e33SKuninori Morimoto 
14638cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
14648cce6569SGuennadi Liakhovetski 			      struct snd_soc_dapm_widget_list *list)
146501d7584cSLiam Girdwood {
146601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
14677afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
14682e5894d7SBenoit Cousson 	unsigned int i;
146901d7584cSLiam Girdwood 
1470c840f769SKuninori Morimoto 	/* is there a valid DAI widget for this BE */
1471c840f769SKuninori Morimoto 	for_each_rtd_dais(dpcm->be, i, dai) {
147219bdcc7aSShreyas NC 		widget = snd_soc_dai_get_widget(dai, stream);
147301d7584cSLiam Girdwood 
147419bdcc7aSShreyas NC 		/*
1475c840f769SKuninori Morimoto 		 * The BE is pruned only if none of the dai
147619bdcc7aSShreyas NC 		 * widgets are in the active list.
147719bdcc7aSShreyas NC 		 */
147801d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
14798cce6569SGuennadi Liakhovetski 			return true;
148019bdcc7aSShreyas NC 	}
148101d7584cSLiam Girdwood 
14828cce6569SGuennadi Liakhovetski 	return false;
14838cce6569SGuennadi Liakhovetski }
14848cce6569SGuennadi Liakhovetski 
14858cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
14868cce6569SGuennadi Liakhovetski 			    struct snd_soc_dapm_widget_list **list_)
14878cce6569SGuennadi Liakhovetski {
14888cce6569SGuennadi Liakhovetski 	struct snd_soc_dpcm *dpcm;
14898cce6569SGuennadi Liakhovetski 	int prune = 0;
14908cce6569SGuennadi Liakhovetski 
14918cce6569SGuennadi Liakhovetski 	/* Destroy any old FE <--> BE connections */
14928cce6569SGuennadi Liakhovetski 	for_each_dpcm_be(fe, stream, dpcm) {
14938cce6569SGuennadi Liakhovetski 		if (dpcm_be_is_active(dpcm, stream, *list_))
1494bed646dcSKuninori Morimoto 			continue;
149501d7584cSLiam Girdwood 
1496103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
149701d7584cSLiam Girdwood 			stream ? "capture" : "playback",
149801d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
149901d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
150001d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
150101d7584cSLiam Girdwood 		prune++;
150201d7584cSLiam Girdwood 	}
150301d7584cSLiam Girdwood 
1504103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
150501d7584cSLiam Girdwood 	return prune;
150601d7584cSLiam Girdwood }
150701d7584cSLiam Girdwood 
150801d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
150901d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
151001d7584cSLiam Girdwood {
151101d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
151201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
151301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
151409e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
151501d7584cSLiam Girdwood 	int i, new = 0, err;
151601d7584cSLiam Girdwood 
151701d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
151809e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
151901d7584cSLiam Girdwood 
152009e88f8aSKuninori Morimoto 		switch (widget->id) {
15214616274dSMark Brown 		case snd_soc_dapm_dai_in:
1522c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1523c5b8540dSKoro Chen 				continue;
1524c5b8540dSKoro Chen 			break;
15254616274dSMark Brown 		case snd_soc_dapm_dai_out:
1526c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1527c5b8540dSKoro Chen 				continue;
15284616274dSMark Brown 			break;
15294616274dSMark Brown 		default:
153001d7584cSLiam Girdwood 			continue;
15314616274dSMark Brown 		}
153201d7584cSLiam Girdwood 
153301d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
153409e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
153501d7584cSLiam Girdwood 		if (!be) {
1536103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
153709e88f8aSKuninori Morimoto 					widget->name);
153801d7584cSLiam Girdwood 			continue;
153901d7584cSLiam Girdwood 		}
154001d7584cSLiam Girdwood 
154101d7584cSLiam Girdwood 		/* don't connect if FE is not running */
154223607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
154301d7584cSLiam Girdwood 			continue;
154401d7584cSLiam Girdwood 
154501d7584cSLiam Girdwood 		/* newly connected FE and BE */
154601d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
154701d7584cSLiam Girdwood 		if (err < 0) {
1548103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
154909e88f8aSKuninori Morimoto 				widget->name);
155001d7584cSLiam Girdwood 			break;
155101d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
155201d7584cSLiam Girdwood 			continue;
155301d7584cSLiam Girdwood 
155401d7584cSLiam Girdwood 		/* new */
155501d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
155601d7584cSLiam Girdwood 		new++;
155701d7584cSLiam Girdwood 	}
155801d7584cSLiam Girdwood 
1559103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
156001d7584cSLiam Girdwood 	return new;
156101d7584cSLiam Girdwood }
156201d7584cSLiam Girdwood 
156301d7584cSLiam Girdwood /*
156401d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
156501d7584cSLiam Girdwood  * FE substream.
156601d7584cSLiam Girdwood  */
156723607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
156801d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
156901d7584cSLiam Girdwood {
157001d7584cSLiam Girdwood 	if (new)
157101d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
157201d7584cSLiam Girdwood 	else
157301d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
157401d7584cSLiam Girdwood }
157501d7584cSLiam Girdwood 
157623607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
157701d7584cSLiam Girdwood {
157801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1579a9764869SKaiChieh Chuang 	unsigned long flags;
158001d7584cSLiam Girdwood 
1581a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
15828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
158301d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
158401d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1585a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
158601d7584cSLiam Girdwood }
158701d7584cSLiam Girdwood 
158801d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
158901d7584cSLiam Girdwood 	int stream)
159001d7584cSLiam Girdwood {
159101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
159201d7584cSLiam Girdwood 
159301d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15948d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
159501d7584cSLiam Girdwood 
159601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
159701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
159801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
159901d7584cSLiam Girdwood 
160001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1601103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
160201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
160301d7584cSLiam Girdwood 				be->dpcm[stream].state);
160401d7584cSLiam Girdwood 
160501d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
160601d7584cSLiam Girdwood 			continue;
160701d7584cSLiam Girdwood 
160801d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
160901d7584cSLiam Girdwood 			continue;
161001d7584cSLiam Girdwood 
161101d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
161201d7584cSLiam Girdwood 		be_substream->runtime = NULL;
161301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
161401d7584cSLiam Girdwood 	}
161501d7584cSLiam Girdwood }
161601d7584cSLiam Girdwood 
161723607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
161801d7584cSLiam Girdwood {
161901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
162001d7584cSLiam Girdwood 	int err, count = 0;
162101d7584cSLiam Girdwood 
162201d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
16238d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
162401d7584cSLiam Girdwood 
162501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
162601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
162701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
162801d7584cSLiam Girdwood 
16292062b4c5SRussell King - ARM Linux 		if (!be_substream) {
16302062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
16312062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
16322062b4c5SRussell King - ARM Linux 			continue;
16332062b4c5SRussell King - ARM Linux 		}
16342062b4c5SRussell King - ARM Linux 
163501d7584cSLiam Girdwood 		/* is this op for this BE ? */
163601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
163701d7584cSLiam Girdwood 			continue;
163801d7584cSLiam Girdwood 
163901d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
164001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1641103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
164201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
164301d7584cSLiam Girdwood 				be->dpcm[stream].state);
164401d7584cSLiam Girdwood 
164501d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
164601d7584cSLiam Girdwood 			continue;
164701d7584cSLiam Girdwood 
164801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
164901d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
165001d7584cSLiam Girdwood 			continue;
165101d7584cSLiam Girdwood 
16522062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
16532062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
165401d7584cSLiam Girdwood 
165501d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
165601d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
165701d7584cSLiam Girdwood 		if (err < 0) {
1658103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
165901d7584cSLiam Girdwood 			be->dpcm[stream].users--;
166001d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1661103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
166201d7584cSLiam Girdwood 					stream ? "capture" : "playback",
166301d7584cSLiam Girdwood 					be->dpcm[stream].state);
166401d7584cSLiam Girdwood 
166501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
166601d7584cSLiam Girdwood 			goto unwind;
166701d7584cSLiam Girdwood 		}
166801d7584cSLiam Girdwood 
166901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
167001d7584cSLiam Girdwood 		count++;
167101d7584cSLiam Girdwood 	}
167201d7584cSLiam Girdwood 
167301d7584cSLiam Girdwood 	return count;
167401d7584cSLiam Girdwood 
167501d7584cSLiam Girdwood unwind:
167601d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
16778d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
167801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
167901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
168001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
168101d7584cSLiam Girdwood 
168201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
168301d7584cSLiam Girdwood 			continue;
168401d7584cSLiam Girdwood 
168501d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1686103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
168701d7584cSLiam Girdwood 				stream ? "capture" : "playback",
168801d7584cSLiam Girdwood 				be->dpcm[stream].state);
168901d7584cSLiam Girdwood 
169001d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
169101d7584cSLiam Girdwood 			continue;
169201d7584cSLiam Girdwood 
169301d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
169401d7584cSLiam Girdwood 			continue;
169501d7584cSLiam Girdwood 
169601d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
169701d7584cSLiam Girdwood 		be_substream->runtime = NULL;
169801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
169901d7584cSLiam Girdwood 	}
170001d7584cSLiam Girdwood 
170101d7584cSLiam Girdwood 	return err;
170201d7584cSLiam Girdwood }
170301d7584cSLiam Girdwood 
170408ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1705435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
170608ae9b45SLars-Peter Clausen {
170708ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1708e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
170908ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
171008ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1711002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1712435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1713002220a9SLars-Peter Clausen 	else
1714435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
171508ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
171608ae9b45SLars-Peter Clausen }
171708ae9b45SLars-Peter Clausen 
1718435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1719435ffb76SJerome Brunet 				      u64 *formats)
1720b073ed4eSKuninori Morimoto {
1721b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1722b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
17237afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1724b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1725b073ed4eSKuninori Morimoto 
1726b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1727435ffb76SJerome Brunet 		return;
1728b073ed4eSKuninori Morimoto 
1729b073ed4eSKuninori Morimoto 	/*
1730b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1731b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1732b073ed4eSKuninori Morimoto 	 */
1733b073ed4eSKuninori Morimoto 
17348d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1735b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1736b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1737b073ed4eSKuninori Morimoto 		int i;
1738b073ed4eSKuninori Morimoto 
1739a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(be, i, dai) {
17404febced1SJerome Brunet 			/*
17414febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
17424febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
17434febced1SJerome Brunet 			 */
17447afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
17454febced1SJerome Brunet 				continue;
17464febced1SJerome Brunet 
1747acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1748b073ed4eSKuninori Morimoto 
1749435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1750435ffb76SJerome Brunet 		}
1751b073ed4eSKuninori Morimoto 	}
1752b073ed4eSKuninori Morimoto }
1753b073ed4eSKuninori Morimoto 
1754435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1755f4c277b8SJiada Wang 				    unsigned int *channels_min,
1756f4c277b8SJiada Wang 				    unsigned int *channels_max)
1757f4c277b8SJiada Wang {
1758f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1759f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1760f4c277b8SJiada Wang 	int stream = substream->stream;
1761f4c277b8SJiada Wang 
1762f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1763f4c277b8SJiada Wang 		return;
1764f4c277b8SJiada Wang 
1765f4c277b8SJiada Wang 	/*
1766f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1767f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1768f4c277b8SJiada Wang 	 */
1769f4c277b8SJiada Wang 
17708d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1771f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
1772f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
17734f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
177419bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
177519bdcc7aSShreyas NC 		int i;
1776f4c277b8SJiada Wang 
1777a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(be, i, dai) {
17780e9cf4c4SBard Liao 			/*
17790e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
17800e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
17810e9cf4c4SBard Liao 			 */
17820e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
17830e9cf4c4SBard Liao 				continue;
17840e9cf4c4SBard Liao 
178519bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
17864f2bd18bSJerome Brunet 
178719bdcc7aSShreyas NC 			*channels_min = max(*channels_min,
178819bdcc7aSShreyas NC 					    cpu_stream->channels_min);
178919bdcc7aSShreyas NC 			*channels_max = min(*channels_max,
179019bdcc7aSShreyas NC 					    cpu_stream->channels_max);
179119bdcc7aSShreyas NC 		}
17924f2bd18bSJerome Brunet 
17934f2bd18bSJerome Brunet 		/*
17944f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
17954f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
17964f2bd18bSJerome Brunet 		 */
17974f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
1798c2233a26SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1799f4c277b8SJiada Wang 
1800f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1801f4c277b8SJiada Wang 					    codec_stream->channels_min);
1802f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1803f4c277b8SJiada Wang 					    codec_stream->channels_max);
1804f4c277b8SJiada Wang 		}
1805f4c277b8SJiada Wang 	}
1806f4c277b8SJiada Wang }
1807f4c277b8SJiada Wang 
1808baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1809baacd8d1SJerome Brunet 				    unsigned int *rates,
1810baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1811baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1812baacd8d1SJerome Brunet {
1813baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1814baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1815baacd8d1SJerome Brunet 	int stream = substream->stream;
1816baacd8d1SJerome Brunet 
1817baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1818baacd8d1SJerome Brunet 		return;
1819baacd8d1SJerome Brunet 
1820baacd8d1SJerome Brunet 	/*
1821baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1822baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1823baacd8d1SJerome Brunet 	 */
1824baacd8d1SJerome Brunet 
18258d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1826baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1827c840f769SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm;
18287afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1829baacd8d1SJerome Brunet 		int i;
1830baacd8d1SJerome Brunet 
1831c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
18320e9cf4c4SBard Liao 			/*
1833c840f769SKuninori Morimoto 			 * Skip DAIs which don't support the current stream
18340e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
18350e9cf4c4SBard Liao 			 */
18360e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
18370e9cf4c4SBard Liao 				continue;
18380e9cf4c4SBard Liao 
1839c840f769SKuninori Morimoto 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1840baacd8d1SJerome Brunet 
1841c840f769SKuninori Morimoto 			*rate_min = max(*rate_min, pcm->rate_min);
1842c840f769SKuninori Morimoto 			*rate_max = min_not_zero(*rate_max, pcm->rate_max);
1843c840f769SKuninori Morimoto 			*rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
1844baacd8d1SJerome Brunet 		}
1845baacd8d1SJerome Brunet 	}
1846baacd8d1SJerome Brunet }
1847baacd8d1SJerome Brunet 
184845c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
184901d7584cSLiam Girdwood {
185001d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
185101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
185219bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
185319bdcc7aSShreyas NC 	int i;
185401d7584cSLiam Girdwood 
1855a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
18560e9cf4c4SBard Liao 		/*
18570e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
18580e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
18590e9cf4c4SBard Liao 		 */
18600e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
18610e9cf4c4SBard Liao 			continue;
18620e9cf4c4SBard Liao 
18630c9ba720SKuninori Morimoto 		dpcm_init_runtime_hw(runtime,
18640c9ba720SKuninori Morimoto 			snd_soc_dai_get_pcm_stream(cpu_dai,
18650c9ba720SKuninori Morimoto 						   substream->stream));
186619bdcc7aSShreyas NC 	}
1867f4c277b8SJiada Wang 
1868435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1869435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1870435ffb76SJerome Brunet 				&runtime->hw.channels_max);
1871baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1872baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
187301d7584cSLiam Girdwood }
187401d7584cSLiam Girdwood 
1875ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1876ea9d0d77STakashi Iwai 
1877ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1878ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1879ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1880ea9d0d77STakashi Iwai  * process the pending trigger action here.
1881ea9d0d77STakashi Iwai  */
1882ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1883ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1884ea9d0d77STakashi Iwai {
1885ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1886ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1887ea9d0d77STakashi Iwai 
1888ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1889ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1890ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1891ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1892ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1893ea9d0d77STakashi Iwai 	}
1894ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1895ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1896ea9d0d77STakashi Iwai }
1897ea9d0d77STakashi Iwai 
1898906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1899906c7d69SPC Liao 			       int stream)
1900906c7d69SPC Liao {
1901906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
1902906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
190319bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
1904906c7d69SPC Liao 	int err;
190519bdcc7aSShreyas NC 	int i;
1906906c7d69SPC Liao 
1907906c7d69SPC Liao 	/* apply symmetry for FE */
1908906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
1909906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1910906c7d69SPC Liao 
1911a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1912906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1913906c7d69SPC Liao 		if (fe_cpu_dai->active) {
1914906c7d69SPC Liao 			err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1915906c7d69SPC Liao 			if (err < 0)
1916906c7d69SPC Liao 				return err;
1917906c7d69SPC Liao 		}
191819bdcc7aSShreyas NC 	}
1919906c7d69SPC Liao 
1920906c7d69SPC Liao 	/* apply symmetry for BE */
19218d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1922906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1923906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1924906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
19256246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
1926c840f769SKuninori Morimoto 		struct snd_soc_dai *dai;
1927906c7d69SPC Liao 		int i;
1928906c7d69SPC Liao 
19296246f283SJerome Brunet 		/* A backend may not have the requested substream */
19306246f283SJerome Brunet 		if (!be_substream)
19316246f283SJerome Brunet 			continue;
19326246f283SJerome Brunet 
19336246f283SJerome Brunet 		rtd = be_substream->private_data;
1934f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1935f1176614SJeeja KP 			continue;
1936f1176614SJeeja KP 
1937906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
1938906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1939906c7d69SPC Liao 
1940906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1941c840f769SKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
1942c840f769SKuninori Morimoto 			if (dai->active) {
1943c840f769SKuninori Morimoto 				err = soc_pcm_apply_symmetry(fe_substream, dai);
1944906c7d69SPC Liao 				if (err < 0)
1945906c7d69SPC Liao 					return err;
1946906c7d69SPC Liao 			}
1947906c7d69SPC Liao 		}
1948906c7d69SPC Liao 	}
1949906c7d69SPC Liao 
1950906c7d69SPC Liao 	return 0;
1951906c7d69SPC Liao }
1952906c7d69SPC Liao 
195301d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
195401d7584cSLiam Girdwood {
195501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
195601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
195701d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
195801d7584cSLiam Girdwood 
1959ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
196001d7584cSLiam Girdwood 
196125c2f515SKuninori Morimoto 	ret = dpcm_be_dai_startup(fe, stream);
196201d7584cSLiam Girdwood 	if (ret < 0) {
1963103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
196401d7584cSLiam Girdwood 		goto be_err;
196501d7584cSLiam Girdwood 	}
196601d7584cSLiam Girdwood 
1967103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
196801d7584cSLiam Girdwood 
196901d7584cSLiam Girdwood 	/* start the DAI frontend */
197001d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
197101d7584cSLiam Girdwood 	if (ret < 0) {
1972103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
197301d7584cSLiam Girdwood 		goto unwind;
197401d7584cSLiam Girdwood 	}
197501d7584cSLiam Girdwood 
197601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
197701d7584cSLiam Girdwood 
197801d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
197901d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
198001d7584cSLiam Girdwood 
1981906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
19828a01fbf0SKuninori Morimoto 	if (ret < 0)
1983906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1984906c7d69SPC Liao 			ret);
198501d7584cSLiam Girdwood 
198601d7584cSLiam Girdwood unwind:
19878a01fbf0SKuninori Morimoto 	if (ret < 0)
198825c2f515SKuninori Morimoto 		dpcm_be_dai_startup_unwind(fe, stream);
198901d7584cSLiam Girdwood be_err:
1990ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
199101d7584cSLiam Girdwood 	return ret;
199201d7584cSLiam Girdwood }
199301d7584cSLiam Girdwood 
199423607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
199501d7584cSLiam Girdwood {
199601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
199701d7584cSLiam Girdwood 
199801d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
19998d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
200001d7584cSLiam Girdwood 
200101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
200201d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
200301d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
200401d7584cSLiam Girdwood 
200501d7584cSLiam Girdwood 		/* is this op for this BE ? */
200601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
200701d7584cSLiam Girdwood 			continue;
200801d7584cSLiam Girdwood 
200901d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
2010103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
201101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
201201d7584cSLiam Girdwood 				be->dpcm[stream].state);
201301d7584cSLiam Girdwood 
201401d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
201501d7584cSLiam Girdwood 			continue;
201601d7584cSLiam Girdwood 
201701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
20189c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
20199c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
20209c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
20219c0ac70aSKai Chieh Chuang 		}
202201d7584cSLiam Girdwood 
2023103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
202494d215ccS彭东林 			be->dai_link->name);
202501d7584cSLiam Girdwood 
202601d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
202701d7584cSLiam Girdwood 		be_substream->runtime = NULL;
202801d7584cSLiam Girdwood 
202901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
203001d7584cSLiam Girdwood 	}
203101d7584cSLiam Girdwood 	return 0;
203201d7584cSLiam Girdwood }
203301d7584cSLiam Girdwood 
203401d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
203501d7584cSLiam Girdwood {
203601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
203701d7584cSLiam Girdwood 	int stream = substream->stream;
203801d7584cSLiam Girdwood 
2039ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
204001d7584cSLiam Girdwood 
204101d7584cSLiam Girdwood 	/* shutdown the BEs */
204225c2f515SKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
204301d7584cSLiam Girdwood 
2044103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
204501d7584cSLiam Girdwood 
204601d7584cSLiam Girdwood 	/* now shutdown the frontend */
204701d7584cSLiam Girdwood 	soc_pcm_close(substream);
204801d7584cSLiam Girdwood 
204901d7584cSLiam Girdwood 	/* run the stream event for each BE */
20501c531230SKuninori Morimoto 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
205101d7584cSLiam Girdwood 
205201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2053ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
205401d7584cSLiam Girdwood 	return 0;
205501d7584cSLiam Girdwood }
205601d7584cSLiam Girdwood 
205723607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
205801d7584cSLiam Girdwood {
205901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
206001d7584cSLiam Girdwood 
206101d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
206201d7584cSLiam Girdwood 	 * to this frontend DAI */
20638d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
206401d7584cSLiam Girdwood 
206501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
206601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
206701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
206801d7584cSLiam Girdwood 
206901d7584cSLiam Girdwood 		/* is this op for this BE ? */
207001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
207101d7584cSLiam Girdwood 			continue;
207201d7584cSLiam Girdwood 
207301d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
207401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
207501d7584cSLiam Girdwood 				continue;
207601d7584cSLiam Girdwood 
207736fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
207836fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
207936fba62cSQiao Zhou 			continue;
208036fba62cSQiao Zhou 
208101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
208201d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
208301d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
208408b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
20855e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
20865e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
208701d7584cSLiam Girdwood 			continue;
208801d7584cSLiam Girdwood 
2089103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
209094d215ccS彭东林 			be->dai_link->name);
209101d7584cSLiam Girdwood 
209201d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
209301d7584cSLiam Girdwood 
209401d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
209501d7584cSLiam Girdwood 	}
209601d7584cSLiam Girdwood 
209701d7584cSLiam Girdwood 	return 0;
209801d7584cSLiam Girdwood }
209901d7584cSLiam Girdwood 
210045c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
210101d7584cSLiam Girdwood {
210201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
210301d7584cSLiam Girdwood 	int err, stream = substream->stream;
210401d7584cSLiam Girdwood 
210501d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2106ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
210701d7584cSLiam Girdwood 
2108103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
210901d7584cSLiam Girdwood 
211001d7584cSLiam Girdwood 	/* call hw_free on the frontend */
211101d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
211201d7584cSLiam Girdwood 	if (err < 0)
2113103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
211401d7584cSLiam Girdwood 			fe->dai_link->name);
211501d7584cSLiam Girdwood 
211601d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
211701d7584cSLiam Girdwood 	 * to this frontend DAI */
211801d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
211901d7584cSLiam Girdwood 
212001d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2121ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
212201d7584cSLiam Girdwood 
212301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
212401d7584cSLiam Girdwood 	return 0;
212501d7584cSLiam Girdwood }
212601d7584cSLiam Girdwood 
212723607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
212801d7584cSLiam Girdwood {
212901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
213001d7584cSLiam Girdwood 	int ret;
213101d7584cSLiam Girdwood 
21328d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
213301d7584cSLiam Girdwood 
213401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
213501d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
213601d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
213701d7584cSLiam Girdwood 
213801d7584cSLiam Girdwood 		/* is this op for this BE ? */
213901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
214001d7584cSLiam Girdwood 			continue;
214101d7584cSLiam Girdwood 
214201d7584cSLiam Girdwood 		/* copy params for each dpcm */
214301d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
214401d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
214501d7584cSLiam Girdwood 
214601d7584cSLiam Girdwood 		/* perform any hw_params fixups */
214701d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
214801d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
214901d7584cSLiam Girdwood 					&dpcm->hw_params);
215001d7584cSLiam Girdwood 			if (ret < 0) {
215101d7584cSLiam Girdwood 				dev_err(be->dev,
2152103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
215301d7584cSLiam Girdwood 					ret);
215401d7584cSLiam Girdwood 				goto unwind;
215501d7584cSLiam Girdwood 			}
215601d7584cSLiam Girdwood 		}
215701d7584cSLiam Girdwood 
2158ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2159ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2160ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2161ae061d2aSLibin Yang 
2162b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2163b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2164b0639bd2SKuninori Morimoto 			continue;
2165b0639bd2SKuninori Morimoto 
2166b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2167b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2168b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2169b0639bd2SKuninori Morimoto 			continue;
2170b0639bd2SKuninori Morimoto 
2171b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
217294d215ccS彭东林 			be->dai_link->name);
2173b0639bd2SKuninori Morimoto 
217401d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
217501d7584cSLiam Girdwood 		if (ret < 0) {
217601d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2177103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
217801d7584cSLiam Girdwood 			goto unwind;
217901d7584cSLiam Girdwood 		}
218001d7584cSLiam Girdwood 
218101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
218201d7584cSLiam Girdwood 	}
218301d7584cSLiam Girdwood 	return 0;
218401d7584cSLiam Girdwood 
218501d7584cSLiam Girdwood unwind:
218601d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
21878d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
218801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
218901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
219001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
219101d7584cSLiam Girdwood 
219201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
219301d7584cSLiam Girdwood 			continue;
219401d7584cSLiam Girdwood 
219501d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
219601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
219701d7584cSLiam Girdwood 			continue;
219801d7584cSLiam Girdwood 
219901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
220001d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
220101d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
220201d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
220301d7584cSLiam Girdwood 			continue;
220401d7584cSLiam Girdwood 
220501d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
220601d7584cSLiam Girdwood 	}
220701d7584cSLiam Girdwood 
220801d7584cSLiam Girdwood 	return ret;
220901d7584cSLiam Girdwood }
221001d7584cSLiam Girdwood 
221145c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
221201d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
221301d7584cSLiam Girdwood {
221401d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
221501d7584cSLiam Girdwood 	int ret, stream = substream->stream;
221601d7584cSLiam Girdwood 
221701d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2218ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
221901d7584cSLiam Girdwood 
222025c2f515SKuninori Morimoto 	memcpy(&fe->dpcm[stream].hw_params, params,
222101d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
222225c2f515SKuninori Morimoto 	ret = dpcm_be_dai_hw_params(fe, stream);
222301d7584cSLiam Girdwood 	if (ret < 0) {
2224103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
222501d7584cSLiam Girdwood 		goto out;
222601d7584cSLiam Girdwood 	}
222701d7584cSLiam Girdwood 
2228103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
222901d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
223001d7584cSLiam Girdwood 			params_channels(params), params_format(params));
223101d7584cSLiam Girdwood 
223201d7584cSLiam Girdwood 	/* call hw_params on the frontend */
223301d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
223401d7584cSLiam Girdwood 	if (ret < 0) {
2235103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
223601d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
223701d7584cSLiam Girdwood 	 } else
223801d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
223901d7584cSLiam Girdwood 
224001d7584cSLiam Girdwood out:
2241ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
224201d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
224301d7584cSLiam Girdwood 	return ret;
224401d7584cSLiam Girdwood }
224501d7584cSLiam Girdwood 
224601d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
224701d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
224801d7584cSLiam Girdwood {
224901d7584cSLiam Girdwood 	int ret;
225001d7584cSLiam Girdwood 
2251103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
225294d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
225301d7584cSLiam Girdwood 
225401d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
225501d7584cSLiam Girdwood 	if (ret < 0)
2256103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
225701d7584cSLiam Girdwood 
225801d7584cSLiam Girdwood 	return ret;
225901d7584cSLiam Girdwood }
226001d7584cSLiam Girdwood 
226123607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
226245c0a188SMark Brown 			       int cmd)
226301d7584cSLiam Girdwood {
226401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
226501d7584cSLiam Girdwood 	int ret = 0;
226601d7584cSLiam Girdwood 
22678d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
226801d7584cSLiam Girdwood 
226901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
227001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
227101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
227201d7584cSLiam Girdwood 
227301d7584cSLiam Girdwood 		/* is this op for this BE ? */
227401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
227501d7584cSLiam Girdwood 			continue;
227601d7584cSLiam Girdwood 
227701d7584cSLiam Girdwood 		switch (cmd) {
227801d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
227901d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
228021fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
228121fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
228201d7584cSLiam Girdwood 				continue;
228301d7584cSLiam Girdwood 
228401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
228501d7584cSLiam Girdwood 			if (ret)
228601d7584cSLiam Girdwood 				return ret;
228701d7584cSLiam Girdwood 
228801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
228901d7584cSLiam Girdwood 			break;
229001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
229101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
229201d7584cSLiam Girdwood 				continue;
229301d7584cSLiam Girdwood 
229401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
229501d7584cSLiam Girdwood 			if (ret)
229601d7584cSLiam Girdwood 				return ret;
229701d7584cSLiam Girdwood 
229801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
229901d7584cSLiam Girdwood 			break;
230001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
230101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
230201d7584cSLiam Girdwood 				continue;
230301d7584cSLiam Girdwood 
230401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
230501d7584cSLiam Girdwood 			if (ret)
230601d7584cSLiam Girdwood 				return ret;
230701d7584cSLiam Girdwood 
230801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
230901d7584cSLiam Girdwood 			break;
231001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
231121fca8bdS이경택 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
231221fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
231301d7584cSLiam Girdwood 				continue;
231401d7584cSLiam Girdwood 
231501d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
231601d7584cSLiam Girdwood 				continue;
231701d7584cSLiam Girdwood 
231801d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
231901d7584cSLiam Girdwood 			if (ret)
232001d7584cSLiam Girdwood 				return ret;
232101d7584cSLiam Girdwood 
232201d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
232301d7584cSLiam Girdwood 			break;
232401d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2325868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
232601d7584cSLiam Girdwood 				continue;
232701d7584cSLiam Girdwood 
232801d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
232901d7584cSLiam Girdwood 				continue;
233001d7584cSLiam Girdwood 
233101d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
233201d7584cSLiam Girdwood 			if (ret)
233301d7584cSLiam Girdwood 				return ret;
233401d7584cSLiam Girdwood 
233501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
233601d7584cSLiam Girdwood 			break;
233701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233801d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
233901d7584cSLiam Girdwood 				continue;
234001d7584cSLiam Girdwood 
234101d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
234201d7584cSLiam Girdwood 				continue;
234301d7584cSLiam Girdwood 
234401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
234501d7584cSLiam Girdwood 			if (ret)
234601d7584cSLiam Girdwood 				return ret;
234701d7584cSLiam Girdwood 
234801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
234901d7584cSLiam Girdwood 			break;
235001d7584cSLiam Girdwood 		}
235101d7584cSLiam Girdwood 	}
235201d7584cSLiam Girdwood 
235301d7584cSLiam Girdwood 	return ret;
235401d7584cSLiam Girdwood }
235501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
235601d7584cSLiam Girdwood 
2357acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2358acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2359acbf2774SRanjani Sridharan {
2360acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2361acbf2774SRanjani Sridharan 	int ret;
2362acbf2774SRanjani Sridharan 
2363acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2364acbf2774SRanjani Sridharan 	if (fe_first) {
2365acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2366acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2367acbf2774SRanjani Sridharan 
2368acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2369acbf2774SRanjani Sridharan 		if (ret < 0)
2370acbf2774SRanjani Sridharan 			return ret;
2371acbf2774SRanjani Sridharan 
2372acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2373acbf2774SRanjani Sridharan 		return ret;
2374acbf2774SRanjani Sridharan 	}
2375acbf2774SRanjani Sridharan 
2376acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2377acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2378acbf2774SRanjani Sridharan 	if (ret < 0)
2379acbf2774SRanjani Sridharan 		return ret;
2380acbf2774SRanjani Sridharan 
2381acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2382acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2383acbf2774SRanjani Sridharan 
2384acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2385acbf2774SRanjani Sridharan 
2386acbf2774SRanjani Sridharan 	return ret;
2387acbf2774SRanjani Sridharan }
2388acbf2774SRanjani Sridharan 
2389ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
239001d7584cSLiam Girdwood {
239101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2392acbf2774SRanjani Sridharan 	int stream = substream->stream;
2393acbf2774SRanjani Sridharan 	int ret = 0;
239401d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
239501d7584cSLiam Girdwood 
239601d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
239701d7584cSLiam Girdwood 
239801d7584cSLiam Girdwood 	switch (trigger) {
239901d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2400acbf2774SRanjani Sridharan 		switch (cmd) {
2401acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2402acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2403acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2404acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2405acbf2774SRanjani Sridharan 			break;
2406acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2407acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2408acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2409acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2410acbf2774SRanjani Sridharan 			break;
2411acbf2774SRanjani Sridharan 		default:
2412acbf2774SRanjani Sridharan 			ret = -EINVAL;
2413acbf2774SRanjani Sridharan 			break;
241401d7584cSLiam Girdwood 		}
241501d7584cSLiam Girdwood 		break;
241601d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2417acbf2774SRanjani Sridharan 		switch (cmd) {
2418acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2419acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2420acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2421acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2422acbf2774SRanjani Sridharan 			break;
2423acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2424acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2425acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2426acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2427acbf2774SRanjani Sridharan 			break;
2428acbf2774SRanjani Sridharan 		default:
2429acbf2774SRanjani Sridharan 			ret = -EINVAL;
2430acbf2774SRanjani Sridharan 			break;
243101d7584cSLiam Girdwood 		}
243201d7584cSLiam Girdwood 		break;
243307bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
243407bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
243507bf84aaSLiam Girdwood 
2436103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
243707bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
243807bf84aaSLiam Girdwood 
243930819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
244007bf84aaSLiam Girdwood 		break;
244101d7584cSLiam Girdwood 	default:
2442103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
244301d7584cSLiam Girdwood 				fe->dai_link->name);
244401d7584cSLiam Girdwood 		ret = -EINVAL;
244501d7584cSLiam Girdwood 		goto out;
244601d7584cSLiam Girdwood 	}
244701d7584cSLiam Girdwood 
2448acbf2774SRanjani Sridharan 	if (ret < 0) {
2449acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2450acbf2774SRanjani Sridharan 			cmd, ret);
2451acbf2774SRanjani Sridharan 		goto out;
2452acbf2774SRanjani Sridharan 	}
2453acbf2774SRanjani Sridharan 
245401d7584cSLiam Girdwood 	switch (cmd) {
245501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
245601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
245701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
245801d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
245901d7584cSLiam Girdwood 		break;
246001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
246101d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
246201d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
246301d7584cSLiam Girdwood 		break;
24649f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
24659f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
24669f169b9fSPatrick Lai 		break;
246701d7584cSLiam Girdwood 	}
246801d7584cSLiam Girdwood 
246901d7584cSLiam Girdwood out:
247001d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
247101d7584cSLiam Girdwood 	return ret;
247201d7584cSLiam Girdwood }
247301d7584cSLiam Girdwood 
2474ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2475ea9d0d77STakashi Iwai {
2476ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2477ea9d0d77STakashi Iwai 	int stream = substream->stream;
2478ea9d0d77STakashi Iwai 
2479ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2480ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2481ea9d0d77STakashi Iwai 	 */
2482ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2483ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2484ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2485ea9d0d77STakashi Iwai 	}
2486ea9d0d77STakashi Iwai 
2487ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2488ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2489ea9d0d77STakashi Iwai }
2490ea9d0d77STakashi Iwai 
249123607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
249201d7584cSLiam Girdwood {
249301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
249401d7584cSLiam Girdwood 	int ret = 0;
249501d7584cSLiam Girdwood 
24968d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
249701d7584cSLiam Girdwood 
249801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
249901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
250001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
250101d7584cSLiam Girdwood 
250201d7584cSLiam Girdwood 		/* is this op for this BE ? */
250301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
250401d7584cSLiam Girdwood 			continue;
250501d7584cSLiam Girdwood 
250601d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
250795f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
25085087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
25095087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
251001d7584cSLiam Girdwood 			continue;
251101d7584cSLiam Girdwood 
2512103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
251394d215ccS彭东林 			be->dai_link->name);
251401d7584cSLiam Girdwood 
251501d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
251601d7584cSLiam Girdwood 		if (ret < 0) {
2517103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
251801d7584cSLiam Girdwood 				ret);
251901d7584cSLiam Girdwood 			break;
252001d7584cSLiam Girdwood 		}
252101d7584cSLiam Girdwood 
252201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
252301d7584cSLiam Girdwood 	}
252401d7584cSLiam Girdwood 	return ret;
252501d7584cSLiam Girdwood }
252601d7584cSLiam Girdwood 
252745c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
252801d7584cSLiam Girdwood {
252901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
253001d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
253101d7584cSLiam Girdwood 
253201d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
253301d7584cSLiam Girdwood 
2534103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
253501d7584cSLiam Girdwood 
2536ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
253701d7584cSLiam Girdwood 
253801d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
253901d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2540103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
254101d7584cSLiam Girdwood 				fe->dai_link->name);
254201d7584cSLiam Girdwood 		ret = -EINVAL;
254301d7584cSLiam Girdwood 		goto out;
254401d7584cSLiam Girdwood 	}
254501d7584cSLiam Girdwood 
254625c2f515SKuninori Morimoto 	ret = dpcm_be_dai_prepare(fe, stream);
254701d7584cSLiam Girdwood 	if (ret < 0)
254801d7584cSLiam Girdwood 		goto out;
254901d7584cSLiam Girdwood 
255001d7584cSLiam Girdwood 	/* call prepare on the frontend */
255101d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
255201d7584cSLiam Girdwood 	if (ret < 0) {
2553103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
255401d7584cSLiam Girdwood 			fe->dai_link->name);
255501d7584cSLiam Girdwood 		goto out;
255601d7584cSLiam Girdwood 	}
255701d7584cSLiam Girdwood 
255801d7584cSLiam Girdwood 	/* run the stream event for each BE */
255901d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
256001d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
256101d7584cSLiam Girdwood 
256201d7584cSLiam Girdwood out:
2563ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
256401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
256501d7584cSLiam Girdwood 
256601d7584cSLiam Girdwood 	return ret;
256701d7584cSLiam Girdwood }
256801d7584cSLiam Girdwood 
2569618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2570618dae11SLiam Girdwood {
257107bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
257207bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
257307bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2574618dae11SLiam Girdwood 	int err;
257501d7584cSLiam Girdwood 
2576103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2577618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2578618dae11SLiam Girdwood 
257907bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
258007bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2581103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
258207bf84aaSLiam Girdwood 				fe->dai_link->name);
258307bf84aaSLiam Girdwood 
258430819358SKuninori Morimoto 		err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
258507bf84aaSLiam Girdwood 		if (err < 0)
2586103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
258707bf84aaSLiam Girdwood 	} else {
2588103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
258907bf84aaSLiam Girdwood 			fe->dai_link->name);
259007bf84aaSLiam Girdwood 
2591618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2592618dae11SLiam Girdwood 		if (err < 0)
2593103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
259407bf84aaSLiam Girdwood 	}
2595618dae11SLiam Girdwood 
2596618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2597618dae11SLiam Girdwood 	if (err < 0)
2598103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2599618dae11SLiam Girdwood 
2600618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2601618dae11SLiam Girdwood 	if (err < 0)
2602103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2603618dae11SLiam Girdwood 
2604618dae11SLiam Girdwood 	/* run the stream event for each BE */
2605618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2606618dae11SLiam Girdwood 
2607618dae11SLiam Girdwood 	return 0;
2608618dae11SLiam Girdwood }
2609618dae11SLiam Girdwood 
2610618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2611618dae11SLiam Girdwood {
261207bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
261307bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2614618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
261507bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2616618dae11SLiam Girdwood 	int ret;
2617a9764869SKaiChieh Chuang 	unsigned long flags;
2618618dae11SLiam Girdwood 
2619103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2620618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2621618dae11SLiam Girdwood 
2622618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2623618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2624618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2625618dae11SLiam Girdwood 		return -EINVAL;
2626618dae11SLiam Girdwood 
2627618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2628618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2629fffc0ca2SDan Carpenter 	if (ret < 0)
2630618dae11SLiam Girdwood 		goto disconnect;
2631618dae11SLiam Girdwood 
2632618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2633618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2634618dae11SLiam Girdwood 		return 0;
2635618dae11SLiam Girdwood 
2636618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2637fffc0ca2SDan Carpenter 	if (ret < 0)
2638618dae11SLiam Girdwood 		goto close;
2639618dae11SLiam Girdwood 
2640618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2641618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2642618dae11SLiam Girdwood 		return 0;
2643618dae11SLiam Girdwood 
2644618dae11SLiam Girdwood 
2645618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2646fffc0ca2SDan Carpenter 	if (ret < 0)
2647618dae11SLiam Girdwood 		goto hw_free;
2648618dae11SLiam Girdwood 
2649618dae11SLiam Girdwood 	/* run the stream event for each BE */
2650618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2651618dae11SLiam Girdwood 
2652618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2653618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2654618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2655618dae11SLiam Girdwood 		return 0;
2656618dae11SLiam Girdwood 
265707bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
265807bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2659103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
266007bf84aaSLiam Girdwood 				fe->dai_link->name);
266107bf84aaSLiam Girdwood 
266230819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
266307bf84aaSLiam Girdwood 		if (ret < 0) {
2664103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
266507bf84aaSLiam Girdwood 			goto hw_free;
266607bf84aaSLiam Girdwood 		}
266707bf84aaSLiam Girdwood 	} else {
2668103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2669618dae11SLiam Girdwood 			fe->dai_link->name);
2670618dae11SLiam Girdwood 
2671618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2672618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2673618dae11SLiam Girdwood 		if (ret < 0) {
2674103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2675618dae11SLiam Girdwood 			goto hw_free;
2676618dae11SLiam Girdwood 		}
267707bf84aaSLiam Girdwood 	}
2678618dae11SLiam Girdwood 
2679618dae11SLiam Girdwood 	return 0;
2680618dae11SLiam Girdwood 
2681618dae11SLiam Girdwood hw_free:
2682618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2683618dae11SLiam Girdwood close:
2684618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2685618dae11SLiam Girdwood disconnect:
2686618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2687a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
26888d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2689618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2690618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2691618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2692618dae11SLiam Girdwood 	}
2693a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2694618dae11SLiam Girdwood 
2695618dae11SLiam Girdwood 	return ret;
2696618dae11SLiam Girdwood }
2697618dae11SLiam Girdwood 
2698de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2699618dae11SLiam Girdwood {
2700618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
27017083f877SKuninori Morimoto 	int stream;
2702de15d7ffSJerome Brunet 	int count, paths;
2703580dff36SKuninori Morimoto 	int ret;
2704618dae11SLiam Girdwood 
27056e1276a5SBard Liao 	if (fe->num_cpus > 1) {
27066e1276a5SBard Liao 		dev_err(fe->dev,
27076e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
27086e1276a5SBard Liao 		return -EINVAL;
27096e1276a5SBard Liao 	}
27106e1276a5SBard Liao 
2711618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2712de15d7ffSJerome Brunet 		return 0;
2713618dae11SLiam Girdwood 
2714618dae11SLiam Girdwood 	/* only check active links */
2715c2233a26SKuninori Morimoto 	if (!asoc_rtd_to_cpu(fe, 0)->active)
2716de15d7ffSJerome Brunet 		return 0;
2717618dae11SLiam Girdwood 
2718618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2719de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2720de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2721618dae11SLiam Girdwood 
27227083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2723075207d2SQiao Zhou 
27247083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
2725c2233a26SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0),   stream) ||
2726c2233a26SKuninori Morimoto 		    !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
27277083f877SKuninori Morimoto 			continue;
2728618dae11SLiam Girdwood 
27297083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
2730c2233a26SKuninori Morimoto 		if (!asoc_rtd_to_cpu(fe, 0)->stream_active[stream] ||
2731c2233a26SKuninori Morimoto 		    !asoc_rtd_to_codec(fe, 0)->stream_active[stream])
27327083f877SKuninori Morimoto 			continue;
27337083f877SKuninori Morimoto 
27347083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2735618dae11SLiam Girdwood 		if (paths < 0) {
2736103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
27377083f877SKuninori Morimoto 				 fe->dai_link->name,
27387083f877SKuninori Morimoto 				 stream == SNDRV_PCM_STREAM_PLAYBACK ?
27397083f877SKuninori Morimoto 				 "playback" : "capture");
2740618dae11SLiam Girdwood 			return paths;
2741618dae11SLiam Girdwood 		}
2742618dae11SLiam Girdwood 
27437083f877SKuninori Morimoto 		/* update any playback/capture paths */
27447083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2745de15d7ffSJerome Brunet 		if (count) {
2746580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2747de15d7ffSJerome Brunet 			if (new)
2748580dff36SKuninori Morimoto 				ret = dpcm_run_update_startup(fe, stream);
2749de15d7ffSJerome Brunet 			else
2750580dff36SKuninori Morimoto 				ret = dpcm_run_update_shutdown(fe, stream);
2751580dff36SKuninori Morimoto 			if (ret < 0)
2752580dff36SKuninori Morimoto 				dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2753580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2754de15d7ffSJerome Brunet 
27557083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
27567083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2757618dae11SLiam Girdwood 		}
2758618dae11SLiam Girdwood 
27597ed9de76SQiao Zhou 		dpcm_path_put(&list);
2760618dae11SLiam Girdwood 	}
2761618dae11SLiam Girdwood 
2762de15d7ffSJerome Brunet 	return 0;
2763618dae11SLiam Girdwood }
2764618dae11SLiam Girdwood 
2765de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2766de15d7ffSJerome Brunet  * any DAI links.
2767de15d7ffSJerome Brunet  */
2768f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2769de15d7ffSJerome Brunet {
2770de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2771de15d7ffSJerome Brunet 	int ret = 0;
2772de15d7ffSJerome Brunet 
2773de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2774de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2775bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2776de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2777de15d7ffSJerome Brunet 		if (ret)
2778de15d7ffSJerome Brunet 			goto out;
2779de15d7ffSJerome Brunet 	}
2780de15d7ffSJerome Brunet 
2781de15d7ffSJerome Brunet 	/* bring new paths up */
2782bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2783de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2784de15d7ffSJerome Brunet 		if (ret)
2785de15d7ffSJerome Brunet 			goto out;
2786de15d7ffSJerome Brunet 	}
2787de15d7ffSJerome Brunet 
2788de15d7ffSJerome Brunet out:
2789618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2790de15d7ffSJerome Brunet 	return ret;
2791618dae11SLiam Girdwood }
2792f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
279301d7584cSLiam Girdwood 
2794265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
279501d7584cSLiam Girdwood {
279601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
279701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2798265694b6SKuninori Morimoto 	int stream = fe_substream->stream;
279930fca26fSKuninori Morimoto 
280030fca26fSKuninori Morimoto 	/* mark FE's links ready to prune */
280130fca26fSKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
280230fca26fSKuninori Morimoto 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
280330fca26fSKuninori Morimoto 
280430fca26fSKuninori Morimoto 	dpcm_be_disconnect(fe, stream);
280530fca26fSKuninori Morimoto 
280630fca26fSKuninori Morimoto 	fe->dpcm[stream].runtime = NULL;
2807265694b6SKuninori Morimoto }
2808265694b6SKuninori Morimoto 
2809265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2810265694b6SKuninori Morimoto {
2811265694b6SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2812265694b6SKuninori Morimoto 	int ret;
2813265694b6SKuninori Morimoto 
2814265694b6SKuninori Morimoto 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2815265694b6SKuninori Morimoto 	ret = dpcm_fe_dai_shutdown(fe_substream);
2816265694b6SKuninori Morimoto 
2817265694b6SKuninori Morimoto 	dpcm_fe_dai_cleanup(fe_substream);
2818265694b6SKuninori Morimoto 
281930fca26fSKuninori Morimoto 	mutex_unlock(&fe->card->mutex);
282030fca26fSKuninori Morimoto 	return ret;
282130fca26fSKuninori Morimoto }
282230fca26fSKuninori Morimoto 
282301d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
282401d7584cSLiam Girdwood {
282501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
282601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
282701d7584cSLiam Girdwood 	int ret;
282801d7584cSLiam Girdwood 	int stream = fe_substream->stream;
282901d7584cSLiam Girdwood 
283001d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
283101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
283201d7584cSLiam Girdwood 
28338f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
28348f70e515SQiao Zhou 	if (ret < 0) {
2835cae06eb9SKuninori Morimoto 		goto open_end;
28368f70e515SQiao Zhou 	} else if (ret == 0) {
2837103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
283801d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
283901d7584cSLiam Girdwood 	}
284001d7584cSLiam Girdwood 
284101d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
284201d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
284301d7584cSLiam Girdwood 
284401d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
2845265694b6SKuninori Morimoto 	if (ret < 0)
2846265694b6SKuninori Morimoto 		dpcm_fe_dai_cleanup(fe_substream);
284701d7584cSLiam Girdwood 
284801d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
284901d7584cSLiam Girdwood 	dpcm_path_put(&list);
2850cae06eb9SKuninori Morimoto open_end:
285101d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
285201d7584cSLiam Girdwood 	return ret;
285301d7584cSLiam Girdwood }
285401d7584cSLiam Girdwood 
2855ddee627cSLiam Girdwood /* create a new pcm */
2856ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2857ddee627cSLiam Girdwood {
28582e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
285919bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
28602b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
2861ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2862ddee627cSLiam Girdwood 	char new_name[64];
2863ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
28642e5894d7SBenoit Cousson 	int i;
2865ddee627cSLiam Girdwood 
286601d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
28679b5db059SStephan Gerhold 		cpu_dai = asoc_rtd_to_cpu(rtd, 0);
28689b5db059SStephan Gerhold 		if (rtd->num_cpus > 1) {
28699b5db059SStephan Gerhold 			dev_err(rtd->dev,
28709b5db059SStephan Gerhold 				"DPCM doesn't support Multi CPU yet\n");
28719b5db059SStephan Gerhold 			return -EINVAL;
28729b5db059SStephan Gerhold 		}
28739b5db059SStephan Gerhold 
28749b5db059SStephan Gerhold 		playback = rtd->dai_link->dpcm_playback &&
28759b5db059SStephan Gerhold 			   snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK);
28769b5db059SStephan Gerhold 		capture = rtd->dai_link->dpcm_capture &&
28779b5db059SStephan Gerhold 			  snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE);
287801d7584cSLiam Girdwood 	} else {
2879a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2880a4877a6fSStephan Gerhold 		int cpu_capture = rtd->dai_link->params ?
2881a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2882a4877a6fSStephan Gerhold 		int cpu_playback = rtd->dai_link->params ?
2883a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2884a342031cSJerome Brunet 
2885a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
288619bdcc7aSShreyas NC 			if (rtd->num_cpus == 1) {
2887c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, 0);
288819bdcc7aSShreyas NC 			} else if (rtd->num_cpus == rtd->num_codecs) {
2889c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, i);
289019bdcc7aSShreyas NC 			} else {
289119bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
289219bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
289319bdcc7aSShreyas NC 				return -EINVAL;
289419bdcc7aSShreyas NC 			}
289519bdcc7aSShreyas NC 
2896467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2897a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2898ddee627cSLiam Girdwood 				playback = 1;
2899467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2900a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2901ddee627cSLiam Girdwood 				capture = 1;
290201d7584cSLiam Girdwood 		}
29032e5894d7SBenoit Cousson 	}
29042e5894d7SBenoit Cousson 
2905d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2906d6bead02SFabio Estevam 		playback = 1;
2907d6bead02SFabio Estevam 		capture = 0;
2908d6bead02SFabio Estevam 	}
2909d6bead02SFabio Estevam 
2910d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2911d6bead02SFabio Estevam 		playback = 0;
2912d6bead02SFabio Estevam 		capture = 1;
2913d6bead02SFabio Estevam 	}
2914d6bead02SFabio Estevam 
291501d7584cSLiam Girdwood 	/* create the PCM */
2916a342031cSJerome Brunet 	if (rtd->dai_link->params) {
2917a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2918a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2919a342031cSJerome Brunet 
2920a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2921a342031cSJerome Brunet 					   playback, capture, &pcm);
2922a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
292301d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
292401d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
292501d7584cSLiam Girdwood 
292601d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
292701d7584cSLiam Girdwood 				playback, capture, &pcm);
292801d7584cSLiam Girdwood 	} else {
292901d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
293001d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
293101d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
293201d7584cSLiam Girdwood 		else
293301d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
29342e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
29352e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
2936c2233a26SKuninori Morimoto 				"multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
293701d7584cSLiam Girdwood 
293801d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
293901d7584cSLiam Girdwood 			capture, &pcm);
294001d7584cSLiam Girdwood 	}
2941ddee627cSLiam Girdwood 	if (ret < 0) {
2942103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
29435cb9b748SLiam Girdwood 			rtd->dai_link->name);
2944ddee627cSLiam Girdwood 		return ret;
2945ddee627cSLiam Girdwood 	}
2946103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2947ddee627cSLiam Girdwood 
2948ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2949a342031cSJerome Brunet 	if (rtd->dai_link->params)
29504bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2951a342031cSJerome Brunet 	else
295283f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2953ddee627cSLiam Girdwood 
295448c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2955ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2956ddee627cSLiam Girdwood 	pcm->private_data = rtd;
295701d7584cSLiam Girdwood 
2958a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
295901d7584cSLiam Girdwood 		if (playback)
296001d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
296101d7584cSLiam Girdwood 		if (capture)
296201d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
296301d7584cSLiam Girdwood 		goto out;
296401d7584cSLiam Girdwood 	}
296501d7584cSLiam Girdwood 
296601d7584cSLiam Girdwood 	/* ASoC PCM operations */
296701d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
296801d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
296901d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
297001d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
297101d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
297201d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
297301d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
297401d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
297501d7584cSLiam Girdwood 	} else {
297601d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
297701d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
297801d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
297901d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
298001d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
298101d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
298201d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
298301d7584cSLiam Girdwood 	}
298401d7584cSLiam Girdwood 
2985613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
29862b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
2987b8135864SKuninori Morimoto 
29883b1c952cSTakashi Iwai 		if (drv->ioctl)
29893b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
29901e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
29911e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2992e9067bb5SKuninori Morimoto 		if (drv->copy_user)
299382d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2994e9067bb5SKuninori Morimoto 		if (drv->page)
29959c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
2996e9067bb5SKuninori Morimoto 		if (drv->mmap)
2997205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
2998b8135864SKuninori Morimoto 	}
2999b8135864SKuninori Morimoto 
3000ddee627cSLiam Girdwood 	if (playback)
300101d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3002ddee627cSLiam Girdwood 
3003ddee627cSLiam Girdwood 	if (capture)
300401d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3005ddee627cSLiam Girdwood 
3006b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
3007ddee627cSLiam Girdwood 	if (ret < 0) {
30087484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3009ddee627cSLiam Girdwood 		return ret;
3010ddee627cSLiam Girdwood 	}
3011c641e5b2SJohan Hovold 
30123d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
301301d7584cSLiam Girdwood out:
30142e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3015c2233a26SKuninori Morimoto 		 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
3016c2233a26SKuninori Morimoto 		 (rtd->num_cpus > 1)   ? "multicpu"   : asoc_rtd_to_cpu(rtd, 0)->name);
3017ddee627cSLiam Girdwood 	return ret;
3018ddee627cSLiam Girdwood }
301901d7584cSLiam Girdwood 
302001d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
302101d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
302201d7584cSLiam Girdwood {
302301d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
302401d7584cSLiam Girdwood 		return 1;
302501d7584cSLiam Girdwood 	return 0;
302601d7584cSLiam Girdwood }
302701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
302801d7584cSLiam Girdwood 
302901d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
303001d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
303101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
303201d7584cSLiam Girdwood {
303301d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
303401d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
303501d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
303601d7584cSLiam Girdwood 		return 1;
303701d7584cSLiam Girdwood 	return 0;
303801d7584cSLiam Girdwood }
303901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
304001d7584cSLiam Girdwood 
304101d7584cSLiam Girdwood /* get the substream for this BE */
304201d7584cSLiam Girdwood struct snd_pcm_substream *
304301d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
304401d7584cSLiam Girdwood {
304501d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
304601d7584cSLiam Girdwood }
304701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
304801d7584cSLiam Girdwood 
3049085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3050085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
3051085d22beSKuninori Morimoto 				    int stream,
3052085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
3053085d22beSKuninori Morimoto 				    int num_states)
305401d7584cSLiam Girdwood {
305501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
305601d7584cSLiam Girdwood 	int state;
3057a9764869SKaiChieh Chuang 	int ret = 1;
3058a9764869SKaiChieh Chuang 	unsigned long flags;
3059085d22beSKuninori Morimoto 	int i;
306001d7584cSLiam Girdwood 
3061a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3062d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
306301d7584cSLiam Girdwood 
306401d7584cSLiam Girdwood 		if (dpcm->fe == fe)
306501d7584cSLiam Girdwood 			continue;
306601d7584cSLiam Girdwood 
306701d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
3068085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
3069085d22beSKuninori Morimoto 			if (state == states[i]) {
3070a9764869SKaiChieh Chuang 				ret = 0;
3071a9764869SKaiChieh Chuang 				break;
307201d7584cSLiam Girdwood 			}
3073a9764869SKaiChieh Chuang 		}
3074085d22beSKuninori Morimoto 	}
3075a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
307601d7584cSLiam Girdwood 
3077085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
3078a9764869SKaiChieh Chuang 	return ret;
307901d7584cSLiam Girdwood }
3080085d22beSKuninori Morimoto 
3081085d22beSKuninori Morimoto /*
3082085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3083085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3084085d22beSKuninori Morimoto  */
3085085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3086085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3087085d22beSKuninori Morimoto {
3088085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3089085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3090085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3091085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3092085d22beSKuninori Morimoto 	};
3093085d22beSKuninori Morimoto 
3094085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3095085d22beSKuninori Morimoto }
309601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
309701d7584cSLiam Girdwood 
309801d7584cSLiam Girdwood /*
309901d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
310001d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
310101d7584cSLiam Girdwood  */
310201d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
310301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
310401d7584cSLiam Girdwood {
3105085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3106085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3107085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3108085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3109085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3110085d22beSKuninori Morimoto 	};
311101d7584cSLiam Girdwood 
3112085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
311301d7584cSLiam Girdwood }
311401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3115