xref: /openbmc/linux/sound/soc/soc-pcm.c (revision e82ebffce3ec07584bcc2fc4c4d33a43fd9515f5)
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 
31f183f927SKuninori Morimoto static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
32f183f927SKuninori Morimoto 			   struct snd_pcm_substream *substream)
33f183f927SKuninori Morimoto {
34f183f927SKuninori Morimoto 	if (rtd->dai_link->ops &&
35f183f927SKuninori Morimoto 	    rtd->dai_link->ops->startup)
36f183f927SKuninori Morimoto 		return rtd->dai_link->ops->startup(substream);
37f183f927SKuninori Morimoto 	return 0;
38f183f927SKuninori Morimoto }
39f183f927SKuninori Morimoto 
400be429f9SKuninori Morimoto static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
410be429f9SKuninori Morimoto 			     struct snd_pcm_substream *substream)
420be429f9SKuninori Morimoto {
430be429f9SKuninori Morimoto 	if (rtd->dai_link->ops &&
440be429f9SKuninori Morimoto 	    rtd->dai_link->ops->shutdown)
450be429f9SKuninori Morimoto 		rtd->dai_link->ops->shutdown(substream);
460be429f9SKuninori Morimoto }
470be429f9SKuninori Morimoto 
4844c1a75bSKuninori Morimoto static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
4944c1a75bSKuninori Morimoto 			   struct snd_pcm_substream *substream)
5044c1a75bSKuninori Morimoto {
5144c1a75bSKuninori Morimoto 	if (rtd->dai_link->ops &&
5244c1a75bSKuninori Morimoto 	    rtd->dai_link->ops->prepare)
5344c1a75bSKuninori Morimoto 		return rtd->dai_link->ops->prepare(substream);
5444c1a75bSKuninori Morimoto 	return 0;
5544c1a75bSKuninori Morimoto }
5644c1a75bSKuninori Morimoto 
57de9ad990SKuninori Morimoto static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
58de9ad990SKuninori Morimoto 			     struct snd_pcm_substream *substream,
59de9ad990SKuninori Morimoto 			     struct snd_pcm_hw_params *params)
60de9ad990SKuninori Morimoto {
61de9ad990SKuninori Morimoto 	if (rtd->dai_link->ops &&
62de9ad990SKuninori Morimoto 	    rtd->dai_link->ops->hw_params)
63de9ad990SKuninori Morimoto 		return rtd->dai_link->ops->hw_params(substream, params);
64de9ad990SKuninori Morimoto 	return 0;
65de9ad990SKuninori Morimoto }
66de9ad990SKuninori Morimoto 
6749f020e5SKuninori Morimoto static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
6849f020e5SKuninori Morimoto 			    struct snd_pcm_substream *substream)
6949f020e5SKuninori Morimoto {
7049f020e5SKuninori Morimoto 	if (rtd->dai_link->ops &&
7149f020e5SKuninori Morimoto 	    rtd->dai_link->ops->hw_free)
7249f020e5SKuninori Morimoto 		rtd->dai_link->ops->hw_free(substream);
7349f020e5SKuninori Morimoto }
7449f020e5SKuninori Morimoto 
75ad2bf9f2SKuninori Morimoto static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
76ad2bf9f2SKuninori Morimoto 			   struct snd_pcm_substream *substream,
77ad2bf9f2SKuninori Morimoto 			   int cmd)
78ad2bf9f2SKuninori Morimoto {
79ad2bf9f2SKuninori Morimoto 	if (rtd->dai_link->ops &&
80ad2bf9f2SKuninori Morimoto 	    rtd->dai_link->ops->trigger)
81ad2bf9f2SKuninori Morimoto 		return rtd->dai_link->ops->trigger(substream, cmd);
82ad2bf9f2SKuninori Morimoto 	return 0;
83ad2bf9f2SKuninori Morimoto }
84ad2bf9f2SKuninori Morimoto 
857a5aaba4SKuninori Morimoto static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
867a5aaba4SKuninori Morimoto 				   int stream, int action)
877a5aaba4SKuninori Morimoto {
887a5aaba4SKuninori Morimoto 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
897a5aaba4SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
907a5aaba4SKuninori Morimoto 	int i;
917a5aaba4SKuninori Morimoto 
927a5aaba4SKuninori Morimoto 	lockdep_assert_held(&rtd->card->pcm_mutex);
937a5aaba4SKuninori Morimoto 
947a5aaba4SKuninori Morimoto 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
957a5aaba4SKuninori Morimoto 		cpu_dai->playback_active += action;
967a5aaba4SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
977a5aaba4SKuninori Morimoto 			codec_dai->playback_active += action;
987a5aaba4SKuninori Morimoto 	} else {
997a5aaba4SKuninori Morimoto 		cpu_dai->capture_active += action;
1007a5aaba4SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai)
1017a5aaba4SKuninori Morimoto 			codec_dai->capture_active += action;
1027a5aaba4SKuninori Morimoto 	}
1037a5aaba4SKuninori Morimoto 
1047a5aaba4SKuninori Morimoto 	cpu_dai->active += action;
1057a5aaba4SKuninori Morimoto 	cpu_dai->component->active += action;
1067a5aaba4SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
1077a5aaba4SKuninori Morimoto 		codec_dai->active += action;
1087a5aaba4SKuninori Morimoto 		codec_dai->component->active += action;
1097a5aaba4SKuninori Morimoto 	}
1107a5aaba4SKuninori Morimoto }
1117a5aaba4SKuninori Morimoto 
11290996f43SLars-Peter Clausen /**
11324894b76SLars-Peter Clausen  * snd_soc_runtime_activate() - Increment active count for PCM runtime components
11424894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is activated
11524894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
11624894b76SLars-Peter Clausen  *
11724894b76SLars-Peter Clausen  * Increments the active count for all the DAIs and components attached to a PCM
11824894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is opened.
11924894b76SLars-Peter Clausen  *
12072b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
12124894b76SLars-Peter Clausen  */
12224894b76SLars-Peter Clausen void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
12324894b76SLars-Peter Clausen {
1247a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, 1);
12524894b76SLars-Peter Clausen }
12624894b76SLars-Peter Clausen 
12724894b76SLars-Peter Clausen /**
12824894b76SLars-Peter Clausen  * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
12924894b76SLars-Peter Clausen  * @rtd: ASoC PCM runtime that is deactivated
13024894b76SLars-Peter Clausen  * @stream: Direction of the PCM stream
13124894b76SLars-Peter Clausen  *
13224894b76SLars-Peter Clausen  * Decrements the active count for all the DAIs and components attached to a PCM
13324894b76SLars-Peter Clausen  * runtime. Should typically be called when a stream is closed.
13424894b76SLars-Peter Clausen  *
13572b745e3SPeter Ujfalusi  * Must be called with the rtd->card->pcm_mutex being held
13624894b76SLars-Peter Clausen  */
13724894b76SLars-Peter Clausen void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
13824894b76SLars-Peter Clausen {
1397a5aaba4SKuninori Morimoto 	snd_soc_runtime_action(rtd, stream, -1);
14024894b76SLars-Peter Clausen }
14124894b76SLars-Peter Clausen 
14224894b76SLars-Peter Clausen /**
143208a1589SLars-Peter Clausen  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
144208a1589SLars-Peter Clausen  * @rtd: The ASoC PCM runtime that should be checked.
145208a1589SLars-Peter Clausen  *
146208a1589SLars-Peter Clausen  * This function checks whether the power down delay should be ignored for a
147208a1589SLars-Peter Clausen  * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
148208a1589SLars-Peter Clausen  * been configured to ignore the delay, or if none of the components benefits
149208a1589SLars-Peter Clausen  * from having the delay.
150208a1589SLars-Peter Clausen  */
151208a1589SLars-Peter Clausen bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
152208a1589SLars-Peter Clausen {
153fbb16563SKuninori Morimoto 	struct snd_soc_component *component;
1542e5894d7SBenoit Cousson 	bool ignore = true;
155613fb500SKuninori Morimoto 	int i;
1562e5894d7SBenoit Cousson 
157208a1589SLars-Peter Clausen 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
158208a1589SLars-Peter Clausen 		return true;
159208a1589SLars-Peter Clausen 
160613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
16172c38184SKuninori Morimoto 		ignore &= !component->driver->use_pmdown_time;
162fbb16563SKuninori Morimoto 
163fbb16563SKuninori Morimoto 	return ignore;
164208a1589SLars-Peter Clausen }
165208a1589SLars-Peter Clausen 
166208a1589SLars-Peter Clausen /**
16790996f43SLars-Peter Clausen  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
16890996f43SLars-Peter Clausen  * @substream: the pcm substream
16990996f43SLars-Peter Clausen  * @hw: the hardware parameters
17090996f43SLars-Peter Clausen  *
17190996f43SLars-Peter Clausen  * Sets the substream runtime hardware parameters.
17290996f43SLars-Peter Clausen  */
17390996f43SLars-Peter Clausen int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
17490996f43SLars-Peter Clausen 	const struct snd_pcm_hardware *hw)
17590996f43SLars-Peter Clausen {
17690996f43SLars-Peter Clausen 	struct snd_pcm_runtime *runtime = substream->runtime;
17790996f43SLars-Peter Clausen 	runtime->hw.info = hw->info;
17890996f43SLars-Peter Clausen 	runtime->hw.formats = hw->formats;
17990996f43SLars-Peter Clausen 	runtime->hw.period_bytes_min = hw->period_bytes_min;
18090996f43SLars-Peter Clausen 	runtime->hw.period_bytes_max = hw->period_bytes_max;
18190996f43SLars-Peter Clausen 	runtime->hw.periods_min = hw->periods_min;
18290996f43SLars-Peter Clausen 	runtime->hw.periods_max = hw->periods_max;
18390996f43SLars-Peter Clausen 	runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
18490996f43SLars-Peter Clausen 	runtime->hw.fifo_size = hw->fifo_size;
18590996f43SLars-Peter Clausen 	return 0;
18690996f43SLars-Peter Clausen }
18790996f43SLars-Peter Clausen EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
18890996f43SLars-Peter Clausen 
18901d7584cSLiam Girdwood /* DPCM stream event, send event to FE and all active BEs. */
19023607025SLiam Girdwood int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
19101d7584cSLiam Girdwood 	int event)
19201d7584cSLiam Girdwood {
19301d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
19401d7584cSLiam Girdwood 
1958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, dir, dpcm) {
19601d7584cSLiam Girdwood 
19701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
19801d7584cSLiam Girdwood 
199103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
20001d7584cSLiam Girdwood 				be->dai_link->name, event, dir);
20101d7584cSLiam Girdwood 
202b1cd2e34SBanajit Goswami 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
203b1cd2e34SBanajit Goswami 		    (be->dpcm[dir].users >= 1))
204b1cd2e34SBanajit Goswami 			continue;
205b1cd2e34SBanajit Goswami 
20601d7584cSLiam Girdwood 		snd_soc_dapm_stream_event(be, dir, event);
20701d7584cSLiam Girdwood 	}
20801d7584cSLiam Girdwood 
20901d7584cSLiam Girdwood 	snd_soc_dapm_stream_event(fe, dir, event);
21001d7584cSLiam Girdwood 
21101d7584cSLiam Girdwood 	return 0;
21201d7584cSLiam Girdwood }
21301d7584cSLiam Girdwood 
21417841020SDong Aisheng static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
21517841020SDong Aisheng 					struct snd_soc_dai *soc_dai)
216ddee627cSLiam Girdwood {
217ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
218ddee627cSLiam Girdwood 	int ret;
219ddee627cSLiam Girdwood 
2203635bf09SNicolin Chen 	if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
2213635bf09SNicolin Chen 				rtd->dai_link->symmetric_rates)) {
2223635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
2233635bf09SNicolin Chen 				soc_dai->rate);
224ddee627cSLiam Girdwood 
2254dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
226ddee627cSLiam Girdwood 						SNDRV_PCM_HW_PARAM_RATE,
2274dcdd43bSLars-Peter Clausen 						soc_dai->rate);
228ddee627cSLiam Girdwood 		if (ret < 0) {
22917841020SDong Aisheng 			dev_err(soc_dai->dev,
2303635bf09SNicolin Chen 				"ASoC: Unable to apply rate constraint: %d\n",
231103d84a3SLiam Girdwood 				ret);
232ddee627cSLiam Girdwood 			return ret;
233ddee627cSLiam Girdwood 		}
2343635bf09SNicolin Chen 	}
2353635bf09SNicolin Chen 
2363635bf09SNicolin Chen 	if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
2373635bf09SNicolin Chen 				rtd->dai_link->symmetric_channels)) {
2383635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
2393635bf09SNicolin Chen 				soc_dai->channels);
2403635bf09SNicolin Chen 
2414dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2423635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_CHANNELS,
2433635bf09SNicolin Chen 						soc_dai->channels);
2443635bf09SNicolin Chen 		if (ret < 0) {
2453635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2463635bf09SNicolin Chen 				"ASoC: Unable to apply channel symmetry constraint: %d\n",
2473635bf09SNicolin Chen 				ret);
2483635bf09SNicolin Chen 			return ret;
2493635bf09SNicolin Chen 		}
2503635bf09SNicolin Chen 	}
2513635bf09SNicolin Chen 
2523635bf09SNicolin Chen 	if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
2533635bf09SNicolin Chen 				rtd->dai_link->symmetric_samplebits)) {
2543635bf09SNicolin Chen 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
2553635bf09SNicolin Chen 				soc_dai->sample_bits);
2563635bf09SNicolin Chen 
2574dcdd43bSLars-Peter Clausen 		ret = snd_pcm_hw_constraint_single(substream->runtime,
2583635bf09SNicolin Chen 						SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2593635bf09SNicolin Chen 						soc_dai->sample_bits);
2603635bf09SNicolin Chen 		if (ret < 0) {
2613635bf09SNicolin Chen 			dev_err(soc_dai->dev,
2623635bf09SNicolin Chen 				"ASoC: Unable to apply sample bits symmetry constraint: %d\n",
2633635bf09SNicolin Chen 				ret);
2643635bf09SNicolin Chen 			return ret;
2653635bf09SNicolin Chen 		}
2663635bf09SNicolin Chen 	}
267ddee627cSLiam Girdwood 
268ddee627cSLiam Girdwood 	return 0;
269ddee627cSLiam Girdwood }
270ddee627cSLiam Girdwood 
2713635bf09SNicolin Chen static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
2723635bf09SNicolin Chen 				struct snd_pcm_hw_params *params)
2733635bf09SNicolin Chen {
2743635bf09SNicolin Chen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2753635bf09SNicolin Chen 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2760b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
2772e5894d7SBenoit Cousson 	unsigned int rate, channels, sample_bits, symmetry, i;
2783635bf09SNicolin Chen 
2793635bf09SNicolin Chen 	rate = params_rate(params);
2803635bf09SNicolin Chen 	channels = params_channels(params);
2813635bf09SNicolin Chen 	sample_bits = snd_pcm_format_physical_width(params_format(params));
2823635bf09SNicolin Chen 
2833635bf09SNicolin Chen 	/* reject unmatched parameters when applying symmetry */
2843635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_rates ||
2853635bf09SNicolin Chen 		rtd->dai_link->symmetric_rates;
2862e5894d7SBenoit Cousson 
2870b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
2880b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_rates;
2892e5894d7SBenoit Cousson 
2903635bf09SNicolin Chen 	if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
2913635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
2923635bf09SNicolin Chen 				cpu_dai->rate, rate);
2933635bf09SNicolin Chen 		return -EINVAL;
2943635bf09SNicolin Chen 	}
2953635bf09SNicolin Chen 
2963635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_channels ||
2973635bf09SNicolin Chen 		rtd->dai_link->symmetric_channels;
2982e5894d7SBenoit Cousson 
2990b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
3000b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_channels;
3012e5894d7SBenoit Cousson 
3023635bf09SNicolin Chen 	if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
3033635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
3043635bf09SNicolin Chen 				cpu_dai->channels, channels);
3053635bf09SNicolin Chen 		return -EINVAL;
3063635bf09SNicolin Chen 	}
3073635bf09SNicolin Chen 
3083635bf09SNicolin Chen 	symmetry = cpu_dai->driver->symmetric_samplebits ||
3093635bf09SNicolin Chen 		rtd->dai_link->symmetric_samplebits;
3102e5894d7SBenoit Cousson 
3110b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
3120b7990e3SKuninori Morimoto 		symmetry |= codec_dai->driver->symmetric_samplebits;
3132e5894d7SBenoit Cousson 
3143635bf09SNicolin Chen 	if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
3153635bf09SNicolin Chen 		dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
3163635bf09SNicolin Chen 				cpu_dai->sample_bits, sample_bits);
3173635bf09SNicolin Chen 		return -EINVAL;
3183635bf09SNicolin Chen 	}
319ddee627cSLiam Girdwood 
320ddee627cSLiam Girdwood 	return 0;
321ddee627cSLiam Girdwood }
322ddee627cSLiam Girdwood 
32362e5f676SLars-Peter Clausen static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
32462e5f676SLars-Peter Clausen {
32562e5f676SLars-Peter Clausen 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
32662e5f676SLars-Peter Clausen 	struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
32762e5f676SLars-Peter Clausen 	struct snd_soc_dai_link *link = rtd->dai_link;
3280b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
3292e5894d7SBenoit Cousson 	unsigned int symmetry, i;
33062e5f676SLars-Peter Clausen 
3312e5894d7SBenoit Cousson 	symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
3322e5894d7SBenoit Cousson 		cpu_driver->symmetric_channels || link->symmetric_channels ||
3332e5894d7SBenoit Cousson 		cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
3342e5894d7SBenoit Cousson 
3350b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
3362e5894d7SBenoit Cousson 		symmetry = symmetry ||
3370b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_rates ||
3380b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_channels ||
3390b7990e3SKuninori Morimoto 			codec_dai->driver->symmetric_samplebits;
3402e5894d7SBenoit Cousson 
3412e5894d7SBenoit Cousson 	return symmetry;
34262e5f676SLars-Peter Clausen }
34362e5f676SLars-Peter Clausen 
3442e5894d7SBenoit Cousson static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
34558ba9b25SMark Brown {
3462e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
347c6068d3aSTakashi Iwai 	int ret;
34858ba9b25SMark Brown 
34958ba9b25SMark Brown 	if (!bits)
35058ba9b25SMark Brown 		return;
35158ba9b25SMark Brown 
3520e2a3751SLars-Peter Clausen 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
35358ba9b25SMark Brown 	if (ret != 0)
3540e2a3751SLars-Peter Clausen 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
3550e2a3751SLars-Peter Clausen 				 bits, ret);
35658ba9b25SMark Brown }
35758ba9b25SMark Brown 
358c8dd1fecSBenoit Cousson static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
359bd477c31SLars-Peter Clausen {
360c8dd1fecSBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
361c8dd1fecSBenoit Cousson 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3622e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
3632e5894d7SBenoit Cousson 	int i;
364c8dd1fecSBenoit Cousson 	unsigned int bits = 0, cpu_bits;
365c8dd1fecSBenoit Cousson 
366c8dd1fecSBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3670b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
3682e5894d7SBenoit Cousson 			if (codec_dai->driver->playback.sig_bits == 0) {
3692e5894d7SBenoit Cousson 				bits = 0;
3702e5894d7SBenoit Cousson 				break;
3712e5894d7SBenoit Cousson 			}
3722e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->playback.sig_bits, bits);
3732e5894d7SBenoit Cousson 		}
374c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->playback.sig_bits;
375c8dd1fecSBenoit Cousson 	} else {
3760b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
3775e63dfccSDaniel Mack 			if (codec_dai->driver->capture.sig_bits == 0) {
3782e5894d7SBenoit Cousson 				bits = 0;
3792e5894d7SBenoit Cousson 				break;
3802e5894d7SBenoit Cousson 			}
3812e5894d7SBenoit Cousson 			bits = max(codec_dai->driver->capture.sig_bits, bits);
3822e5894d7SBenoit Cousson 		}
383c8dd1fecSBenoit Cousson 		cpu_bits = cpu_dai->driver->capture.sig_bits;
384c8dd1fecSBenoit Cousson 	}
385c8dd1fecSBenoit Cousson 
3862e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, bits);
3872e5894d7SBenoit Cousson 	soc_pcm_set_msb(substream, cpu_bits);
388c8dd1fecSBenoit Cousson }
389c8dd1fecSBenoit Cousson 
3902e5894d7SBenoit Cousson static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
391bd477c31SLars-Peter Clausen {
3922e5894d7SBenoit Cousson 	struct snd_pcm_runtime *runtime = substream->runtime;
39378e45c99SLars-Peter Clausen 	struct snd_pcm_hardware *hw = &runtime->hw;
3942e5894d7SBenoit Cousson 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
3950b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
3962e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
3972e5894d7SBenoit Cousson 	struct snd_soc_dai_driver *codec_dai_drv;
3982e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *codec_stream;
3992e5894d7SBenoit Cousson 	struct snd_soc_pcm_stream *cpu_stream;
4002e5894d7SBenoit Cousson 	unsigned int chan_min = 0, chan_max = UINT_MAX;
4012e5894d7SBenoit Cousson 	unsigned int rate_min = 0, rate_max = UINT_MAX;
4022e5894d7SBenoit Cousson 	unsigned int rates = UINT_MAX;
4032e5894d7SBenoit Cousson 	u64 formats = ULLONG_MAX;
4042e5894d7SBenoit Cousson 	int i;
40578e45c99SLars-Peter Clausen 
4062e5894d7SBenoit Cousson 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4072e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->playback;
40816d7ea91SLars-Peter Clausen 	else
4092e5894d7SBenoit Cousson 		cpu_stream = &cpu_dai_drv->capture;
41078e45c99SLars-Peter Clausen 
4112e5894d7SBenoit Cousson 	/* first calculate min/max only for CODECs in the DAI link */
4120b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
413cde79035SRicard Wanderlof 
414cde79035SRicard Wanderlof 		/*
415cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type.
416cde79035SRicard Wanderlof 		 * Otherwise, since the rate, channel, and format values will
417cde79035SRicard Wanderlof 		 * zero in that case, we would have no usable settings left,
418cde79035SRicard Wanderlof 		 * causing the resulting setup to fail.
419cde79035SRicard Wanderlof 		 * At least one CODEC should match, otherwise we should have
420cde79035SRicard Wanderlof 		 * bailed out on a higher level, since there would be no
421cde79035SRicard Wanderlof 		 * CODEC to support the transfer direction in that case.
422cde79035SRicard Wanderlof 		 */
4230b7990e3SKuninori Morimoto 		if (!snd_soc_dai_stream_valid(codec_dai,
424cde79035SRicard Wanderlof 					      substream->stream))
425cde79035SRicard Wanderlof 			continue;
426cde79035SRicard Wanderlof 
4270b7990e3SKuninori Morimoto 		codec_dai_drv = codec_dai->driver;
4282e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
4292e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->playback;
4302e5894d7SBenoit Cousson 		else
4312e5894d7SBenoit Cousson 			codec_stream = &codec_dai_drv->capture;
4322e5894d7SBenoit Cousson 		chan_min = max(chan_min, codec_stream->channels_min);
4332e5894d7SBenoit Cousson 		chan_max = min(chan_max, codec_stream->channels_max);
4342e5894d7SBenoit Cousson 		rate_min = max(rate_min, codec_stream->rate_min);
4352e5894d7SBenoit Cousson 		rate_max = min_not_zero(rate_max, codec_stream->rate_max);
4362e5894d7SBenoit Cousson 		formats &= codec_stream->formats;
4372e5894d7SBenoit Cousson 		rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
4382e5894d7SBenoit Cousson 	}
4392e5894d7SBenoit Cousson 
4402e5894d7SBenoit Cousson 	/*
4412e5894d7SBenoit Cousson 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
4422e5894d7SBenoit Cousson 	 * connected to a single CPU DAI, use CPU DAI's directly and let
4432e5894d7SBenoit Cousson 	 * channel allocation be fixed up later
4442e5894d7SBenoit Cousson 	 */
4452e5894d7SBenoit Cousson 	if (rtd->num_codecs > 1) {
4462e5894d7SBenoit Cousson 		chan_min = cpu_stream->channels_min;
4472e5894d7SBenoit Cousson 		chan_max = cpu_stream->channels_max;
4482e5894d7SBenoit Cousson 	}
4492e5894d7SBenoit Cousson 
4502e5894d7SBenoit Cousson 	hw->channels_min = max(chan_min, cpu_stream->channels_min);
4512e5894d7SBenoit Cousson 	hw->channels_max = min(chan_max, cpu_stream->channels_max);
4522e5894d7SBenoit Cousson 	if (hw->formats)
4532e5894d7SBenoit Cousson 		hw->formats &= formats & cpu_stream->formats;
4542e5894d7SBenoit Cousson 	else
4552e5894d7SBenoit Cousson 		hw->formats = formats & cpu_stream->formats;
4562e5894d7SBenoit Cousson 	hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
45778e45c99SLars-Peter Clausen 
45878e45c99SLars-Peter Clausen 	snd_pcm_limit_hw_rates(runtime);
45978e45c99SLars-Peter Clausen 
46078e45c99SLars-Peter Clausen 	hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
4612e5894d7SBenoit Cousson 	hw->rate_min = max(hw->rate_min, rate_min);
46278e45c99SLars-Peter Clausen 	hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
4632e5894d7SBenoit Cousson 	hw->rate_max = min_not_zero(hw->rate_max, rate_max);
464bd477c31SLars-Peter Clausen }
465bd477c31SLars-Peter Clausen 
466e7ecfdb7SKuninori Morimoto static int soc_pcm_components_open(struct snd_pcm_substream *substream,
467e7ecfdb7SKuninori Morimoto 				   struct snd_soc_component **last)
468e7ecfdb7SKuninori Morimoto {
469e7ecfdb7SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
470e7ecfdb7SKuninori Morimoto 	struct snd_soc_component *component;
471613fb500SKuninori Morimoto 	int i, ret = 0;
472e7ecfdb7SKuninori Morimoto 
473613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
474e7ecfdb7SKuninori Morimoto 		*last = component;
475e7ecfdb7SKuninori Morimoto 
4764a81e8f3SKuninori Morimoto 		ret = snd_soc_component_module_get_when_open(component);
4774a81e8f3SKuninori Morimoto 		if (ret < 0) {
478e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
479e7ecfdb7SKuninori Morimoto 				"ASoC: can't get module %s\n",
480e7ecfdb7SKuninori Morimoto 				component->name);
4814a81e8f3SKuninori Morimoto 			return ret;
482e7ecfdb7SKuninori Morimoto 		}
483e7ecfdb7SKuninori Morimoto 
484ae2f4849SKuninori Morimoto 		ret = snd_soc_component_open(component, substream);
485e7ecfdb7SKuninori Morimoto 		if (ret < 0) {
486e7ecfdb7SKuninori Morimoto 			dev_err(component->dev,
487e7ecfdb7SKuninori Morimoto 				"ASoC: can't open component %s: %d\n",
488e7ecfdb7SKuninori Morimoto 				component->name, ret);
489e7ecfdb7SKuninori Morimoto 			return ret;
490e7ecfdb7SKuninori Morimoto 		}
491e7ecfdb7SKuninori Morimoto 	}
492e7ecfdb7SKuninori Morimoto 	*last = NULL;
493e7ecfdb7SKuninori Morimoto 	return 0;
494e7ecfdb7SKuninori Morimoto }
495e7ecfdb7SKuninori Morimoto 
496244e2936SCharles Keepax static int soc_pcm_components_close(struct snd_pcm_substream *substream,
497244e2936SCharles Keepax 				    struct snd_soc_component *last)
498244e2936SCharles Keepax {
499244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
500244e2936SCharles Keepax 	struct snd_soc_component *component;
501*e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
502244e2936SCharles Keepax 
503613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
504244e2936SCharles Keepax 		if (component == last)
505244e2936SCharles Keepax 			break;
506244e2936SCharles Keepax 
507*e82ebffcSKuninori Morimoto 		r = snd_soc_component_close(component, substream);
508*e82ebffcSKuninori Morimoto 		if (r < 0)
509*e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
510*e82ebffcSKuninori Morimoto 
5114a81e8f3SKuninori Morimoto 		snd_soc_component_module_put_when_close(component);
512244e2936SCharles Keepax 	}
513244e2936SCharles Keepax 
5143672beb8SKuninori Morimoto 	return ret;
515244e2936SCharles Keepax }
516244e2936SCharles Keepax 
51758ba9b25SMark Brown /*
518ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
519ddee627cSLiam Girdwood  * then initialized and any private data can be allocated. This also calls
520ef050becSCharles Keepax  * startup for the cpu DAI, component, machine and codec DAI.
521ddee627cSLiam Girdwood  */
522ddee627cSLiam Girdwood static int soc_pcm_open(struct snd_pcm_substream *substream)
523ddee627cSLiam Girdwood {
524ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
525ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
52690be711eSKuninori Morimoto 	struct snd_soc_component *component;
527ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
5282e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
5292e5894d7SBenoit Cousson 	const char *codec_dai_name = "multicodec";
530244e2936SCharles Keepax 	int i, ret = 0;
531ddee627cSLiam Girdwood 
53276c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
53376c39e86SKuninori Morimoto 		pinctrl_pm_select_default_state(component->dev);
53490be711eSKuninori Morimoto 
535613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
53690be711eSKuninori Morimoto 		pm_runtime_get_sync(component->dev);
537d6652ef8SMark Brown 
53872b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
539ddee627cSLiam Girdwood 
540ddee627cSLiam Girdwood 	/* startup the audio subsystem */
5415a52a045SKuninori Morimoto 	ret = snd_soc_dai_startup(cpu_dai, substream);
542ddee627cSLiam Girdwood 	if (ret < 0) {
5435a52a045SKuninori Morimoto 		dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
5445a52a045SKuninori Morimoto 			cpu_dai->name, ret);
545ddee627cSLiam Girdwood 		goto out;
546ddee627cSLiam Girdwood 	}
547ddee627cSLiam Girdwood 
548e7ecfdb7SKuninori Morimoto 	ret = soc_pcm_components_open(substream, &component);
549e7ecfdb7SKuninori Morimoto 	if (ret < 0)
550b8135864SKuninori Morimoto 		goto component_err;
551b8135864SKuninori Morimoto 
5520b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
5535a52a045SKuninori Morimoto 		ret = snd_soc_dai_startup(codec_dai, substream);
554ddee627cSLiam Girdwood 		if (ret < 0) {
5552e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
5562e5894d7SBenoit Cousson 				"ASoC: can't open codec %s: %d\n",
5572e5894d7SBenoit Cousson 				codec_dai->name, ret);
558ddee627cSLiam Girdwood 			goto codec_dai_err;
559ddee627cSLiam Girdwood 		}
560ddee627cSLiam Girdwood 
5612e5894d7SBenoit Cousson 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
5622e5894d7SBenoit Cousson 			codec_dai->tx_mask = 0;
5632e5894d7SBenoit Cousson 		else
5642e5894d7SBenoit Cousson 			codec_dai->rx_mask = 0;
5652e5894d7SBenoit Cousson 	}
5662e5894d7SBenoit Cousson 
567f183f927SKuninori Morimoto 	ret = soc_rtd_startup(rtd, substream);
568ddee627cSLiam Girdwood 	if (ret < 0) {
569103d84a3SLiam Girdwood 		pr_err("ASoC: %s startup failed: %d\n",
57025bfe662SMark Brown 		       rtd->dai_link->name, ret);
571ddee627cSLiam Girdwood 		goto machine_err;
572ddee627cSLiam Girdwood 	}
573ddee627cSLiam Girdwood 
57401d7584cSLiam Girdwood 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
57501d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
57601d7584cSLiam Girdwood 		goto dynamic;
57701d7584cSLiam Girdwood 
578ddee627cSLiam Girdwood 	/* Check that the codec and cpu DAIs are compatible */
5792e5894d7SBenoit Cousson 	soc_pcm_init_runtime_hw(substream);
5802e5894d7SBenoit Cousson 
5812e5894d7SBenoit Cousson 	if (rtd->num_codecs == 1)
5822e5894d7SBenoit Cousson 		codec_dai_name = rtd->codec_dai->name;
583ddee627cSLiam Girdwood 
58462e5f676SLars-Peter Clausen 	if (soc_pcm_has_symmetry(substream))
58562e5f676SLars-Peter Clausen 		runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
58662e5f676SLars-Peter Clausen 
587ddee627cSLiam Girdwood 	ret = -EINVAL;
588ddee627cSLiam Girdwood 	if (!runtime->hw.rates) {
589103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
5902e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
591ddee627cSLiam Girdwood 		goto config_err;
592ddee627cSLiam Girdwood 	}
593ddee627cSLiam Girdwood 	if (!runtime->hw.formats) {
594103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
5952e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
596ddee627cSLiam Girdwood 		goto config_err;
597ddee627cSLiam Girdwood 	}
598ddee627cSLiam Girdwood 	if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
599ddee627cSLiam Girdwood 	    runtime->hw.channels_min > runtime->hw.channels_max) {
600103d84a3SLiam Girdwood 		printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
6012e5894d7SBenoit Cousson 				codec_dai_name, cpu_dai->name);
602ddee627cSLiam Girdwood 		goto config_err;
603ddee627cSLiam Girdwood 	}
604ddee627cSLiam Girdwood 
605c8dd1fecSBenoit Cousson 	soc_pcm_apply_msb(substream);
60658ba9b25SMark Brown 
607ddee627cSLiam Girdwood 	/* Symmetry only applies if we've already got an active stream. */
60817841020SDong Aisheng 	if (cpu_dai->active) {
60917841020SDong Aisheng 		ret = soc_pcm_apply_symmetry(substream, cpu_dai);
61017841020SDong Aisheng 		if (ret != 0)
61117841020SDong Aisheng 			goto config_err;
61217841020SDong Aisheng 	}
61317841020SDong Aisheng 
6140b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
6150b7990e3SKuninori Morimoto 		if (codec_dai->active) {
6160b7990e3SKuninori Morimoto 			ret = soc_pcm_apply_symmetry(substream, codec_dai);
617ddee627cSLiam Girdwood 			if (ret != 0)
618ddee627cSLiam Girdwood 				goto config_err;
619ddee627cSLiam Girdwood 		}
6202e5894d7SBenoit Cousson 	}
621ddee627cSLiam Girdwood 
622103d84a3SLiam Girdwood 	pr_debug("ASoC: %s <-> %s info:\n",
6232e5894d7SBenoit Cousson 			codec_dai_name, cpu_dai->name);
624103d84a3SLiam Girdwood 	pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
625103d84a3SLiam Girdwood 	pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
626ddee627cSLiam Girdwood 		 runtime->hw.channels_max);
627103d84a3SLiam Girdwood 	pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
628ddee627cSLiam Girdwood 		 runtime->hw.rate_max);
629ddee627cSLiam Girdwood 
63001d7584cSLiam Girdwood dynamic:
63124894b76SLars-Peter Clausen 
63224894b76SLars-Peter Clausen 	snd_soc_runtime_activate(rtd, substream->stream);
63324894b76SLars-Peter Clausen 
63472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
635ddee627cSLiam Girdwood 	return 0;
636ddee627cSLiam Girdwood 
637ddee627cSLiam Girdwood config_err:
6380be429f9SKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
639ddee627cSLiam Girdwood 
640ddee627cSLiam Girdwood machine_err:
6412e5894d7SBenoit Cousson 	i = rtd->num_codecs;
642ddee627cSLiam Girdwood 
643ddee627cSLiam Girdwood codec_dai_err:
644330fcb51SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
645330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
6462e5894d7SBenoit Cousson 
647b8135864SKuninori Morimoto component_err:
648244e2936SCharles Keepax 	soc_pcm_components_close(substream, component);
649e7ecfdb7SKuninori Morimoto 
650330fcb51SKuninori Morimoto 	snd_soc_dai_shutdown(cpu_dai, substream);
651ddee627cSLiam Girdwood out:
65272b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
653d6652ef8SMark Brown 
654613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
65590be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
65690be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
6573f809783SSanyog Kale 	}
6583f809783SSanyog Kale 
65976c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
66076c39e86SKuninori Morimoto 		if (!component->active)
66176c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
662d6652ef8SMark Brown 
663ddee627cSLiam Girdwood 	return ret;
664ddee627cSLiam Girdwood }
665ddee627cSLiam Girdwood 
6664bf2e385SCurtis Malainey static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
667a342031cSJerome Brunet {
668a342031cSJerome Brunet 	/*
669a342031cSJerome Brunet 	 * Currently nothing to do for c2c links
670a342031cSJerome Brunet 	 * Since c2c links are internal nodes in the DAPM graph and
671a342031cSJerome Brunet 	 * don't interface with the outside world or application layer
672a342031cSJerome Brunet 	 * we don't have to do any special handling on close.
673a342031cSJerome Brunet 	 */
674a342031cSJerome Brunet }
675a342031cSJerome Brunet 
676ddee627cSLiam Girdwood /*
677ddee627cSLiam Girdwood  * Called by ALSA when a PCM substream is closed. Private data can be
678ef050becSCharles Keepax  * freed here. The cpu DAI, codec DAI, machine and components are also
679ddee627cSLiam Girdwood  * shutdown.
680ddee627cSLiam Girdwood  */
68191d5e6b4SLiam Girdwood static int soc_pcm_close(struct snd_pcm_substream *substream)
682ddee627cSLiam Girdwood {
683ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
68490be711eSKuninori Morimoto 	struct snd_soc_component *component;
685ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
6862e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
6872e5894d7SBenoit Cousson 	int i;
688ddee627cSLiam Girdwood 
68972b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
690ddee627cSLiam Girdwood 
69124894b76SLars-Peter Clausen 	snd_soc_runtime_deactivate(rtd, substream->stream);
692ddee627cSLiam Girdwood 
693ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
694ae11601bSRamesh Babu 
695330fcb51SKuninori Morimoto 	snd_soc_dai_shutdown(cpu_dai, substream);
696ddee627cSLiam Girdwood 
697330fcb51SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
698330fcb51SKuninori Morimoto 		snd_soc_dai_shutdown(codec_dai, substream);
699ddee627cSLiam Girdwood 
7000be429f9SKuninori Morimoto 	soc_rtd_shutdown(rtd, substream);
701ddee627cSLiam Girdwood 
702244e2936SCharles Keepax 	soc_pcm_components_close(substream, NULL);
703b8135864SKuninori Morimoto 
7043f4cf797SKuninori Morimoto 	snd_soc_dapm_stream_stop(rtd, substream->stream);
705ddee627cSLiam Girdwood 
70672b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
707d6652ef8SMark Brown 
708613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
70990be711eSKuninori Morimoto 		pm_runtime_mark_last_busy(component->dev);
71090be711eSKuninori Morimoto 		pm_runtime_put_autosuspend(component->dev);
7113f809783SSanyog Kale 	}
7123f809783SSanyog Kale 
71376c39e86SKuninori Morimoto 	for_each_rtd_components(rtd, i, component)
71476c39e86SKuninori Morimoto 		if (!component->active)
71576c39e86SKuninori Morimoto 			pinctrl_pm_select_sleep_state(component->dev);
716d6652ef8SMark Brown 
717ddee627cSLiam Girdwood 	return 0;
718ddee627cSLiam Girdwood }
719ddee627cSLiam Girdwood 
720ddee627cSLiam Girdwood /*
721ddee627cSLiam Girdwood  * Called by ALSA when the PCM substream is prepared, can set format, sample
722ddee627cSLiam Girdwood  * rate, etc.  This function is non atomic and can be called multiple times,
723ddee627cSLiam Girdwood  * it can refer to the runtime info.
724ddee627cSLiam Girdwood  */
725ddee627cSLiam Girdwood static int soc_pcm_prepare(struct snd_pcm_substream *substream)
726ddee627cSLiam Girdwood {
727ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
728b8135864SKuninori Morimoto 	struct snd_soc_component *component;
729ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
7302e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
7312e5894d7SBenoit Cousson 	int i, ret = 0;
732ddee627cSLiam Girdwood 
73372b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
734ddee627cSLiam Girdwood 
73544c1a75bSKuninori Morimoto 	ret = soc_rtd_prepare(rtd, substream);
736ddee627cSLiam Girdwood 	if (ret < 0) {
73744c1a75bSKuninori Morimoto 		dev_err(rtd->card->dev,
73844c1a75bSKuninori Morimoto 			"ASoC: machine prepare error: %d\n", ret);
739ddee627cSLiam Girdwood 		goto out;
740ddee627cSLiam Girdwood 	}
741ddee627cSLiam Girdwood 
742613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
7436d537233SKuninori Morimoto 		ret = snd_soc_component_prepare(component, substream);
744b8135864SKuninori Morimoto 		if (ret < 0) {
745b8135864SKuninori Morimoto 			dev_err(component->dev,
746b8135864SKuninori Morimoto 				"ASoC: platform prepare error: %d\n", ret);
747b8135864SKuninori Morimoto 			goto out;
748b8135864SKuninori Morimoto 		}
749b8135864SKuninori Morimoto 	}
750b8135864SKuninori Morimoto 
7510b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
7524beb8e10SKuninori Morimoto 		ret = snd_soc_dai_prepare(codec_dai, substream);
753ddee627cSLiam Girdwood 		if (ret < 0) {
7542e5894d7SBenoit Cousson 			dev_err(codec_dai->dev,
75590cc7f1cSJarkko Nikula 				"ASoC: codec DAI prepare error: %d\n",
75690cc7f1cSJarkko Nikula 				ret);
757ddee627cSLiam Girdwood 			goto out;
758ddee627cSLiam Girdwood 		}
759ddee627cSLiam Girdwood 	}
760ddee627cSLiam Girdwood 
7614beb8e10SKuninori Morimoto 	ret = snd_soc_dai_prepare(cpu_dai, substream);
762ddee627cSLiam Girdwood 	if (ret < 0) {
76390cc7f1cSJarkko Nikula 		dev_err(cpu_dai->dev,
76490cc7f1cSJarkko Nikula 			"ASoC: cpu DAI prepare error: %d\n", ret);
765ddee627cSLiam Girdwood 		goto out;
766ddee627cSLiam Girdwood 	}
767ddee627cSLiam Girdwood 
768ddee627cSLiam Girdwood 	/* cancel any delayed stream shutdown that is pending */
769ddee627cSLiam Girdwood 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
7709bffb1fbSMisael Lopez Cruz 	    rtd->pop_wait) {
7719bffb1fbSMisael Lopez Cruz 		rtd->pop_wait = 0;
772ddee627cSLiam Girdwood 		cancel_delayed_work(&rtd->delayed_work);
773ddee627cSLiam Girdwood 	}
774ddee627cSLiam Girdwood 
775d9b0951bSLiam Girdwood 	snd_soc_dapm_stream_event(rtd, substream->stream,
776ddee627cSLiam Girdwood 			SND_SOC_DAPM_STREAM_START);
777ddee627cSLiam Girdwood 
7780b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai)
7790b7990e3SKuninori Morimoto 		snd_soc_dai_digital_mute(codec_dai, 0,
7802e5894d7SBenoit Cousson 					 substream->stream);
781ae11601bSRamesh Babu 	snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
782ddee627cSLiam Girdwood 
783ddee627cSLiam Girdwood out:
78472b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
785ddee627cSLiam Girdwood 	return ret;
786ddee627cSLiam Girdwood }
787ddee627cSLiam Girdwood 
7882e5894d7SBenoit Cousson static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
7892e5894d7SBenoit Cousson 				       unsigned int mask)
7902e5894d7SBenoit Cousson {
7912e5894d7SBenoit Cousson 	struct snd_interval *interval;
7922e5894d7SBenoit Cousson 	int channels = hweight_long(mask);
7932e5894d7SBenoit Cousson 
7942e5894d7SBenoit Cousson 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
7952e5894d7SBenoit Cousson 	interval->min = channels;
7962e5894d7SBenoit Cousson 	interval->max = channels;
7972e5894d7SBenoit Cousson }
7982e5894d7SBenoit Cousson 
799244e2936SCharles Keepax static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
800244e2936SCharles Keepax 				      struct snd_soc_component *last)
801244e2936SCharles Keepax {
802244e2936SCharles Keepax 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
803244e2936SCharles Keepax 	struct snd_soc_component *component;
804*e82ebffcSKuninori Morimoto 	int i, r, ret = 0;
805244e2936SCharles Keepax 
806613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
807244e2936SCharles Keepax 		if (component == last)
808244e2936SCharles Keepax 			break;
809244e2936SCharles Keepax 
810*e82ebffcSKuninori Morimoto 		r = snd_soc_component_hw_free(component, substream);
811*e82ebffcSKuninori Morimoto 		if (r < 0)
812*e82ebffcSKuninori Morimoto 			ret = r; /* use last ret */
813244e2936SCharles Keepax 	}
814244e2936SCharles Keepax 
815eae7136aSKuninori Morimoto 	return ret;
816244e2936SCharles Keepax }
817244e2936SCharles Keepax 
818ddee627cSLiam Girdwood /*
819ddee627cSLiam Girdwood  * Called by ALSA when the hardware params are set by application. This
820ddee627cSLiam Girdwood  * function can also be called multiple times and can allocate buffers
821ddee627cSLiam Girdwood  * (using snd_pcm_lib_* ). It's non-atomic.
822ddee627cSLiam Girdwood  */
823ddee627cSLiam Girdwood static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
824ddee627cSLiam Girdwood 				struct snd_pcm_hw_params *params)
825ddee627cSLiam Girdwood {
826ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
827b8135864SKuninori Morimoto 	struct snd_soc_component *component;
828ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
8290b7990e3SKuninori Morimoto 	struct snd_soc_dai *codec_dai;
830244e2936SCharles Keepax 	int i, ret = 0;
831ddee627cSLiam Girdwood 
83272b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
8335cca5951SShengjiu Wang 
8345cca5951SShengjiu Wang 	ret = soc_pcm_params_symmetry(substream, params);
8355cca5951SShengjiu Wang 	if (ret)
8365cca5951SShengjiu Wang 		goto out;
8375cca5951SShengjiu Wang 
838de9ad990SKuninori Morimoto 	ret = soc_rtd_hw_params(rtd, substream, params);
839ddee627cSLiam Girdwood 	if (ret < 0) {
840de9ad990SKuninori Morimoto 		dev_err(rtd->card->dev,
841de9ad990SKuninori Morimoto 			"ASoC: machine hw_params failed: %d\n", ret);
842ddee627cSLiam Girdwood 		goto out;
843ddee627cSLiam Girdwood 	}
844ddee627cSLiam Girdwood 
8450b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
8462e5894d7SBenoit Cousson 		struct snd_pcm_hw_params codec_params;
8472e5894d7SBenoit Cousson 
848cde79035SRicard Wanderlof 		/*
849cde79035SRicard Wanderlof 		 * Skip CODECs which don't support the current stream type,
850cde79035SRicard Wanderlof 		 * the idea being that if a CODEC is not used for the currently
851cde79035SRicard Wanderlof 		 * set up transfer direction, it should not need to be
852cde79035SRicard Wanderlof 		 * configured, especially since the configuration used might
853cde79035SRicard Wanderlof 		 * not even be supported by that CODEC. There may be cases
854cde79035SRicard Wanderlof 		 * however where a CODEC needs to be set up although it is
855cde79035SRicard Wanderlof 		 * actually not being used for the transfer, e.g. if a
856cde79035SRicard Wanderlof 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
857cde79035SRicard Wanderlof 		 * for the DAI link including a playback-only CODEC.
858cde79035SRicard Wanderlof 		 * If this becomes necessary, we will have to augment the
859cde79035SRicard Wanderlof 		 * machine driver setup with information on how to act, so
860cde79035SRicard Wanderlof 		 * we can do the right thing here.
861cde79035SRicard Wanderlof 		 */
862cde79035SRicard Wanderlof 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
863cde79035SRicard Wanderlof 			continue;
864cde79035SRicard Wanderlof 
8652e5894d7SBenoit Cousson 		/* copy params for each codec */
8662e5894d7SBenoit Cousson 		codec_params = *params;
8672e5894d7SBenoit Cousson 
8682e5894d7SBenoit Cousson 		/* fixup params based on TDM slot masks */
869570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
870570f18b6SRander Wang 		    codec_dai->tx_mask)
8712e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8722e5894d7SBenoit Cousson 						   codec_dai->tx_mask);
873570f18b6SRander Wang 
874570f18b6SRander Wang 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
875570f18b6SRander Wang 		    codec_dai->rx_mask)
8762e5894d7SBenoit Cousson 			soc_pcm_codec_params_fixup(&codec_params,
8772e5894d7SBenoit Cousson 						   codec_dai->rx_mask);
8782e5894d7SBenoit Cousson 
879aa6166c2SKuninori Morimoto 		ret = snd_soc_dai_hw_params(codec_dai, substream,
880aa6166c2SKuninori Morimoto 					    &codec_params);
88193e6958aSBenoit Cousson 		if(ret < 0)
882ddee627cSLiam Girdwood 			goto codec_err;
883ddee627cSLiam Girdwood 
8842e5894d7SBenoit Cousson 		codec_dai->rate = params_rate(&codec_params);
8852e5894d7SBenoit Cousson 		codec_dai->channels = params_channels(&codec_params);
8862e5894d7SBenoit Cousson 		codec_dai->sample_bits = snd_pcm_format_physical_width(
8872e5894d7SBenoit Cousson 						params_format(&codec_params));
888078a85f2SCharles Keepax 
889078a85f2SCharles Keepax 		snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
890ddee627cSLiam Girdwood 	}
891ddee627cSLiam Girdwood 
892aa6166c2SKuninori Morimoto 	ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
89393e6958aSBenoit Cousson 	if (ret < 0)
894ddee627cSLiam Girdwood 		goto interface_err;
895ddee627cSLiam Girdwood 
896ca58221dSKuninori Morimoto 	/* store the parameters for each DAIs */
897ca58221dSKuninori Morimoto 	cpu_dai->rate = params_rate(params);
898ca58221dSKuninori Morimoto 	cpu_dai->channels = params_channels(params);
899ca58221dSKuninori Morimoto 	cpu_dai->sample_bits =
900ca58221dSKuninori Morimoto 		snd_pcm_format_physical_width(params_format(params));
901ca58221dSKuninori Morimoto 
902ca58221dSKuninori Morimoto 	snd_soc_dapm_update_dai(substream, params, cpu_dai);
903ca58221dSKuninori Morimoto 
904613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
905245c539aSKuninori Morimoto 		ret = snd_soc_component_hw_params(component, substream, params);
906244e2936SCharles Keepax 		if (ret < 0) {
907b8135864SKuninori Morimoto 			dev_err(component->dev,
908b8135864SKuninori Morimoto 				"ASoC: %s hw params failed: %d\n",
909244e2936SCharles Keepax 				component->name, ret);
910b8135864SKuninori Morimoto 			goto component_err;
911244e2936SCharles Keepax 		}
912244e2936SCharles Keepax 	}
913244e2936SCharles Keepax 	component = NULL;
914b8135864SKuninori Morimoto 
915ddee627cSLiam Girdwood out:
91672b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
917ddee627cSLiam Girdwood 	return ret;
918ddee627cSLiam Girdwood 
919b8135864SKuninori Morimoto component_err:
920244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, component);
921b8135864SKuninori Morimoto 
922846faaedSKuninori Morimoto 	snd_soc_dai_hw_free(cpu_dai, substream);
9232371abdcSKuninori Morimoto 	cpu_dai->rate = 0;
924ddee627cSLiam Girdwood 
925ddee627cSLiam Girdwood interface_err:
9262e5894d7SBenoit Cousson 	i = rtd->num_codecs;
927ddee627cSLiam Girdwood 
928ddee627cSLiam Girdwood codec_err:
9296d11b128SKuninori Morimoto 	for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
930f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
931f47b9ad9SJerome Brunet 			continue;
932f47b9ad9SJerome Brunet 
933846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
9342e5894d7SBenoit Cousson 		codec_dai->rate = 0;
9352e5894d7SBenoit Cousson 	}
9362e5894d7SBenoit Cousson 
93749f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
938ddee627cSLiam Girdwood 
93972b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
940ddee627cSLiam Girdwood 	return ret;
941ddee627cSLiam Girdwood }
942ddee627cSLiam Girdwood 
943ddee627cSLiam Girdwood /*
944ddee627cSLiam Girdwood  * Frees resources allocated by hw_params, can be called multiple times
945ddee627cSLiam Girdwood  */
946ddee627cSLiam Girdwood static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
947ddee627cSLiam Girdwood {
948ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
949ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
9502e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
9517f62b6eeSNicolin Chen 	bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
9522e5894d7SBenoit Cousson 	int i;
953ddee627cSLiam Girdwood 
95472b745e3SPeter Ujfalusi 	mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
955ddee627cSLiam Girdwood 
956d3383420SNicolin Chen 	/* clear the corresponding DAIs parameters when going to be inactive */
957d3383420SNicolin Chen 	if (cpu_dai->active == 1) {
958d3383420SNicolin Chen 		cpu_dai->rate = 0;
959d3383420SNicolin Chen 		cpu_dai->channels = 0;
960d3383420SNicolin Chen 		cpu_dai->sample_bits = 0;
961d3383420SNicolin Chen 	}
962d3383420SNicolin Chen 
9630b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
964d3383420SNicolin Chen 		if (codec_dai->active == 1) {
965d3383420SNicolin Chen 			codec_dai->rate = 0;
966d3383420SNicolin Chen 			codec_dai->channels = 0;
967d3383420SNicolin Chen 			codec_dai->sample_bits = 0;
968d3383420SNicolin Chen 		}
9692e5894d7SBenoit Cousson 	}
970d3383420SNicolin Chen 
971ddee627cSLiam Girdwood 	/* apply codec digital mute */
9720b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
9730b7990e3SKuninori Morimoto 		if ((playback && codec_dai->playback_active == 1) ||
9740b7990e3SKuninori Morimoto 		    (!playback && codec_dai->capture_active == 1))
9750b7990e3SKuninori Morimoto 			snd_soc_dai_digital_mute(codec_dai, 1,
9762e5894d7SBenoit Cousson 						 substream->stream);
9772e5894d7SBenoit Cousson 	}
978ddee627cSLiam Girdwood 
979ddee627cSLiam Girdwood 	/* free any machine hw params */
98049f020e5SKuninori Morimoto 	soc_rtd_hw_free(rtd, substream);
981ddee627cSLiam Girdwood 
982b8135864SKuninori Morimoto 	/* free any component resources */
983244e2936SCharles Keepax 	soc_pcm_components_hw_free(substream, NULL);
984b8135864SKuninori Morimoto 
985ddee627cSLiam Girdwood 	/* now free hw params for the DAIs  */
9860b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
987f47b9ad9SJerome Brunet 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
988f47b9ad9SJerome Brunet 			continue;
989f47b9ad9SJerome Brunet 
990846faaedSKuninori Morimoto 		snd_soc_dai_hw_free(codec_dai, substream);
9912e5894d7SBenoit Cousson 	}
992ddee627cSLiam Girdwood 
993846faaedSKuninori Morimoto 	snd_soc_dai_hw_free(cpu_dai, substream);
994ddee627cSLiam Girdwood 
99572b745e3SPeter Ujfalusi 	mutex_unlock(&rtd->card->pcm_mutex);
996ddee627cSLiam Girdwood 	return 0;
997ddee627cSLiam Girdwood }
998ddee627cSLiam Girdwood 
9994378f1fbSPeter Ujfalusi static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1000ddee627cSLiam Girdwood {
1001ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1002b8135864SKuninori Morimoto 	struct snd_soc_component *component;
1003ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10042e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10052e5894d7SBenoit Cousson 	int i, ret;
1006ddee627cSLiam Girdwood 
1007ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
1008ddee627cSLiam Girdwood 	if (ret < 0)
1009ddee627cSLiam Girdwood 		return ret;
1010ddee627cSLiam Girdwood 
1011613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
10125693d50cSKuninori Morimoto 		ret = snd_soc_component_trigger(component, substream, cmd);
1013b8135864SKuninori Morimoto 		if (ret < 0)
1014b8135864SKuninori Morimoto 			return ret;
1015b8135864SKuninori Morimoto 	}
1016b8135864SKuninori Morimoto 
1017901e822bSDan Carpenter 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1018ddee627cSLiam Girdwood 	if (ret < 0)
1019ddee627cSLiam Girdwood 		return ret;
10204792b0dbSJarkko Nikula 
10214378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10224378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
10234378f1fbSPeter Ujfalusi 		if (ret < 0)
10244378f1fbSPeter Ujfalusi 			return ret;
10254378f1fbSPeter Ujfalusi 	}
10264378f1fbSPeter Ujfalusi 
10274378f1fbSPeter Ujfalusi 	return 0;
10284378f1fbSPeter Ujfalusi }
10294378f1fbSPeter Ujfalusi 
10304378f1fbSPeter Ujfalusi static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
10314378f1fbSPeter Ujfalusi {
10324378f1fbSPeter Ujfalusi 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
10334378f1fbSPeter Ujfalusi 	struct snd_soc_component *component;
10344378f1fbSPeter Ujfalusi 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10354378f1fbSPeter Ujfalusi 	struct snd_soc_dai *codec_dai;
10364378f1fbSPeter Ujfalusi 	int i, ret;
10374378f1fbSPeter Ujfalusi 
10384378f1fbSPeter Ujfalusi 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10394378f1fbSPeter Ujfalusi 		ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
10404378f1fbSPeter Ujfalusi 		if (ret < 0)
10414378f1fbSPeter Ujfalusi 			return ret;
10424378f1fbSPeter Ujfalusi 	}
10434378f1fbSPeter Ujfalusi 
10444378f1fbSPeter Ujfalusi 	ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
10454378f1fbSPeter Ujfalusi 	if (ret < 0)
10464378f1fbSPeter Ujfalusi 		return ret;
10474378f1fbSPeter Ujfalusi 
1048613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
10494378f1fbSPeter Ujfalusi 		ret = snd_soc_component_trigger(component, substream, cmd);
10504378f1fbSPeter Ujfalusi 		if (ret < 0)
10514378f1fbSPeter Ujfalusi 			return ret;
10524378f1fbSPeter Ujfalusi 	}
10534378f1fbSPeter Ujfalusi 
1054ad2bf9f2SKuninori Morimoto 	ret = soc_rtd_trigger(rtd, substream, cmd);
10554792b0dbSJarkko Nikula 	if (ret < 0)
10564792b0dbSJarkko Nikula 		return ret;
10574792b0dbSJarkko Nikula 
1058ddee627cSLiam Girdwood 	return 0;
1059ddee627cSLiam Girdwood }
1060ddee627cSLiam Girdwood 
10614378f1fbSPeter Ujfalusi static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
10624378f1fbSPeter Ujfalusi {
10634378f1fbSPeter Ujfalusi 	int ret;
10644378f1fbSPeter Ujfalusi 
10654378f1fbSPeter Ujfalusi 	switch (cmd) {
10664378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_START:
10674378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_RESUME:
10684378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
10694378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_start(substream, cmd);
10704378f1fbSPeter Ujfalusi 		break;
10714378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_STOP:
10724378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_SUSPEND:
10734378f1fbSPeter Ujfalusi 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
10744378f1fbSPeter Ujfalusi 		ret = soc_pcm_trigger_stop(substream, cmd);
10754378f1fbSPeter Ujfalusi 		break;
10764378f1fbSPeter Ujfalusi 	default:
10774378f1fbSPeter Ujfalusi 		return -EINVAL;
10784378f1fbSPeter Ujfalusi 	}
10794378f1fbSPeter Ujfalusi 
10804378f1fbSPeter Ujfalusi 	return ret;
10814378f1fbSPeter Ujfalusi }
10824378f1fbSPeter Ujfalusi 
108345c0a188SMark Brown static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
108445c0a188SMark Brown 				   int cmd)
108507bf84aaSLiam Girdwood {
108607bf84aaSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
108707bf84aaSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
10882e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
10892e5894d7SBenoit Cousson 	int i, ret;
109007bf84aaSLiam Girdwood 
10910b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
10925c0769afSKuninori Morimoto 		ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
109307bf84aaSLiam Girdwood 		if (ret < 0)
109407bf84aaSLiam Girdwood 			return ret;
109507bf84aaSLiam Girdwood 	}
109607bf84aaSLiam Girdwood 
1097901e822bSDan Carpenter 	ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
109807bf84aaSLiam Girdwood 	if (ret < 0)
109907bf84aaSLiam Girdwood 		return ret;
11005c0769afSKuninori Morimoto 
110107bf84aaSLiam Girdwood 	return 0;
110207bf84aaSLiam Girdwood }
1103ddee627cSLiam Girdwood /*
1104ddee627cSLiam Girdwood  * soc level wrapper for pointer callback
1105ef050becSCharles Keepax  * If cpu_dai, codec_dai, component driver has the delay callback, then
1106ddee627cSLiam Girdwood  * the runtime->delay will be updated accordingly.
1107ddee627cSLiam Girdwood  */
1108ddee627cSLiam Girdwood static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1109ddee627cSLiam Girdwood {
1110ddee627cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1111ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
11122e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
1113ddee627cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
1114ddee627cSLiam Girdwood 	snd_pcm_uframes_t offset = 0;
1115ddee627cSLiam Girdwood 	snd_pcm_sframes_t delay = 0;
11162e5894d7SBenoit Cousson 	snd_pcm_sframes_t codec_delay = 0;
11172e5894d7SBenoit Cousson 	int i;
1118ddee627cSLiam Girdwood 
11199fb4c2bfSAkshu Agrawal 	/* clearing the previous total delay */
11209fb4c2bfSAkshu Agrawal 	runtime->delay = 0;
11219fb4c2bfSAkshu Agrawal 
11220035e256SKuninori Morimoto 	offset = snd_soc_pcm_component_pointer(substream);
1123b8135864SKuninori Morimoto 
11249fb4c2bfSAkshu Agrawal 	/* base delay if assigned in pointer callback */
11259fb4c2bfSAkshu Agrawal 	delay = runtime->delay;
1126b8135864SKuninori Morimoto 
11271dea80d4SKuninori Morimoto 	delay += snd_soc_dai_delay(cpu_dai, substream);
1128ddee627cSLiam Girdwood 
11290b7990e3SKuninori Morimoto 	for_each_rtd_codec_dai(rtd, i, codec_dai) {
11302e5894d7SBenoit Cousson 		codec_delay = max(codec_delay,
11311dea80d4SKuninori Morimoto 				  snd_soc_dai_delay(codec_dai, substream));
11322e5894d7SBenoit Cousson 	}
11332e5894d7SBenoit Cousson 	delay += codec_delay;
1134ddee627cSLiam Girdwood 
1135ddee627cSLiam Girdwood 	runtime->delay = delay;
1136ddee627cSLiam Girdwood 
1137ddee627cSLiam Girdwood 	return offset;
1138ddee627cSLiam Girdwood }
1139ddee627cSLiam Girdwood 
114001d7584cSLiam Girdwood /* connect a FE and BE */
114101d7584cSLiam Girdwood static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
114201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
114301d7584cSLiam Girdwood {
114401d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1145a9764869SKaiChieh Chuang 	unsigned long flags;
1146bd0b609eSTakashi Iwai #ifdef CONFIG_DEBUG_FS
11470632fa04SHans de Goede 	char *name;
1148bd0b609eSTakashi Iwai #endif
114901d7584cSLiam Girdwood 
115001d7584cSLiam Girdwood 	/* only add new dpcms */
11518d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
115201d7584cSLiam Girdwood 		if (dpcm->be == be && dpcm->fe == fe)
115301d7584cSLiam Girdwood 			return 0;
115401d7584cSLiam Girdwood 	}
115501d7584cSLiam Girdwood 
115601d7584cSLiam Girdwood 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
115701d7584cSLiam Girdwood 	if (!dpcm)
115801d7584cSLiam Girdwood 		return -ENOMEM;
115901d7584cSLiam Girdwood 
116001d7584cSLiam Girdwood 	dpcm->be = be;
116101d7584cSLiam Girdwood 	dpcm->fe = fe;
116201d7584cSLiam Girdwood 	be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
116301d7584cSLiam Girdwood 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1164a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
116501d7584cSLiam Girdwood 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
116601d7584cSLiam Girdwood 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1167a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
116801d7584cSLiam Girdwood 
116901d7584cSLiam Girdwood 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
117001d7584cSLiam Girdwood 			stream ? "capture" : "playback",  fe->dai_link->name,
117101d7584cSLiam Girdwood 			stream ? "<-" : "->", be->dai_link->name);
117201d7584cSLiam Girdwood 
1173f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
11740632fa04SHans de Goede 	name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
11750632fa04SHans de Goede 			 stream ? "capture" : "playback");
11760632fa04SHans de Goede 	if (name) {
11770632fa04SHans de Goede 		dpcm->debugfs_state = debugfs_create_dir(name,
1178fee531d6SGreg Kroah-Hartman 							 fe->debugfs_dpcm_root);
11790632fa04SHans de Goede 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
11800632fa04SHans de Goede 				   &dpcm->state);
11810632fa04SHans de Goede 		kfree(name);
11820632fa04SHans de Goede 	}
1183f86dcef8SLiam Girdwood #endif
118401d7584cSLiam Girdwood 	return 1;
118501d7584cSLiam Girdwood }
118601d7584cSLiam Girdwood 
118701d7584cSLiam Girdwood /* reparent a BE onto another FE */
118801d7584cSLiam Girdwood static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
118901d7584cSLiam Girdwood 			struct snd_soc_pcm_runtime *be, int stream)
119001d7584cSLiam Girdwood {
119101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
119201d7584cSLiam Girdwood 	struct snd_pcm_substream *fe_substream, *be_substream;
119301d7584cSLiam Girdwood 
119401d7584cSLiam Girdwood 	/* reparent if BE is connected to other FEs */
119501d7584cSLiam Girdwood 	if (!be->dpcm[stream].users)
119601d7584cSLiam Girdwood 		return;
119701d7584cSLiam Girdwood 
119801d7584cSLiam Girdwood 	be_substream = snd_soc_dpcm_get_substream(be, stream);
119901d7584cSLiam Girdwood 
1200d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
120101d7584cSLiam Girdwood 		if (dpcm->fe == fe)
120201d7584cSLiam Girdwood 			continue;
120301d7584cSLiam Girdwood 
120401d7584cSLiam Girdwood 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
120501d7584cSLiam Girdwood 			stream ? "capture" : "playback",
120601d7584cSLiam Girdwood 			dpcm->fe->dai_link->name,
120701d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
120801d7584cSLiam Girdwood 
120901d7584cSLiam Girdwood 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
121001d7584cSLiam Girdwood 		be_substream->runtime = fe_substream->runtime;
121101d7584cSLiam Girdwood 		break;
121201d7584cSLiam Girdwood 	}
121301d7584cSLiam Girdwood }
121401d7584cSLiam Girdwood 
121501d7584cSLiam Girdwood /* disconnect a BE and FE */
121623607025SLiam Girdwood void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
121701d7584cSLiam Girdwood {
121801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm, *d;
1219a9764869SKaiChieh Chuang 	unsigned long flags;
122001d7584cSLiam Girdwood 
12218d6258a4SKuninori Morimoto 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1222103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
122301d7584cSLiam Girdwood 				stream ? "capture" : "playback",
122401d7584cSLiam Girdwood 				dpcm->be->dai_link->name);
122501d7584cSLiam Girdwood 
122601d7584cSLiam Girdwood 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
122701d7584cSLiam Girdwood 			continue;
122801d7584cSLiam Girdwood 
122901d7584cSLiam Girdwood 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
123001d7584cSLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name,
123101d7584cSLiam Girdwood 			stream ? "<-" : "->", dpcm->be->dai_link->name);
123201d7584cSLiam Girdwood 
123301d7584cSLiam Girdwood 		/* BEs still alive need new FE */
123401d7584cSLiam Girdwood 		dpcm_be_reparent(fe, dpcm->be, stream);
123501d7584cSLiam Girdwood 
1236f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
1237fee531d6SGreg Kroah-Hartman 		debugfs_remove_recursive(dpcm->debugfs_state);
1238f86dcef8SLiam Girdwood #endif
1239a9764869SKaiChieh Chuang 		spin_lock_irqsave(&fe->card->dpcm_lock, flags);
124001d7584cSLiam Girdwood 		list_del(&dpcm->list_be);
124101d7584cSLiam Girdwood 		list_del(&dpcm->list_fe);
1242a9764869SKaiChieh Chuang 		spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
124301d7584cSLiam Girdwood 		kfree(dpcm);
124401d7584cSLiam Girdwood 	}
124501d7584cSLiam Girdwood }
124601d7584cSLiam Girdwood 
124701d7584cSLiam Girdwood /* get BE for DAI widget and stream */
124801d7584cSLiam Girdwood static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
124901d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget, int stream)
125001d7584cSLiam Girdwood {
125101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
12527afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
12531a497983SMengdong Lin 	int i;
125401d7584cSLiam Girdwood 
12553c146465SLiam Girdwood 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
12563c146465SLiam Girdwood 
125701d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1258bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, be) {
125901d7584cSLiam Girdwood 
126035ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
126135ea0655SLiam Girdwood 				continue;
126235ea0655SLiam Girdwood 
12633c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
12643c146465SLiam Girdwood 				be->cpu_dai->playback_widget ?
12653c146465SLiam Girdwood 				be->cpu_dai->playback_widget->name : "(not set)");
12663c146465SLiam Girdwood 
12672e5894d7SBenoit Cousson 			if (be->cpu_dai->playback_widget == widget)
126801d7584cSLiam Girdwood 				return be;
12692e5894d7SBenoit Cousson 
12707afecb30SKuninori Morimoto 			for_each_rtd_codec_dai(be, i, dai) {
12712e5894d7SBenoit Cousson 				if (dai->playback_widget == widget)
12722e5894d7SBenoit Cousson 					return be;
12732e5894d7SBenoit Cousson 			}
127401d7584cSLiam Girdwood 		}
127501d7584cSLiam Girdwood 	} else {
127601d7584cSLiam Girdwood 
1277bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, be) {
127801d7584cSLiam Girdwood 
127935ea0655SLiam Girdwood 			if (!be->dai_link->no_pcm)
128035ea0655SLiam Girdwood 				continue;
128135ea0655SLiam Girdwood 
12823c146465SLiam Girdwood 			dev_dbg(card->dev, "ASoC: try BE %s\n",
12833c146465SLiam Girdwood 				be->cpu_dai->capture_widget ?
12843c146465SLiam Girdwood 				be->cpu_dai->capture_widget->name : "(not set)");
12853c146465SLiam Girdwood 
12862e5894d7SBenoit Cousson 			if (be->cpu_dai->capture_widget == widget)
128701d7584cSLiam Girdwood 				return be;
12882e5894d7SBenoit Cousson 
12897afecb30SKuninori Morimoto 			for_each_rtd_codec_dai(be, i, dai) {
12902e5894d7SBenoit Cousson 				if (dai->capture_widget == widget)
12912e5894d7SBenoit Cousson 					return be;
12922e5894d7SBenoit Cousson 			}
129301d7584cSLiam Girdwood 		}
129401d7584cSLiam Girdwood 	}
129501d7584cSLiam Girdwood 
12963c146465SLiam Girdwood 	/* dai link name and stream name set correctly ? */
1297103d84a3SLiam Girdwood 	dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
129801d7584cSLiam Girdwood 		stream ? "capture" : "playback", widget->name);
129901d7584cSLiam Girdwood 	return NULL;
130001d7584cSLiam Girdwood }
130101d7584cSLiam Girdwood 
130201d7584cSLiam Girdwood static inline struct snd_soc_dapm_widget *
130337018610SBenoit Cousson 	dai_get_widget(struct snd_soc_dai *dai, int stream)
130401d7584cSLiam Girdwood {
130501d7584cSLiam Girdwood 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
130637018610SBenoit Cousson 		return dai->playback_widget;
130701d7584cSLiam Girdwood 	else
130837018610SBenoit Cousson 		return dai->capture_widget;
130901d7584cSLiam Girdwood }
131001d7584cSLiam Girdwood 
131101d7584cSLiam Girdwood static int widget_in_list(struct snd_soc_dapm_widget_list *list,
131201d7584cSLiam Girdwood 		struct snd_soc_dapm_widget *widget)
131301d7584cSLiam Girdwood {
131409e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *w;
131501d7584cSLiam Girdwood 	int i;
131601d7584cSLiam Girdwood 
131709e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, w)
131809e88f8aSKuninori Morimoto 		if (widget == w)
131901d7584cSLiam Girdwood 			return 1;
132001d7584cSLiam Girdwood 
132101d7584cSLiam Girdwood 	return 0;
132201d7584cSLiam Girdwood }
132301d7584cSLiam Girdwood 
13245fdd022cSPiotr Stankiewicz static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
13255fdd022cSPiotr Stankiewicz 		enum snd_soc_dapm_direction dir)
13265fdd022cSPiotr Stankiewicz {
13275fdd022cSPiotr Stankiewicz 	struct snd_soc_card *card = widget->dapm->card;
13285fdd022cSPiotr Stankiewicz 	struct snd_soc_pcm_runtime *rtd;
13290b7990e3SKuninori Morimoto 	struct snd_soc_dai *dai;
13305fdd022cSPiotr Stankiewicz 	int i;
13315fdd022cSPiotr Stankiewicz 
13325fdd022cSPiotr Stankiewicz 	if (dir == SND_SOC_DAPM_DIR_OUT) {
1333bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13345fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
13355fdd022cSPiotr Stankiewicz 				continue;
13365fdd022cSPiotr Stankiewicz 
13375fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->playback_widget == widget)
13385fdd022cSPiotr Stankiewicz 				return true;
13395fdd022cSPiotr Stankiewicz 
13400b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, dai) {
13415fdd022cSPiotr Stankiewicz 				if (dai->playback_widget == widget)
13425fdd022cSPiotr Stankiewicz 					return true;
13435fdd022cSPiotr Stankiewicz 			}
13445fdd022cSPiotr Stankiewicz 		}
13455fdd022cSPiotr Stankiewicz 	} else { /* SND_SOC_DAPM_DIR_IN */
1346bcb1fd1fSKuninori Morimoto 		for_each_card_rtds(card, rtd) {
13475fdd022cSPiotr Stankiewicz 			if (!rtd->dai_link->no_pcm)
13485fdd022cSPiotr Stankiewicz 				continue;
13495fdd022cSPiotr Stankiewicz 
13505fdd022cSPiotr Stankiewicz 			if (rtd->cpu_dai->capture_widget == widget)
13515fdd022cSPiotr Stankiewicz 				return true;
13525fdd022cSPiotr Stankiewicz 
13530b7990e3SKuninori Morimoto 			for_each_rtd_codec_dai(rtd, i, dai) {
13545fdd022cSPiotr Stankiewicz 				if (dai->capture_widget == widget)
13555fdd022cSPiotr Stankiewicz 					return true;
13565fdd022cSPiotr Stankiewicz 			}
13575fdd022cSPiotr Stankiewicz 		}
13585fdd022cSPiotr Stankiewicz 	}
13595fdd022cSPiotr Stankiewicz 
13605fdd022cSPiotr Stankiewicz 	return false;
13615fdd022cSPiotr Stankiewicz }
13625fdd022cSPiotr Stankiewicz 
136323607025SLiam Girdwood int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
13641ce43acfSLars-Peter Clausen 	int stream, struct snd_soc_dapm_widget_list **list)
136501d7584cSLiam Girdwood {
136601d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = fe->cpu_dai;
136701d7584cSLiam Girdwood 	int paths;
136801d7584cSLiam Girdwood 
136901d7584cSLiam Girdwood 	/* get number of valid DAI paths and their widgets */
13706742064aSPiotr Stankiewicz 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
13715fdd022cSPiotr Stankiewicz 			dpcm_end_walk_at_be);
137201d7584cSLiam Girdwood 
1373103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
137401d7584cSLiam Girdwood 			stream ? "capture" : "playback");
137501d7584cSLiam Girdwood 
137601d7584cSLiam Girdwood 	return paths;
137701d7584cSLiam Girdwood }
137801d7584cSLiam Girdwood 
137901d7584cSLiam Girdwood static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
138001d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
138101d7584cSLiam Girdwood {
138201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
138301d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
138401d7584cSLiam Girdwood 	struct snd_soc_dapm_widget *widget;
13857afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
138601d7584cSLiam Girdwood 	int prune = 0;
1387bed646dcSKuninori Morimoto 	int do_prune;
138801d7584cSLiam Girdwood 
138901d7584cSLiam Girdwood 	/* Destroy any old FE <--> BE connections */
13908d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
13912e5894d7SBenoit Cousson 		unsigned int i;
139201d7584cSLiam Girdwood 
139301d7584cSLiam Girdwood 		/* is there a valid CPU DAI widget for this BE */
139437018610SBenoit Cousson 		widget = dai_get_widget(dpcm->be->cpu_dai, stream);
139501d7584cSLiam Girdwood 
139601d7584cSLiam Girdwood 		/* prune the BE if it's no longer in our active list */
139701d7584cSLiam Girdwood 		if (widget && widget_in_list(list, widget))
139801d7584cSLiam Girdwood 			continue;
139901d7584cSLiam Girdwood 
140001d7584cSLiam Girdwood 		/* is there a valid CODEC DAI widget for this BE */
1401bed646dcSKuninori Morimoto 		do_prune = 1;
14027afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(dpcm->be, i, dai) {
14032e5894d7SBenoit Cousson 			widget = dai_get_widget(dai, stream);
140401d7584cSLiam Girdwood 
140501d7584cSLiam Girdwood 			/* prune the BE if it's no longer in our active list */
140601d7584cSLiam Girdwood 			if (widget && widget_in_list(list, widget))
1407bed646dcSKuninori Morimoto 				do_prune = 0;
14082e5894d7SBenoit Cousson 		}
1409bed646dcSKuninori Morimoto 		if (!do_prune)
1410bed646dcSKuninori Morimoto 			continue;
141101d7584cSLiam Girdwood 
1412103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
141301d7584cSLiam Girdwood 			stream ? "capture" : "playback",
141401d7584cSLiam Girdwood 			dpcm->be->dai_link->name, fe->dai_link->name);
141501d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
141601d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
141701d7584cSLiam Girdwood 		prune++;
141801d7584cSLiam Girdwood 	}
141901d7584cSLiam Girdwood 
1420103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
142101d7584cSLiam Girdwood 	return prune;
142201d7584cSLiam Girdwood }
142301d7584cSLiam Girdwood 
142401d7584cSLiam Girdwood static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
142501d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list **list_)
142601d7584cSLiam Girdwood {
142701d7584cSLiam Girdwood 	struct snd_soc_card *card = fe->card;
142801d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list = *list_;
142901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *be;
143009e88f8aSKuninori Morimoto 	struct snd_soc_dapm_widget *widget;
143101d7584cSLiam Girdwood 	int i, new = 0, err;
143201d7584cSLiam Girdwood 
143301d7584cSLiam Girdwood 	/* Create any new FE <--> BE connections */
143409e88f8aSKuninori Morimoto 	for_each_dapm_widgets(list, i, widget) {
143501d7584cSLiam Girdwood 
143609e88f8aSKuninori Morimoto 		switch (widget->id) {
14374616274dSMark Brown 		case snd_soc_dapm_dai_in:
1438c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1439c5b8540dSKoro Chen 				continue;
1440c5b8540dSKoro Chen 			break;
14414616274dSMark Brown 		case snd_soc_dapm_dai_out:
1442c5b8540dSKoro Chen 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1443c5b8540dSKoro Chen 				continue;
14444616274dSMark Brown 			break;
14454616274dSMark Brown 		default:
144601d7584cSLiam Girdwood 			continue;
14474616274dSMark Brown 		}
144801d7584cSLiam Girdwood 
144901d7584cSLiam Girdwood 		/* is there a valid BE rtd for this widget */
145009e88f8aSKuninori Morimoto 		be = dpcm_get_be(card, widget, stream);
145101d7584cSLiam Girdwood 		if (!be) {
1452103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: no BE found for %s\n",
145309e88f8aSKuninori Morimoto 					widget->name);
145401d7584cSLiam Girdwood 			continue;
145501d7584cSLiam Girdwood 		}
145601d7584cSLiam Girdwood 
145701d7584cSLiam Girdwood 		/* make sure BE is a real BE */
145801d7584cSLiam Girdwood 		if (!be->dai_link->no_pcm)
145901d7584cSLiam Girdwood 			continue;
146001d7584cSLiam Girdwood 
146101d7584cSLiam Girdwood 		/* don't connect if FE is not running */
146223607025SLiam Girdwood 		if (!fe->dpcm[stream].runtime && !fe->fe_compr)
146301d7584cSLiam Girdwood 			continue;
146401d7584cSLiam Girdwood 
146501d7584cSLiam Girdwood 		/* newly connected FE and BE */
146601d7584cSLiam Girdwood 		err = dpcm_be_connect(fe, be, stream);
146701d7584cSLiam Girdwood 		if (err < 0) {
1468103d84a3SLiam Girdwood 			dev_err(fe->dev, "ASoC: can't connect %s\n",
146909e88f8aSKuninori Morimoto 				widget->name);
147001d7584cSLiam Girdwood 			break;
147101d7584cSLiam Girdwood 		} else if (err == 0) /* already connected */
147201d7584cSLiam Girdwood 			continue;
147301d7584cSLiam Girdwood 
147401d7584cSLiam Girdwood 		/* new */
147501d7584cSLiam Girdwood 		be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
147601d7584cSLiam Girdwood 		new++;
147701d7584cSLiam Girdwood 	}
147801d7584cSLiam Girdwood 
1479103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
148001d7584cSLiam Girdwood 	return new;
148101d7584cSLiam Girdwood }
148201d7584cSLiam Girdwood 
148301d7584cSLiam Girdwood /*
148401d7584cSLiam Girdwood  * Find the corresponding BE DAIs that source or sink audio to this
148501d7584cSLiam Girdwood  * FE substream.
148601d7584cSLiam Girdwood  */
148723607025SLiam Girdwood int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
148801d7584cSLiam Girdwood 	int stream, struct snd_soc_dapm_widget_list **list, int new)
148901d7584cSLiam Girdwood {
149001d7584cSLiam Girdwood 	if (new)
149101d7584cSLiam Girdwood 		return dpcm_add_paths(fe, stream, list);
149201d7584cSLiam Girdwood 	else
149301d7584cSLiam Girdwood 		return dpcm_prune_paths(fe, stream, list);
149401d7584cSLiam Girdwood }
149501d7584cSLiam Girdwood 
149623607025SLiam Girdwood void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
149701d7584cSLiam Girdwood {
149801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
1499a9764869SKaiChieh Chuang 	unsigned long flags;
150001d7584cSLiam Girdwood 
1501a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
15028d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
150301d7584cSLiam Girdwood 		dpcm->be->dpcm[stream].runtime_update =
150401d7584cSLiam Girdwood 						SND_SOC_DPCM_UPDATE_NO;
1505a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
150601d7584cSLiam Girdwood }
150701d7584cSLiam Girdwood 
150801d7584cSLiam Girdwood static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
150901d7584cSLiam Girdwood 	int stream)
151001d7584cSLiam Girdwood {
151101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
151201d7584cSLiam Girdwood 
151301d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15148d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
151501d7584cSLiam Girdwood 
151601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
151701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
151801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
151901d7584cSLiam Girdwood 
152001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1521103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
152201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
152301d7584cSLiam Girdwood 				be->dpcm[stream].state);
152401d7584cSLiam Girdwood 
152501d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
152601d7584cSLiam Girdwood 			continue;
152701d7584cSLiam Girdwood 
152801d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
152901d7584cSLiam Girdwood 			continue;
153001d7584cSLiam Girdwood 
153101d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
153201d7584cSLiam Girdwood 		be_substream->runtime = NULL;
153301d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
153401d7584cSLiam Girdwood 	}
153501d7584cSLiam Girdwood }
153601d7584cSLiam Girdwood 
153723607025SLiam Girdwood int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
153801d7584cSLiam Girdwood {
153901d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
154001d7584cSLiam Girdwood 	int err, count = 0;
154101d7584cSLiam Girdwood 
154201d7584cSLiam Girdwood 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
15438d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
154401d7584cSLiam Girdwood 
154501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
154601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
154701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
154801d7584cSLiam Girdwood 
15492062b4c5SRussell King - ARM Linux 		if (!be_substream) {
15502062b4c5SRussell King - ARM Linux 			dev_err(be->dev, "ASoC: no backend %s stream\n",
15512062b4c5SRussell King - ARM Linux 				stream ? "capture" : "playback");
15522062b4c5SRussell King - ARM Linux 			continue;
15532062b4c5SRussell King - ARM Linux 		}
15542062b4c5SRussell King - ARM Linux 
155501d7584cSLiam Girdwood 		/* is this op for this BE ? */
155601d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
155701d7584cSLiam Girdwood 			continue;
155801d7584cSLiam Girdwood 
155901d7584cSLiam Girdwood 		/* first time the dpcm is open ? */
156001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1561103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
156201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
156301d7584cSLiam Girdwood 				be->dpcm[stream].state);
156401d7584cSLiam Girdwood 
156501d7584cSLiam Girdwood 		if (be->dpcm[stream].users++ != 0)
156601d7584cSLiam Girdwood 			continue;
156701d7584cSLiam Girdwood 
156801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
156901d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
157001d7584cSLiam Girdwood 			continue;
157101d7584cSLiam Girdwood 
15722062b4c5SRussell King - ARM Linux 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
15732062b4c5SRussell King - ARM Linux 			stream ? "capture" : "playback", be->dai_link->name);
157401d7584cSLiam Girdwood 
157501d7584cSLiam Girdwood 		be_substream->runtime = be->dpcm[stream].runtime;
157601d7584cSLiam Girdwood 		err = soc_pcm_open(be_substream);
157701d7584cSLiam Girdwood 		if (err < 0) {
1578103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: BE open failed %d\n", err);
157901d7584cSLiam Girdwood 			be->dpcm[stream].users--;
158001d7584cSLiam Girdwood 			if (be->dpcm[stream].users < 0)
1581103d84a3SLiam Girdwood 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
158201d7584cSLiam Girdwood 					stream ? "capture" : "playback",
158301d7584cSLiam Girdwood 					be->dpcm[stream].state);
158401d7584cSLiam Girdwood 
158501d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
158601d7584cSLiam Girdwood 			goto unwind;
158701d7584cSLiam Girdwood 		}
158801d7584cSLiam Girdwood 
158901d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
159001d7584cSLiam Girdwood 		count++;
159101d7584cSLiam Girdwood 	}
159201d7584cSLiam Girdwood 
159301d7584cSLiam Girdwood 	return count;
159401d7584cSLiam Girdwood 
159501d7584cSLiam Girdwood unwind:
159601d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
15978d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
159801d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
159901d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
160001d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
160101d7584cSLiam Girdwood 
160201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
160301d7584cSLiam Girdwood 			continue;
160401d7584cSLiam Girdwood 
160501d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1606103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close %d\n",
160701d7584cSLiam Girdwood 				stream ? "capture" : "playback",
160801d7584cSLiam Girdwood 				be->dpcm[stream].state);
160901d7584cSLiam Girdwood 
161001d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
161101d7584cSLiam Girdwood 			continue;
161201d7584cSLiam Girdwood 
161301d7584cSLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
161401d7584cSLiam Girdwood 			continue;
161501d7584cSLiam Girdwood 
161601d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
161701d7584cSLiam Girdwood 		be_substream->runtime = NULL;
161801d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
161901d7584cSLiam Girdwood 	}
162001d7584cSLiam Girdwood 
162101d7584cSLiam Girdwood 	return err;
162201d7584cSLiam Girdwood }
162301d7584cSLiam Girdwood 
162408ae9b45SLars-Peter Clausen static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1625435ffb76SJerome Brunet 				 struct snd_soc_pcm_stream *stream)
162608ae9b45SLars-Peter Clausen {
162708ae9b45SLars-Peter Clausen 	runtime->hw.rate_min = stream->rate_min;
1628e33ffbd9SCharles Keepax 	runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
162908ae9b45SLars-Peter Clausen 	runtime->hw.channels_min = stream->channels_min;
163008ae9b45SLars-Peter Clausen 	runtime->hw.channels_max = stream->channels_max;
1631002220a9SLars-Peter Clausen 	if (runtime->hw.formats)
1632435ffb76SJerome Brunet 		runtime->hw.formats &= stream->formats;
1633002220a9SLars-Peter Clausen 	else
1634435ffb76SJerome Brunet 		runtime->hw.formats = stream->formats;
163508ae9b45SLars-Peter Clausen 	runtime->hw.rates = stream->rates;
163608ae9b45SLars-Peter Clausen }
163708ae9b45SLars-Peter Clausen 
1638435ffb76SJerome Brunet static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1639435ffb76SJerome Brunet 				      u64 *formats)
1640b073ed4eSKuninori Morimoto {
1641b073ed4eSKuninori Morimoto 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1642b073ed4eSKuninori Morimoto 	struct snd_soc_dpcm *dpcm;
16437afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
1644b073ed4eSKuninori Morimoto 	int stream = substream->stream;
1645b073ed4eSKuninori Morimoto 
1646b073ed4eSKuninori Morimoto 	if (!fe->dai_link->dpcm_merged_format)
1647435ffb76SJerome Brunet 		return;
1648b073ed4eSKuninori Morimoto 
1649b073ed4eSKuninori Morimoto 	/*
1650b073ed4eSKuninori Morimoto 	 * It returns merged BE codec format
1651b073ed4eSKuninori Morimoto 	 * if FE want to use it (= dpcm_merged_format)
1652b073ed4eSKuninori Morimoto 	 */
1653b073ed4eSKuninori Morimoto 
16548d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1655b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_runtime *be = dpcm->be;
1656b073ed4eSKuninori Morimoto 		struct snd_soc_dai_driver *codec_dai_drv;
1657b073ed4eSKuninori Morimoto 		struct snd_soc_pcm_stream *codec_stream;
1658b073ed4eSKuninori Morimoto 		int i;
1659b073ed4eSKuninori Morimoto 
16607afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
16614febced1SJerome Brunet 			/*
16624febced1SJerome Brunet 			 * Skip CODECs which don't support the current stream
16634febced1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
16644febced1SJerome Brunet 			 */
16657afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
16664febced1SJerome Brunet 				continue;
16674febced1SJerome Brunet 
16687afecb30SKuninori Morimoto 			codec_dai_drv = dai->driver;
1669b073ed4eSKuninori Morimoto 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1670b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->playback;
1671b073ed4eSKuninori Morimoto 			else
1672b073ed4eSKuninori Morimoto 				codec_stream = &codec_dai_drv->capture;
1673b073ed4eSKuninori Morimoto 
1674435ffb76SJerome Brunet 			*formats &= codec_stream->formats;
1675435ffb76SJerome Brunet 		}
1676b073ed4eSKuninori Morimoto 	}
1677b073ed4eSKuninori Morimoto }
1678b073ed4eSKuninori Morimoto 
1679435ffb76SJerome Brunet static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1680f4c277b8SJiada Wang 				    unsigned int *channels_min,
1681f4c277b8SJiada Wang 				    unsigned int *channels_max)
1682f4c277b8SJiada Wang {
1683f4c277b8SJiada Wang 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1684f4c277b8SJiada Wang 	struct snd_soc_dpcm *dpcm;
1685f4c277b8SJiada Wang 	int stream = substream->stream;
1686f4c277b8SJiada Wang 
1687f4c277b8SJiada Wang 	if (!fe->dai_link->dpcm_merged_chan)
1688f4c277b8SJiada Wang 		return;
1689f4c277b8SJiada Wang 
1690f4c277b8SJiada Wang 	/*
1691f4c277b8SJiada Wang 	 * It returns merged BE codec channel;
1692f4c277b8SJiada Wang 	 * if FE want to use it (= dpcm_merged_chan)
1693f4c277b8SJiada Wang 	 */
1694f4c277b8SJiada Wang 
16958d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1696f4c277b8SJiada Wang 		struct snd_soc_pcm_runtime *be = dpcm->be;
16974f2bd18bSJerome Brunet 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1698f4c277b8SJiada Wang 		struct snd_soc_dai_driver *codec_dai_drv;
1699f4c277b8SJiada Wang 		struct snd_soc_pcm_stream *codec_stream;
17004f2bd18bSJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
1701f4c277b8SJiada Wang 
17024f2bd18bSJerome Brunet 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
17034f2bd18bSJerome Brunet 			cpu_stream = &cpu_dai_drv->playback;
17044f2bd18bSJerome Brunet 		else
17054f2bd18bSJerome Brunet 			cpu_stream = &cpu_dai_drv->capture;
17064f2bd18bSJerome Brunet 
17074f2bd18bSJerome Brunet 		*channels_min = max(*channels_min, cpu_stream->channels_min);
17084f2bd18bSJerome Brunet 		*channels_max = min(*channels_max, cpu_stream->channels_max);
17094f2bd18bSJerome Brunet 
17104f2bd18bSJerome Brunet 		/*
17114f2bd18bSJerome Brunet 		 * chan min/max cannot be enforced if there are multiple CODEC
17124f2bd18bSJerome Brunet 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
17134f2bd18bSJerome Brunet 		 */
17144f2bd18bSJerome Brunet 		if (be->num_codecs == 1) {
17154f2bd18bSJerome Brunet 			codec_dai_drv = be->codec_dais[0]->driver;
17164f2bd18bSJerome Brunet 
1717f4c277b8SJiada Wang 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1718f4c277b8SJiada Wang 				codec_stream = &codec_dai_drv->playback;
1719f4c277b8SJiada Wang 			else
1720f4c277b8SJiada Wang 				codec_stream = &codec_dai_drv->capture;
1721f4c277b8SJiada Wang 
1722f4c277b8SJiada Wang 			*channels_min = max(*channels_min,
1723f4c277b8SJiada Wang 					    codec_stream->channels_min);
1724f4c277b8SJiada Wang 			*channels_max = min(*channels_max,
1725f4c277b8SJiada Wang 					    codec_stream->channels_max);
1726f4c277b8SJiada Wang 		}
1727f4c277b8SJiada Wang 	}
1728f4c277b8SJiada Wang }
1729f4c277b8SJiada Wang 
1730baacd8d1SJerome Brunet static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1731baacd8d1SJerome Brunet 				    unsigned int *rates,
1732baacd8d1SJerome Brunet 				    unsigned int *rate_min,
1733baacd8d1SJerome Brunet 				    unsigned int *rate_max)
1734baacd8d1SJerome Brunet {
1735baacd8d1SJerome Brunet 	struct snd_soc_pcm_runtime *fe = substream->private_data;
1736baacd8d1SJerome Brunet 	struct snd_soc_dpcm *dpcm;
1737baacd8d1SJerome Brunet 	int stream = substream->stream;
1738baacd8d1SJerome Brunet 
1739baacd8d1SJerome Brunet 	if (!fe->dai_link->dpcm_merged_rate)
1740baacd8d1SJerome Brunet 		return;
1741baacd8d1SJerome Brunet 
1742baacd8d1SJerome Brunet 	/*
1743baacd8d1SJerome Brunet 	 * It returns merged BE codec channel;
1744baacd8d1SJerome Brunet 	 * if FE want to use it (= dpcm_merged_chan)
1745baacd8d1SJerome Brunet 	 */
1746baacd8d1SJerome Brunet 
17478d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1748baacd8d1SJerome Brunet 		struct snd_soc_pcm_runtime *be = dpcm->be;
1749baacd8d1SJerome Brunet 		struct snd_soc_dai_driver *cpu_dai_drv =  be->cpu_dai->driver;
1750baacd8d1SJerome Brunet 		struct snd_soc_dai_driver *codec_dai_drv;
1751baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *codec_stream;
1752baacd8d1SJerome Brunet 		struct snd_soc_pcm_stream *cpu_stream;
17537afecb30SKuninori Morimoto 		struct snd_soc_dai *dai;
1754baacd8d1SJerome Brunet 		int i;
1755baacd8d1SJerome Brunet 
1756baacd8d1SJerome Brunet 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1757baacd8d1SJerome Brunet 			cpu_stream = &cpu_dai_drv->playback;
1758baacd8d1SJerome Brunet 		else
1759baacd8d1SJerome Brunet 			cpu_stream = &cpu_dai_drv->capture;
1760baacd8d1SJerome Brunet 
1761baacd8d1SJerome Brunet 		*rate_min = max(*rate_min, cpu_stream->rate_min);
1762baacd8d1SJerome Brunet 		*rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1763baacd8d1SJerome Brunet 		*rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1764baacd8d1SJerome Brunet 
17657afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
1766baacd8d1SJerome Brunet 			/*
1767baacd8d1SJerome Brunet 			 * Skip CODECs which don't support the current stream
1768baacd8d1SJerome Brunet 			 * type. See soc_pcm_init_runtime_hw() for more details
1769baacd8d1SJerome Brunet 			 */
17707afecb30SKuninori Morimoto 			if (!snd_soc_dai_stream_valid(dai, stream))
1771baacd8d1SJerome Brunet 				continue;
1772baacd8d1SJerome Brunet 
17737afecb30SKuninori Morimoto 			codec_dai_drv = dai->driver;
1774baacd8d1SJerome Brunet 			if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1775baacd8d1SJerome Brunet 				codec_stream = &codec_dai_drv->playback;
1776baacd8d1SJerome Brunet 			else
1777baacd8d1SJerome Brunet 				codec_stream = &codec_dai_drv->capture;
1778baacd8d1SJerome Brunet 
1779baacd8d1SJerome Brunet 			*rate_min = max(*rate_min, codec_stream->rate_min);
1780baacd8d1SJerome Brunet 			*rate_max = min_not_zero(*rate_max,
1781baacd8d1SJerome Brunet 						 codec_stream->rate_max);
1782baacd8d1SJerome Brunet 			*rates = snd_pcm_rate_mask_intersect(*rates,
1783baacd8d1SJerome Brunet 						codec_stream->rates);
1784baacd8d1SJerome Brunet 		}
1785baacd8d1SJerome Brunet 	}
1786baacd8d1SJerome Brunet }
1787baacd8d1SJerome Brunet 
178845c0a188SMark Brown static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
178901d7584cSLiam Girdwood {
179001d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = substream->runtime;
179101d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
179201d7584cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
179301d7584cSLiam Girdwood 	struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
179401d7584cSLiam Girdwood 
179508ae9b45SLars-Peter Clausen 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1796435ffb76SJerome Brunet 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
179708ae9b45SLars-Peter Clausen 	else
1798435ffb76SJerome Brunet 		dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1799f4c277b8SJiada Wang 
1800435ffb76SJerome Brunet 	dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1801435ffb76SJerome Brunet 	dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1802435ffb76SJerome Brunet 				&runtime->hw.channels_max);
1803baacd8d1SJerome Brunet 	dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1804baacd8d1SJerome Brunet 				&runtime->hw.rate_min, &runtime->hw.rate_max);
180501d7584cSLiam Girdwood }
180601d7584cSLiam Girdwood 
1807ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1808ea9d0d77STakashi Iwai 
1809ea9d0d77STakashi Iwai /* Set FE's runtime_update state; the state is protected via PCM stream lock
1810ea9d0d77STakashi Iwai  * for avoiding the race with trigger callback.
1811ea9d0d77STakashi Iwai  * If the state is unset and a trigger is pending while the previous operation,
1812ea9d0d77STakashi Iwai  * process the pending trigger action here.
1813ea9d0d77STakashi Iwai  */
1814ea9d0d77STakashi Iwai static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1815ea9d0d77STakashi Iwai 				     int stream, enum snd_soc_dpcm_update state)
1816ea9d0d77STakashi Iwai {
1817ea9d0d77STakashi Iwai 	struct snd_pcm_substream *substream =
1818ea9d0d77STakashi Iwai 		snd_soc_dpcm_get_substream(fe, stream);
1819ea9d0d77STakashi Iwai 
1820ea9d0d77STakashi Iwai 	snd_pcm_stream_lock_irq(substream);
1821ea9d0d77STakashi Iwai 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1822ea9d0d77STakashi Iwai 		dpcm_fe_dai_do_trigger(substream,
1823ea9d0d77STakashi Iwai 				       fe->dpcm[stream].trigger_pending - 1);
1824ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = 0;
1825ea9d0d77STakashi Iwai 	}
1826ea9d0d77STakashi Iwai 	fe->dpcm[stream].runtime_update = state;
1827ea9d0d77STakashi Iwai 	snd_pcm_stream_unlock_irq(substream);
1828ea9d0d77STakashi Iwai }
1829ea9d0d77STakashi Iwai 
1830906c7d69SPC Liao static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1831906c7d69SPC Liao 			       int stream)
1832906c7d69SPC Liao {
1833906c7d69SPC Liao 	struct snd_soc_dpcm *dpcm;
1834906c7d69SPC Liao 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1835906c7d69SPC Liao 	struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1836906c7d69SPC Liao 	int err;
1837906c7d69SPC Liao 
1838906c7d69SPC Liao 	/* apply symmetry for FE */
1839906c7d69SPC Liao 	if (soc_pcm_has_symmetry(fe_substream))
1840906c7d69SPC Liao 		fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1841906c7d69SPC Liao 
1842906c7d69SPC Liao 	/* Symmetry only applies if we've got an active stream. */
1843906c7d69SPC Liao 	if (fe_cpu_dai->active) {
1844906c7d69SPC Liao 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1845906c7d69SPC Liao 		if (err < 0)
1846906c7d69SPC Liao 			return err;
1847906c7d69SPC Liao 	}
1848906c7d69SPC Liao 
1849906c7d69SPC Liao 	/* apply symmetry for BE */
18508d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
1851906c7d69SPC Liao 		struct snd_soc_pcm_runtime *be = dpcm->be;
1852906c7d69SPC Liao 		struct snd_pcm_substream *be_substream =
1853906c7d69SPC Liao 			snd_soc_dpcm_get_substream(be, stream);
18546246f283SJerome Brunet 		struct snd_soc_pcm_runtime *rtd;
18550b7990e3SKuninori Morimoto 		struct snd_soc_dai *codec_dai;
1856906c7d69SPC Liao 		int i;
1857906c7d69SPC Liao 
18586246f283SJerome Brunet 		/* A backend may not have the requested substream */
18596246f283SJerome Brunet 		if (!be_substream)
18606246f283SJerome Brunet 			continue;
18616246f283SJerome Brunet 
18626246f283SJerome Brunet 		rtd = be_substream->private_data;
1863f1176614SJeeja KP 		if (rtd->dai_link->be_hw_params_fixup)
1864f1176614SJeeja KP 			continue;
1865f1176614SJeeja KP 
1866906c7d69SPC Liao 		if (soc_pcm_has_symmetry(be_substream))
1867906c7d69SPC Liao 			be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1868906c7d69SPC Liao 
1869906c7d69SPC Liao 		/* Symmetry only applies if we've got an active stream. */
1870906c7d69SPC Liao 		if (rtd->cpu_dai->active) {
187199bcedbdSKai Chieh Chuang 			err = soc_pcm_apply_symmetry(fe_substream,
187299bcedbdSKai Chieh Chuang 						     rtd->cpu_dai);
1873906c7d69SPC Liao 			if (err < 0)
1874906c7d69SPC Liao 				return err;
1875906c7d69SPC Liao 		}
1876906c7d69SPC Liao 
18770b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
18780b7990e3SKuninori Morimoto 			if (codec_dai->active) {
187999bcedbdSKai Chieh Chuang 				err = soc_pcm_apply_symmetry(fe_substream,
18800b7990e3SKuninori Morimoto 							     codec_dai);
1881906c7d69SPC Liao 				if (err < 0)
1882906c7d69SPC Liao 					return err;
1883906c7d69SPC Liao 			}
1884906c7d69SPC Liao 		}
1885906c7d69SPC Liao 	}
1886906c7d69SPC Liao 
1887906c7d69SPC Liao 	return 0;
1888906c7d69SPC Liao }
1889906c7d69SPC Liao 
189001d7584cSLiam Girdwood static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
189101d7584cSLiam Girdwood {
189201d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
189301d7584cSLiam Girdwood 	struct snd_pcm_runtime *runtime = fe_substream->runtime;
189401d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret = 0;
189501d7584cSLiam Girdwood 
1896ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
189701d7584cSLiam Girdwood 
189801d7584cSLiam Girdwood 	ret = dpcm_be_dai_startup(fe, fe_substream->stream);
189901d7584cSLiam Girdwood 	if (ret < 0) {
1900103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
190101d7584cSLiam Girdwood 		goto be_err;
190201d7584cSLiam Girdwood 	}
190301d7584cSLiam Girdwood 
1904103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
190501d7584cSLiam Girdwood 
190601d7584cSLiam Girdwood 	/* start the DAI frontend */
190701d7584cSLiam Girdwood 	ret = soc_pcm_open(fe_substream);
190801d7584cSLiam Girdwood 	if (ret < 0) {
1909103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
191001d7584cSLiam Girdwood 		goto unwind;
191101d7584cSLiam Girdwood 	}
191201d7584cSLiam Girdwood 
191301d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
191401d7584cSLiam Girdwood 
191501d7584cSLiam Girdwood 	dpcm_set_fe_runtime(fe_substream);
191601d7584cSLiam Girdwood 	snd_pcm_limit_hw_rates(runtime);
191701d7584cSLiam Girdwood 
1918906c7d69SPC Liao 	ret = dpcm_apply_symmetry(fe_substream, stream);
1919906c7d69SPC Liao 	if (ret < 0) {
1920906c7d69SPC Liao 		dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1921906c7d69SPC Liao 			ret);
1922906c7d69SPC Liao 		goto unwind;
1923906c7d69SPC Liao 	}
1924906c7d69SPC Liao 
1925ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
192601d7584cSLiam Girdwood 	return 0;
192701d7584cSLiam Girdwood 
192801d7584cSLiam Girdwood unwind:
192901d7584cSLiam Girdwood 	dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
193001d7584cSLiam Girdwood be_err:
1931ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
193201d7584cSLiam Girdwood 	return ret;
193301d7584cSLiam Girdwood }
193401d7584cSLiam Girdwood 
193523607025SLiam Girdwood int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
193601d7584cSLiam Girdwood {
193701d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
193801d7584cSLiam Girdwood 
193901d7584cSLiam Girdwood 	/* only shutdown BEs that are either sinks or sources to this FE DAI */
19408d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
194101d7584cSLiam Girdwood 
194201d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
194301d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
194401d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
194501d7584cSLiam Girdwood 
194601d7584cSLiam Girdwood 		/* is this op for this BE ? */
194701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
194801d7584cSLiam Girdwood 			continue;
194901d7584cSLiam Girdwood 
195001d7584cSLiam Girdwood 		if (be->dpcm[stream].users == 0)
1951103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
195201d7584cSLiam Girdwood 				stream ? "capture" : "playback",
195301d7584cSLiam Girdwood 				be->dpcm[stream].state);
195401d7584cSLiam Girdwood 
195501d7584cSLiam Girdwood 		if (--be->dpcm[stream].users != 0)
195601d7584cSLiam Girdwood 			continue;
195701d7584cSLiam Girdwood 
195801d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
19599c0ac70aSKai Chieh Chuang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
19609c0ac70aSKai Chieh Chuang 			soc_pcm_hw_free(be_substream);
19619c0ac70aSKai Chieh Chuang 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
19629c0ac70aSKai Chieh Chuang 		}
196301d7584cSLiam Girdwood 
1964103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: close BE %s\n",
196594d215ccS彭东林 			be->dai_link->name);
196601d7584cSLiam Girdwood 
196701d7584cSLiam Girdwood 		soc_pcm_close(be_substream);
196801d7584cSLiam Girdwood 		be_substream->runtime = NULL;
196901d7584cSLiam Girdwood 
197001d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
197101d7584cSLiam Girdwood 	}
197201d7584cSLiam Girdwood 	return 0;
197301d7584cSLiam Girdwood }
197401d7584cSLiam Girdwood 
197501d7584cSLiam Girdwood static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
197601d7584cSLiam Girdwood {
197701d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
197801d7584cSLiam Girdwood 	int stream = substream->stream;
197901d7584cSLiam Girdwood 
1980ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
198101d7584cSLiam Girdwood 
198201d7584cSLiam Girdwood 	/* shutdown the BEs */
198301d7584cSLiam Girdwood 	dpcm_be_dai_shutdown(fe, substream->stream);
198401d7584cSLiam Girdwood 
1985103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
198601d7584cSLiam Girdwood 
198701d7584cSLiam Girdwood 	/* now shutdown the frontend */
198801d7584cSLiam Girdwood 	soc_pcm_close(substream);
198901d7584cSLiam Girdwood 
199001d7584cSLiam Girdwood 	/* run the stream event for each BE */
1991b0edff42SKuninori Morimoto 	snd_soc_dapm_stream_stop(fe, stream);
199201d7584cSLiam Girdwood 
199301d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1994ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
199501d7584cSLiam Girdwood 	return 0;
199601d7584cSLiam Girdwood }
199701d7584cSLiam Girdwood 
199823607025SLiam Girdwood int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
199901d7584cSLiam Girdwood {
200001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
200101d7584cSLiam Girdwood 
200201d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
200301d7584cSLiam Girdwood 	 * to this frontend DAI */
20048d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
200501d7584cSLiam Girdwood 
200601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
200701d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
200801d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
200901d7584cSLiam Girdwood 
201001d7584cSLiam Girdwood 		/* is this op for this BE ? */
201101d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
201201d7584cSLiam Girdwood 			continue;
201301d7584cSLiam Girdwood 
201401d7584cSLiam Girdwood 		/* only free hw when no longer used - check all FEs */
201501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
201601d7584cSLiam Girdwood 				continue;
201701d7584cSLiam Girdwood 
201836fba62cSQiao Zhou 		/* do not free hw if this BE is used by other FE */
201936fba62cSQiao Zhou 		if (be->dpcm[stream].users > 1)
202036fba62cSQiao Zhou 			continue;
202136fba62cSQiao Zhou 
202201d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
202301d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
202401d7584cSLiam Girdwood 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
202508b27848SPatrick Lai 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
20265e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
20275e82d2beSVinod Koul 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
202801d7584cSLiam Girdwood 			continue;
202901d7584cSLiam Girdwood 
2030103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
203194d215ccS彭东林 			be->dai_link->name);
203201d7584cSLiam Girdwood 
203301d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
203401d7584cSLiam Girdwood 
203501d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
203601d7584cSLiam Girdwood 	}
203701d7584cSLiam Girdwood 
203801d7584cSLiam Girdwood 	return 0;
203901d7584cSLiam Girdwood }
204001d7584cSLiam Girdwood 
204145c0a188SMark Brown static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
204201d7584cSLiam Girdwood {
204301d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
204401d7584cSLiam Girdwood 	int err, stream = substream->stream;
204501d7584cSLiam Girdwood 
204601d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2047ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
204801d7584cSLiam Girdwood 
2049103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
205001d7584cSLiam Girdwood 
205101d7584cSLiam Girdwood 	/* call hw_free on the frontend */
205201d7584cSLiam Girdwood 	err = soc_pcm_hw_free(substream);
205301d7584cSLiam Girdwood 	if (err < 0)
2054103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
205501d7584cSLiam Girdwood 			fe->dai_link->name);
205601d7584cSLiam Girdwood 
205701d7584cSLiam Girdwood 	/* only hw_params backends that are either sinks or sources
205801d7584cSLiam Girdwood 	 * to this frontend DAI */
205901d7584cSLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
206001d7584cSLiam Girdwood 
206101d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2062ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
206301d7584cSLiam Girdwood 
206401d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
206501d7584cSLiam Girdwood 	return 0;
206601d7584cSLiam Girdwood }
206701d7584cSLiam Girdwood 
206823607025SLiam Girdwood int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
206901d7584cSLiam Girdwood {
207001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
207101d7584cSLiam Girdwood 	int ret;
207201d7584cSLiam Girdwood 
20738d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
207401d7584cSLiam Girdwood 
207501d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
207601d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
207701d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
207801d7584cSLiam Girdwood 
207901d7584cSLiam Girdwood 		/* is this op for this BE ? */
208001d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
208101d7584cSLiam Girdwood 			continue;
208201d7584cSLiam Girdwood 
208301d7584cSLiam Girdwood 		/* copy params for each dpcm */
208401d7584cSLiam Girdwood 		memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
208501d7584cSLiam Girdwood 				sizeof(struct snd_pcm_hw_params));
208601d7584cSLiam Girdwood 
208701d7584cSLiam Girdwood 		/* perform any hw_params fixups */
208801d7584cSLiam Girdwood 		if (be->dai_link->be_hw_params_fixup) {
208901d7584cSLiam Girdwood 			ret = be->dai_link->be_hw_params_fixup(be,
209001d7584cSLiam Girdwood 					&dpcm->hw_params);
209101d7584cSLiam Girdwood 			if (ret < 0) {
209201d7584cSLiam Girdwood 				dev_err(be->dev,
2093103d84a3SLiam Girdwood 					"ASoC: hw_params BE fixup failed %d\n",
209401d7584cSLiam Girdwood 					ret);
209501d7584cSLiam Girdwood 				goto unwind;
209601d7584cSLiam Girdwood 			}
209701d7584cSLiam Girdwood 		}
209801d7584cSLiam Girdwood 
2099ae061d2aSLibin Yang 		/* copy the fixed-up hw params for BE dai */
2100ae061d2aSLibin Yang 		memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2101ae061d2aSLibin Yang 		       sizeof(struct snd_pcm_hw_params));
2102ae061d2aSLibin Yang 
2103b0639bd2SKuninori Morimoto 		/* only allow hw_params() if no connected FEs are running */
2104b0639bd2SKuninori Morimoto 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2105b0639bd2SKuninori Morimoto 			continue;
2106b0639bd2SKuninori Morimoto 
2107b0639bd2SKuninori Morimoto 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2108b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2109b0639bd2SKuninori Morimoto 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2110b0639bd2SKuninori Morimoto 			continue;
2111b0639bd2SKuninori Morimoto 
2112b0639bd2SKuninori Morimoto 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
211394d215ccS彭东林 			be->dai_link->name);
2114b0639bd2SKuninori Morimoto 
211501d7584cSLiam Girdwood 		ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
211601d7584cSLiam Girdwood 		if (ret < 0) {
211701d7584cSLiam Girdwood 			dev_err(dpcm->be->dev,
2118103d84a3SLiam Girdwood 				"ASoC: hw_params BE failed %d\n", ret);
211901d7584cSLiam Girdwood 			goto unwind;
212001d7584cSLiam Girdwood 		}
212101d7584cSLiam Girdwood 
212201d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
212301d7584cSLiam Girdwood 	}
212401d7584cSLiam Girdwood 	return 0;
212501d7584cSLiam Girdwood 
212601d7584cSLiam Girdwood unwind:
212701d7584cSLiam Girdwood 	/* disable any enabled and non active backends */
21288d6258a4SKuninori Morimoto 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
212901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
213001d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
213101d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
213201d7584cSLiam Girdwood 
213301d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
213401d7584cSLiam Girdwood 			continue;
213501d7584cSLiam Girdwood 
213601d7584cSLiam Girdwood 		/* only allow hw_free() if no connected FEs are running */
213701d7584cSLiam Girdwood 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
213801d7584cSLiam Girdwood 			continue;
213901d7584cSLiam Girdwood 
214001d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
214101d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
214201d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
214301d7584cSLiam Girdwood 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
214401d7584cSLiam Girdwood 			continue;
214501d7584cSLiam Girdwood 
214601d7584cSLiam Girdwood 		soc_pcm_hw_free(be_substream);
214701d7584cSLiam Girdwood 	}
214801d7584cSLiam Girdwood 
214901d7584cSLiam Girdwood 	return ret;
215001d7584cSLiam Girdwood }
215101d7584cSLiam Girdwood 
215245c0a188SMark Brown static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
215301d7584cSLiam Girdwood 				 struct snd_pcm_hw_params *params)
215401d7584cSLiam Girdwood {
215501d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
215601d7584cSLiam Girdwood 	int ret, stream = substream->stream;
215701d7584cSLiam Girdwood 
215801d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2159ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
216001d7584cSLiam Girdwood 
216101d7584cSLiam Girdwood 	memcpy(&fe->dpcm[substream->stream].hw_params, params,
216201d7584cSLiam Girdwood 			sizeof(struct snd_pcm_hw_params));
216301d7584cSLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, substream->stream);
216401d7584cSLiam Girdwood 	if (ret < 0) {
2165103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
216601d7584cSLiam Girdwood 		goto out;
216701d7584cSLiam Girdwood 	}
216801d7584cSLiam Girdwood 
2169103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
217001d7584cSLiam Girdwood 			fe->dai_link->name, params_rate(params),
217101d7584cSLiam Girdwood 			params_channels(params), params_format(params));
217201d7584cSLiam Girdwood 
217301d7584cSLiam Girdwood 	/* call hw_params on the frontend */
217401d7584cSLiam Girdwood 	ret = soc_pcm_hw_params(substream, params);
217501d7584cSLiam Girdwood 	if (ret < 0) {
2176103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
217701d7584cSLiam Girdwood 		dpcm_be_dai_hw_free(fe, stream);
217801d7584cSLiam Girdwood 	 } else
217901d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
218001d7584cSLiam Girdwood 
218101d7584cSLiam Girdwood out:
2182ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
218301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
218401d7584cSLiam Girdwood 	return ret;
218501d7584cSLiam Girdwood }
218601d7584cSLiam Girdwood 
218701d7584cSLiam Girdwood static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
218801d7584cSLiam Girdwood 		struct snd_pcm_substream *substream, int cmd)
218901d7584cSLiam Girdwood {
219001d7584cSLiam Girdwood 	int ret;
219101d7584cSLiam Girdwood 
2192103d84a3SLiam Girdwood 	dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
219394d215ccS彭东林 			dpcm->be->dai_link->name, cmd);
219401d7584cSLiam Girdwood 
219501d7584cSLiam Girdwood 	ret = soc_pcm_trigger(substream, cmd);
219601d7584cSLiam Girdwood 	if (ret < 0)
2197103d84a3SLiam Girdwood 		dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
219801d7584cSLiam Girdwood 
219901d7584cSLiam Girdwood 	return ret;
220001d7584cSLiam Girdwood }
220101d7584cSLiam Girdwood 
220223607025SLiam Girdwood int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
220345c0a188SMark Brown 			       int cmd)
220401d7584cSLiam Girdwood {
220501d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
220601d7584cSLiam Girdwood 	int ret = 0;
220701d7584cSLiam Girdwood 
22088d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
220901d7584cSLiam Girdwood 
221001d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
221101d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
221201d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
221301d7584cSLiam Girdwood 
221401d7584cSLiam Girdwood 		/* is this op for this BE ? */
221501d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
221601d7584cSLiam Girdwood 			continue;
221701d7584cSLiam Girdwood 
221801d7584cSLiam Girdwood 		switch (cmd) {
221901d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_START:
222001d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
222101d7584cSLiam Girdwood 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
222201d7584cSLiam Girdwood 				continue;
222301d7584cSLiam Girdwood 
222401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
222501d7584cSLiam Girdwood 			if (ret)
222601d7584cSLiam Girdwood 				return ret;
222701d7584cSLiam Girdwood 
222801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
222901d7584cSLiam Girdwood 			break;
223001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_RESUME:
223101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
223201d7584cSLiam Girdwood 				continue;
223301d7584cSLiam Girdwood 
223401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
223501d7584cSLiam Girdwood 			if (ret)
223601d7584cSLiam Girdwood 				return ret;
223701d7584cSLiam Girdwood 
223801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
223901d7584cSLiam Girdwood 			break;
224001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
224101d7584cSLiam Girdwood 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
224201d7584cSLiam Girdwood 				continue;
224301d7584cSLiam Girdwood 
224401d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
224501d7584cSLiam Girdwood 			if (ret)
224601d7584cSLiam Girdwood 				return ret;
224701d7584cSLiam Girdwood 
224801d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
224901d7584cSLiam Girdwood 			break;
225001d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_STOP:
225101d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
225201d7584cSLiam Girdwood 				continue;
225301d7584cSLiam Girdwood 
225401d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
225501d7584cSLiam Girdwood 				continue;
225601d7584cSLiam Girdwood 
225701d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
225801d7584cSLiam Girdwood 			if (ret)
225901d7584cSLiam Girdwood 				return ret;
226001d7584cSLiam Girdwood 
226101d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
226201d7584cSLiam Girdwood 			break;
226301d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_SUSPEND:
2264868a6ca8SNicolin Chen 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
226501d7584cSLiam Girdwood 				continue;
226601d7584cSLiam Girdwood 
226701d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
226801d7584cSLiam Girdwood 				continue;
226901d7584cSLiam Girdwood 
227001d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
227101d7584cSLiam Girdwood 			if (ret)
227201d7584cSLiam Girdwood 				return ret;
227301d7584cSLiam Girdwood 
227401d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
227501d7584cSLiam Girdwood 			break;
227601d7584cSLiam Girdwood 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
227701d7584cSLiam Girdwood 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
227801d7584cSLiam Girdwood 				continue;
227901d7584cSLiam Girdwood 
228001d7584cSLiam Girdwood 			if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
228101d7584cSLiam Girdwood 				continue;
228201d7584cSLiam Girdwood 
228301d7584cSLiam Girdwood 			ret = dpcm_do_trigger(dpcm, be_substream, cmd);
228401d7584cSLiam Girdwood 			if (ret)
228501d7584cSLiam Girdwood 				return ret;
228601d7584cSLiam Girdwood 
228701d7584cSLiam Girdwood 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
228801d7584cSLiam Girdwood 			break;
228901d7584cSLiam Girdwood 		}
229001d7584cSLiam Girdwood 	}
229101d7584cSLiam Girdwood 
229201d7584cSLiam Girdwood 	return ret;
229301d7584cSLiam Girdwood }
229401d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
229501d7584cSLiam Girdwood 
2296acbf2774SRanjani Sridharan static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2297acbf2774SRanjani Sridharan 				  int cmd, bool fe_first)
2298acbf2774SRanjani Sridharan {
2299acbf2774SRanjani Sridharan 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2300acbf2774SRanjani Sridharan 	int ret;
2301acbf2774SRanjani Sridharan 
2302acbf2774SRanjani Sridharan 	/* call trigger on the frontend before the backend. */
2303acbf2774SRanjani Sridharan 	if (fe_first) {
2304acbf2774SRanjani Sridharan 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2305acbf2774SRanjani Sridharan 			fe->dai_link->name, cmd);
2306acbf2774SRanjani Sridharan 
2307acbf2774SRanjani Sridharan 		ret = soc_pcm_trigger(substream, cmd);
2308acbf2774SRanjani Sridharan 		if (ret < 0)
2309acbf2774SRanjani Sridharan 			return ret;
2310acbf2774SRanjani Sridharan 
2311acbf2774SRanjani Sridharan 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2312acbf2774SRanjani Sridharan 		return ret;
2313acbf2774SRanjani Sridharan 	}
2314acbf2774SRanjani Sridharan 
2315acbf2774SRanjani Sridharan 	/* call trigger on the frontend after the backend. */
2316acbf2774SRanjani Sridharan 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2317acbf2774SRanjani Sridharan 	if (ret < 0)
2318acbf2774SRanjani Sridharan 		return ret;
2319acbf2774SRanjani Sridharan 
2320acbf2774SRanjani Sridharan 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2321acbf2774SRanjani Sridharan 		fe->dai_link->name, cmd);
2322acbf2774SRanjani Sridharan 
2323acbf2774SRanjani Sridharan 	ret = soc_pcm_trigger(substream, cmd);
2324acbf2774SRanjani Sridharan 
2325acbf2774SRanjani Sridharan 	return ret;
2326acbf2774SRanjani Sridharan }
2327acbf2774SRanjani Sridharan 
2328ea9d0d77STakashi Iwai static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
232901d7584cSLiam Girdwood {
233001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2331acbf2774SRanjani Sridharan 	int stream = substream->stream;
2332acbf2774SRanjani Sridharan 	int ret = 0;
233301d7584cSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
233401d7584cSLiam Girdwood 
233501d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
233601d7584cSLiam Girdwood 
233701d7584cSLiam Girdwood 	switch (trigger) {
233801d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_PRE:
2339acbf2774SRanjani Sridharan 		switch (cmd) {
2340acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2341acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2342acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2343acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2344acbf2774SRanjani Sridharan 			break;
2345acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2346acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2347acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2348acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2349acbf2774SRanjani Sridharan 			break;
2350acbf2774SRanjani Sridharan 		default:
2351acbf2774SRanjani Sridharan 			ret = -EINVAL;
2352acbf2774SRanjani Sridharan 			break;
235301d7584cSLiam Girdwood 		}
235401d7584cSLiam Girdwood 		break;
235501d7584cSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_POST:
2356acbf2774SRanjani Sridharan 		switch (cmd) {
2357acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_START:
2358acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_RESUME:
2359acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2360acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2361acbf2774SRanjani Sridharan 			break;
2362acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_STOP:
2363acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_SUSPEND:
2364acbf2774SRanjani Sridharan 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2365acbf2774SRanjani Sridharan 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2366acbf2774SRanjani Sridharan 			break;
2367acbf2774SRanjani Sridharan 		default:
2368acbf2774SRanjani Sridharan 			ret = -EINVAL;
2369acbf2774SRanjani Sridharan 			break;
237001d7584cSLiam Girdwood 		}
237101d7584cSLiam Girdwood 		break;
237207bf84aaSLiam Girdwood 	case SND_SOC_DPCM_TRIGGER_BESPOKE:
237307bf84aaSLiam Girdwood 		/* bespoke trigger() - handles both FE and BEs */
237407bf84aaSLiam Girdwood 
2375103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
237607bf84aaSLiam Girdwood 				fe->dai_link->name, cmd);
237707bf84aaSLiam Girdwood 
237807bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, cmd);
237907bf84aaSLiam Girdwood 		break;
238001d7584cSLiam Girdwood 	default:
2381103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
238201d7584cSLiam Girdwood 				fe->dai_link->name);
238301d7584cSLiam Girdwood 		ret = -EINVAL;
238401d7584cSLiam Girdwood 		goto out;
238501d7584cSLiam Girdwood 	}
238601d7584cSLiam Girdwood 
2387acbf2774SRanjani Sridharan 	if (ret < 0) {
2388acbf2774SRanjani Sridharan 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2389acbf2774SRanjani Sridharan 			cmd, ret);
2390acbf2774SRanjani Sridharan 		goto out;
2391acbf2774SRanjani Sridharan 	}
2392acbf2774SRanjani Sridharan 
239301d7584cSLiam Girdwood 	switch (cmd) {
239401d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
239501d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_RESUME:
239601d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
239701d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
239801d7584cSLiam Girdwood 		break;
239901d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_STOP:
240001d7584cSLiam Girdwood 	case SNDRV_PCM_TRIGGER_SUSPEND:
240101d7584cSLiam Girdwood 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
240201d7584cSLiam Girdwood 		break;
24039f169b9fSPatrick Lai 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
24049f169b9fSPatrick Lai 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
24059f169b9fSPatrick Lai 		break;
240601d7584cSLiam Girdwood 	}
240701d7584cSLiam Girdwood 
240801d7584cSLiam Girdwood out:
240901d7584cSLiam Girdwood 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
241001d7584cSLiam Girdwood 	return ret;
241101d7584cSLiam Girdwood }
241201d7584cSLiam Girdwood 
2413ea9d0d77STakashi Iwai static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2414ea9d0d77STakashi Iwai {
2415ea9d0d77STakashi Iwai 	struct snd_soc_pcm_runtime *fe = substream->private_data;
2416ea9d0d77STakashi Iwai 	int stream = substream->stream;
2417ea9d0d77STakashi Iwai 
2418ea9d0d77STakashi Iwai 	/* if FE's runtime_update is already set, we're in race;
2419ea9d0d77STakashi Iwai 	 * process this trigger later at exit
2420ea9d0d77STakashi Iwai 	 */
2421ea9d0d77STakashi Iwai 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2422ea9d0d77STakashi Iwai 		fe->dpcm[stream].trigger_pending = cmd + 1;
2423ea9d0d77STakashi Iwai 		return 0; /* delayed, assuming it's successful */
2424ea9d0d77STakashi Iwai 	}
2425ea9d0d77STakashi Iwai 
2426ea9d0d77STakashi Iwai 	/* we're alone, let's trigger */
2427ea9d0d77STakashi Iwai 	return dpcm_fe_dai_do_trigger(substream, cmd);
2428ea9d0d77STakashi Iwai }
2429ea9d0d77STakashi Iwai 
243023607025SLiam Girdwood int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
243101d7584cSLiam Girdwood {
243201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
243301d7584cSLiam Girdwood 	int ret = 0;
243401d7584cSLiam Girdwood 
24358d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
243601d7584cSLiam Girdwood 
243701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
243801d7584cSLiam Girdwood 		struct snd_pcm_substream *be_substream =
243901d7584cSLiam Girdwood 			snd_soc_dpcm_get_substream(be, stream);
244001d7584cSLiam Girdwood 
244101d7584cSLiam Girdwood 		/* is this op for this BE ? */
244201d7584cSLiam Girdwood 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
244301d7584cSLiam Girdwood 			continue;
244401d7584cSLiam Girdwood 
244501d7584cSLiam Girdwood 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
244695f444dcSKoro Chen 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
24475087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
24485087a8f1SLibin Yang 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
244901d7584cSLiam Girdwood 			continue;
245001d7584cSLiam Girdwood 
2451103d84a3SLiam Girdwood 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
245294d215ccS彭东林 			be->dai_link->name);
245301d7584cSLiam Girdwood 
245401d7584cSLiam Girdwood 		ret = soc_pcm_prepare(be_substream);
245501d7584cSLiam Girdwood 		if (ret < 0) {
2456103d84a3SLiam Girdwood 			dev_err(be->dev, "ASoC: backend prepare failed %d\n",
245701d7584cSLiam Girdwood 				ret);
245801d7584cSLiam Girdwood 			break;
245901d7584cSLiam Girdwood 		}
246001d7584cSLiam Girdwood 
246101d7584cSLiam Girdwood 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
246201d7584cSLiam Girdwood 	}
246301d7584cSLiam Girdwood 	return ret;
246401d7584cSLiam Girdwood }
246501d7584cSLiam Girdwood 
246645c0a188SMark Brown static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
246701d7584cSLiam Girdwood {
246801d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = substream->private_data;
246901d7584cSLiam Girdwood 	int stream = substream->stream, ret = 0;
247001d7584cSLiam Girdwood 
247101d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
247201d7584cSLiam Girdwood 
2473103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
247401d7584cSLiam Girdwood 
2475ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
247601d7584cSLiam Girdwood 
247701d7584cSLiam Girdwood 	/* there is no point preparing this FE if there are no BEs */
247801d7584cSLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2479103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
248001d7584cSLiam Girdwood 				fe->dai_link->name);
248101d7584cSLiam Girdwood 		ret = -EINVAL;
248201d7584cSLiam Girdwood 		goto out;
248301d7584cSLiam Girdwood 	}
248401d7584cSLiam Girdwood 
248501d7584cSLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, substream->stream);
248601d7584cSLiam Girdwood 	if (ret < 0)
248701d7584cSLiam Girdwood 		goto out;
248801d7584cSLiam Girdwood 
248901d7584cSLiam Girdwood 	/* call prepare on the frontend */
249001d7584cSLiam Girdwood 	ret = soc_pcm_prepare(substream);
249101d7584cSLiam Girdwood 	if (ret < 0) {
2492103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
249301d7584cSLiam Girdwood 			fe->dai_link->name);
249401d7584cSLiam Girdwood 		goto out;
249501d7584cSLiam Girdwood 	}
249601d7584cSLiam Girdwood 
249701d7584cSLiam Girdwood 	/* run the stream event for each BE */
249801d7584cSLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
249901d7584cSLiam Girdwood 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
250001d7584cSLiam Girdwood 
250101d7584cSLiam Girdwood out:
2502ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
250301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
250401d7584cSLiam Girdwood 
250501d7584cSLiam Girdwood 	return ret;
250601d7584cSLiam Girdwood }
250701d7584cSLiam Girdwood 
2508618dae11SLiam Girdwood static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2509618dae11SLiam Girdwood {
251007bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
251107bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
251207bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2513618dae11SLiam Girdwood 	int err;
251401d7584cSLiam Girdwood 
2515103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2516618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2517618dae11SLiam Girdwood 
251807bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
251907bf84aaSLiam Girdwood 		/* call bespoke trigger - FE takes care of all BE triggers */
2520103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
252107bf84aaSLiam Girdwood 				fe->dai_link->name);
252207bf84aaSLiam Girdwood 
252307bf84aaSLiam Girdwood 		err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
252407bf84aaSLiam Girdwood 		if (err < 0)
2525103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
252607bf84aaSLiam Girdwood 	} else {
2527103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
252807bf84aaSLiam Girdwood 			fe->dai_link->name);
252907bf84aaSLiam Girdwood 
2530618dae11SLiam Girdwood 		err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2531618dae11SLiam Girdwood 		if (err < 0)
2532103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
253307bf84aaSLiam Girdwood 	}
2534618dae11SLiam Girdwood 
2535618dae11SLiam Girdwood 	err = dpcm_be_dai_hw_free(fe, stream);
2536618dae11SLiam Girdwood 	if (err < 0)
2537103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2538618dae11SLiam Girdwood 
2539618dae11SLiam Girdwood 	err = dpcm_be_dai_shutdown(fe, stream);
2540618dae11SLiam Girdwood 	if (err < 0)
2541103d84a3SLiam Girdwood 		dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2542618dae11SLiam Girdwood 
2543618dae11SLiam Girdwood 	/* run the stream event for each BE */
2544618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2545618dae11SLiam Girdwood 
2546618dae11SLiam Girdwood 	return 0;
2547618dae11SLiam Girdwood }
2548618dae11SLiam Girdwood 
2549618dae11SLiam Girdwood static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2550618dae11SLiam Girdwood {
255107bf84aaSLiam Girdwood 	struct snd_pcm_substream *substream =
255207bf84aaSLiam Girdwood 		snd_soc_dpcm_get_substream(fe, stream);
2553618dae11SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
255407bf84aaSLiam Girdwood 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2555618dae11SLiam Girdwood 	int ret;
2556a9764869SKaiChieh Chuang 	unsigned long flags;
2557618dae11SLiam Girdwood 
2558103d84a3SLiam Girdwood 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2559618dae11SLiam Girdwood 			stream ? "capture" : "playback", fe->dai_link->name);
2560618dae11SLiam Girdwood 
2561618dae11SLiam Girdwood 	/* Only start the BE if the FE is ready */
2562618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2563618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2564618dae11SLiam Girdwood 		return -EINVAL;
2565618dae11SLiam Girdwood 
2566618dae11SLiam Girdwood 	/* startup must always be called for new BEs */
2567618dae11SLiam Girdwood 	ret = dpcm_be_dai_startup(fe, stream);
2568fffc0ca2SDan Carpenter 	if (ret < 0)
2569618dae11SLiam Girdwood 		goto disconnect;
2570618dae11SLiam Girdwood 
2571618dae11SLiam Girdwood 	/* keep going if FE state is > open */
2572618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2573618dae11SLiam Girdwood 		return 0;
2574618dae11SLiam Girdwood 
2575618dae11SLiam Girdwood 	ret = dpcm_be_dai_hw_params(fe, stream);
2576fffc0ca2SDan Carpenter 	if (ret < 0)
2577618dae11SLiam Girdwood 		goto close;
2578618dae11SLiam Girdwood 
2579618dae11SLiam Girdwood 	/* keep going if FE state is > hw_params */
2580618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2581618dae11SLiam Girdwood 		return 0;
2582618dae11SLiam Girdwood 
2583618dae11SLiam Girdwood 
2584618dae11SLiam Girdwood 	ret = dpcm_be_dai_prepare(fe, stream);
2585fffc0ca2SDan Carpenter 	if (ret < 0)
2586618dae11SLiam Girdwood 		goto hw_free;
2587618dae11SLiam Girdwood 
2588618dae11SLiam Girdwood 	/* run the stream event for each BE */
2589618dae11SLiam Girdwood 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2590618dae11SLiam Girdwood 
2591618dae11SLiam Girdwood 	/* keep going if FE state is > prepare */
2592618dae11SLiam Girdwood 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2593618dae11SLiam Girdwood 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2594618dae11SLiam Girdwood 		return 0;
2595618dae11SLiam Girdwood 
259607bf84aaSLiam Girdwood 	if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
259707bf84aaSLiam Girdwood 		/* call trigger on the frontend - FE takes care of all BE triggers */
2598103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
259907bf84aaSLiam Girdwood 				fe->dai_link->name);
260007bf84aaSLiam Girdwood 
260107bf84aaSLiam Girdwood 		ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
260207bf84aaSLiam Girdwood 		if (ret < 0) {
2603103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
260407bf84aaSLiam Girdwood 			goto hw_free;
260507bf84aaSLiam Girdwood 		}
260607bf84aaSLiam Girdwood 	} else {
2607103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2608618dae11SLiam Girdwood 			fe->dai_link->name);
2609618dae11SLiam Girdwood 
2610618dae11SLiam Girdwood 		ret = dpcm_be_dai_trigger(fe, stream,
2611618dae11SLiam Girdwood 					SNDRV_PCM_TRIGGER_START);
2612618dae11SLiam Girdwood 		if (ret < 0) {
2613103d84a3SLiam Girdwood 			dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2614618dae11SLiam Girdwood 			goto hw_free;
2615618dae11SLiam Girdwood 		}
261607bf84aaSLiam Girdwood 	}
2617618dae11SLiam Girdwood 
2618618dae11SLiam Girdwood 	return 0;
2619618dae11SLiam Girdwood 
2620618dae11SLiam Girdwood hw_free:
2621618dae11SLiam Girdwood 	dpcm_be_dai_hw_free(fe, stream);
2622618dae11SLiam Girdwood close:
2623618dae11SLiam Girdwood 	dpcm_be_dai_shutdown(fe, stream);
2624618dae11SLiam Girdwood disconnect:
2625618dae11SLiam Girdwood 	/* disconnect any non started BEs */
2626a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
26278d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
2628618dae11SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
2629618dae11SLiam Girdwood 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2630618dae11SLiam Girdwood 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2631618dae11SLiam Girdwood 	}
2632a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2633618dae11SLiam Girdwood 
2634618dae11SLiam Girdwood 	return ret;
2635618dae11SLiam Girdwood }
2636618dae11SLiam Girdwood 
2637618dae11SLiam Girdwood static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2638618dae11SLiam Girdwood {
2639618dae11SLiam Girdwood 	int ret;
2640618dae11SLiam Girdwood 
2641ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2642618dae11SLiam Girdwood 	ret = dpcm_run_update_startup(fe, stream);
2643618dae11SLiam Girdwood 	if (ret < 0)
2644103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2645ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2646618dae11SLiam Girdwood 
2647618dae11SLiam Girdwood 	return ret;
2648618dae11SLiam Girdwood }
2649618dae11SLiam Girdwood 
2650618dae11SLiam Girdwood static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2651618dae11SLiam Girdwood {
2652618dae11SLiam Girdwood 	int ret;
2653618dae11SLiam Girdwood 
2654ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2655618dae11SLiam Girdwood 	ret = dpcm_run_update_shutdown(fe, stream);
2656618dae11SLiam Girdwood 	if (ret < 0)
2657103d84a3SLiam Girdwood 		dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2658ea9d0d77STakashi Iwai 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2659618dae11SLiam Girdwood 
2660618dae11SLiam Girdwood 	return ret;
2661618dae11SLiam Girdwood }
2662618dae11SLiam Girdwood 
2663de15d7ffSJerome Brunet static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2664618dae11SLiam Girdwood {
2665618dae11SLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
2666de15d7ffSJerome Brunet 	int count, paths;
2667618dae11SLiam Girdwood 
2668618dae11SLiam Girdwood 	if (!fe->dai_link->dynamic)
2669de15d7ffSJerome Brunet 		return 0;
2670618dae11SLiam Girdwood 
2671618dae11SLiam Girdwood 	/* only check active links */
2672618dae11SLiam Girdwood 	if (!fe->cpu_dai->active)
2673de15d7ffSJerome Brunet 		return 0;
2674618dae11SLiam Girdwood 
2675618dae11SLiam Girdwood 	/* DAPM sync will call this to update DSP paths */
2676de15d7ffSJerome Brunet 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2677de15d7ffSJerome Brunet 		new ? "new" : "old", fe->dai_link->name);
2678618dae11SLiam Girdwood 
2679618dae11SLiam Girdwood 	/* skip if FE doesn't have playback capability */
2680467fece8SKuninori Morimoto 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK) ||
2681467fece8SKuninori Morimoto 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
2682075207d2SQiao Zhou 		goto capture;
2683075207d2SQiao Zhou 
2684075207d2SQiao Zhou 	/* skip if FE isn't currently playing */
2685de15d7ffSJerome Brunet 	if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2686618dae11SLiam Girdwood 		goto capture;
2687618dae11SLiam Girdwood 
2688618dae11SLiam Girdwood 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2689618dae11SLiam Girdwood 	if (paths < 0) {
2690103d84a3SLiam Girdwood 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2691618dae11SLiam Girdwood 			 fe->dai_link->name,  "playback");
2692618dae11SLiam Girdwood 		return paths;
2693618dae11SLiam Girdwood 	}
2694618dae11SLiam Girdwood 
2695de15d7ffSJerome Brunet 	/* update any playback paths */
2696de15d7ffSJerome Brunet 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2697de15d7ffSJerome Brunet 	if (count) {
2698de15d7ffSJerome Brunet 		if (new)
2699618dae11SLiam Girdwood 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2700de15d7ffSJerome Brunet 		else
2701618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2702de15d7ffSJerome Brunet 
2703618dae11SLiam Girdwood 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2704618dae11SLiam Girdwood 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2705618dae11SLiam Girdwood 	}
2706618dae11SLiam Girdwood 
27077ed9de76SQiao Zhou 	dpcm_path_put(&list);
2708de15d7ffSJerome Brunet 
2709618dae11SLiam Girdwood capture:
2710618dae11SLiam Girdwood 	/* skip if FE doesn't have capture capability */
2711467fece8SKuninori Morimoto 	if (!snd_soc_dai_stream_valid(fe->cpu_dai,   SNDRV_PCM_STREAM_CAPTURE) ||
2712467fece8SKuninori Morimoto 	    !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
2713de15d7ffSJerome Brunet 		return 0;
2714075207d2SQiao Zhou 
2715075207d2SQiao Zhou 	/* skip if FE isn't currently capturing */
2716de15d7ffSJerome Brunet 	if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2717de15d7ffSJerome Brunet 		return 0;
2718618dae11SLiam Girdwood 
2719618dae11SLiam Girdwood 	paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2720618dae11SLiam Girdwood 	if (paths < 0) {
2721103d84a3SLiam Girdwood 		dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2722618dae11SLiam Girdwood 			 fe->dai_link->name,  "capture");
2723618dae11SLiam Girdwood 		return paths;
2724618dae11SLiam Girdwood 	}
2725618dae11SLiam Girdwood 
2726618dae11SLiam Girdwood 	/* update any old capture paths */
2727de15d7ffSJerome Brunet 	count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2728de15d7ffSJerome Brunet 	if (count) {
2729de15d7ffSJerome Brunet 		if (new)
2730de15d7ffSJerome Brunet 			dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2731de15d7ffSJerome Brunet 		else
2732618dae11SLiam Girdwood 			dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2733de15d7ffSJerome Brunet 
2734618dae11SLiam Girdwood 		dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2735618dae11SLiam Girdwood 		dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2736618dae11SLiam Girdwood 	}
2737618dae11SLiam Girdwood 
2738618dae11SLiam Girdwood 	dpcm_path_put(&list);
2739de15d7ffSJerome Brunet 
2740de15d7ffSJerome Brunet 	return 0;
2741618dae11SLiam Girdwood }
2742618dae11SLiam Girdwood 
2743de15d7ffSJerome Brunet /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2744de15d7ffSJerome Brunet  * any DAI links.
2745de15d7ffSJerome Brunet  */
2746de15d7ffSJerome Brunet int soc_dpcm_runtime_update(struct snd_soc_card *card)
2747de15d7ffSJerome Brunet {
2748de15d7ffSJerome Brunet 	struct snd_soc_pcm_runtime *fe;
2749de15d7ffSJerome Brunet 	int ret = 0;
2750de15d7ffSJerome Brunet 
2751de15d7ffSJerome Brunet 	mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2752de15d7ffSJerome Brunet 	/* shutdown all old paths first */
2753bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2754de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2755de15d7ffSJerome Brunet 		if (ret)
2756de15d7ffSJerome Brunet 			goto out;
2757de15d7ffSJerome Brunet 	}
2758de15d7ffSJerome Brunet 
2759de15d7ffSJerome Brunet 	/* bring new paths up */
2760bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, fe) {
2761de15d7ffSJerome Brunet 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2762de15d7ffSJerome Brunet 		if (ret)
2763de15d7ffSJerome Brunet 			goto out;
2764de15d7ffSJerome Brunet 	}
2765de15d7ffSJerome Brunet 
2766de15d7ffSJerome Brunet out:
2767618dae11SLiam Girdwood 	mutex_unlock(&card->mutex);
2768de15d7ffSJerome Brunet 	return ret;
2769618dae11SLiam Girdwood }
277001d7584cSLiam Girdwood int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
277101d7584cSLiam Girdwood {
277201d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
27737afecb30SKuninori Morimoto 	struct snd_soc_dai *dai;
277401d7584cSLiam Girdwood 
27758d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
277601d7584cSLiam Girdwood 
277701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
27782e5894d7SBenoit Cousson 		int i;
277901d7584cSLiam Girdwood 
278001d7584cSLiam Girdwood 		if (be->dai_link->ignore_suspend)
278101d7584cSLiam Girdwood 			continue;
278201d7584cSLiam Girdwood 
27837afecb30SKuninori Morimoto 		for_each_rtd_codec_dai(be, i, dai) {
27842e5894d7SBenoit Cousson 			struct snd_soc_dai_driver *drv = dai->driver;
278501d7584cSLiam Girdwood 
27862e5894d7SBenoit Cousson 			dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
27872e5894d7SBenoit Cousson 					 be->dai_link->name);
27882e5894d7SBenoit Cousson 
27892e5894d7SBenoit Cousson 			if (drv->ops && drv->ops->digital_mute &&
27902e5894d7SBenoit Cousson 							dai->playback_active)
279101d7584cSLiam Girdwood 				drv->ops->digital_mute(dai, mute);
279201d7584cSLiam Girdwood 		}
27932e5894d7SBenoit Cousson 	}
279401d7584cSLiam Girdwood 
279501d7584cSLiam Girdwood 	return 0;
279601d7584cSLiam Girdwood }
279701d7584cSLiam Girdwood 
279845c0a188SMark Brown static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
279901d7584cSLiam Girdwood {
280001d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
280101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
280201d7584cSLiam Girdwood 	struct snd_soc_dapm_widget_list *list;
280301d7584cSLiam Girdwood 	int ret;
280401d7584cSLiam Girdwood 	int stream = fe_substream->stream;
280501d7584cSLiam Girdwood 
280601d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
280701d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = fe_substream->runtime;
280801d7584cSLiam Girdwood 
28098f70e515SQiao Zhou 	ret = dpcm_path_get(fe, stream, &list);
28108f70e515SQiao Zhou 	if (ret < 0) {
28118f70e515SQiao Zhou 		mutex_unlock(&fe->card->mutex);
28128f70e515SQiao Zhou 		return ret;
28138f70e515SQiao Zhou 	} else if (ret == 0) {
2814103d84a3SLiam Girdwood 		dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
281501d7584cSLiam Girdwood 			fe->dai_link->name, stream ? "capture" : "playback");
281601d7584cSLiam Girdwood 	}
281701d7584cSLiam Girdwood 
281801d7584cSLiam Girdwood 	/* calculate valid and active FE <-> BE dpcms */
281901d7584cSLiam Girdwood 	dpcm_process_paths(fe, stream, &list, 1);
282001d7584cSLiam Girdwood 
282101d7584cSLiam Girdwood 	ret = dpcm_fe_dai_startup(fe_substream);
282201d7584cSLiam Girdwood 	if (ret < 0) {
282301d7584cSLiam Girdwood 		/* clean up all links */
28248d6258a4SKuninori Morimoto 		for_each_dpcm_be(fe, stream, dpcm)
282501d7584cSLiam Girdwood 			dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
282601d7584cSLiam Girdwood 
282701d7584cSLiam Girdwood 		dpcm_be_disconnect(fe, stream);
282801d7584cSLiam Girdwood 		fe->dpcm[stream].runtime = NULL;
282901d7584cSLiam Girdwood 	}
283001d7584cSLiam Girdwood 
283101d7584cSLiam Girdwood 	dpcm_clear_pending_state(fe, stream);
283201d7584cSLiam Girdwood 	dpcm_path_put(&list);
283301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
283401d7584cSLiam Girdwood 	return ret;
283501d7584cSLiam Girdwood }
283601d7584cSLiam Girdwood 
283745c0a188SMark Brown static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
283801d7584cSLiam Girdwood {
283901d7584cSLiam Girdwood 	struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
284001d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
284101d7584cSLiam Girdwood 	int stream = fe_substream->stream, ret;
284201d7584cSLiam Girdwood 
284301d7584cSLiam Girdwood 	mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
284401d7584cSLiam Girdwood 	ret = dpcm_fe_dai_shutdown(fe_substream);
284501d7584cSLiam Girdwood 
284601d7584cSLiam Girdwood 	/* mark FE's links ready to prune */
28478d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm)
284801d7584cSLiam Girdwood 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
284901d7584cSLiam Girdwood 
285001d7584cSLiam Girdwood 	dpcm_be_disconnect(fe, stream);
285101d7584cSLiam Girdwood 
285201d7584cSLiam Girdwood 	fe->dpcm[stream].runtime = NULL;
285301d7584cSLiam Girdwood 	mutex_unlock(&fe->card->mutex);
285401d7584cSLiam Girdwood 	return ret;
285501d7584cSLiam Girdwood }
285601d7584cSLiam Girdwood 
2857ddee627cSLiam Girdwood /* create a new pcm */
2858ddee627cSLiam Girdwood int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2859ddee627cSLiam Girdwood {
28602e5894d7SBenoit Cousson 	struct snd_soc_dai *codec_dai;
2861ddee627cSLiam Girdwood 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
28622b544dd7SKuninori Morimoto 	struct snd_soc_component *component;
2863ddee627cSLiam Girdwood 	struct snd_pcm *pcm;
2864ddee627cSLiam Girdwood 	char new_name[64];
2865ddee627cSLiam Girdwood 	int ret = 0, playback = 0, capture = 0;
28662e5894d7SBenoit Cousson 	int i;
2867ddee627cSLiam Girdwood 
286801d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
28691e9de42fSLiam Girdwood 		playback = rtd->dai_link->dpcm_playback;
28701e9de42fSLiam Girdwood 		capture = rtd->dai_link->dpcm_capture;
287101d7584cSLiam Girdwood 	} else {
2872a342031cSJerome Brunet 		/* Adapt stream for codec2codec links */
2873a342031cSJerome Brunet 		struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2874a342031cSJerome Brunet 			&cpu_dai->driver->playback : &cpu_dai->driver->capture;
2875a342031cSJerome Brunet 		struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2876a342031cSJerome Brunet 			&cpu_dai->driver->capture : &cpu_dai->driver->playback;
2877a342031cSJerome Brunet 
28780b7990e3SKuninori Morimoto 		for_each_rtd_codec_dai(rtd, i, codec_dai) {
2879467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2880af4bac11SSameer Pujar 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
2881ddee627cSLiam Girdwood 				playback = 1;
2882467fece8SKuninori Morimoto 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2883af4bac11SSameer Pujar 			    snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
2884ddee627cSLiam Girdwood 				capture = 1;
288501d7584cSLiam Girdwood 		}
2886a342031cSJerome Brunet 
2887a342031cSJerome Brunet 		capture = capture && cpu_capture->channels_min;
2888a342031cSJerome Brunet 		playback = playback && cpu_playback->channels_min;
28892e5894d7SBenoit Cousson 	}
28902e5894d7SBenoit Cousson 
2891d6bead02SFabio Estevam 	if (rtd->dai_link->playback_only) {
2892d6bead02SFabio Estevam 		playback = 1;
2893d6bead02SFabio Estevam 		capture = 0;
2894d6bead02SFabio Estevam 	}
2895d6bead02SFabio Estevam 
2896d6bead02SFabio Estevam 	if (rtd->dai_link->capture_only) {
2897d6bead02SFabio Estevam 		playback = 0;
2898d6bead02SFabio Estevam 		capture = 1;
2899d6bead02SFabio Estevam 	}
2900d6bead02SFabio Estevam 
290101d7584cSLiam Girdwood 	/* create the PCM */
2902a342031cSJerome Brunet 	if (rtd->dai_link->params) {
2903a342031cSJerome Brunet 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2904a342031cSJerome Brunet 			 rtd->dai_link->stream_name);
2905a342031cSJerome Brunet 
2906a342031cSJerome Brunet 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2907a342031cSJerome Brunet 					   playback, capture, &pcm);
2908a342031cSJerome Brunet 	} else if (rtd->dai_link->no_pcm) {
290901d7584cSLiam Girdwood 		snprintf(new_name, sizeof(new_name), "(%s)",
291001d7584cSLiam Girdwood 			rtd->dai_link->stream_name);
291101d7584cSLiam Girdwood 
291201d7584cSLiam Girdwood 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
291301d7584cSLiam Girdwood 				playback, capture, &pcm);
291401d7584cSLiam Girdwood 	} else {
291501d7584cSLiam Girdwood 		if (rtd->dai_link->dynamic)
291601d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s (*)",
291701d7584cSLiam Girdwood 				rtd->dai_link->stream_name);
291801d7584cSLiam Girdwood 		else
291901d7584cSLiam Girdwood 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
29202e5894d7SBenoit Cousson 				rtd->dai_link->stream_name,
29212e5894d7SBenoit Cousson 				(rtd->num_codecs > 1) ?
29222e5894d7SBenoit Cousson 				"multicodec" : rtd->codec_dai->name, num);
292301d7584cSLiam Girdwood 
292401d7584cSLiam Girdwood 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
292501d7584cSLiam Girdwood 			capture, &pcm);
292601d7584cSLiam Girdwood 	}
2927ddee627cSLiam Girdwood 	if (ret < 0) {
2928103d84a3SLiam Girdwood 		dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
29295cb9b748SLiam Girdwood 			rtd->dai_link->name);
2930ddee627cSLiam Girdwood 		return ret;
2931ddee627cSLiam Girdwood 	}
2932103d84a3SLiam Girdwood 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2933ddee627cSLiam Girdwood 
2934ddee627cSLiam Girdwood 	/* DAPM dai link stream work */
2935a342031cSJerome Brunet 	if (rtd->dai_link->params)
29364bf2e385SCurtis Malainey 		rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2937a342031cSJerome Brunet 	else
293883f94a2eSKuninori Morimoto 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2939ddee627cSLiam Girdwood 
294048c7699fSVinod Koul 	pcm->nonatomic = rtd->dai_link->nonatomic;
2941ddee627cSLiam Girdwood 	rtd->pcm = pcm;
2942ddee627cSLiam Girdwood 	pcm->private_data = rtd;
294301d7584cSLiam Girdwood 
2944a342031cSJerome Brunet 	if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
294501d7584cSLiam Girdwood 		if (playback)
294601d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
294701d7584cSLiam Girdwood 		if (capture)
294801d7584cSLiam Girdwood 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
294901d7584cSLiam Girdwood 		goto out;
295001d7584cSLiam Girdwood 	}
295101d7584cSLiam Girdwood 
295201d7584cSLiam Girdwood 	/* ASoC PCM operations */
295301d7584cSLiam Girdwood 	if (rtd->dai_link->dynamic) {
295401d7584cSLiam Girdwood 		rtd->ops.open		= dpcm_fe_dai_open;
295501d7584cSLiam Girdwood 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
295601d7584cSLiam Girdwood 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
295701d7584cSLiam Girdwood 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
295801d7584cSLiam Girdwood 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
295901d7584cSLiam Girdwood 		rtd->ops.close		= dpcm_fe_dai_close;
296001d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
296101d7584cSLiam Girdwood 	} else {
296201d7584cSLiam Girdwood 		rtd->ops.open		= soc_pcm_open;
296301d7584cSLiam Girdwood 		rtd->ops.hw_params	= soc_pcm_hw_params;
296401d7584cSLiam Girdwood 		rtd->ops.prepare	= soc_pcm_prepare;
296501d7584cSLiam Girdwood 		rtd->ops.trigger	= soc_pcm_trigger;
296601d7584cSLiam Girdwood 		rtd->ops.hw_free	= soc_pcm_hw_free;
296701d7584cSLiam Girdwood 		rtd->ops.close		= soc_pcm_close;
296801d7584cSLiam Girdwood 		rtd->ops.pointer	= soc_pcm_pointer;
296901d7584cSLiam Girdwood 	}
297001d7584cSLiam Girdwood 
2971613fb500SKuninori Morimoto 	for_each_rtd_components(rtd, i, component) {
29722b544dd7SKuninori Morimoto 		const struct snd_soc_component_driver *drv = component->driver;
2973b8135864SKuninori Morimoto 
29743b1c952cSTakashi Iwai 		if (drv->ioctl)
29753b1c952cSTakashi Iwai 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
29761e5ddb6bSTakashi Iwai 		if (drv->sync_stop)
29771e5ddb6bSTakashi Iwai 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
2978e9067bb5SKuninori Morimoto 		if (drv->copy_user)
297982d81f5cSKuninori Morimoto 			rtd->ops.copy_user	= snd_soc_pcm_component_copy_user;
2980e9067bb5SKuninori Morimoto 		if (drv->page)
29819c712e4fSKuninori Morimoto 			rtd->ops.page		= snd_soc_pcm_component_page;
2982e9067bb5SKuninori Morimoto 		if (drv->mmap)
2983205875e1SKuninori Morimoto 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
2984b8135864SKuninori Morimoto 	}
2985b8135864SKuninori Morimoto 
2986ddee627cSLiam Girdwood 	if (playback)
298701d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2988ddee627cSLiam Girdwood 
2989ddee627cSLiam Girdwood 	if (capture)
299001d7584cSLiam Girdwood 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2991ddee627cSLiam Girdwood 
2992b2b2afbbSKuninori Morimoto 	ret = snd_soc_pcm_component_new(rtd);
2993ddee627cSLiam Girdwood 	if (ret < 0) {
29947484291eSKuninori Morimoto 		dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2995ddee627cSLiam Girdwood 		return ret;
2996ddee627cSLiam Girdwood 	}
2997c641e5b2SJohan Hovold 
29983d21ef0bSTakashi Iwai 	pcm->no_device_suspend = true;
299901d7584cSLiam Girdwood out:
30002e5894d7SBenoit Cousson 	dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
30012e5894d7SBenoit Cousson 		 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3002ddee627cSLiam Girdwood 		 cpu_dai->name);
3003ddee627cSLiam Girdwood 	return ret;
3004ddee627cSLiam Girdwood }
300501d7584cSLiam Girdwood 
300601d7584cSLiam Girdwood /* is the current PCM operation for this FE ? */
300701d7584cSLiam Girdwood int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
300801d7584cSLiam Girdwood {
300901d7584cSLiam Girdwood 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
301001d7584cSLiam Girdwood 		return 1;
301101d7584cSLiam Girdwood 	return 0;
301201d7584cSLiam Girdwood }
301301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
301401d7584cSLiam Girdwood 
301501d7584cSLiam Girdwood /* is the current PCM operation for this BE ? */
301601d7584cSLiam Girdwood int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
301701d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
301801d7584cSLiam Girdwood {
301901d7584cSLiam Girdwood 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
302001d7584cSLiam Girdwood 	   ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
302101d7584cSLiam Girdwood 		  be->dpcm[stream].runtime_update))
302201d7584cSLiam Girdwood 		return 1;
302301d7584cSLiam Girdwood 	return 0;
302401d7584cSLiam Girdwood }
302501d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
302601d7584cSLiam Girdwood 
302701d7584cSLiam Girdwood /* get the substream for this BE */
302801d7584cSLiam Girdwood struct snd_pcm_substream *
302901d7584cSLiam Girdwood 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
303001d7584cSLiam Girdwood {
303101d7584cSLiam Girdwood 	return be->pcm->streams[stream].substream;
303201d7584cSLiam Girdwood }
303301d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
303401d7584cSLiam Girdwood 
303501d7584cSLiam Girdwood /* get the BE runtime state */
303601d7584cSLiam Girdwood enum snd_soc_dpcm_state
303701d7584cSLiam Girdwood 	snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
303801d7584cSLiam Girdwood {
303901d7584cSLiam Girdwood 	return be->dpcm[stream].state;
304001d7584cSLiam Girdwood }
304101d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
304201d7584cSLiam Girdwood 
304301d7584cSLiam Girdwood /* set the BE runtime state */
304401d7584cSLiam Girdwood void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
304501d7584cSLiam Girdwood 		int stream, enum snd_soc_dpcm_state state)
304601d7584cSLiam Girdwood {
304701d7584cSLiam Girdwood 	be->dpcm[stream].state = state;
304801d7584cSLiam Girdwood }
304901d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
305001d7584cSLiam Girdwood 
305101d7584cSLiam Girdwood /*
305201d7584cSLiam Girdwood  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
305301d7584cSLiam Girdwood  * are not running, paused or suspended for the specified stream direction.
305401d7584cSLiam Girdwood  */
305501d7584cSLiam Girdwood int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
305601d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
305701d7584cSLiam Girdwood {
305801d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
305901d7584cSLiam Girdwood 	int state;
3060a9764869SKaiChieh Chuang 	int ret = 1;
3061a9764869SKaiChieh Chuang 	unsigned long flags;
306201d7584cSLiam Girdwood 
3063a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3064d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
306501d7584cSLiam Girdwood 
306601d7584cSLiam Girdwood 		if (dpcm->fe == fe)
306701d7584cSLiam Girdwood 			continue;
306801d7584cSLiam Girdwood 
306901d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
307001d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
307101d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
3072a9764869SKaiChieh Chuang 			state == SND_SOC_DPCM_STATE_SUSPEND) {
3073a9764869SKaiChieh Chuang 			ret = 0;
3074a9764869SKaiChieh Chuang 			break;
307501d7584cSLiam Girdwood 		}
3076a9764869SKaiChieh Chuang 	}
3077a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
307801d7584cSLiam Girdwood 
307901d7584cSLiam Girdwood 	/* it's safe to free/stop this BE DAI */
3080a9764869SKaiChieh Chuang 	return ret;
308101d7584cSLiam Girdwood }
308201d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
308301d7584cSLiam Girdwood 
308401d7584cSLiam Girdwood /*
308501d7584cSLiam Girdwood  * We can only change hw params a BE DAI if any of it's FE are not prepared,
308601d7584cSLiam Girdwood  * running, paused or suspended for the specified stream direction.
308701d7584cSLiam Girdwood  */
308801d7584cSLiam Girdwood int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
308901d7584cSLiam Girdwood 		struct snd_soc_pcm_runtime *be, int stream)
309001d7584cSLiam Girdwood {
309101d7584cSLiam Girdwood 	struct snd_soc_dpcm *dpcm;
309201d7584cSLiam Girdwood 	int state;
3093a9764869SKaiChieh Chuang 	int ret = 1;
3094a9764869SKaiChieh Chuang 	unsigned long flags;
309501d7584cSLiam Girdwood 
3096a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3097d2e24d64SKuninori Morimoto 	for_each_dpcm_fe(be, stream, dpcm) {
309801d7584cSLiam Girdwood 
309901d7584cSLiam Girdwood 		if (dpcm->fe == fe)
310001d7584cSLiam Girdwood 			continue;
310101d7584cSLiam Girdwood 
310201d7584cSLiam Girdwood 		state = dpcm->fe->dpcm[stream].state;
310301d7584cSLiam Girdwood 		if (state == SND_SOC_DPCM_STATE_START ||
310401d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_PAUSED ||
310501d7584cSLiam Girdwood 			state == SND_SOC_DPCM_STATE_SUSPEND ||
3106a9764869SKaiChieh Chuang 			state == SND_SOC_DPCM_STATE_PREPARE) {
3107a9764869SKaiChieh Chuang 			ret = 0;
3108a9764869SKaiChieh Chuang 			break;
310901d7584cSLiam Girdwood 		}
3110a9764869SKaiChieh Chuang 	}
3111a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
311201d7584cSLiam Girdwood 
311301d7584cSLiam Girdwood 	/* it's safe to change hw_params */
3114a9764869SKaiChieh Chuang 	return ret;
311501d7584cSLiam Girdwood }
311601d7584cSLiam Girdwood EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3117f86dcef8SLiam Girdwood 
3118f86dcef8SLiam Girdwood #ifdef CONFIG_DEBUG_FS
311985280141SLars-Peter Clausen static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3120f86dcef8SLiam Girdwood {
3121f86dcef8SLiam Girdwood 	switch (state) {
3122f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_NEW:
3123f86dcef8SLiam Girdwood 		return "new";
3124f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_OPEN:
3125f86dcef8SLiam Girdwood 		return "open";
3126f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_PARAMS:
3127f86dcef8SLiam Girdwood 		return "hw_params";
3128f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PREPARE:
3129f86dcef8SLiam Girdwood 		return "prepare";
3130f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_START:
3131f86dcef8SLiam Girdwood 		return "start";
3132f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_STOP:
3133f86dcef8SLiam Girdwood 		return "stop";
3134f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_SUSPEND:
3135f86dcef8SLiam Girdwood 		return "suspend";
3136f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_PAUSED:
3137f86dcef8SLiam Girdwood 		return "paused";
3138f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_HW_FREE:
3139f86dcef8SLiam Girdwood 		return "hw_free";
3140f86dcef8SLiam Girdwood 	case SND_SOC_DPCM_STATE_CLOSE:
3141f86dcef8SLiam Girdwood 		return "close";
3142f86dcef8SLiam Girdwood 	}
3143f86dcef8SLiam Girdwood 
3144f86dcef8SLiam Girdwood 	return "unknown";
3145f86dcef8SLiam Girdwood }
3146f86dcef8SLiam Girdwood 
3147f86dcef8SLiam Girdwood static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3148f86dcef8SLiam Girdwood 				int stream, char *buf, size_t size)
3149f86dcef8SLiam Girdwood {
3150f86dcef8SLiam Girdwood 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3151f86dcef8SLiam Girdwood 	struct snd_soc_dpcm *dpcm;
3152f86dcef8SLiam Girdwood 	ssize_t offset = 0;
3153a9764869SKaiChieh Chuang 	unsigned long flags;
3154f86dcef8SLiam Girdwood 
3155f86dcef8SLiam Girdwood 	/* FE state */
3156f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset,
3157f86dcef8SLiam Girdwood 			"[%s - %s]\n", fe->dai_link->name,
3158f86dcef8SLiam Girdwood 			stream ? "Capture" : "Playback");
3159f86dcef8SLiam Girdwood 
3160f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "State: %s\n",
3161f86dcef8SLiam Girdwood 	                dpcm_state_string(fe->dpcm[stream].state));
3162f86dcef8SLiam Girdwood 
3163f86dcef8SLiam Girdwood 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3164f86dcef8SLiam Girdwood 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3165f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3166f86dcef8SLiam Girdwood 				"Hardware Params: "
3167f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
3168f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
3169f86dcef8SLiam Girdwood 				params_channels(params),
3170f86dcef8SLiam Girdwood 				params_rate(params));
3171f86dcef8SLiam Girdwood 
3172f86dcef8SLiam Girdwood 	/* BEs state */
3173f86dcef8SLiam Girdwood 	offset += snprintf(buf + offset, size - offset, "Backends:\n");
3174f86dcef8SLiam Girdwood 
3175f86dcef8SLiam Girdwood 	if (list_empty(&fe->dpcm[stream].be_clients)) {
3176f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3177f86dcef8SLiam Girdwood 				" No active DSP links\n");
3178f86dcef8SLiam Girdwood 		goto out;
3179f86dcef8SLiam Girdwood 	}
3180f86dcef8SLiam Girdwood 
3181a9764869SKaiChieh Chuang 	spin_lock_irqsave(&fe->card->dpcm_lock, flags);
31828d6258a4SKuninori Morimoto 	for_each_dpcm_be(fe, stream, dpcm) {
3183f86dcef8SLiam Girdwood 		struct snd_soc_pcm_runtime *be = dpcm->be;
3184f86dcef8SLiam Girdwood 		params = &dpcm->hw_params;
3185f86dcef8SLiam Girdwood 
3186f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3187f86dcef8SLiam Girdwood 				"- %s\n", be->dai_link->name);
3188f86dcef8SLiam Girdwood 
3189f86dcef8SLiam Girdwood 		offset += snprintf(buf + offset, size - offset,
3190f86dcef8SLiam Girdwood 				"   State: %s\n",
3191f86dcef8SLiam Girdwood 				dpcm_state_string(be->dpcm[stream].state));
3192f86dcef8SLiam Girdwood 
3193f86dcef8SLiam Girdwood 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3194f86dcef8SLiam Girdwood 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3195f86dcef8SLiam Girdwood 			offset += snprintf(buf + offset, size - offset,
3196f86dcef8SLiam Girdwood 				"   Hardware Params: "
3197f86dcef8SLiam Girdwood 				"Format = %s, Channels = %d, Rate = %d\n",
3198f86dcef8SLiam Girdwood 				snd_pcm_format_name(params_format(params)),
3199f86dcef8SLiam Girdwood 				params_channels(params),
3200f86dcef8SLiam Girdwood 				params_rate(params));
3201f86dcef8SLiam Girdwood 	}
3202a9764869SKaiChieh Chuang 	spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3203f86dcef8SLiam Girdwood out:
3204f86dcef8SLiam Girdwood 	return offset;
3205f86dcef8SLiam Girdwood }
3206f86dcef8SLiam Girdwood 
3207f86dcef8SLiam Girdwood static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3208f86dcef8SLiam Girdwood 				size_t count, loff_t *ppos)
3209f86dcef8SLiam Girdwood {
3210f86dcef8SLiam Girdwood 	struct snd_soc_pcm_runtime *fe = file->private_data;
3211f86dcef8SLiam Girdwood 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3212f86dcef8SLiam Girdwood 	char *buf;
3213f86dcef8SLiam Girdwood 
3214f86dcef8SLiam Girdwood 	buf = kmalloc(out_count, GFP_KERNEL);
3215f86dcef8SLiam Girdwood 	if (!buf)
3216f86dcef8SLiam Girdwood 		return -ENOMEM;
3217f86dcef8SLiam Girdwood 
3218467fece8SKuninori Morimoto 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
3219f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3220f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
3221f86dcef8SLiam Girdwood 
3222467fece8SKuninori Morimoto 	if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
3223f86dcef8SLiam Girdwood 		offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3224f86dcef8SLiam Girdwood 					buf + offset, out_count - offset);
3225f86dcef8SLiam Girdwood 
3226f86dcef8SLiam Girdwood 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3227f86dcef8SLiam Girdwood 
3228f86dcef8SLiam Girdwood 	kfree(buf);
3229f86dcef8SLiam Girdwood 	return ret;
3230f86dcef8SLiam Girdwood }
3231f86dcef8SLiam Girdwood 
3232f86dcef8SLiam Girdwood static const struct file_operations dpcm_state_fops = {
3233f57b8488SLiam Girdwood 	.open = simple_open,
3234f86dcef8SLiam Girdwood 	.read = dpcm_state_read_file,
3235f86dcef8SLiam Girdwood 	.llseek = default_llseek,
3236f86dcef8SLiam Girdwood };
3237f86dcef8SLiam Girdwood 
32382e55b90aSLars-Peter Clausen void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3239f86dcef8SLiam Girdwood {
3240b3bba9a1SMark Brown 	if (!rtd->dai_link)
32412e55b90aSLars-Peter Clausen 		return;
3242b3bba9a1SMark Brown 
3243596becd3SKuninori Morimoto 	if (!rtd->dai_link->dynamic)
3244596becd3SKuninori Morimoto 		return;
3245596becd3SKuninori Morimoto 
32466553bf06SLars-Peter Clausen 	if (!rtd->card->debugfs_card_root)
32476553bf06SLars-Peter Clausen 		return;
3248f86dcef8SLiam Girdwood 
3249f86dcef8SLiam Girdwood 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3250f86dcef8SLiam Girdwood 			rtd->card->debugfs_card_root);
3251f86dcef8SLiam Girdwood 
3252f1e3f409SFabio Estevam 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3253f86dcef8SLiam Girdwood 			    rtd, &dpcm_state_fops);
3254f86dcef8SLiam Girdwood }
3255f86dcef8SLiam Girdwood #endif
3256