xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 092830cf550667d5fa6286605167d232f2c1f61e)
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>
17ddee627cSLiam Girdwood #include <linux/slab.h>
18ddee627cSLiam Girdwood #include <linux/workqueue.h>
1901d7584cSLiam Girdwood #include <linux/export.h>
20f86dcef8SLiam Girdwood #include <linux/debugfs.h>
21ddee627cSLiam Girdwood #include <sound/core.h>
22ddee627cSLiam Girdwood #include <sound/pcm.h>
23ddee627cSLiam Girdwood #include <sound/pcm_params.h>
24ddee627cSLiam Girdwood #include <sound/soc.h>
2501d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
26a5e6c109SKuninori Morimoto #include <sound/soc-link.h>
27ddee627cSLiam Girdwood #include <sound/initval.h>
28ddee627cSLiam Girdwood 
2904110728SKuninori Morimoto #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
3004110728SKuninori Morimoto static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
3104110728SKuninori Morimoto 			       const char *func, int ret)
3204110728SKuninori Morimoto {
3304110728SKuninori Morimoto 	/* Positive, Zero values are not errors */
3404110728SKuninori Morimoto 	if (ret >= 0)
3504110728SKuninori Morimoto 		return ret;
3604110728SKuninori Morimoto 
3704110728SKuninori Morimoto 	/* Negative values might be errors */
3804110728SKuninori Morimoto 	switch (ret) {
3904110728SKuninori Morimoto 	case -EPROBE_DEFER:
4004110728SKuninori Morimoto 	case -ENOTSUPP:
4104110728SKuninori Morimoto 		break;
4204110728SKuninori Morimoto 	default:
4304110728SKuninori Morimoto 		dev_err(rtd->dev,
4404110728SKuninori Morimoto 			"ASoC: error at %s on %s: %d\n",
4504110728SKuninori Morimoto 			func, rtd->dai_link->name, ret);
4604110728SKuninori Morimoto 	}
4704110728SKuninori Morimoto 
4804110728SKuninori Morimoto 	return ret;
4904110728SKuninori Morimoto }
5004110728SKuninori Morimoto 
51b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
52b7898396STakashi Iwai 						int stream)
53b7898396STakashi Iwai {
54b7898396STakashi Iwai 	snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
55b7898396STakashi Iwai }
56b7898396STakashi Iwai 
573c75c0eaSTakashi Iwai #define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
583c75c0eaSTakashi Iwai 	snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
59b2ae8066STakashi Iwai 
60b7898396STakashi Iwai static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
61b7898396STakashi Iwai 						  int stream)
62b7898396STakashi Iwai {
63b7898396STakashi Iwai 	snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
64b7898396STakashi Iwai }
65b7898396STakashi Iwai 
66b2ae8066STakashi Iwai #define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
67b2ae8066STakashi Iwai 	snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
68b2ae8066STakashi Iwai 
6901d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS	8
7001d7584cSLiam Girdwood 
716fb8944cSKuninori Morimoto static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
726fb8944cSKuninori Morimoto {
733989ade2SKuninori Morimoto 	return (rtd)->dai_link->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
746fb8944cSKuninori Morimoto }
756fb8944cSKuninori Morimoto static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
766fb8944cSKuninori Morimoto {
773989ade2SKuninori Morimoto 	return (rtd)->dai_link->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
786fb8944cSKuninori Morimoto }
796fb8944cSKuninori Morimoto 
80c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS
81c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
82c3212829SKuninori Morimoto {
83c3212829SKuninori Morimoto 	switch (state) {
84c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_NEW:
85c3212829SKuninori Morimoto 		return "new";
86c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_OPEN:
87c3212829SKuninori Morimoto 		return "open";
88c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_PARAMS:
89c3212829SKuninori Morimoto 		return "hw_params";
90c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PREPARE:
91c3212829SKuninori Morimoto 		return "prepare";
92c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_START:
93c3212829SKuninori Morimoto 		return "start";
94c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_STOP:
95c3212829SKuninori Morimoto 		return "stop";
96c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_SUSPEND:
97c3212829SKuninori Morimoto 		return "suspend";
98c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PAUSED:
99c3212829SKuninori Morimoto 		return "paused";
100c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_FREE:
101c3212829SKuninori Morimoto 		return "hw_free";
102c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_CLOSE:
103c3212829SKuninori Morimoto 		return "close";
104c3212829SKuninori Morimoto 	}
105c3212829SKuninori Morimoto 
106c3212829SKuninori Morimoto 	return "unknown";
107c3212829SKuninori Morimoto }
108c3212829SKuninori Morimoto 
109c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
110c3212829SKuninori Morimoto 			       int stream, char *buf, size_t size)
111c3212829SKuninori Morimoto {
112c3212829SKuninori Morimoto 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
113c3212829SKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
114c3212829SKuninori Morimoto 	ssize_t offset = 0;
115c3212829SKuninori Morimoto 
116c3212829SKuninori Morimoto 	/* FE state */
117d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset,
118c3212829SKuninori Morimoto 			   "[%s - %s]\n", fe->dai_link->name,
119c3212829SKuninori Morimoto 			   stream ? "Capture" : "Playback");
120c3212829SKuninori Morimoto 
121d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset, "State: %s\n",
122c3212829SKuninori Morimoto 			   dpcm_state_string(fe->dpcm[stream].state));
123c3212829SKuninori Morimoto 
124c3212829SKuninori Morimoto 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
125c3212829SKuninori Morimoto 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
126d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
127c3212829SKuninori Morimoto 				   "Hardware Params: "
128c3212829SKuninori Morimoto 				   "Format = %s, Channels = %d, Rate = %d\n",
129c3212829SKuninori Morimoto 				   snd_pcm_format_name(params_format(params)),
130c3212829SKuninori Morimoto 				   params_channels(params),
131c3212829SKuninori Morimoto 				   params_rate(params));
132c3212829SKuninori Morimoto 
133c3212829SKuninori Morimoto 	/* BEs state */
134d0c9abb8STakashi Iwai 	offset += scnprintf(buf + offset, size - offset, "Backends:\n");
135c3212829SKuninori Morimoto 
136c3212829SKuninori Morimoto 	if (list_empty(&fe->dpcm[stream].be_clients)) {
137d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
138c3212829SKuninori Morimoto 				   " No active DSP links\n");
139c3212829SKuninori Morimoto 		goto out;
140c3212829SKuninori Morimoto 	}
141c3212829SKuninori Morimoto 
142c3212829SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
143c3212829SKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
14425106550SKuninori Morimoto 		params = &be->dpcm[stream].hw_params;
145c3212829SKuninori Morimoto 
146d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
147c3212829SKuninori Morimoto 				   "- %s\n", be->dai_link->name);
148c3212829SKuninori Morimoto 
149d0c9abb8STakashi Iwai 		offset += scnprintf(buf + offset, size - offset,
150c3212829SKuninori Morimoto 				   "   State: %s\n",
151c3212829SKuninori Morimoto 				   dpcm_state_string(be->dpcm[stream].state));
152c3212829SKuninori Morimoto 
153c3212829SKuninori Morimoto 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
154c3212829SKuninori Morimoto 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
155d0c9abb8STakashi Iwai 			offset += scnprintf(buf + offset, size - offset,
156c3212829SKuninori Morimoto 					   "   Hardware Params: "
157c3212829SKuninori Morimoto 					   "Format = %s, Channels = %d, Rate = %d\n",
158c3212829SKuninori Morimoto 					   snd_pcm_format_name(params_format(params)),
159c3212829SKuninori Morimoto 					   params_channels(params),
160c3212829SKuninori Morimoto 					   params_rate(params));
161c3212829SKuninori Morimoto 	}
162c3212829SKuninori Morimoto out:
163c3212829SKuninori Morimoto 	return offset;
164c3212829SKuninori Morimoto }
165c3212829SKuninori Morimoto 
166c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
167c3212829SKuninori Morimoto 				    size_t count, loff_t *ppos)
168c3212829SKuninori Morimoto {
169c3212829SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = file->private_data;
170c3212829SKuninori Morimoto 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
171c3212829SKuninori Morimoto 	int stream;
172c3212829SKuninori Morimoto 	char *buf;
173c3212829SKuninori Morimoto 
1743989ade2SKuninori Morimoto 	if (fe->dai_link->num_cpus > 1) {
1756e1276a5SBard Liao 		dev_err(fe->dev,
1766e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
1776e1276a5SBard Liao 		return -EINVAL;
1786e1276a5SBard Liao 	}
1796e1276a5SBard Liao 
180c3212829SKuninori Morimoto 	buf = kmalloc(out_count, GFP_KERNEL);
181c3212829SKuninori Morimoto 	if (!buf)
182c3212829SKuninori Morimoto 		return -ENOMEM;
183c3212829SKuninori Morimoto 
184b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
185c3212829SKuninori Morimoto 	for_each_pcm_streams(stream)
186c2233a26SKuninori Morimoto 		if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
187c3212829SKuninori Morimoto 			offset += dpcm_show_state(fe, stream,
188c3212829SKuninori Morimoto 						  buf + offset,
189c3212829SKuninori Morimoto 						  out_count - offset);
190b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
191c3212829SKuninori Morimoto 
192c3212829SKuninori Morimoto 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
193c3212829SKuninori Morimoto 
194c3212829SKuninori Morimoto 	kfree(buf);
195c3212829SKuninori Morimoto 	return ret;
196c3212829SKuninori Morimoto }
197c3212829SKuninori Morimoto 
198c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
199c3212829SKuninori Morimoto 	.open = simple_open,
200c3212829SKuninori Morimoto 	.read = dpcm_state_read_file,
201c3212829SKuninori Morimoto 	.llseek = default_llseek,
202c3212829SKuninori Morimoto };
203c3212829SKuninori Morimoto 
204c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
205c3212829SKuninori Morimoto {
206c3212829SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
207c3212829SKuninori Morimoto 		return;
208c3212829SKuninori Morimoto 
209c3212829SKuninori Morimoto 	if (!rtd->card->debugfs_card_root)
210c3212829SKuninori Morimoto 		return;
211c3212829SKuninori Morimoto 
212c3212829SKuninori Morimoto 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
213c3212829SKuninori Morimoto 						    rtd->card->debugfs_card_root);
214c3212829SKuninori Morimoto 
215c3212829SKuninori Morimoto 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
216c3212829SKuninori Morimoto 			    rtd, &dpcm_state_fops);
217c3212829SKuninori Morimoto }
218154dae87SKuninori Morimoto 
219154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
220154dae87SKuninori Morimoto {
221154dae87SKuninori Morimoto 	char *name;
222154dae87SKuninori Morimoto 
223154dae87SKuninori Morimoto 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
224154dae87SKuninori Morimoto 			 stream ? "capture" : "playback");
225154dae87SKuninori Morimoto 	if (name) {
226154dae87SKuninori Morimoto 		dpcm->debugfs_state = debugfs_create_dir(
227154dae87SKuninori Morimoto 			name, dpcm->fe->debugfs_dpcm_root);
228154dae87SKuninori Morimoto 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
229154dae87SKuninori Morimoto 				   &dpcm->state);
230154dae87SKuninori Morimoto 		kfree(name);
231154dae87SKuninori Morimoto 	}
232154dae87SKuninori Morimoto }
233154dae87SKuninori Morimoto 
234154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
235154dae87SKuninori Morimoto {
236154dae87SKuninori Morimoto 	debugfs_remove_recursive(dpcm->debugfs_state);
237154dae87SKuninori Morimoto }
238154dae87SKuninori Morimoto 
239154dae87SKuninori Morimoto #else
240154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
241154dae87SKuninori Morimoto 					     int stream)
242154dae87SKuninori Morimoto {
243154dae87SKuninori Morimoto }
244154dae87SKuninori Morimoto 
245154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
246154dae87SKuninori Morimoto {
247154dae87SKuninori Morimoto }
248c3212829SKuninori Morimoto #endif
249c3212829SKuninori Morimoto 
2509c6d7f93SKuninori Morimoto /* Set FE's runtime_update state; the state is protected via PCM stream lock
2519c6d7f93SKuninori Morimoto  * for avoiding the race with trigger callback.
2529c6d7f93SKuninori Morimoto  * If the state is unset and a trigger is pending while the previous operation,
2539c6d7f93SKuninori Morimoto  * process the pending trigger action here.
2549c6d7f93SKuninori Morimoto  */
2559c6d7f93SKuninori Morimoto static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
2569c6d7f93SKuninori Morimoto static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2579c6d7f93SKuninori Morimoto 				     int stream, enum snd_soc_dpcm_update state)
2589c6d7f93SKuninori Morimoto {
2599c6d7f93SKuninori Morimoto 	struct snd_pcm_substream *substream =
2609c6d7f93SKuninori Morimoto 		snd_soc_dpcm_get_substream(fe, stream);
2619c6d7f93SKuninori Morimoto 
262b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
2639c6d7f93SKuninori Morimoto 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2649c6d7f93SKuninori Morimoto 		dpcm_fe_dai_do_trigger(substream,
2659c6d7f93SKuninori Morimoto 				       fe->dpcm[stream].trigger_pending - 1);
2669c6d7f93SKuninori Morimoto 		fe->dpcm[stream].trigger_pending = 0;
2679c6d7f93SKuninori Morimoto 	}
2689c6d7f93SKuninori Morimoto 	fe->dpcm[stream].runtime_update = state;
269b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
2709c6d7f93SKuninori Morimoto }
2719c6d7f93SKuninori Morimoto 
272a7e20444SKuninori Morimoto static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
273a7e20444SKuninori Morimoto 				     int stream, enum snd_soc_dpcm_update state)
274a7e20444SKuninori Morimoto {
275a7e20444SKuninori Morimoto 	be->dpcm[stream].runtime_update = state;
276a7e20444SKuninori Morimoto }
277a7e20444SKuninori Morimoto 
278d9051d86SKuninori Morimoto /**
279d9051d86SKuninori Morimoto  * snd_soc_runtime_action() - Increment/Decrement active count for
280d9051d86SKuninori Morimoto  * PCM runtime components
281d9051d86SKuninori Morimoto  * @rtd: ASoC PCM runtime that is activated
282d9051d86SKuninori Morimoto  * @stream: Direction of the PCM stream
283b6d6e9eaSColton Lewis  * @action: Activate stream if 1. Deactivate if -1.
284d9051d86SKuninori Morimoto  *
285d9051d86SKuninori Morimoto  * Increments/Decrements the active count for all the DAIs and components
286d9051d86SKuninori Morimoto  * attached to a PCM runtime.
287d9051d86SKuninori Morimoto  * Should typically be called when a stream is opened.
288d9051d86SKuninori Morimoto  *
289d9051d86SKuninori Morimoto  * Must be called with the rtd->card->pcm_mutex being held
290d9051d86SKuninori Morimoto  */
291d9051d86SKuninori Morimoto void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2927a5aaba4SKuninori Morimoto 			    int stream, int action)
2937a5aaba4SKuninori Morimoto {
294c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
2957a5aaba4SKuninori Morimoto 	int i;
2967a5aaba4SKuninori Morimoto 
297b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
2987a5aaba4SKuninori Morimoto 
299dc829106SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
300dc829106SKuninori Morimoto 		snd_soc_dai_action(dai, stream, action);
3017a5aaba4SKuninori Morimoto }
302d9051d86SKuninori Morimoto EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
30324894b76SLars-Peter Clausen 
30424894b76SLars-Peter Clausen /**
305208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
306208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
307208a1589SLars-Peter Clausen  *
308208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
309208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
310208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
311208a1589SLars-Peter Clausen  * from having the delay.
312208a1589SLars-Peter Clausen  */
313208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
314208a1589SLars-Peter Clausen {
315fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
3162e5894d7SBenoit Cousson 	bool ignore = true;
317613fb500SKuninori Morimoto 	int i;
3182e5894d7SBenoit Cousson 
319208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
320208a1589SLars-Peter Clausen 		return true;
321208a1589SLars-Peter Clausen 
322613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
32372c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
324fbb16563SKuninori Morimoto 
325fbb16563SKuninori Morimoto 	return ignore;
326208a1589SLars-Peter Clausen }
327208a1589SLars-Peter Clausen 
328208a1589SLars-Peter Clausen /**
32990996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
33090996f43SLars-Peter Clausen  * @substream: the pcm substream
33190996f43SLars-Peter Clausen  * @hw: the hardware parameters
33290996f43SLars-Peter Clausen  *
33390996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
33490996f43SLars-Peter Clausen  */
33590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
33690996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
33790996f43SLars-Peter Clausen {
33856e749baSKuninori Morimoto 	substream->runtime->hw = *hw;
33956e749baSKuninori Morimoto 
34090996f43SLars-Peter Clausen 	return 0;
34190996f43SLars-Peter Clausen }
34290996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
34390996f43SLars-Peter Clausen 
34401d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
34523607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
34601d7584cSLiam Girdwood 	int event)
34701d7584cSLiam Girdwood {
34801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
34901d7584cSLiam Girdwood 
350b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
351b7898396STakashi Iwai 
3528d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
35301d7584cSLiam Girdwood 
35401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
35501d7584cSLiam Girdwood 
356103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
35701d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
35801d7584cSLiam Girdwood 
359b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
360b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
361b1cd2e34SBanajit Goswami 			continue;
362b1cd2e34SBanajit Goswami 
36301d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
36401d7584cSLiam Girdwood 	}
36501d7584cSLiam Girdwood 
36601d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
36701d7584cSLiam Girdwood 
36801d7584cSLiam Girdwood 	return 0;
36901d7584cSLiam Girdwood }
37001d7584cSLiam Girdwood 
3712805b8bdSKuninori Morimoto static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
3722805b8bdSKuninori Morimoto 				   struct snd_pcm_hw_params *params)
3732805b8bdSKuninori Morimoto {
3742805b8bdSKuninori Morimoto 	if (params) {
3752805b8bdSKuninori Morimoto 		dai->rate	 = params_rate(params);
3762805b8bdSKuninori Morimoto 		dai->channels	 = params_channels(params);
3772805b8bdSKuninori Morimoto 		dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
3782805b8bdSKuninori Morimoto 	} else {
3792805b8bdSKuninori Morimoto 		dai->rate	 = 0;
3802805b8bdSKuninori Morimoto 		dai->channels	 = 0;
3812805b8bdSKuninori Morimoto 		dai->sample_bits = 0;
3822805b8bdSKuninori Morimoto 	}
3832805b8bdSKuninori Morimoto }
3842805b8bdSKuninori Morimoto 
38517841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
38617841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
387ddee627cSLiam Girdwood {
3880ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
389ddee627cSLiam Girdwood 	int ret;
390ddee627cSLiam Girdwood 
391f8fc9ec5SKuninori Morimoto 	if (!snd_soc_dai_active(soc_dai))
392f8fc9ec5SKuninori Morimoto 		return 0;
393f8fc9ec5SKuninori Morimoto 
394fac110cbSKuninori Morimoto #define __soc_pcm_apply_symmetry(name, NAME)				\
395fac110cbSKuninori Morimoto 	if (soc_dai->name && (soc_dai->driver->symmetric_##name ||	\
396fac110cbSKuninori Morimoto 			      rtd->dai_link->symmetric_##name)) {	\
397fac110cbSKuninori Morimoto 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
398fac110cbSKuninori Morimoto 			#name, soc_dai->name);				\
399fac110cbSKuninori Morimoto 									\
400fac110cbSKuninori Morimoto 		ret = snd_pcm_hw_constraint_single(substream->runtime,	\
401fac110cbSKuninori Morimoto 						   SNDRV_PCM_HW_PARAM_##NAME,\
402fac110cbSKuninori Morimoto 						   soc_dai->name);	\
403fac110cbSKuninori Morimoto 		if (ret < 0) {						\
404fac110cbSKuninori Morimoto 			dev_err(soc_dai->dev,				\
405fac110cbSKuninori Morimoto 				"ASoC: Unable to apply %s constraint: %d\n",\
406fac110cbSKuninori Morimoto 				#name, ret);				\
407fac110cbSKuninori Morimoto 			return ret;					\
408fac110cbSKuninori Morimoto 		}							\
4093635bf09SNicolin Chen 	}
4103635bf09SNicolin Chen 
411fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(rate,		RATE);
412fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(channels,	CHANNELS);
413fac110cbSKuninori Morimoto 	__soc_pcm_apply_symmetry(sample_bits,	SAMPLE_BITS);
414ddee627cSLiam Girdwood 
415ddee627cSLiam Girdwood 	return 0;
416ddee627cSLiam Girdwood }
417ddee627cSLiam Girdwood 
4183635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4193635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4203635bf09SNicolin Chen {
4210ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
4222805b8bdSKuninori Morimoto 	struct snd_soc_dai d;
423c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
42419bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4252805b8bdSKuninori Morimoto 	unsigned int symmetry, i;
4263635bf09SNicolin Chen 
427ee39d77eSKuninori Morimoto 	d.name = __func__;
4282805b8bdSKuninori Morimoto 	soc_pcm_set_dai_params(&d, params);
4293635bf09SNicolin Chen 
4301cacbac4SKuninori Morimoto #define __soc_pcm_params_symmetry(xxx)					\
4311cacbac4SKuninori Morimoto 	symmetry = rtd->dai_link->symmetric_##xxx;			\
4323a906721SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)					\
4331cacbac4SKuninori Morimoto 		symmetry |= dai->driver->symmetric_##xxx;		\
4343a906721SKuninori Morimoto 									\
4353a906721SKuninori Morimoto 	if (symmetry)							\
4363a906721SKuninori Morimoto 		for_each_rtd_cpu_dais(rtd, i, cpu_dai)			\
4379c2ae363SKuninori Morimoto 			if (!snd_soc_dai_is_dummy(cpu_dai) &&		\
4389c2ae363SKuninori Morimoto 			    cpu_dai->xxx && cpu_dai->xxx != d.xxx) {	\
439ee39d77eSKuninori Morimoto 				dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
440ee39d77eSKuninori Morimoto 					#xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
4413a906721SKuninori Morimoto 				return -EINVAL;				\
4423a906721SKuninori Morimoto 			}
4433a906721SKuninori Morimoto 
4443635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
4453a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(rate);
4463a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(channels);
4473a906721SKuninori Morimoto 	__soc_pcm_params_symmetry(sample_bits);
448ddee627cSLiam Girdwood 
449ddee627cSLiam Girdwood 	return 0;
450ddee627cSLiam Girdwood }
451ddee627cSLiam Girdwood 
45268cbc557SKuninori Morimoto static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
45362e5f676SLars-Peter Clausen {
4540ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
45562e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
456c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
4572e5894d7SBenoit Cousson 	unsigned int symmetry, i;
45862e5f676SLars-Peter Clausen 
459f14654ddSKuninori Morimoto 	symmetry = link->symmetric_rate ||
46019bdcc7aSShreyas NC 		link->symmetric_channels ||
461f14654ddSKuninori Morimoto 		link->symmetric_sample_bits;
46219bdcc7aSShreyas NC 
463c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
46419bdcc7aSShreyas NC 		symmetry = symmetry ||
465f14654ddSKuninori Morimoto 			dai->driver->symmetric_rate ||
466c840f769SKuninori Morimoto 			dai->driver->symmetric_channels ||
467f14654ddSKuninori Morimoto 			dai->driver->symmetric_sample_bits;
4682e5894d7SBenoit Cousson 
46968cbc557SKuninori Morimoto 	if (symmetry)
47068cbc557SKuninori Morimoto 		substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
47162e5f676SLars-Peter Clausen }
47262e5f676SLars-Peter Clausen 
4732e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
47458ba9b25SMark Brown {
4750ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
476c6068d3aSTakashi Iwai 	int ret;
47758ba9b25SMark Brown 
47858ba9b25SMark Brown 	if (!bits)
47958ba9b25SMark Brown 		return;
48058ba9b25SMark Brown 
4810e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
48258ba9b25SMark Brown 	if (ret != 0)
4830e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
4840e2a3751SLars-Peter Clausen 				 bits, ret);
48558ba9b25SMark Brown }
48658ba9b25SMark Brown 
487c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
488bd477c31SLars-Peter Clausen {
4890ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
49019bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4912e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
49257be9206SKuninori Morimoto 	int stream = substream->stream;
4932e5894d7SBenoit Cousson 	int i;
49419bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
495c8dd1fecSBenoit Cousson 
496a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
4972bc3e1f2SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
49857be9206SKuninori Morimoto 
49957be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
5002e5894d7SBenoit Cousson 			bits = 0;
5012e5894d7SBenoit Cousson 			break;
5022e5894d7SBenoit Cousson 		}
50357be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
5042e5894d7SBenoit Cousson 	}
50557be9206SKuninori Morimoto 
506a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5072bc3e1f2SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
50819bdcc7aSShreyas NC 
50919bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
51019bdcc7aSShreyas NC 			cpu_bits = 0;
51119bdcc7aSShreyas NC 			break;
51219bdcc7aSShreyas NC 		}
51319bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
51419bdcc7aSShreyas NC 	}
515c8dd1fecSBenoit Cousson 
5162e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5172e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
518c8dd1fecSBenoit Cousson }
519c8dd1fecSBenoit Cousson 
520f6c04af5SKuninori Morimoto static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
521f6c04af5SKuninori Morimoto {
522f6c04af5SKuninori Morimoto 	hw->rates		= UINT_MAX;
523f6c04af5SKuninori Morimoto 	hw->rate_min		= 0;
524f6c04af5SKuninori Morimoto 	hw->rate_max		= UINT_MAX;
5256cb56a45SKuninori Morimoto 	hw->channels_min	= 0;
5266cb56a45SKuninori Morimoto 	hw->channels_max	= UINT_MAX;
527debc71f2SKuninori Morimoto 	hw->formats		= ULLONG_MAX;
528f6c04af5SKuninori Morimoto }
529f6c04af5SKuninori Morimoto 
530f6c04af5SKuninori Morimoto static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
531f6c04af5SKuninori Morimoto 				   struct snd_soc_pcm_stream *p)
532f6c04af5SKuninori Morimoto {
533f6c04af5SKuninori Morimoto 	hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
534f6c04af5SKuninori Morimoto 
535f6c04af5SKuninori Morimoto 	/* setup hw->rate_min/max via hw->rates first */
536f6c04af5SKuninori Morimoto 	snd_pcm_hw_limit_rates(hw);
537f6c04af5SKuninori Morimoto 
538f6c04af5SKuninori Morimoto 	/* update hw->rate_min/max by snd_soc_pcm_stream */
539f6c04af5SKuninori Morimoto 	hw->rate_min = max(hw->rate_min, p->rate_min);
540f6c04af5SKuninori Morimoto 	hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
541f6c04af5SKuninori Morimoto }
542f6c04af5SKuninori Morimoto 
5436cb56a45SKuninori Morimoto static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
5446cb56a45SKuninori Morimoto 				   struct snd_soc_pcm_stream *p)
5456cb56a45SKuninori Morimoto {
5466cb56a45SKuninori Morimoto 	hw->channels_min = max(hw->channels_min, p->channels_min);
5476cb56a45SKuninori Morimoto 	hw->channels_max = min(hw->channels_max, p->channels_max);
5486cb56a45SKuninori Morimoto }
5496cb56a45SKuninori Morimoto 
550debc71f2SKuninori Morimoto static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
551debc71f2SKuninori Morimoto 				     struct snd_soc_pcm_stream *p)
552debc71f2SKuninori Morimoto {
553debc71f2SKuninori Morimoto 	hw->formats &= p->formats;
554debc71f2SKuninori Morimoto }
555debc71f2SKuninori Morimoto 
5565854a464SSamuel Holland /**
5575854a464SSamuel Holland  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
5585854a464SSamuel Holland  * @rtd: ASoC PCM runtime
5595854a464SSamuel Holland  * @hw: PCM hardware parameters (output)
5605854a464SSamuel Holland  * @stream: Direction of the PCM stream
5615854a464SSamuel Holland  *
5625854a464SSamuel Holland  * Calculates the subset of stream parameters supported by all DAIs
5635854a464SSamuel Holland  * associated with the PCM stream.
5645854a464SSamuel Holland  */
5655854a464SSamuel Holland int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
5665854a464SSamuel Holland 			    struct snd_pcm_hardware *hw, int stream)
567bd477c31SLars-Peter Clausen {
5680b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
56919bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5702e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5712e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
57219bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
5732e5894d7SBenoit Cousson 	int i;
57478e45c99SLars-Peter Clausen 
575f6c04af5SKuninori Morimoto 	soc_pcm_hw_init(hw);
576f6c04af5SKuninori Morimoto 
57719bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
578a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
5790e9cf4c4SBard Liao 
5800e9cf4c4SBard Liao 		/*
5810e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream type.
5820e9cf4c4SBard Liao 		 * Otherwise, since the rate, channel, and format values will
5830e9cf4c4SBard Liao 		 * zero in that case, we would have no usable settings left,
5840e9cf4c4SBard Liao 		 * causing the resulting setup to fail.
5850e9cf4c4SBard Liao 		 */
5865854a464SSamuel Holland 		if (!snd_soc_dai_stream_valid(cpu_dai, stream))
5870e9cf4c4SBard Liao 			continue;
5880e9cf4c4SBard Liao 
58919bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
59078e45c99SLars-Peter Clausen 
5916cb56a45SKuninori Morimoto 		soc_pcm_hw_update_chan(hw, cpu_stream);
592f6c04af5SKuninori Morimoto 		soc_pcm_hw_update_rate(hw, cpu_stream);
593debc71f2SKuninori Morimoto 		soc_pcm_hw_update_format(hw, cpu_stream);
59419bdcc7aSShreyas NC 	}
5956cb56a45SKuninori Morimoto 	cpu_chan_min = hw->channels_min;
5966cb56a45SKuninori Morimoto 	cpu_chan_max = hw->channels_max;
59719bdcc7aSShreyas NC 
59819bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
599a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
600cde79035SRicard Wanderlof 
601cde79035SRicard Wanderlof 		/*
602cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
603cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
604cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
605cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
606cde79035SRicard Wanderlof 		 */
60725c2f515SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
608cde79035SRicard Wanderlof 			continue;
609cde79035SRicard Wanderlof 
610acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
611acf253c1SKuninori Morimoto 
6126cb56a45SKuninori Morimoto 		soc_pcm_hw_update_chan(hw, codec_stream);
613f6c04af5SKuninori Morimoto 		soc_pcm_hw_update_rate(hw, codec_stream);
614debc71f2SKuninori Morimoto 		soc_pcm_hw_update_format(hw, codec_stream);
6152e5894d7SBenoit Cousson 	}
6162e5894d7SBenoit Cousson 
6175854a464SSamuel Holland 	/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
6186cb56a45SKuninori Morimoto 	if (!hw->channels_min)
6195854a464SSamuel Holland 		return -EINVAL;
6205854a464SSamuel Holland 
6212e5894d7SBenoit Cousson 	/*
6222e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
62319bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6242e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6252e5894d7SBenoit Cousson 	 */
6263989ade2SKuninori Morimoto 	if (rtd->dai_link->num_codecs > 1) {
6276cb56a45SKuninori Morimoto 		hw->channels_min = cpu_chan_min;
6286cb56a45SKuninori Morimoto 		hw->channels_max = cpu_chan_max;
6292e5894d7SBenoit Cousson 	}
6302e5894d7SBenoit Cousson 
6315854a464SSamuel Holland 	return 0;
6325854a464SSamuel Holland }
6335854a464SSamuel Holland EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
6345854a464SSamuel Holland 
6355854a464SSamuel Holland static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
6365854a464SSamuel Holland {
6375854a464SSamuel Holland 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
6380ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
6395854a464SSamuel Holland 	u64 formats = hw->formats;
6405854a464SSamuel Holland 
6415854a464SSamuel Holland 	/*
6425854a464SSamuel Holland 	 * At least one CPU and one CODEC should match. Otherwise, we should
6435854a464SSamuel Holland 	 * have bailed out on a higher level, since there would be no CPU or
6445854a464SSamuel Holland 	 * CODEC to support the transfer direction in that case.
6455854a464SSamuel Holland 	 */
6465854a464SSamuel Holland 	snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
6475854a464SSamuel Holland 
6485854a464SSamuel Holland 	if (formats)
6495854a464SSamuel Holland 		hw->formats &= formats;
650bd477c31SLars-Peter Clausen }
651bd477c31SLars-Peter Clausen 
652dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
653e7ecfdb7SKuninori Morimoto {
6540ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
655e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
656613fb500SKuninori Morimoto 	int i, ret = 0;
657e7ecfdb7SKuninori Morimoto 
658613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
65951aff91aSKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component, substream);
660bcae1631SKuninori Morimoto 		if (ret < 0)
661d2aaa8d8SKai Vehmanen 			break;
662e7ecfdb7SKuninori Morimoto 
663ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
664bcae1631SKuninori Morimoto 		if (ret < 0)
665d2aaa8d8SKai Vehmanen 			break;
666e7ecfdb7SKuninori Morimoto 	}
667dd03907bSKuninori Morimoto 
668d2aaa8d8SKai Vehmanen 	return ret;
669e7ecfdb7SKuninori Morimoto }
670e7ecfdb7SKuninori Morimoto 
67151aff91aSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream,
67251aff91aSKuninori Morimoto 				    int rollback)
673244e2936SCharles Keepax {
6740ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
675244e2936SCharles Keepax 	struct snd_soc_component *component;
67633be10b5SKuninori Morimoto 	int i, ret = 0;
677244e2936SCharles Keepax 
678613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
67933be10b5SKuninori Morimoto 		int r = snd_soc_component_close(component, substream, rollback);
680e82ebffcSKuninori Morimoto 		if (r < 0)
681e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
682e82ebffcSKuninori Morimoto 
68351aff91aSKuninori Morimoto 		snd_soc_component_module_put_when_close(component, substream, rollback);
684244e2936SCharles Keepax 	}
685244e2936SCharles Keepax 
6863672beb8SKuninori Morimoto 	return ret;
687244e2936SCharles Keepax }
688244e2936SCharles Keepax 
689b7898396STakashi Iwai static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
690b7898396STakashi Iwai 			 struct snd_pcm_substream *substream, int rollback)
69162c86d1dSKuninori Morimoto {
69262c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
693c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
69462c86d1dSKuninori Morimoto 	int i;
69562c86d1dSKuninori Morimoto 
696b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
69762c86d1dSKuninori Morimoto 
6981da681e5SChancel Liu 	if (!rollback) {
69962c86d1dSKuninori Morimoto 		snd_soc_runtime_deactivate(rtd, substream->stream);
7001da681e5SChancel Liu 		/* clear the corresponding DAIs parameters when going to be inactive */
7011da681e5SChancel Liu 		for_each_rtd_dais(rtd, i, dai) {
7021da681e5SChancel Liu 			if (snd_soc_dai_active(dai) == 0)
7031da681e5SChancel Liu 				soc_pcm_set_dai_params(dai, NULL);
7041da681e5SChancel Liu 
7051da681e5SChancel Liu 			if (snd_soc_dai_stream_active(dai, substream->stream) == 0)
7061da681e5SChancel Liu 				snd_soc_dai_digital_mute(dai, 1, substream->stream);
7071da681e5SChancel Liu 		}
7081da681e5SChancel Liu 	}
70962c86d1dSKuninori Morimoto 
710c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
711140a4532SKuninori Morimoto 		snd_soc_dai_shutdown(dai, substream, rollback);
71262c86d1dSKuninori Morimoto 
713140a4532SKuninori Morimoto 	snd_soc_link_shutdown(substream, rollback);
71462c86d1dSKuninori Morimoto 
715140a4532SKuninori Morimoto 	soc_pcm_components_close(substream, rollback);
71662c86d1dSKuninori Morimoto 
717140a4532SKuninori Morimoto 	snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
71862c86d1dSKuninori Morimoto 
71962c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
720b3dea624SKuninori Morimoto 		if (!snd_soc_component_active(component))
72162c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
72262c86d1dSKuninori Morimoto 
72362c86d1dSKuninori Morimoto 	return 0;
72462c86d1dSKuninori Morimoto }
72562c86d1dSKuninori Morimoto 
72662c86d1dSKuninori Morimoto /*
727140a4532SKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
728140a4532SKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
729140a4532SKuninori Morimoto  * shutdown.
730140a4532SKuninori Morimoto  */
731b7898396STakashi Iwai static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
732b7898396STakashi Iwai 			   struct snd_pcm_substream *substream)
733b7898396STakashi Iwai {
734b7898396STakashi Iwai 	return soc_pcm_clean(rtd, substream, 0);
735b7898396STakashi Iwai }
736b7898396STakashi Iwai 
737b7898396STakashi Iwai /* PCM close ops for non-DPCM streams */
738140a4532SKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
739140a4532SKuninori Morimoto {
740b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
741b7898396STakashi Iwai 
742b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
7436bbabd28SKuninori Morimoto 	__soc_pcm_close(rtd, substream);
744b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
745b7898396STakashi Iwai 	return 0;
746140a4532SKuninori Morimoto }
747140a4532SKuninori Morimoto 
748c393281aSKuninori Morimoto static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
749c393281aSKuninori Morimoto {
750c393281aSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
751c393281aSKuninori Morimoto 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
752c393281aSKuninori Morimoto 	const char *name_cpu = soc_cpu_dai_name(rtd);
753c393281aSKuninori Morimoto 	const char *name_codec = soc_codec_dai_name(rtd);
754c393281aSKuninori Morimoto 	const char *err_msg;
755c393281aSKuninori Morimoto 	struct device *dev = rtd->dev;
756c393281aSKuninori Morimoto 
757c393281aSKuninori Morimoto 	err_msg = "rates";
758c393281aSKuninori Morimoto 	if (!hw->rates)
759c393281aSKuninori Morimoto 		goto config_err;
760c393281aSKuninori Morimoto 
761c393281aSKuninori Morimoto 	err_msg = "formats";
762c393281aSKuninori Morimoto 	if (!hw->formats)
763c393281aSKuninori Morimoto 		goto config_err;
764c393281aSKuninori Morimoto 
765c393281aSKuninori Morimoto 	err_msg = "channels";
766c393281aSKuninori Morimoto 	if (!hw->channels_min || !hw->channels_max ||
767c393281aSKuninori Morimoto 	     hw->channels_min  >  hw->channels_max)
768c393281aSKuninori Morimoto 		goto config_err;
769c393281aSKuninori Morimoto 
770c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: %s <-> %s info:\n",		name_codec,
771c393281aSKuninori Morimoto 							name_cpu);
772c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: rate mask 0x%x\n",		hw->rates);
773c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: ch   min %d max %d\n",	hw->channels_min,
774c393281aSKuninori Morimoto 							hw->channels_max);
775c393281aSKuninori Morimoto 	dev_dbg(dev, "ASoC: rate min %d max %d\n",	hw->rate_min,
776c393281aSKuninori Morimoto 							hw->rate_max);
777c393281aSKuninori Morimoto 
778c393281aSKuninori Morimoto 	return 0;
779c393281aSKuninori Morimoto 
780c393281aSKuninori Morimoto config_err:
781c393281aSKuninori Morimoto 	dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
782c393281aSKuninori Morimoto 		name_codec, name_cpu, err_msg);
783c393281aSKuninori Morimoto 	return -EINVAL;
784c393281aSKuninori Morimoto }
785c393281aSKuninori Morimoto 
786140a4532SKuninori Morimoto /*
787ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
788ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
789ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
790ddee627cSLiam Girdwood  */
791b7898396STakashi Iwai static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
792b7898396STakashi Iwai 			  struct snd_pcm_substream *substream)
793ddee627cSLiam Girdwood {
79490be711eSKuninori Morimoto 	struct snd_soc_component *component;
795c840f769SKuninori Morimoto 	struct snd_soc_dai *dai;
796244e2936SCharles Keepax 	int i, ret = 0;
797ddee627cSLiam Girdwood 
798b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
799b7898396STakashi Iwai 
80076c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
80176c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
80290be711eSKuninori Morimoto 
803939a5cfbSKuninori Morimoto 	ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
804939a5cfbSKuninori Morimoto 	if (ret < 0)
805b7898396STakashi Iwai 		goto err;
806ddee627cSLiam Girdwood 
8075d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
8085d9fa03eSKuninori Morimoto 	if (ret < 0)
809140a4532SKuninori Morimoto 		goto err;
8105d9fa03eSKuninori Morimoto 
8117cf3c5b4SKuninori Morimoto 	ret = snd_soc_link_startup(substream);
812a5e6c109SKuninori Morimoto 	if (ret < 0)
813140a4532SKuninori Morimoto 		goto err;
8145d9fa03eSKuninori Morimoto 
815ddee627cSLiam Girdwood 	/* startup the audio subsystem */
816c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
817c840f769SKuninori Morimoto 		ret = snd_soc_dai_startup(dai, substream);
818ce820145SKuninori Morimoto 		if (ret < 0)
819140a4532SKuninori Morimoto 			goto err;
8202e5894d7SBenoit Cousson 	}
8212e5894d7SBenoit Cousson 
82201d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
82301d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
82401d7584cSLiam Girdwood 		goto dynamic;
82501d7584cSLiam Girdwood 
826ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8272e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8282e5894d7SBenoit Cousson 
82968cbc557SKuninori Morimoto 	soc_pcm_update_symmetry(substream);
83062e5f676SLars-Peter Clausen 
831c393281aSKuninori Morimoto 	ret = soc_hw_sanity_check(substream);
832c393281aSKuninori Morimoto 	if (ret < 0)
833140a4532SKuninori Morimoto 		goto err;
834ddee627cSLiam Girdwood 
835c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
83658ba9b25SMark Brown 
837ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
838c840f769SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai) {
839c840f769SKuninori Morimoto 		ret = soc_pcm_apply_symmetry(substream, dai);
840ddee627cSLiam Girdwood 		if (ret != 0)
841140a4532SKuninori Morimoto 			goto err;
842ddee627cSLiam Girdwood 	}
84301d7584cSLiam Girdwood dynamic:
84424894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
8458e7875aeSKuninori Morimoto 	ret = 0;
846140a4532SKuninori Morimoto err:
84704110728SKuninori Morimoto 	if (ret < 0)
848b7898396STakashi Iwai 		soc_pcm_clean(rtd, substream, 1);
849d6652ef8SMark Brown 
85004110728SKuninori Morimoto 	return soc_pcm_ret(rtd, ret);
851ddee627cSLiam Girdwood }
852ddee627cSLiam Girdwood 
853b7898396STakashi Iwai /* PCM open ops for non-DPCM streams */
854b7898396STakashi Iwai static int soc_pcm_open(struct snd_pcm_substream *substream)
855b7898396STakashi Iwai {
856b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
857b7898396STakashi Iwai 	int ret;
858b7898396STakashi Iwai 
859b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
860b7898396STakashi Iwai 	ret = __soc_pcm_open(rtd, substream);
861b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
862b7898396STakashi Iwai 	return ret;
863b7898396STakashi Iwai }
864b7898396STakashi Iwai 
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:
90404110728SKuninori Morimoto 	return soc_pcm_ret(rtd, ret);
905ddee627cSLiam Girdwood }
906ddee627cSLiam Girdwood 
907b7898396STakashi Iwai /* PCM prepare ops for non-DPCM streams */
908b7898396STakashi Iwai static int soc_pcm_prepare(struct snd_pcm_substream *substream)
909b7898396STakashi Iwai {
910b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
911b7898396STakashi Iwai 	int ret;
912b7898396STakashi Iwai 
913b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
914b7898396STakashi Iwai 	ret = __soc_pcm_prepare(rtd, substream);
915b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
916b7898396STakashi Iwai 	return ret;
917b7898396STakashi Iwai }
918b7898396STakashi Iwai 
9192e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
9202e5894d7SBenoit Cousson 				       unsigned int mask)
9212e5894d7SBenoit Cousson {
9222e5894d7SBenoit Cousson 	struct snd_interval *interval;
9232e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
9242e5894d7SBenoit Cousson 
9252e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
9262e5894d7SBenoit Cousson 	interval->min = channels;
9272e5894d7SBenoit Cousson 	interval->max = channels;
9282e5894d7SBenoit Cousson }
9292e5894d7SBenoit Cousson 
930b7898396STakashi Iwai static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
931b7898396STakashi Iwai 			    struct snd_pcm_substream *substream, int rollback)
932ab49436eSKuninori Morimoto {
933ab49436eSKuninori Morimoto 	struct snd_soc_dai *dai;
934ab49436eSKuninori Morimoto 	int i;
935ab49436eSKuninori Morimoto 
936b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
937ab49436eSKuninori Morimoto 
938a27b421fSRanjani Sridharan 	/* run the stream event */
939a27b421fSRanjani Sridharan 	snd_soc_dapm_stream_stop(rtd, substream->stream);
940a27b421fSRanjani Sridharan 
941ab49436eSKuninori Morimoto 	/* free any machine hw params */
9424662c596SKuninori Morimoto 	snd_soc_link_hw_free(substream, rollback);
943ab49436eSKuninori Morimoto 
944ab49436eSKuninori Morimoto 	/* free any component resources */
9454662c596SKuninori Morimoto 	snd_soc_pcm_component_hw_free(substream, rollback);
946ab49436eSKuninori Morimoto 
947ab49436eSKuninori Morimoto 	/* now free hw params for the DAIs  */
948121966d0SKuninori Morimoto 	for_each_rtd_dais(rtd, i, dai)
949121966d0SKuninori Morimoto 		if (snd_soc_dai_stream_valid(dai, substream->stream))
9504662c596SKuninori Morimoto 			snd_soc_dai_hw_free(dai, substream, rollback);
951ab49436eSKuninori Morimoto 
952ab49436eSKuninori Morimoto 	return 0;
953ab49436eSKuninori Morimoto }
954ab49436eSKuninori Morimoto 
955ab49436eSKuninori Morimoto /*
9564662c596SKuninori Morimoto  * Frees resources allocated by hw_params, can be called multiple times
9574662c596SKuninori Morimoto  */
958b7898396STakashi Iwai static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
959b7898396STakashi Iwai 			     struct snd_pcm_substream *substream)
960b7898396STakashi Iwai {
961b7898396STakashi Iwai 	return soc_pcm_hw_clean(rtd, substream, 0);
962b7898396STakashi Iwai }
963b7898396STakashi Iwai 
964b7898396STakashi Iwai /* hw_free PCM ops for non-DPCM streams */
9654662c596SKuninori Morimoto static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
9664662c596SKuninori Morimoto {
967b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
968b7898396STakashi Iwai 	int ret;
969b7898396STakashi Iwai 
970b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
971b7898396STakashi Iwai 	ret = __soc_pcm_hw_free(rtd, substream);
972b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
973b7898396STakashi Iwai 	return ret;
9744662c596SKuninori Morimoto }
9754662c596SKuninori Morimoto 
9764662c596SKuninori Morimoto /*
977ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
978ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
979ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
980ddee627cSLiam Girdwood  */
981b7898396STakashi Iwai static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
982b7898396STakashi Iwai 			       struct snd_pcm_substream *substream,
983ddee627cSLiam Girdwood 			       struct snd_pcm_hw_params *params)
984ddee627cSLiam Girdwood {
98519bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
9860b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
987244e2936SCharles Keepax 	int i, ret = 0;
988ddee627cSLiam Girdwood 
989b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(rtd);
9905cca5951SShengjiu Wang 
9915cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
9925cca5951SShengjiu Wang 	if (ret)
9935cca5951SShengjiu Wang 		goto out;
9945cca5951SShengjiu Wang 
9957cf3c5b4SKuninori Morimoto 	ret = snd_soc_link_hw_params(substream, params);
996a5e6c109SKuninori Morimoto 	if (ret < 0)
997ddee627cSLiam Girdwood 		goto out;
998ddee627cSLiam Girdwood 
999a4be4187SKuninori Morimoto 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
10002e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
1001e15ff262SKuninori Morimoto 		unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
10022e5894d7SBenoit Cousson 
1003cde79035SRicard Wanderlof 		/*
1004cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1005cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1006cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1007cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1008cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1009cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1010cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1011cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1012cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1013cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1014cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1015cde79035SRicard Wanderlof 		 * we can do the right thing here.
1016cde79035SRicard Wanderlof 		 */
1017cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1018cde79035SRicard Wanderlof 			continue;
1019cde79035SRicard Wanderlof 
10202e5894d7SBenoit Cousson 		/* copy params for each codec */
10212e5894d7SBenoit Cousson 		codec_params = *params;
10222e5894d7SBenoit Cousson 
10232e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1024e15ff262SKuninori Morimoto 		if (tdm_mask)
1025e15ff262SKuninori Morimoto 			soc_pcm_codec_params_fixup(&codec_params, tdm_mask);
10262e5894d7SBenoit Cousson 
1027aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1028aa6166c2SKuninori Morimoto 					    &codec_params);
102993e6958aSBenoit Cousson 		if(ret < 0)
10304662c596SKuninori Morimoto 			goto out;
1031ddee627cSLiam Girdwood 
10322805b8bdSKuninori Morimoto 		soc_pcm_set_dai_params(codec_dai, &codec_params);
1033078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1034ddee627cSLiam Girdwood 	}
1035ddee627cSLiam Girdwood 
1036a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
10370e9cf4c4SBard Liao 		/*
10380e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
10390e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
10400e9cf4c4SBard Liao 		 */
10410e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
10420e9cf4c4SBard Liao 			continue;
10430e9cf4c4SBard Liao 
1044aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
104593e6958aSBenoit Cousson 		if (ret < 0)
10464662c596SKuninori Morimoto 			goto out;
1047ddee627cSLiam Girdwood 
104819bdcc7aSShreyas NC 		/* store the parameters for each DAI */
10492805b8bdSKuninori Morimoto 		soc_pcm_set_dai_params(cpu_dai, params);
1050ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
105119bdcc7aSShreyas NC 	}
1052ca58221dSKuninori Morimoto 
10533a36a64aSKuninori Morimoto 	ret = snd_soc_pcm_component_hw_params(substream, params);
1054ddee627cSLiam Girdwood out:
105504110728SKuninori Morimoto 	if (ret < 0)
1056b7898396STakashi Iwai 		soc_pcm_hw_clean(rtd, substream, 1);
1057b8135864SKuninori Morimoto 
105804110728SKuninori Morimoto 	return soc_pcm_ret(rtd, ret);
1059ddee627cSLiam Girdwood }
1060ddee627cSLiam Girdwood 
1061b7898396STakashi Iwai /* hw_params PCM ops for non-DPCM streams */
1062b7898396STakashi Iwai static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1063b7898396STakashi Iwai 			     struct snd_pcm_hw_params *params)
1064b7898396STakashi Iwai {
1065b7898396STakashi Iwai 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1066b7898396STakashi Iwai 	int ret;
1067b7898396STakashi Iwai 
1068b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(rtd);
1069b7898396STakashi Iwai 	ret = __soc_pcm_hw_params(rtd, substream, params);
1070b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(rtd);
1071b7898396STakashi Iwai 	return ret;
1072b7898396STakashi Iwai }
1073b7898396STakashi Iwai 
10744378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
10754378f1fbSPeter Ujfalusi {
107659dd33f8SVijendar Mukunda 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
107754fc4b72SClaudiu Beznea 	struct snd_soc_component *component;
107854fc4b72SClaudiu Beznea 	int ret = -EINVAL, _ret = 0, start_dma_last = 0, i;
10796374f493SKuninori Morimoto 	int rollback = 0;
10804378f1fbSPeter Ujfalusi 
10814378f1fbSPeter Ujfalusi 	switch (cmd) {
10824378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
10834378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
10844378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
108554fc4b72SClaudiu Beznea 		/* Do we need to start dma last? */
108654fc4b72SClaudiu Beznea 		for_each_rtd_components(rtd, i, component) {
108754fc4b72SClaudiu Beznea 			if (component->driver->start_dma_last) {
108854fc4b72SClaudiu Beznea 				start_dma_last = 1;
108954fc4b72SClaudiu Beznea 				break;
109054fc4b72SClaudiu Beznea 			}
109154fc4b72SClaudiu Beznea 		}
109254fc4b72SClaudiu Beznea 
10936374f493SKuninori Morimoto 		ret = snd_soc_link_trigger(substream, cmd, 0);
1094836367beSKuninori Morimoto 		if (ret < 0)
10956374f493SKuninori Morimoto 			goto start_err;
1096836367beSKuninori Morimoto 
109754fc4b72SClaudiu Beznea 		if (start_dma_last) {
109854fc4b72SClaudiu Beznea 			ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
109954fc4b72SClaudiu Beznea 			if (ret < 0)
110054fc4b72SClaudiu Beznea 				goto start_err;
110154fc4b72SClaudiu Beznea 
110254fc4b72SClaudiu Beznea 			ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
110354fc4b72SClaudiu Beznea 		} else {
11046374f493SKuninori Morimoto 			ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1105836367beSKuninori Morimoto 			if (ret < 0)
11066374f493SKuninori Morimoto 				goto start_err;
1107836367beSKuninori Morimoto 
11086374f493SKuninori Morimoto 			ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
110954fc4b72SClaudiu Beznea 		}
11106374f493SKuninori Morimoto start_err:
11116374f493SKuninori Morimoto 		if (ret < 0)
11126374f493SKuninori Morimoto 			rollback = 1;
11136374f493SKuninori Morimoto 	}
11146374f493SKuninori Morimoto 
11156374f493SKuninori Morimoto 	if (rollback) {
11166374f493SKuninori Morimoto 		_ret = ret;
11176374f493SKuninori Morimoto 		switch (cmd) {
11186374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_START:
11196374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_STOP;
11204378f1fbSPeter Ujfalusi 			break;
11216374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_RESUME:
11226374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_SUSPEND;
11236374f493SKuninori Morimoto 			break;
11246374f493SKuninori Morimoto 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11256374f493SKuninori Morimoto 			cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
11266374f493SKuninori Morimoto 			break;
11276374f493SKuninori Morimoto 		}
11286374f493SKuninori Morimoto 	}
11296374f493SKuninori Morimoto 
11306374f493SKuninori Morimoto 	switch (cmd) {
11314378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
11324378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
11334378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
113459dd33f8SVijendar Mukunda 		if (rtd->dai_link->stop_dma_first) {
113559dd33f8SVijendar Mukunda 			ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
113659dd33f8SVijendar Mukunda 			if (ret < 0)
113759dd33f8SVijendar Mukunda 				break;
113859dd33f8SVijendar Mukunda 
113959dd33f8SVijendar Mukunda 			ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
114059dd33f8SVijendar Mukunda 			if (ret < 0)
114159dd33f8SVijendar Mukunda 				break;
114259dd33f8SVijendar Mukunda 		} else {
11436374f493SKuninori Morimoto 			ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1144836367beSKuninori Morimoto 			if (ret < 0)
11454378f1fbSPeter Ujfalusi 				break;
1146836367beSKuninori Morimoto 
11476374f493SKuninori Morimoto 			ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1148836367beSKuninori Morimoto 			if (ret < 0)
1149836367beSKuninori Morimoto 				break;
115059dd33f8SVijendar Mukunda 		}
11516374f493SKuninori Morimoto 		ret = snd_soc_link_trigger(substream, cmd, rollback);
1152836367beSKuninori Morimoto 		break;
11534378f1fbSPeter Ujfalusi 	}
11544378f1fbSPeter Ujfalusi 
11556374f493SKuninori Morimoto 	if (_ret)
11566374f493SKuninori Morimoto 		ret = _ret;
11576374f493SKuninori Morimoto 
11584378f1fbSPeter Ujfalusi 	return ret;
11594378f1fbSPeter Ujfalusi }
11604378f1fbSPeter Ujfalusi 
1161ddee627cSLiam Girdwood /*
1162ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1163ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1164dd894f4cSKuninori Morimoto  * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1165ddee627cSLiam Girdwood  */
1166ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1167ddee627cSLiam Girdwood {
1168ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1169ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
11702e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
117119bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
1172ddee627cSLiam Girdwood 
11730035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1174b8135864SKuninori Morimoto 
1175403f830eSKuninori Morimoto 	/* should be called *after* snd_soc_pcm_component_pointer() */
11768544f08cSKuninori Morimoto 	snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1177403f830eSKuninori Morimoto 	snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1178ddee627cSLiam Girdwood 
1179dd894f4cSKuninori Morimoto 	runtime->delay = cpu_delay + codec_delay;
1180ddee627cSLiam Girdwood 
1181ddee627cSLiam Girdwood 	return offset;
1182ddee627cSLiam Girdwood }
1183ddee627cSLiam Girdwood 
118401d7584cSLiam Girdwood /* connect a FE and BE */
118501d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
118601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
118701d7584cSLiam Girdwood {
1188bbf7d3b1SPierre-Louis Bossart 	struct snd_pcm_substream *fe_substream;
1189bbf7d3b1SPierre-Louis Bossart 	struct snd_pcm_substream *be_substream;
119001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1191b7898396STakashi Iwai 
1192b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
119301d7584cSLiam Girdwood 
119401d7584cSLiam Girdwood 	/* only add new dpcms */
11958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
119601d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
119701d7584cSLiam Girdwood 			return 0;
119801d7584cSLiam Girdwood 	}
119901d7584cSLiam Girdwood 
1200bbf7d3b1SPierre-Louis Bossart 	fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1201bbf7d3b1SPierre-Louis Bossart 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1202bbf7d3b1SPierre-Louis Bossart 
1203bbf7d3b1SPierre-Louis Bossart 	if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1204bbf7d3b1SPierre-Louis Bossart 		dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1205bbf7d3b1SPierre-Louis Bossart 			__func__);
1206bbf7d3b1SPierre-Louis Bossart 		return -EINVAL;
1207bbf7d3b1SPierre-Louis Bossart 	}
1208bbf7d3b1SPierre-Louis Bossart 	if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
12094ccf0949SPierre-Louis Bossart 		dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1210bbf7d3b1SPierre-Louis Bossart 		be_substream->pcm->nonatomic = 1;
1211bbf7d3b1SPierre-Louis Bossart 	}
1212bbf7d3b1SPierre-Louis Bossart 
1213fb6d679fSChristophe JAILLET 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
121401d7584cSLiam Girdwood 	if (!dpcm)
121501d7584cSLiam Girdwood 		return -ENOMEM;
121601d7584cSLiam Girdwood 
121701d7584cSLiam Girdwood 	dpcm->be = be;
121801d7584cSLiam Girdwood 	dpcm->fe = fe;
121901d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1220b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
122101d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
122201d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1223b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
122401d7584cSLiam Girdwood 
122501d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
122601d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
122701d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
122801d7584cSLiam Girdwood 
1229154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1230154dae87SKuninori Morimoto 
123101d7584cSLiam Girdwood 	return 1;
123201d7584cSLiam Girdwood }
123301d7584cSLiam Girdwood 
123401d7584cSLiam Girdwood /* reparent a BE onto another FE */
123501d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
123601d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
123701d7584cSLiam Girdwood {
123801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
123901d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
124001d7584cSLiam Girdwood 
124101d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
124201d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
124301d7584cSLiam Girdwood 		return;
124401d7584cSLiam Girdwood 
124501d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1246db8f91d4SSrinivasa Rao Mandadapu 	if (!be_substream)
1247db8f91d4SSrinivasa Rao Mandadapu 		return;
124801d7584cSLiam Girdwood 
1249d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
125001d7584cSLiam Girdwood 		if (dpcm->fe == fe)
125101d7584cSLiam Girdwood 			continue;
125201d7584cSLiam Girdwood 
125301d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
125401d7584cSLiam Girdwood 			stream ? "capture" : "playback",
125501d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
125601d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
125701d7584cSLiam Girdwood 
125801d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
125901d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
126001d7584cSLiam Girdwood 		break;
126101d7584cSLiam Girdwood 	}
126201d7584cSLiam Girdwood }
126301d7584cSLiam Girdwood 
126401d7584cSLiam Girdwood /* disconnect a BE and FE */
126523607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
126601d7584cSLiam Girdwood {
126701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
12689f620684STakashi Iwai 	LIST_HEAD(deleted_dpcms);
126901d7584cSLiam Girdwood 
1270b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
1271b7898396STakashi Iwai 
1272b7898396STakashi Iwai 	snd_soc_dpcm_stream_lock_irq(fe, stream);
12738d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1274103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
127501d7584cSLiam Girdwood 				stream ? "capture" : "playback",
127601d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
127701d7584cSLiam Girdwood 
127801d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
127901d7584cSLiam Girdwood 			continue;
128001d7584cSLiam Girdwood 
128101d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
128201d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
128301d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
128401d7584cSLiam Girdwood 
128501d7584cSLiam Girdwood 		/* BEs still alive need new FE */
128601d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
128701d7584cSLiam Girdwood 
128801d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
12899f620684STakashi Iwai 		list_move(&dpcm->list_fe, &deleted_dpcms);
129001d7584cSLiam Girdwood 	}
1291b7898396STakashi Iwai 	snd_soc_dpcm_stream_unlock_irq(fe, stream);
12929f620684STakashi Iwai 
12939f620684STakashi Iwai 	while (!list_empty(&deleted_dpcms)) {
12949f620684STakashi Iwai 		dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
12959f620684STakashi Iwai 					list_fe);
12969f620684STakashi Iwai 		list_del(&dpcm->list_fe);
12979f620684STakashi Iwai 		dpcm_remove_debugfs_state(dpcm);
12989f620684STakashi Iwai 		kfree(dpcm);
12999f620684STakashi Iwai 	}
130001d7584cSLiam Girdwood }
130101d7584cSLiam Girdwood 
130201d7584cSLiam Girdwood /* get BE for DAI widget and stream */
130301d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
130401d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
130501d7584cSLiam Girdwood {
130601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
130793597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
13087afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
13091a497983SMengdong Lin 	int i;
131001d7584cSLiam Girdwood 
13113c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
13123c146465SLiam Girdwood 
1313bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
131401d7584cSLiam Girdwood 
131535ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
131635ea0655SLiam Girdwood 			continue;
131735ea0655SLiam Girdwood 
131875459065STakashi Iwai 		if (!snd_soc_dpcm_get_substream(be, stream))
131975459065STakashi Iwai 			continue;
132075459065STakashi Iwai 
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 
13365edcf2a3SRanjani Sridharan 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 }
13485edcf2a3SRanjani Sridharan EXPORT_SYMBOL_GPL(widget_in_list);
134901d7584cSLiam Girdwood 
1350d1a7af09SRanjani Sridharan bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
13515fdd022cSPiotr Stankiewicz {
13525fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
13535fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1354c2cd8216SKuninori Morimoto 	int stream;
13555fdd022cSPiotr Stankiewicz 
1356c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1357c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1358c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1359c2cd8216SKuninori Morimoto 	else
1360c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1361c2cd8216SKuninori Morimoto 
1362027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1363027a4838SKuninori Morimoto 	if (rtd)
13645fdd022cSPiotr Stankiewicz 		return true;
13655fdd022cSPiotr Stankiewicz 
13665fdd022cSPiotr Stankiewicz 	return false;
13675fdd022cSPiotr Stankiewicz }
1368d1a7af09SRanjani Sridharan EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
13695fdd022cSPiotr Stankiewicz 
137023607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
13711ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
137201d7584cSLiam Girdwood {
1373c2233a26SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
137401d7584cSLiam Girdwood 	int paths;
137501d7584cSLiam Girdwood 
13763989ade2SKuninori Morimoto 	if (fe->dai_link->num_cpus > 1) {
13776e1276a5SBard Liao 		dev_err(fe->dev,
13786e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
13796e1276a5SBard Liao 		return -EINVAL;
13806e1276a5SBard Liao 	}
13816e1276a5SBard Liao 
138201d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
13836742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1384aa293777SSameer Pujar 			fe->card->component_chaining ?
1385aa293777SSameer Pujar 				NULL : dpcm_end_walk_at_be);
138601d7584cSLiam Girdwood 
1387d479f00bSKuninori Morimoto 	if (paths > 0)
1388103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
138901d7584cSLiam Girdwood 			stream ? "capture" : "playback");
1390d479f00bSKuninori Morimoto 	else if (paths == 0)
1391d479f00bSKuninori Morimoto 		dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1392d479f00bSKuninori Morimoto 			 stream ? "capture" : "playback");
139301d7584cSLiam Girdwood 
139401d7584cSLiam Girdwood 	return paths;
139501d7584cSLiam Girdwood }
139601d7584cSLiam Girdwood 
139752645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
139852645e33SKuninori Morimoto {
139952645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
140052645e33SKuninori Morimoto }
140152645e33SKuninori Morimoto 
14028cce6569SGuennadi Liakhovetski static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
14038cce6569SGuennadi Liakhovetski 			      struct snd_soc_dapm_widget_list *list)
140401d7584cSLiam Girdwood {
14057afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
14062e5894d7SBenoit Cousson 	unsigned int i;
140701d7584cSLiam Girdwood 
1408c840f769SKuninori Morimoto 	/* is there a valid DAI widget for this BE */
1409c840f769SKuninori Morimoto 	for_each_rtd_dais(dpcm->be, i, dai) {
14107931df9bSKuninori Morimoto 		struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
141101d7584cSLiam Girdwood 
141219bdcc7aSShreyas NC 		/*
1413c840f769SKuninori Morimoto 		 * The BE is pruned only if none of the dai
141419bdcc7aSShreyas NC 		 * widgets are in the active list.
141519bdcc7aSShreyas NC 		 */
141601d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
14178cce6569SGuennadi Liakhovetski 			return true;
141819bdcc7aSShreyas NC 	}
141901d7584cSLiam Girdwood 
14208cce6569SGuennadi Liakhovetski 	return false;
14218cce6569SGuennadi Liakhovetski }
14228cce6569SGuennadi Liakhovetski 
14238cce6569SGuennadi Liakhovetski static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
14248cce6569SGuennadi Liakhovetski 			    struct snd_soc_dapm_widget_list **list_)
14258cce6569SGuennadi Liakhovetski {
14268cce6569SGuennadi Liakhovetski 	struct snd_soc_dpcm *dpcm;
14278cce6569SGuennadi Liakhovetski 	int prune = 0;
14288cce6569SGuennadi Liakhovetski 
14298cce6569SGuennadi Liakhovetski 	/* Destroy any old FE <--> BE connections */
14308cce6569SGuennadi Liakhovetski 	for_each_dpcm_be(fe, stream, dpcm) {
14318cce6569SGuennadi Liakhovetski 		if (dpcm_be_is_active(dpcm, stream, *list_))
1432bed646dcSKuninori Morimoto 			continue;
143301d7584cSLiam Girdwood 
1434103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
143501d7584cSLiam Girdwood 			stream ? "capture" : "playback",
143601d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
143701d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1438a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
143901d7584cSLiam Girdwood 		prune++;
144001d7584cSLiam Girdwood 	}
144101d7584cSLiam Girdwood 
1442103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
144301d7584cSLiam Girdwood 	return prune;
144401d7584cSLiam Girdwood }
144501d7584cSLiam Girdwood 
144601d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
144701d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
144801d7584cSLiam Girdwood {
144901d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
145001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
145101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
145209e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
14530d3a5178SKuninori Morimoto 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
145401d7584cSLiam Girdwood 	int i, new = 0, err;
145501d7584cSLiam Girdwood 
14566932b20dSKuninori Morimoto 	/* don't connect if FE is not running */
14570d3a5178SKuninori Morimoto 	if (!fe_substream->runtime && !fe->fe_compr)
14586932b20dSKuninori Morimoto 		return new;
14596932b20dSKuninori Morimoto 
146001d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
146109e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
146201d7584cSLiam Girdwood 
146309e88f8aSKuninori Morimoto 		switch (widget->id) {
14644616274dSMark Brown 		case snd_soc_dapm_dai_in:
1465c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1466c5b8540dSKoro Chen 				continue;
1467c5b8540dSKoro Chen 			break;
14684616274dSMark Brown 		case snd_soc_dapm_dai_out:
1469c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1470c5b8540dSKoro Chen 				continue;
14714616274dSMark Brown 			break;
14724616274dSMark Brown 		default:
147301d7584cSLiam Girdwood 			continue;
14744616274dSMark Brown 		}
147501d7584cSLiam Girdwood 
147601d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
147709e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
147801d7584cSLiam Girdwood 		if (!be) {
1479b6eabd24SShengjiu Wang 			dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
148009e88f8aSKuninori Morimoto 				widget->name);
148101d7584cSLiam Girdwood 			continue;
148201d7584cSLiam Girdwood 		}
148301d7584cSLiam Girdwood 
14849609cfcdSPierre-Louis Bossart 		/*
14859609cfcdSPierre-Louis Bossart 		 * Filter for systems with 'component_chaining' enabled.
14869609cfcdSPierre-Louis Bossart 		 * This helps to avoid unnecessary re-configuration of an
14879609cfcdSPierre-Louis Bossart 		 * already active BE on such systems.
14889609cfcdSPierre-Louis Bossart 		 */
14899609cfcdSPierre-Louis Bossart 		if (fe->card->component_chaining &&
14909609cfcdSPierre-Louis Bossart 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
14910c25db3fSSameer Pujar 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
14920c25db3fSSameer Pujar 			continue;
14930c25db3fSSameer Pujar 
149401d7584cSLiam Girdwood 		/* newly connected FE and BE */
149501d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
149601d7584cSLiam Girdwood 		if (err < 0) {
1497103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
149809e88f8aSKuninori Morimoto 				widget->name);
149901d7584cSLiam Girdwood 			break;
150001d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
150101d7584cSLiam Girdwood 			continue;
150201d7584cSLiam Girdwood 
150301d7584cSLiam Girdwood 		/* new */
1504a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
150501d7584cSLiam Girdwood 		new++;
150601d7584cSLiam Girdwood 	}
150701d7584cSLiam Girdwood 
1508103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
150901d7584cSLiam Girdwood 	return new;
151001d7584cSLiam Girdwood }
151101d7584cSLiam Girdwood 
151201d7584cSLiam Girdwood /*
151301d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
151401d7584cSLiam Girdwood  * FE substream.
151501d7584cSLiam Girdwood  */
151623607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
151701d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
151801d7584cSLiam Girdwood {
151901d7584cSLiam Girdwood 	if (new)
152001d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
152101d7584cSLiam Girdwood 	else
152201d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
152301d7584cSLiam Girdwood }
152401d7584cSLiam Girdwood 
152523607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
152601d7584cSLiam Girdwood {
152701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
152801d7584cSLiam Girdwood 
15298d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
1530a7e20444SKuninori Morimoto 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
153101d7584cSLiam Girdwood }
153201d7584cSLiam Girdwood 
1533531590bbSKuninori Morimoto void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1534531590bbSKuninori Morimoto 		      int do_hw_free, struct snd_soc_dpcm *last)
153501d7584cSLiam Girdwood {
153601d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
153701d7584cSLiam Girdwood 
153801d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15398d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
154001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
154101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
154201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
154301d7584cSLiam Girdwood 
1544531590bbSKuninori Morimoto 		if (dpcm == last)
1545531590bbSKuninori Morimoto 			return;
1546531590bbSKuninori Morimoto 
1547531590bbSKuninori Morimoto 		/* is this op for this BE ? */
1548531590bbSKuninori Morimoto 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1549531590bbSKuninori Morimoto 			continue;
1550531590bbSKuninori Morimoto 
15511db19c15SKuninori Morimoto 		if (be->dpcm[stream].users == 0) {
1552103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
155301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
155401d7584cSLiam Girdwood 				be->dpcm[stream].state);
15551db19c15SKuninori Morimoto 			continue;
15561db19c15SKuninori Morimoto 		}
155701d7584cSLiam Girdwood 
155801d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
155901d7584cSLiam Girdwood 			continue;
156001d7584cSLiam Girdwood 
1561531590bbSKuninori Morimoto 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1562531590bbSKuninori Morimoto 			if (!do_hw_free)
156301d7584cSLiam Girdwood 				continue;
156401d7584cSLiam Girdwood 
1565531590bbSKuninori Morimoto 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1566b7898396STakashi Iwai 				__soc_pcm_hw_free(be, be_substream);
1567531590bbSKuninori Morimoto 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1568531590bbSKuninori Morimoto 			}
1569531590bbSKuninori Morimoto 		}
1570531590bbSKuninori Morimoto 
1571b7898396STakashi Iwai 		__soc_pcm_close(be, be_substream);
157201d7584cSLiam Girdwood 		be_substream->runtime = NULL;
157301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
157401d7584cSLiam Girdwood 	}
157501d7584cSLiam Girdwood }
157601d7584cSLiam Girdwood 
157723607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
157801d7584cSLiam Girdwood {
15790d3a5178SKuninori Morimoto 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
158006aaeb87SKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
158101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
158201d7584cSLiam Girdwood 	int err, count = 0;
158301d7584cSLiam Girdwood 
158401d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
15858d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
158606aaeb87SKuninori Morimoto 		struct snd_pcm_substream *be_substream;
158701d7584cSLiam Girdwood 
158806aaeb87SKuninori Morimoto 		be = dpcm->be;
158906aaeb87SKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
159001d7584cSLiam Girdwood 
15912062b4c5SRussell King - ARM Linux 		if (!be_substream) {
15922062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
15932062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
15942062b4c5SRussell King - ARM Linux 			continue;
15952062b4c5SRussell King - ARM Linux 		}
15962062b4c5SRussell King - ARM Linux 
159701d7584cSLiam Girdwood 		/* is this op for this BE ? */
159801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
159901d7584cSLiam Girdwood 			continue;
160001d7584cSLiam Girdwood 
160101d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
16021db19c15SKuninori Morimoto 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1603103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
160401d7584cSLiam Girdwood 				stream ? "capture" : "playback",
160501d7584cSLiam Girdwood 				be->dpcm[stream].state);
16061db19c15SKuninori Morimoto 			continue;
16071db19c15SKuninori Morimoto 		}
160801d7584cSLiam Girdwood 
160901d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
161001d7584cSLiam Girdwood 			continue;
161101d7584cSLiam Girdwood 
161201d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
161301d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
161401d7584cSLiam Girdwood 			continue;
161501d7584cSLiam Girdwood 
16162062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
16172062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
161801d7584cSLiam Girdwood 
16190d3a5178SKuninori Morimoto 		be_substream->runtime = fe_substream->runtime;
1620b7898396STakashi Iwai 		err = __soc_pcm_open(be, be_substream);
162101d7584cSLiam Girdwood 		if (err < 0) {
162201d7584cSLiam Girdwood 			be->dpcm[stream].users--;
162301d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1624103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
162501d7584cSLiam Girdwood 					stream ? "capture" : "playback",
162601d7584cSLiam Girdwood 					be->dpcm[stream].state);
162701d7584cSLiam Girdwood 
162801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
162901d7584cSLiam Girdwood 			goto unwind;
163001d7584cSLiam Girdwood 		}
1631848aedfdSPierre-Louis Bossart 		be->dpcm[stream].be_start = 0;
163201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
163301d7584cSLiam Girdwood 		count++;
163401d7584cSLiam Girdwood 	}
163501d7584cSLiam Girdwood 
163601d7584cSLiam Girdwood 	return count;
163701d7584cSLiam Girdwood 
163801d7584cSLiam Girdwood unwind:
1639531590bbSKuninori Morimoto 	dpcm_be_dai_startup_rollback(fe, stream, dpcm);
164001d7584cSLiam Girdwood 
164104110728SKuninori Morimoto 	return soc_pcm_ret(fe, 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;
1651083a25b1SShengjiu Wang 	u64 formats = hw->formats;
16525f53898aSKuninori Morimoto 	int i;
16535f53898aSKuninori Morimoto 
16545f53898aSKuninori Morimoto 	soc_pcm_hw_init(hw);
16555f53898aSKuninori Morimoto 
1656083a25b1SShengjiu Wang 	if (formats)
1657083a25b1SShengjiu Wang 		hw->formats &= formats;
1658083a25b1SShengjiu Wang 
16595f53898aSKuninori Morimoto 	for_each_rtd_cpu_dais(fe, i, dai) {
16605f53898aSKuninori Morimoto 		struct snd_soc_pcm_stream *cpu_stream;
16615f53898aSKuninori Morimoto 
16625f53898aSKuninori Morimoto 		/*
16635f53898aSKuninori Morimoto 		 * Skip CPUs which don't support the current stream
16645f53898aSKuninori Morimoto 		 * type. See soc_pcm_init_runtime_hw() for more details
16655f53898aSKuninori Morimoto 		 */
16665f53898aSKuninori Morimoto 		if (!snd_soc_dai_stream_valid(dai, stream))
16675f53898aSKuninori Morimoto 			continue;
16685f53898aSKuninori Morimoto 
16695f53898aSKuninori Morimoto 		cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
16705f53898aSKuninori Morimoto 
16715f53898aSKuninori Morimoto 		soc_pcm_hw_update_rate(hw, cpu_stream);
16725f53898aSKuninori Morimoto 		soc_pcm_hw_update_chan(hw, cpu_stream);
16735f53898aSKuninori Morimoto 		soc_pcm_hw_update_format(hw, cpu_stream);
16745f53898aSKuninori Morimoto 	}
16755f53898aSKuninori Morimoto 
16765f53898aSKuninori Morimoto }
16775f53898aSKuninori Morimoto 
16781b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1679b073ed4eSKuninori Morimoto {
16800ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
16811b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
16824b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1683b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
16847afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1685b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1686b073ed4eSKuninori Morimoto 
1687b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1688435ffb76SJerome Brunet 		return;
1689b073ed4eSKuninori Morimoto 
1690b073ed4eSKuninori Morimoto 	/*
1691b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1692b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1693b073ed4eSKuninori Morimoto 	 */
1694b073ed4eSKuninori Morimoto 
16958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1696b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1697b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1698b073ed4eSKuninori Morimoto 		int i;
1699b073ed4eSKuninori Morimoto 
1700a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(be, i, dai) {
17014febced1SJerome Brunet 			/*
17024febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
17034febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
17044febced1SJerome Brunet 			 */
17057afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
17064febced1SJerome Brunet 				continue;
17074febced1SJerome Brunet 
1708acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1709b073ed4eSKuninori Morimoto 
1710debc71f2SKuninori Morimoto 			soc_pcm_hw_update_format(hw, codec_stream);
1711435ffb76SJerome Brunet 		}
1712b073ed4eSKuninori Morimoto 	}
1713b073ed4eSKuninori Morimoto }
1714b073ed4eSKuninori Morimoto 
17151b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1716f4c277b8SJiada Wang {
17170ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17181b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
17194b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1720f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1721f4c277b8SJiada Wang 	int stream = substream->stream;
1722f4c277b8SJiada Wang 
1723f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1724f4c277b8SJiada Wang 		return;
1725f4c277b8SJiada Wang 
1726f4c277b8SJiada Wang 	/*
1727f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1728f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1729f4c277b8SJiada Wang 	 */
1730f4c277b8SJiada Wang 
17318d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1732f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
17334f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
173419bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
173519bdcc7aSShreyas NC 		int i;
1736f4c277b8SJiada Wang 
1737a4be4187SKuninori Morimoto 		for_each_rtd_cpu_dais(be, i, dai) {
17380e9cf4c4SBard Liao 			/*
17390e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
17400e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
17410e9cf4c4SBard Liao 			 */
17420e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
17430e9cf4c4SBard Liao 				continue;
17440e9cf4c4SBard Liao 
174519bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
17464f2bd18bSJerome Brunet 
17476cb56a45SKuninori Morimoto 			soc_pcm_hw_update_chan(hw, cpu_stream);
174819bdcc7aSShreyas NC 		}
17494f2bd18bSJerome Brunet 
17504f2bd18bSJerome Brunet 		/*
17514f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
17524f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
17534f2bd18bSJerome Brunet 		 */
17543989ade2SKuninori Morimoto 		if (be->dai_link->num_codecs == 1) {
17559bdc573dSKuninori Morimoto 			struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
17569bdc573dSKuninori Morimoto 				asoc_rtd_to_codec(be, 0), stream);
1757f4c277b8SJiada Wang 
17586cb56a45SKuninori Morimoto 			soc_pcm_hw_update_chan(hw, codec_stream);
1759f4c277b8SJiada Wang 		}
1760f4c277b8SJiada Wang 	}
1761f4c277b8SJiada Wang }
1762f4c277b8SJiada Wang 
17631b8cb123SKuninori Morimoto static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1764baacd8d1SJerome Brunet {
17650ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
17661b8cb123SKuninori Morimoto 	struct snd_pcm_runtime *runtime = substream->runtime;
17674b260f42SKuninori Morimoto 	struct snd_pcm_hardware *hw = &runtime->hw;
1768baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1769baacd8d1SJerome Brunet 	int stream = substream->stream;
1770baacd8d1SJerome Brunet 
1771baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1772baacd8d1SJerome Brunet 		return;
1773baacd8d1SJerome Brunet 
1774baacd8d1SJerome Brunet 	/*
1775baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1776baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1777baacd8d1SJerome Brunet 	 */
1778baacd8d1SJerome Brunet 
17798d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1780baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1781c840f769SKuninori Morimoto 		struct snd_soc_pcm_stream *pcm;
17827afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1783baacd8d1SJerome Brunet 		int i;
1784baacd8d1SJerome Brunet 
1785c840f769SKuninori Morimoto 		for_each_rtd_dais(be, i, dai) {
17860e9cf4c4SBard Liao 			/*
1787c840f769SKuninori Morimoto 			 * Skip DAIs which don't support the current stream
17880e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
17890e9cf4c4SBard Liao 			 */
17900e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
17910e9cf4c4SBard Liao 				continue;
17920e9cf4c4SBard Liao 
1793c840f769SKuninori Morimoto 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1794baacd8d1SJerome Brunet 
1795f6c04af5SKuninori Morimoto 			soc_pcm_hw_update_rate(hw, pcm);
1796baacd8d1SJerome Brunet 		}
1797baacd8d1SJerome Brunet 	}
1798baacd8d1SJerome Brunet }
1799baacd8d1SJerome Brunet 
1800906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1801906c7d69SPC Liao 			       int stream)
1802906c7d69SPC Liao {
1803906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
18040ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
180519bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
180612ffd726SJaroslav Kysela 	int err = 0;
180719bdcc7aSShreyas NC 	int i;
1808906c7d69SPC Liao 
1809906c7d69SPC Liao 	/* apply symmetry for FE */
181068cbc557SKuninori Morimoto 	soc_pcm_update_symmetry(fe_substream);
1811906c7d69SPC Liao 
1812a4be4187SKuninori Morimoto 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1813906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1814906c7d69SPC Liao 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1815906c7d69SPC Liao 		if (err < 0)
1816bbd2bac8SKuninori Morimoto 			goto error;
1817906c7d69SPC Liao 	}
1818906c7d69SPC Liao 
1819906c7d69SPC Liao 	/* apply symmetry for BE */
18208d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1821906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1822906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1823906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
18246246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
1825c840f769SKuninori Morimoto 		struct snd_soc_dai *dai;
1826906c7d69SPC Liao 
18276246f283SJerome Brunet 		/* A backend may not have the requested substream */
18286246f283SJerome Brunet 		if (!be_substream)
18296246f283SJerome Brunet 			continue;
18306246f283SJerome Brunet 
18310ceef681SKuninori Morimoto 		rtd = asoc_substream_to_rtd(be_substream);
1832f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1833f1176614SJeeja KP 			continue;
1834f1176614SJeeja KP 
183568cbc557SKuninori Morimoto 		soc_pcm_update_symmetry(be_substream);
1836906c7d69SPC Liao 
1837906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1838c840f769SKuninori Morimoto 		for_each_rtd_dais(rtd, i, dai) {
1839c840f769SKuninori Morimoto 			err = soc_pcm_apply_symmetry(fe_substream, dai);
1840906c7d69SPC Liao 			if (err < 0)
1841bbd2bac8SKuninori Morimoto 				goto error;
1842906c7d69SPC Liao 		}
1843906c7d69SPC Liao 	}
1844bbd2bac8SKuninori Morimoto error:
184504110728SKuninori Morimoto 	return soc_pcm_ret(fe, err);
1846906c7d69SPC Liao }
1847906c7d69SPC Liao 
184801d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
184901d7584cSLiam Girdwood {
18500ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
185101d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
185201d7584cSLiam Girdwood 
1853ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
185401d7584cSLiam Girdwood 
185525c2f515SKuninori Morimoto 	ret = dpcm_be_dai_startup(fe, stream);
185606aaeb87SKuninori Morimoto 	if (ret < 0)
185701d7584cSLiam Girdwood 		goto be_err;
185801d7584cSLiam Girdwood 
1859103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
186001d7584cSLiam Girdwood 
186101d7584cSLiam Girdwood 	/* start the DAI frontend */
1862b7898396STakashi Iwai 	ret = __soc_pcm_open(fe, fe_substream);
1863e4b044f4SKuninori Morimoto 	if (ret < 0)
186401d7584cSLiam Girdwood 		goto unwind;
186501d7584cSLiam Girdwood 
186601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
186701d7584cSLiam Girdwood 
18684fe28461SKuninori Morimoto 	dpcm_runtime_setup_fe(fe_substream);
18694fe28461SKuninori Morimoto 
18704fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_format(fe_substream);
18714fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_chan(fe_substream);
18724fe28461SKuninori Morimoto 	dpcm_runtime_setup_be_rate(fe_substream);
187301d7584cSLiam Girdwood 
1874906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
187501d7584cSLiam Girdwood 
187601d7584cSLiam Girdwood unwind:
18778a01fbf0SKuninori Morimoto 	if (ret < 0)
187825c2f515SKuninori Morimoto 		dpcm_be_dai_startup_unwind(fe, stream);
187901d7584cSLiam Girdwood be_err:
1880ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
188106aaeb87SKuninori Morimoto 
188204110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
188301d7584cSLiam Girdwood }
188401d7584cSLiam Girdwood 
188501d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
188601d7584cSLiam Girdwood {
18870ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
188801d7584cSLiam Girdwood 	int stream = substream->stream;
188901d7584cSLiam Girdwood 
1890b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
1891b7898396STakashi Iwai 
1892ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
189301d7584cSLiam Girdwood 
189401d7584cSLiam Girdwood 	/* shutdown the BEs */
189525c2f515SKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
189601d7584cSLiam Girdwood 
1897103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
189801d7584cSLiam Girdwood 
189901d7584cSLiam Girdwood 	/* now shutdown the frontend */
1900b7898396STakashi Iwai 	__soc_pcm_close(fe, substream);
190101d7584cSLiam Girdwood 
1902bb9dd3ceSRanjani Sridharan 	/* run the stream stop event */
1903bb9dd3ceSRanjani Sridharan 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1904bb9dd3ceSRanjani Sridharan 
190501d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1906ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
190701d7584cSLiam Girdwood 	return 0;
190801d7584cSLiam Girdwood }
190901d7584cSLiam Girdwood 
1910f52366e6SKuninori Morimoto void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
191101d7584cSLiam Girdwood {
191201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
191301d7584cSLiam Girdwood 
191401d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
191501d7584cSLiam Girdwood 	 * to this frontend DAI */
19168d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
191701d7584cSLiam Girdwood 
191801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
191901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
192001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
192101d7584cSLiam Girdwood 
192201d7584cSLiam Girdwood 		/* is this op for this BE ? */
192301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
192401d7584cSLiam Girdwood 			continue;
192501d7584cSLiam Girdwood 
192601d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
192701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
192801d7584cSLiam Girdwood 				continue;
192901d7584cSLiam Girdwood 
193036fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
193136fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
193236fba62cSQiao Zhou 			continue;
193336fba62cSQiao Zhou 
193401d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
193501d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
193601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
193708b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
19385e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
19395e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
194001d7584cSLiam Girdwood 			continue;
194101d7584cSLiam Girdwood 
1942103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
194394d215ccS彭东林 			be->dai_link->name);
194401d7584cSLiam Girdwood 
1945b7898396STakashi Iwai 		__soc_pcm_hw_free(be, be_substream);
194601d7584cSLiam Girdwood 
194701d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
194801d7584cSLiam Girdwood 	}
194901d7584cSLiam Girdwood }
195001d7584cSLiam Girdwood 
195145c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
195201d7584cSLiam Girdwood {
19530ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1954f52366e6SKuninori Morimoto 	int stream = substream->stream;
195501d7584cSLiam Girdwood 
1956b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
1957ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
195801d7584cSLiam Girdwood 
1959103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
196001d7584cSLiam Girdwood 
196101d7584cSLiam Girdwood 	/* call hw_free on the frontend */
1962b7898396STakashi Iwai 	soc_pcm_hw_clean(fe, substream, 0);
196301d7584cSLiam Girdwood 
196401d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
196501d7584cSLiam Girdwood 	 * to this frontend DAI */
1966f52366e6SKuninori Morimoto 	dpcm_be_dai_hw_free(fe, stream);
196701d7584cSLiam Girdwood 
196801d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1969ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
197001d7584cSLiam Girdwood 
1971b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
197201d7584cSLiam Girdwood 	return 0;
197301d7584cSLiam Girdwood }
197401d7584cSLiam Girdwood 
197523607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
197601d7584cSLiam Girdwood {
197733b6b94fSKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
197833b6b94fSKuninori Morimoto 	struct snd_pcm_substream *be_substream;
197901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
198001d7584cSLiam Girdwood 	int ret;
198101d7584cSLiam Girdwood 
19828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
198325106550SKuninori Morimoto 		struct snd_pcm_hw_params hw_params;
198425106550SKuninori Morimoto 
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 */
199325106550SKuninori Morimoto 		memcpy(&hw_params, &fe->dpcm[stream].hw_params,
199401d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
199501d7584cSLiam Girdwood 
199601d7584cSLiam Girdwood 		/* perform any hw_params fixups */
199725106550SKuninori Morimoto 		ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
19980cbbf8a0SKuninori Morimoto 		if (ret < 0)
199901d7584cSLiam Girdwood 			goto unwind;
200001d7584cSLiam Girdwood 
2001ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
200225106550SKuninori Morimoto 		memcpy(&be->dpcm[stream].hw_params, &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 
201725106550SKuninori Morimoto 		ret = __soc_pcm_hw_params(be, be_substream, &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 
208304110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
208401d7584cSLiam Girdwood }
208501d7584cSLiam Girdwood 
208623607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
208745c0a188SMark Brown 			       int cmd)
208801d7584cSLiam Girdwood {
2089db3aa39cSKuninori Morimoto 	struct snd_soc_pcm_runtime *be;
20909995c1d0SPierre-Louis Bossart 	bool pause_stop_transition;
209101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2092b2ae8066STakashi Iwai 	unsigned long flags;
209301d7584cSLiam Girdwood 	int ret = 0;
209401d7584cSLiam Girdwood 
20958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2096db3aa39cSKuninori Morimoto 		struct snd_pcm_substream *be_substream;
209701d7584cSLiam Girdwood 
2098db3aa39cSKuninori Morimoto 		be = dpcm->be;
2099db3aa39cSKuninori Morimoto 		be_substream = snd_soc_dpcm_get_substream(be, stream);
210001d7584cSLiam Girdwood 
21013c75c0eaSTakashi Iwai 		snd_soc_dpcm_stream_lock_irqsave_nested(be, stream, flags);
2102b2ae8066STakashi Iwai 
210301d7584cSLiam Girdwood 		/* is this op for this BE ? */
210401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2105b2ae8066STakashi Iwai 			goto next;
210601d7584cSLiam Girdwood 
2107a9faca15SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2108a9faca15SKuninori Morimoto 			be->dai_link->name, cmd);
2109a9faca15SKuninori Morimoto 
211001d7584cSLiam Girdwood 		switch (cmd) {
211101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
2112848aedfdSPierre-Louis Bossart 			if (!be->dpcm[stream].be_start &&
2113848aedfdSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
211421fca8bdS이경택 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
21153202e2f5SMark Brown 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2116b2ae8066STakashi Iwai 				goto next;
21176479f758SPierre-Louis Bossart 
2118848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2119848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2120b2ae8066STakashi Iwai 				goto next;
212101d7584cSLiam Girdwood 
2122374b50e2SPierre-Louis Bossart 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2123374b50e2SPierre-Louis Bossart 				ret = soc_pcm_trigger(be_substream,
2124374b50e2SPierre-Louis Bossart 						      SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2125374b50e2SPierre-Louis Bossart 			else
2126374b50e2SPierre-Louis Bossart 				ret = soc_pcm_trigger(be_substream,
2127374b50e2SPierre-Louis Bossart 						      SNDRV_PCM_TRIGGER_START);
2128848aedfdSPierre-Louis Bossart 			if (ret) {
2129848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2130848aedfdSPierre-Louis Bossart 				goto next;
2131848aedfdSPierre-Louis Bossart 			}
2132848aedfdSPierre-Louis Bossart 
21333202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
213401d7584cSLiam Girdwood 			break;
213501d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
21363202e2f5SMark Brown 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2137b2ae8066STakashi Iwai 				goto next;
21386479f758SPierre-Louis Bossart 
2139848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2140848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2141b2ae8066STakashi Iwai 				goto next;
214201d7584cSLiam Girdwood 
2143848aedfdSPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2144848aedfdSPierre-Louis Bossart 			if (ret) {
2145848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2146848aedfdSPierre-Louis Bossart 				goto next;
2147848aedfdSPierre-Louis Bossart 			}
2148848aedfdSPierre-Louis Bossart 
21493202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
215001d7584cSLiam Girdwood 			break;
215101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
21523aa1e96aSPierre-Louis Bossart 			if (!be->dpcm[stream].be_start &&
21533aa1e96aSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
21543aa1e96aSPierre-Louis Bossart 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2155b2ae8066STakashi Iwai 				goto next;
21566479f758SPierre-Louis Bossart 
21579995c1d0SPierre-Louis Bossart 			fe->dpcm[stream].fe_pause = false;
21589995c1d0SPierre-Louis Bossart 			be->dpcm[stream].be_pause--;
21599995c1d0SPierre-Louis Bossart 
2160848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start++;
2161848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 1)
2162b2ae8066STakashi Iwai 				goto next;
216301d7584cSLiam Girdwood 
2164848aedfdSPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2165848aedfdSPierre-Louis Bossart 			if (ret) {
2166848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2167848aedfdSPierre-Louis Bossart 				goto next;
2168848aedfdSPierre-Louis Bossart 			}
2169848aedfdSPierre-Louis Bossart 
21703202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
217101d7584cSLiam Girdwood 			break;
217201d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
217321fca8bdS이경택 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
21743202e2f5SMark Brown 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2175b2ae8066STakashi Iwai 				goto next;
217601d7584cSLiam Girdwood 
2177848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2178848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start--;
2179848aedfdSPierre-Louis Bossart 
2180848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2181b2ae8066STakashi Iwai 				goto next;
21820c75fc71SPierre-Louis Bossart 
21839995c1d0SPierre-Louis Bossart 			pause_stop_transition = false;
21849995c1d0SPierre-Louis Bossart 			if (fe->dpcm[stream].fe_pause) {
21859995c1d0SPierre-Louis Bossart 				pause_stop_transition = true;
21869995c1d0SPierre-Louis Bossart 				fe->dpcm[stream].fe_pause = false;
21879995c1d0SPierre-Louis Bossart 				be->dpcm[stream].be_pause--;
21889995c1d0SPierre-Louis Bossart 			}
21899995c1d0SPierre-Louis Bossart 
21909995c1d0SPierre-Louis Bossart 			if (be->dpcm[stream].be_pause != 0)
21919995c1d0SPierre-Louis Bossart 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
21929995c1d0SPierre-Louis Bossart 			else
21939995c1d0SPierre-Louis Bossart 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
21949995c1d0SPierre-Louis Bossart 
2195848aedfdSPierre-Louis Bossart 			if (ret) {
2196848aedfdSPierre-Louis Bossart 				if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2197848aedfdSPierre-Louis Bossart 					be->dpcm[stream].be_start++;
21989995c1d0SPierre-Louis Bossart 				if (pause_stop_transition) {
21999995c1d0SPierre-Louis Bossart 					fe->dpcm[stream].fe_pause = true;
22009995c1d0SPierre-Louis Bossart 					be->dpcm[stream].be_pause++;
22019995c1d0SPierre-Louis Bossart 				}
2202b2ae8066STakashi Iwai 				goto next;
2203848aedfdSPierre-Louis Bossart 			}
22040c75fc71SPierre-Louis Bossart 
22059995c1d0SPierre-Louis Bossart 			if (be->dpcm[stream].be_pause != 0)
22069995c1d0SPierre-Louis Bossart 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
22079995c1d0SPierre-Louis Bossart 			else
22083202e2f5SMark Brown 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
22099995c1d0SPierre-Louis Bossart 
221001d7584cSLiam Girdwood 			break;
221101d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
22123202e2f5SMark Brown 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2213b2ae8066STakashi Iwai 				goto next;
221401d7584cSLiam Girdwood 
2215848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start--;
2216848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2217b2ae8066STakashi Iwai 				goto next;
221801d7584cSLiam Girdwood 
22190c75fc71SPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2220848aedfdSPierre-Louis Bossart 			if (ret) {
2221848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start++;
2222b2ae8066STakashi Iwai 				goto next;
2223848aedfdSPierre-Louis Bossart 			}
22240c75fc71SPierre-Louis Bossart 
22253202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
222601d7584cSLiam Girdwood 			break;
222701d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
22283202e2f5SMark Brown 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2229b2ae8066STakashi Iwai 				goto next;
223001d7584cSLiam Girdwood 
22319995c1d0SPierre-Louis Bossart 			fe->dpcm[stream].fe_pause = true;
22329995c1d0SPierre-Louis Bossart 			be->dpcm[stream].be_pause++;
22339995c1d0SPierre-Louis Bossart 
2234848aedfdSPierre-Louis Bossart 			be->dpcm[stream].be_start--;
2235848aedfdSPierre-Louis Bossart 			if (be->dpcm[stream].be_start != 0)
2236b2ae8066STakashi Iwai 				goto next;
22370c75fc71SPierre-Louis Bossart 
22380c75fc71SPierre-Louis Bossart 			ret = soc_pcm_trigger(be_substream, cmd);
2239848aedfdSPierre-Louis Bossart 			if (ret) {
2240848aedfdSPierre-Louis Bossart 				be->dpcm[stream].be_start++;
2241b2ae8066STakashi Iwai 				goto next;
2242848aedfdSPierre-Louis Bossart 			}
22430c75fc71SPierre-Louis Bossart 
22443202e2f5SMark Brown 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
224501d7584cSLiam Girdwood 			break;
224601d7584cSLiam Girdwood 		}
2247b2ae8066STakashi Iwai next:
2248b2ae8066STakashi Iwai 		snd_soc_dpcm_stream_unlock_irqrestore(be, stream, flags);
2249b2ae8066STakashi Iwai 		if (ret)
2250b2ae8066STakashi Iwai 			break;
225101d7584cSLiam Girdwood 	}
225204110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
225301d7584cSLiam Girdwood }
225401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
225501d7584cSLiam Girdwood 
2256acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2257acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2258acbf2774SRanjani Sridharan {
22590ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2260acbf2774SRanjani Sridharan 	int ret;
2261acbf2774SRanjani Sridharan 
2262acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2263acbf2774SRanjani Sridharan 	if (fe_first) {
2264acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2265acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2266acbf2774SRanjani Sridharan 
2267acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2268acbf2774SRanjani Sridharan 		if (ret < 0)
2269acbf2774SRanjani Sridharan 			return ret;
2270acbf2774SRanjani Sridharan 
2271acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2272acbf2774SRanjani Sridharan 		return ret;
2273acbf2774SRanjani Sridharan 	}
2274acbf2774SRanjani Sridharan 
2275acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2276acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2277acbf2774SRanjani Sridharan 	if (ret < 0)
2278acbf2774SRanjani Sridharan 		return ret;
2279acbf2774SRanjani Sridharan 
2280acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2281acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2282acbf2774SRanjani Sridharan 
2283acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2284acbf2774SRanjani Sridharan 
2285acbf2774SRanjani Sridharan 	return ret;
2286acbf2774SRanjani Sridharan }
2287acbf2774SRanjani Sridharan 
2288ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
228901d7584cSLiam Girdwood {
22900ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2291acbf2774SRanjani Sridharan 	int stream = substream->stream;
2292acbf2774SRanjani Sridharan 	int ret = 0;
229301d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
229401d7584cSLiam Girdwood 
229501d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
229601d7584cSLiam Girdwood 
229701d7584cSLiam Girdwood 	switch (trigger) {
229801d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2299acbf2774SRanjani Sridharan 		switch (cmd) {
2300acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2301acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2302acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23034c22b80fSCezary Rojewski 		case SNDRV_PCM_TRIGGER_DRAIN:
2304acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2305acbf2774SRanjani Sridharan 			break;
2306acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2307acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2308acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2309acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2310acbf2774SRanjani Sridharan 			break;
2311acbf2774SRanjani Sridharan 		default:
2312acbf2774SRanjani Sridharan 			ret = -EINVAL;
2313acbf2774SRanjani Sridharan 			break;
231401d7584cSLiam Girdwood 		}
231501d7584cSLiam Girdwood 		break;
231601d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2317acbf2774SRanjani Sridharan 		switch (cmd) {
2318acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2319acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2320acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23214c22b80fSCezary Rojewski 		case SNDRV_PCM_TRIGGER_DRAIN:
2322acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2323acbf2774SRanjani Sridharan 			break;
2324acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2325acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2326acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2327acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2328acbf2774SRanjani Sridharan 			break;
2329acbf2774SRanjani Sridharan 		default:
2330acbf2774SRanjani Sridharan 			ret = -EINVAL;
2331acbf2774SRanjani Sridharan 			break;
233201d7584cSLiam Girdwood 		}
233301d7584cSLiam Girdwood 		break;
233407bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
233507bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
233607bf84aaSLiam Girdwood 
2337103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
233807bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
233907bf84aaSLiam Girdwood 
234030819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
234107bf84aaSLiam Girdwood 		break;
234201d7584cSLiam Girdwood 	default:
2343103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
234401d7584cSLiam Girdwood 				fe->dai_link->name);
234501d7584cSLiam Girdwood 		ret = -EINVAL;
234601d7584cSLiam Girdwood 		goto out;
234701d7584cSLiam Girdwood 	}
234801d7584cSLiam Girdwood 
2349acbf2774SRanjani Sridharan 	if (ret < 0) {
2350acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2351acbf2774SRanjani Sridharan 			cmd, ret);
2352acbf2774SRanjani Sridharan 		goto out;
2353acbf2774SRanjani Sridharan 	}
2354acbf2774SRanjani Sridharan 
235501d7584cSLiam Girdwood 	switch (cmd) {
235601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
235701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
235801d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
235901d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
236001d7584cSLiam Girdwood 		break;
236101d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
236201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
236301d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
236401d7584cSLiam Girdwood 		break;
23659f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
23669f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
23679f169b9fSPatrick Lai 		break;
236801d7584cSLiam Girdwood 	}
236901d7584cSLiam Girdwood 
237001d7584cSLiam Girdwood out:
237101d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
237201d7584cSLiam Girdwood 	return ret;
237301d7584cSLiam Girdwood }
237401d7584cSLiam Girdwood 
2375ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2376ea9d0d77STakashi Iwai {
23770ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2378ea9d0d77STakashi Iwai 	int stream = substream->stream;
2379ea9d0d77STakashi Iwai 
2380ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2381ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2382ea9d0d77STakashi Iwai 	 */
2383ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2384ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2385ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2386ea9d0d77STakashi Iwai 	}
2387ea9d0d77STakashi Iwai 
2388ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2389ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2390ea9d0d77STakashi Iwai }
2391ea9d0d77STakashi Iwai 
239223607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
239301d7584cSLiam Girdwood {
239401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
239501d7584cSLiam Girdwood 	int ret = 0;
239601d7584cSLiam Girdwood 
23978d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
239801d7584cSLiam Girdwood 
239901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
240001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
240101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
240201d7584cSLiam Girdwood 
240301d7584cSLiam Girdwood 		/* is this op for this BE ? */
240401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
240501d7584cSLiam Girdwood 			continue;
240601d7584cSLiam Girdwood 
240701d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
240895f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
24095087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
24105087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
241101d7584cSLiam Girdwood 			continue;
241201d7584cSLiam Girdwood 
2413103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
241494d215ccS彭东林 			be->dai_link->name);
241501d7584cSLiam Girdwood 
2416b7898396STakashi Iwai 		ret = __soc_pcm_prepare(be, be_substream);
2417dab7eeb4SKuninori Morimoto 		if (ret < 0)
241801d7584cSLiam Girdwood 			break;
241901d7584cSLiam Girdwood 
242001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
242101d7584cSLiam Girdwood 	}
2422273db971SKuninori Morimoto 
242304110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
242401d7584cSLiam Girdwood }
242501d7584cSLiam Girdwood 
242645c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
242701d7584cSLiam Girdwood {
24280ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
242901d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
243001d7584cSLiam Girdwood 
2431b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
243201d7584cSLiam Girdwood 
2433103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
243401d7584cSLiam Girdwood 
2435ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
243601d7584cSLiam Girdwood 
243701d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
243801d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2439103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
244001d7584cSLiam Girdwood 				fe->dai_link->name);
244101d7584cSLiam Girdwood 		ret = -EINVAL;
244201d7584cSLiam Girdwood 		goto out;
244301d7584cSLiam Girdwood 	}
244401d7584cSLiam Girdwood 
244525c2f515SKuninori Morimoto 	ret = dpcm_be_dai_prepare(fe, stream);
244601d7584cSLiam Girdwood 	if (ret < 0)
244701d7584cSLiam Girdwood 		goto out;
244801d7584cSLiam Girdwood 
244901d7584cSLiam Girdwood 	/* call prepare on the frontend */
2450b7898396STakashi Iwai 	ret = __soc_pcm_prepare(fe, substream);
2451dab7eeb4SKuninori Morimoto 	if (ret < 0)
245201d7584cSLiam Girdwood 		goto out;
245301d7584cSLiam Girdwood 
245401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
245501d7584cSLiam Girdwood 
245601d7584cSLiam Girdwood out:
2457ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2458b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
245901d7584cSLiam Girdwood 
246004110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
246101d7584cSLiam Girdwood }
246201d7584cSLiam Girdwood 
2463618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2464618dae11SLiam Girdwood {
246507bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
246607bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
246707bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2468618dae11SLiam Girdwood 	int err;
246901d7584cSLiam Girdwood 
2470103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2471618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2472618dae11SLiam Girdwood 
247307bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
247407bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2475103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
247607bf84aaSLiam Girdwood 				fe->dai_link->name);
247707bf84aaSLiam Girdwood 
247830819358SKuninori Morimoto 		err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
247907bf84aaSLiam Girdwood 	} else {
2480103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
248107bf84aaSLiam Girdwood 			fe->dai_link->name);
248207bf84aaSLiam Girdwood 
2483618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
248407bf84aaSLiam Girdwood 	}
2485618dae11SLiam Girdwood 
2486f52366e6SKuninori Morimoto 	dpcm_be_dai_hw_free(fe, stream);
2487618dae11SLiam Girdwood 
2488531590bbSKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
2489618dae11SLiam Girdwood 
2490618dae11SLiam Girdwood 	/* run the stream event for each BE */
2491618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2492618dae11SLiam Girdwood 
249304110728SKuninori Morimoto 	return soc_pcm_ret(fe, err);
2494618dae11SLiam Girdwood }
2495618dae11SLiam Girdwood 
2496618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2497618dae11SLiam Girdwood {
249807bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
249907bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2500618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
250107bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
25024eeed5f4SSouptick Joarder 	int ret = 0;
2503618dae11SLiam Girdwood 
2504103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2505618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2506618dae11SLiam Girdwood 
2507618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2508618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
25092c138284S朱灿灿 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
25102c138284S朱灿灿 		dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
25112c138284S朱灿灿 			fe->dai_link->name, fe->dpcm[stream].state);
2512e91b65b3SDan Carpenter 		ret = -EINVAL;
25132c138284S朱灿灿 		goto disconnect;
25142c138284S朱灿灿 	}
2515618dae11SLiam Girdwood 
2516618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2517618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2518fffc0ca2SDan Carpenter 	if (ret < 0)
2519618dae11SLiam Girdwood 		goto disconnect;
2520618dae11SLiam Girdwood 
2521618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2522618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2523618dae11SLiam Girdwood 		return 0;
2524618dae11SLiam Girdwood 
2525618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2526fffc0ca2SDan Carpenter 	if (ret < 0)
2527618dae11SLiam Girdwood 		goto close;
2528618dae11SLiam Girdwood 
2529618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2530618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2531618dae11SLiam Girdwood 		return 0;
2532618dae11SLiam Girdwood 
2533618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2534fffc0ca2SDan Carpenter 	if (ret < 0)
2535618dae11SLiam Girdwood 		goto hw_free;
2536618dae11SLiam Girdwood 
2537618dae11SLiam Girdwood 	/* run the stream event for each BE */
2538618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2539618dae11SLiam Girdwood 
2540618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2541618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2542618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2543618dae11SLiam Girdwood 		return 0;
2544618dae11SLiam Girdwood 
254507bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
254607bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2547103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
254807bf84aaSLiam Girdwood 				fe->dai_link->name);
254907bf84aaSLiam Girdwood 
255030819358SKuninori Morimoto 		ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
255162462e01SKuninori Morimoto 		if (ret < 0)
255207bf84aaSLiam Girdwood 			goto hw_free;
255307bf84aaSLiam Girdwood 	} else {
2554103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2555618dae11SLiam Girdwood 			fe->dai_link->name);
2556618dae11SLiam Girdwood 
2557618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2558618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2559db3aa39cSKuninori Morimoto 		if (ret < 0)
2560618dae11SLiam Girdwood 			goto hw_free;
2561618dae11SLiam Girdwood 	}
2562618dae11SLiam Girdwood 
2563618dae11SLiam Girdwood 	return 0;
2564618dae11SLiam Girdwood 
2565618dae11SLiam Girdwood hw_free:
2566618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2567618dae11SLiam Girdwood close:
2568618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2569618dae11SLiam Girdwood disconnect:
25702c138284S朱灿灿 	/* disconnect any pending BEs */
25718d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2572618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
25732c138284S朱灿灿 
25742c138284S朱灿灿 		/* is this op for this BE ? */
25752c138284S朱灿灿 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
25762c138284S朱灿灿 			continue;
25772c138284S朱灿灿 
25782c138284S朱灿灿 		if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
25792c138284S朱灿灿 			be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2580618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2581618dae11SLiam Girdwood 	}
2582618dae11SLiam Girdwood 
258304110728SKuninori Morimoto 	return soc_pcm_ret(fe, ret);
2584618dae11SLiam Girdwood }
2585618dae11SLiam Girdwood 
2586de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2587618dae11SLiam Girdwood {
2588618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
25897083f877SKuninori Morimoto 	int stream;
2590de15d7ffSJerome Brunet 	int count, paths;
2591618dae11SLiam Girdwood 
259296bf62f0SPierre-Louis Bossart 	if (!fe->dai_link->dynamic)
259396bf62f0SPierre-Louis Bossart 		return 0;
259496bf62f0SPierre-Louis Bossart 
25953989ade2SKuninori Morimoto 	if (fe->dai_link->num_cpus > 1) {
25966e1276a5SBard Liao 		dev_err(fe->dev,
25976e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
25986e1276a5SBard Liao 		return -EINVAL;
25996e1276a5SBard Liao 	}
26006e1276a5SBard Liao 
2601618dae11SLiam Girdwood 	/* only check active links */
2602b3dea624SKuninori Morimoto 	if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2603de15d7ffSJerome Brunet 		return 0;
2604618dae11SLiam Girdwood 
2605618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2606de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2607de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2608618dae11SLiam Girdwood 
26097083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2610075207d2SQiao Zhou 
26117083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
2612c2233a26SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0),   stream) ||
2613c2233a26SKuninori Morimoto 		    !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
26147083f877SKuninori Morimoto 			continue;
2615618dae11SLiam Girdwood 
26167083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
2617b3dea624SKuninori Morimoto 		if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2618b3dea624SKuninori Morimoto 		    !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
26197083f877SKuninori Morimoto 			continue;
26207083f877SKuninori Morimoto 
26217083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2622d479f00bSKuninori Morimoto 		if (paths < 0)
2623618dae11SLiam Girdwood 			return paths;
2624618dae11SLiam Girdwood 
26257083f877SKuninori Morimoto 		/* update any playback/capture paths */
26267083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2627de15d7ffSJerome Brunet 		if (count) {
2628580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2629de15d7ffSJerome Brunet 			if (new)
263081c82a9eSKuninori Morimoto 				dpcm_run_update_startup(fe, stream);
2631de15d7ffSJerome Brunet 			else
263281c82a9eSKuninori Morimoto 				dpcm_run_update_shutdown(fe, stream);
2633580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2634de15d7ffSJerome Brunet 
26357083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
26367083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2637618dae11SLiam Girdwood 		}
2638618dae11SLiam Girdwood 
26397ed9de76SQiao Zhou 		dpcm_path_put(&list);
2640618dae11SLiam Girdwood 	}
2641618dae11SLiam Girdwood 
2642de15d7ffSJerome Brunet 	return 0;
2643618dae11SLiam Girdwood }
2644618dae11SLiam Girdwood 
2645de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2646de15d7ffSJerome Brunet  * any DAI links.
2647de15d7ffSJerome Brunet  */
2648f17a1478SGuennadi Liakhovetski int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2649de15d7ffSJerome Brunet {
2650de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2651de15d7ffSJerome Brunet 	int ret = 0;
2652de15d7ffSJerome Brunet 
265338e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_lock(card);
2654de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2655bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2656de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2657de15d7ffSJerome Brunet 		if (ret)
2658de15d7ffSJerome Brunet 			goto out;
2659de15d7ffSJerome Brunet 	}
2660de15d7ffSJerome Brunet 
2661de15d7ffSJerome Brunet 	/* bring new paths up */
2662bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2663de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2664de15d7ffSJerome Brunet 		if (ret)
2665de15d7ffSJerome Brunet 			goto out;
2666de15d7ffSJerome Brunet 	}
2667de15d7ffSJerome Brunet 
2668de15d7ffSJerome Brunet out:
266938e42f6dSKuninori Morimoto 	snd_soc_dpcm_mutex_unlock(card);
2670de15d7ffSJerome Brunet 	return ret;
2671618dae11SLiam Girdwood }
2672f17a1478SGuennadi Liakhovetski EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
267301d7584cSLiam Girdwood 
2674265694b6SKuninori Morimoto static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
267501d7584cSLiam Girdwood {
26760ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
267701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
2678265694b6SKuninori Morimoto 	int stream = fe_substream->stream;
267930fca26fSKuninori Morimoto 
2680b7898396STakashi Iwai 	snd_soc_dpcm_mutex_assert_held(fe);
2681b7898396STakashi Iwai 
268230fca26fSKuninori Morimoto 	/* mark FE's links ready to prune */
268330fca26fSKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
268430fca26fSKuninori Morimoto 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
268530fca26fSKuninori Morimoto 
268630fca26fSKuninori Morimoto 	dpcm_be_disconnect(fe, stream);
2687265694b6SKuninori Morimoto }
2688265694b6SKuninori Morimoto 
2689265694b6SKuninori Morimoto static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2690265694b6SKuninori Morimoto {
26910ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2692265694b6SKuninori Morimoto 	int ret;
2693265694b6SKuninori Morimoto 
2694b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
2695265694b6SKuninori Morimoto 	ret = dpcm_fe_dai_shutdown(fe_substream);
2696265694b6SKuninori Morimoto 
2697265694b6SKuninori Morimoto 	dpcm_fe_dai_cleanup(fe_substream);
2698265694b6SKuninori Morimoto 
2699b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
270030fca26fSKuninori Morimoto 	return ret;
270130fca26fSKuninori Morimoto }
270230fca26fSKuninori Morimoto 
270301d7584cSLiam Girdwood static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
270401d7584cSLiam Girdwood {
27050ceef681SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
270601d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
270701d7584cSLiam Girdwood 	int ret;
270801d7584cSLiam Girdwood 	int stream = fe_substream->stream;
270901d7584cSLiam Girdwood 
2710b7898396STakashi Iwai 	snd_soc_dpcm_mutex_lock(fe);
271101d7584cSLiam Girdwood 
27128f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
2713d479f00bSKuninori Morimoto 	if (ret < 0)
2714cae06eb9SKuninori Morimoto 		goto open_end;
271501d7584cSLiam Girdwood 
271601d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
271701d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
271801d7584cSLiam Girdwood 
271901d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
2720265694b6SKuninori Morimoto 	if (ret < 0)
2721265694b6SKuninori Morimoto 		dpcm_fe_dai_cleanup(fe_substream);
272201d7584cSLiam Girdwood 
272301d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
272401d7584cSLiam Girdwood 	dpcm_path_put(&list);
2725cae06eb9SKuninori Morimoto open_end:
2726b7898396STakashi Iwai 	snd_soc_dpcm_mutex_unlock(fe);
272701d7584cSLiam Girdwood 	return ret;
272801d7584cSLiam Girdwood }
272901d7584cSLiam Girdwood 
27307fc6bebdSKuninori Morimoto static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
27317fc6bebdSKuninori Morimoto 				    int *playback, int *capture)
2732ddee627cSLiam Girdwood {
273319bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
27342e5894d7SBenoit Cousson 	int i;
2735ddee627cSLiam Girdwood 
27363989ade2SKuninori Morimoto 	if (rtd->dai_link->dynamic && rtd->dai_link->num_cpus > 1) {
27379b5db059SStephan Gerhold 		dev_err(rtd->dev,
2738b73287f0SPierre-Louis Bossart 			"DPCM doesn't support Multi CPU for Front-Ends yet\n");
27399b5db059SStephan Gerhold 		return -EINVAL;
27409b5db059SStephan Gerhold 	}
27419b5db059SStephan Gerhold 
2742b73287f0SPierre-Louis Bossart 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2743940a1f43SKuninori Morimoto 		int stream;
2744940a1f43SKuninori Morimoto 
2745b73287f0SPierre-Louis Bossart 		if (rtd->dai_link->dpcm_playback) {
2746b73287f0SPierre-Louis Bossart 			stream = SNDRV_PCM_STREAM_PLAYBACK;
2747b73287f0SPierre-Louis Bossart 
27484f872154SPierre-Louis Bossart 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
27494f872154SPierre-Louis Bossart 				if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
27507fc6bebdSKuninori Morimoto 					*playback = 1;
27514f872154SPierre-Louis Bossart 					break;
27524f872154SPierre-Louis Bossart 				}
27534f872154SPierre-Louis Bossart 			}
27547fc6bebdSKuninori Morimoto 			if (!*playback) {
2755b73287f0SPierre-Louis Bossart 				dev_err(rtd->card->dev,
27564f872154SPierre-Louis Bossart 					"No CPU DAIs support playback for stream %s\n",
2757b73287f0SPierre-Louis Bossart 					rtd->dai_link->stream_name);
2758b73287f0SPierre-Louis Bossart 				return -EINVAL;
2759b73287f0SPierre-Louis Bossart 			}
2760b73287f0SPierre-Louis Bossart 		}
2761b73287f0SPierre-Louis Bossart 		if (rtd->dai_link->dpcm_capture) {
2762b73287f0SPierre-Louis Bossart 			stream = SNDRV_PCM_STREAM_CAPTURE;
2763b73287f0SPierre-Louis Bossart 
27644f872154SPierre-Louis Bossart 			for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
27654f872154SPierre-Louis Bossart 				if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
27667fc6bebdSKuninori Morimoto 					*capture = 1;
27674f872154SPierre-Louis Bossart 					break;
27684f872154SPierre-Louis Bossart 				}
27694f872154SPierre-Louis Bossart 			}
27704f872154SPierre-Louis Bossart 
27717fc6bebdSKuninori Morimoto 			if (!*capture) {
2772b73287f0SPierre-Louis Bossart 				dev_err(rtd->card->dev,
27734f872154SPierre-Louis Bossart 					"No CPU DAIs support capture for stream %s\n",
2774b73287f0SPierre-Louis Bossart 					rtd->dai_link->stream_name);
2775b73287f0SPierre-Louis Bossart 				return -EINVAL;
2776b73287f0SPierre-Louis Bossart 			}
2777b73287f0SPierre-Louis Bossart 		}
277801d7584cSLiam Girdwood 	} else {
2779940a1f43SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
2780940a1f43SKuninori Morimoto 
2781a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
27827ddc7f91SKuninori Morimoto 		int cpu_capture = rtd->dai_link->c2c_params ?
2783a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
27847ddc7f91SKuninori Morimoto 		int cpu_playback = rtd->dai_link->c2c_params ?
2785a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2786a342031cSJerome Brunet 
2787a4be4187SKuninori Morimoto 		for_each_rtd_codec_dais(rtd, i, codec_dai) {
27883989ade2SKuninori Morimoto 			if (rtd->dai_link->num_cpus == 1) {
2789c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, 0);
27903989ade2SKuninori Morimoto 			} else if (rtd->dai_link->num_cpus == rtd->dai_link->num_codecs) {
2791c2233a26SKuninori Morimoto 				cpu_dai = asoc_rtd_to_cpu(rtd, i);
279219bdcc7aSShreyas NC 			} else {
279319bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
279419bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
279519bdcc7aSShreyas NC 				return -EINVAL;
279619bdcc7aSShreyas NC 			}
279719bdcc7aSShreyas NC 
2798467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2799a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
28007fc6bebdSKuninori Morimoto 				*playback = 1;
2801467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2802a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
28037fc6bebdSKuninori Morimoto 				*capture = 1;
280401d7584cSLiam Girdwood 		}
28052e5894d7SBenoit Cousson 	}
28062e5894d7SBenoit Cousson 
2807d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
28087fc6bebdSKuninori Morimoto 		*playback = 1;
28097fc6bebdSKuninori Morimoto 		*capture = 0;
2810d6bead02SFabio Estevam 	}
2811d6bead02SFabio Estevam 
2812d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
28137fc6bebdSKuninori Morimoto 		*playback = 0;
28147fc6bebdSKuninori Morimoto 		*capture = 1;
2815d6bead02SFabio Estevam 	}
2816d6bead02SFabio Estevam 
2817*092830cfSKuninori Morimoto 	if (!*playback && !*capture) {
2818*092830cfSKuninori Morimoto 		dev_err(rtd->dev, "substream %s has no playback, no capture\n",
2819*092830cfSKuninori Morimoto 			rtd->dai_link->stream_name);
2820*092830cfSKuninori Morimoto 
2821*092830cfSKuninori Morimoto 		return -EINVAL;
2822*092830cfSKuninori Morimoto 	}
2823*092830cfSKuninori Morimoto 
28247fc6bebdSKuninori Morimoto 	return 0;
28257fc6bebdSKuninori Morimoto }
28267fc6bebdSKuninori Morimoto 
28272b39123bSKuninori Morimoto static int soc_create_pcm(struct snd_pcm **pcm,
28282b39123bSKuninori Morimoto 			  struct snd_soc_pcm_runtime *rtd,
28292b39123bSKuninori Morimoto 			  int playback, int capture, int num)
28307fc6bebdSKuninori Morimoto {
28317fc6bebdSKuninori Morimoto 	char new_name[64];
28322b39123bSKuninori Morimoto 	int ret;
28337fc6bebdSKuninori Morimoto 
283401d7584cSLiam Girdwood 	/* create the PCM */
28357ddc7f91SKuninori Morimoto 	if (rtd->dai_link->c2c_params) {
2836a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2837a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2838a342031cSJerome Brunet 
2839a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
28402b39123bSKuninori Morimoto 					   playback, capture, pcm);
2841a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
284201d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
284301d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
284401d7584cSLiam Girdwood 
284501d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
28462b39123bSKuninori Morimoto 				playback, capture, pcm);
284701d7584cSLiam Girdwood 	} else {
284801d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
284901d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
285001d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
285101d7584cSLiam Girdwood 		else
285201d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
28532e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
28546fb8944cSKuninori Morimoto 				soc_codec_dai_name(rtd), num);
285501d7584cSLiam Girdwood 
285601d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
28572b39123bSKuninori Morimoto 			capture, pcm);
285801d7584cSLiam Girdwood 	}
2859ddee627cSLiam Girdwood 	if (ret < 0) {
2860799827a4SPierre-Louis Bossart 		dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2861799827a4SPierre-Louis Bossart 			new_name, rtd->dai_link->name, ret);
2862ddee627cSLiam Girdwood 		return ret;
2863ddee627cSLiam Girdwood 	}
2864103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2865ddee627cSLiam Girdwood 
28662b39123bSKuninori Morimoto 	return 0;
28672b39123bSKuninori Morimoto }
28682b39123bSKuninori Morimoto 
28692b39123bSKuninori Morimoto /* create a new pcm */
28702b39123bSKuninori Morimoto int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
28712b39123bSKuninori Morimoto {
28722b39123bSKuninori Morimoto 	struct snd_soc_component *component;
28732b39123bSKuninori Morimoto 	struct snd_pcm *pcm;
28742b39123bSKuninori Morimoto 	int ret = 0, playback = 0, capture = 0;
28752b39123bSKuninori Morimoto 	int i;
28762b39123bSKuninori Morimoto 
28772b39123bSKuninori Morimoto 	ret = soc_get_playback_capture(rtd, &playback, &capture);
28782b39123bSKuninori Morimoto 	if (ret < 0)
28792b39123bSKuninori Morimoto 		return ret;
28802b39123bSKuninori Morimoto 
28812b39123bSKuninori Morimoto 	ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
28822b39123bSKuninori Morimoto 	if (ret < 0)
28832b39123bSKuninori Morimoto 		return ret;
28842b39123bSKuninori Morimoto 
2885ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
288610d5d8cbSKuninori Morimoto 	/*
288710d5d8cbSKuninori Morimoto 	 * Currently nothing to do for c2c links
288810d5d8cbSKuninori Morimoto 	 * Since c2c links are internal nodes in the DAPM graph and
288910d5d8cbSKuninori Morimoto 	 * don't interface with the outside world or application layer
289010d5d8cbSKuninori Morimoto 	 * we don't have to do any special handling on close.
289110d5d8cbSKuninori Morimoto 	 */
28927ddc7f91SKuninori Morimoto 	if (!rtd->dai_link->c2c_params)
289383f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2894ddee627cSLiam Girdwood 
2895ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2896e04e7b8cSKuninori Morimoto 	pcm->nonatomic = rtd->dai_link->nonatomic;
2897ddee627cSLiam Girdwood 	pcm->private_data = rtd;
28984d45d944SKuninori Morimoto 	pcm->no_device_suspend = true;
289901d7584cSLiam Girdwood 
29007ddc7f91SKuninori Morimoto 	if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
290101d7584cSLiam Girdwood 		if (playback)
290201d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
290301d7584cSLiam Girdwood 		if (capture)
290401d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
290501d7584cSLiam Girdwood 		goto out;
290601d7584cSLiam Girdwood 	}
290701d7584cSLiam Girdwood 
290801d7584cSLiam Girdwood 	/* ASoC PCM operations */
290901d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
291001d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
291101d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
291201d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
291301d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
291401d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
291501d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
291601d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
291701d7584cSLiam Girdwood 	} else {
291801d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
291901d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
292001d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
292101d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
292201d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
292301d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
292401d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
292501d7584cSLiam Girdwood 	}
292601d7584cSLiam Girdwood 
2927613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
29282b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
2929b8135864SKuninori Morimoto 
29303b1c952cSTakashi Iwai 		if (drv->ioctl)
29313b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
29321e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
29331e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2934e9067bb5SKuninori Morimoto 		if (drv->copy_user)
293582d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2936e9067bb5SKuninori Morimoto 		if (drv->page)
29379c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
2938e9067bb5SKuninori Morimoto 		if (drv->mmap)
2939205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
29408bdfc045SShengjiu Wang 		if (drv->ack)
29418bdfc045SShengjiu Wang 			rtd->ops.ack            = snd_soc_pcm_component_ack;
2942b8135864SKuninori Morimoto 	}
2943b8135864SKuninori Morimoto 
2944ddee627cSLiam Girdwood 	if (playback)
294501d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2946ddee627cSLiam Girdwood 
2947ddee627cSLiam Girdwood 	if (capture)
294801d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2949ddee627cSLiam Girdwood 
2950b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
295160adbd8fSKuninori Morimoto 	if (ret < 0)
2952ddee627cSLiam Girdwood 		return ret;
295301d7584cSLiam Girdwood out:
29541d5cd525SPierre-Louis Bossart 	dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
29556fb8944cSKuninori Morimoto 		soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2956ddee627cSLiam Girdwood 	return ret;
2957ddee627cSLiam Girdwood }
295801d7584cSLiam Girdwood 
295901d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
296001d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
296101d7584cSLiam Girdwood {
296201d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
296301d7584cSLiam Girdwood 		return 1;
296401d7584cSLiam Girdwood 	return 0;
296501d7584cSLiam Girdwood }
296601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
296701d7584cSLiam Girdwood 
296801d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
296901d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
297001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
297101d7584cSLiam Girdwood {
297201d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
297301d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
297401d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
297501d7584cSLiam Girdwood 		return 1;
297601d7584cSLiam Girdwood 	return 0;
297701d7584cSLiam Girdwood }
297801d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
297901d7584cSLiam Girdwood 
298001d7584cSLiam Girdwood /* get the substream for this BE */
298101d7584cSLiam Girdwood struct snd_pcm_substream *
298201d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
298301d7584cSLiam Girdwood {
298401d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
298501d7584cSLiam Girdwood }
298601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
298701d7584cSLiam Girdwood 
2988085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2989085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
2990085d22beSKuninori Morimoto 				    int stream,
2991085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
2992085d22beSKuninori Morimoto 				    int num_states)
299301d7584cSLiam Girdwood {
299401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
299501d7584cSLiam Girdwood 	int state;
2996a9764869SKaiChieh Chuang 	int ret = 1;
2997085d22beSKuninori Morimoto 	int i;
299801d7584cSLiam Girdwood 
2999d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
300001d7584cSLiam Girdwood 
300101d7584cSLiam Girdwood 		if (dpcm->fe == fe)
300201d7584cSLiam Girdwood 			continue;
300301d7584cSLiam Girdwood 
300401d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
3005085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
3006085d22beSKuninori Morimoto 			if (state == states[i]) {
3007a9764869SKaiChieh Chuang 				ret = 0;
3008a9764869SKaiChieh Chuang 				break;
300901d7584cSLiam Girdwood 			}
3010a9764869SKaiChieh Chuang 		}
3011085d22beSKuninori Morimoto 	}
301201d7584cSLiam Girdwood 
3013085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
3014a9764869SKaiChieh Chuang 	return ret;
301501d7584cSLiam Girdwood }
3016085d22beSKuninori Morimoto 
3017085d22beSKuninori Morimoto /*
3018085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3019085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3020085d22beSKuninori Morimoto  */
3021085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3022085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3023085d22beSKuninori Morimoto {
3024085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3025085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3026085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3027085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3028085d22beSKuninori Morimoto 	};
3029085d22beSKuninori Morimoto 
3030085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3031085d22beSKuninori Morimoto }
303201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
303301d7584cSLiam Girdwood 
303401d7584cSLiam Girdwood /*
303501d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
303601d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
303701d7584cSLiam Girdwood  */
303801d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
303901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
304001d7584cSLiam Girdwood {
3041085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3042085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3043085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3044085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3045085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3046085d22beSKuninori Morimoto 	};
304701d7584cSLiam Girdwood 
3048085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
304901d7584cSLiam Girdwood }
305001d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3051