xref: /openbmc/linux/sound/soc/soc-pcm.c (revision 25c2f5156dd57f03aee2de079248c23a56222c92)
1ed517582SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2ed517582SKuninori Morimoto //
3ed517582SKuninori Morimoto // soc-pcm.c  --  ALSA SoC PCM
4ed517582SKuninori Morimoto //
5ed517582SKuninori Morimoto // Copyright 2005 Wolfson Microelectronics PLC.
6ed517582SKuninori Morimoto // Copyright 2005 Openedhand Ltd.
7ed517582SKuninori Morimoto // Copyright (C) 2010 Slimlogic Ltd.
8ed517582SKuninori Morimoto // Copyright (C) 2010 Texas Instruments Inc.
9ed517582SKuninori Morimoto //
10ed517582SKuninori Morimoto // Authors: Liam Girdwood <lrg@ti.com>
11ed517582SKuninori Morimoto //          Mark Brown <broonie@opensource.wolfsonmicro.com>
12ddee627cSLiam Girdwood 
13ddee627cSLiam Girdwood #include <linux/kernel.h>
14ddee627cSLiam Girdwood #include <linux/init.h>
15ddee627cSLiam Girdwood #include <linux/delay.h>
16988e8cc4SNicolin Chen #include <linux/pinctrl/consumer.h>
17d6652ef8SMark Brown #include <linux/pm_runtime.h>
18ddee627cSLiam Girdwood #include <linux/slab.h>
19ddee627cSLiam Girdwood #include <linux/workqueue.h>
2001d7584cSLiam Girdwood #include <linux/export.h>
21f86dcef8SLiam Girdwood #include <linux/debugfs.h>
22ddee627cSLiam Girdwood #include <sound/core.h>
23ddee627cSLiam Girdwood #include <sound/pcm.h>
24ddee627cSLiam Girdwood #include <sound/pcm_params.h>
25ddee627cSLiam Girdwood #include <sound/soc.h>
2601d7584cSLiam Girdwood #include <sound/soc-dpcm.h>
27ddee627cSLiam Girdwood #include <sound/initval.h>
28ddee627cSLiam Girdwood 
2901d7584cSLiam Girdwood #define DPCM_MAX_BE_USERS	8
3001d7584cSLiam Girdwood 
31c3212829SKuninori Morimoto #ifdef CONFIG_DEBUG_FS
32c3212829SKuninori Morimoto static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
33c3212829SKuninori Morimoto {
34c3212829SKuninori Morimoto 	switch (state) {
35c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_NEW:
36c3212829SKuninori Morimoto 		return "new";
37c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_OPEN:
38c3212829SKuninori Morimoto 		return "open";
39c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_PARAMS:
40c3212829SKuninori Morimoto 		return "hw_params";
41c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PREPARE:
42c3212829SKuninori Morimoto 		return "prepare";
43c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_START:
44c3212829SKuninori Morimoto 		return "start";
45c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_STOP:
46c3212829SKuninori Morimoto 		return "stop";
47c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_SUSPEND:
48c3212829SKuninori Morimoto 		return "suspend";
49c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_PAUSED:
50c3212829SKuninori Morimoto 		return "paused";
51c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_HW_FREE:
52c3212829SKuninori Morimoto 		return "hw_free";
53c3212829SKuninori Morimoto 	case SND_SOC_DPCM_STATE_CLOSE:
54c3212829SKuninori Morimoto 		return "close";
55c3212829SKuninori Morimoto 	}
56c3212829SKuninori Morimoto 
57c3212829SKuninori Morimoto 	return "unknown";
58c3212829SKuninori Morimoto }
59c3212829SKuninori Morimoto 
60c3212829SKuninori Morimoto static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
61c3212829SKuninori Morimoto 			       int stream, char *buf, size_t size)
62c3212829SKuninori Morimoto {
63c3212829SKuninori Morimoto 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
64c3212829SKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
65c3212829SKuninori Morimoto 	ssize_t offset = 0;
66c3212829SKuninori Morimoto 	unsigned long flags;
67c3212829SKuninori Morimoto 
68c3212829SKuninori Morimoto 	/* FE state */
69c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset,
70c3212829SKuninori Morimoto 			   "[%s - %s]\n", fe->dai_link->name,
71c3212829SKuninori Morimoto 			   stream ? "Capture" : "Playback");
72c3212829SKuninori Morimoto 
73c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
74c3212829SKuninori Morimoto 			   dpcm_state_string(fe->dpcm[stream].state));
75c3212829SKuninori Morimoto 
76c3212829SKuninori Morimoto 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
77c3212829SKuninori Morimoto 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
78c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
79c3212829SKuninori Morimoto 				   "Hardware Params: "
80c3212829SKuninori Morimoto 				   "Format = %s, Channels = %d, Rate = %d\n",
81c3212829SKuninori Morimoto 				   snd_pcm_format_name(params_format(params)),
82c3212829SKuninori Morimoto 				   params_channels(params),
83c3212829SKuninori Morimoto 				   params_rate(params));
84c3212829SKuninori Morimoto 
85c3212829SKuninori Morimoto 	/* BEs state */
86c3212829SKuninori Morimoto 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
87c3212829SKuninori Morimoto 
88c3212829SKuninori Morimoto 	if (list_empty(&fe->dpcm[stream].be_clients)) {
89c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
90c3212829SKuninori Morimoto 				   " No active DSP links\n");
91c3212829SKuninori Morimoto 		goto out;
92c3212829SKuninori Morimoto 	}
93c3212829SKuninori Morimoto 
94c3212829SKuninori Morimoto 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
95c3212829SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
96c3212829SKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
97c3212829SKuninori Morimoto 		params = &dpcm->hw_params;
98c3212829SKuninori Morimoto 
99c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
100c3212829SKuninori Morimoto 				   "- %s\n", be->dai_link->name);
101c3212829SKuninori Morimoto 
102c3212829SKuninori Morimoto 		offset += snprintf(buf + offset, size - offset,
103c3212829SKuninori Morimoto 				   "   State: %s\n",
104c3212829SKuninori Morimoto 				   dpcm_state_string(be->dpcm[stream].state));
105c3212829SKuninori Morimoto 
106c3212829SKuninori Morimoto 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
107c3212829SKuninori Morimoto 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
108c3212829SKuninori Morimoto 			offset += snprintf(buf + offset, size - offset,
109c3212829SKuninori Morimoto 					   "   Hardware Params: "
110c3212829SKuninori Morimoto 					   "Format = %s, Channels = %d, Rate = %d\n",
111c3212829SKuninori Morimoto 					   snd_pcm_format_name(params_format(params)),
112c3212829SKuninori Morimoto 					   params_channels(params),
113c3212829SKuninori Morimoto 					   params_rate(params));
114c3212829SKuninori Morimoto 	}
115c3212829SKuninori Morimoto 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116c3212829SKuninori Morimoto out:
117c3212829SKuninori Morimoto 	return offset;
118c3212829SKuninori Morimoto }
119c3212829SKuninori Morimoto 
120c3212829SKuninori Morimoto static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
121c3212829SKuninori Morimoto 				    size_t count, loff_t *ppos)
122c3212829SKuninori Morimoto {
123c3212829SKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = file->private_data;
124c3212829SKuninori Morimoto 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
125c3212829SKuninori Morimoto 	int stream;
126c3212829SKuninori Morimoto 	char *buf;
127c3212829SKuninori Morimoto 
1286e1276a5SBard Liao 	if (fe->num_cpus > 1) {
1296e1276a5SBard Liao 		dev_err(fe->dev,
1306e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
1316e1276a5SBard Liao 		return -EINVAL;
1326e1276a5SBard Liao 	}
1336e1276a5SBard Liao 
134c3212829SKuninori Morimoto 	buf = kmalloc(out_count, GFP_KERNEL);
135c3212829SKuninori Morimoto 	if (!buf)
136c3212829SKuninori Morimoto 		return -ENOMEM;
137c3212829SKuninori Morimoto 
138c3212829SKuninori Morimoto 	for_each_pcm_streams(stream)
139c3212829SKuninori Morimoto 		if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
140c3212829SKuninori Morimoto 			offset += dpcm_show_state(fe, stream,
141c3212829SKuninori Morimoto 						  buf + offset,
142c3212829SKuninori Morimoto 						  out_count - offset);
143c3212829SKuninori Morimoto 
144c3212829SKuninori Morimoto 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
145c3212829SKuninori Morimoto 
146c3212829SKuninori Morimoto 	kfree(buf);
147c3212829SKuninori Morimoto 	return ret;
148c3212829SKuninori Morimoto }
149c3212829SKuninori Morimoto 
150c3212829SKuninori Morimoto static const struct file_operations dpcm_state_fops = {
151c3212829SKuninori Morimoto 	.open = simple_open,
152c3212829SKuninori Morimoto 	.read = dpcm_state_read_file,
153c3212829SKuninori Morimoto 	.llseek = default_llseek,
154c3212829SKuninori Morimoto };
155c3212829SKuninori Morimoto 
156c3212829SKuninori Morimoto void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
157c3212829SKuninori Morimoto {
158c3212829SKuninori Morimoto 	if (!rtd->dai_link)
159c3212829SKuninori Morimoto 		return;
160c3212829SKuninori Morimoto 
161c3212829SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
162c3212829SKuninori Morimoto 		return;
163c3212829SKuninori Morimoto 
164c3212829SKuninori Morimoto 	if (!rtd->card->debugfs_card_root)
165c3212829SKuninori Morimoto 		return;
166c3212829SKuninori Morimoto 
167c3212829SKuninori Morimoto 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
168c3212829SKuninori Morimoto 						    rtd->card->debugfs_card_root);
169c3212829SKuninori Morimoto 
170c3212829SKuninori Morimoto 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
171c3212829SKuninori Morimoto 			    rtd, &dpcm_state_fops);
172c3212829SKuninori Morimoto }
173154dae87SKuninori Morimoto 
174154dae87SKuninori Morimoto static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
175154dae87SKuninori Morimoto {
176154dae87SKuninori Morimoto 	char *name;
177154dae87SKuninori Morimoto 
178154dae87SKuninori Morimoto 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
179154dae87SKuninori Morimoto 			 stream ? "capture" : "playback");
180154dae87SKuninori Morimoto 	if (name) {
181154dae87SKuninori Morimoto 		dpcm->debugfs_state = debugfs_create_dir(
182154dae87SKuninori Morimoto 			name, dpcm->fe->debugfs_dpcm_root);
183154dae87SKuninori Morimoto 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
184154dae87SKuninori Morimoto 				   &dpcm->state);
185154dae87SKuninori Morimoto 		kfree(name);
186154dae87SKuninori Morimoto 	}
187154dae87SKuninori Morimoto }
188154dae87SKuninori Morimoto 
189154dae87SKuninori Morimoto static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
190154dae87SKuninori Morimoto {
191154dae87SKuninori Morimoto 	debugfs_remove_recursive(dpcm->debugfs_state);
192154dae87SKuninori Morimoto }
193154dae87SKuninori Morimoto 
194154dae87SKuninori Morimoto #else
195154dae87SKuninori Morimoto static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
196154dae87SKuninori Morimoto 					     int stream)
197154dae87SKuninori Morimoto {
198154dae87SKuninori Morimoto }
199154dae87SKuninori Morimoto 
200154dae87SKuninori Morimoto static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
201154dae87SKuninori Morimoto {
202154dae87SKuninori Morimoto }
203c3212829SKuninori Morimoto #endif
204c3212829SKuninori Morimoto 
205f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
206f183f927SKuninori Morimoto 			   struct snd_pcm_substream *substream)
207f183f927SKuninori Morimoto {
208f183f927SKuninori Morimoto 	if (rtd->dai_link->ops &&
209f183f927SKuninori Morimoto 	    rtd->dai_link->ops->startup)
210f183f927SKuninori Morimoto 		return rtd->dai_link->ops->startup(substream);
211f183f927SKuninori Morimoto 	return 0;
212f183f927SKuninori Morimoto }
213f183f927SKuninori Morimoto 
2140be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
2150be429f9SKuninori Morimoto 			     struct snd_pcm_substream *substream)
2160be429f9SKuninori Morimoto {
2170be429f9SKuninori Morimoto 	if (rtd->dai_link->ops &&
2180be429f9SKuninori Morimoto 	    rtd->dai_link->ops->shutdown)
2190be429f9SKuninori Morimoto 		rtd->dai_link->ops->shutdown(substream);
2200be429f9SKuninori Morimoto }
2210be429f9SKuninori Morimoto 
22244c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
22344c1a75bSKuninori Morimoto 			   struct snd_pcm_substream *substream)
22444c1a75bSKuninori Morimoto {
22544c1a75bSKuninori Morimoto 	if (rtd->dai_link->ops &&
22644c1a75bSKuninori Morimoto 	    rtd->dai_link->ops->prepare)
22744c1a75bSKuninori Morimoto 		return rtd->dai_link->ops->prepare(substream);
22844c1a75bSKuninori Morimoto 	return 0;
22944c1a75bSKuninori Morimoto }
23044c1a75bSKuninori Morimoto 
231de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
232de9ad990SKuninori Morimoto 			     struct snd_pcm_substream *substream,
233de9ad990SKuninori Morimoto 			     struct snd_pcm_hw_params *params)
234de9ad990SKuninori Morimoto {
235de9ad990SKuninori Morimoto 	if (rtd->dai_link->ops &&
236de9ad990SKuninori Morimoto 	    rtd->dai_link->ops->hw_params)
237de9ad990SKuninori Morimoto 		return rtd->dai_link->ops->hw_params(substream, params);
238de9ad990SKuninori Morimoto 	return 0;
239de9ad990SKuninori Morimoto }
240de9ad990SKuninori Morimoto 
24149f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
24249f020e5SKuninori Morimoto 			    struct snd_pcm_substream *substream)
24349f020e5SKuninori Morimoto {
24449f020e5SKuninori Morimoto 	if (rtd->dai_link->ops &&
24549f020e5SKuninori Morimoto 	    rtd->dai_link->ops->hw_free)
24649f020e5SKuninori Morimoto 		rtd->dai_link->ops->hw_free(substream);
24749f020e5SKuninori Morimoto }
24849f020e5SKuninori Morimoto 
249ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
250ad2bf9f2SKuninori Morimoto 			   struct snd_pcm_substream *substream,
251ad2bf9f2SKuninori Morimoto 			   int cmd)
252ad2bf9f2SKuninori Morimoto {
253ad2bf9f2SKuninori Morimoto 	if (rtd->dai_link->ops &&
254ad2bf9f2SKuninori Morimoto 	    rtd->dai_link->ops->trigger)
255ad2bf9f2SKuninori Morimoto 		return rtd->dai_link->ops->trigger(substream, cmd);
256ad2bf9f2SKuninori Morimoto 	return 0;
257ad2bf9f2SKuninori Morimoto }
258ad2bf9f2SKuninori Morimoto 
2597a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
2607a5aaba4SKuninori Morimoto 				   int stream, int action)
2617a5aaba4SKuninori Morimoto {
26219bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
2637a5aaba4SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
2647a5aaba4SKuninori Morimoto 	int i;
2657a5aaba4SKuninori Morimoto 
2667a5aaba4SKuninori Morimoto 	lockdep_assert_held(&rtd->card->pcm_mutex);
2677a5aaba4SKuninori Morimoto 
26819bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
2690f6011fdSKuninori Morimoto 		cpu_dai->stream_active[stream] += action;
27019bdcc7aSShreyas NC 
2717a5aaba4SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2720f6011fdSKuninori Morimoto 		codec_dai->stream_active[stream] += action;
2737a5aaba4SKuninori Morimoto 
27419bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
2757a5aaba4SKuninori Morimoto 		cpu_dai->active += action;
2767a5aaba4SKuninori Morimoto 		cpu_dai->component->active += action;
27719bdcc7aSShreyas NC 	}
2787a5aaba4SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
2797a5aaba4SKuninori Morimoto 		codec_dai->active += action;
2807a5aaba4SKuninori Morimoto 		codec_dai->component->active += action;
2817a5aaba4SKuninori Morimoto 	}
2827a5aaba4SKuninori Morimoto }
2837a5aaba4SKuninori Morimoto 
28490996f43SLars-Peter Clausen /**
28524894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
28624894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
28724894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
28824894b76SLars-Peter Clausen  *
28924894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
29024894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
29124894b76SLars-Peter Clausen  *
29272b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
29324894b76SLars-Peter Clausen  */
29424894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
29524894b76SLars-Peter Clausen {
2967a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, 1);
29724894b76SLars-Peter Clausen }
29824894b76SLars-Peter Clausen 
29924894b76SLars-Peter Clausen /**
30024894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
30124894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
30224894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
30324894b76SLars-Peter Clausen  *
30424894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
30524894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
30624894b76SLars-Peter Clausen  *
30772b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
30824894b76SLars-Peter Clausen  */
30924894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
31024894b76SLars-Peter Clausen {
3117a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, -1);
31224894b76SLars-Peter Clausen }
31324894b76SLars-Peter Clausen 
31424894b76SLars-Peter Clausen /**
315208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
316208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
317208a1589SLars-Peter Clausen  *
318208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
319208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
320208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
321208a1589SLars-Peter Clausen  * from having the delay.
322208a1589SLars-Peter Clausen  */
323208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
324208a1589SLars-Peter Clausen {
325fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
3262e5894d7SBenoit Cousson 	bool ignore = true;
327613fb500SKuninori Morimoto 	int i;
3282e5894d7SBenoit Cousson 
329208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
330208a1589SLars-Peter Clausen 		return true;
331208a1589SLars-Peter Clausen 
332613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
33372c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
334fbb16563SKuninori Morimoto 
335fbb16563SKuninori Morimoto 	return ignore;
336208a1589SLars-Peter Clausen }
337208a1589SLars-Peter Clausen 
338208a1589SLars-Peter Clausen /**
33990996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
34090996f43SLars-Peter Clausen  * @substream: the pcm substream
34190996f43SLars-Peter Clausen  * @hw: the hardware parameters
34290996f43SLars-Peter Clausen  *
34390996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
34490996f43SLars-Peter Clausen  */
34590996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
34690996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
34790996f43SLars-Peter Clausen {
34890996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
34990996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
35090996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
35190996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
35290996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
35390996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
35490996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
35590996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
35690996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
35790996f43SLars-Peter Clausen 	return 0;
35890996f43SLars-Peter Clausen }
35990996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
36090996f43SLars-Peter Clausen 
36101d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
36223607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
36301d7584cSLiam Girdwood 	int event)
36401d7584cSLiam Girdwood {
36501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
36601d7584cSLiam Girdwood 
3678d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
36801d7584cSLiam Girdwood 
36901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
37001d7584cSLiam Girdwood 
371103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
37201d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
37301d7584cSLiam Girdwood 
374b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
375b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
376b1cd2e34SBanajit Goswami 			continue;
377b1cd2e34SBanajit Goswami 
37801d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
37901d7584cSLiam Girdwood 	}
38001d7584cSLiam Girdwood 
38101d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
38201d7584cSLiam Girdwood 
38301d7584cSLiam Girdwood 	return 0;
38401d7584cSLiam Girdwood }
38501d7584cSLiam Girdwood 
38617841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
38717841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
388ddee627cSLiam Girdwood {
389ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
390ddee627cSLiam Girdwood 	int ret;
391ddee627cSLiam Girdwood 
3923635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
3933635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
3943635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
3953635bf09SNicolin Chen 				soc_dai->rate);
396ddee627cSLiam Girdwood 
3974dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
398ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
3994dcdd43bSLars-Peter Clausen 						soc_dai->rate);
400ddee627cSLiam Girdwood 		if (ret < 0) {
40117841020SDong Aisheng 			dev_err(soc_dai->dev,
4023635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
403103d84a3SLiam Girdwood 				ret);
404ddee627cSLiam Girdwood 			return ret;
405ddee627cSLiam Girdwood 		}
4063635bf09SNicolin Chen 	}
4073635bf09SNicolin Chen 
4083635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
4093635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
4103635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
4113635bf09SNicolin Chen 				soc_dai->channels);
4123635bf09SNicolin Chen 
4134dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4143635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
4153635bf09SNicolin Chen 						soc_dai->channels);
4163635bf09SNicolin Chen 		if (ret < 0) {
4173635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4183635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
4193635bf09SNicolin Chen 				ret);
4203635bf09SNicolin Chen 			return ret;
4213635bf09SNicolin Chen 		}
4223635bf09SNicolin Chen 	}
4233635bf09SNicolin Chen 
4243635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
4253635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
4263635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
4273635bf09SNicolin Chen 				soc_dai->sample_bits);
4283635bf09SNicolin Chen 
4294dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
4303635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
4313635bf09SNicolin Chen 						soc_dai->sample_bits);
4323635bf09SNicolin Chen 		if (ret < 0) {
4333635bf09SNicolin Chen 			dev_err(soc_dai->dev,
4343635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
4353635bf09SNicolin Chen 				ret);
4363635bf09SNicolin Chen 			return ret;
4373635bf09SNicolin Chen 		}
4383635bf09SNicolin Chen 	}
439ddee627cSLiam Girdwood 
440ddee627cSLiam Girdwood 	return 0;
441ddee627cSLiam Girdwood }
442ddee627cSLiam Girdwood 
4433635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
4443635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
4453635bf09SNicolin Chen {
4463635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
44719bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
4480b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
4492e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
4503635bf09SNicolin Chen 
4513635bf09SNicolin Chen 	rate = params_rate(params);
4523635bf09SNicolin Chen 	channels = params_channels(params);
4533635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
4543635bf09SNicolin Chen 
4553635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
45619bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_rates;
45719bdcc7aSShreyas NC 
45819bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
45919bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_rates;
4602e5894d7SBenoit Cousson 
4610b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4620b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_rates;
4632e5894d7SBenoit Cousson 
46419bdcc7aSShreyas NC 	if (symmetry) {
46519bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
46619bdcc7aSShreyas NC 			if (cpu_dai->rate && cpu_dai->rate != rate) {
4673635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
4683635bf09SNicolin Chen 					cpu_dai->rate, rate);
4693635bf09SNicolin Chen 				return -EINVAL;
4703635bf09SNicolin Chen 			}
47119bdcc7aSShreyas NC 		}
47219bdcc7aSShreyas NC 	}
4733635bf09SNicolin Chen 
47419bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_channels;
47519bdcc7aSShreyas NC 
47619bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
47719bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_channels;
4782e5894d7SBenoit Cousson 
4790b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4800b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_channels;
4812e5894d7SBenoit Cousson 
48219bdcc7aSShreyas NC 	if (symmetry) {
48319bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
48419bdcc7aSShreyas NC 			if (cpu_dai->channels &&
48519bdcc7aSShreyas NC 			    cpu_dai->channels != channels) {
4863635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
4873635bf09SNicolin Chen 					cpu_dai->channels, channels);
4883635bf09SNicolin Chen 				return -EINVAL;
4893635bf09SNicolin Chen 			}
49019bdcc7aSShreyas NC 		}
49119bdcc7aSShreyas NC 	}
4923635bf09SNicolin Chen 
49319bdcc7aSShreyas NC 	symmetry = rtd->dai_link->symmetric_samplebits;
49419bdcc7aSShreyas NC 
49519bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
49619bdcc7aSShreyas NC 		symmetry |= cpu_dai->driver->symmetric_samplebits;
4972e5894d7SBenoit Cousson 
4980b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
4990b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_samplebits;
5002e5894d7SBenoit Cousson 
50119bdcc7aSShreyas NC 	if (symmetry) {
50219bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
50319bdcc7aSShreyas NC 			if (cpu_dai->sample_bits &&
50419bdcc7aSShreyas NC 			    cpu_dai->sample_bits != sample_bits) {
5053635bf09SNicolin Chen 				dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
5063635bf09SNicolin Chen 					cpu_dai->sample_bits, sample_bits);
5073635bf09SNicolin Chen 				return -EINVAL;
5083635bf09SNicolin Chen 			}
50919bdcc7aSShreyas NC 		}
51019bdcc7aSShreyas NC 	}
511ddee627cSLiam Girdwood 
512ddee627cSLiam Girdwood 	return 0;
513ddee627cSLiam Girdwood }
514ddee627cSLiam Girdwood 
51562e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
51662e5f676SLars-Peter Clausen {
51762e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
51862e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
5190b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
52019bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5212e5894d7SBenoit Cousson 	unsigned int symmetry, i;
52262e5f676SLars-Peter Clausen 
52319bdcc7aSShreyas NC 	symmetry = link->symmetric_rates ||
52419bdcc7aSShreyas NC 		link->symmetric_channels ||
52519bdcc7aSShreyas NC 		link->symmetric_samplebits;
52619bdcc7aSShreyas NC 
52719bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
52819bdcc7aSShreyas NC 		symmetry = symmetry ||
52919bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_rates ||
53019bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_channels ||
53119bdcc7aSShreyas NC 			cpu_dai->driver->symmetric_samplebits;
5322e5894d7SBenoit Cousson 
5330b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
5342e5894d7SBenoit Cousson 		symmetry = symmetry ||
5350b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_rates ||
5360b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_channels ||
5370b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_samplebits;
5382e5894d7SBenoit Cousson 
5392e5894d7SBenoit Cousson 	return symmetry;
54062e5f676SLars-Peter Clausen }
54162e5f676SLars-Peter Clausen 
5422e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
54358ba9b25SMark Brown {
5442e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
545c6068d3aSTakashi Iwai 	int ret;
54658ba9b25SMark Brown 
54758ba9b25SMark Brown 	if (!bits)
54858ba9b25SMark Brown 		return;
54958ba9b25SMark Brown 
5500e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
55158ba9b25SMark Brown 	if (ret != 0)
5520e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
5530e2a3751SLars-Peter Clausen 				 bits, ret);
55458ba9b25SMark Brown }
55558ba9b25SMark Brown 
556c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
557bd477c31SLars-Peter Clausen {
558c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
55919bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5602e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
56157be9206SKuninori Morimoto 	struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
56257be9206SKuninori Morimoto 	int stream = substream->stream;
5632e5894d7SBenoit Cousson 	int i;
56419bdcc7aSShreyas NC 	unsigned int bits = 0, cpu_bits = 0;
565c8dd1fecSBenoit Cousson 
5660b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
56757be9206SKuninori Morimoto 		pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
56857be9206SKuninori Morimoto 
56957be9206SKuninori Morimoto 		if (pcm_codec->sig_bits == 0) {
5702e5894d7SBenoit Cousson 			bits = 0;
5712e5894d7SBenoit Cousson 			break;
5722e5894d7SBenoit Cousson 		}
57357be9206SKuninori Morimoto 		bits = max(pcm_codec->sig_bits, bits);
5742e5894d7SBenoit Cousson 	}
57557be9206SKuninori Morimoto 
57619bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
57757be9206SKuninori Morimoto 		pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
57819bdcc7aSShreyas NC 
57919bdcc7aSShreyas NC 		if (pcm_cpu->sig_bits == 0) {
58019bdcc7aSShreyas NC 			cpu_bits = 0;
58119bdcc7aSShreyas NC 			break;
58219bdcc7aSShreyas NC 		}
58319bdcc7aSShreyas NC 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
58419bdcc7aSShreyas NC 	}
585c8dd1fecSBenoit Cousson 
5862e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
5872e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
588c8dd1fecSBenoit Cousson }
589c8dd1fecSBenoit Cousson 
5902e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
591bd477c31SLars-Peter Clausen {
5922e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
59378e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
5942e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
5950b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
59619bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
5972e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
5982e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
5992e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
60019bdcc7aSShreyas NC 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
6012e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
60219bdcc7aSShreyas NC 	unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
60319bdcc7aSShreyas NC 	unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
6042e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
605acf253c1SKuninori Morimoto 	int stream = substream->stream;
6062e5894d7SBenoit Cousson 	int i;
60778e45c99SLars-Peter Clausen 
60819bdcc7aSShreyas NC 	/* first calculate min/max only for CPUs in the DAI link */
60919bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
6100e9cf4c4SBard Liao 
6110e9cf4c4SBard Liao 		/*
6120e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream type.
6130e9cf4c4SBard Liao 		 * Otherwise, since the rate, channel, and format values will
6140e9cf4c4SBard Liao 		 * zero in that case, we would have no usable settings left,
6150e9cf4c4SBard Liao 		 * causing the resulting setup to fail.
6160e9cf4c4SBard Liao 		 * At least one CPU should match, otherwise we should have
6170e9cf4c4SBard Liao 		 * bailed out on a higher level, since there would be no
6180e9cf4c4SBard Liao 		 * CPU to support the transfer direction in that case.
6190e9cf4c4SBard Liao 		 */
6200e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai,
6210e9cf4c4SBard Liao 					      substream->stream))
6220e9cf4c4SBard Liao 			continue;
6230e9cf4c4SBard Liao 
62419bdcc7aSShreyas NC 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
62578e45c99SLars-Peter Clausen 
62619bdcc7aSShreyas NC 		cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
62719bdcc7aSShreyas NC 		cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
62819bdcc7aSShreyas NC 		cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
62919bdcc7aSShreyas NC 		cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
63019bdcc7aSShreyas NC 		formats &= cpu_stream->formats;
63119bdcc7aSShreyas NC 		cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
63219bdcc7aSShreyas NC 							cpu_rates);
63319bdcc7aSShreyas NC 	}
63419bdcc7aSShreyas NC 
63519bdcc7aSShreyas NC 	/* second calculate min/max only for CODECs in the DAI link */
6360b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
637cde79035SRicard Wanderlof 
638cde79035SRicard Wanderlof 		/*
639cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
640cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
641cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
642cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
643cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
644cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
645cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
646cde79035SRicard Wanderlof 		 */
647*25c2f515SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
648cde79035SRicard Wanderlof 			continue;
649cde79035SRicard Wanderlof 
650acf253c1SKuninori Morimoto 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
651acf253c1SKuninori Morimoto 
6522e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
6532e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
6542e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
6552e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
6562e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
6572e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
6582e5894d7SBenoit Cousson 	}
6592e5894d7SBenoit Cousson 
6602e5894d7SBenoit Cousson 	/*
6612e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
66219bdcc7aSShreyas NC 	 * connected to CPU DAI(s), use CPU DAI's directly and let
6632e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
6642e5894d7SBenoit Cousson 	 */
6652e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
66619bdcc7aSShreyas NC 		chan_min = cpu_chan_min;
66719bdcc7aSShreyas NC 		chan_max = cpu_chan_max;
6682e5894d7SBenoit Cousson 	}
6692e5894d7SBenoit Cousson 
67019bdcc7aSShreyas NC 	/* finally find a intersection between CODECs and CPUs */
67119bdcc7aSShreyas NC 	hw->channels_min = max(chan_min, cpu_chan_min);
67219bdcc7aSShreyas NC 	hw->channels_max = min(chan_max, cpu_chan_max);
6732e5894d7SBenoit Cousson 	if (hw->formats)
67419bdcc7aSShreyas NC 		hw->formats &= formats;
6752e5894d7SBenoit Cousson 	else
67619bdcc7aSShreyas NC 		hw->formats = formats;
67719bdcc7aSShreyas NC 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
67878e45c99SLars-Peter Clausen 
67978e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
68078e45c99SLars-Peter Clausen 
68119bdcc7aSShreyas NC 	hw->rate_min = max(hw->rate_min, cpu_rate_min);
6822e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
68319bdcc7aSShreyas NC 	hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
6842e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
685bd477c31SLars-Peter Clausen }
686bd477c31SLars-Peter Clausen 
687dd03907bSKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream)
688e7ecfdb7SKuninori Morimoto {
689e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
690d2aaa8d8SKai Vehmanen 	struct snd_soc_component *last = NULL;
691e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
692613fb500SKuninori Morimoto 	int i, ret = 0;
693e7ecfdb7SKuninori Morimoto 
694613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
695d2aaa8d8SKai Vehmanen 		last = component;
696d2aaa8d8SKai Vehmanen 
6974a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
6984a81e8f3SKuninori Morimoto 		if (ret < 0) {
699e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
700e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
701e7ecfdb7SKuninori Morimoto 				component->name);
702d2aaa8d8SKai Vehmanen 			break;
703e7ecfdb7SKuninori Morimoto 		}
704e7ecfdb7SKuninori Morimoto 
705ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
706e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
707d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
708e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
709e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
710e7ecfdb7SKuninori Morimoto 				component->name, ret);
711d2aaa8d8SKai Vehmanen 			break;
712e7ecfdb7SKuninori Morimoto 		}
713e7ecfdb7SKuninori Morimoto 	}
714dd03907bSKuninori Morimoto 
715d2aaa8d8SKai Vehmanen 	if (ret < 0) {
716d2aaa8d8SKai Vehmanen 		/* rollback on error */
717d2aaa8d8SKai Vehmanen 		for_each_rtd_components(rtd, i, component) {
718d2aaa8d8SKai Vehmanen 			if (component == last)
719d2aaa8d8SKai Vehmanen 				break;
720d2aaa8d8SKai Vehmanen 
721d2aaa8d8SKai Vehmanen 			snd_soc_component_close(component, substream);
722d2aaa8d8SKai Vehmanen 			snd_soc_component_module_put_when_close(component);
723d2aaa8d8SKai Vehmanen 		}
724d2aaa8d8SKai Vehmanen 	}
725d2aaa8d8SKai Vehmanen 
726d2aaa8d8SKai Vehmanen 	return ret;
727e7ecfdb7SKuninori Morimoto }
728e7ecfdb7SKuninori Morimoto 
729dd03907bSKuninori Morimoto static int soc_pcm_components_close(struct snd_pcm_substream *substream)
730244e2936SCharles Keepax {
731244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
732244e2936SCharles Keepax 	struct snd_soc_component *component;
733e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
734244e2936SCharles Keepax 
735613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
736e82ebffcSKuninori Morimoto 		r = snd_soc_component_close(component, substream);
737e82ebffcSKuninori Morimoto 		if (r < 0)
738e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
739e82ebffcSKuninori Morimoto 
7404a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
741244e2936SCharles Keepax 	}
742244e2936SCharles Keepax 
7433672beb8SKuninori Morimoto 	return ret;
744244e2936SCharles Keepax }
745244e2936SCharles Keepax 
74658ba9b25SMark Brown /*
74762c86d1dSKuninori Morimoto  * Called by ALSA when a PCM substream is closed. Private data can be
74862c86d1dSKuninori Morimoto  * freed here. The cpu DAI, codec DAI, machine and components are also
74962c86d1dSKuninori Morimoto  * shutdown.
75062c86d1dSKuninori Morimoto  */
75162c86d1dSKuninori Morimoto static int soc_pcm_close(struct snd_pcm_substream *substream)
75262c86d1dSKuninori Morimoto {
75362c86d1dSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
75462c86d1dSKuninori Morimoto 	struct snd_soc_component *component;
75519bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
75662c86d1dSKuninori Morimoto 	struct snd_soc_dai *codec_dai;
75762c86d1dSKuninori Morimoto 	int i;
75862c86d1dSKuninori Morimoto 
75962c86d1dSKuninori Morimoto 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
76062c86d1dSKuninori Morimoto 
76162c86d1dSKuninori Morimoto 	snd_soc_runtime_deactivate(rtd, substream->stream);
76262c86d1dSKuninori Morimoto 
76319bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
76462c86d1dSKuninori Morimoto 		snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
76562c86d1dSKuninori Morimoto 
76619bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
76762c86d1dSKuninori Morimoto 		snd_soc_dai_shutdown(cpu_dai, substream);
76862c86d1dSKuninori Morimoto 
76962c86d1dSKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
77062c86d1dSKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
77162c86d1dSKuninori Morimoto 
77262c86d1dSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
77362c86d1dSKuninori Morimoto 
77462c86d1dSKuninori Morimoto 	soc_pcm_components_close(substream);
77562c86d1dSKuninori Morimoto 
77662c86d1dSKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
77762c86d1dSKuninori Morimoto 
77862c86d1dSKuninori Morimoto 	mutex_unlock(&rtd->card->pcm_mutex);
77962c86d1dSKuninori Morimoto 
78062c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
78162c86d1dSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
78262c86d1dSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
78362c86d1dSKuninori Morimoto 	}
78462c86d1dSKuninori Morimoto 
78562c86d1dSKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
78662c86d1dSKuninori Morimoto 		if (!component->active)
78762c86d1dSKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
78862c86d1dSKuninori Morimoto 
78962c86d1dSKuninori Morimoto 	return 0;
79062c86d1dSKuninori Morimoto }
79162c86d1dSKuninori Morimoto 
79262c86d1dSKuninori Morimoto /*
793ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
794ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
795ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
796ddee627cSLiam Girdwood  */
797ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
798ddee627cSLiam Girdwood {
799ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
800ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
80190be711eSKuninori Morimoto 	struct snd_soc_component *component;
80219bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
8032e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
8042e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
80519bdcc7aSShreyas NC 	const char *cpu_dai_name = "multicpu";
806244e2936SCharles Keepax 	int i, ret = 0;
807ddee627cSLiam Girdwood 
80876c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
80976c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
81090be711eSKuninori Morimoto 
811613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
81290be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
813d6652ef8SMark Brown 
81472b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
815ddee627cSLiam Girdwood 
8165d9fa03eSKuninori Morimoto 	ret = soc_pcm_components_open(substream);
8175d9fa03eSKuninori Morimoto 	if (ret < 0)
8185d9fa03eSKuninori Morimoto 		goto component_err;
8195d9fa03eSKuninori Morimoto 
8205d9fa03eSKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
8215d9fa03eSKuninori Morimoto 	if (ret < 0) {
8225d9fa03eSKuninori Morimoto 		pr_err("ASoC: %s startup failed: %d\n",
8235d9fa03eSKuninori Morimoto 		       rtd->dai_link->name, ret);
824d2aaa8d8SKai Vehmanen 		goto rtd_startup_err;
8255d9fa03eSKuninori Morimoto 	}
8265d9fa03eSKuninori Morimoto 
827ddee627cSLiam Girdwood 	/* startup the audio subsystem */
82819bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
8295a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(cpu_dai, substream);
830ddee627cSLiam Girdwood 		if (ret < 0) {
8315a52a045SKuninori Morimoto 			dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
8325a52a045SKuninori Morimoto 				cpu_dai->name, ret);
8335d9fa03eSKuninori Morimoto 			goto cpu_dai_err;
834ddee627cSLiam Girdwood 		}
83519bdcc7aSShreyas NC 	}
836ddee627cSLiam Girdwood 
8370b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8385a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(codec_dai, substream);
839ddee627cSLiam Girdwood 		if (ret < 0) {
8402e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
8412e5894d7SBenoit Cousson 				"ASoC: can't open codec %s: %d\n",
8422e5894d7SBenoit Cousson 				codec_dai->name, ret);
8435d9fa03eSKuninori Morimoto 			goto config_err;
844ddee627cSLiam Girdwood 		}
845ddee627cSLiam Girdwood 
8462e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
8472e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
8482e5894d7SBenoit Cousson 		else
8492e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
8502e5894d7SBenoit Cousson 	}
8512e5894d7SBenoit Cousson 
85201d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
85301d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
85401d7584cSLiam Girdwood 		goto dynamic;
85501d7584cSLiam Girdwood 
856ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
8572e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
8582e5894d7SBenoit Cousson 
8592e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
8602e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
861ddee627cSLiam Girdwood 
86219bdcc7aSShreyas NC 	if (rtd->num_cpus == 1)
86319bdcc7aSShreyas NC 		cpu_dai_name = rtd->cpu_dai->name;
86419bdcc7aSShreyas NC 
86562e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
86662e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
86762e5f676SLars-Peter Clausen 
868ddee627cSLiam Girdwood 	ret = -EINVAL;
869ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
870103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
87119bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
872ddee627cSLiam Girdwood 		goto config_err;
873ddee627cSLiam Girdwood 	}
874ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
875103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
87619bdcc7aSShreyas NC 			codec_dai_name, cpu_dai_name);
877ddee627cSLiam Girdwood 		goto config_err;
878ddee627cSLiam Girdwood 	}
879ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
880ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
881103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
88219bdcc7aSShreyas NC 				codec_dai_name, cpu_dai_name);
883ddee627cSLiam Girdwood 		goto config_err;
884ddee627cSLiam Girdwood 	}
885ddee627cSLiam Girdwood 
886c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
88758ba9b25SMark Brown 
888ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
88919bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
89017841020SDong Aisheng 		if (cpu_dai->active) {
89117841020SDong Aisheng 			ret = soc_pcm_apply_symmetry(substream, cpu_dai);
89217841020SDong Aisheng 			if (ret != 0)
89317841020SDong Aisheng 				goto config_err;
89417841020SDong Aisheng 		}
89519bdcc7aSShreyas NC 	}
89617841020SDong Aisheng 
8970b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8980b7990e3SKuninori Morimoto 		if (codec_dai->active) {
8990b7990e3SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, codec_dai);
900ddee627cSLiam Girdwood 			if (ret != 0)
901ddee627cSLiam Girdwood 				goto config_err;
902ddee627cSLiam Girdwood 		}
9032e5894d7SBenoit Cousson 	}
904ddee627cSLiam Girdwood 
905103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
90619bdcc7aSShreyas NC 		 codec_dai_name, cpu_dai_name);
907103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
908103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
909ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
910103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
911ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
912ddee627cSLiam Girdwood 
91301d7584cSLiam Girdwood dynamic:
91424894b76SLars-Peter Clausen 
91524894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
91624894b76SLars-Peter Clausen 
91772b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
918ddee627cSLiam Girdwood 	return 0;
919ddee627cSLiam Girdwood 
920ddee627cSLiam Girdwood config_err:
921b56be800SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
922330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
9235d9fa03eSKuninori Morimoto cpu_dai_err:
92419bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
9255d9fa03eSKuninori Morimoto 		snd_soc_dai_shutdown(cpu_dai, substream);
9262e5894d7SBenoit Cousson 
9275d9fa03eSKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
928d2aaa8d8SKai Vehmanen rtd_startup_err:
929dd03907bSKuninori Morimoto 	soc_pcm_components_close(substream);
930d2aaa8d8SKai Vehmanen component_err:
93172b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
932d6652ef8SMark Brown 
933613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
93490be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
93590be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
9363f809783SSanyog Kale 	}
9373f809783SSanyog Kale 
93876c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
93976c39e86SKuninori Morimoto 		if (!component->active)
94076c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
941d6652ef8SMark Brown 
942ddee627cSLiam Girdwood 	return ret;
943ddee627cSLiam Girdwood }
944ddee627cSLiam Girdwood 
9454bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
946a342031cSJerome Brunet {
947a342031cSJerome Brunet 	/*
948a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
949a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
950a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
951a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
952a342031cSJerome Brunet 	 */
953a342031cSJerome Brunet }
954a342031cSJerome Brunet 
955ddee627cSLiam Girdwood /*
956ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
957ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
958ddee627cSLiam Girdwood  * it can refer to the runtime info.
959ddee627cSLiam Girdwood  */
960ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
961ddee627cSLiam Girdwood {
962ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
963b8135864SKuninori Morimoto 	struct snd_soc_component *component;
96419bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
9652e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9662e5894d7SBenoit Cousson 	int i, ret = 0;
967ddee627cSLiam Girdwood 
96872b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
969ddee627cSLiam Girdwood 
97044c1a75bSKuninori Morimoto 	ret = soc_rtd_prepare(rtd, substream);
971ddee627cSLiam Girdwood 	if (ret < 0) {
97244c1a75bSKuninori Morimoto 		dev_err(rtd->card->dev,
97344c1a75bSKuninori Morimoto 			"ASoC: machine prepare error: %d\n", ret);
974ddee627cSLiam Girdwood 		goto out;
975ddee627cSLiam Girdwood 	}
976ddee627cSLiam Girdwood 
977613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
9786d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
979b8135864SKuninori Morimoto 		if (ret < 0) {
980b8135864SKuninori Morimoto 			dev_err(component->dev,
981b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
982b8135864SKuninori Morimoto 			goto out;
983b8135864SKuninori Morimoto 		}
984b8135864SKuninori Morimoto 	}
985b8135864SKuninori Morimoto 
9860b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
9874beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(codec_dai, substream);
988ddee627cSLiam Girdwood 		if (ret < 0) {
9892e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
99090cc7f1cSJarkko Nikula 				"ASoC: codec DAI prepare error: %d\n",
99190cc7f1cSJarkko Nikula 				ret);
992ddee627cSLiam Girdwood 			goto out;
993ddee627cSLiam Girdwood 		}
994ddee627cSLiam Girdwood 	}
995ddee627cSLiam Girdwood 
99619bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
9974beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(cpu_dai, substream);
998ddee627cSLiam Girdwood 		if (ret < 0) {
99990cc7f1cSJarkko Nikula 			dev_err(cpu_dai->dev,
100090cc7f1cSJarkko Nikula 				"ASoC: cpu DAI prepare error: %d\n", ret);
1001ddee627cSLiam Girdwood 			goto out;
1002ddee627cSLiam Girdwood 		}
100319bdcc7aSShreyas NC 	}
1004ddee627cSLiam Girdwood 
1005ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
1006ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
10079bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
10089bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
1009ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
1010ddee627cSLiam Girdwood 	}
1011ddee627cSLiam Girdwood 
1012d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
1013ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
1014ddee627cSLiam Girdwood 
10150b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
10160b7990e3SKuninori Morimoto 		snd_soc_dai_digital_mute(codec_dai, 0,
10172e5894d7SBenoit Cousson 					 substream->stream);
101819bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai)
1019ae11601bSRamesh Babu 		snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
1020ddee627cSLiam Girdwood 
1021ddee627cSLiam Girdwood out:
102272b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1023ddee627cSLiam Girdwood 	return ret;
1024ddee627cSLiam Girdwood }
1025ddee627cSLiam Girdwood 
10262e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
10272e5894d7SBenoit Cousson 				       unsigned int mask)
10282e5894d7SBenoit Cousson {
10292e5894d7SBenoit Cousson 	struct snd_interval *interval;
10302e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
10312e5894d7SBenoit Cousson 
10322e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
10332e5894d7SBenoit Cousson 	interval->min = channels;
10342e5894d7SBenoit Cousson 	interval->max = channels;
10352e5894d7SBenoit Cousson }
10362e5894d7SBenoit Cousson 
1037244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
1038244e2936SCharles Keepax 				      struct snd_soc_component *last)
1039244e2936SCharles Keepax {
1040244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1041244e2936SCharles Keepax 	struct snd_soc_component *component;
1042e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
1043244e2936SCharles Keepax 
1044613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1045244e2936SCharles Keepax 		if (component == last)
1046244e2936SCharles Keepax 			break;
1047244e2936SCharles Keepax 
1048e82ebffcSKuninori Morimoto 		r = snd_soc_component_hw_free(component, substream);
1049e82ebffcSKuninori Morimoto 		if (r < 0)
1050e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
1051244e2936SCharles Keepax 	}
1052244e2936SCharles Keepax 
1053eae7136aSKuninori Morimoto 	return ret;
1054244e2936SCharles Keepax }
1055244e2936SCharles Keepax 
1056ddee627cSLiam Girdwood /*
1057ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
1058ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
1059ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
1060ddee627cSLiam Girdwood  */
1061ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1062ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
1063ddee627cSLiam Girdwood {
1064ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1065b8135864SKuninori Morimoto 	struct snd_soc_component *component;
106619bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
10670b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
1068244e2936SCharles Keepax 	int i, ret = 0;
1069ddee627cSLiam Girdwood 
107072b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
10715cca5951SShengjiu Wang 
10725cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
10735cca5951SShengjiu Wang 	if (ret)
10745cca5951SShengjiu Wang 		goto out;
10755cca5951SShengjiu Wang 
1076de9ad990SKuninori Morimoto 	ret = soc_rtd_hw_params(rtd, substream, params);
1077ddee627cSLiam Girdwood 	if (ret < 0) {
1078de9ad990SKuninori Morimoto 		dev_err(rtd->card->dev,
1079de9ad990SKuninori Morimoto 			"ASoC: machine hw_params failed: %d\n", ret);
1080ddee627cSLiam Girdwood 		goto out;
1081ddee627cSLiam Girdwood 	}
1082ddee627cSLiam Girdwood 
10830b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10842e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
10852e5894d7SBenoit Cousson 
1086cde79035SRicard Wanderlof 		/*
1087cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
1088cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
1089cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
1090cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
1091cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
1092cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
1093cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
1094cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1095cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
1096cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
1097cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
1098cde79035SRicard Wanderlof 		 * we can do the right thing here.
1099cde79035SRicard Wanderlof 		 */
1100cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1101cde79035SRicard Wanderlof 			continue;
1102cde79035SRicard Wanderlof 
11032e5894d7SBenoit Cousson 		/* copy params for each codec */
11042e5894d7SBenoit Cousson 		codec_params = *params;
11052e5894d7SBenoit Cousson 
11062e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
1107570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1108570f18b6SRander Wang 		    codec_dai->tx_mask)
11092e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
11102e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
1111570f18b6SRander Wang 
1112570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1113570f18b6SRander Wang 		    codec_dai->rx_mask)
11142e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
11152e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
11162e5894d7SBenoit Cousson 
1117aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1118aa6166c2SKuninori Morimoto 					    &codec_params);
111993e6958aSBenoit Cousson 		if(ret < 0)
1120ddee627cSLiam Girdwood 			goto codec_err;
1121ddee627cSLiam Girdwood 
11222e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
11232e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
11242e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
11252e5894d7SBenoit Cousson 						params_format(&codec_params));
1126078a85f2SCharles Keepax 
1127078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1128ddee627cSLiam Girdwood 	}
1129ddee627cSLiam Girdwood 
113019bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
11310e9cf4c4SBard Liao 		/*
11320e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
11330e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
11340e9cf4c4SBard Liao 		 */
11350e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
11360e9cf4c4SBard Liao 			continue;
11370e9cf4c4SBard Liao 
1138aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
113993e6958aSBenoit Cousson 		if (ret < 0)
1140ddee627cSLiam Girdwood 			goto interface_err;
1141ddee627cSLiam Girdwood 
114219bdcc7aSShreyas NC 		/* store the parameters for each DAI */
1143ca58221dSKuninori Morimoto 		cpu_dai->rate = params_rate(params);
1144ca58221dSKuninori Morimoto 		cpu_dai->channels = params_channels(params);
1145ca58221dSKuninori Morimoto 		cpu_dai->sample_bits =
1146ca58221dSKuninori Morimoto 			snd_pcm_format_physical_width(params_format(params));
1147ca58221dSKuninori Morimoto 
1148ca58221dSKuninori Morimoto 		snd_soc_dapm_update_dai(substream, params, cpu_dai);
114919bdcc7aSShreyas NC 	}
1150ca58221dSKuninori Morimoto 
1151613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
1152245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
1153244e2936SCharles Keepax 		if (ret < 0) {
1154b8135864SKuninori Morimoto 			dev_err(component->dev,
1155b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
1156244e2936SCharles Keepax 				component->name, ret);
1157b8135864SKuninori Morimoto 			goto component_err;
1158244e2936SCharles Keepax 		}
1159244e2936SCharles Keepax 	}
1160244e2936SCharles Keepax 	component = NULL;
1161b8135864SKuninori Morimoto 
1162ddee627cSLiam Girdwood out:
116372b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1164ddee627cSLiam Girdwood 	return ret;
1165ddee627cSLiam Girdwood 
1166b8135864SKuninori Morimoto component_err:
1167244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
1168b8135864SKuninori Morimoto 
116919bdcc7aSShreyas NC 	i = rtd->num_cpus;
1170ddee627cSLiam Girdwood 
1171ddee627cSLiam Girdwood interface_err:
117219bdcc7aSShreyas NC 	for_each_rtd_cpu_dai_rollback(rtd, i, cpu_dai) {
11730e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
11740e9cf4c4SBard Liao 			continue;
11750e9cf4c4SBard Liao 
117619bdcc7aSShreyas NC 		snd_soc_dai_hw_free(cpu_dai, substream);
117719bdcc7aSShreyas NC 		cpu_dai->rate = 0;
117819bdcc7aSShreyas NC 	}
117919bdcc7aSShreyas NC 
11802e5894d7SBenoit Cousson 	i = rtd->num_codecs;
1181ddee627cSLiam Girdwood 
1182ddee627cSLiam Girdwood codec_err:
11836d11b128SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1184f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1185f47b9ad9SJerome Brunet 			continue;
1186f47b9ad9SJerome Brunet 
1187846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
11882e5894d7SBenoit Cousson 		codec_dai->rate = 0;
11892e5894d7SBenoit Cousson 	}
11902e5894d7SBenoit Cousson 
119149f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1192ddee627cSLiam Girdwood 
119372b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1194ddee627cSLiam Girdwood 	return ret;
1195ddee627cSLiam Girdwood }
1196ddee627cSLiam Girdwood 
1197ddee627cSLiam Girdwood /*
1198ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
1199ddee627cSLiam Girdwood  */
1200ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1201ddee627cSLiam Girdwood {
1202ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
120319bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12042e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
12057f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
12062e5894d7SBenoit Cousson 	int i;
1207ddee627cSLiam Girdwood 
120872b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1209ddee627cSLiam Girdwood 
1210d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
121119bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1212d3383420SNicolin Chen 		if (cpu_dai->active == 1) {
1213d3383420SNicolin Chen 			cpu_dai->rate = 0;
1214d3383420SNicolin Chen 			cpu_dai->channels = 0;
1215d3383420SNicolin Chen 			cpu_dai->sample_bits = 0;
1216d3383420SNicolin Chen 		}
121719bdcc7aSShreyas NC 	}
1218d3383420SNicolin Chen 
12190b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1220d3383420SNicolin Chen 		if (codec_dai->active == 1) {
1221d3383420SNicolin Chen 			codec_dai->rate = 0;
1222d3383420SNicolin Chen 			codec_dai->channels = 0;
1223d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
1224d3383420SNicolin Chen 		}
12252e5894d7SBenoit Cousson 	}
1226d3383420SNicolin Chen 
1227ddee627cSLiam Girdwood 	/* apply codec digital mute */
12280b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
12290f6011fdSKuninori Morimoto 		int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK];
12300f6011fdSKuninori Morimoto 		int capture_active  = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE];
12310f6011fdSKuninori Morimoto 
12320f6011fdSKuninori Morimoto 		if ((playback && playback_active == 1) ||
12330f6011fdSKuninori Morimoto 		    (!playback && capture_active == 1))
12340b7990e3SKuninori Morimoto 			snd_soc_dai_digital_mute(codec_dai, 1,
12352e5894d7SBenoit Cousson 						 substream->stream);
12362e5894d7SBenoit Cousson 	}
1237ddee627cSLiam Girdwood 
1238ddee627cSLiam Girdwood 	/* free any machine hw params */
123949f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
1240ddee627cSLiam Girdwood 
1241b8135864SKuninori Morimoto 	/* free any component resources */
1242244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
1243b8135864SKuninori Morimoto 
1244ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
12450b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1246f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1247f47b9ad9SJerome Brunet 			continue;
1248f47b9ad9SJerome Brunet 
1249846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
12502e5894d7SBenoit Cousson 	}
1251ddee627cSLiam Girdwood 
12520e9cf4c4SBard Liao 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
12530e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
12540e9cf4c4SBard Liao 			continue;
12550e9cf4c4SBard Liao 
1256846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(cpu_dai, substream);
12570e9cf4c4SBard Liao 	}
1258ddee627cSLiam Girdwood 
125972b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
1260ddee627cSLiam Girdwood 	return 0;
1261ddee627cSLiam Girdwood }
1262ddee627cSLiam Girdwood 
12634378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1264ddee627cSLiam Girdwood {
1265ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1266b8135864SKuninori Morimoto 	struct snd_soc_component *component;
126719bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
12682e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
12692e5894d7SBenoit Cousson 	int i, ret;
1270ddee627cSLiam Girdwood 
1271ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
1272ddee627cSLiam Girdwood 	if (ret < 0)
1273ddee627cSLiam Girdwood 		return ret;
1274ddee627cSLiam Girdwood 
1275613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
12765693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1277b8135864SKuninori Morimoto 		if (ret < 0)
1278b8135864SKuninori Morimoto 			return ret;
1279b8135864SKuninori Morimoto 	}
1280b8135864SKuninori Morimoto 
128119bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1282901e822bSDan Carpenter 		ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1283ddee627cSLiam Girdwood 		if (ret < 0)
1284ddee627cSLiam Girdwood 			return ret;
128519bdcc7aSShreyas NC 	}
12864792b0dbSJarkko Nikula 
12874378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
12884378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
12894378f1fbSPeter Ujfalusi 		if (ret < 0)
12904378f1fbSPeter Ujfalusi 			return ret;
12914378f1fbSPeter Ujfalusi 	}
12924378f1fbSPeter Ujfalusi 
12934378f1fbSPeter Ujfalusi 	return 0;
12944378f1fbSPeter Ujfalusi }
12954378f1fbSPeter Ujfalusi 
12964378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
12974378f1fbSPeter Ujfalusi {
12984378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
12994378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
130019bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
13014378f1fbSPeter Ujfalusi 	struct snd_soc_dai *codec_dai;
13024378f1fbSPeter Ujfalusi 	int i, ret;
13034378f1fbSPeter Ujfalusi 
13044378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
13054378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
13064378f1fbSPeter Ujfalusi 		if (ret < 0)
13074378f1fbSPeter Ujfalusi 			return ret;
13084378f1fbSPeter Ujfalusi 	}
13094378f1fbSPeter Ujfalusi 
131019bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
13114378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
13124378f1fbSPeter Ujfalusi 		if (ret < 0)
13134378f1fbSPeter Ujfalusi 			return ret;
131419bdcc7aSShreyas NC 	}
13154378f1fbSPeter Ujfalusi 
1316613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
13174378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
13184378f1fbSPeter Ujfalusi 		if (ret < 0)
13194378f1fbSPeter Ujfalusi 			return ret;
13204378f1fbSPeter Ujfalusi 	}
13214378f1fbSPeter Ujfalusi 
1322ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
13234792b0dbSJarkko Nikula 	if (ret < 0)
13244792b0dbSJarkko Nikula 		return ret;
13254792b0dbSJarkko Nikula 
1326ddee627cSLiam Girdwood 	return 0;
1327ddee627cSLiam Girdwood }
1328ddee627cSLiam Girdwood 
13294378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
13304378f1fbSPeter Ujfalusi {
13314378f1fbSPeter Ujfalusi 	int ret;
13324378f1fbSPeter Ujfalusi 
13334378f1fbSPeter Ujfalusi 	switch (cmd) {
13344378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
13354378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
13364378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
13374378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
13384378f1fbSPeter Ujfalusi 		break;
13394378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
13404378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
13414378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
13424378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
13434378f1fbSPeter Ujfalusi 		break;
13444378f1fbSPeter Ujfalusi 	default:
13454378f1fbSPeter Ujfalusi 		return -EINVAL;
13464378f1fbSPeter Ujfalusi 	}
13474378f1fbSPeter Ujfalusi 
13484378f1fbSPeter Ujfalusi 	return ret;
13494378f1fbSPeter Ujfalusi }
13504378f1fbSPeter Ujfalusi 
135145c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
135245c0a188SMark Brown 				   int cmd)
135307bf84aaSLiam Girdwood {
135407bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
135519bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
13562e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
13572e5894d7SBenoit Cousson 	int i, ret;
135807bf84aaSLiam Girdwood 
13590b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
13605c0769afSKuninori Morimoto 		ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
136107bf84aaSLiam Girdwood 		if (ret < 0)
136207bf84aaSLiam Girdwood 			return ret;
136307bf84aaSLiam Girdwood 	}
136407bf84aaSLiam Girdwood 
136519bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
1366901e822bSDan Carpenter 		ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
136707bf84aaSLiam Girdwood 		if (ret < 0)
136807bf84aaSLiam Girdwood 			return ret;
136919bdcc7aSShreyas NC 	}
13705c0769afSKuninori Morimoto 
137107bf84aaSLiam Girdwood 	return 0;
137207bf84aaSLiam Girdwood }
1373ddee627cSLiam Girdwood /*
1374ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1375ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1376ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1377ddee627cSLiam Girdwood  */
1378ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1379ddee627cSLiam Girdwood {
1380ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
138119bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
13822e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1383ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1384ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1385ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
13862e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
138719bdcc7aSShreyas NC 	snd_pcm_sframes_t cpu_delay = 0;
13882e5894d7SBenoit Cousson 	int i;
1389ddee627cSLiam Girdwood 
13909fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
13919fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
13929fb4c2bfSAkshu Agrawal 
13930035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1394b8135864SKuninori Morimoto 
13959fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
13969fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1397b8135864SKuninori Morimoto 
139819bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
139919bdcc7aSShreyas NC 		cpu_delay = max(cpu_delay,
140019bdcc7aSShreyas NC 				snd_soc_dai_delay(cpu_dai, substream));
140119bdcc7aSShreyas NC 	}
140219bdcc7aSShreyas NC 	delay += cpu_delay;
1403ddee627cSLiam Girdwood 
14040b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
14052e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
14061dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
14072e5894d7SBenoit Cousson 	}
14082e5894d7SBenoit Cousson 	delay += codec_delay;
1409ddee627cSLiam Girdwood 
1410ddee627cSLiam Girdwood 	runtime->delay = delay;
1411ddee627cSLiam Girdwood 
1412ddee627cSLiam Girdwood 	return offset;
1413ddee627cSLiam Girdwood }
1414ddee627cSLiam Girdwood 
141501d7584cSLiam Girdwood /* connect a FE and BE */
141601d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
141701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
141801d7584cSLiam Girdwood {
141901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1420a9764869SKaiChieh Chuang 	unsigned long flags;
142101d7584cSLiam Girdwood 
142201d7584cSLiam Girdwood 	/* only add new dpcms */
14238d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
142401d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
142501d7584cSLiam Girdwood 			return 0;
142601d7584cSLiam Girdwood 	}
142701d7584cSLiam Girdwood 
142801d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
142901d7584cSLiam Girdwood 	if (!dpcm)
143001d7584cSLiam Girdwood 		return -ENOMEM;
143101d7584cSLiam Girdwood 
143201d7584cSLiam Girdwood 	dpcm->be = be;
143301d7584cSLiam Girdwood 	dpcm->fe = fe;
143401d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
143501d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1436a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
143701d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
143801d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1439a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
144001d7584cSLiam Girdwood 
144101d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
144201d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
144301d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
144401d7584cSLiam Girdwood 
1445154dae87SKuninori Morimoto 	dpcm_create_debugfs_state(dpcm, stream);
1446154dae87SKuninori Morimoto 
144701d7584cSLiam Girdwood 	return 1;
144801d7584cSLiam Girdwood }
144901d7584cSLiam Girdwood 
145001d7584cSLiam Girdwood /* reparent a BE onto another FE */
145101d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
145201d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
145301d7584cSLiam Girdwood {
145401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
145501d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
145601d7584cSLiam Girdwood 
145701d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
145801d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
145901d7584cSLiam Girdwood 		return;
146001d7584cSLiam Girdwood 
146101d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
146201d7584cSLiam Girdwood 
1463d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
146401d7584cSLiam Girdwood 		if (dpcm->fe == fe)
146501d7584cSLiam Girdwood 			continue;
146601d7584cSLiam Girdwood 
146701d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
146801d7584cSLiam Girdwood 			stream ? "capture" : "playback",
146901d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
147001d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
147101d7584cSLiam Girdwood 
147201d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
147301d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
147401d7584cSLiam Girdwood 		break;
147501d7584cSLiam Girdwood 	}
147601d7584cSLiam Girdwood }
147701d7584cSLiam Girdwood 
147801d7584cSLiam Girdwood /* disconnect a BE and FE */
147923607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
148001d7584cSLiam Girdwood {
148101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1482a9764869SKaiChieh Chuang 	unsigned long flags;
148301d7584cSLiam Girdwood 
14848d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1485103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
148601d7584cSLiam Girdwood 				stream ? "capture" : "playback",
148701d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
148801d7584cSLiam Girdwood 
148901d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
149001d7584cSLiam Girdwood 			continue;
149101d7584cSLiam Girdwood 
149201d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
149301d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
149401d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
149501d7584cSLiam Girdwood 
149601d7584cSLiam Girdwood 		/* BEs still alive need new FE */
149701d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
149801d7584cSLiam Girdwood 
1499154dae87SKuninori Morimoto 		dpcm_remove_debugfs_state(dpcm);
1500154dae87SKuninori Morimoto 
1501a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
150201d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
150301d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1504a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
150501d7584cSLiam Girdwood 		kfree(dpcm);
150601d7584cSLiam Girdwood 	}
150701d7584cSLiam Girdwood }
150801d7584cSLiam Girdwood 
150901d7584cSLiam Girdwood /* get BE for DAI widget and stream */
151001d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
151101d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
151201d7584cSLiam Girdwood {
151301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
151493597faeSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
15157afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
15161a497983SMengdong Lin 	int i;
151701d7584cSLiam Girdwood 
15183c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
15193c146465SLiam Girdwood 
1520bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, be) {
152101d7584cSLiam Girdwood 
152235ea0655SLiam Girdwood 		if (!be->dai_link->no_pcm)
152335ea0655SLiam Girdwood 			continue;
152435ea0655SLiam Girdwood 
152519bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
152619bdcc7aSShreyas NC 			w = snd_soc_dai_get_widget(dai, stream);
152793597faeSKuninori Morimoto 
15283c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
152993597faeSKuninori Morimoto 				w ? w->name : "(not set)");
15303c146465SLiam Girdwood 
153193597faeSKuninori Morimoto 			if (w == widget)
153201d7584cSLiam Girdwood 				return be;
153319bdcc7aSShreyas NC 		}
15342e5894d7SBenoit Cousson 
15357afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
15360c01f6caSKuninori Morimoto 			w = snd_soc_dai_get_widget(dai, stream);
153793597faeSKuninori Morimoto 
153893597faeSKuninori Morimoto 			if (w == widget)
15392e5894d7SBenoit Cousson 				return be;
15402e5894d7SBenoit Cousson 		}
154101d7584cSLiam Girdwood 	}
154201d7584cSLiam Girdwood 
15439d6ee365SJerome Brunet 	/* Widget provided is not a BE */
154401d7584cSLiam Girdwood 	return NULL;
154501d7584cSLiam Girdwood }
154601d7584cSLiam Girdwood 
154701d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
154801d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
154901d7584cSLiam Girdwood {
155009e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
155101d7584cSLiam Girdwood 	int i;
155201d7584cSLiam Girdwood 
155309e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
155409e88f8aSKuninori Morimoto 		if (widget == w)
155501d7584cSLiam Girdwood 			return 1;
155601d7584cSLiam Girdwood 
155701d7584cSLiam Girdwood 	return 0;
155801d7584cSLiam Girdwood }
155901d7584cSLiam Girdwood 
15605fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
15615fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
15625fdd022cSPiotr Stankiewicz {
15635fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
15645fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
1565c2cd8216SKuninori Morimoto 	int stream;
15665fdd022cSPiotr Stankiewicz 
1567c2cd8216SKuninori Morimoto 	/* adjust dir to stream */
1568c2cd8216SKuninori Morimoto 	if (dir == SND_SOC_DAPM_DIR_OUT)
1569c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1570c2cd8216SKuninori Morimoto 	else
1571c2cd8216SKuninori Morimoto 		stream = SNDRV_PCM_STREAM_CAPTURE;
1572c2cd8216SKuninori Morimoto 
1573027a4838SKuninori Morimoto 	rtd = dpcm_get_be(card, widget, stream);
1574027a4838SKuninori Morimoto 	if (rtd)
15755fdd022cSPiotr Stankiewicz 		return true;
15765fdd022cSPiotr Stankiewicz 
15775fdd022cSPiotr Stankiewicz 	return false;
15785fdd022cSPiotr Stankiewicz }
15795fdd022cSPiotr Stankiewicz 
158023607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
15811ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
158201d7584cSLiam Girdwood {
158301d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
158401d7584cSLiam Girdwood 	int paths;
158501d7584cSLiam Girdwood 
15866e1276a5SBard Liao 	if (fe->num_cpus > 1) {
15876e1276a5SBard Liao 		dev_err(fe->dev,
15886e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
15896e1276a5SBard Liao 		return -EINVAL;
15906e1276a5SBard Liao 	}
15916e1276a5SBard Liao 
159201d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
15936742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
15945fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
159501d7584cSLiam Girdwood 
1596103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
159701d7584cSLiam Girdwood 			stream ? "capture" : "playback");
159801d7584cSLiam Girdwood 
159901d7584cSLiam Girdwood 	return paths;
160001d7584cSLiam Girdwood }
160101d7584cSLiam Girdwood 
160252645e33SKuninori Morimoto void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
160352645e33SKuninori Morimoto {
160452645e33SKuninori Morimoto 	snd_soc_dapm_dai_free_widgets(list);
160552645e33SKuninori Morimoto }
160652645e33SKuninori Morimoto 
160701d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
160801d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
160901d7584cSLiam Girdwood {
161001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
161101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
161201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
16137afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
161401d7584cSLiam Girdwood 	int prune = 0;
1615bed646dcSKuninori Morimoto 	int do_prune;
161601d7584cSLiam Girdwood 
161701d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
16188d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
16192e5894d7SBenoit Cousson 		unsigned int i;
162001d7584cSLiam Girdwood 
162101d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
162219bdcc7aSShreyas NC 		do_prune = 1;
162319bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(dpcm->be, i, dai) {
162419bdcc7aSShreyas NC 			widget = snd_soc_dai_get_widget(dai, stream);
162501d7584cSLiam Girdwood 
162619bdcc7aSShreyas NC 			/*
162719bdcc7aSShreyas NC 			 * The BE is pruned only if none of the cpu_dai
162819bdcc7aSShreyas NC 			 * widgets are in the active list.
162919bdcc7aSShreyas NC 			 */
163001d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
163119bdcc7aSShreyas NC 				do_prune = 0;
163219bdcc7aSShreyas NC 		}
163319bdcc7aSShreyas NC 		if (!do_prune)
163401d7584cSLiam Girdwood 			continue;
163501d7584cSLiam Girdwood 
163601d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
1637bed646dcSKuninori Morimoto 		do_prune = 1;
16387afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(dpcm->be, i, dai) {
16390c01f6caSKuninori Morimoto 			widget = snd_soc_dai_get_widget(dai, stream);
164001d7584cSLiam Girdwood 
164101d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
164201d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
1643bed646dcSKuninori Morimoto 				do_prune = 0;
16442e5894d7SBenoit Cousson 		}
1645bed646dcSKuninori Morimoto 		if (!do_prune)
1646bed646dcSKuninori Morimoto 			continue;
164701d7584cSLiam Girdwood 
1648103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
164901d7584cSLiam Girdwood 			stream ? "capture" : "playback",
165001d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
165101d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
165201d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
165301d7584cSLiam Girdwood 		prune++;
165401d7584cSLiam Girdwood 	}
165501d7584cSLiam Girdwood 
1656103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
165701d7584cSLiam Girdwood 	return prune;
165801d7584cSLiam Girdwood }
165901d7584cSLiam Girdwood 
166001d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
166101d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
166201d7584cSLiam Girdwood {
166301d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
166401d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
166501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
166609e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
166701d7584cSLiam Girdwood 	int i, new = 0, err;
166801d7584cSLiam Girdwood 
166901d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
167009e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
167101d7584cSLiam Girdwood 
167209e88f8aSKuninori Morimoto 		switch (widget->id) {
16734616274dSMark Brown 		case snd_soc_dapm_dai_in:
1674c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1675c5b8540dSKoro Chen 				continue;
1676c5b8540dSKoro Chen 			break;
16774616274dSMark Brown 		case snd_soc_dapm_dai_out:
1678c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1679c5b8540dSKoro Chen 				continue;
16804616274dSMark Brown 			break;
16814616274dSMark Brown 		default:
168201d7584cSLiam Girdwood 			continue;
16834616274dSMark Brown 		}
168401d7584cSLiam Girdwood 
168501d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
168609e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
168701d7584cSLiam Girdwood 		if (!be) {
1688103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
168909e88f8aSKuninori Morimoto 					widget->name);
169001d7584cSLiam Girdwood 			continue;
169101d7584cSLiam Girdwood 		}
169201d7584cSLiam Girdwood 
169301d7584cSLiam Girdwood 		/* make sure BE is a real BE */
169401d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
169501d7584cSLiam Girdwood 			continue;
169601d7584cSLiam Girdwood 
169701d7584cSLiam Girdwood 		/* don't connect if FE is not running */
169823607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
169901d7584cSLiam Girdwood 			continue;
170001d7584cSLiam Girdwood 
170101d7584cSLiam Girdwood 		/* newly connected FE and BE */
170201d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
170301d7584cSLiam Girdwood 		if (err < 0) {
1704103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
170509e88f8aSKuninori Morimoto 				widget->name);
170601d7584cSLiam Girdwood 			break;
170701d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
170801d7584cSLiam Girdwood 			continue;
170901d7584cSLiam Girdwood 
171001d7584cSLiam Girdwood 		/* new */
171101d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
171201d7584cSLiam Girdwood 		new++;
171301d7584cSLiam Girdwood 	}
171401d7584cSLiam Girdwood 
1715103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
171601d7584cSLiam Girdwood 	return new;
171701d7584cSLiam Girdwood }
171801d7584cSLiam Girdwood 
171901d7584cSLiam Girdwood /*
172001d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
172101d7584cSLiam Girdwood  * FE substream.
172201d7584cSLiam Girdwood  */
172323607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
172401d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
172501d7584cSLiam Girdwood {
172601d7584cSLiam Girdwood 	if (new)
172701d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
172801d7584cSLiam Girdwood 	else
172901d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
173001d7584cSLiam Girdwood }
173101d7584cSLiam Girdwood 
173223607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
173301d7584cSLiam Girdwood {
173401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1735a9764869SKaiChieh Chuang 	unsigned long flags;
173601d7584cSLiam Girdwood 
1737a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
17388d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
173901d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
174001d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1741a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
174201d7584cSLiam Girdwood }
174301d7584cSLiam Girdwood 
174401d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
174501d7584cSLiam Girdwood 	int stream)
174601d7584cSLiam Girdwood {
174701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
174801d7584cSLiam Girdwood 
174901d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
17508d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
175101d7584cSLiam Girdwood 
175201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
175301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
175401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
175501d7584cSLiam Girdwood 
175601d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1757103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
175801d7584cSLiam Girdwood 				stream ? "capture" : "playback",
175901d7584cSLiam Girdwood 				be->dpcm[stream].state);
176001d7584cSLiam Girdwood 
176101d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
176201d7584cSLiam Girdwood 			continue;
176301d7584cSLiam Girdwood 
176401d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
176501d7584cSLiam Girdwood 			continue;
176601d7584cSLiam Girdwood 
176701d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
176801d7584cSLiam Girdwood 		be_substream->runtime = NULL;
176901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
177001d7584cSLiam Girdwood 	}
177101d7584cSLiam Girdwood }
177201d7584cSLiam Girdwood 
177323607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
177401d7584cSLiam Girdwood {
177501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
177601d7584cSLiam Girdwood 	int err, count = 0;
177701d7584cSLiam Girdwood 
177801d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
17798d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
178001d7584cSLiam Girdwood 
178101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
178201d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
178301d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
178401d7584cSLiam Girdwood 
17852062b4c5SRussell King - ARM Linux 		if (!be_substream) {
17862062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
17872062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
17882062b4c5SRussell King - ARM Linux 			continue;
17892062b4c5SRussell King - ARM Linux 		}
17902062b4c5SRussell King - ARM Linux 
179101d7584cSLiam Girdwood 		/* is this op for this BE ? */
179201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
179301d7584cSLiam Girdwood 			continue;
179401d7584cSLiam Girdwood 
179501d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
179601d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1797103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
179801d7584cSLiam Girdwood 				stream ? "capture" : "playback",
179901d7584cSLiam Girdwood 				be->dpcm[stream].state);
180001d7584cSLiam Girdwood 
180101d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
180201d7584cSLiam Girdwood 			continue;
180301d7584cSLiam Girdwood 
180401d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
180501d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
180601d7584cSLiam Girdwood 			continue;
180701d7584cSLiam Girdwood 
18082062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
18092062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
181001d7584cSLiam Girdwood 
181101d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
181201d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
181301d7584cSLiam Girdwood 		if (err < 0) {
1814103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
181501d7584cSLiam Girdwood 			be->dpcm[stream].users--;
181601d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1817103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
181801d7584cSLiam Girdwood 					stream ? "capture" : "playback",
181901d7584cSLiam Girdwood 					be->dpcm[stream].state);
182001d7584cSLiam Girdwood 
182101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
182201d7584cSLiam Girdwood 			goto unwind;
182301d7584cSLiam Girdwood 		}
182401d7584cSLiam Girdwood 
182501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
182601d7584cSLiam Girdwood 		count++;
182701d7584cSLiam Girdwood 	}
182801d7584cSLiam Girdwood 
182901d7584cSLiam Girdwood 	return count;
183001d7584cSLiam Girdwood 
183101d7584cSLiam Girdwood unwind:
183201d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
18338d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
183401d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
183501d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
183601d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
183701d7584cSLiam Girdwood 
183801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
183901d7584cSLiam Girdwood 			continue;
184001d7584cSLiam Girdwood 
184101d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1842103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
184301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
184401d7584cSLiam Girdwood 				be->dpcm[stream].state);
184501d7584cSLiam Girdwood 
184601d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
184701d7584cSLiam Girdwood 			continue;
184801d7584cSLiam Girdwood 
184901d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
185001d7584cSLiam Girdwood 			continue;
185101d7584cSLiam Girdwood 
185201d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
185301d7584cSLiam Girdwood 		be_substream->runtime = NULL;
185401d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
185501d7584cSLiam Girdwood 	}
185601d7584cSLiam Girdwood 
185701d7584cSLiam Girdwood 	return err;
185801d7584cSLiam Girdwood }
185901d7584cSLiam Girdwood 
186008ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1861435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
186208ae9b45SLars-Peter Clausen {
186308ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1864e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
186508ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
186608ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1867002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1868435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1869002220a9SLars-Peter Clausen 	else
1870435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
187108ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
187208ae9b45SLars-Peter Clausen }
187308ae9b45SLars-Peter Clausen 
1874435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1875435ffb76SJerome Brunet 				      u64 *formats)
1876b073ed4eSKuninori Morimoto {
1877b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1878b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
18797afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1880b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1881b073ed4eSKuninori Morimoto 
1882b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1883435ffb76SJerome Brunet 		return;
1884b073ed4eSKuninori Morimoto 
1885b073ed4eSKuninori Morimoto 	/*
1886b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1887b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1888b073ed4eSKuninori Morimoto 	 */
1889b073ed4eSKuninori Morimoto 
18908d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1891b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1892b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1893b073ed4eSKuninori Morimoto 		int i;
1894b073ed4eSKuninori Morimoto 
18957afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
18964febced1SJerome Brunet 			/*
18974febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
18984febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
18994febced1SJerome Brunet 			 */
19007afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
19014febced1SJerome Brunet 				continue;
19024febced1SJerome Brunet 
1903acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1904b073ed4eSKuninori Morimoto 
1905435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1906435ffb76SJerome Brunet 		}
1907b073ed4eSKuninori Morimoto 	}
1908b073ed4eSKuninori Morimoto }
1909b073ed4eSKuninori Morimoto 
1910435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1911f4c277b8SJiada Wang 				    unsigned int *channels_min,
1912f4c277b8SJiada Wang 				    unsigned int *channels_max)
1913f4c277b8SJiada Wang {
1914f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1915f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1916f4c277b8SJiada Wang 	int stream = substream->stream;
1917f4c277b8SJiada Wang 
1918f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1919f4c277b8SJiada Wang 		return;
1920f4c277b8SJiada Wang 
1921f4c277b8SJiada Wang 	/*
1922f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1923f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1924f4c277b8SJiada Wang 	 */
1925f4c277b8SJiada Wang 
19268d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1927f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
1928f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
19294f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
193019bdcc7aSShreyas NC 		struct snd_soc_dai *dai;
193119bdcc7aSShreyas NC 		int i;
1932f4c277b8SJiada Wang 
193319bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
19340e9cf4c4SBard Liao 			/*
19350e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
19360e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
19370e9cf4c4SBard Liao 			 */
19380e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
19390e9cf4c4SBard Liao 				continue;
19400e9cf4c4SBard Liao 
194119bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
19424f2bd18bSJerome Brunet 
194319bdcc7aSShreyas NC 			*channels_min = max(*channels_min,
194419bdcc7aSShreyas NC 					    cpu_stream->channels_min);
194519bdcc7aSShreyas NC 			*channels_max = min(*channels_max,
194619bdcc7aSShreyas NC 					    cpu_stream->channels_max);
194719bdcc7aSShreyas NC 		}
19484f2bd18bSJerome Brunet 
19494f2bd18bSJerome Brunet 		/*
19504f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
19514f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
19524f2bd18bSJerome Brunet 		 */
19534f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
1954acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream);
1955f4c277b8SJiada Wang 
1956f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1957f4c277b8SJiada Wang 					    codec_stream->channels_min);
1958f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1959f4c277b8SJiada Wang 					    codec_stream->channels_max);
1960f4c277b8SJiada Wang 		}
1961f4c277b8SJiada Wang 	}
1962f4c277b8SJiada Wang }
1963f4c277b8SJiada Wang 
1964baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1965baacd8d1SJerome Brunet 				    unsigned int *rates,
1966baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1967baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1968baacd8d1SJerome Brunet {
1969baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1970baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1971baacd8d1SJerome Brunet 	int stream = substream->stream;
1972baacd8d1SJerome Brunet 
1973baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1974baacd8d1SJerome Brunet 		return;
1975baacd8d1SJerome Brunet 
1976baacd8d1SJerome Brunet 	/*
1977baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1978baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1979baacd8d1SJerome Brunet 	 */
1980baacd8d1SJerome Brunet 
19818d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1982baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1983baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *codec_stream;
1984baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
19857afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1986baacd8d1SJerome Brunet 		int i;
1987baacd8d1SJerome Brunet 
198819bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(be, i, dai) {
19890e9cf4c4SBard Liao 			/*
19900e9cf4c4SBard Liao 			 * Skip CPUs which don't support the current stream
19910e9cf4c4SBard Liao 			 * type. See soc_pcm_init_runtime_hw() for more details
19920e9cf4c4SBard Liao 			 */
19930e9cf4c4SBard Liao 			if (!snd_soc_dai_stream_valid(dai, stream))
19940e9cf4c4SBard Liao 				continue;
19950e9cf4c4SBard Liao 
199619bdcc7aSShreyas NC 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1997baacd8d1SJerome Brunet 
1998baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, cpu_stream->rate_min);
199919bdcc7aSShreyas NC 			*rate_max = min_not_zero(*rate_max,
200019bdcc7aSShreyas NC 						 cpu_stream->rate_max);
200119bdcc7aSShreyas NC 			*rates = snd_pcm_rate_mask_intersect(*rates,
200219bdcc7aSShreyas NC 						cpu_stream->rates);
200319bdcc7aSShreyas NC 		}
2004baacd8d1SJerome Brunet 
20057afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
2006baacd8d1SJerome Brunet 			/*
2007baacd8d1SJerome Brunet 			 * Skip CODECs which don't support the current stream
2008baacd8d1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
2009baacd8d1SJerome Brunet 			 */
20107afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
2011baacd8d1SJerome Brunet 				continue;
2012baacd8d1SJerome Brunet 
2013acf253c1SKuninori Morimoto 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
2014baacd8d1SJerome Brunet 
2015baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, codec_stream->rate_min);
2016baacd8d1SJerome Brunet 			*rate_max = min_not_zero(*rate_max,
2017baacd8d1SJerome Brunet 						 codec_stream->rate_max);
2018baacd8d1SJerome Brunet 			*rates = snd_pcm_rate_mask_intersect(*rates,
2019baacd8d1SJerome Brunet 						codec_stream->rates);
2020baacd8d1SJerome Brunet 		}
2021baacd8d1SJerome Brunet 	}
2022baacd8d1SJerome Brunet }
2023baacd8d1SJerome Brunet 
202445c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
202501d7584cSLiam Girdwood {
202601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
202701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
202819bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
202919bdcc7aSShreyas NC 	struct snd_soc_dai_driver *cpu_dai_drv;
203019bdcc7aSShreyas NC 	int i;
203101d7584cSLiam Girdwood 
203219bdcc7aSShreyas NC 	for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
20330e9cf4c4SBard Liao 		/*
20340e9cf4c4SBard Liao 		 * Skip CPUs which don't support the current stream
20350e9cf4c4SBard Liao 		 * type. See soc_pcm_init_runtime_hw() for more details
20360e9cf4c4SBard Liao 		 */
20370e9cf4c4SBard Liao 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
20380e9cf4c4SBard Liao 			continue;
20390e9cf4c4SBard Liao 
204019bdcc7aSShreyas NC 		cpu_dai_drv = cpu_dai->driver;
204108ae9b45SLars-Peter Clausen 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2042435ffb76SJerome Brunet 			dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
204308ae9b45SLars-Peter Clausen 		else
2044435ffb76SJerome Brunet 			dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
204519bdcc7aSShreyas NC 	}
2046f4c277b8SJiada Wang 
2047435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
2048435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
2049435ffb76SJerome Brunet 				&runtime->hw.channels_max);
2050baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
2051baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
205201d7584cSLiam Girdwood }
205301d7584cSLiam Girdwood 
2054ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
2055ea9d0d77STakashi Iwai 
2056ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
2057ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
2058ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
2059ea9d0d77STakashi Iwai  * process the pending trigger action here.
2060ea9d0d77STakashi Iwai  */
2061ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
2062ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
2063ea9d0d77STakashi Iwai {
2064ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
2065ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
2066ea9d0d77STakashi Iwai 
2067ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
2068ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
2069ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
2070ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
2071ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
2072ea9d0d77STakashi Iwai 	}
2073ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
2074ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
2075ea9d0d77STakashi Iwai }
2076ea9d0d77STakashi Iwai 
2077906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
2078906c7d69SPC Liao 			       int stream)
2079906c7d69SPC Liao {
2080906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
2081906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
208219bdcc7aSShreyas NC 	struct snd_soc_dai *fe_cpu_dai;
2083906c7d69SPC Liao 	int err;
208419bdcc7aSShreyas NC 	int i;
2085906c7d69SPC Liao 
2086906c7d69SPC Liao 	/* apply symmetry for FE */
2087906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
2088906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2089906c7d69SPC Liao 
209019bdcc7aSShreyas NC 	for_each_rtd_cpu_dai (fe, i, fe_cpu_dai) {
2091906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
2092906c7d69SPC Liao 		if (fe_cpu_dai->active) {
2093906c7d69SPC Liao 			err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
2094906c7d69SPC Liao 			if (err < 0)
2095906c7d69SPC Liao 				return err;
2096906c7d69SPC Liao 		}
209719bdcc7aSShreyas NC 	}
2098906c7d69SPC Liao 
2099906c7d69SPC Liao 	/* apply symmetry for BE */
21008d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2101906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
2102906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
2103906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
21046246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
21050b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
210619bdcc7aSShreyas NC 		struct snd_soc_dai *cpu_dai;
2107906c7d69SPC Liao 		int i;
2108906c7d69SPC Liao 
21096246f283SJerome Brunet 		/* A backend may not have the requested substream */
21106246f283SJerome Brunet 		if (!be_substream)
21116246f283SJerome Brunet 			continue;
21126246f283SJerome Brunet 
21136246f283SJerome Brunet 		rtd = be_substream->private_data;
2114f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
2115f1176614SJeeja KP 			continue;
2116f1176614SJeeja KP 
2117906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
2118906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
2119906c7d69SPC Liao 
2120906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
212119bdcc7aSShreyas NC 		for_each_rtd_cpu_dai(rtd, i, cpu_dai) {
212219bdcc7aSShreyas NC 			if (cpu_dai->active) {
212399bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
212419bdcc7aSShreyas NC 							     cpu_dai);
2125906c7d69SPC Liao 				if (err < 0)
2126906c7d69SPC Liao 					return err;
2127906c7d69SPC Liao 			}
212819bdcc7aSShreyas NC 		}
2129906c7d69SPC Liao 
21300b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
21310b7990e3SKuninori Morimoto 			if (codec_dai->active) {
213299bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
21330b7990e3SKuninori Morimoto 							     codec_dai);
2134906c7d69SPC Liao 				if (err < 0)
2135906c7d69SPC Liao 					return err;
2136906c7d69SPC Liao 			}
2137906c7d69SPC Liao 		}
2138906c7d69SPC Liao 	}
2139906c7d69SPC Liao 
2140906c7d69SPC Liao 	return 0;
2141906c7d69SPC Liao }
2142906c7d69SPC Liao 
214301d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
214401d7584cSLiam Girdwood {
214501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
214601d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
214701d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
214801d7584cSLiam Girdwood 
2149ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
215001d7584cSLiam Girdwood 
2151*25c2f515SKuninori Morimoto 	ret = dpcm_be_dai_startup(fe, stream);
215201d7584cSLiam Girdwood 	if (ret < 0) {
2153103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
215401d7584cSLiam Girdwood 		goto be_err;
215501d7584cSLiam Girdwood 	}
215601d7584cSLiam Girdwood 
2157103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
215801d7584cSLiam Girdwood 
215901d7584cSLiam Girdwood 	/* start the DAI frontend */
216001d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
216101d7584cSLiam Girdwood 	if (ret < 0) {
2162103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
216301d7584cSLiam Girdwood 		goto unwind;
216401d7584cSLiam Girdwood 	}
216501d7584cSLiam Girdwood 
216601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
216701d7584cSLiam Girdwood 
216801d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
216901d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
217001d7584cSLiam Girdwood 
2171906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
2172906c7d69SPC Liao 	if (ret < 0) {
2173906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2174906c7d69SPC Liao 			ret);
2175906c7d69SPC Liao 		goto unwind;
2176906c7d69SPC Liao 	}
2177906c7d69SPC Liao 
2178ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
217901d7584cSLiam Girdwood 	return 0;
218001d7584cSLiam Girdwood 
218101d7584cSLiam Girdwood unwind:
2182*25c2f515SKuninori Morimoto 	dpcm_be_dai_startup_unwind(fe, stream);
218301d7584cSLiam Girdwood be_err:
2184ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
218501d7584cSLiam Girdwood 	return ret;
218601d7584cSLiam Girdwood }
218701d7584cSLiam Girdwood 
218823607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
218901d7584cSLiam Girdwood {
219001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
219101d7584cSLiam Girdwood 
219201d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
21938d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
219401d7584cSLiam Girdwood 
219501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
219601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
219701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
219801d7584cSLiam Girdwood 
219901d7584cSLiam Girdwood 		/* is this op for this BE ? */
220001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
220101d7584cSLiam Girdwood 			continue;
220201d7584cSLiam Girdwood 
220301d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
2204103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
220501d7584cSLiam Girdwood 				stream ? "capture" : "playback",
220601d7584cSLiam Girdwood 				be->dpcm[stream].state);
220701d7584cSLiam Girdwood 
220801d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
220901d7584cSLiam Girdwood 			continue;
221001d7584cSLiam Girdwood 
221101d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
22129c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
22139c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
22149c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
22159c0ac70aSKai Chieh Chuang 		}
221601d7584cSLiam Girdwood 
2217103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
221894d215ccS彭东林 			be->dai_link->name);
221901d7584cSLiam Girdwood 
222001d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
222101d7584cSLiam Girdwood 		be_substream->runtime = NULL;
222201d7584cSLiam Girdwood 
222301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
222401d7584cSLiam Girdwood 	}
222501d7584cSLiam Girdwood 	return 0;
222601d7584cSLiam Girdwood }
222701d7584cSLiam Girdwood 
222801d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
222901d7584cSLiam Girdwood {
223001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
223101d7584cSLiam Girdwood 	int stream = substream->stream;
223201d7584cSLiam Girdwood 
2233ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
223401d7584cSLiam Girdwood 
223501d7584cSLiam Girdwood 	/* shutdown the BEs */
2236*25c2f515SKuninori Morimoto 	dpcm_be_dai_shutdown(fe, stream);
223701d7584cSLiam Girdwood 
2238103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
223901d7584cSLiam Girdwood 
224001d7584cSLiam Girdwood 	/* now shutdown the frontend */
224101d7584cSLiam Girdwood 	soc_pcm_close(substream);
224201d7584cSLiam Girdwood 
224301d7584cSLiam Girdwood 	/* run the stream event for each BE */
22441c531230SKuninori Morimoto 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
224501d7584cSLiam Girdwood 
224601d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2247ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
224801d7584cSLiam Girdwood 	return 0;
224901d7584cSLiam Girdwood }
225001d7584cSLiam Girdwood 
225123607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
225201d7584cSLiam Girdwood {
225301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
225401d7584cSLiam Girdwood 
225501d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
225601d7584cSLiam Girdwood 	 * to this frontend DAI */
22578d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
225801d7584cSLiam Girdwood 
225901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
226001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
226101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
226201d7584cSLiam Girdwood 
226301d7584cSLiam Girdwood 		/* is this op for this BE ? */
226401d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
226501d7584cSLiam Girdwood 			continue;
226601d7584cSLiam Girdwood 
226701d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
226801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
226901d7584cSLiam Girdwood 				continue;
227001d7584cSLiam Girdwood 
227136fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
227236fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
227336fba62cSQiao Zhou 			continue;
227436fba62cSQiao Zhou 
227501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
227601d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
227701d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
227808b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
22795e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
22805e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
228101d7584cSLiam Girdwood 			continue;
228201d7584cSLiam Girdwood 
2283103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
228494d215ccS彭东林 			be->dai_link->name);
228501d7584cSLiam Girdwood 
228601d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
228701d7584cSLiam Girdwood 
228801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
228901d7584cSLiam Girdwood 	}
229001d7584cSLiam Girdwood 
229101d7584cSLiam Girdwood 	return 0;
229201d7584cSLiam Girdwood }
229301d7584cSLiam Girdwood 
229445c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
229501d7584cSLiam Girdwood {
229601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
229701d7584cSLiam Girdwood 	int err, stream = substream->stream;
229801d7584cSLiam Girdwood 
229901d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2300ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
230101d7584cSLiam Girdwood 
2302103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
230301d7584cSLiam Girdwood 
230401d7584cSLiam Girdwood 	/* call hw_free on the frontend */
230501d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
230601d7584cSLiam Girdwood 	if (err < 0)
2307103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
230801d7584cSLiam Girdwood 			fe->dai_link->name);
230901d7584cSLiam Girdwood 
231001d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
231101d7584cSLiam Girdwood 	 * to this frontend DAI */
231201d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
231301d7584cSLiam Girdwood 
231401d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2315ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
231601d7584cSLiam Girdwood 
231701d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
231801d7584cSLiam Girdwood 	return 0;
231901d7584cSLiam Girdwood }
232001d7584cSLiam Girdwood 
232123607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
232201d7584cSLiam Girdwood {
232301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
232401d7584cSLiam Girdwood 	int ret;
232501d7584cSLiam Girdwood 
23268d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
232701d7584cSLiam Girdwood 
232801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
232901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
233001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
233101d7584cSLiam Girdwood 
233201d7584cSLiam Girdwood 		/* is this op for this BE ? */
233301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
233401d7584cSLiam Girdwood 			continue;
233501d7584cSLiam Girdwood 
233601d7584cSLiam Girdwood 		/* copy params for each dpcm */
233701d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
233801d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
233901d7584cSLiam Girdwood 
234001d7584cSLiam Girdwood 		/* perform any hw_params fixups */
234101d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
234201d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
234301d7584cSLiam Girdwood 					&dpcm->hw_params);
234401d7584cSLiam Girdwood 			if (ret < 0) {
234501d7584cSLiam Girdwood 				dev_err(be->dev,
2346103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
234701d7584cSLiam Girdwood 					ret);
234801d7584cSLiam Girdwood 				goto unwind;
234901d7584cSLiam Girdwood 			}
235001d7584cSLiam Girdwood 		}
235101d7584cSLiam Girdwood 
2352ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2353ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2354ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2355ae061d2aSLibin Yang 
2356b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2357b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2358b0639bd2SKuninori Morimoto 			continue;
2359b0639bd2SKuninori Morimoto 
2360b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2361b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2362b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2363b0639bd2SKuninori Morimoto 			continue;
2364b0639bd2SKuninori Morimoto 
2365b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
236694d215ccS彭东林 			be->dai_link->name);
2367b0639bd2SKuninori Morimoto 
236801d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
236901d7584cSLiam Girdwood 		if (ret < 0) {
237001d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2371103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
237201d7584cSLiam Girdwood 			goto unwind;
237301d7584cSLiam Girdwood 		}
237401d7584cSLiam Girdwood 
237501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
237601d7584cSLiam Girdwood 	}
237701d7584cSLiam Girdwood 	return 0;
237801d7584cSLiam Girdwood 
237901d7584cSLiam Girdwood unwind:
238001d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
23818d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
238201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
238301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
238401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
238501d7584cSLiam Girdwood 
238601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
238701d7584cSLiam Girdwood 			continue;
238801d7584cSLiam Girdwood 
238901d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
239001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
239101d7584cSLiam Girdwood 			continue;
239201d7584cSLiam Girdwood 
239301d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
239401d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
239501d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
239601d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
239701d7584cSLiam Girdwood 			continue;
239801d7584cSLiam Girdwood 
239901d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
240001d7584cSLiam Girdwood 	}
240101d7584cSLiam Girdwood 
240201d7584cSLiam Girdwood 	return ret;
240301d7584cSLiam Girdwood }
240401d7584cSLiam Girdwood 
240545c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
240601d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
240701d7584cSLiam Girdwood {
240801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
240901d7584cSLiam Girdwood 	int ret, stream = substream->stream;
241001d7584cSLiam Girdwood 
241101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2412ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
241301d7584cSLiam Girdwood 
2414*25c2f515SKuninori Morimoto 	memcpy(&fe->dpcm[stream].hw_params, params,
241501d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
2416*25c2f515SKuninori Morimoto 	ret = dpcm_be_dai_hw_params(fe, stream);
241701d7584cSLiam Girdwood 	if (ret < 0) {
2418103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
241901d7584cSLiam Girdwood 		goto out;
242001d7584cSLiam Girdwood 	}
242101d7584cSLiam Girdwood 
2422103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
242301d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
242401d7584cSLiam Girdwood 			params_channels(params), params_format(params));
242501d7584cSLiam Girdwood 
242601d7584cSLiam Girdwood 	/* call hw_params on the frontend */
242701d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
242801d7584cSLiam Girdwood 	if (ret < 0) {
2429103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
243001d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
243101d7584cSLiam Girdwood 	 } else
243201d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
243301d7584cSLiam Girdwood 
243401d7584cSLiam Girdwood out:
2435ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
243601d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
243701d7584cSLiam Girdwood 	return ret;
243801d7584cSLiam Girdwood }
243901d7584cSLiam Girdwood 
244001d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
244101d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
244201d7584cSLiam Girdwood {
244301d7584cSLiam Girdwood 	int ret;
244401d7584cSLiam Girdwood 
2445103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
244694d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
244701d7584cSLiam Girdwood 
244801d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
244901d7584cSLiam Girdwood 	if (ret < 0)
2450103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
245101d7584cSLiam Girdwood 
245201d7584cSLiam Girdwood 	return ret;
245301d7584cSLiam Girdwood }
245401d7584cSLiam Girdwood 
245523607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
245645c0a188SMark Brown 			       int cmd)
245701d7584cSLiam Girdwood {
245801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
245901d7584cSLiam Girdwood 	int ret = 0;
246001d7584cSLiam Girdwood 
24618d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
246201d7584cSLiam Girdwood 
246301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
246401d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
246501d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
246601d7584cSLiam Girdwood 
246701d7584cSLiam Girdwood 		/* is this op for this BE ? */
246801d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
246901d7584cSLiam Girdwood 			continue;
247001d7584cSLiam Girdwood 
247101d7584cSLiam Girdwood 		switch (cmd) {
247201d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
247301d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
247401d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
247501d7584cSLiam Girdwood 				continue;
247601d7584cSLiam Girdwood 
247701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
247801d7584cSLiam Girdwood 			if (ret)
247901d7584cSLiam Girdwood 				return ret;
248001d7584cSLiam Girdwood 
248101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
248201d7584cSLiam Girdwood 			break;
248301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
248401d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
248501d7584cSLiam Girdwood 				continue;
248601d7584cSLiam Girdwood 
248701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
248801d7584cSLiam Girdwood 			if (ret)
248901d7584cSLiam Girdwood 				return ret;
249001d7584cSLiam Girdwood 
249101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
249201d7584cSLiam Girdwood 			break;
249301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
249401d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
249501d7584cSLiam Girdwood 				continue;
249601d7584cSLiam Girdwood 
249701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
249801d7584cSLiam Girdwood 			if (ret)
249901d7584cSLiam Girdwood 				return ret;
250001d7584cSLiam Girdwood 
250101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
250201d7584cSLiam Girdwood 			break;
250301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
250401d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
250501d7584cSLiam Girdwood 				continue;
250601d7584cSLiam Girdwood 
250701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
250801d7584cSLiam Girdwood 				continue;
250901d7584cSLiam Girdwood 
251001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
251101d7584cSLiam Girdwood 			if (ret)
251201d7584cSLiam Girdwood 				return ret;
251301d7584cSLiam Girdwood 
251401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
251501d7584cSLiam Girdwood 			break;
251601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2517868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
251801d7584cSLiam Girdwood 				continue;
251901d7584cSLiam Girdwood 
252001d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
252101d7584cSLiam Girdwood 				continue;
252201d7584cSLiam Girdwood 
252301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
252401d7584cSLiam Girdwood 			if (ret)
252501d7584cSLiam Girdwood 				return ret;
252601d7584cSLiam Girdwood 
252701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
252801d7584cSLiam Girdwood 			break;
252901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
253001d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
253101d7584cSLiam Girdwood 				continue;
253201d7584cSLiam Girdwood 
253301d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
253401d7584cSLiam Girdwood 				continue;
253501d7584cSLiam Girdwood 
253601d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
253701d7584cSLiam Girdwood 			if (ret)
253801d7584cSLiam Girdwood 				return ret;
253901d7584cSLiam Girdwood 
254001d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
254101d7584cSLiam Girdwood 			break;
254201d7584cSLiam Girdwood 		}
254301d7584cSLiam Girdwood 	}
254401d7584cSLiam Girdwood 
254501d7584cSLiam Girdwood 	return ret;
254601d7584cSLiam Girdwood }
254701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
254801d7584cSLiam Girdwood 
2549acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2550acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2551acbf2774SRanjani Sridharan {
2552acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2553acbf2774SRanjani Sridharan 	int ret;
2554acbf2774SRanjani Sridharan 
2555acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2556acbf2774SRanjani Sridharan 	if (fe_first) {
2557acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2558acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2559acbf2774SRanjani Sridharan 
2560acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2561acbf2774SRanjani Sridharan 		if (ret < 0)
2562acbf2774SRanjani Sridharan 			return ret;
2563acbf2774SRanjani Sridharan 
2564acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2565acbf2774SRanjani Sridharan 		return ret;
2566acbf2774SRanjani Sridharan 	}
2567acbf2774SRanjani Sridharan 
2568acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2569acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2570acbf2774SRanjani Sridharan 	if (ret < 0)
2571acbf2774SRanjani Sridharan 		return ret;
2572acbf2774SRanjani Sridharan 
2573acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2574acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2575acbf2774SRanjani Sridharan 
2576acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2577acbf2774SRanjani Sridharan 
2578acbf2774SRanjani Sridharan 	return ret;
2579acbf2774SRanjani Sridharan }
2580acbf2774SRanjani Sridharan 
2581ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
258201d7584cSLiam Girdwood {
258301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2584acbf2774SRanjani Sridharan 	int stream = substream->stream;
2585acbf2774SRanjani Sridharan 	int ret = 0;
258601d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
258701d7584cSLiam Girdwood 
258801d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
258901d7584cSLiam Girdwood 
259001d7584cSLiam Girdwood 	switch (trigger) {
259101d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2592acbf2774SRanjani Sridharan 		switch (cmd) {
2593acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2594acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2595acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2596acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2597acbf2774SRanjani Sridharan 			break;
2598acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2599acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2600acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2601acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2602acbf2774SRanjani Sridharan 			break;
2603acbf2774SRanjani Sridharan 		default:
2604acbf2774SRanjani Sridharan 			ret = -EINVAL;
2605acbf2774SRanjani Sridharan 			break;
260601d7584cSLiam Girdwood 		}
260701d7584cSLiam Girdwood 		break;
260801d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2609acbf2774SRanjani Sridharan 		switch (cmd) {
2610acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2611acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2612acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2613acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2614acbf2774SRanjani Sridharan 			break;
2615acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2616acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2617acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2618acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2619acbf2774SRanjani Sridharan 			break;
2620acbf2774SRanjani Sridharan 		default:
2621acbf2774SRanjani Sridharan 			ret = -EINVAL;
2622acbf2774SRanjani Sridharan 			break;
262301d7584cSLiam Girdwood 		}
262401d7584cSLiam Girdwood 		break;
262507bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
262607bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
262707bf84aaSLiam Girdwood 
2628103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
262907bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
263007bf84aaSLiam Girdwood 
263107bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
263207bf84aaSLiam Girdwood 		break;
263301d7584cSLiam Girdwood 	default:
2634103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
263501d7584cSLiam Girdwood 				fe->dai_link->name);
263601d7584cSLiam Girdwood 		ret = -EINVAL;
263701d7584cSLiam Girdwood 		goto out;
263801d7584cSLiam Girdwood 	}
263901d7584cSLiam Girdwood 
2640acbf2774SRanjani Sridharan 	if (ret < 0) {
2641acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2642acbf2774SRanjani Sridharan 			cmd, ret);
2643acbf2774SRanjani Sridharan 		goto out;
2644acbf2774SRanjani Sridharan 	}
2645acbf2774SRanjani Sridharan 
264601d7584cSLiam Girdwood 	switch (cmd) {
264701d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
264801d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
264901d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
265001d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
265101d7584cSLiam Girdwood 		break;
265201d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
265301d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
265401d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
265501d7584cSLiam Girdwood 		break;
26569f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
26579f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
26589f169b9fSPatrick Lai 		break;
265901d7584cSLiam Girdwood 	}
266001d7584cSLiam Girdwood 
266101d7584cSLiam Girdwood out:
266201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
266301d7584cSLiam Girdwood 	return ret;
266401d7584cSLiam Girdwood }
266501d7584cSLiam Girdwood 
2666ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2667ea9d0d77STakashi Iwai {
2668ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2669ea9d0d77STakashi Iwai 	int stream = substream->stream;
2670ea9d0d77STakashi Iwai 
2671ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2672ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2673ea9d0d77STakashi Iwai 	 */
2674ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2675ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2676ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2677ea9d0d77STakashi Iwai 	}
2678ea9d0d77STakashi Iwai 
2679ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2680ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2681ea9d0d77STakashi Iwai }
2682ea9d0d77STakashi Iwai 
268323607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
268401d7584cSLiam Girdwood {
268501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
268601d7584cSLiam Girdwood 	int ret = 0;
268701d7584cSLiam Girdwood 
26888d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
268901d7584cSLiam Girdwood 
269001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
269101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
269201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
269301d7584cSLiam Girdwood 
269401d7584cSLiam Girdwood 		/* is this op for this BE ? */
269501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
269601d7584cSLiam Girdwood 			continue;
269701d7584cSLiam Girdwood 
269801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
269995f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
27005087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
27015087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
270201d7584cSLiam Girdwood 			continue;
270301d7584cSLiam Girdwood 
2704103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
270594d215ccS彭东林 			be->dai_link->name);
270601d7584cSLiam Girdwood 
270701d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
270801d7584cSLiam Girdwood 		if (ret < 0) {
2709103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
271001d7584cSLiam Girdwood 				ret);
271101d7584cSLiam Girdwood 			break;
271201d7584cSLiam Girdwood 		}
271301d7584cSLiam Girdwood 
271401d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
271501d7584cSLiam Girdwood 	}
271601d7584cSLiam Girdwood 	return ret;
271701d7584cSLiam Girdwood }
271801d7584cSLiam Girdwood 
271945c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
272001d7584cSLiam Girdwood {
272101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
272201d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
272301d7584cSLiam Girdwood 
272401d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
272501d7584cSLiam Girdwood 
2726103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
272701d7584cSLiam Girdwood 
2728ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
272901d7584cSLiam Girdwood 
273001d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
273101d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2732103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
273301d7584cSLiam Girdwood 				fe->dai_link->name);
273401d7584cSLiam Girdwood 		ret = -EINVAL;
273501d7584cSLiam Girdwood 		goto out;
273601d7584cSLiam Girdwood 	}
273701d7584cSLiam Girdwood 
2738*25c2f515SKuninori Morimoto 	ret = dpcm_be_dai_prepare(fe, stream);
273901d7584cSLiam Girdwood 	if (ret < 0)
274001d7584cSLiam Girdwood 		goto out;
274101d7584cSLiam Girdwood 
274201d7584cSLiam Girdwood 	/* call prepare on the frontend */
274301d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
274401d7584cSLiam Girdwood 	if (ret < 0) {
2745103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
274601d7584cSLiam Girdwood 			fe->dai_link->name);
274701d7584cSLiam Girdwood 		goto out;
274801d7584cSLiam Girdwood 	}
274901d7584cSLiam Girdwood 
275001d7584cSLiam Girdwood 	/* run the stream event for each BE */
275101d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
275201d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
275301d7584cSLiam Girdwood 
275401d7584cSLiam Girdwood out:
2755ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
275601d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
275701d7584cSLiam Girdwood 
275801d7584cSLiam Girdwood 	return ret;
275901d7584cSLiam Girdwood }
276001d7584cSLiam Girdwood 
2761618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2762618dae11SLiam Girdwood {
276307bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
276407bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
276507bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2766618dae11SLiam Girdwood 	int err;
276701d7584cSLiam Girdwood 
2768103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2769618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2770618dae11SLiam Girdwood 
277107bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
277207bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2773103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
277407bf84aaSLiam Girdwood 				fe->dai_link->name);
277507bf84aaSLiam Girdwood 
277607bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
277707bf84aaSLiam Girdwood 		if (err < 0)
2778103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
277907bf84aaSLiam Girdwood 	} else {
2780103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
278107bf84aaSLiam Girdwood 			fe->dai_link->name);
278207bf84aaSLiam Girdwood 
2783618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2784618dae11SLiam Girdwood 		if (err < 0)
2785103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
278607bf84aaSLiam Girdwood 	}
2787618dae11SLiam Girdwood 
2788618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2789618dae11SLiam Girdwood 	if (err < 0)
2790103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2791618dae11SLiam Girdwood 
2792618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2793618dae11SLiam Girdwood 	if (err < 0)
2794103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2795618dae11SLiam Girdwood 
2796618dae11SLiam Girdwood 	/* run the stream event for each BE */
2797618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2798618dae11SLiam Girdwood 
2799618dae11SLiam Girdwood 	return 0;
2800618dae11SLiam Girdwood }
2801618dae11SLiam Girdwood 
2802618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2803618dae11SLiam Girdwood {
280407bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
280507bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2806618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
280707bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2808618dae11SLiam Girdwood 	int ret;
2809a9764869SKaiChieh Chuang 	unsigned long flags;
2810618dae11SLiam Girdwood 
2811103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2812618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2813618dae11SLiam Girdwood 
2814618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2815618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2816618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2817618dae11SLiam Girdwood 		return -EINVAL;
2818618dae11SLiam Girdwood 
2819618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2820618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2821fffc0ca2SDan Carpenter 	if (ret < 0)
2822618dae11SLiam Girdwood 		goto disconnect;
2823618dae11SLiam Girdwood 
2824618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2825618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2826618dae11SLiam Girdwood 		return 0;
2827618dae11SLiam Girdwood 
2828618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2829fffc0ca2SDan Carpenter 	if (ret < 0)
2830618dae11SLiam Girdwood 		goto close;
2831618dae11SLiam Girdwood 
2832618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2833618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2834618dae11SLiam Girdwood 		return 0;
2835618dae11SLiam Girdwood 
2836618dae11SLiam Girdwood 
2837618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2838fffc0ca2SDan Carpenter 	if (ret < 0)
2839618dae11SLiam Girdwood 		goto hw_free;
2840618dae11SLiam Girdwood 
2841618dae11SLiam Girdwood 	/* run the stream event for each BE */
2842618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2843618dae11SLiam Girdwood 
2844618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2845618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2846618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2847618dae11SLiam Girdwood 		return 0;
2848618dae11SLiam Girdwood 
284907bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
285007bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2851103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
285207bf84aaSLiam Girdwood 				fe->dai_link->name);
285307bf84aaSLiam Girdwood 
285407bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
285507bf84aaSLiam Girdwood 		if (ret < 0) {
2856103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
285707bf84aaSLiam Girdwood 			goto hw_free;
285807bf84aaSLiam Girdwood 		}
285907bf84aaSLiam Girdwood 	} else {
2860103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2861618dae11SLiam Girdwood 			fe->dai_link->name);
2862618dae11SLiam Girdwood 
2863618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2864618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2865618dae11SLiam Girdwood 		if (ret < 0) {
2866103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2867618dae11SLiam Girdwood 			goto hw_free;
2868618dae11SLiam Girdwood 		}
286907bf84aaSLiam Girdwood 	}
2870618dae11SLiam Girdwood 
2871618dae11SLiam Girdwood 	return 0;
2872618dae11SLiam Girdwood 
2873618dae11SLiam Girdwood hw_free:
2874618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2875618dae11SLiam Girdwood close:
2876618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2877618dae11SLiam Girdwood disconnect:
2878618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2879a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
28808d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2881618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2882618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2883618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2884618dae11SLiam Girdwood 	}
2885a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2886618dae11SLiam Girdwood 
2887618dae11SLiam Girdwood 	return ret;
2888618dae11SLiam Girdwood }
2889618dae11SLiam Girdwood 
2890de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2891618dae11SLiam Girdwood {
2892618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
28937083f877SKuninori Morimoto 	int stream;
2894de15d7ffSJerome Brunet 	int count, paths;
2895580dff36SKuninori Morimoto 	int ret;
2896618dae11SLiam Girdwood 
28976e1276a5SBard Liao 	if (fe->num_cpus > 1) {
28986e1276a5SBard Liao 		dev_err(fe->dev,
28996e1276a5SBard Liao 			"%s doesn't support Multi CPU yet\n", __func__);
29006e1276a5SBard Liao 		return -EINVAL;
29016e1276a5SBard Liao 	}
29026e1276a5SBard Liao 
2903618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2904de15d7ffSJerome Brunet 		return 0;
2905618dae11SLiam Girdwood 
2906618dae11SLiam Girdwood 	/* only check active links */
2907618dae11SLiam Girdwood 	if (!fe->cpu_dai->active)
2908de15d7ffSJerome Brunet 		return 0;
2909618dae11SLiam Girdwood 
2910618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2911de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2912de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2913618dae11SLiam Girdwood 
29147083f877SKuninori Morimoto 	for_each_pcm_streams(stream) {
2915075207d2SQiao Zhou 
29167083f877SKuninori Morimoto 		/* skip if FE doesn't have playback/capture capability */
29177083f877SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(fe->cpu_dai,   stream) ||
29187083f877SKuninori Morimoto 		    !snd_soc_dai_stream_valid(fe->codec_dai, stream))
29197083f877SKuninori Morimoto 			continue;
2920618dae11SLiam Girdwood 
29217083f877SKuninori Morimoto 		/* skip if FE isn't currently playing/capturing */
29227083f877SKuninori Morimoto 		if (!fe->cpu_dai->stream_active[stream] ||
29237083f877SKuninori Morimoto 		    !fe->codec_dai->stream_active[stream])
29247083f877SKuninori Morimoto 			continue;
29257083f877SKuninori Morimoto 
29267083f877SKuninori Morimoto 		paths = dpcm_path_get(fe, stream, &list);
2927618dae11SLiam Girdwood 		if (paths < 0) {
2928103d84a3SLiam Girdwood 			dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
29297083f877SKuninori Morimoto 				 fe->dai_link->name,
29307083f877SKuninori Morimoto 				 stream == SNDRV_PCM_STREAM_PLAYBACK ?
29317083f877SKuninori Morimoto 				 "playback" : "capture");
2932618dae11SLiam Girdwood 			return paths;
2933618dae11SLiam Girdwood 		}
2934618dae11SLiam Girdwood 
29357083f877SKuninori Morimoto 		/* update any playback/capture paths */
29367083f877SKuninori Morimoto 		count = dpcm_process_paths(fe, stream, &list, new);
2937de15d7ffSJerome Brunet 		if (count) {
2938580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2939de15d7ffSJerome Brunet 			if (new)
2940580dff36SKuninori Morimoto 				ret = dpcm_run_update_startup(fe, stream);
2941de15d7ffSJerome Brunet 			else
2942580dff36SKuninori Morimoto 				ret = dpcm_run_update_shutdown(fe, stream);
2943580dff36SKuninori Morimoto 			if (ret < 0)
2944580dff36SKuninori Morimoto 				dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2945580dff36SKuninori Morimoto 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2946de15d7ffSJerome Brunet 
29477083f877SKuninori Morimoto 			dpcm_clear_pending_state(fe, stream);
29487083f877SKuninori Morimoto 			dpcm_be_disconnect(fe, stream);
2949618dae11SLiam Girdwood 		}
2950618dae11SLiam Girdwood 
29517ed9de76SQiao Zhou 		dpcm_path_put(&list);
2952618dae11SLiam Girdwood 	}
2953618dae11SLiam Girdwood 
2954de15d7ffSJerome Brunet 	return 0;
2955618dae11SLiam Girdwood }
2956618dae11SLiam Girdwood 
2957de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2958de15d7ffSJerome Brunet  * any DAI links.
2959de15d7ffSJerome Brunet  */
2960de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card)
2961de15d7ffSJerome Brunet {
2962de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2963de15d7ffSJerome Brunet 	int ret = 0;
2964de15d7ffSJerome Brunet 
2965de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2966de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2967bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2968de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2969de15d7ffSJerome Brunet 		if (ret)
2970de15d7ffSJerome Brunet 			goto out;
2971de15d7ffSJerome Brunet 	}
2972de15d7ffSJerome Brunet 
2973de15d7ffSJerome Brunet 	/* bring new paths up */
2974bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2975de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2976de15d7ffSJerome Brunet 		if (ret)
2977de15d7ffSJerome Brunet 			goto out;
2978de15d7ffSJerome Brunet 	}
2979de15d7ffSJerome Brunet 
2980de15d7ffSJerome Brunet out:
2981618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2982de15d7ffSJerome Brunet 	return ret;
2983618dae11SLiam Girdwood }
298401d7584cSLiam Girdwood 
298545c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
298601d7584cSLiam Girdwood {
298701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
298801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
298901d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
299001d7584cSLiam Girdwood 	int ret;
299101d7584cSLiam Girdwood 	int stream = fe_substream->stream;
299201d7584cSLiam Girdwood 
299301d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
299401d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
299501d7584cSLiam Girdwood 
29968f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
29978f70e515SQiao Zhou 	if (ret < 0) {
2998cae06eb9SKuninori Morimoto 		goto open_end;
29998f70e515SQiao Zhou 	} else if (ret == 0) {
3000103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
300101d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
300201d7584cSLiam Girdwood 	}
300301d7584cSLiam Girdwood 
300401d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
300501d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
300601d7584cSLiam Girdwood 
300701d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
300801d7584cSLiam Girdwood 	if (ret < 0) {
300901d7584cSLiam Girdwood 		/* clean up all links */
30108d6258a4SKuninori Morimoto 		for_each_dpcm_be(fe, stream, dpcm)
301101d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
301201d7584cSLiam Girdwood 
301301d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
301401d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
301501d7584cSLiam Girdwood 	}
301601d7584cSLiam Girdwood 
301701d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
301801d7584cSLiam Girdwood 	dpcm_path_put(&list);
3019cae06eb9SKuninori Morimoto open_end:
302001d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
302101d7584cSLiam Girdwood 	return ret;
302201d7584cSLiam Girdwood }
302301d7584cSLiam Girdwood 
302445c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
302501d7584cSLiam Girdwood {
302601d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
302701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
302801d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
302901d7584cSLiam Girdwood 
303001d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
303101d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
303201d7584cSLiam Girdwood 
303301d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
30348d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
303501d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
303601d7584cSLiam Girdwood 
303701d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
303801d7584cSLiam Girdwood 
303901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
304001d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
304101d7584cSLiam Girdwood 	return ret;
304201d7584cSLiam Girdwood }
304301d7584cSLiam Girdwood 
3044ddee627cSLiam Girdwood /* create a new pcm */
3045ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3046ddee627cSLiam Girdwood {
30472e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
304819bdcc7aSShreyas NC 	struct snd_soc_dai *cpu_dai;
30492b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
3050ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
3051ddee627cSLiam Girdwood 	char new_name[64];
3052ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
30532e5894d7SBenoit Cousson 	int i;
3054ddee627cSLiam Girdwood 
305501d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
30561e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
30571e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
305801d7584cSLiam Girdwood 	} else {
3059a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
3060a4877a6fSStephan Gerhold 		int cpu_capture = rtd->dai_link->params ?
3061a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
3062a4877a6fSStephan Gerhold 		int cpu_playback = rtd->dai_link->params ?
3063a4877a6fSStephan Gerhold 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
3064a342031cSJerome Brunet 
30650b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
306619bdcc7aSShreyas NC 			if (rtd->num_cpus == 1) {
306719bdcc7aSShreyas NC 				cpu_dai = rtd->cpu_dais[0];
306819bdcc7aSShreyas NC 			} else if (rtd->num_cpus == rtd->num_codecs) {
306919bdcc7aSShreyas NC 				cpu_dai = rtd->cpu_dais[i];
307019bdcc7aSShreyas NC 			} else {
307119bdcc7aSShreyas NC 				dev_err(rtd->card->dev,
307219bdcc7aSShreyas NC 					"N cpus to M codecs link is not supported yet\n");
307319bdcc7aSShreyas NC 				return -EINVAL;
307419bdcc7aSShreyas NC 			}
307519bdcc7aSShreyas NC 
3076467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
3077a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
3078ddee627cSLiam Girdwood 				playback = 1;
3079467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
3080a4877a6fSStephan Gerhold 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
3081ddee627cSLiam Girdwood 				capture = 1;
308201d7584cSLiam Girdwood 		}
30832e5894d7SBenoit Cousson 	}
30842e5894d7SBenoit Cousson 
3085d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
3086d6bead02SFabio Estevam 		playback = 1;
3087d6bead02SFabio Estevam 		capture = 0;
3088d6bead02SFabio Estevam 	}
3089d6bead02SFabio Estevam 
3090d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
3091d6bead02SFabio Estevam 		playback = 0;
3092d6bead02SFabio Estevam 		capture = 1;
3093d6bead02SFabio Estevam 	}
3094d6bead02SFabio Estevam 
309501d7584cSLiam Girdwood 	/* create the PCM */
3096a342031cSJerome Brunet 	if (rtd->dai_link->params) {
3097a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
3098a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
3099a342031cSJerome Brunet 
3100a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3101a342031cSJerome Brunet 					   playback, capture, &pcm);
3102a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
310301d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
310401d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
310501d7584cSLiam Girdwood 
310601d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
310701d7584cSLiam Girdwood 				playback, capture, &pcm);
310801d7584cSLiam Girdwood 	} else {
310901d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
311001d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
311101d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
311201d7584cSLiam Girdwood 		else
311301d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
31142e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
31152e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
31162e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
311701d7584cSLiam Girdwood 
311801d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
311901d7584cSLiam Girdwood 			capture, &pcm);
312001d7584cSLiam Girdwood 	}
3121ddee627cSLiam Girdwood 	if (ret < 0) {
3122103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
31235cb9b748SLiam Girdwood 			rtd->dai_link->name);
3124ddee627cSLiam Girdwood 		return ret;
3125ddee627cSLiam Girdwood 	}
3126103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3127ddee627cSLiam Girdwood 
3128ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
3129a342031cSJerome Brunet 	if (rtd->dai_link->params)
31304bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
3131a342031cSJerome Brunet 	else
313283f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
3133ddee627cSLiam Girdwood 
313448c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
3135ddee627cSLiam Girdwood 	rtd->pcm = pcm;
3136ddee627cSLiam Girdwood 	pcm->private_data = rtd;
313701d7584cSLiam Girdwood 
3138a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
313901d7584cSLiam Girdwood 		if (playback)
314001d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
314101d7584cSLiam Girdwood 		if (capture)
314201d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
314301d7584cSLiam Girdwood 		goto out;
314401d7584cSLiam Girdwood 	}
314501d7584cSLiam Girdwood 
314601d7584cSLiam Girdwood 	/* ASoC PCM operations */
314701d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
314801d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
314901d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
315001d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
315101d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
315201d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
315301d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
315401d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
315501d7584cSLiam Girdwood 	} else {
315601d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
315701d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
315801d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
315901d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
316001d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
316101d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
316201d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
316301d7584cSLiam Girdwood 	}
316401d7584cSLiam Girdwood 
3165613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
31662b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
3167b8135864SKuninori Morimoto 
31683b1c952cSTakashi Iwai 		if (drv->ioctl)
31693b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
31701e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
31711e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
3172e9067bb5SKuninori Morimoto 		if (drv->copy_user)
317382d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
3174e9067bb5SKuninori Morimoto 		if (drv->page)
31759c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
3176e9067bb5SKuninori Morimoto 		if (drv->mmap)
3177205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
3178b8135864SKuninori Morimoto 	}
3179b8135864SKuninori Morimoto 
3180ddee627cSLiam Girdwood 	if (playback)
318101d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3182ddee627cSLiam Girdwood 
3183ddee627cSLiam Girdwood 	if (capture)
318401d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3185ddee627cSLiam Girdwood 
3186b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
3187ddee627cSLiam Girdwood 	if (ret < 0) {
31887484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3189ddee627cSLiam Girdwood 		return ret;
3190ddee627cSLiam Girdwood 	}
3191c641e5b2SJohan Hovold 
31923d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
319301d7584cSLiam Girdwood out:
31942e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
31952e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
319619bdcc7aSShreyas NC 		 (rtd->num_cpus > 1) ? "multicpu" : rtd->cpu_dai->name);
3197ddee627cSLiam Girdwood 	return ret;
3198ddee627cSLiam Girdwood }
319901d7584cSLiam Girdwood 
320001d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
320101d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
320201d7584cSLiam Girdwood {
320301d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
320401d7584cSLiam Girdwood 		return 1;
320501d7584cSLiam Girdwood 	return 0;
320601d7584cSLiam Girdwood }
320701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
320801d7584cSLiam Girdwood 
320901d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
321001d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
321101d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
321201d7584cSLiam Girdwood {
321301d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
321401d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
321501d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
321601d7584cSLiam Girdwood 		return 1;
321701d7584cSLiam Girdwood 	return 0;
321801d7584cSLiam Girdwood }
321901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
322001d7584cSLiam Girdwood 
322101d7584cSLiam Girdwood /* get the substream for this BE */
322201d7584cSLiam Girdwood struct snd_pcm_substream *
322301d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
322401d7584cSLiam Girdwood {
322501d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
322601d7584cSLiam Girdwood }
322701d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
322801d7584cSLiam Girdwood 
3229085d22beSKuninori Morimoto static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3230085d22beSKuninori Morimoto 				    struct snd_soc_pcm_runtime *be,
3231085d22beSKuninori Morimoto 				    int stream,
3232085d22beSKuninori Morimoto 				    const enum snd_soc_dpcm_state *states,
3233085d22beSKuninori Morimoto 				    int num_states)
323401d7584cSLiam Girdwood {
323501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
323601d7584cSLiam Girdwood 	int state;
3237a9764869SKaiChieh Chuang 	int ret = 1;
3238a9764869SKaiChieh Chuang 	unsigned long flags;
3239085d22beSKuninori Morimoto 	int i;
324001d7584cSLiam Girdwood 
3241a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3242d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
324301d7584cSLiam Girdwood 
324401d7584cSLiam Girdwood 		if (dpcm->fe == fe)
324501d7584cSLiam Girdwood 			continue;
324601d7584cSLiam Girdwood 
324701d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
3248085d22beSKuninori Morimoto 		for (i = 0; i < num_states; i++) {
3249085d22beSKuninori Morimoto 			if (state == states[i]) {
3250a9764869SKaiChieh Chuang 				ret = 0;
3251a9764869SKaiChieh Chuang 				break;
325201d7584cSLiam Girdwood 			}
3253a9764869SKaiChieh Chuang 		}
3254085d22beSKuninori Morimoto 	}
3255a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
325601d7584cSLiam Girdwood 
3257085d22beSKuninori Morimoto 	/* it's safe to do this BE DAI */
3258a9764869SKaiChieh Chuang 	return ret;
325901d7584cSLiam Girdwood }
3260085d22beSKuninori Morimoto 
3261085d22beSKuninori Morimoto /*
3262085d22beSKuninori Morimoto  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3263085d22beSKuninori Morimoto  * are not running, paused or suspended for the specified stream direction.
3264085d22beSKuninori Morimoto  */
3265085d22beSKuninori Morimoto int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3266085d22beSKuninori Morimoto 		struct snd_soc_pcm_runtime *be, int stream)
3267085d22beSKuninori Morimoto {
3268085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3269085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3270085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3271085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3272085d22beSKuninori Morimoto 	};
3273085d22beSKuninori Morimoto 
3274085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3275085d22beSKuninori Morimoto }
327601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
327701d7584cSLiam Girdwood 
327801d7584cSLiam Girdwood /*
327901d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
328001d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
328101d7584cSLiam Girdwood  */
328201d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
328301d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
328401d7584cSLiam Girdwood {
3285085d22beSKuninori Morimoto 	const enum snd_soc_dpcm_state state[] = {
3286085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_START,
3287085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PAUSED,
3288085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_SUSPEND,
3289085d22beSKuninori Morimoto 		SND_SOC_DPCM_STATE_PREPARE,
3290085d22beSKuninori Morimoto 	};
329101d7584cSLiam Girdwood 
3292085d22beSKuninori Morimoto 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
329301d7584cSLiam Girdwood }
329401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3295