xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 9f620684c1ef5a002b6622ecc7b5818e81252f48)
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>
27a5e6c109SKuninori Morimoto #include <sound/soc-link.h>
28ddee627cSLiam Girdwood #include <sound/initval.h>
29ddee627cSLiam Girdwood 
30b7898396STakashi Iwai static inline void snd_soc_dpcm_mutex_lock(struct snd_soc_pcm_runtime *rtd)
31b7898396STakashi Iwai {
32b7898396STakashi Iwai 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
33b7898396STakashi Iwai }
34b7898396STakashi Iwai 
35b7898396STakashi Iwai static inline void snd_soc_dpcm_mutex_unlock(struct snd_soc_pcm_runtime *rtd)
36b7898396STakashi Iwai {
37b7898396STakashi Iwai 	mutex_unlock(&rtd->card->pcm_mutex);
38b7898396STakashi Iwai }
39b7898396STakashi Iwai 
40b7898396STakashi Iwai #define snd_soc_dpcm_mutex_assert_held(rtd) \
41b7898396STakashi Iwai 	lockdep_assert_held(&(rtd)->card->pcm_mutex)
42b7898396STakashi Iwai 
43b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
44b7898396STakashi Iwai 						int stream)
45b7898396STakashi Iwai {
46b7898396STakashi Iwai 	snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
47b7898396STakashi Iwai }
48b7898396STakashi Iwai 
493c75c0eaSTakashi Iwai #define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
503c75c0eaSTakashi Iwai 	snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
51b2ae8066STakashi Iwai 
52b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
53b7898396STakashi Iwai 						  int stream)
54b7898396STakashi Iwai {
55b7898396STakashi Iwai 	snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
56b7898396STakashi Iwai }
57b7898396STakashi Iwai 
58b2ae8066STakashi Iwai #define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
59b2ae8066STakashi Iwai 	snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
60b2ae8066STakashi Iwai 
6101d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS	8
6201d7584cSLiam Girdwood 
636fb8944cSKuninori Morimoto static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
646fb8944cSKuninori Morimoto {
656fb8944cSKuninori Morimoto 	return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
666fb8944cSKuninori Morimoto }
676fb8944cSKuninori Morimoto static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
686fb8944cSKuninori Morimoto {
696fb8944cSKuninori Morimoto 	return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
706fb8944cSKuninori Morimoto }
716fb8944cSKuninori Morimoto 
72c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS
73c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
74c3212829SKuninori Morimoto {
75c3212829SKuninori Morimoto 	switch (state) {
76c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_NEW:
77c3212829SKuninori Morimoto 		return "new";
78c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_OPEN:
79c3212829SKuninori Morimoto 		return "open";
80c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_PARAMS:
81c3212829SKuninori Morimoto 		return "hw_params";
82c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PREPARE:
83c3212829SKuninori Morimoto 		return "prepare";
84c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_START:
85c3212829SKuninori Morimoto 		return "start";
86c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_STOP:
87c3212829SKuninori Morimoto 		return "stop";
88c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_SUSPEND:
89c3212829SKuninori Morimoto 		return "suspend";
90c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PAUSED:
91c3212829SKuninori Morimoto 		return "paused";
92c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_FREE:
93c3212829SKuninori Morimoto 		return "hw_free";
94c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_CLOSE:
95c3212829SKuninori Morimoto 		return "close";
96c3212829SKuninori Morimoto 	}
97c3212829SKuninori Morimoto 
98c3212829SKuninori Morimoto 	return "unknown";
99c3212829SKuninori Morimoto }
100c3212829SKuninori Morimoto 
101c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
102c3212829SKuninori Morimoto 			       int stream, char *buf, size_t size)
103c3212829SKuninori Morimoto {
104c3212829SKuninori Morimoto 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
105c3212829SKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
106c3212829SKuninori Morimoto 	ssize_t offset = 0;
107c3212829SKuninori Morimoto 
108c3212829SKuninori Morimoto 	/* FE state */
109d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset,
110c3212829SKuninori Morimoto 			   "[%s - %s]\n", fe->dai_link->name,
111c3212829SKuninori Morimoto 			   stream ? "Capture" : "Playback");
112c3212829SKuninori Morimoto 
113d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset, "State: %s\n",
114c3212829SKuninori Morimoto 			   dpcm_state_string(fe->dpcm[stream].state));
115c3212829SKuninori Morimoto 
116c3212829SKuninori Morimoto 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
117c3212829SKuninori Morimoto 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
118d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
119c3212829SKuninori Morimoto 				   "Hardware Params: "
120c3212829SKuninori Morimoto 				   "Format = %s, Channels = %d, Rate = %d\n",
121c3212829SKuninori Morimoto 				   snd_pcm_format_name(params_format(params)),
122c3212829SKuninori Morimoto 				   params_channels(params),
123c3212829SKuninori Morimoto 				   params_rate(params));
124c3212829SKuninori Morimoto 
125c3212829SKuninori Morimoto 	/* BEs state */
126d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset, "Backends:\n");
127c3212829SKuninori Morimoto 
128c3212829SKuninori Morimoto 	if (list_empty(&fe->dpcm[stream].be_clients)) {
129d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
130c3212829SKuninori Morimoto 				   " No active DSP links\n");
131c3212829SKuninori Morimoto 		goto out;
132c3212829SKuninori Morimoto 	}
133c3212829SKuninori Morimoto 
134c3212829SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
135c3212829SKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
136c3212829SKuninori Morimoto 		params = &dpcm->hw_params;
137c3212829SKuninori Morimoto 
138d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
139c3212829SKuninori Morimoto 				   "- %s\n", be->dai_link->name);
140c3212829SKuninori Morimoto 
141d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
142c3212829SKuninori Morimoto 				   "   State: %s\n",
143c3212829SKuninori Morimoto 				   dpcm_state_string(be->dpcm[stream].state));
144c3212829SKuninori Morimoto 
145c3212829SKuninori Morimoto 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
146c3212829SKuninori Morimoto 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
147d0c9abb8STakashi Iwai 			offset += scnprintf(buf + offset, size - offset,
148c3212829SKuninori Morimoto 					   "   Hardware Params: "
149c3212829SKuninori Morimoto 					   "Format = %s, Channels = %d, Rate = %d\n",
150c3212829SKuninori Morimoto 					   snd_pcm_format_name(params_format(params)),
151c3212829SKuninori Morimoto 					   params_channels(params),
152c3212829SKuninori Morimoto 					   params_rate(params));
153c3212829SKuninori Morimoto 	}
154c3212829SKuninori Morimoto out:
155c3212829SKuninori Morimoto 	return offset;
156c3212829SKuninori Morimoto }
157c3212829SKuninori Morimoto 
158c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
159c3212829SKuninori Morimoto 				    size_t count, loff_t *ppos)
160c3212829SKuninori Morimoto {
161c3212829SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = file->private_data;
162c3212829SKuninori Morimoto 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
163c3212829SKuninori Morimoto 	int stream;
164c3212829SKuninori Morimoto 	char *buf;
165c3212829SKuninori Morimoto 
1666e1276a5SBard Liao 	if (fe->num_cpus > 1) {
1676e1276a5SBard Liao 		dev_err(fe->dev,
1686e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
1696e1276a5SBard Liao 		return -EINVAL;
1706e1276a5SBard Liao 	}
1716e1276a5SBard Liao 
172c3212829SKuninori Morimoto 	buf = kmalloc(out_count, GFP_KERNEL);
173c3212829SKuninori Morimoto 	if (!buf)
174c3212829SKuninori Morimoto 		return -ENOMEM;
175c3212829SKuninori Morimoto 
176b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
177c3212829SKuninori Morimoto 	for_each_pcm_streams(stream)
178c2233a26SKuninori Morimoto 		if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
179c3212829SKuninori Morimoto 			offset += dpcm_show_state(fe, stream,
180c3212829SKuninori Morimoto 						  buf + offset,
181c3212829SKuninori Morimoto 						  out_count - offset);
182b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
183c3212829SKuninori Morimoto 
184c3212829SKuninori Morimoto 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
185c3212829SKuninori Morimoto 
186c3212829SKuninori Morimoto 	kfree(buf);
187c3212829SKuninori Morimoto 	return ret;
188c3212829SKuninori Morimoto }
189c3212829SKuninori Morimoto 
190c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
191c3212829SKuninori Morimoto 	.open = simple_open,
192c3212829SKuninori Morimoto 	.read = dpcm_state_read_file,
193c3212829SKuninori Morimoto 	.llseek = default_llseek,
194c3212829SKuninori Morimoto };
195c3212829SKuninori Morimoto 
196c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
197c3212829SKuninori Morimoto {
198c3212829SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
199c3212829SKuninori Morimoto 		return;
200c3212829SKuninori Morimoto 
201c3212829SKuninori Morimoto 	if (!rtd->card->debugfs_card_root)
202c3212829SKuninori Morimoto 		return;
203c3212829SKuninori Morimoto 
204c3212829SKuninori Morimoto 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
205c3212829SKuninori Morimoto 						    rtd->card->debugfs_card_root);
206c3212829SKuninori Morimoto 
207c3212829SKuninori Morimoto 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
208c3212829SKuninori Morimoto 			    rtd, &dpcm_state_fops);
209c3212829SKuninori Morimoto }
210154dae87SKuninori Morimoto 
211154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
212154dae87SKuninori Morimoto {
213154dae87SKuninori Morimoto 	char *name;
214154dae87SKuninori Morimoto 
215154dae87SKuninori Morimoto 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
216154dae87SKuninori Morimoto 			 stream ? "capture" : "playback");
217154dae87SKuninori Morimoto 	if (name) {
218154dae87SKuninori Morimoto 		dpcm->debugfs_state = debugfs_create_dir(
219154dae87SKuninori Morimoto 			name, dpcm->fe->debugfs_dpcm_root);
220154dae87SKuninori Morimoto 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
221154dae87SKuninori Morimoto 				   &dpcm->state);
222154dae87SKuninori Morimoto 		kfree(name);
223154dae87SKuninori Morimoto 	}
224154dae87SKuninori Morimoto }
225154dae87SKuninori Morimoto 
226154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
227154dae87SKuninori Morimoto {
228154dae87SKuninori Morimoto 	debugfs_remove_recursive(dpcm->debugfs_state);
229154dae87SKuninori Morimoto }
230154dae87SKuninori Morimoto 
231154dae87SKuninori Morimoto #else
232154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
233154dae87SKuninori Morimoto 					     int stream)
234154dae87SKuninori Morimoto {
235154dae87SKuninori Morimoto }
236154dae87SKuninori Morimoto 
237154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
238154dae87SKuninori Morimoto {
239154dae87SKuninori Morimoto }
240c3212829SKuninori Morimoto #endif
241c3212829SKuninori Morimoto 
2429c6d7f93SKuninori Morimoto /* Set FE's runtime_update state; the state is protected via PCM stream lock
2439c6d7f93SKuninori Morimoto  * for avoiding the race with trigger callback.
2449c6d7f93SKuninori Morimoto  * If the state is unset and a trigger is pending while the previous operation,
2459c6d7f93SKuninori Morimoto  * process the pending trigger action here.
2469c6d7f93SKuninori Morimoto  */
2479c6d7f93SKuninori Morimoto static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
2489c6d7f93SKuninori Morimoto static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2499c6d7f93SKuninori Morimoto 				     int stream, enum snd_soc_dpcm_update state)
2509c6d7f93SKuninori Morimoto {
2519c6d7f93SKuninori Morimoto 	struct snd_pcm_substream *substream =
2529c6d7f93SKuninori Morimoto 		snd_soc_dpcm_get_substream(fe, stream);
2539c6d7f93SKuninori Morimoto 
254b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
2559c6d7f93SKuninori Morimoto 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2569c6d7f93SKuninori Morimoto 		dpcm_fe_dai_do_trigger(substream,
2579c6d7f93SKuninori Morimoto 				       fe->dpcm[stream].trigger_pending - 1);
2589c6d7f93SKuninori Morimoto 		fe->dpcm[stream].trigger_pending = 0;
2599c6d7f93SKuninori Morimoto 	}
2609c6d7f93SKuninori Morimoto 	fe->dpcm[stream].runtime_update = state;
261b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
2629c6d7f93SKuninori Morimoto }
2639c6d7f93SKuninori Morimoto 
264a7e20444SKuninori Morimoto static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
265a7e20444SKuninori Morimoto 				     int stream, enum snd_soc_dpcm_update state)
266a7e20444SKuninori Morimoto {
267a7e20444SKuninori Morimoto 	be->dpcm[stream].runtime_update = state;
268a7e20444SKuninori Morimoto }
269a7e20444SKuninori Morimoto 
270d9051d86SKuninori Morimoto /**
271d9051d86SKuninori Morimoto  * snd_soc_runtime_action() - Increment/Decrement active count for
272d9051d86SKuninori Morimoto  * PCM runtime components
273d9051d86SKuninori Morimoto  * @rtd: ASoC PCM runtime that is activated
274d9051d86SKuninori Morimoto  * @stream: Direction of the PCM stream
275b6d6e9eaSColton Lewis  * @action: Activate stream if 1. Deactivate if -1.
276d9051d86SKuninori Morimoto  *
277d9051d86SKuninori Morimoto  * Increments/Decrements the active count for all the DAIs and components
278d9051d86SKuninori Morimoto  * attached to a PCM runtime.
279d9051d86SKuninori Morimoto  * Should typically be called when a stream is opened.
280d9051d86SKuninori Morimoto  *
281d9051d86SKuninori Morimoto  * Must be called with the rtd->card->pcm_mutex being held
282d9051d86SKuninori Morimoto  */
283d9051d86SKuninori Morimoto void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2847a5aaba4SKuninori Morimoto 			    int stream, int action)
2857a5aaba4SKuninori Morimoto {
286c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
2877a5aaba4SKuninori Morimoto 	int i;
2887a5aaba4SKuninori Morimoto 
289b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
2907a5aaba4SKuninori Morimoto 
291dc829106SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
292dc829106SKuninori Morimoto 		snd_soc_dai_action(dai, stream, action);
2937a5aaba4SKuninori Morimoto }
294d9051d86SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
29524894b76SLars-Peter Clausen 
29624894b76SLars-Peter Clausen /**
297208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
298208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
299208a1589SLars-Peter Clausen  *
300208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
301208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
302208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
303208a1589SLars-Peter Clausen  * from having the delay.
304208a1589SLars-Peter Clausen  */
305208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
306208a1589SLars-Peter Clausen {
307fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
3082e5894d7SBenoit Cousson 	bool ignore = true;
309613fb500SKuninori Morimoto 	int i;
3102e5894d7SBenoit Cousson 
311208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
312208a1589SLars-Peter Clausen 		return true;
313208a1589SLars-Peter Clausen 
314613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
31572c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
316fbb16563SKuninori Morimoto 
317fbb16563SKuninori Morimoto 	return ignore;
318208a1589SLars-Peter Clausen }
319208a1589SLars-Peter Clausen 
320208a1589SLars-Peter Clausen /**
32190996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
32290996f43SLars-Peter Clausen  * @substream: the pcm substream
32390996f43SLars-Peter Clausen  * @hw: the hardware parameters
32490996f43SLars-Peter Clausen  *
32590996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
32690996f43SLars-Peter Clausen  */
32790996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
32890996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
32990996f43SLars-Peter Clausen {
33056e749baSKuninori Morimoto 	substream->runtime->hw = *hw;
33156e749baSKuninori Morimoto 
33290996f43SLars-Peter Clausen 	return 0;
33390996f43SLars-Peter Clausen }
33490996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
33590996f43SLars-Peter Clausen 
33601d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
33723607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
33801d7584cSLiam Girdwood 	int event)
33901d7584cSLiam Girdwood {
34001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
34101d7584cSLiam Girdwood 
342b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
343b7898396STakashi Iwai 
3448d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
34501d7584cSLiam Girdwood 
34601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
34701d7584cSLiam Girdwood 
348103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
34901d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
35001d7584cSLiam Girdwood 
351b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
352b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
353b1cd2e34SBanajit Goswami 			continue;
354b1cd2e34SBanajit Goswami 
35501d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
35601d7584cSLiam Girdwood 	}
35701d7584cSLiam Girdwood 
35801d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
35901d7584cSLiam Girdwood 
36001d7584cSLiam Girdwood 	return 0;
36101d7584cSLiam Girdwood }
36201d7584cSLiam Girdwood 
3632805b8bdSKuninori Morimoto static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
3642805b8bdSKuninori Morimoto 				   struct snd_pcm_hw_params *params)
3652805b8bdSKuninori Morimoto {
3662805b8bdSKuninori Morimoto 	if (params) {
3672805b8bdSKuninori Morimoto 		dai->rate	 = params_rate(params);
3682805b8bdSKuninori Morimoto 		dai->channels	 = params_channels(params);
3692805b8bdSKuninori Morimoto 		dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
3702805b8bdSKuninori Morimoto 	} else {
3712805b8bdSKuninori Morimoto 		dai->rate	 = 0;
3722805b8bdSKuninori Morimoto 		dai->channels	 = 0;
3732805b8bdSKuninori Morimoto 		dai->sample_bits = 0;
3742805b8bdSKuninori Morimoto 	}
3752805b8bdSKuninori Morimoto }
3762805b8bdSKuninori Morimoto 
37717841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
37817841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
379ddee627cSLiam Girdwood {
3800ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
381ddee627cSLiam Girdwood 	int ret;
382ddee627cSLiam Girdwood 
383f8fc9ec5SKuninori Morimoto 	if (!snd_soc_dai_active(soc_dai))
384f8fc9ec5SKuninori Morimoto 		return 0;
385f8fc9ec5SKuninori Morimoto 
386fac110cbSKuninori Morimoto #define __soc_pcm_apply_symmetry(name, NAME)				\
387fac110cbSKuninori Morimoto 	if (soc_dai->name && (soc_dai->driver->symmetric_##name ||	\
388fac110cbSKuninori Morimoto 			      rtd->dai_link->symmetric_##name)) {	\
389fac110cbSKuninori Morimoto 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
390fac110cbSKuninori Morimoto 			#name, soc_dai->name);				\
391fac110cbSKuninori Morimoto 									\
392fac110cbSKuninori Morimoto 		ret = snd_pcm_hw_constraint_single(substream->runtime,	\
393fac110cbSKuninori Morimoto 						   SNDRV_PCM_HW_PARAM_##NAME,\
394fac110cbSKuninori Morimoto 						   soc_dai->name);	\
395fac110cbSKuninori Morimoto 		if (ret < 0) {						\
396fac110cbSKuninori Morimoto 			dev_err(soc_dai->dev,				\
397fac110cbSKuninori Morimoto 				"ASoC: Unable to apply %s constraint: %d\n",\
398fac110cbSKuninori Morimoto 				#name, ret);				\
399fac110cbSKuninori Morimoto 			return ret;					\
400fac110cbSKuninori Morimoto 		}							\
4013635bf09SNicolin Chen 	}
4023635bf09SNicolin Chen 
403fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(rate,		RATE);
404fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(channels,	CHANNELS);
405fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(sample_bits,	SAMPLE_BITS);
406ddee627cSLiam Girdwood 
407ddee627cSLiam Girdwood 	return 0;
408ddee627cSLiam Girdwood }
409ddee627cSLiam Girdwood 
4103635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4113635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4123635bf09SNicolin Chen {
4130ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4142805b8bdSKuninori Morimoto 	struct snd_soc_dai d;
415c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
41619bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4172805b8bdSKuninori Morimoto 	unsigned int symmetry, i;
4183635bf09SNicolin Chen 
419ee39d77eSKuninori Morimoto 	d.name = __func__;
4202805b8bdSKuninori Morimoto 	soc_pcm_set_dai_params(&d, params);
4213635bf09SNicolin Chen 
4221cacbac4SKuninori Morimoto #define __soc_pcm_params_symmetry(xxx)					\
4231cacbac4SKuninori Morimoto 	symmetry = rtd->dai_link->symmetric_##xxx;			\
4243a906721SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)					\
4251cacbac4SKuninori Morimoto 		symmetry |= dai->driver->symmetric_##xxx;		\
4263a906721SKuninori Morimoto 									\
4273a906721SKuninori Morimoto 	if (symmetry)							\
4283a906721SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai)			\
4299c2ae363SKuninori Morimoto 			if (!snd_soc_dai_is_dummy(cpu_dai) &&		\
4309c2ae363SKuninori Morimoto 			    cpu_dai->xxx && cpu_dai->xxx != d.xxx) {	\
431ee39d77eSKuninori Morimoto 				dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
432ee39d77eSKuninori Morimoto 					#xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
4333a906721SKuninori Morimoto 				return -EINVAL;				\
4343a906721SKuninori Morimoto 			}
4353a906721SKuninori Morimoto 
4363635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
4373a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(rate);
4383a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(channels);
4393a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(sample_bits);
440ddee627cSLiam Girdwood 
441ddee627cSLiam Girdwood 	return 0;
442ddee627cSLiam Girdwood }
443ddee627cSLiam Girdwood 
44468cbc557SKuninori Morimoto static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
44562e5f676SLars-Peter Clausen {
4460ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
44762e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
448c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
4492e5894d7SBenoit Cousson 	unsigned int symmetry, i;
45062e5f676SLars-Peter Clausen 
451f14654ddSKuninori Morimoto 	symmetry = link->symmetric_rate ||
45219bdcc7aSShreyas NC 		link->symmetric_channels ||
453f14654ddSKuninori Morimoto 		link->symmetric_sample_bits;
45419bdcc7aSShreyas NC 
455c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
45619bdcc7aSShreyas NC 		symmetry = symmetry ||
457f14654ddSKuninori Morimoto 			dai->driver->symmetric_rate ||
458c840f769SKuninori Morimoto 			dai->driver->symmetric_channels ||
459f14654ddSKuninori Morimoto 			dai->driver->symmetric_sample_bits;
4602e5894d7SBenoit Cousson 
46168cbc557SKuninori Morimoto 	if (symmetry)
46268cbc557SKuninori Morimoto 		substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
46362e5f676SLars-Peter Clausen }
46462e5f676SLars-Peter Clausen 
4652e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
46658ba9b25SMark Brown {
4670ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
468c6068d3aSTakashi Iwai 	int ret;
46958ba9b25SMark Brown 
47058ba9b25SMark Brown 	if (!bits)
47158ba9b25SMark Brown 		return;
47258ba9b25SMark Brown 
4730e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
47458ba9b25SMark Brown 	if (ret != 0)
4750e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
4760e2a3751SLars-Peter Clausen 				 bits, ret);
47758ba9b25SMark Brown }
47858ba9b25SMark Brown 
479c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
480bd477c31SLars-Peter Clausen {
4810ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
48219bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4832e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
48457be9206SKuninori Morimoto 	int stream = substream->stream;
4852e5894d7SBenoit Cousson 	int i;
48619bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
487c8dd1fecSBenoit Cousson 
488a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
4892bc3e1f2SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
49057be9206SKuninori Morimoto 
49157be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
4922e5894d7SBenoit Cousson 			bits = 0;
4932e5894d7SBenoit Cousson 			break;
4942e5894d7SBenoit Cousson 		}
49557be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
4962e5894d7SBenoit Cousson 	}
49757be9206SKuninori Morimoto 
498a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
4992bc3e1f2SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
50019bdcc7aSShreyas NC 
50119bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
50219bdcc7aSShreyas NC 			cpu_bits = 0;
50319bdcc7aSShreyas NC 			break;
50419bdcc7aSShreyas NC 		}
50519bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
50619bdcc7aSShreyas NC 	}
507c8dd1fecSBenoit Cousson 
5082e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5092e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
510c8dd1fecSBenoit Cousson }
511c8dd1fecSBenoit Cousson 
512f6c04af5SKuninori Morimoto static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
513f6c04af5SKuninori Morimoto {
514f6c04af5SKuninori Morimoto 	hw->rates		= UINT_MAX;
515f6c04af5SKuninori Morimoto 	hw->rate_min		= 0;
516f6c04af5SKuninori Morimoto 	hw->rate_max		= UINT_MAX;
5176cb56a45SKuninori Morimoto 	hw->channels_min	= 0;
5186cb56a45SKuninori Morimoto 	hw->channels_max	= UINT_MAX;
519debc71f2SKuninori Morimoto 	hw->formats		= ULLONG_MAX;
520f6c04af5SKuninori Morimoto }
521f6c04af5SKuninori Morimoto 
522f6c04af5SKuninori Morimoto static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
523f6c04af5SKuninori Morimoto 				   struct snd_soc_pcm_stream *p)
524f6c04af5SKuninori Morimoto {
525f6c04af5SKuninori Morimoto 	hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
526f6c04af5SKuninori Morimoto 
527f6c04af5SKuninori Morimoto 	/* setup hw->rate_min/max via hw->rates first */
528f6c04af5SKuninori Morimoto 	snd_pcm_hw_limit_rates(hw);
529f6c04af5SKuninori Morimoto 
530f6c04af5SKuninori Morimoto 	/* update hw->rate_min/max by snd_soc_pcm_stream */
531f6c04af5SKuninori Morimoto 	hw->rate_min = max(hw->rate_min, p->rate_min);
532f6c04af5SKuninori Morimoto 	hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
533f6c04af5SKuninori Morimoto }
534f6c04af5SKuninori Morimoto 
5356cb56a45SKuninori Morimoto static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
5366cb56a45SKuninori Morimoto 				   struct snd_soc_pcm_stream *p)
5376cb56a45SKuninori Morimoto {
5386cb56a45SKuninori Morimoto 	hw->channels_min = max(hw->channels_min, p->channels_min);
5396cb56a45SKuninori Morimoto 	hw->channels_max = min(hw->channels_max, p->channels_max);
5406cb56a45SKuninori Morimoto }
5416cb56a45SKuninori Morimoto 
542debc71f2SKuninori Morimoto static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
543debc71f2SKuninori Morimoto 				     struct snd_soc_pcm_stream *p)
544debc71f2SKuninori Morimoto {
545debc71f2SKuninori Morimoto 	hw->formats &= p->formats;
546debc71f2SKuninori Morimoto }
547debc71f2SKuninori Morimoto 
5485854a464SSamuel Holland /**
5495854a464SSamuel Holland  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
5505854a464SSamuel Holland  * @rtd: ASoC PCM runtime
5515854a464SSamuel Holland  * @hw: PCM hardware parameters (output)
5525854a464SSamuel Holland  * @stream: Direction of the PCM stream
5535854a464SSamuel Holland  *
5545854a464SSamuel Holland  * Calculates the subset of stream parameters supported by all DAIs
5555854a464SSamuel Holland  * associated with the PCM stream.
5565854a464SSamuel Holland  */
5575854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
5585854a464SSamuel Holland 			    struct snd_pcm_hardware *hw, int stream)
559bd477c31SLars-Peter Clausen {
5600b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
56119bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5622e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5632e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
56419bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5652e5894d7SBenoit Cousson 	int i;
56678e45c99SLars-Peter Clausen 
567f6c04af5SKuninori Morimoto 	soc_pcm_hw_init(hw);
568f6c04af5SKuninori Morimoto 
56919bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
570a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5710e9cf4c4SBard Liao 
5720e9cf4c4SBard Liao 		/*
5730e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream type.
5740e9cf4c4SBard Liao 		 * Otherwise, since the rate, channel, and format values will
5750e9cf4c4SBard Liao 		 * zero in that case, we would have no usable settings left,
5760e9cf4c4SBard Liao 		 * causing the resulting setup to fail.
5770e9cf4c4SBard Liao 		 */
5785854a464SSamuel Holland 		if (!snd_soc_dai_stream_valid(cpu_dai, stream))
5790e9cf4c4SBard Liao 			continue;
5800e9cf4c4SBard Liao 
58119bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
58278e45c99SLars-Peter Clausen 
5836cb56a45SKuninori Morimoto 		soc_pcm_hw_update_chan(hw, cpu_stream);
584f6c04af5SKuninori Morimoto 		soc_pcm_hw_update_rate(hw, cpu_stream);
585debc71f2SKuninori Morimoto 		soc_pcm_hw_update_format(hw, cpu_stream);
58619bdcc7aSShreyas NC 	}
5876cb56a45SKuninori Morimoto 	cpu_chan_min = hw->channels_min;
5886cb56a45SKuninori Morimoto 	cpu_chan_max = hw->channels_max;
58919bdcc7aSShreyas NC 
59019bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
591a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
592cde79035SRicard Wanderlof 
593cde79035SRicard Wanderlof 		/*
594cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
595cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
596cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
597cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
598cde79035SRicard Wanderlof 		 */
59925c2f515SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
600cde79035SRicard Wanderlof 			continue;
601cde79035SRicard Wanderlof 
602acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
603acf253c1SKuninori Morimoto 
6046cb56a45SKuninori Morimoto 		soc_pcm_hw_update_chan(hw, codec_stream);
605f6c04af5SKuninori Morimoto 		soc_pcm_hw_update_rate(hw, codec_stream);
606debc71f2SKuninori Morimoto 		soc_pcm_hw_update_format(hw, codec_stream);
6072e5894d7SBenoit Cousson 	}
6082e5894d7SBenoit Cousson 
6095854a464SSamuel Holland 	/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6106cb56a45SKuninori Morimoto 	if (!hw->channels_min)
6115854a464SSamuel Holland 		return -EINVAL;
6125854a464SSamuel Holland 
6132e5894d7SBenoit Cousson 	/*
6142e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
61519bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6162e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6172e5894d7SBenoit Cousson 	 */
6182e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
6196cb56a45SKuninori Morimoto 		hw->channels_min = cpu_chan_min;
6206cb56a45SKuninori Morimoto 		hw->channels_max = cpu_chan_max;
6212e5894d7SBenoit Cousson 	}
6222e5894d7SBenoit Cousson 
6235854a464SSamuel Holland 	return 0;
6245854a464SSamuel Holland }
6255854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
6265854a464SSamuel Holland 
6275854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
6285854a464SSamuel Holland {
6295854a464SSamuel Holland 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
6300ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
6315854a464SSamuel Holland 	u64 formats = hw->formats;
6325854a464SSamuel Holland 
6335854a464SSamuel Holland 	/*
6345854a464SSamuel Holland 	 * At least one CPU and one CODEC should match. Otherwise, we should
6355854a464SSamuel Holland 	 * have bailed out on a higher level, since there would be no CPU or
6365854a464SSamuel Holland 	 * CODEC to support the transfer direction in that case.
6375854a464SSamuel Holland 	 */
6385854a464SSamuel Holland 	snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
6395854a464SSamuel Holland 
6405854a464SSamuel Holland 	if (formats)
6415854a464SSamuel Holland 		hw->formats &= formats;
642bd477c31SLars-Peter Clausen }
643bd477c31SLars-Peter Clausen 
644dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
645e7ecfdb7SKuninori Morimoto {
6460ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
647e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
648613fb500SKuninori Morimoto 	int i, ret = 0;
649e7ecfdb7SKuninori Morimoto 
650613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
65151aff91aSKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component, substream);
652bcae1631SKuninori Morimoto 		if (ret < 0)
653d2aaa8d8SKai Vehmanen 			break;
654e7ecfdb7SKuninori Morimoto 
655ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
656bcae1631SKuninori Morimoto 		if (ret < 0)
657d2aaa8d8SKai Vehmanen 			break;
658e7ecfdb7SKuninori Morimoto 	}
659dd03907bSKuninori Morimoto 
660d2aaa8d8SKai Vehmanen 	return ret;
661e7ecfdb7SKuninori Morimoto }
662e7ecfdb7SKuninori Morimoto 
66351aff91aSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream,
66451aff91aSKuninori Morimoto 				    int rollback)
665244e2936SCharles Keepax {
6660ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
667244e2936SCharles Keepax 	struct snd_soc_component *component;
66833be10b5SKuninori Morimoto 	int i, ret = 0;
669244e2936SCharles Keepax 
670613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
67133be10b5SKuninori Morimoto 		int r = snd_soc_component_close(component, substream, rollback);
672e82ebffcSKuninori Morimoto 		if (r < 0)
673e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
674e82ebffcSKuninori Morimoto 
67551aff91aSKuninori Morimoto 		snd_soc_component_module_put_when_close(component, substream, rollback);
676244e2936SCharles Keepax 	}
677244e2936SCharles Keepax 
6783672beb8SKuninori Morimoto 	return ret;
679244e2936SCharles Keepax }
680244e2936SCharles Keepax 
681b7898396STakashi Iwai static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
682b7898396STakashi Iwai 			 struct snd_pcm_substream *substream, int rollback)
68362c86d1dSKuninori Morimoto {
68462c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
685c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
68662c86d1dSKuninori Morimoto 	int i;
68762c86d1dSKuninori Morimoto 
688b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
68962c86d1dSKuninori Morimoto 
690140a4532SKuninori Morimoto 	if (!rollback)
69162c86d1dSKuninori Morimoto 		snd_soc_runtime_deactivate(rtd, substream->stream);
69262c86d1dSKuninori Morimoto 
693c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
694140a4532SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream, rollback);
69562c86d1dSKuninori Morimoto 
696140a4532SKuninori Morimoto 	snd_soc_link_shutdown(substream, rollback);
69762c86d1dSKuninori Morimoto 
698140a4532SKuninori Morimoto 	soc_pcm_components_close(substream, rollback);
69962c86d1dSKuninori Morimoto 
700140a4532SKuninori Morimoto 	snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
70162c86d1dSKuninori Morimoto 
70262c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
703b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
70462c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
70562c86d1dSKuninori Morimoto 
70662c86d1dSKuninori Morimoto 	return 0;
70762c86d1dSKuninori Morimoto }
70862c86d1dSKuninori Morimoto 
70962c86d1dSKuninori Morimoto /*
710140a4532SKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
711140a4532SKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
712140a4532SKuninori Morimoto  * shutdown.
713140a4532SKuninori Morimoto  */
714b7898396STakashi Iwai static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
715b7898396STakashi Iwai 			   struct snd_pcm_substream *substream)
716b7898396STakashi Iwai {
717b7898396STakashi Iwai 	return soc_pcm_clean(rtd, substream, 0);
718b7898396STakashi Iwai }
719b7898396STakashi Iwai 
720b7898396STakashi Iwai /* PCM close ops for non-DPCM streams */
721140a4532SKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
722140a4532SKuninori Morimoto {
723b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
724b7898396STakashi Iwai 
725b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
726b7898396STakashi Iwai 	soc_pcm_clean(rtd, substream, 0);
727b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
728b7898396STakashi Iwai 	return 0;
729140a4532SKuninori Morimoto }
730140a4532SKuninori Morimoto 
731c393281aSKuninori Morimoto static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
732c393281aSKuninori Morimoto {
733c393281aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
734c393281aSKuninori Morimoto 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
735c393281aSKuninori Morimoto 	const char *name_cpu = soc_cpu_dai_name(rtd);
736c393281aSKuninori Morimoto 	const char *name_codec = soc_codec_dai_name(rtd);
737c393281aSKuninori Morimoto 	const char *err_msg;
738c393281aSKuninori Morimoto 	struct device *dev = rtd->dev;
739c393281aSKuninori Morimoto 
740c393281aSKuninori Morimoto 	err_msg = "rates";
741c393281aSKuninori Morimoto 	if (!hw->rates)
742c393281aSKuninori Morimoto 		goto config_err;
743c393281aSKuninori Morimoto 
744c393281aSKuninori Morimoto 	err_msg = "formats";
745c393281aSKuninori Morimoto 	if (!hw->formats)
746c393281aSKuninori Morimoto 		goto config_err;
747c393281aSKuninori Morimoto 
748c393281aSKuninori Morimoto 	err_msg = "channels";
749c393281aSKuninori Morimoto 	if (!hw->channels_min || !hw->channels_max ||
750c393281aSKuninori Morimoto 	     hw->channels_min  >  hw->channels_max)
751c393281aSKuninori Morimoto 		goto config_err;
752c393281aSKuninori Morimoto 
753c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: %s <-> %s info:\n",		name_codec,
754c393281aSKuninori Morimoto 							name_cpu);
755c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: rate mask 0x%x\n",		hw->rates);
756c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: ch   min %d max %d\n",	hw->channels_min,
757c393281aSKuninori Morimoto 							hw->channels_max);
758c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: rate min %d max %d\n",	hw->rate_min,
759c393281aSKuninori Morimoto 							hw->rate_max);
760c393281aSKuninori Morimoto 
761c393281aSKuninori Morimoto 	return 0;
762c393281aSKuninori Morimoto 
763c393281aSKuninori Morimoto config_err:
764c393281aSKuninori Morimoto 	dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
765c393281aSKuninori Morimoto 		name_codec, name_cpu, err_msg);
766c393281aSKuninori Morimoto 	return -EINVAL;
767c393281aSKuninori Morimoto }
768c393281aSKuninori Morimoto 
769140a4532SKuninori Morimoto /*
770ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
771ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
772ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
773ddee627cSLiam Girdwood  */
774b7898396STakashi Iwai static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
775b7898396STakashi Iwai 			  struct snd_pcm_substream *substream)
776ddee627cSLiam Girdwood {
77790be711eSKuninori Morimoto 	struct snd_soc_component *component;
778c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
779244e2936SCharles Keepax 	int i, ret = 0;
780ddee627cSLiam Girdwood 
781b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
782b7898396STakashi Iwai 
78376c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
78476c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
78590be711eSKuninori Morimoto 
786939a5cfbSKuninori Morimoto 	ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
787939a5cfbSKuninori Morimoto 	if (ret < 0)
788b7898396STakashi Iwai 		goto err;
789ddee627cSLiam Girdwood 
7905d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
7915d9fa03eSKuninori Morimoto 	if (ret < 0)
792140a4532SKuninori Morimoto 		goto err;
7935d9fa03eSKuninori Morimoto 
7947cf3c5b4SKuninori Morimoto 	ret = snd_soc_link_startup(substream);
795a5e6c109SKuninori Morimoto 	if (ret < 0)
796140a4532SKuninori Morimoto 		goto err;
7975d9fa03eSKuninori Morimoto 
798ddee627cSLiam Girdwood 	/* startup the audio subsystem */
799c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
800c840f769SKuninori Morimoto 		ret = snd_soc_dai_startup(dai, substream);
801ce820145SKuninori Morimoto 		if (ret < 0)
802140a4532SKuninori Morimoto 			goto err;
803ddee627cSLiam Girdwood 
8042e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
805c840f769SKuninori Morimoto 			dai->tx_mask = 0;
8062e5894d7SBenoit Cousson 		else
807c840f769SKuninori Morimoto 			dai->rx_mask = 0;
8082e5894d7SBenoit Cousson 	}
8092e5894d7SBenoit Cousson 
81001d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
81101d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
81201d7584cSLiam Girdwood 		goto dynamic;
81301d7584cSLiam Girdwood 
814ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8152e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8162e5894d7SBenoit Cousson 
81768cbc557SKuninori Morimoto 	soc_pcm_update_symmetry(substream);
81862e5f676SLars-Peter Clausen 
819c393281aSKuninori Morimoto 	ret = soc_hw_sanity_check(substream);
820c393281aSKuninori Morimoto 	if (ret < 0)
821140a4532SKuninori Morimoto 		goto err;
822ddee627cSLiam Girdwood 
823c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
82458ba9b25SMark Brown 
825ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
826c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
827c840f769SKuninori Morimoto 		ret = soc_pcm_apply_symmetry(substream, dai);
828ddee627cSLiam Girdwood 		if (ret != 0)
829140a4532SKuninori Morimoto 			goto err;
830ddee627cSLiam Girdwood 	}
83101d7584cSLiam Girdwood dynamic:
83224894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
8338e7875aeSKuninori Morimoto 	ret = 0;
834140a4532SKuninori Morimoto err:
835e4b044f4SKuninori Morimoto 	if (ret < 0) {
836b7898396STakashi Iwai 		soc_pcm_clean(rtd, substream, 1);
837e4b044f4SKuninori Morimoto 		dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
838e4b044f4SKuninori Morimoto 	}
839d6652ef8SMark Brown 
840ddee627cSLiam Girdwood 	return ret;
841ddee627cSLiam Girdwood }
842ddee627cSLiam Girdwood 
843b7898396STakashi Iwai /* PCM open ops for non-DPCM streams */
844b7898396STakashi Iwai static int soc_pcm_open(struct snd_pcm_substream *substream)
845b7898396STakashi Iwai {
846b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
847b7898396STakashi Iwai 	int ret;
848b7898396STakashi Iwai 
849b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
850b7898396STakashi Iwai 	ret = __soc_pcm_open(rtd, substream);
851b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
852b7898396STakashi Iwai 	return ret;
853b7898396STakashi Iwai }
854b7898396STakashi Iwai 
8554bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
856a342031cSJerome Brunet {
857a342031cSJerome Brunet 	/*
858a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
859a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
860a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
861a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
862a342031cSJerome Brunet 	 */
863a342031cSJerome Brunet }
864a342031cSJerome Brunet 
865ddee627cSLiam Girdwood /*
866ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
867ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
868ddee627cSLiam Girdwood  * it can refer to the runtime info.
869ddee627cSLiam Girdwood  */
870b7898396STakashi Iwai static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
871b7898396STakashi Iwai 			     struct snd_pcm_substream *substream)
872ddee627cSLiam Girdwood {
873c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
8742e5894d7SBenoit Cousson 	int i, ret = 0;
875ddee627cSLiam Girdwood 
876b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
877ddee627cSLiam Girdwood 
8787cf3c5b4SKuninori Morimoto 	ret = snd_soc_link_prepare(substream);
879a5e6c109SKuninori Morimoto 	if (ret < 0)
880ddee627cSLiam Girdwood 		goto out;
881ddee627cSLiam Girdwood 
8824f39514fSKuninori Morimoto 	ret = snd_soc_pcm_component_prepare(substream);
8834f39514fSKuninori Morimoto 	if (ret < 0)
884b8135864SKuninori Morimoto 		goto out;
885b8135864SKuninori Morimoto 
886d108c7fdSKuninori Morimoto 	ret = snd_soc_pcm_dai_prepare(substream);
88762462e01SKuninori Morimoto 	if (ret < 0)
888ddee627cSLiam Girdwood 		goto out;
889ddee627cSLiam Girdwood 
890ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
891ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
8929bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
8939bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
894ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
895ddee627cSLiam Girdwood 	}
896ddee627cSLiam Girdwood 
897d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
898ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
899ddee627cSLiam Girdwood 
900c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
901c840f769SKuninori Morimoto 		snd_soc_dai_digital_mute(dai, 0, substream->stream);
902ddee627cSLiam Girdwood 
903ddee627cSLiam Girdwood out:
904dab7eeb4SKuninori Morimoto 	if (ret < 0)
905dab7eeb4SKuninori Morimoto 		dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
906dab7eeb4SKuninori Morimoto 
907ddee627cSLiam Girdwood 	return ret;
908ddee627cSLiam Girdwood }
909ddee627cSLiam Girdwood 
910b7898396STakashi Iwai /* PCM prepare ops for non-DPCM streams */
911b7898396STakashi Iwai static int soc_pcm_prepare(struct snd_pcm_substream *substream)
912b7898396STakashi Iwai {
913b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
914b7898396STakashi Iwai 	int ret;
915b7898396STakashi Iwai 
916b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
917b7898396STakashi Iwai 	ret = __soc_pcm_prepare(rtd, substream);
918b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
919b7898396STakashi Iwai 	return ret;
920b7898396STakashi Iwai }
921b7898396STakashi Iwai 
9222e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
9232e5894d7SBenoit Cousson 				       unsigned int mask)
9242e5894d7SBenoit Cousson {
9252e5894d7SBenoit Cousson 	struct snd_interval *interval;
9262e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
9272e5894d7SBenoit Cousson 
9282e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
9292e5894d7SBenoit Cousson 	interval->min = channels;
9302e5894d7SBenoit Cousson 	interval->max = channels;
9312e5894d7SBenoit Cousson }
9322e5894d7SBenoit Cousson 
933b7898396STakashi Iwai static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
934b7898396STakashi Iwai 			    struct snd_pcm_substream *substream, int rollback)
935ab49436eSKuninori Morimoto {
936ab49436eSKuninori Morimoto 	struct snd_soc_dai *dai;
937ab49436eSKuninori Morimoto 	int i;
938ab49436eSKuninori Morimoto 
939b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
940ab49436eSKuninori Morimoto 
941ab49436eSKuninori Morimoto 	/* clear the corresponding DAIs parameters when going to be inactive */
942ab49436eSKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
9432805b8bdSKuninori Morimoto 		if (snd_soc_dai_active(dai) == 1)
9442805b8bdSKuninori Morimoto 			soc_pcm_set_dai_params(dai, NULL);
945ab49436eSKuninori Morimoto 
94686e4aef6SKuninori Morimoto 		if (snd_soc_dai_stream_active(dai, substream->stream) == 1)
947ab49436eSKuninori Morimoto 			snd_soc_dai_digital_mute(dai, 1, substream->stream);
948ab49436eSKuninori Morimoto 	}
949ab49436eSKuninori Morimoto 
950a27b421fSRanjani Sridharan 	/* run the stream event */
951a27b421fSRanjani Sridharan 	snd_soc_dapm_stream_stop(rtd, substream->stream);
952a27b421fSRanjani Sridharan 
953ab49436eSKuninori Morimoto 	/* free any machine hw params */
9544662c596SKuninori Morimoto 	snd_soc_link_hw_free(substream, rollback);
955ab49436eSKuninori Morimoto 
956ab49436eSKuninori Morimoto 	/* free any component resources */
9574662c596SKuninori Morimoto 	snd_soc_pcm_component_hw_free(substream, rollback);
958ab49436eSKuninori Morimoto 
959ab49436eSKuninori Morimoto 	/* now free hw params for the DAIs  */
960121966d0SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
961121966d0SKuninori Morimoto 		if (snd_soc_dai_stream_valid(dai, substream->stream))
9624662c596SKuninori Morimoto 			snd_soc_dai_hw_free(dai, substream, rollback);
963ab49436eSKuninori Morimoto 
964ab49436eSKuninori Morimoto 	return 0;
965ab49436eSKuninori Morimoto }
966ab49436eSKuninori Morimoto 
967ab49436eSKuninori Morimoto /*
9684662c596SKuninori Morimoto  * Frees resources allocated by hw_params, can be called multiple times
9694662c596SKuninori Morimoto  */
970b7898396STakashi Iwai static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
971b7898396STakashi Iwai 			     struct snd_pcm_substream *substream)
972b7898396STakashi Iwai {
973b7898396STakashi Iwai 	return soc_pcm_hw_clean(rtd, substream, 0);
974b7898396STakashi Iwai }
975b7898396STakashi Iwai 
976b7898396STakashi Iwai /* hw_free PCM ops for non-DPCM streams */
9774662c596SKuninori Morimoto static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
9784662c596SKuninori Morimoto {
979b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
980b7898396STakashi Iwai 	int ret;
981b7898396STakashi Iwai 
982b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
983b7898396STakashi Iwai 	ret = __soc_pcm_hw_free(rtd, substream);
984b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
985b7898396STakashi Iwai 	return ret;
9864662c596SKuninori Morimoto }
9874662c596SKuninori Morimoto 
9884662c596SKuninori Morimoto /*
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  */
993b7898396STakashi Iwai static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
994b7898396STakashi Iwai 			       struct snd_pcm_substream *substream,
995ddee627cSLiam Girdwood 			       struct snd_pcm_hw_params *params)
996ddee627cSLiam Girdwood {
99719bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
9980b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
999244e2936SCharles Keepax 	int i, ret = 0;
1000ddee627cSLiam Girdwood 
1001b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
10025cca5951SShengjiu Wang 
10035cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
10045cca5951SShengjiu Wang 	if (ret)
10055cca5951SShengjiu Wang 		goto out;
10065cca5951SShengjiu Wang 
10077cf3c5b4SKuninori Morimoto 	ret = snd_soc_link_hw_params(substream, params);
1008a5e6c109SKuninori Morimoto 	if (ret < 0)
1009ddee627cSLiam Girdwood 		goto out;
1010ddee627cSLiam Girdwood 
1011a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
10122e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
10132e5894d7SBenoit Cousson 
1014cde79035SRicard Wanderlof 		/*
1015cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1016cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1017cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1018cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1019cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1020cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1021cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1022cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1023cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1024cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1025cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1026cde79035SRicard Wanderlof 		 * we can do the right thing here.
1027cde79035SRicard Wanderlof 		 */
1028cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1029cde79035SRicard Wanderlof 			continue;
1030cde79035SRicard Wanderlof 
10312e5894d7SBenoit Cousson 		/* copy params for each codec */
10322e5894d7SBenoit Cousson 		codec_params = *params;
10332e5894d7SBenoit Cousson 
10342e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1035570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1036570f18b6SRander Wang 		    codec_dai->tx_mask)
10372e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10382e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
1039570f18b6SRander Wang 
1040570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1041570f18b6SRander Wang 		    codec_dai->rx_mask)
10422e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
10432e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
10442e5894d7SBenoit Cousson 
1045aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1046aa6166c2SKuninori Morimoto 					    &codec_params);
104793e6958aSBenoit Cousson 		if(ret < 0)
10484662c596SKuninori Morimoto 			goto out;
1049ddee627cSLiam Girdwood 
10502805b8bdSKuninori Morimoto 		soc_pcm_set_dai_params(codec_dai, &codec_params);
1051078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1052ddee627cSLiam Girdwood 	}
1053ddee627cSLiam Girdwood 
1054a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
10550e9cf4c4SBard Liao 		/*
10560e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
10570e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
10580e9cf4c4SBard Liao 		 */
10590e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
10600e9cf4c4SBard Liao 			continue;
10610e9cf4c4SBard Liao 
1062aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
106393e6958aSBenoit Cousson 		if (ret < 0)
10644662c596SKuninori Morimoto 			goto out;
1065ddee627cSLiam Girdwood 
106619bdcc7aSShreyas NC 		/* store the parameters for each DAI */
10672805b8bdSKuninori Morimoto 		soc_pcm_set_dai_params(cpu_dai, params);
1068ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
106919bdcc7aSShreyas NC 	}
1070ca58221dSKuninori Morimoto 
10713a36a64aSKuninori Morimoto 	ret = snd_soc_pcm_component_hw_params(substream, params);
1072ddee627cSLiam Girdwood out:
1073cb11f79bSKuninori Morimoto 	if (ret < 0) {
1074b7898396STakashi Iwai 		soc_pcm_hw_clean(rtd, substream, 1);
1075cb11f79bSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1076cb11f79bSKuninori Morimoto 	}
1077b8135864SKuninori Morimoto 
1078ddee627cSLiam Girdwood 	return ret;
1079ddee627cSLiam Girdwood }
1080ddee627cSLiam Girdwood 
1081b7898396STakashi Iwai /* hw_params PCM ops for non-DPCM streams */
1082b7898396STakashi Iwai static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1083b7898396STakashi Iwai 			     struct snd_pcm_hw_params *params)
1084b7898396STakashi Iwai {
1085b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1086b7898396STakashi Iwai 	int ret;
1087b7898396STakashi Iwai 
1088b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
1089b7898396STakashi Iwai 	ret = __soc_pcm_hw_params(rtd, substream, params);
1090b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
1091b7898396STakashi Iwai 	return ret;
1092b7898396STakashi Iwai }
1093b7898396STakashi Iwai 
10944378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
10954378f1fbSPeter Ujfalusi {
109659dd33f8SVijendar Mukunda 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
10976374f493SKuninori Morimoto 	int ret = -EINVAL, _ret = 0;
10986374f493SKuninori Morimoto 	int rollback = 0;
10994378f1fbSPeter Ujfalusi 
11004378f1fbSPeter Ujfalusi 	switch (cmd) {
11014378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
11024378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
11034378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11046374f493SKuninori Morimoto 		ret = snd_soc_link_trigger(substream, cmd, 0);
1105836367beSKuninori Morimoto 		if (ret < 0)
11066374f493SKuninori Morimoto 			goto start_err;
1107836367beSKuninori Morimoto 
11086374f493SKuninori Morimoto 		ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1109836367beSKuninori Morimoto 		if (ret < 0)
11106374f493SKuninori Morimoto 			goto start_err;
1111836367beSKuninori Morimoto 
11126374f493SKuninori Morimoto 		ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
11136374f493SKuninori Morimoto start_err:
11146374f493SKuninori Morimoto 		if (ret < 0)
11156374f493SKuninori Morimoto 			rollback = 1;
11166374f493SKuninori Morimoto 	}
11176374f493SKuninori Morimoto 
11186374f493SKuninori Morimoto 	if (rollback) {
11196374f493SKuninori Morimoto 		_ret = ret;
11206374f493SKuninori Morimoto 		switch (cmd) {
11216374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_START:
11226374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_STOP;
11234378f1fbSPeter Ujfalusi 			break;
11246374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_RESUME:
11256374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_SUSPEND;
11266374f493SKuninori Morimoto 			break;
11276374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11286374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
11296374f493SKuninori Morimoto 			break;
11306374f493SKuninori Morimoto 		}
11316374f493SKuninori Morimoto 	}
11326374f493SKuninori Morimoto 
11336374f493SKuninori Morimoto 	switch (cmd) {
11344378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
11354378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
11364378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
113759dd33f8SVijendar Mukunda 		if (rtd->dai_link->stop_dma_first) {
113859dd33f8SVijendar Mukunda 			ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
113959dd33f8SVijendar Mukunda 			if (ret < 0)
114059dd33f8SVijendar Mukunda 				break;
114159dd33f8SVijendar Mukunda 
114259dd33f8SVijendar Mukunda 			ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
114359dd33f8SVijendar Mukunda 			if (ret < 0)
114459dd33f8SVijendar Mukunda 				break;
114559dd33f8SVijendar Mukunda 		} else {
11466374f493SKuninori Morimoto 			ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1147836367beSKuninori Morimoto 			if (ret < 0)
11484378f1fbSPeter Ujfalusi 				break;
1149836367beSKuninori Morimoto 
11506374f493SKuninori Morimoto 			ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1151836367beSKuninori Morimoto 			if (ret < 0)
1152836367beSKuninori Morimoto 				break;
115359dd33f8SVijendar Mukunda 		}
11546374f493SKuninori Morimoto 		ret = snd_soc_link_trigger(substream, cmd, rollback);
1155836367beSKuninori Morimoto 		break;
11564378f1fbSPeter Ujfalusi 	}
11574378f1fbSPeter Ujfalusi 
11586374f493SKuninori Morimoto 	if (_ret)
11596374f493SKuninori Morimoto 		ret = _ret;
11606374f493SKuninori Morimoto 
11614378f1fbSPeter Ujfalusi 	return ret;
11624378f1fbSPeter Ujfalusi }
11634378f1fbSPeter Ujfalusi 
1164ddee627cSLiam Girdwood /*
1165ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1166ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1167dd894f4cSKuninori Morimoto  * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1168ddee627cSLiam Girdwood  */
1169ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1170ddee627cSLiam Girdwood {
1171ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1172ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
11732e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
117419bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
1175ddee627cSLiam Girdwood 
11760035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1177b8135864SKuninori Morimoto 
1178403f830eSKuninori Morimoto 	/* should be called *after* snd_soc_pcm_component_pointer() */
11798544f08cSKuninori Morimoto 	snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1180403f830eSKuninori Morimoto 	snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1181ddee627cSLiam Girdwood 
1182dd894f4cSKuninori Morimoto 	runtime->delay = cpu_delay + codec_delay;
1183ddee627cSLiam Girdwood 
1184ddee627cSLiam Girdwood 	return offset;
1185ddee627cSLiam Girdwood }
1186ddee627cSLiam Girdwood 
118701d7584cSLiam Girdwood /* connect a FE and BE */
118801d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
118901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
119001d7584cSLiam Girdwood {
1191bbf7d3b1SPierre-Louis Bossart 	struct snd_pcm_substream *fe_substream;
1192bbf7d3b1SPierre-Louis Bossart 	struct snd_pcm_substream *be_substream;
119301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1194b7898396STakashi Iwai 
1195b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
119601d7584cSLiam Girdwood 
119701d7584cSLiam Girdwood 	/* only add new dpcms */
11988d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
119901d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
120001d7584cSLiam Girdwood 			return 0;
120101d7584cSLiam Girdwood 	}
120201d7584cSLiam Girdwood 
1203bbf7d3b1SPierre-Louis Bossart 	fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1204bbf7d3b1SPierre-Louis Bossart 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1205bbf7d3b1SPierre-Louis Bossart 
1206bbf7d3b1SPierre-Louis Bossart 	if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1207bbf7d3b1SPierre-Louis Bossart 		dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1208bbf7d3b1SPierre-Louis Bossart 			__func__);
1209bbf7d3b1SPierre-Louis Bossart 		return -EINVAL;
1210bbf7d3b1SPierre-Louis Bossart 	}
1211bbf7d3b1SPierre-Louis Bossart 	if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1212bbf7d3b1SPierre-Louis Bossart 		dev_warn(be->dev, "%s: FE is nonatomic but BE is not, forcing BE as nonatomic\n",
1213bbf7d3b1SPierre-Louis Bossart 			 __func__);
1214bbf7d3b1SPierre-Louis Bossart 		be_substream->pcm->nonatomic = 1;
1215bbf7d3b1SPierre-Louis Bossart 	}
1216bbf7d3b1SPierre-Louis Bossart 
1217d8a9c6e1SPierre-Louis Bossart 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_ATOMIC);
121801d7584cSLiam Girdwood 	if (!dpcm)
121901d7584cSLiam Girdwood 		return -ENOMEM;
122001d7584cSLiam Girdwood 
122101d7584cSLiam Girdwood 	dpcm->be = be;
122201d7584cSLiam Girdwood 	dpcm->fe = fe;
122301d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
122401d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1225b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
122601d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
122701d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1228b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
122901d7584cSLiam Girdwood 
123001d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
123101d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
123201d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
123301d7584cSLiam Girdwood 
1234154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1235154dae87SKuninori Morimoto 
123601d7584cSLiam Girdwood 	return 1;
123701d7584cSLiam Girdwood }
123801d7584cSLiam Girdwood 
123901d7584cSLiam Girdwood /* reparent a BE onto another FE */
124001d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
124101d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
124201d7584cSLiam Girdwood {
124301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
124401d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
124501d7584cSLiam Girdwood 
124601d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
124701d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
124801d7584cSLiam Girdwood 		return;
124901d7584cSLiam Girdwood 
125001d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
125101d7584cSLiam Girdwood 
1252d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
125301d7584cSLiam Girdwood 		if (dpcm->fe == fe)
125401d7584cSLiam Girdwood 			continue;
125501d7584cSLiam Girdwood 
125601d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
125701d7584cSLiam Girdwood 			stream ? "capture" : "playback",
125801d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
125901d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
126001d7584cSLiam Girdwood 
126101d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
126201d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
126301d7584cSLiam Girdwood 		break;
126401d7584cSLiam Girdwood 	}
126501d7584cSLiam Girdwood }
126601d7584cSLiam Girdwood 
126701d7584cSLiam Girdwood /* disconnect a BE and FE */
126823607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
126901d7584cSLiam Girdwood {
127001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1271*9f620684STakashi Iwai 	LIST_HEAD(deleted_dpcms);
127201d7584cSLiam Girdwood 
1273b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
1274b7898396STakashi Iwai 
1275b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
12768d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1277103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
127801d7584cSLiam Girdwood 				stream ? "capture" : "playback",
127901d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
128001d7584cSLiam Girdwood 
128101d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
128201d7584cSLiam Girdwood 			continue;
128301d7584cSLiam Girdwood 
128401d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
128501d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
128601d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
128701d7584cSLiam Girdwood 
128801d7584cSLiam Girdwood 		/* BEs still alive need new FE */
128901d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
129001d7584cSLiam Girdwood 
129101d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
1292*9f620684STakashi Iwai 		list_move(&dpcm->list_fe, &deleted_dpcms);
129301d7584cSLiam Girdwood 	}
1294b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
1295*9f620684STakashi Iwai 
1296*9f620684STakashi Iwai 	while (!list_empty(&deleted_dpcms)) {
1297*9f620684STakashi Iwai 		dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1298*9f620684STakashi Iwai 					list_fe);
1299*9f620684STakashi Iwai 		list_del(&dpcm->list_fe);
1300*9f620684STakashi Iwai 		dpcm_remove_debugfs_state(dpcm);
1301*9f620684STakashi Iwai 		kfree(dpcm);
1302*9f620684STakashi Iwai 	}
130301d7584cSLiam Girdwood }
130401d7584cSLiam Girdwood 
130501d7584cSLiam Girdwood /* get BE for DAI widget and stream */
130601d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
130701d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
130801d7584cSLiam Girdwood {
130901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
131093597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
13117afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
13121a497983SMengdong Lin 	int i;
131301d7584cSLiam Girdwood 
13143c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
13153c146465SLiam Girdwood 
1316bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
131701d7584cSLiam Girdwood 
131835ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
131935ea0655SLiam Girdwood 			continue;
132035ea0655SLiam Girdwood 
1321c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
132219bdcc7aSShreyas NC 			w = snd_soc_dai_get_widget(dai, stream);
132393597faeSKuninori Morimoto 
13243c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
132593597faeSKuninori Morimoto 				w ? w->name : "(not set)");
13263c146465SLiam Girdwood 
132793597faeSKuninori Morimoto 			if (w == widget)
132801d7584cSLiam Girdwood 				return be;
132919bdcc7aSShreyas NC 		}
133001d7584cSLiam Girdwood 	}
133101d7584cSLiam Girdwood 
13329d6ee365SJerome Brunet 	/* Widget provided is not a BE */
133301d7584cSLiam Girdwood 	return NULL;
133401d7584cSLiam Girdwood }
133501d7584cSLiam Girdwood 
133601d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
133701d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
133801d7584cSLiam Girdwood {
133909e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
134001d7584cSLiam Girdwood 	int i;
134101d7584cSLiam Girdwood 
134209e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
134309e88f8aSKuninori Morimoto 		if (widget == w)
134401d7584cSLiam Girdwood 			return 1;
134501d7584cSLiam Girdwood 
134601d7584cSLiam Girdwood 	return 0;
134701d7584cSLiam Girdwood }
134801d7584cSLiam Girdwood 
1349d1a7af09SRanjani Sridharan bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
13505fdd022cSPiotr Stankiewicz {
13515fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
13525fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1353c2cd8216SKuninori Morimoto 	int stream;
13545fdd022cSPiotr Stankiewicz 
1355c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1356c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1357c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1358c2cd8216SKuninori Morimoto 	else
1359c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1360c2cd8216SKuninori Morimoto 
1361027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1362027a4838SKuninori Morimoto 	if (rtd)
13635fdd022cSPiotr Stankiewicz 		return true;
13645fdd022cSPiotr Stankiewicz 
13655fdd022cSPiotr Stankiewicz 	return false;
13665fdd022cSPiotr Stankiewicz }
1367d1a7af09SRanjani Sridharan EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
13685fdd022cSPiotr Stankiewicz 
136923607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
13701ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
137101d7584cSLiam Girdwood {
1372c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
137301d7584cSLiam Girdwood 	int paths;
137401d7584cSLiam Girdwood 
13756e1276a5SBard Liao 	if (fe->num_cpus > 1) {
13766e1276a5SBard Liao 		dev_err(fe->dev,
13776e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
13786e1276a5SBard Liao 		return -EINVAL;
13796e1276a5SBard Liao 	}
13806e1276a5SBard Liao 
138101d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
13826742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1383aa293777SSameer Pujar 			fe->card->component_chaining ?
1384aa293777SSameer Pujar 				NULL : dpcm_end_walk_at_be);
138501d7584cSLiam Girdwood 
1386d479f00bSKuninori Morimoto 	if (paths > 0)
1387103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
138801d7584cSLiam Girdwood 			stream ? "capture" : "playback");
1389d479f00bSKuninori Morimoto 	else if (paths == 0)
1390d479f00bSKuninori Morimoto 		dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1391d479f00bSKuninori Morimoto 			 stream ? "capture" : "playback");
139201d7584cSLiam Girdwood 
139301d7584cSLiam Girdwood 	return paths;
139401d7584cSLiam Girdwood }
139501d7584cSLiam Girdwood 
139652645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
139752645e33SKuninori Morimoto {
139852645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
139952645e33SKuninori Morimoto }
140052645e33SKuninori Morimoto 
14018cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
14028cce6569SGuennadi Liakhovetski 			      struct snd_soc_dapm_widget_list *list)
140301d7584cSLiam Girdwood {
14047afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
14052e5894d7SBenoit Cousson 	unsigned int i;
140601d7584cSLiam Girdwood 
1407c840f769SKuninori Morimoto 	/* is there a valid DAI widget for this BE */
1408c840f769SKuninori Morimoto 	for_each_rtd_dais(dpcm->be, i, dai) {
14097931df9bSKuninori Morimoto 		struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
141001d7584cSLiam Girdwood 
141119bdcc7aSShreyas NC 		/*
1412c840f769SKuninori Morimoto 		 * The BE is pruned only if none of the dai
141319bdcc7aSShreyas NC 		 * widgets are in the active list.
141419bdcc7aSShreyas NC 		 */
141501d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
14168cce6569SGuennadi Liakhovetski 			return true;
141719bdcc7aSShreyas NC 	}
141801d7584cSLiam Girdwood 
14198cce6569SGuennadi Liakhovetski 	return false;
14208cce6569SGuennadi Liakhovetski }
14218cce6569SGuennadi Liakhovetski 
14228cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
14238cce6569SGuennadi Liakhovetski 			    struct snd_soc_dapm_widget_list **list_)
14248cce6569SGuennadi Liakhovetski {
14258cce6569SGuennadi Liakhovetski 	struct snd_soc_dpcm *dpcm;
14268cce6569SGuennadi Liakhovetski 	int prune = 0;
14278cce6569SGuennadi Liakhovetski 
14288cce6569SGuennadi Liakhovetski 	/* Destroy any old FE <--> BE connections */
14298cce6569SGuennadi Liakhovetski 	for_each_dpcm_be(fe, stream, dpcm) {
14308cce6569SGuennadi Liakhovetski 		if (dpcm_be_is_active(dpcm, stream, *list_))
1431bed646dcSKuninori Morimoto 			continue;
143201d7584cSLiam Girdwood 
1433103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
143401d7584cSLiam Girdwood 			stream ? "capture" : "playback",
143501d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
143601d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1437a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
143801d7584cSLiam Girdwood 		prune++;
143901d7584cSLiam Girdwood 	}
144001d7584cSLiam Girdwood 
1441103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
144201d7584cSLiam Girdwood 	return prune;
144301d7584cSLiam Girdwood }
144401d7584cSLiam Girdwood 
144501d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
144601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
144701d7584cSLiam Girdwood {
144801d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
144901d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
145001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
145109e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
145201d7584cSLiam Girdwood 	int i, new = 0, err;
145301d7584cSLiam Girdwood 
145401d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
145509e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
145601d7584cSLiam Girdwood 
145709e88f8aSKuninori Morimoto 		switch (widget->id) {
14584616274dSMark Brown 		case snd_soc_dapm_dai_in:
1459c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1460c5b8540dSKoro Chen 				continue;
1461c5b8540dSKoro Chen 			break;
14624616274dSMark Brown 		case snd_soc_dapm_dai_out:
1463c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1464c5b8540dSKoro Chen 				continue;
14654616274dSMark Brown 			break;
14664616274dSMark Brown 		default:
146701d7584cSLiam Girdwood 			continue;
14684616274dSMark Brown 		}
146901d7584cSLiam Girdwood 
147001d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
147109e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
147201d7584cSLiam Girdwood 		if (!be) {
1473b6eabd24SShengjiu Wang 			dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
147409e88f8aSKuninori Morimoto 				widget->name);
147501d7584cSLiam Girdwood 			continue;
147601d7584cSLiam Girdwood 		}
147701d7584cSLiam Girdwood 
147801d7584cSLiam Girdwood 		/* don't connect if FE is not running */
147923607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
148001d7584cSLiam Girdwood 			continue;
148101d7584cSLiam Girdwood 
14829609cfcdSPierre-Louis Bossart 		/*
14839609cfcdSPierre-Louis Bossart 		 * Filter for systems with 'component_chaining' enabled.
14849609cfcdSPierre-Louis Bossart 		 * This helps to avoid unnecessary re-configuration of an
14859609cfcdSPierre-Louis Bossart 		 * already active BE on such systems.
14869609cfcdSPierre-Louis Bossart 		 */
14879609cfcdSPierre-Louis Bossart 		if (fe->card->component_chaining &&
14889609cfcdSPierre-Louis Bossart 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
14890c25db3fSSameer Pujar 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
14900c25db3fSSameer Pujar 			continue;
14910c25db3fSSameer Pujar 
149201d7584cSLiam Girdwood 		/* newly connected FE and BE */
149301d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
149401d7584cSLiam Girdwood 		if (err < 0) {
1495103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
149609e88f8aSKuninori Morimoto 				widget->name);
149701d7584cSLiam Girdwood 			break;
149801d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
149901d7584cSLiam Girdwood 			continue;
150001d7584cSLiam Girdwood 
150101d7584cSLiam Girdwood 		/* new */
1502a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
150301d7584cSLiam Girdwood 		new++;
150401d7584cSLiam Girdwood 	}
150501d7584cSLiam Girdwood 
1506103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
150701d7584cSLiam Girdwood 	return new;
150801d7584cSLiam Girdwood }
150901d7584cSLiam Girdwood 
151001d7584cSLiam Girdwood /*
151101d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
151201d7584cSLiam Girdwood  * FE substream.
151301d7584cSLiam Girdwood  */
151423607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
151501d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
151601d7584cSLiam Girdwood {
151701d7584cSLiam Girdwood 	if (new)
151801d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
151901d7584cSLiam Girdwood 	else
152001d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
152101d7584cSLiam Girdwood }
152201d7584cSLiam Girdwood 
152323607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
152401d7584cSLiam Girdwood {
152501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
152601d7584cSLiam Girdwood 
15278d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
1528a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
152901d7584cSLiam Girdwood }
153001d7584cSLiam Girdwood 
1531531590bbSKuninori Morimoto void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1532531590bbSKuninori Morimoto 		      int do_hw_free, struct snd_soc_dpcm *last)
153301d7584cSLiam Girdwood {
153401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
153501d7584cSLiam Girdwood 
153601d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15378d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
153801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
153901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
154001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
154101d7584cSLiam Girdwood 
1542531590bbSKuninori Morimoto 		if (dpcm == last)
1543531590bbSKuninori Morimoto 			return;
1544531590bbSKuninori Morimoto 
1545531590bbSKuninori Morimoto 		/* is this op for this BE ? */
1546531590bbSKuninori Morimoto 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1547531590bbSKuninori Morimoto 			continue;
1548531590bbSKuninori Morimoto 
15491db19c15SKuninori Morimoto 		if (be->dpcm[stream].users == 0) {
1550103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
155101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
155201d7584cSLiam Girdwood 				be->dpcm[stream].state);
15531db19c15SKuninori Morimoto 			continue;
15541db19c15SKuninori Morimoto 		}
155501d7584cSLiam Girdwood 
155601d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
155701d7584cSLiam Girdwood 			continue;
155801d7584cSLiam Girdwood 
1559531590bbSKuninori Morimoto 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1560531590bbSKuninori Morimoto 			if (!do_hw_free)
156101d7584cSLiam Girdwood 				continue;
156201d7584cSLiam Girdwood 
1563531590bbSKuninori Morimoto 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1564b7898396STakashi Iwai 				__soc_pcm_hw_free(be, be_substream);
1565531590bbSKuninori Morimoto 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1566531590bbSKuninori Morimoto 			}
1567531590bbSKuninori Morimoto 		}
1568531590bbSKuninori Morimoto 
1569b7898396STakashi Iwai 		__soc_pcm_close(be, be_substream);
157001d7584cSLiam Girdwood 		be_substream->runtime = NULL;
157101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
157201d7584cSLiam Girdwood 	}
157301d7584cSLiam Girdwood }
157401d7584cSLiam Girdwood 
157523607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
157601d7584cSLiam Girdwood {
157706aaeb87SKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
157801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
157901d7584cSLiam Girdwood 	int err, count = 0;
158001d7584cSLiam Girdwood 
158101d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
15828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
158306aaeb87SKuninori Morimoto 		struct snd_pcm_substream *be_substream;
158401d7584cSLiam Girdwood 
158506aaeb87SKuninori Morimoto 		be = dpcm->be;
158606aaeb87SKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
158701d7584cSLiam Girdwood 
15882062b4c5SRussell King - ARM Linux 		if (!be_substream) {
15892062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
15902062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
15912062b4c5SRussell King - ARM Linux 			continue;
15922062b4c5SRussell King - ARM Linux 		}
15932062b4c5SRussell King - ARM Linux 
159401d7584cSLiam Girdwood 		/* is this op for this BE ? */
159501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
159601d7584cSLiam Girdwood 			continue;
159701d7584cSLiam Girdwood 
159801d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
15991db19c15SKuninori Morimoto 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1600103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
160101d7584cSLiam Girdwood 				stream ? "capture" : "playback",
160201d7584cSLiam Girdwood 				be->dpcm[stream].state);
16031db19c15SKuninori Morimoto 			continue;
16041db19c15SKuninori Morimoto 		}
160501d7584cSLiam Girdwood 
160601d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
160701d7584cSLiam Girdwood 			continue;
160801d7584cSLiam Girdwood 
160901d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
161001d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
161101d7584cSLiam Girdwood 			continue;
161201d7584cSLiam Girdwood 
16132062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
16142062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
161501d7584cSLiam Girdwood 
161601d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
1617b7898396STakashi Iwai 		err = __soc_pcm_open(be, be_substream);
161801d7584cSLiam Girdwood 		if (err < 0) {
161901d7584cSLiam Girdwood 			be->dpcm[stream].users--;
162001d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1621103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
162201d7584cSLiam Girdwood 					stream ? "capture" : "playback",
162301d7584cSLiam Girdwood 					be->dpcm[stream].state);
162401d7584cSLiam Girdwood 
162501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
162601d7584cSLiam Girdwood 			goto unwind;
162701d7584cSLiam Girdwood 		}
1628848aedfdSPierre-Louis Bossart 		be->dpcm[stream].be_start = 0;
162901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
163001d7584cSLiam Girdwood 		count++;
163101d7584cSLiam Girdwood 	}
163201d7584cSLiam Girdwood 
163301d7584cSLiam Girdwood 	return count;
163401d7584cSLiam Girdwood 
163501d7584cSLiam Girdwood unwind:
1636531590bbSKuninori Morimoto 	dpcm_be_dai_startup_rollback(fe, stream, dpcm);
163701d7584cSLiam Girdwood 
163806aaeb87SKuninori Morimoto 	dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
163906aaeb87SKuninori Morimoto 		__func__, be->dai_link->name, err);
164006aaeb87SKuninori Morimoto 
164101d7584cSLiam Girdwood 	return err;
164201d7584cSLiam Girdwood }
164301d7584cSLiam Girdwood 
16445f53898aSKuninori Morimoto static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
16455f53898aSKuninori Morimoto {
16465f53898aSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
16475f53898aSKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
16485f53898aSKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
16495f53898aSKuninori Morimoto 	struct snd_soc_dai *dai;
16505f53898aSKuninori Morimoto 	int stream = substream->stream;
16515f53898aSKuninori Morimoto 	int i;
16525f53898aSKuninori Morimoto 
16535f53898aSKuninori Morimoto 	soc_pcm_hw_init(hw);
16545f53898aSKuninori Morimoto 
16555f53898aSKuninori Morimoto 	for_each_rtd_cpu_dais(fe, i, dai) {
16565f53898aSKuninori Morimoto 		struct snd_soc_pcm_stream *cpu_stream;
16575f53898aSKuninori Morimoto 
16585f53898aSKuninori Morimoto 		/*
16595f53898aSKuninori Morimoto 		 * Skip CPUs which don't support the current stream
16605f53898aSKuninori Morimoto 		 * type. See soc_pcm_init_runtime_hw() for more details
16615f53898aSKuninori Morimoto 		 */
16625f53898aSKuninori Morimoto 		if (!snd_soc_dai_stream_valid(dai, stream))
16635f53898aSKuninori Morimoto 			continue;
16645f53898aSKuninori Morimoto 
16655f53898aSKuninori Morimoto 		cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
16665f53898aSKuninori Morimoto 
16675f53898aSKuninori Morimoto 		soc_pcm_hw_update_rate(hw, cpu_stream);
16685f53898aSKuninori Morimoto 		soc_pcm_hw_update_chan(hw, cpu_stream);
16695f53898aSKuninori Morimoto 		soc_pcm_hw_update_format(hw, cpu_stream);
16705f53898aSKuninori Morimoto 	}
16715f53898aSKuninori Morimoto 
16725f53898aSKuninori Morimoto }
16735f53898aSKuninori Morimoto 
16741b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1675b073ed4eSKuninori Morimoto {
16760ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
16771b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
16784b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1679b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
16807afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1681b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1682b073ed4eSKuninori Morimoto 
1683b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1684435ffb76SJerome Brunet 		return;
1685b073ed4eSKuninori Morimoto 
1686b073ed4eSKuninori Morimoto 	/*
1687b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1688b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1689b073ed4eSKuninori Morimoto 	 */
1690b073ed4eSKuninori Morimoto 
16918d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1692b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1693b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1694b073ed4eSKuninori Morimoto 		int i;
1695b073ed4eSKuninori Morimoto 
1696a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(be, i, dai) {
16974febced1SJerome Brunet 			/*
16984febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
16994febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
17004febced1SJerome Brunet 			 */
17017afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
17024febced1SJerome Brunet 				continue;
17034febced1SJerome Brunet 
1704acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1705b073ed4eSKuninori Morimoto 
1706debc71f2SKuninori Morimoto 			soc_pcm_hw_update_format(hw, codec_stream);
1707435ffb76SJerome Brunet 		}
1708b073ed4eSKuninori Morimoto 	}
1709b073ed4eSKuninori Morimoto }
1710b073ed4eSKuninori Morimoto 
17111b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1712f4c277b8SJiada Wang {
17130ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17141b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
17154b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1716f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1717f4c277b8SJiada Wang 	int stream = substream->stream;
1718f4c277b8SJiada Wang 
1719f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1720f4c277b8SJiada Wang 		return;
1721f4c277b8SJiada Wang 
1722f4c277b8SJiada Wang 	/*
1723f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1724f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1725f4c277b8SJiada Wang 	 */
1726f4c277b8SJiada Wang 
17278d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1728f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
17294f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
173019bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
173119bdcc7aSShreyas NC 		int i;
1732f4c277b8SJiada Wang 
1733a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(be, i, dai) {
17340e9cf4c4SBard Liao 			/*
17350e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
17360e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
17370e9cf4c4SBard Liao 			 */
17380e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
17390e9cf4c4SBard Liao 				continue;
17400e9cf4c4SBard Liao 
174119bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
17424f2bd18bSJerome Brunet 
17436cb56a45SKuninori Morimoto 			soc_pcm_hw_update_chan(hw, cpu_stream);
174419bdcc7aSShreyas NC 		}
17454f2bd18bSJerome Brunet 
17464f2bd18bSJerome Brunet 		/*
17474f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
17484f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
17494f2bd18bSJerome Brunet 		 */
17504f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
17519bdc573dSKuninori Morimoto 			struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
17529bdc573dSKuninori Morimoto 				asoc_rtd_to_codec(be, 0), stream);
1753f4c277b8SJiada Wang 
17546cb56a45SKuninori Morimoto 			soc_pcm_hw_update_chan(hw, codec_stream);
1755f4c277b8SJiada Wang 		}
1756f4c277b8SJiada Wang 	}
1757f4c277b8SJiada Wang }
1758f4c277b8SJiada Wang 
17591b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1760baacd8d1SJerome Brunet {
17610ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17621b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
17634b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1764baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1765baacd8d1SJerome Brunet 	int stream = substream->stream;
1766baacd8d1SJerome Brunet 
1767baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1768baacd8d1SJerome Brunet 		return;
1769baacd8d1SJerome Brunet 
1770baacd8d1SJerome Brunet 	/*
1771baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1772baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1773baacd8d1SJerome Brunet 	 */
1774baacd8d1SJerome Brunet 
17758d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1776baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1777c840f769SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm;
17787afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1779baacd8d1SJerome Brunet 		int i;
1780baacd8d1SJerome Brunet 
1781c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
17820e9cf4c4SBard Liao 			/*
1783c840f769SKuninori Morimoto 			 * Skip DAIs which don't support the current stream
17840e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
17850e9cf4c4SBard Liao 			 */
17860e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
17870e9cf4c4SBard Liao 				continue;
17880e9cf4c4SBard Liao 
1789c840f769SKuninori Morimoto 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1790baacd8d1SJerome Brunet 
1791f6c04af5SKuninori Morimoto 			soc_pcm_hw_update_rate(hw, pcm);
1792baacd8d1SJerome Brunet 		}
1793baacd8d1SJerome Brunet 	}
1794baacd8d1SJerome Brunet }
1795baacd8d1SJerome Brunet 
1796906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1797906c7d69SPC Liao 			       int stream)
1798906c7d69SPC Liao {
1799906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
18000ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
180119bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
180212ffd726SJaroslav Kysela 	int err = 0;
180319bdcc7aSShreyas NC 	int i;
1804906c7d69SPC Liao 
1805906c7d69SPC Liao 	/* apply symmetry for FE */
180668cbc557SKuninori Morimoto 	soc_pcm_update_symmetry(fe_substream);
1807906c7d69SPC Liao 
1808a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1809906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1810906c7d69SPC Liao 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1811906c7d69SPC Liao 		if (err < 0)
1812bbd2bac8SKuninori Morimoto 			goto error;
1813906c7d69SPC Liao 	}
1814906c7d69SPC Liao 
1815906c7d69SPC Liao 	/* apply symmetry for BE */
18168d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1817906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1818906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1819906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
18206246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
1821c840f769SKuninori Morimoto 		struct snd_soc_dai *dai;
1822906c7d69SPC Liao 
18236246f283SJerome Brunet 		/* A backend may not have the requested substream */
18246246f283SJerome Brunet 		if (!be_substream)
18256246f283SJerome Brunet 			continue;
18266246f283SJerome Brunet 
18270ceef681SKuninori Morimoto 		rtd = asoc_substream_to_rtd(be_substream);
1828f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1829f1176614SJeeja KP 			continue;
1830f1176614SJeeja KP 
183168cbc557SKuninori Morimoto 		soc_pcm_update_symmetry(be_substream);
1832906c7d69SPC Liao 
1833906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1834c840f769SKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
1835c840f769SKuninori Morimoto 			err = soc_pcm_apply_symmetry(fe_substream, dai);
1836906c7d69SPC Liao 			if (err < 0)
1837bbd2bac8SKuninori Morimoto 				goto error;
1838906c7d69SPC Liao 		}
1839906c7d69SPC Liao 	}
1840bbd2bac8SKuninori Morimoto error:
1841bbd2bac8SKuninori Morimoto 	if (err < 0)
1842bbd2bac8SKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1843906c7d69SPC Liao 
1844bbd2bac8SKuninori Morimoto 	return err;
1845906c7d69SPC Liao }
1846906c7d69SPC Liao 
184701d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
184801d7584cSLiam Girdwood {
18490ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
185001d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
185101d7584cSLiam Girdwood 
1852ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
185301d7584cSLiam Girdwood 
185425c2f515SKuninori Morimoto 	ret = dpcm_be_dai_startup(fe, stream);
185506aaeb87SKuninori Morimoto 	if (ret < 0)
185601d7584cSLiam Girdwood 		goto be_err;
185701d7584cSLiam Girdwood 
1858103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
185901d7584cSLiam Girdwood 
186001d7584cSLiam Girdwood 	/* start the DAI frontend */
1861b7898396STakashi Iwai 	ret = __soc_pcm_open(fe, fe_substream);
1862e4b044f4SKuninori Morimoto 	if (ret < 0)
186301d7584cSLiam Girdwood 		goto unwind;
186401d7584cSLiam Girdwood 
186501d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
186601d7584cSLiam Girdwood 
18674fe28461SKuninori Morimoto 	dpcm_runtime_setup_fe(fe_substream);
18684fe28461SKuninori Morimoto 
18694fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_format(fe_substream);
18704fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_chan(fe_substream);
18714fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_rate(fe_substream);
187201d7584cSLiam Girdwood 
1873906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
187401d7584cSLiam Girdwood 
187501d7584cSLiam Girdwood unwind:
18768a01fbf0SKuninori Morimoto 	if (ret < 0)
187725c2f515SKuninori Morimoto 		dpcm_be_dai_startup_unwind(fe, stream);
187801d7584cSLiam Girdwood be_err:
1879ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
188006aaeb87SKuninori Morimoto 
188106aaeb87SKuninori Morimoto 	if (ret < 0)
188206aaeb87SKuninori Morimoto 		dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
188306aaeb87SKuninori Morimoto 
188401d7584cSLiam Girdwood 	return ret;
188501d7584cSLiam Girdwood }
188601d7584cSLiam Girdwood 
188701d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
188801d7584cSLiam Girdwood {
18890ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
189001d7584cSLiam Girdwood 	int stream = substream->stream;
189101d7584cSLiam Girdwood 
1892b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
1893b7898396STakashi Iwai 
1894ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
189501d7584cSLiam Girdwood 
189601d7584cSLiam Girdwood 	/* shutdown the BEs */
189725c2f515SKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
189801d7584cSLiam Girdwood 
1899103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
190001d7584cSLiam Girdwood 
190101d7584cSLiam Girdwood 	/* now shutdown the frontend */
1902b7898396STakashi Iwai 	__soc_pcm_close(fe, substream);
190301d7584cSLiam Girdwood 
1904bb9dd3ceSRanjani Sridharan 	/* run the stream stop event */
1905bb9dd3ceSRanjani Sridharan 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1906bb9dd3ceSRanjani Sridharan 
190701d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1908ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
190901d7584cSLiam Girdwood 	return 0;
191001d7584cSLiam Girdwood }
191101d7584cSLiam Girdwood 
1912f52366e6SKuninori Morimoto void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
191301d7584cSLiam Girdwood {
191401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
191501d7584cSLiam Girdwood 
191601d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
191701d7584cSLiam Girdwood 	 * to this frontend DAI */
19188d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
191901d7584cSLiam Girdwood 
192001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
192101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
192201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
192301d7584cSLiam Girdwood 
192401d7584cSLiam Girdwood 		/* is this op for this BE ? */
192501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
192601d7584cSLiam Girdwood 			continue;
192701d7584cSLiam Girdwood 
192801d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
192901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
193001d7584cSLiam Girdwood 				continue;
193101d7584cSLiam Girdwood 
193236fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
193336fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
193436fba62cSQiao Zhou 			continue;
193536fba62cSQiao Zhou 
193601d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
193701d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
193801d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
193908b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
19405e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
19415e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
194201d7584cSLiam Girdwood 			continue;
194301d7584cSLiam Girdwood 
1944103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
194594d215ccS彭东林 			be->dai_link->name);
194601d7584cSLiam Girdwood 
1947b7898396STakashi Iwai 		__soc_pcm_hw_free(be, be_substream);
194801d7584cSLiam Girdwood 
194901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
195001d7584cSLiam Girdwood 	}
195101d7584cSLiam Girdwood }
195201d7584cSLiam Girdwood 
195345c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
195401d7584cSLiam Girdwood {
19550ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1956f52366e6SKuninori Morimoto 	int stream = substream->stream;
195701d7584cSLiam Girdwood 
1958b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
1959ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
196001d7584cSLiam Girdwood 
1961103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
196201d7584cSLiam Girdwood 
196301d7584cSLiam Girdwood 	/* call hw_free on the frontend */
1964b7898396STakashi Iwai 	soc_pcm_hw_clean(fe, substream, 0);
196501d7584cSLiam Girdwood 
196601d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
196701d7584cSLiam Girdwood 	 * to this frontend DAI */
1968f52366e6SKuninori Morimoto 	dpcm_be_dai_hw_free(fe, stream);
196901d7584cSLiam Girdwood 
197001d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1971ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
197201d7584cSLiam Girdwood 
1973b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
197401d7584cSLiam Girdwood 	return 0;
197501d7584cSLiam Girdwood }
197601d7584cSLiam Girdwood 
197723607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
197801d7584cSLiam Girdwood {
197933b6b94fSKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
198033b6b94fSKuninori Morimoto 	struct snd_pcm_substream *be_substream;
198101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
198201d7584cSLiam Girdwood 	int ret;
198301d7584cSLiam Girdwood 
19848d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
198533b6b94fSKuninori Morimoto 		be = dpcm->be;
198633b6b94fSKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
198701d7584cSLiam Girdwood 
198801d7584cSLiam Girdwood 		/* is this op for this BE ? */
198901d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
199001d7584cSLiam Girdwood 			continue;
199101d7584cSLiam Girdwood 
199201d7584cSLiam Girdwood 		/* copy params for each dpcm */
199301d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
199401d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
199501d7584cSLiam Girdwood 
199601d7584cSLiam Girdwood 		/* perform any hw_params fixups */
19970cbbf8a0SKuninori Morimoto 		ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
19980cbbf8a0SKuninori Morimoto 		if (ret < 0)
199901d7584cSLiam Girdwood 			goto unwind;
200001d7584cSLiam Girdwood 
2001ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2002ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2003ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2004ae061d2aSLibin Yang 
2005b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2006b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2007b0639bd2SKuninori Morimoto 			continue;
2008b0639bd2SKuninori Morimoto 
2009b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2010b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2011b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2012b0639bd2SKuninori Morimoto 			continue;
2013b0639bd2SKuninori Morimoto 
2014b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
201594d215ccS彭东林 			be->dai_link->name);
2016b0639bd2SKuninori Morimoto 
2017b7898396STakashi Iwai 		ret = __soc_pcm_hw_params(be, be_substream, &dpcm->hw_params);
2018cb11f79bSKuninori Morimoto 		if (ret < 0)
201901d7584cSLiam Girdwood 			goto unwind;
202001d7584cSLiam Girdwood 
202101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
202201d7584cSLiam Girdwood 	}
202301d7584cSLiam Girdwood 	return 0;
202401d7584cSLiam Girdwood 
202501d7584cSLiam Girdwood unwind:
202633b6b94fSKuninori Morimoto 	dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
202733b6b94fSKuninori Morimoto 		__func__, be->dai_link->name, ret);
202833b6b94fSKuninori Morimoto 
202901d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
20308d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
203133b6b94fSKuninori Morimoto 		be = dpcm->be;
203233b6b94fSKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
203301d7584cSLiam Girdwood 
203401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
203501d7584cSLiam Girdwood 			continue;
203601d7584cSLiam Girdwood 
203701d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
203801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
203901d7584cSLiam Girdwood 			continue;
204001d7584cSLiam Girdwood 
204101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
204201d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
204301d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
204401d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
204501d7584cSLiam Girdwood 			continue;
204601d7584cSLiam Girdwood 
2047b7898396STakashi Iwai 		__soc_pcm_hw_free(be, be_substream);
204801d7584cSLiam Girdwood 	}
204901d7584cSLiam Girdwood 
205001d7584cSLiam Girdwood 	return ret;
205101d7584cSLiam Girdwood }
205201d7584cSLiam Girdwood 
205345c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
205401d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
205501d7584cSLiam Girdwood {
20560ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
205701d7584cSLiam Girdwood 	int ret, stream = substream->stream;
205801d7584cSLiam Girdwood 
2059b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
2060ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
206101d7584cSLiam Girdwood 
206225c2f515SKuninori Morimoto 	memcpy(&fe->dpcm[stream].hw_params, params,
206301d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
206425c2f515SKuninori Morimoto 	ret = dpcm_be_dai_hw_params(fe, stream);
206533b6b94fSKuninori Morimoto 	if (ret < 0)
206601d7584cSLiam Girdwood 		goto out;
206701d7584cSLiam Girdwood 
2068103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
206901d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
207001d7584cSLiam Girdwood 			params_channels(params), params_format(params));
207101d7584cSLiam Girdwood 
207201d7584cSLiam Girdwood 	/* call hw_params on the frontend */
2073b7898396STakashi Iwai 	ret = __soc_pcm_hw_params(fe, substream, params);
2074cb11f79bSKuninori Morimoto 	if (ret < 0)
207501d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
2076cb11f79bSKuninori Morimoto 	else
207701d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
207801d7584cSLiam Girdwood 
207901d7584cSLiam Girdwood out:
2080ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2081b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
208233b6b94fSKuninori Morimoto 
208333b6b94fSKuninori Morimoto 	if (ret < 0)
208433b6b94fSKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
208533b6b94fSKuninori Morimoto 
208601d7584cSLiam Girdwood 	return ret;
208701d7584cSLiam Girdwood }
208801d7584cSLiam Girdwood 
208923607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
209045c0a188SMark Brown 			       int cmd)
209101d7584cSLiam Girdwood {
2092db3aa39cSKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
209301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2094b2ae8066STakashi Iwai 	unsigned long flags;
209501d7584cSLiam Girdwood 	int ret = 0;
209601d7584cSLiam Girdwood 
20978d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2098db3aa39cSKuninori Morimoto 		struct snd_pcm_substream *be_substream;
209901d7584cSLiam Girdwood 
2100db3aa39cSKuninori Morimoto 		be = dpcm->be;
2101db3aa39cSKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
210201d7584cSLiam Girdwood 
21033c75c0eaSTakashi Iwai 		snd_soc_dpcm_stream_lock_irqsave_nested(be, stream, flags);
2104b2ae8066STakashi Iwai 
210501d7584cSLiam Girdwood 		/* is this op for this BE ? */
210601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2107b2ae8066STakashi Iwai 			goto next;
210801d7584cSLiam Girdwood 
2109a9faca15SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2110a9faca15SKuninori Morimoto 			be->dai_link->name, cmd);
2111a9faca15SKuninori Morimoto 
211201d7584cSLiam Girdwood 		switch (cmd) {
211301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
2114848aedfdSPierre-Louis Bossart 			if (!be->dpcm[stream].be_start &&
2115848aedfdSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
211621fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
21173202e2f5SMark Brown 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2118b2ae8066STakashi Iwai 				goto next;
21196479f758SPierre-Louis Bossart 
2120848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2121848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2122b2ae8066STakashi Iwai 				goto next;
212301d7584cSLiam Girdwood 
2124848aedfdSPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2125848aedfdSPierre-Louis Bossart 			if (ret) {
2126848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2127848aedfdSPierre-Louis Bossart 				goto next;
2128848aedfdSPierre-Louis Bossart 			}
2129848aedfdSPierre-Louis Bossart 
21303202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
213101d7584cSLiam Girdwood 			break;
213201d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
21333202e2f5SMark Brown 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2134b2ae8066STakashi Iwai 				goto next;
21356479f758SPierre-Louis Bossart 
2136848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2137848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2138b2ae8066STakashi Iwai 				goto next;
213901d7584cSLiam Girdwood 
2140848aedfdSPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2141848aedfdSPierre-Louis Bossart 			if (ret) {
2142848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2143848aedfdSPierre-Louis Bossart 				goto next;
2144848aedfdSPierre-Louis Bossart 			}
2145848aedfdSPierre-Louis Bossart 
21463202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
214701d7584cSLiam Girdwood 			break;
214801d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
21493aa1e96aSPierre-Louis Bossart 			if (!be->dpcm[stream].be_start &&
21503aa1e96aSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
21513aa1e96aSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
21523aa1e96aSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2153b2ae8066STakashi Iwai 				goto next;
21546479f758SPierre-Louis Bossart 
2155848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2156848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2157b2ae8066STakashi Iwai 				goto next;
215801d7584cSLiam Girdwood 
2159848aedfdSPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2160848aedfdSPierre-Louis Bossart 			if (ret) {
2161848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2162848aedfdSPierre-Louis Bossart 				goto next;
2163848aedfdSPierre-Louis Bossart 			}
2164848aedfdSPierre-Louis Bossart 
21653202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
216601d7584cSLiam Girdwood 			break;
216701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
216821fca8bdS이경택 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
21693202e2f5SMark Brown 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2170b2ae8066STakashi Iwai 				goto next;
217101d7584cSLiam Girdwood 
2172848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2173848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2174848aedfdSPierre-Louis Bossart 
2175848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2176b2ae8066STakashi Iwai 				goto next;
21770c75fc71SPierre-Louis Bossart 
21780c75fc71SPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2179848aedfdSPierre-Louis Bossart 			if (ret) {
2180848aedfdSPierre-Louis Bossart 				if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2181848aedfdSPierre-Louis Bossart 					be->dpcm[stream].be_start++;
2182b2ae8066STakashi Iwai 				goto next;
2183848aedfdSPierre-Louis Bossart 			}
21840c75fc71SPierre-Louis Bossart 
21853202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
218601d7584cSLiam Girdwood 			break;
218701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
21883202e2f5SMark Brown 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2189b2ae8066STakashi Iwai 				goto next;
219001d7584cSLiam Girdwood 
2191848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start--;
2192848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2193b2ae8066STakashi Iwai 				goto next;
219401d7584cSLiam Girdwood 
21950c75fc71SPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2196848aedfdSPierre-Louis Bossart 			if (ret) {
2197848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start++;
2198b2ae8066STakashi Iwai 				goto next;
2199848aedfdSPierre-Louis Bossart 			}
22000c75fc71SPierre-Louis Bossart 
22013202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
220201d7584cSLiam Girdwood 			break;
220301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
22043202e2f5SMark Brown 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2205b2ae8066STakashi Iwai 				goto next;
220601d7584cSLiam Girdwood 
2207848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start--;
2208848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2209b2ae8066STakashi Iwai 				goto next;
22100c75fc71SPierre-Louis Bossart 
22110c75fc71SPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2212848aedfdSPierre-Louis Bossart 			if (ret) {
2213848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start++;
2214b2ae8066STakashi Iwai 				goto next;
2215848aedfdSPierre-Louis Bossart 			}
22160c75fc71SPierre-Louis Bossart 
22173202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
221801d7584cSLiam Girdwood 			break;
221901d7584cSLiam Girdwood 		}
2220b2ae8066STakashi Iwai next:
2221b2ae8066STakashi Iwai 		snd_soc_dpcm_stream_unlock_irqrestore(be, stream, flags);
2222b2ae8066STakashi Iwai 		if (ret)
2223b2ae8066STakashi Iwai 			break;
222401d7584cSLiam Girdwood 	}
2225db3aa39cSKuninori Morimoto 	if (ret < 0)
2226db3aa39cSKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2227db3aa39cSKuninori Morimoto 			__func__, be->dai_link->name, ret);
222801d7584cSLiam Girdwood 	return ret;
222901d7584cSLiam Girdwood }
223001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
223101d7584cSLiam Girdwood 
2232acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2233acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2234acbf2774SRanjani Sridharan {
22350ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2236acbf2774SRanjani Sridharan 	int ret;
2237acbf2774SRanjani Sridharan 
2238acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2239acbf2774SRanjani Sridharan 	if (fe_first) {
2240acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2241acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2242acbf2774SRanjani Sridharan 
2243acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2244acbf2774SRanjani Sridharan 		if (ret < 0)
2245acbf2774SRanjani Sridharan 			return ret;
2246acbf2774SRanjani Sridharan 
2247acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2248acbf2774SRanjani Sridharan 		return ret;
2249acbf2774SRanjani Sridharan 	}
2250acbf2774SRanjani Sridharan 
2251acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2252acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2253acbf2774SRanjani Sridharan 	if (ret < 0)
2254acbf2774SRanjani Sridharan 		return ret;
2255acbf2774SRanjani Sridharan 
2256acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2257acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2258acbf2774SRanjani Sridharan 
2259acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2260acbf2774SRanjani Sridharan 
2261acbf2774SRanjani Sridharan 	return ret;
2262acbf2774SRanjani Sridharan }
2263acbf2774SRanjani Sridharan 
2264ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
226501d7584cSLiam Girdwood {
22660ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2267acbf2774SRanjani Sridharan 	int stream = substream->stream;
2268acbf2774SRanjani Sridharan 	int ret = 0;
226901d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
227001d7584cSLiam Girdwood 
227101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
227201d7584cSLiam Girdwood 
227301d7584cSLiam Girdwood 	switch (trigger) {
227401d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2275acbf2774SRanjani Sridharan 		switch (cmd) {
2276acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2277acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2278acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
22794c22b80fSCezary Rojewski 		case SNDRV_PCM_TRIGGER_DRAIN:
2280acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2281acbf2774SRanjani Sridharan 			break;
2282acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2283acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2284acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2285acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2286acbf2774SRanjani Sridharan 			break;
2287acbf2774SRanjani Sridharan 		default:
2288acbf2774SRanjani Sridharan 			ret = -EINVAL;
2289acbf2774SRanjani Sridharan 			break;
229001d7584cSLiam Girdwood 		}
229101d7584cSLiam Girdwood 		break;
229201d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2293acbf2774SRanjani Sridharan 		switch (cmd) {
2294acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2295acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2296acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
22974c22b80fSCezary Rojewski 		case SNDRV_PCM_TRIGGER_DRAIN:
2298acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2299acbf2774SRanjani Sridharan 			break;
2300acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2301acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2302acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2303acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2304acbf2774SRanjani Sridharan 			break;
2305acbf2774SRanjani Sridharan 		default:
2306acbf2774SRanjani Sridharan 			ret = -EINVAL;
2307acbf2774SRanjani Sridharan 			break;
230801d7584cSLiam Girdwood 		}
230901d7584cSLiam Girdwood 		break;
231007bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
231107bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
231207bf84aaSLiam Girdwood 
2313103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
231407bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
231507bf84aaSLiam Girdwood 
231630819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
231707bf84aaSLiam Girdwood 		break;
231801d7584cSLiam Girdwood 	default:
2319103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
232001d7584cSLiam Girdwood 				fe->dai_link->name);
232101d7584cSLiam Girdwood 		ret = -EINVAL;
232201d7584cSLiam Girdwood 		goto out;
232301d7584cSLiam Girdwood 	}
232401d7584cSLiam Girdwood 
2325acbf2774SRanjani Sridharan 	if (ret < 0) {
2326acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2327acbf2774SRanjani Sridharan 			cmd, ret);
2328acbf2774SRanjani Sridharan 		goto out;
2329acbf2774SRanjani Sridharan 	}
2330acbf2774SRanjani Sridharan 
233101d7584cSLiam Girdwood 	switch (cmd) {
233201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
233301d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
233401d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
233501d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
233601d7584cSLiam Girdwood 		break;
233701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
233801d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
233901d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
234001d7584cSLiam Girdwood 		break;
23419f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
23429f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
23439f169b9fSPatrick Lai 		break;
234401d7584cSLiam Girdwood 	}
234501d7584cSLiam Girdwood 
234601d7584cSLiam Girdwood out:
234701d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
234801d7584cSLiam Girdwood 	return ret;
234901d7584cSLiam Girdwood }
235001d7584cSLiam Girdwood 
2351ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2352ea9d0d77STakashi Iwai {
23530ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2354ea9d0d77STakashi Iwai 	int stream = substream->stream;
2355ea9d0d77STakashi Iwai 
2356ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2357ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2358ea9d0d77STakashi Iwai 	 */
2359ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2360ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2361ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2362ea9d0d77STakashi Iwai 	}
2363ea9d0d77STakashi Iwai 
2364ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2365ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2366ea9d0d77STakashi Iwai }
2367ea9d0d77STakashi Iwai 
236823607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
236901d7584cSLiam Girdwood {
237001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
237101d7584cSLiam Girdwood 	int ret = 0;
237201d7584cSLiam Girdwood 
23738d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
237401d7584cSLiam Girdwood 
237501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
237601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
237701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
237801d7584cSLiam Girdwood 
237901d7584cSLiam Girdwood 		/* is this op for this BE ? */
238001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
238101d7584cSLiam Girdwood 			continue;
238201d7584cSLiam Girdwood 
238301d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
238495f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
23855087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
23865087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
238701d7584cSLiam Girdwood 			continue;
238801d7584cSLiam Girdwood 
2389103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
239094d215ccS彭东林 			be->dai_link->name);
239101d7584cSLiam Girdwood 
2392b7898396STakashi Iwai 		ret = __soc_pcm_prepare(be, be_substream);
2393dab7eeb4SKuninori Morimoto 		if (ret < 0)
239401d7584cSLiam Girdwood 			break;
239501d7584cSLiam Girdwood 
239601d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
239701d7584cSLiam Girdwood 	}
2398273db971SKuninori Morimoto 
2399273db971SKuninori Morimoto 	if (ret < 0)
2400273db971SKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2401273db971SKuninori Morimoto 
240201d7584cSLiam Girdwood 	return ret;
240301d7584cSLiam Girdwood }
240401d7584cSLiam Girdwood 
240545c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
240601d7584cSLiam Girdwood {
24070ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
240801d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
240901d7584cSLiam Girdwood 
2410b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
241101d7584cSLiam Girdwood 
2412103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
241301d7584cSLiam Girdwood 
2414ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
241501d7584cSLiam Girdwood 
241601d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
241701d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2418103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
241901d7584cSLiam Girdwood 				fe->dai_link->name);
242001d7584cSLiam Girdwood 		ret = -EINVAL;
242101d7584cSLiam Girdwood 		goto out;
242201d7584cSLiam Girdwood 	}
242301d7584cSLiam Girdwood 
242425c2f515SKuninori Morimoto 	ret = dpcm_be_dai_prepare(fe, stream);
242501d7584cSLiam Girdwood 	if (ret < 0)
242601d7584cSLiam Girdwood 		goto out;
242701d7584cSLiam Girdwood 
242801d7584cSLiam Girdwood 	/* call prepare on the frontend */
2429b7898396STakashi Iwai 	ret = __soc_pcm_prepare(fe, substream);
2430dab7eeb4SKuninori Morimoto 	if (ret < 0)
243101d7584cSLiam Girdwood 		goto out;
243201d7584cSLiam Girdwood 
243301d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
243401d7584cSLiam Girdwood 
243501d7584cSLiam Girdwood out:
2436ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2437b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
243801d7584cSLiam Girdwood 
2439273db971SKuninori Morimoto 	if (ret < 0)
2440273db971SKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2441273db971SKuninori Morimoto 
244201d7584cSLiam Girdwood 	return ret;
244301d7584cSLiam Girdwood }
244401d7584cSLiam Girdwood 
2445618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2446618dae11SLiam Girdwood {
244707bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
244807bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
244907bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2450618dae11SLiam Girdwood 	int err;
245101d7584cSLiam Girdwood 
2452103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2453618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2454618dae11SLiam Girdwood 
245507bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
245607bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2457103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
245807bf84aaSLiam Girdwood 				fe->dai_link->name);
245907bf84aaSLiam Girdwood 
246030819358SKuninori Morimoto 		err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
246107bf84aaSLiam Girdwood 	} else {
2462103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
246307bf84aaSLiam Girdwood 			fe->dai_link->name);
246407bf84aaSLiam Girdwood 
2465618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
246607bf84aaSLiam Girdwood 	}
2467618dae11SLiam Girdwood 
2468f52366e6SKuninori Morimoto 	dpcm_be_dai_hw_free(fe, stream);
2469618dae11SLiam Girdwood 
2470531590bbSKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
2471618dae11SLiam Girdwood 
2472618dae11SLiam Girdwood 	/* run the stream event for each BE */
2473618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2474618dae11SLiam Girdwood 
247581c82a9eSKuninori Morimoto 	if (err < 0)
247681c82a9eSKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
247781c82a9eSKuninori Morimoto 
247881c82a9eSKuninori Morimoto 	return err;
2479618dae11SLiam Girdwood }
2480618dae11SLiam Girdwood 
2481618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2482618dae11SLiam Girdwood {
248307bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
248407bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2485618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
248607bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
24874eeed5f4SSouptick Joarder 	int ret = 0;
2488618dae11SLiam Girdwood 
2489103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2490618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2491618dae11SLiam Girdwood 
2492618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2493618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
24942c138284S朱灿灿 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
24952c138284S朱灿灿 		dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
24962c138284S朱灿灿 			fe->dai_link->name, fe->dpcm[stream].state);
2497e91b65b3SDan Carpenter 		ret = -EINVAL;
24982c138284S朱灿灿 		goto disconnect;
24992c138284S朱灿灿 	}
2500618dae11SLiam Girdwood 
2501618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2502618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2503fffc0ca2SDan Carpenter 	if (ret < 0)
2504618dae11SLiam Girdwood 		goto disconnect;
2505618dae11SLiam Girdwood 
2506618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2507618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2508618dae11SLiam Girdwood 		return 0;
2509618dae11SLiam Girdwood 
2510618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2511fffc0ca2SDan Carpenter 	if (ret < 0)
2512618dae11SLiam Girdwood 		goto close;
2513618dae11SLiam Girdwood 
2514618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2515618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2516618dae11SLiam Girdwood 		return 0;
2517618dae11SLiam Girdwood 
2518618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2519fffc0ca2SDan Carpenter 	if (ret < 0)
2520618dae11SLiam Girdwood 		goto hw_free;
2521618dae11SLiam Girdwood 
2522618dae11SLiam Girdwood 	/* run the stream event for each BE */
2523618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2524618dae11SLiam Girdwood 
2525618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2526618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2527618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2528618dae11SLiam Girdwood 		return 0;
2529618dae11SLiam Girdwood 
253007bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
253107bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2532103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
253307bf84aaSLiam Girdwood 				fe->dai_link->name);
253407bf84aaSLiam Girdwood 
253530819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
253662462e01SKuninori Morimoto 		if (ret < 0)
253707bf84aaSLiam Girdwood 			goto hw_free;
253807bf84aaSLiam Girdwood 	} else {
2539103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2540618dae11SLiam Girdwood 			fe->dai_link->name);
2541618dae11SLiam Girdwood 
2542618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2543618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2544db3aa39cSKuninori Morimoto 		if (ret < 0)
2545618dae11SLiam Girdwood 			goto hw_free;
2546618dae11SLiam Girdwood 	}
2547618dae11SLiam Girdwood 
2548618dae11SLiam Girdwood 	return 0;
2549618dae11SLiam Girdwood 
2550618dae11SLiam Girdwood hw_free:
2551618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2552618dae11SLiam Girdwood close:
2553618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2554618dae11SLiam Girdwood disconnect:
25552c138284S朱灿灿 	/* disconnect any pending BEs */
25568d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2557618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
25582c138284S朱灿灿 
25592c138284S朱灿灿 		/* is this op for this BE ? */
25602c138284S朱灿灿 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
25612c138284S朱灿灿 			continue;
25622c138284S朱灿灿 
25632c138284S朱灿灿 		if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
25642c138284S朱灿灿 			be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2565618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2566618dae11SLiam Girdwood 	}
2567618dae11SLiam Girdwood 
256881c82a9eSKuninori Morimoto 	if (ret < 0)
256981c82a9eSKuninori Morimoto 		dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
257081c82a9eSKuninori Morimoto 
2571618dae11SLiam Girdwood 	return ret;
2572618dae11SLiam Girdwood }
2573618dae11SLiam Girdwood 
2574de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2575618dae11SLiam Girdwood {
2576618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
25777083f877SKuninori Morimoto 	int stream;
2578de15d7ffSJerome Brunet 	int count, paths;
2579618dae11SLiam Girdwood 
258096bf62f0SPierre-Louis Bossart 	if (!fe->dai_link->dynamic)
258196bf62f0SPierre-Louis Bossart 		return 0;
258296bf62f0SPierre-Louis Bossart 
25836e1276a5SBard Liao 	if (fe->num_cpus > 1) {
25846e1276a5SBard Liao 		dev_err(fe->dev,
25856e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
25866e1276a5SBard Liao 		return -EINVAL;
25876e1276a5SBard Liao 	}
25886e1276a5SBard Liao 
2589618dae11SLiam Girdwood 	/* only check active links */
2590b3dea624SKuninori Morimoto 	if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2591de15d7ffSJerome Brunet 		return 0;
2592618dae11SLiam Girdwood 
2593618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2594de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2595de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2596618dae11SLiam Girdwood 
25977083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2598075207d2SQiao Zhou 
25997083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
2600c2233a26SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0),   stream) ||
2601c2233a26SKuninori Morimoto 		    !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
26027083f877SKuninori Morimoto 			continue;
2603618dae11SLiam Girdwood 
26047083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
2605b3dea624SKuninori Morimoto 		if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2606b3dea624SKuninori Morimoto 		    !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
26077083f877SKuninori Morimoto 			continue;
26087083f877SKuninori Morimoto 
26097083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2610d479f00bSKuninori Morimoto 		if (paths < 0)
2611618dae11SLiam Girdwood 			return paths;
2612618dae11SLiam Girdwood 
26137083f877SKuninori Morimoto 		/* update any playback/capture paths */
26147083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2615de15d7ffSJerome Brunet 		if (count) {
2616580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2617de15d7ffSJerome Brunet 			if (new)
261881c82a9eSKuninori Morimoto 				dpcm_run_update_startup(fe, stream);
2619de15d7ffSJerome Brunet 			else
262081c82a9eSKuninori Morimoto 				dpcm_run_update_shutdown(fe, stream);
2621580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2622de15d7ffSJerome Brunet 
26237083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
26247083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2625618dae11SLiam Girdwood 		}
2626618dae11SLiam Girdwood 
26277ed9de76SQiao Zhou 		dpcm_path_put(&list);
2628618dae11SLiam Girdwood 	}
2629618dae11SLiam Girdwood 
2630de15d7ffSJerome Brunet 	return 0;
2631618dae11SLiam Girdwood }
2632618dae11SLiam Girdwood 
2633de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2634de15d7ffSJerome Brunet  * any DAI links.
2635de15d7ffSJerome Brunet  */
2636f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2637de15d7ffSJerome Brunet {
2638de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2639de15d7ffSJerome Brunet 	int ret = 0;
2640de15d7ffSJerome Brunet 
2641b7898396STakashi Iwai 	mutex_lock_nested(&card->pcm_mutex, card->pcm_subclass);
2642de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2643bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2644de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2645de15d7ffSJerome Brunet 		if (ret)
2646de15d7ffSJerome Brunet 			goto out;
2647de15d7ffSJerome Brunet 	}
2648de15d7ffSJerome Brunet 
2649de15d7ffSJerome Brunet 	/* bring new paths up */
2650bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2651de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2652de15d7ffSJerome Brunet 		if (ret)
2653de15d7ffSJerome Brunet 			goto out;
2654de15d7ffSJerome Brunet 	}
2655de15d7ffSJerome Brunet 
2656de15d7ffSJerome Brunet out:
2657b7898396STakashi Iwai 	mutex_unlock(&card->pcm_mutex);
2658de15d7ffSJerome Brunet 	return ret;
2659618dae11SLiam Girdwood }
2660f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
266101d7584cSLiam Girdwood 
2662265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
266301d7584cSLiam Girdwood {
26640ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
266501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2666265694b6SKuninori Morimoto 	int stream = fe_substream->stream;
266730fca26fSKuninori Morimoto 
2668b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
2669b7898396STakashi Iwai 
267030fca26fSKuninori Morimoto 	/* mark FE's links ready to prune */
267130fca26fSKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
267230fca26fSKuninori Morimoto 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
267330fca26fSKuninori Morimoto 
267430fca26fSKuninori Morimoto 	dpcm_be_disconnect(fe, stream);
267530fca26fSKuninori Morimoto 
267630fca26fSKuninori Morimoto 	fe->dpcm[stream].runtime = NULL;
2677265694b6SKuninori Morimoto }
2678265694b6SKuninori Morimoto 
2679265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2680265694b6SKuninori Morimoto {
26810ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2682265694b6SKuninori Morimoto 	int ret;
2683265694b6SKuninori Morimoto 
2684b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
2685265694b6SKuninori Morimoto 	ret = dpcm_fe_dai_shutdown(fe_substream);
2686265694b6SKuninori Morimoto 
2687265694b6SKuninori Morimoto 	dpcm_fe_dai_cleanup(fe_substream);
2688265694b6SKuninori Morimoto 
2689b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
269030fca26fSKuninori Morimoto 	return ret;
269130fca26fSKuninori Morimoto }
269230fca26fSKuninori Morimoto 
269301d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
269401d7584cSLiam Girdwood {
26950ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
269601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
269701d7584cSLiam Girdwood 	int ret;
269801d7584cSLiam Girdwood 	int stream = fe_substream->stream;
269901d7584cSLiam Girdwood 
2700b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
270101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
270201d7584cSLiam Girdwood 
27038f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
2704d479f00bSKuninori Morimoto 	if (ret < 0)
2705cae06eb9SKuninori Morimoto 		goto open_end;
270601d7584cSLiam Girdwood 
270701d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
270801d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
270901d7584cSLiam Girdwood 
271001d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
2711265694b6SKuninori Morimoto 	if (ret < 0)
2712265694b6SKuninori Morimoto 		dpcm_fe_dai_cleanup(fe_substream);
271301d7584cSLiam Girdwood 
271401d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
271501d7584cSLiam Girdwood 	dpcm_path_put(&list);
2716cae06eb9SKuninori Morimoto open_end:
2717b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
271801d7584cSLiam Girdwood 	return ret;
271901d7584cSLiam Girdwood }
272001d7584cSLiam Girdwood 
27217fc6bebdSKuninori Morimoto static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
27227fc6bebdSKuninori Morimoto 				    int *playback, int *capture)
2723ddee627cSLiam Girdwood {
272419bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
27252e5894d7SBenoit Cousson 	int i;
2726ddee627cSLiam Girdwood 
2727b73287f0SPierre-Louis Bossart 	if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
27289b5db059SStephan Gerhold 		dev_err(rtd->dev,
2729b73287f0SPierre-Louis Bossart 			"DPCM doesn't support Multi CPU for Front-Ends yet\n");
27309b5db059SStephan Gerhold 		return -EINVAL;
27319b5db059SStephan Gerhold 	}
27329b5db059SStephan Gerhold 
2733b73287f0SPierre-Louis Bossart 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2734940a1f43SKuninori Morimoto 		int stream;
2735940a1f43SKuninori Morimoto 
2736b73287f0SPierre-Louis Bossart 		if (rtd->dai_link->dpcm_playback) {
2737b73287f0SPierre-Louis Bossart 			stream = SNDRV_PCM_STREAM_PLAYBACK;
2738b73287f0SPierre-Louis Bossart 
27394f872154SPierre-Louis Bossart 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
27404f872154SPierre-Louis Bossart 				if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
27417fc6bebdSKuninori Morimoto 					*playback = 1;
27424f872154SPierre-Louis Bossart 					break;
27434f872154SPierre-Louis Bossart 				}
27444f872154SPierre-Louis Bossart 			}
27457fc6bebdSKuninori Morimoto 			if (!*playback) {
2746b73287f0SPierre-Louis Bossart 				dev_err(rtd->card->dev,
27474f872154SPierre-Louis Bossart 					"No CPU DAIs support playback for stream %s\n",
2748b73287f0SPierre-Louis Bossart 					rtd->dai_link->stream_name);
2749b73287f0SPierre-Louis Bossart 				return -EINVAL;
2750b73287f0SPierre-Louis Bossart 			}
2751b73287f0SPierre-Louis Bossart 		}
2752b73287f0SPierre-Louis Bossart 		if (rtd->dai_link->dpcm_capture) {
2753b73287f0SPierre-Louis Bossart 			stream = SNDRV_PCM_STREAM_CAPTURE;
2754b73287f0SPierre-Louis Bossart 
27554f872154SPierre-Louis Bossart 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
27564f872154SPierre-Louis Bossart 				if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
27577fc6bebdSKuninori Morimoto 					*capture = 1;
27584f872154SPierre-Louis Bossart 					break;
27594f872154SPierre-Louis Bossart 				}
27604f872154SPierre-Louis Bossart 			}
27614f872154SPierre-Louis Bossart 
27627fc6bebdSKuninori Morimoto 			if (!*capture) {
2763b73287f0SPierre-Louis Bossart 				dev_err(rtd->card->dev,
27644f872154SPierre-Louis Bossart 					"No CPU DAIs support capture for stream %s\n",
2765b73287f0SPierre-Louis Bossart 					rtd->dai_link->stream_name);
2766b73287f0SPierre-Louis Bossart 				return -EINVAL;
2767b73287f0SPierre-Louis Bossart 			}
2768b73287f0SPierre-Louis Bossart 		}
276901d7584cSLiam Girdwood 	} else {
2770940a1f43SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
2771940a1f43SKuninori Morimoto 
2772a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2773a4877a6fSStephan Gerhold 		int cpu_capture = rtd->dai_link->params ?
2774a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2775a4877a6fSStephan Gerhold 		int cpu_playback = rtd->dai_link->params ?
2776a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2777a342031cSJerome Brunet 
2778a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
277919bdcc7aSShreyas NC 			if (rtd->num_cpus == 1) {
2780c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, 0);
278119bdcc7aSShreyas NC 			} else if (rtd->num_cpus == rtd->num_codecs) {
2782c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, i);
278319bdcc7aSShreyas NC 			} else {
278419bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
278519bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
278619bdcc7aSShreyas NC 				return -EINVAL;
278719bdcc7aSShreyas NC 			}
278819bdcc7aSShreyas NC 
2789467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2790a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
27917fc6bebdSKuninori Morimoto 				*playback = 1;
2792467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2793a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
27947fc6bebdSKuninori Morimoto 				*capture = 1;
279501d7584cSLiam Girdwood 		}
27962e5894d7SBenoit Cousson 	}
27972e5894d7SBenoit Cousson 
2798d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
27997fc6bebdSKuninori Morimoto 		*playback = 1;
28007fc6bebdSKuninori Morimoto 		*capture = 0;
2801d6bead02SFabio Estevam 	}
2802d6bead02SFabio Estevam 
2803d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
28047fc6bebdSKuninori Morimoto 		*playback = 0;
28057fc6bebdSKuninori Morimoto 		*capture = 1;
2806d6bead02SFabio Estevam 	}
2807d6bead02SFabio Estevam 
28087fc6bebdSKuninori Morimoto 	return 0;
28097fc6bebdSKuninori Morimoto }
28107fc6bebdSKuninori Morimoto 
28112b39123bSKuninori Morimoto static int soc_create_pcm(struct snd_pcm **pcm,
28122b39123bSKuninori Morimoto 			  struct snd_soc_pcm_runtime *rtd,
28132b39123bSKuninori Morimoto 			  int playback, int capture, int num)
28147fc6bebdSKuninori Morimoto {
28157fc6bebdSKuninori Morimoto 	char new_name[64];
28162b39123bSKuninori Morimoto 	int ret;
28177fc6bebdSKuninori Morimoto 
281801d7584cSLiam Girdwood 	/* create the PCM */
2819a342031cSJerome Brunet 	if (rtd->dai_link->params) {
2820a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2821a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2822a342031cSJerome Brunet 
2823a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
28242b39123bSKuninori Morimoto 					   playback, capture, pcm);
2825a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
282601d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
282701d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
282801d7584cSLiam Girdwood 
282901d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
28302b39123bSKuninori Morimoto 				playback, capture, pcm);
283101d7584cSLiam Girdwood 	} else {
283201d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
283301d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
283401d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
283501d7584cSLiam Girdwood 		else
283601d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
28372e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
28386fb8944cSKuninori Morimoto 				soc_codec_dai_name(rtd), num);
283901d7584cSLiam Girdwood 
284001d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
28412b39123bSKuninori Morimoto 			capture, pcm);
284201d7584cSLiam Girdwood 	}
2843ddee627cSLiam Girdwood 	if (ret < 0) {
2844799827a4SPierre-Louis Bossart 		dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2845799827a4SPierre-Louis Bossart 			new_name, rtd->dai_link->name, ret);
2846ddee627cSLiam Girdwood 		return ret;
2847ddee627cSLiam Girdwood 	}
2848103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2849ddee627cSLiam Girdwood 
28502b39123bSKuninori Morimoto 	return 0;
28512b39123bSKuninori Morimoto }
28522b39123bSKuninori Morimoto 
28532b39123bSKuninori Morimoto /* create a new pcm */
28542b39123bSKuninori Morimoto int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
28552b39123bSKuninori Morimoto {
28562b39123bSKuninori Morimoto 	struct snd_soc_component *component;
28572b39123bSKuninori Morimoto 	struct snd_pcm *pcm;
28582b39123bSKuninori Morimoto 	int ret = 0, playback = 0, capture = 0;
28592b39123bSKuninori Morimoto 	int i;
28602b39123bSKuninori Morimoto 
28612b39123bSKuninori Morimoto 	ret = soc_get_playback_capture(rtd, &playback, &capture);
28622b39123bSKuninori Morimoto 	if (ret < 0)
28632b39123bSKuninori Morimoto 		return ret;
28642b39123bSKuninori Morimoto 
28652b39123bSKuninori Morimoto 	ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
28662b39123bSKuninori Morimoto 	if (ret < 0)
28672b39123bSKuninori Morimoto 		return ret;
28682b39123bSKuninori Morimoto 
2869ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2870a342031cSJerome Brunet 	if (rtd->dai_link->params)
28714bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2872a342031cSJerome Brunet 	else
287383f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2874ddee627cSLiam Girdwood 
2875ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2876e04e7b8cSKuninori Morimoto 	pcm->nonatomic = rtd->dai_link->nonatomic;
2877ddee627cSLiam Girdwood 	pcm->private_data = rtd;
287801d7584cSLiam Girdwood 
2879a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
288001d7584cSLiam Girdwood 		if (playback)
288101d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
288201d7584cSLiam Girdwood 		if (capture)
288301d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
288401d7584cSLiam Girdwood 		goto out;
288501d7584cSLiam Girdwood 	}
288601d7584cSLiam Girdwood 
288701d7584cSLiam Girdwood 	/* ASoC PCM operations */
288801d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
288901d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
289001d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
289101d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
289201d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
289301d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
289401d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
289501d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
289601d7584cSLiam Girdwood 	} else {
289701d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
289801d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
289901d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
290001d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
290101d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
290201d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
290301d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
290401d7584cSLiam Girdwood 	}
290501d7584cSLiam Girdwood 
2906613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
29072b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
2908b8135864SKuninori Morimoto 
29093b1c952cSTakashi Iwai 		if (drv->ioctl)
29103b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
29111e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
29121e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2913e9067bb5SKuninori Morimoto 		if (drv->copy_user)
291482d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2915e9067bb5SKuninori Morimoto 		if (drv->page)
29169c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
2917e9067bb5SKuninori Morimoto 		if (drv->mmap)
2918205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
29198bdfc045SShengjiu Wang 		if (drv->ack)
29208bdfc045SShengjiu Wang 			rtd->ops.ack            = snd_soc_pcm_component_ack;
2921b8135864SKuninori Morimoto 	}
2922b8135864SKuninori Morimoto 
2923ddee627cSLiam Girdwood 	if (playback)
292401d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2925ddee627cSLiam Girdwood 
2926ddee627cSLiam Girdwood 	if (capture)
292701d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2928ddee627cSLiam Girdwood 
2929b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
293060adbd8fSKuninori Morimoto 	if (ret < 0)
2931ddee627cSLiam Girdwood 		return ret;
2932c641e5b2SJohan Hovold 
29333d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
293401d7584cSLiam Girdwood out:
29351d5cd525SPierre-Louis Bossart 	dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
29366fb8944cSKuninori Morimoto 		soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2937ddee627cSLiam Girdwood 	return ret;
2938ddee627cSLiam Girdwood }
293901d7584cSLiam Girdwood 
294001d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
294101d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
294201d7584cSLiam Girdwood {
294301d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
294401d7584cSLiam Girdwood 		return 1;
294501d7584cSLiam Girdwood 	return 0;
294601d7584cSLiam Girdwood }
294701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
294801d7584cSLiam Girdwood 
294901d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
295001d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
295101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
295201d7584cSLiam Girdwood {
295301d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
295401d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
295501d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
295601d7584cSLiam Girdwood 		return 1;
295701d7584cSLiam Girdwood 	return 0;
295801d7584cSLiam Girdwood }
295901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
296001d7584cSLiam Girdwood 
296101d7584cSLiam Girdwood /* get the substream for this BE */
296201d7584cSLiam Girdwood struct snd_pcm_substream *
296301d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
296401d7584cSLiam Girdwood {
296501d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
296601d7584cSLiam Girdwood }
296701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
296801d7584cSLiam Girdwood 
2969085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2970085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
2971085d22beSKuninori Morimoto 				    int stream,
2972085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
2973085d22beSKuninori Morimoto 				    int num_states)
297401d7584cSLiam Girdwood {
297501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
297601d7584cSLiam Girdwood 	int state;
2977a9764869SKaiChieh Chuang 	int ret = 1;
2978085d22beSKuninori Morimoto 	int i;
297901d7584cSLiam Girdwood 
2980d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
298101d7584cSLiam Girdwood 
298201d7584cSLiam Girdwood 		if (dpcm->fe == fe)
298301d7584cSLiam Girdwood 			continue;
298401d7584cSLiam Girdwood 
298501d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
2986085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
2987085d22beSKuninori Morimoto 			if (state == states[i]) {
2988a9764869SKaiChieh Chuang 				ret = 0;
2989a9764869SKaiChieh Chuang 				break;
299001d7584cSLiam Girdwood 			}
2991a9764869SKaiChieh Chuang 		}
2992085d22beSKuninori Morimoto 	}
299301d7584cSLiam Girdwood 
2994085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
2995a9764869SKaiChieh Chuang 	return ret;
299601d7584cSLiam Girdwood }
2997085d22beSKuninori Morimoto 
2998085d22beSKuninori Morimoto /*
2999085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3000085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3001085d22beSKuninori Morimoto  */
3002085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3003085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3004085d22beSKuninori Morimoto {
3005085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3006085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3007085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3008085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3009085d22beSKuninori Morimoto 	};
3010085d22beSKuninori Morimoto 
3011085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3012085d22beSKuninori Morimoto }
301301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
301401d7584cSLiam Girdwood 
301501d7584cSLiam Girdwood /*
301601d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
301701d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
301801d7584cSLiam Girdwood  */
301901d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
302001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
302101d7584cSLiam Girdwood {
3022085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3023085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3024085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3025085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3026085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3027085d22beSKuninori Morimoto 	};
302801d7584cSLiam Girdwood 
3029085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
303001d7584cSLiam Girdwood }
303101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3032